26#include <QtCore/QFile>
27#include <QtCore/QLocale>
28#include <QtCore/QString>
29#include <QtCore/QTextStream>
32 #include <QStringConverter>
35#define XMLNS_BASE "http://www.hydrogen-music.org/"
36#define XMLNS_XSI "http://www.w3.org/2001/XMLSchema-instance"
46 XMLNode node = this->ownerDocument().createElement( name );
54 ERRORLOG( QString(
"try to read %1 XML node from an empty parent %2." )
55 .arg( node ).arg( nodeName() ) );
58 QDomElement el = firstChildElement( node );
60 if ( !inexistent_ok && ! bSilent ) {
61 WARNINGLOG( QString(
"XML node %1->%2 should exists." )
62 .arg( nodeName() ).arg( node ) );
66 if( el.text().isEmpty() ) {
67 if( !empty_ok && ! bSilent ) {
68 WARNINGLOG( QString(
"XML node %1->%2 should not be empty." )
69 .arg( nodeName() ).arg( node ) );
76QString
XMLNode::read_string(
const QString& node,
const QString& sDefaultValue,
bool inexistent_ok,
bool empty_ok,
bool bSilent )
78 QString sText =
read_child_node( node, inexistent_ok, empty_ok, bSilent );
79 if ( sText.isNull() && ! sDefaultValue.isEmpty() ) {
81 WARNINGLOG( QString(
"Using default value %1 for %2" )
82 .arg( sDefaultValue ).arg( node ) );
88QColor
XMLNode::read_color(
const QString& node,
const QColor& default_value,
bool inexistent_ok,
bool empty_ok,
bool bSilent )
90 QString text =
read_child_node( node, inexistent_ok, empty_ok, bSilent );
92 if ( ! text.isEmpty() ) {
93 QStringList textList = text.split( QLatin1Char(
',' ) );
94 if ( textList.size() != 3 ) {
96 WARNINGLOG( QString(
"Invalid color format [%1] for node [%2]" )
97 .arg( default_value.name() ).arg( node ) );
102 QColor color( textList[ 0 ].toInt(), textList[ 1 ].toInt(), textList[ 2 ].toInt() );
103 if ( ! color.isValid() ) {
105 WARNINGLOG( QString(
"Invalid color values [%1] for node [%2]" )
106 .arg( default_value.name() ).arg( node ) );
108 return default_value;
114 WARNINGLOG( QString(
"Using default value [%1] for node [%2]" )
115 .arg( default_value.name() ).arg( node ) );
117 return default_value;
120float XMLNode::read_float(
const QString& node,
float default_value,
bool inexistent_ok,
bool empty_ok,
bool bSilent )
122 QString ret =
read_child_node( node, inexistent_ok, empty_ok, bSilent );
125 WARNINGLOG( QString(
"Using default value %1 for %2" )
126 .arg( default_value ).arg( node ) );
128 return default_value;
130 QLocale c_locale = QLocale::c();
131 return c_locale.toFloat( ret );
134float XMLNode::read_float(
const QString& node,
float default_value,
bool *pFound,
bool inexistent_ok,
bool empty_ok,
bool bSilent )
136 QString ret =
read_child_node( node, inexistent_ok, empty_ok, bSilent );
139 WARNINGLOG( QString(
"Using default value %1 for %2" )
140 .arg( default_value ).arg( node ) );
143 return default_value;
146 QLocale c_locale = QLocale::c();
147 return c_locale.toFloat( ret );
151int XMLNode::read_int(
const QString& node,
int default_value,
bool inexistent_ok,
bool empty_ok,
bool bSilent )
153 QString ret =
read_child_node( node, inexistent_ok, empty_ok, bSilent );
156 WARNINGLOG( QString(
"Using default value %1 for %2" )
157 .arg( default_value ).arg( node ) );
159 return default_value;
161 QLocale c_locale = QLocale::c();
162 return c_locale.toInt( ret );
165bool XMLNode::read_bool(
const QString& node,
bool default_value,
bool inexistent_ok,
bool empty_ok,
bool bSilent )
167 QString ret =
read_child_node( node, inexistent_ok, empty_ok, bSilent );
170 WARNINGLOG( QString(
"Using default value %1 for %2" )
171 .arg( default_value ).arg( node ) );
173 return default_value;
182bool XMLNode::read_bool(
const QString& node,
bool default_value,
bool* pFound,
bool inexistent_ok,
bool empty_ok,
bool bSilent )
184 QString ret =
read_child_node( node, inexistent_ok, empty_ok, bSilent );
188 WARNINGLOG( QString(
"Using default value %1 for %2" )
189 .arg( default_value ).arg( node ) );
191 return default_value;
204 QString text = toElement().text();
205 if ( !empty_ok && text.isEmpty() && ! bSilent ) {
206 WARNINGLOG( QString(
"XML node %1 should not be empty." ).arg( nodeName() ) );
211QString
XMLNode::read_attribute(
const QString& attribute,
const QString& default_value,
bool inexistent_ok,
bool empty_ok,
bool bSilent )
213 QDomElement el = toElement();
214 if ( !inexistent_ok && !el.hasAttribute( attribute ) ) {
216 WARNINGLOG( QString(
"XML node %1 attribute %2 should exists." )
217 .arg( nodeName() ).arg( attribute ) );
219 return default_value;
221 QString attr = el.attribute( attribute );
222 if ( attr.isEmpty() ) {
223 if( !empty_ok && ! bSilent ) {
224 WARNINGLOG( QString(
"XML node %1 attribute %2 should not be empty." )
225 .arg( nodeName() ).arg( attribute ) );
229 WARNINGLOG( QString(
"Using default value %1 for attribute %2" )
230 .arg( default_value ).arg( attribute ) );
232 return default_value;
239 toElement().setAttribute( attribute, value );
244 QDomDocument doc = this->ownerDocument();
245 QDomElement el = doc.createElement( node );
246 QDomText txt = doc.createTextNode( text );
247 el.appendChild( txt );
248 this->appendChild( el );
258 .arg( color.green() )
259 .arg( color.blue() ) );
280 QFile file( sFilePath );
281 if ( !file.open( QIODevice::ReadOnly ) ) {
282 ERRORLOG( QString(
"Unable to open [%1] for reading" )
291 ERRORLOG( QString(
"Unable to read conversion result document [%1]" )
299 if ( ! setContent( &file ) ) {
300 ERRORLOG( QString(
"Unable to read XML document [%1]" )
313 QFile file( filepath );
314 if ( !file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ) ) {
315 ERRORLOG( QString(
"Unable to open %1 for writing" ).arg( filepath ) );
318 QTextStream out( &file );
319#ifdef H2CORE_HAVE_QT6
320 out.setEncoding( QStringConverter::Utf8 );
322 out.setCodec(
"UTF-8" );
324 out << toString().toUtf8();
328 if ( !toString().isEmpty() && file.size() == 0 ) {
338 QDomProcessingInstruction header = createProcessingInstruction(
"xml",
"version=\"1.0\" encoding=\"UTF-8\"" );
339 appendChild( header );
340 XMLNode root = createElement( node_name );
341 if ( !xmlns.isEmpty() ) {
342 QDomElement el = root.toElement();
344 el.setAttribute(
"xmlns:xsi",
XMLNS_XSI );
static bool checkTinyXMLCompatMode(QFile *pFile, bool bSilent=false)
static QByteArray convertFromTinyXML(QFile *pFile, bool bSilent=false)
XMLNode set_root(const QString &node_name, const QString &xmlns=nullptr)
create the xml header and root node
bool read(const QString &filepath, bool bSilent=false)
read the content of an xml file
bool write(const QString &filepath)
write itself into a file
XMLDoc()
basic constructor
XMLNode is a subclass of QDomNode with read and write values methods.
int read_int(const QString &node, int default_value, bool inexistent_ok=true, bool empty_ok=true, bool bSilent=false)
reads an integer stored into a child node
void write_attribute(const QString &attribute, const QString &value)
write a string as an attribute of the node
QString read_child_node(const QString &node, bool inexistent_ok, bool empty_ok, bool bSilent=false)
reads a string stored into a child node
bool read_bool(const QString &node, bool default_value, bool inexistent_ok=true, bool empty_ok=true, bool bSilent=false)
reads a boolean stored into a child node
QString read_attribute(const QString &attribute, const QString &default_value, bool inexistent_ok, bool empty_ok, bool bSilent=false)
reads an attribute from the node
QColor read_color(const QString &node, const QColor &defaultValue=QColor(97, 167, 251), bool inexistent_ok=true, bool empty_ok=true, bool bSilent=false)
void write_child_node(const QString &node, const QString &text)
write a string into a child node
QString read_string(const QString &node, const QString &default_value, bool inexistent_ok=true, bool empty_ok=true, bool bSilent=false)
reads a string stored into a child node
float read_float(const QString &node, float default_value, bool inexistent_ok=true, bool empty_ok=true, bool bSilent=false)
reads a float stored into a child node
void write_color(const QString &node, const QColor &color)
void write_float(const QString &node, const float value)
write a float into a child node
XMLNode createNode(const QString &name)
create a new XMLNode that has to be appended into de XMLDoc
XMLNode()
basic constructor
void write_string(const QString &node, const QString &value)
write a string into a child node
void write_bool(const QString &node, const bool value)
write a boolean into a child node
QString read_text(bool empty_ok, bool bSilent=false)
reads the text (content) from the node
void write_int(const QString &node, const int value)
write an integer into a child node