Go to the documentation of this file.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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_XmlParser_cpp)
00056 #define ALIZE_XmlParser_cpp
00057
00058 #include "XmlParser.h"
00059 #include "MixtureGD.h"
00060 #include "MixtureGF.h"
00061
00062
00063
00064 using namespace alize;
00065
00066
00067 XmlParser::XmlParser()
00068 :Object() {}
00069
00070 void XmlParser::parse()
00071 {
00072
00073 test(readNextChar() == "<", ": first character must be '<'");
00074 parseElement("", readOneChar());
00075 }
00076
00077 void XmlParser::parseElement(String path, String s)
00078 {
00079 String tag, value;
00080
00081
00082 test(s != ">" && s != "<" && s != "\"" && !isASeparator(s), "");
00083 while (s != "/" && s != ">" && !isASeparator(s))
00084 {
00085 tag += s;
00086 s = readOneChar();
00087 }
00088 eventOpeningElement(path += "<" + tag + ">");
00089
00090 if (isASeparator(s))
00091 s = readNextChar();
00092
00093
00094
00095 while ( s != "/" && s != ">")
00096 {
00097 parseAttribute(path, s);
00098 s = readNextChar();
00099 }
00100
00101
00102
00103 if (s == "/")
00104 {
00105 test(readOneChar() == ">", ": character '>' expected after '/'");
00106 eventClosingElement(path, value);
00107 return;
00108 }
00109
00110
00111
00112 while (true)
00113 {
00114 while ( (s = readOneChar()) != "<")
00115 {
00116 if (s != "\r" && s != "\t" && s != "\n")
00117 value += s;
00118 }
00119 s = readOneChar();
00120
00121
00122
00123 if (s == "/")
00124 {
00125 s = readOneChar();
00126 test(s != ">", ": a tag cannot be empty");
00127 String closingElement;
00128 while (s != ">")
00129 {
00130 test(s != "/" && s != "\"" && s != "<" && !isASeparator(s),
00131 ": the tag contains an invalid character");
00132 closingElement += s;
00133 s = readOneChar();
00134 }
00135 test(tag == closingElement, " : End tag <" + closingElement +
00136 "> does not match the start tag <" + tag + ">");
00137
00138 eventClosingElement(path, value);
00139 return;
00140 }
00141 parseElement(path, s);
00142 }
00143 }
00144
00145 void XmlParser::parseAttribute(String path, String s)
00146 {
00147 String attribute, value;
00148 test(s != "\"" && s != "<" && s != "=", "");
00149 while (s != "=" && !isASeparator(s))
00150 {
00151 attribute += s;
00152 s = readOneChar();
00153 test(s != "/" && s != ">" && s != "<" && s != "\"" && s != "'",
00154 ": an attribute contain an invalid character");
00155 }
00156 eventOpeningElement(path += "<" + attribute + ">");
00157 if (isASeparator(s))
00158 test(readNextChar() == "=",
00159 ": Missing equals sign between attribute and attribute value");
00160 String quote = readNextChar();
00161 test(quote == "\"" || quote == "'", String(": a string literal was")
00162 + "expected, but no opening quote character was found");
00163 while ( (s = readOneChar()) != quote)
00164 value += s;
00165 eventClosingElement(path, value);
00166 }
00167
00168
00169
00170 const String& XmlParser::readNextChar()
00171 {
00172 while(true)
00173 {
00174 const String& s = readOneChar();
00175 if (!isASeparator(s))
00176 return s;
00177 }
00178 return readNextChar();
00179 }
00180
00181 bool XmlParser::isASeparator(String s) const
00182 { return s == " " || s == "\n" || s == "\t" ||s == "\r"; }
00183
00184 void XmlParser::test(bool v, const String& msg) { if (!v) eventError(msg); }
00185
00186 XmlParser::~XmlParser() {}
00187
00188
00189 #endif // !defined(ALIZE_XmlParser_cpp)
00190