DNXMLElement::~DNXMLElement()
{
+ DNXMLElement *e = inner;
+ while (e && e != this && depth <= e->depth)
+ {
+ while(e->inner) e = e->inner;
+ DNXMLElement *ne = e->next ? e->next : e->outer;
+ delete e;
+ e = ne;
+ }
mAttributes.clear();
}
mAttributes.insert(std::map<std::string, std::string>::value_type( _key, _value));
}
+void DNXMLElement::setAttribute(std::string name, std::string value)
+{
+ mAttributes.insert(std::map<std::string, std::string>::value_type( name, value));
+}
+
std::string DNXMLElement::getAttributeValue(const char *name)
{
std::string key = name;
//
#include "qtdnxmlimpl.h"
-DNXMLImpl *DNXMLImpl::createXMLFromFileImpl(const char *docRoot, const char *path)
+#include "DNXMLElement.h"
+#include "TKLog.h"
+
+DNXMLImpl *DNXMLImpl::createXMLFromFileImpl(const char *docRoot, const char *docPath)
+{
+ QString qDocRoot = QString::fromLocal8Bit(docRoot);
+ QString qDocPath = QString::fromLocal8Bit(docPath);
+
+ if (qDocRoot.lastIndexOf("/") != qDocRoot.length()-1 && qDocPath.at(0) != QChar('/'))
+ {
+ qDocRoot.append("/");
+ }
+ QString filePath = qDocRoot;
+ filePath.append(qDocPath);
+
+ QFile file(filePath);
+ if (file.open(QFile::ReadOnly | QFile::Text)) {
+ QXmlInputSource xmlInputSource(&file);
+ return new QtDNXMLImpl(&xmlInputSource);
+ }
+ else
+ {
+ TKLog::printf("ERROR failed to load file.%s/%s", docRoot,docPath);
+ return NULL;
+ }
+}
+
+QtDNXMLImpl::QtDNXMLImpl(QXmlInputSource *input)
+{
+ mHandler = new QtXMLHandler();
+ QXmlSimpleReader reader;
+ reader.setContentHandler(mHandler);
+ reader.setErrorHandler(mHandler);
+
+ reader.parse(input,true);
+}
+
+QtDNXMLImpl::~QtDNXMLImpl()
+{
+ if (mHandler)
+ delete mHandler;
+}
+
+DNXMLElement* QtDNXMLImpl::getRoot()
+{
+ if (mHandler)
+ {
+ return mHandler->getRoot();
+ }
+ else
+ {
+ return NULL;
+ }
+}
+
+QtXMLHandler::QtXMLHandler() : mRootElement(NULL), mCurrentElement(NULL)
+{
+}
+
+QtXMLHandler::~QtXMLHandler()
{
- //TODO
- return 0;
+ if (mRootElement)
+ {
+ delete mRootElement;
+ }
}
-QtDNXMLImpl::QtDNXMLImpl()
+//QXML handler implementation
+bool QtXMLHandler::startDocument()
{
+ return true;
}
+
+bool QtXMLHandler::endDocument()
+{
+ return true;
+}
+
+bool QtXMLHandler::startElement(const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts )
+{
+ if (mRootElement == NULL)
+ {
+ mRootElement = new DNXMLElement(qName.toStdString());
+ mCurrentElement = mRootElement;
+ }
+ else
+ {
+ if (!mCurrentElement->inner)
+ {
+ mCurrentElement->inner = new DNXMLElement(qName.toStdString());
+ DNXMLElement *outer = mCurrentElement;
+ mCurrentElement = mCurrentElement->inner;
+ mCurrentElement->outer = outer;
+ mCurrentElement->depth = outer->depth++;
+ }
+ else
+ {
+ DNXMLElement *prev = mCurrentElement->inner;
+ prev->next = new DNXMLElement(qName.toStdString());
+ mCurrentElement = prev->next;
+ mCurrentElement->outer = prev->outer;
+ mCurrentElement->depth = prev->depth;
+ }
+ }
+
+ if (mCurrentElement)
+ {
+ for(int i = 0; i < atts.count(); i++)
+ {
+ QString qname = atts.qName(i);
+ QString value = atts.value(i);
+ mCurrentElement->setAttribute(qname.toStdString(), value.toStdString());
+ }
+
+ }
+ return true;
+}
+
+bool QtXMLHandler::endElement (const QString & namespaceURI, const QString & localName, const QString & qName )
+{
+ mCurrentElement = mCurrentElement->outer;
+ return true;
+}
+
+bool QtXMLHandler::characters (const QString & ch )
+{
+ mCurrentElement->text = ch.toStdString();
+ return true;
+}
+
+bool QtXMLHandler::error (const QXmlParseException & exception )
+{
+ std::string message = exception.message().toStdString();
+ TKLog::printf("Error while reading XML file!! \n%s", message.c_str());
+ return true;
+}
+
#define QTDNXMLIMPL_H
#include "DNXMLImpl.h"
+#include <QXmlDefaultHandler>
+
+class QtXMLHandler : public QXmlDefaultHandler
+{
+public:
+ QtXMLHandler();
+ virtual ~QtXMLHandler();
+
+ DNXMLElement *getRoot() { return mRootElement; }
+
+ //QXML handler implementation
+ virtual bool startDocument();
+ virtual bool endDocument();
+ virtual bool startElement(const QString & namespaceURI, const QString & localName, const QString & qName, const QXmlAttributes & atts );
+ virtual bool endElement (const QString & namespaceURI, const QString & localName, const QString & qName );
+ virtual bool characters (const QString & ch );
+ virtual bool error (const QXmlParseException & exception );
+
+private:
+ DNXMLElement *mRootElement;
+ DNXMLElement *mCurrentElement;
+
+};
class QtDNXMLImpl : public DNXMLImpl
{
public:
- QtDNXMLImpl();
+ QtDNXMLImpl(QXmlInputSource *input);
+ virtual ~QtDNXMLImpl();
+ virtual DNXMLElement *getRoot();
+
+private:
+ QtXMLHandler *mHandler;
};
+
#endif // QTDNXMLIMPL_H