oopapidocs  2.0
qobjectreader.cpp
00001 #include <QFile>
00002 #include <QVariant>
00003 #include <QMetaProperty>
00004 #include <QDebug>
00005 #include <QTextStream>
00006 #include <QXmlInputSource>
00007 #include <QXmlSimpleReader>
00008 
00009 #include "objectfactory.h"
00010 #include "qobjectreader.h"
00011 
00012 
00013 
00014 //start id=setup
00015 QObjectReader::QObjectReader(QString filename, AbstractFactory* factory)
00016 : m_Factory(factory), m_Current(0) {
00017     parseFile(filename);
00018     if (m_Factory == 0) m_Factory = new ObjectFactory();
00019 }
00020 
00021 void QObjectReader::parseFile(QString filename) {
00022      if (!QFile::exists(filename)) {
00023          qDebug() << "XMLIMPORT: unable to open " << filename;
00024          return;
00025      }
00026     QFile xmlFile(filename);
00027     QXmlInputSource source( &xmlFile );
00028     QXmlSimpleReader reader;
00029     reader.setContentHandler( this );
00030     reader.parse( source );
00031 }
00032 
00033 void QObjectReader::parse(QString text) {
00034     QXmlInputSource source;
00035     source.setData(text);
00036     QXmlSimpleReader reader;
00037     reader.setContentHandler(this);
00038     reader.parse(source);
00039 }
00040 //end
00041 
00042 //start id=startelement
00043 bool QObjectReader::startElement( const QString&,
00044      const QString& elementName, const QString&,
00045      const QXmlAttributes& atts) {              /* No need to name unused parameters. */
00046     if (elementName == "object") {
00047         if (m_Current != 0)                     /* If already inside a <object>, */
00048             m_ParentStack.push(m_Current);      /* push previous current onto stack. */
00049         QString classname = atts.value("class");
00050         QString instancename = atts.value("name");
00051         m_Current = m_Factory->newObject(classname);
00052         m_Current->setObjectName(instancename);
00053         if (!m_ParentStack.empty()) {           /* Top of parentstack, or previous current, should be my parent. */           
00054             m_Current->setParent(m_ParentStack.top());
00055         }
00056         return true;
00057     }
00058     if (elementName == "property") {
00059         QString fieldType = atts.value("type");
00060         QString fieldName = atts.value("name");
00061         QString fieldValue = atts.value("value");
00062         QVariant qv = QVariant(fieldValue);
00063         m_Current->setProperty(fieldName.toAscii(), qv);
00064 
00065     }
00066     return true;
00067 }
00068 //end
00069 //start id="endelement"
00070 bool QObjectReader::endElement( const QString& ,
00071                             const QString& elementName,
00072                             const QString& ) {
00073     if (elementName == "object") {
00074         if (!m_ParentStack.empty())
00075             m_Current = m_ParentStack.pop();
00076         else {
00077             addCurrentToQueue();
00078         }
00079     }
00080     return true;
00081 }
00082 //end
00083 
00084 QObject* QObjectReader::getRoot() {
00085     if (m_ObjectList.isEmpty()) return 0;
00086     return m_ObjectList.dequeue();
00087 }
00088 
00089 bool QObjectReader:: endDocument() {
00090     //  qDebug() << "End of XML Document";
00091     return true;
00092 }
00093 
00094 
00095 void QObjectReader::addCurrentToQueue() {
00096     //  qDebug() << "Added Object to Queue";
00097     if (m_Current != 0) {
00098         m_ObjectList.enqueue(m_Current);
00099         m_Current = 0;
00100     }
00101 }
00102 
00103 
00104 QObjectReader::~QObjectReader() {}
00105 
 All Classes Namespaces Functions Enumerations