00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef XMLCLASS_H
00030 #define XMLCLASS_H
00031
00032 #include "QGpCoreToolsDLLExport.h"
00033 #include "XMLStream.h"
00034 #include "XMLContext.h"
00035 #include "XMLMember.h"
00036 #include "XMLSaveAttributes.h"
00037 #include "Translations.h"
00038 #include "StringSection.h"
00039
00040 namespace QGpCoreTools {
00041
00042 #define IMPL_XML_WRITE_PROPERTY(type, convert) \
00043 void XMLClass::writeProperty(XMLStream& s, QString name, type content) \
00044 { \
00045 TRACE; \
00046 QString tmp=s.indent(); \
00047 tmp+=openBeginTagStr; \
00048 tmp+=name; \
00049 tmp+=endTagStr; \
00050 tmp+=convert; \
00051 tmp+=closeBeginTagStr; \
00052 tmp+=name; \
00053 tmp+=endTagStr; \
00054 s << tmp << ::endl; \
00055 }
00056
00057 #define IMPL_XML_WRITE_PROPERTY_ATT(type, convert) \
00058 void XMLClass::writeProperty(XMLStream& s, QString name, const XMLSaveAttributes& attributes, type content) \
00059 { \
00060 TRACE; \
00061 QString tmp=s.indent(); \
00062 tmp+=openBeginTagStr; \
00063 tmp+=name; \
00064 tmp+=attributes.toEncodedString(); \
00065 tmp+=endTagStr; \
00066 tmp+=convert; \
00067 tmp+=closeBeginTagStr; \
00068 tmp+=name; \
00069 tmp+=endTagStr; \
00070 s << tmp << ::endl; \
00071 }
00072
00073 #define DECL_XML_WRITE_PROPERTY(type) \
00074 static void writeProperty(XMLStream& s, QString name, type content);
00075 #define DECL_XML_WRITE_PROPERTY_ATT(type) \
00076 static void writeProperty(XMLStream& s, QString name, const XMLSaveAttributes& attributes, type content);
00077
00078 #define XML_WRITEPROPERTIES_ARGS XMLStream& s, XMLContext * context
00079 #define XML_WRITECHILDREN_ARGS XMLStream& s, XMLContext * context
00080 #define XML_MEMBER_ARGS StringSection& tag, const XMLRestoreAttributes& attributes, XMLContext * context
00081 #define XML_SETPROPERTY_ARGS int memberID, StringSection& tag, const XMLRestoreAttributes& attributes, StringSection& content, XMLContext * context
00082 #define XML_POLISH_ARGS XMLContext * context
00083 #define XML_POLISHCHILD_ARGS XMLClass * child, XMLContext * context
00084 #define XML_POLISHED_ARGS
00085 #define XML_WRITEBINARYDATA_ARGS QDataStream& s, XMLContext * context
00086 #define XML_SETBINARYDATA_ARGS QDataStream& s, XMLContext * context
00087 #define XML_ATTRIBUTES_ARGS XMLSaveAttributes& attributes, XMLContext * context
00088 #define XML_SETATTRIBUTES_ARGS const XMLRestoreAttributes& attributes, XMLContext * context
00089
00090 typedef QHash<StringSection, StringSection> XMLRestoreAttributes;
00091 typedef QHash<StringSection, StringSection>::const_iterator XMLRestoreAttributeIterator;
00092
00093 class QGPCORETOOLS_EXPORT XMLClass
00094 {
00095 TRANSLATIONS( "XMLClass" )
00096 public:
00097 XMLClass() {}
00098 virtual ~XMLClass() {}
00099
00100 enum Error {
00101 NoError,
00102 ErrorFileNotOpen,
00103 ErrorWritingFile,
00104 ErrorNoDocType,
00105 ErrorNoVersion,
00106 ErrorUnmatchedAmpSemiColon,
00107 ErrorUnknowSpecialCharacter,
00108 ErrorEmptyTag,
00109 ErrorUnmatchedTag,
00110 ErrorUnmatchedTopLevelTag,
00111 ErrorEmptyContextStack,
00112 ErrorParsingContent,
00113 ErrorParsingAttributes,
00114 ErrorSettingAttributes,
00115 ErrorSettingBinaryData,
00116 ErrorWrongBinaryOffset,
00117 ErrorWrongBinaryTag,
00118 ErrorBinaryFileNotFound,
00119 ErrorEndTruncatedTag,
00120 ErrorEndTruncatedString,
00121 ErrorEndTruncatedContext,
00122 ErrorEndStillInside,
00123 ErrorEndTagNotFound
00124 };
00125
00126 enum FileType {
00127 TarFile,
00128 XmlFile
00129 };
00130
00131 void xml_save(XMLStream& s, XMLContext * context, const XMLSaveAttributes& attributes=nullSaveAttributes) const;
00132 XMLClass::Error restore(const QChar *& ptr, int& line, XMLStream * s, XMLContext * context);
00133
00134 inline virtual void xml_attributes(XML_ATTRIBUTES_ARGS) const;
00135 inline virtual bool xml_setAttributes(XML_SETATTRIBUTES_ARGS);
00136 inline virtual XMLMember xml_member(XML_MEMBER_ARGS);
00137 inline virtual void xml_polishChild(XML_POLISHCHILD_ARGS);
00138
00139 virtual const QString& xml_tagName() const=0;
00140 virtual bool xml_inherits(const QString& tagName) const;
00141
00142 static void skipBlanks(const QChar *& ptr, int& line);
00143 static QString saveSpecChar(QString content);
00144 static QString message(Error err, const QString& file=QString::null, int line=0);
00145
00146
00147 DECL_XML_WRITE_PROPERTY(const char *)
00148 DECL_XML_WRITE_PROPERTY(QString)
00149 DECL_XML_WRITE_PROPERTY(QChar)
00150 DECL_XML_WRITE_PROPERTY(double)
00151 DECL_XML_WRITE_PROPERTY(Complex)
00152 DECL_XML_WRITE_PROPERTY(int)
00153 DECL_XML_WRITE_PROPERTY(qint64)
00154 DECL_XML_WRITE_PROPERTY(bool)
00155 DECL_XML_WRITE_PROPERTY_ATT(const char *)
00156 DECL_XML_WRITE_PROPERTY_ATT(QString)
00157 DECL_XML_WRITE_PROPERTY_ATT(QChar)
00158 DECL_XML_WRITE_PROPERTY_ATT(double)
00159 DECL_XML_WRITE_PROPERTY_ATT(Complex)
00160 DECL_XML_WRITE_PROPERTY_ATT(int)
00161 DECL_XML_WRITE_PROPERTY_ATT(qint64)
00162 DECL_XML_WRITE_PROPERTY_ATT(bool)
00163 static void writeProperty(XMLStream& s, QString name, const XMLSaveAttributes& attributes);
00164 static void writeChildren(XMLStream& s, QString name, QString children) {
00165 writeChildren(s, name, nullSaveAttributes, children);
00166 }
00167 static void writeChildren(XMLStream& s, QString name, const XMLSaveAttributes& attributes, QString children);
00168 void writeBinaryData(XMLStream& s, XMLContext * context) const;
00169
00170
00171 inline virtual void xml_writeProperties(XML_WRITEPROPERTIES_ARGS) const;
00172 inline virtual void xml_writeChildren(XML_WRITECHILDREN_ARGS) const;
00173 inline virtual bool xml_setProperty(XML_SETPROPERTY_ARGS);
00174 inline virtual void xml_polish(XML_POLISH_ARGS);
00175 inline virtual bool xml_polished(XML_POLISHED_ARGS);
00176 inline virtual void xml_writeBinaryData(XML_WRITEBINARYDATA_ARGS) const;
00177 inline virtual bool xml_setBinaryData(XML_SETBINARYDATA_ARGS);
00178 inline virtual bool xml_setBinaryData200411(XML_SETBINARYDATA_ARGS) {return xml_setBinaryData(s, context);}
00179
00180 static const QString openBeginTagStr;
00181 static const QString closeBeginTagStr;
00182 static const QString endTagStr;
00183 static const QString closeEndTagStr;
00184 static const QString trueStr;
00185 static const QString falseStr;
00186 static const QString binDataTag;
00187 static const QString binData200510Tag;
00188 protected:
00189 static void qobject_writeProperties(const QObject * o, const XMLClass * xmlo, XML_WRITEPROPERTIES_ARGS);
00190 static XMLMember qobject_member(QObject * o, XML_MEMBER_ARGS);
00191 static bool qobject_setProperty(QObject * o, XML_SETPROPERTY_ARGS);
00192 private:
00193 StringSection getPropertyName(const StringSection& tag, int& line);
00194
00195 static int fileIndex;
00196 static const XMLSaveAttributes nullSaveAttributes;
00197 };
00198
00199 inline void XMLMember::release()
00200 {
00201 if(_child && _memberID==TemporaryChild) {
00202 delete _child;
00203 _child=0;
00204 }
00205 if(_context) {
00206 delete _context;
00207 }
00208 }
00209
00210 inline void XMLClass::xml_writeProperties(XML_WRITEPROPERTIES_ARGS) const
00211 {
00212 Q_UNUSED(s);
00213 Q_UNUSED(context);
00214 }
00215
00216 inline void XMLClass::xml_writeChildren(XML_WRITECHILDREN_ARGS) const
00217 {
00218 Q_UNUSED(s);
00219 Q_UNUSED(context);
00220 }
00221
00222 inline XMLMember XMLClass::xml_member(XML_MEMBER_ARGS)
00223 {
00224 Q_UNUSED(tag);
00225 Q_UNUSED(attributes);
00226 Q_UNUSED(context);
00227 return XMLMember(XMLMember::Unknown);
00228 }
00229
00230 inline bool XMLClass::xml_setProperty(XML_SETPROPERTY_ARGS)
00231 {
00232 Q_UNUSED(memberID);
00233 Q_UNUSED(tag);
00234 Q_UNUSED(attributes);
00235 Q_UNUSED(content);
00236 Q_UNUSED(context);
00237 return false;
00238 }
00239
00240 inline void XMLClass::xml_polish(XML_POLISH_ARGS)
00241 {
00242 Q_UNUSED(context);
00243 }
00244
00245 inline void XMLClass::xml_polishChild(XML_POLISHCHILD_ARGS)
00246 {
00247 Q_UNUSED(child);
00248 Q_UNUSED(context);
00249 }
00250
00251 inline bool XMLClass::xml_polished(XML_POLISHED_ARGS)
00252 {
00253 return false;
00254 }
00255
00256 inline void XMLClass::xml_writeBinaryData(XML_WRITEBINARYDATA_ARGS) const
00257 {
00258 Q_UNUSED(s);
00259 Q_UNUSED(context);
00260 }
00261
00262 inline bool XMLClass::xml_setBinaryData(XML_SETBINARYDATA_ARGS)
00263 {
00264 Q_UNUSED(s);
00265 Q_UNUSED(context);
00266 return false;
00267 }
00268
00269 inline void XMLClass::xml_attributes(XML_ATTRIBUTES_ARGS) const
00270 {
00271 Q_UNUSED(attributes);
00272 Q_UNUSED(context);
00273 }
00274
00275 inline bool XMLClass::xml_setAttributes(XML_SETATTRIBUTES_ARGS)
00276 {
00277 Q_UNUSED(attributes);
00278 Q_UNUSED(context);
00279 return true;
00280 }
00281
00282 inline QString XMLSaveAttributes::toEncodedString() const
00283 {
00284 QString tmp;
00285 for(const_iterator it=begin();it!=end();it++) {
00286 tmp += " ";
00287 tmp += it->key();
00288 tmp += "=\"";
00289 tmp += XMLClass::saveSpecChar(it->value());
00290 tmp += "\"";
00291 }
00292 return tmp;
00293 }
00294
00295 }
00296
00297 #endif // XMLCLASS_H