00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef XMLSOMWRITER_H
00022 #define XMLSOMWRITER_H
00023
00024 #include <string>
00025 #include <stdexcept>
00026 #include <map>
00027 #include <libxml/encoding.h>
00028 #include <libxml/xmlwriter.h>
00029
00030 #define DEFAULT_ENCODING "ISO-8859-1"
00031
00032
00033 typedef enum {
00034
00035 AT_BEGINNING = 0,
00036
00037 AT_DOCUMENT,
00038
00039 AT_ELEMENT,
00040
00041 AT_END
00042
00043 } XMLWriterState;
00044
00045
00046 namespace xmlsom {
00047
00048
00052 class XMLSOMWriter {
00053
00054 public:
00055
00056 XMLSOMWriter(std::string fileName) throw(std::exception);
00057
00058 virtual void startDocument() throw(std::exception);
00059
00060 virtual void startElement( const std::string& name,
00061 const std::map<std::string,std::string>& attrs ) throw(std::exception);
00062
00063 virtual void startElement( const std::string& name ) throw(std::exception);
00064
00065 virtual void writeElement( const std::string& name,
00066 const std::string& value,
00067 const std::map<std::string,std::string>& attrs ) throw(std::exception);
00068
00069 virtual void writeElement( const std::string& name, const std::string& value ) throw(std::exception);
00070
00071
00072 virtual void endElement() throw(std::exception);
00073
00074 virtual void endDocument() throw(std::exception);
00075
00076 ~XMLSOMWriter();
00077
00078 protected:
00079
00080 void writeAttributes( const std::map<std::string,std::string>* attrs ) throw(std::exception);
00081
00082 std::string filename;
00083
00084 XMLWriterState status;
00085
00086 xmlTextWriterPtr writer;
00087
00088 };
00089
00090
00091 XMLSOMWriter::XMLSOMWriter(std::string fileName)
00092 throw(std::exception) : filename(fileName) {
00093
00094
00095 writer = xmlNewTextWriterFilename(filename.data(), 0);
00096
00097 xmlTextWriterSetIndent(writer, 3);
00098
00099 if (writer == NULL)
00100 throw std::invalid_argument("Error creating the xml writer");
00101
00102 status = AT_BEGINNING;
00103 };
00104
00105
00106
00107
00108 XMLSOMWriter::~XMLSOMWriter() {};
00109
00110
00111
00112
00113 void XMLSOMWriter::startDocument() throw(std::exception) {
00114
00115 if (status != AT_BEGINNING)
00116 throw std::invalid_argument("Invalid XMLWriter state to start a document");
00117
00118 status = AT_DOCUMENT;
00119
00120
00121 int rc = xmlTextWriterStartDocument(writer, 0, DEFAULT_ENCODING, 0);
00122
00123 if (rc < 0)
00124 throw std::invalid_argument("Error starting document");
00125 };
00126
00127
00128
00129
00130 void XMLSOMWriter::startElement( const std::string& name )
00131 throw(std::exception) {
00132
00133 if (status != AT_DOCUMENT)
00134 throw std::invalid_argument("Invalid XMLWriter state to start an element");
00135
00136 status = AT_ELEMENT;
00137
00138
00139 int rc = xmlTextWriterStartElement(writer, BAD_CAST name.data());
00140
00141 if (rc < 0)
00142 throw std::invalid_argument("Error starting element");
00143
00144 };
00145
00146
00147
00148
00149 void xmlsom::XMLSOMWriter::startElement( const std::string& name,
00150 const std::map<std::string,std::string>& attrs )
00151 throw(std::exception) {
00152
00153 this->startElement(name);
00154
00155
00156 writeAttributes(&attrs);
00157 };
00158
00159
00160
00161
00162 void XMLSOMWriter::writeElement( const std::string& name,
00163 const std::string& value )
00164 throw(std::exception) {
00165
00166 if (status != AT_DOCUMENT && status != AT_ELEMENT)
00167 throw std::invalid_argument("Invalid XMLWriter state to write an element");
00168
00169 int rc = xmlTextWriterWriteElement(writer, BAD_CAST name.data(),
00170 BAD_CAST value.data());
00171
00172 if (rc < 0)
00173 throw std::invalid_argument("Error writing element");
00174 };
00175
00176
00177
00178
00179
00180 void XMLSOMWriter::writeElement( const std::string& name,
00181 const std::string& value,
00182 const std::map<std::string,std::string>& attrs )
00183 throw(std::exception) {
00184
00185 if (status != AT_DOCUMENT && status != AT_ELEMENT)
00186 throw std::invalid_argument("Invalid XMLWriter state to write an element");
00187
00188 int rc = xmlTextWriterStartElement(writer, BAD_CAST name.data());
00189
00190
00191 writeAttributes(&attrs);
00192
00193 rc = xmlTextWriterWriteString(writer, BAD_CAST value.data());
00194
00195 rc = xmlTextWriterEndElement(writer);
00196
00197 if (rc < 0)
00198 throw std::invalid_argument("Error writing element");
00199 };
00200
00201
00202
00203
00204 void XMLSOMWriter::endElement() throw(std::exception) {
00205
00206 if (status != AT_ELEMENT)
00207 throw std::invalid_argument("Invalid XMLWriter state to finnish an element");
00208
00209 status = AT_DOCUMENT;
00210
00211 int rc = xmlTextWriterEndElement(writer);
00212
00213 if (rc < 0)
00214 throw std::invalid_argument("Error finnishing element");
00215 };
00216
00217
00218
00219
00220 void XMLSOMWriter::endDocument() throw(std::exception) {
00221
00222 if (status != AT_DOCUMENT)
00223 throw std::invalid_argument("Invalid XMLWriter state to finnish a document");
00224
00225 status = AT_END;
00226
00227 int rc = xmlTextWriterEndDocument(writer);
00228
00229 xmlFreeTextWriter(writer);
00230
00231 if (rc < 0)
00232 throw std::invalid_argument("Error finnishing document");
00233 };
00234
00235
00236
00237
00238
00239 void XMLSOMWriter::writeAttributes( const std::map<std::string,std::string>* attrs )
00240 throw(std::exception) {
00241
00242 if (attrs != 0) {
00243
00244 int rc = 0;
00245 std::map<std::string,std::string>::const_iterator it;
00246
00247 for(it = attrs->begin() ; it != attrs->end() ; it++ ) {
00248
00249 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST it->first.data(),
00250 BAD_CAST it->second.data());
00251 if (rc < 0)
00252 throw std::invalid_argument("Error writing attribute");
00253 }
00254 }
00255 };
00256
00257
00258 };
00259
00260
00261 #endif
00262
00263