[rkward-cvs] rkward/rkward/misc xmlhelper.cpp,NONE,1.1 xmlhelper.h,NONE,1.1
Thomas Friedrichsmeier
tfry at users.sourceforge.net
Sun May 8 16:55:46 UTC 2005
Update of /cvsroot/rkward/rkward/rkward/misc
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25486
Added Files:
xmlhelper.cpp xmlhelper.h
Log Message:
Adding a class with XML-parsing helper functions. Neither used nor tested, yet.
--- NEW FILE: xmlhelper.cpp ---
/***************************************************************************
xmlhelper.cpp - description
-------------------
begin : Fri May 6 2005
copyright : (C) 2005 by Thomas Friedrichsmeier
email : tfry at users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "xmlhelper.h"
#include <klocale.h>
#include <qstringlist.h>
#include <qfile.h>
#include "../debug.h"
//static
XMLHelper *XMLHelper::static_xml_helper=0;
XMLHelper::XMLHelper () {
highest_error = 0;
}
XMLHelper::~XMLHelper () {
}
//static
XMLHelper *XMLHelper::getStaticHelper () {
if (!static_xml_helper) {
static_xml_helper = new XMLHelper ();
}
return static_xml_helper;
}
QDomElement XMLHelper::openXMLFile (const QString &filename, int debug_level) {
int error_line, error_column;
QString error_message;
QDomDocument doc;
XMLHelper::filename = filename;
highest_error = 0;
QFile f (filename);
if (!f.open (IO_ReadOnly)) displayError (0, i18n("Could not open file for reading"), debug_level, DL_ERROR);
if (!doc.setContent(&f, false, &error_message, &error_line, &error_column)) {
displayError (0, i18n ("Error parsing XML-file. Error-message was: '%1' in line '%2', column '%3'. Expect further errors to be reported below").arg (error_message).arg (QString::number (error_line)).arg (QString::number (error_column)), debug_level, DL_ERROR);
}
f.close();
return doc.documentElement ();
}
QDomNodeList XMLHelper::getChildElements (const QDomElement &parent, const QString &name, int debug_level) {
QString convert;
if (!parent.isNull()) {
if (name != "") {
return (parent.elementsByTagName (name));
} else {
return (parent.childNodes ());
}
} else {
displayError (&parent, i18n ("Trying to retrieve children of invalid element"), debug_level);
}
QDomNodeList list;
return (list);
}
QString XMLHelper::getStringAttribute (const QDomElement &element, const QString &name, const QString &def, int debug_level) {
if (!element.hasAttribute (name)) {
displayError (&element, i18n ("'%1'-attribute not given. Assuming '%2'").arg (name).arg (def), debug_level);
return (def);
}
return (element.attribute (name));
}
bool XMLHelper::getBoolAttribute (const QDomElement &element, const QString &name, bool def, int debug_level) {
QString defstring, res;
if (def) defstring = "true";
else defstring = "false";
res = getStringAttribute (element, name, defstring, debug_level);
if (res == "true") return true;
if (res == "false") return false;
displayError (&element, i18n ("Illegal attribute value. Allowed values are 'true' or 'false', only."), debug_level, DL_ERROR);
return def;
}
void XMLHelper::displayError (const QDomNode *in_node, const QString &message, int debug_level, int message_level) {
if (message_level < debug_level) message_level = debug_level;
if (highest_error < debug_level) highest_error = debug_level;
if ((RK_Debug_Flags & XML) && (message_level >= RK_Debug_Level)) {
QString backtrace = i18n ("XML-parsing '%1' ").arg (filename);
// create a "backtrace"
QStringList list;
QDomNode node_copy = *in_node;
while (!((node_copy.isDocument ()) || (node_copy.isNull ()))) {
list.prepend (node_copy.nodeName ());
node_copy = node_copy.parentNode ();
}
backtrace += list.join ("->");
RK_DO (qDebug ("%s: %s", backtrace.latin1 (), message.latin1 ()), XML, message_level);
}
}
--- NEW FILE: xmlhelper.h ---
/***************************************************************************
xmlhelper.h - description
-------------------
begin : Fri May 6 2005
copyright : (C) 2005 by Thomas Friedrichsmeier
email : tfry at users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef XMLHELPER_H
#define XMLHELPER_H
#include <qdom.h>
/** This class contains some convenience functions for parsing XML files (DOM). Usually you will use a static instance of this class (getStaticHelper ()), which will be created early in rkward initialization. The error-logs will be reset every time you open a new XML-file using openXMLFile (). This is fine as long as you are parsing files one by one instead of mixing several files. In the latter case you will want to create additional instances of XMLHelper (it's quite possible, this mechanism will be changed, but I want to get going before considering all implications ;-)).
The functions in this class provide error-messages for illegal/problematic input. Information on the error status of the last commands is provided. More documentation to come once the API is somewhat finalized.
Warning/Error messages will always be printed using the standard debugging framework (shown according to user settings).
@author Thomas Friedrichsmeier
*/
class XMLHelper {
public:
/** create an instance of XMLHelper. Usually you will use the instance returned by getStaticHelper () instead of creating a new instance. */
XMLHelper ();
/** destrcutor */
~XMLHelper ();
/** open the given filename (read-only) and do basic parsing. Internally, the file will be closed right away, so there is no need to call an additional closeFile-equivalent. Once the returned element (and any copies you make of it) goes out of scope, the entire element-tree allocated will be freed.
When calling this function, highestError () will be reset to 0.
@param filename the name of the file to parse
@param debug_level level of debug message to generate if opening/parsing fails
@returns the document-element of the file. */
QDomElement openXMLFile (const QString &filename, int debug_level);
/** returns all child elements with a given tag-name of the given parent
@param parent the element whose children to return
@param name the tag-name to look for (if none given, will return all children)
@param debug_level level of debug message to generate in case of failure
@returns a list of child elements (you'll have to call toElement () on the list items), in the order of occurence in the XML file */
QDomNodeList getChildElements (const QDomElement &parent, const QString &name, int debug_level);
/** returns the value of a string attribute
@param element the element whose attributes to search
@param name the name of the attribute to read
@param def default value to return if no such attribute is given
@param debug_level level of debug message to generate in case of failure (i.e. no such attribute was found)
@returns the value of the given attribute or the given default */
QString getStringAttribute (const QDomElement &element, const QString &name, const QString &def, int debug_level);
/** returns the value of a boolean attribute ("true" or "false")
@param element the element whose attributes to search
@param name the name of the attribute to read
@param def default value to return if no such attribute is given
@param debug_level level of debug message to generate in case of failure (i.e. no such attribute was found. Note that if the given attribute is found, but is not a valid boolean, an error-message will be shown regardless of this setting, but highestError () will still use debug_level)
@returns true or false based on the value of the given attribute or the given default */
bool getBoolAttribute (const QDomElement &element, const QString &name, bool def, int debug_level);
/** @returns the level of the most severe error since the last call to openXMLFile () (based on the debug_level options passed to XMLHelper () */
int highestError () { return (highest_error); };
/** displays a custom-error message (also used internally by XMLHelper to display errors
@param in_node a pointer to the node/element to which the error relates (or 0). If given and non-zero, a "backtrace" of where the error is located will be generated
@param message the error-message to display
@param debug_level the debug level to show the message at (highestError () will be adujsted if applicable)
@param message_level sometime you may want to make sure your message is being shown even if it is not very important to your code. For instance, if there is a typo/illegal value in an optional setting, your code can continue using a reasonable default, but the user should still be notified of this error. If you omit this parameter or set it to something smaller that debug_level, debug_level will be used instead. */
void displayError (const QDomNode *in_node, const QString &message, int debug_level, int message_level=-1);
/** @returns a pointer to the default instance of XMLHelper (if none has been created so far, this will happen automatically. */
static XMLHelper *getStaticHelper ();
private:
int highest_error;
static XMLHelper *static_xml_helper;
QString filename;
};
#endif
More information about the rkward-tracker
mailing list