00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __XML_H__
00022 #define __XML_H__
00023
00024 #include <vector>
00025 #include <string>
00026 #include <iostream>
00027
00028 #include <map>
00029 #include <exception>
00030
00039 class xmlWriterException_c : public std::exception
00040 {
00041 private:
00042
00043 std::string text;
00044
00045 public:
00046
00047 xmlWriterException_c(const std::string & txt) : text(txt) {}
00048 ~xmlWriterException_c() throw() {}
00049
00050 const char * what(void) const throw() { return text.c_str(); }
00051
00052 };
00053
00055 class xmlWriter_c
00056 {
00057 private:
00058
00061 std::ostream & stream;
00062
00064 std::vector<std::string> tagStack;
00065
00067 enum {
00068 StBase,
00069 StInTag,
00070 StInContent,
00071 } state;
00072
00073 public:
00074
00076 xmlWriter_c(std::ostream & str);
00077
00079 ~xmlWriter_c(void);
00080
00082 void newTag(const std::string & name);
00083
00089 void newAttrib(const std::string & attrib, const std::string & value);
00090 void newAttrib(const std::string & attrib, unsigned long value);
00091 void newAttrib(const std::string & attrib, signed long value);
00092 void newAttrib(const std::string & attrib, unsigned int value) { newAttrib(attrib, (unsigned long)value); }
00093 void newAttrib(const std::string & attrib, signed int value) { newAttrib(attrib, (signed long)value); }
00095
00101 void endTag(const std::string & name);
00102
00105 void addContent(const std::string & text);
00106 void addContent(unsigned long val);
00107 void addContent(signed long val);
00108 void addContent(unsigned int val) { addContent((unsigned long)val); }
00109 void addContent(signed int val) { addContent((signed long)val); }
00118 std::ostream & addContent(void);
00120 };
00121
00122
00123
00125 class xmlParserException_c : public std::exception
00126 {
00127 public:
00128
00129 xmlParserException_c(std::string desc, std::string state, int line, int col);
00130
00131 xmlParserException_c(std::string desc);
00132
00133 ~xmlParserException_c() throw() {};
00134
00135 const char * what(void) const throw() { return description.c_str(); }
00136
00137 private:
00138
00139 std::string description;
00140 };
00141
00148 class xmlParser_c
00149 {
00150 public:
00151
00152 xmlParser_c(std::istream & is);
00153 xmlParser_c(void);
00154
00155 ~xmlParser_c(void);
00156
00157 std::string getInputEncoding(void);
00158
00159 void defineEntityReplacementText(std::string entity, std::string value);
00160
00161 int getDepth(void);
00162
00163 std::string getPositionDescription(void);
00164
00165 bool isWhitespace(void);
00166
00170 std::string getText(void);
00171
00172 const char *getTextCharacters(int *poslen);
00173
00175 std::string getName(void)
00176 {
00177 return name;
00178 }
00179
00180 std::string getPrefix(void)
00181 {
00182 return prefix;
00183 }
00184
00185 bool isEmptyElementTag(void);
00186
00188 int getAttributeCount(void)
00189 {
00190 return attributeCount;
00191 }
00192
00194 std::string getAttributeName(int index);
00195 std::string getAttributePrefix(int index);
00197 std::string getAttributeValue(int index);
00198
00202 std::string getAttributeValue(std::string name);
00203
00204 int getEventType(void)
00205 {
00206 return type;
00207 }
00208
00210 int next(void);
00211 int nextToken(void);
00212 int nextTag(void);
00213 void prevTag(void);
00214
00222 void require(int type, std::string name);
00223
00224 std::string nextText(void);
00225
00230 void skipSubTree(void);
00231
00233 enum
00234 {
00235 START_DOCUMENT,
00236 END_DOCUMENT,
00237 START_TAG,
00238 END_TAG,
00239 TEXT,
00240 CDSECT,
00241 ENTITY_REF,
00242 IGNORABLE_WHITESPACE,
00243 PROCESSING_INSTRUCTION,
00244 COMMENT,
00245 DOCDECL,
00246 UNKNOWN
00247 };
00248
00252 void exception(std::string desc);
00253
00254 private:
00255
00256 void commonInit (void);
00257 void initBuf (void);
00258
00259
00260 std::string state (int eventType);
00261 std::string *ensureCapacity (std::string * arr, int required);
00262
00266 void nextImpl ();
00267 int parseLegacy (bool push);
00268
00269
00270
00272 void parseDoctype (bool push);
00273
00274
00275 void parseEndTag ();
00276 int peekType ();
00277 std::string get (int pos);
00278 void push (int c);
00279
00281 void parseStartTag (bool xmldecl);
00282
00286
00287 void pushEntity ();
00288
00294 void pushText (int delimiter, bool resolveEntities);
00295 void read (char c);
00296 int read ();
00297
00299 int peekbuf (int pos);
00300 std::string readName ();
00301 void skip ();
00302 std::string unexpected_eof;
00303 std::string illegal_type;
00304 int LEGACY;
00305 int XML_DECL;
00306
00307
00308 std::string version;
00309 bool standalone;
00310
00311
00312 bool processNsp;
00313 bool relaxed;
00314 std::map < std::string, std::string > entityMap;
00315 int depth;
00316 std::vector < std::string > nspStack;
00317 std::vector < std::string > elementStack;
00318 int *nspCounts;
00319 int nspSize;
00320
00321
00322 std::string encoding;
00323 char *srcBuf;
00324 int srcPos;
00325 int srcCount;
00326 int srcBuflength;
00327
00328
00329 int line;
00330 int column;
00331
00332
00333 char *txtBuf;
00334 int txtPos;
00335 int txtBufSize;
00336
00337
00338 int type;
00339 std::string text;
00340 bool isWspace,skipNextTag;
00341 std::string Ns;
00342 std::string prefix;
00343 std::string name;
00344 bool degenerated;
00345 int attributeCount;
00346 std::vector < std::string > attributes;
00347
00348 std::istream & reader;
00349
00353 int peek[2];
00354 int peekCount;
00355 bool wasCR;
00356 bool unresolved;
00357 bool token;
00358 };
00359
00360
00361 #endif