| 1 | #ifndef _XML_DOCUMENT_MANAGING_H |
| 2 | #define _XML_DOCUMENT_MANAGING_H |
| 3 | |
| 4 | #include <vector> |
| 5 | #include <set> |
| 6 | #include <QtXml/QDomDocument> |
| 7 | #include<QtCore/QFile> |
| 8 | #include<QtCore/QVector> |
| 9 | #include <QtXml/QXmlStreamWriter> |
| 10 | #include <QtCore/QString> |
| 11 | |
| 12 | class XMLTag |
| 13 | { |
| 14 | public: |
| 15 | typedef std::pair<QString,QString> TagAttribute; |
| 16 | typedef QVector<TagAttribute> TagAttributes; |
| 17 | QString _tagname; |
| 18 | TagAttributes _attributes; |
| 19 | |
| 20 | XMLTag(const QString& tagname = QString(),const TagAttributes& attr = TagAttributes()) |
| 21 | :_tagname(tagname),_attributes(attr) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | virtual ~XMLTag() |
| 26 | { |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | class XMLLeafTag : public XMLTag |
| 31 | { |
| 32 | public: |
| 33 | QVector<QString> _text; |
| 34 | |
| 35 | XMLLeafTag(const QString& tagname = QString(),const QVector<QString>& text = QVector<QString>()) |
| 36 | :XMLTag(tagname),_text(text) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | virtual ~XMLLeafTag() |
| 41 | { |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | class XMLDocumentWriter; |
| 46 | |
| 47 | class Visitor; |
| 48 | |
| 49 | class XMLNode |
| 50 | { |
| 51 | public: |
| 52 | XMLNode(XMLTag* tag); |
| 53 | virtual ~XMLNode(); |
| 54 | |
| 55 | virtual void applyProcedure(Visitor& v) = 0; |
| 56 | |
| 57 | XMLTag* _tag; |
| 58 | }; |
| 59 | |
| 60 | class XMLInteriorNode : public XMLNode |
| 61 | { |
| 62 | public: |
| 63 | XMLInteriorNode(XMLTag* tag); |
| 64 | |
| 65 | XMLNode* son(int ii); |
| 66 | |
| 67 | QVector< XMLNode* > sons(); |
| 68 | |
| 69 | void applyProcedure(Visitor& v); |
| 70 | |
| 71 | ~XMLInteriorNode(); |
| 72 | |
| 73 | QVector< XMLNode* > _sons; |
| 74 | }; |
| 75 | |
| 76 | class XMLLeafNode : public XMLNode |
| 77 | { |
| 78 | public: |
| 79 | XMLLeafNode(XMLLeafTag* leaftag); |
| 80 | |
| 81 | void applyProcedure(Visitor& v); |
| 82 | virtual ~XMLLeafNode(); |
| 83 | }; |
| 84 | |
| 85 | class XMLDocument |
| 86 | { |
| 87 | public: |
| 88 | XMLDocument(XMLInteriorNode* root) |
| 89 | :_root(root) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | ~XMLDocument() |
| 94 | { |
| 95 | delete (_root); |
| 96 | } |
| 97 | |
| 98 | XMLInteriorNode* _root; |
| 99 | }; |
| 100 | |
| 101 | |
| 102 | class Visitor |
| 103 | { |
| 104 | public: |
| 105 | virtual void operator()(XMLLeafNode& leaf) = 0; |
| 106 | virtual void operator()(XMLInteriorNode& intnode) = 0; |
| 107 | }; |
| 108 | |
| 109 | |
| 110 | class XMLDocumentWriter : public Visitor |
| 111 | { |
| 112 | private: |
| 113 | QXmlStreamWriter _stream; |
| 114 | QFile _file; |
| 115 | bool _error; |
| 116 | |
| 117 | void writeText(XMLLeafNode& node) |
| 118 | { |
| 119 | XMLLeafTag* leaftag = static_cast<XMLLeafTag*>(node._tag); |
| 120 | for(QVector<QString>::iterator it = leaftag->_text.begin();it != leaftag->_text.end();++it) |
| 121 | { |
| 122 | QString tmp = ""; |
| 123 | if (it != leaftag->_text.begin()) |
| 124 | tmp = QString(" "); |
| 125 | _stream.writeCharacters(tmp + *it); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void writeAttributes(XMLNode& node) |
| 130 | { |
| 131 | QXmlStreamAttributes attr; |
| 132 | for(XMLTag::TagAttributes::iterator it = node._tag->_attributes.begin();it != node._tag->_attributes.end();++it) |
| 133 | attr.append(it->first,it->second); |
| 134 | _stream.writeAttributes(attr); |
| 135 | } |
| 136 | |
| 137 | void recursiveStep(XMLInteriorNode& intnode) |
| 138 | { |
| 139 | QVector< XMLNode* > sons = intnode.sons(); |
| 140 | for(QVector< XMLNode* >::iterator its = sons.begin();its != sons.end();++its) |
| 141 | (*its)->applyProcedure(*this); |
| 142 | } |
| 143 | |
| 144 | public: |
| 145 | |
| 146 | void operator()(XMLLeafNode& node) |
| 147 | { |
| 148 | _stream.writeStartElement(node._tag->_tagname); |
| 149 | writeAttributes(node); |
| 150 | writeText(node); |
| 151 | _stream.writeEndElement(); |
| 152 | } |
| 153 | |
| 154 | void operator()(XMLInteriorNode& intnode) |
| 155 | { |
| 156 | _stream.writeStartElement(intnode._tag->_tagname); |
| 157 | writeAttributes(intnode); |
| 158 | recursiveStep(intnode); |
| 159 | _stream.writeEndElement(); |
| 160 | } |
| 161 | |
| 162 | void operator()(XMLDocument& doc) |
| 163 | { |
| 164 | _stream.writeStartDocument(); |
| 165 | operator()(*(doc._root)); |
| 166 | _stream.writeEndDocument(); |
| 167 | } |
| 168 | |
| 169 | void write(XMLDocument& doc) |
| 170 | { |
| 171 | (*this)(doc); |
| 172 | } |
| 173 | |
| 174 | bool isReliable() const |
| 175 | { |
| 176 | return !_error; |
| 177 | } |
| 178 | |
| 179 | XMLDocumentWriter(const char* filename,const bool autoformatting = true) |
| 180 | :_stream(),_file(filename),_error(false) |
| 181 | { |
| 182 | if (!_file.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 183 | _error = true; |
| 184 | _stream.setDevice(&_file); |
| 185 | _stream.setAutoFormatting(autoformatting); |
| 186 | } |
| 187 | |
| 188 | ~XMLDocumentWriter() |
| 189 | { |
| 190 | _file.close(); |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | #endif |