00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef Tanl_TEXT_XmlReader_H
00025 #define Tanl_TEXT_XmlReader_H
00026
00027
00028 #include "FlexLexer.h"
00029
00030
00031 #include <fstream>
00032 #include <vector>
00033
00034
00035 #include "include/error.h"
00036
00037 namespace Tanl {
00038 namespace XML {
00039
00040 enum XmlTokenType {
00041 XML_DECL,
00042 PROC_INST,
00043 DOCTYPE,
00044 COMMENT,
00045 NOTATION,
00046 TAG_OPEN,
00047 TAG_CLOSE,
00048 TAG_EMPTY,
00049 TAG_END,
00050 ID,
00051 EQ,
00052 VAL,
00053 CHARACTERS,
00054 RAW_CHARS,
00055 WHITESPACE,
00056 EoF
00057 };
00058
00059 class XmlScanner : public yyFlexLexer
00060 {
00061 public:
00062 XmlScanner(std::istream* arg_yyin = 0, std::ostream* arg_yyout = 0) :
00063 yyFlexLexer(arg_yyin, arg_yyout)
00064 { }
00065
00069 int scan();
00070
00071 };
00072
00073 #define YY_DECL int Tanl::XML::XmlScanner::scan()
00074
00075 enum XmlNodeType
00076 {
00077 None,
00078 Attribute,
00079 CDATA,
00080 Comment,
00081 Document,
00082 DocumentType,
00083 Element,
00084 EndElement,
00085 Entity,
00086 EntityReference,
00087 Notation,
00088 ProcessingInstruction,
00089 Text,
00090 Whitespace,
00091 XmlDeclaration
00092 };
00093
00094 struct XmlException : public std::runtime_error {
00095 XmlException(char const* msg) : std::runtime_error(msg) {}
00096 };
00097
00098 class XmlReader
00099 {
00100 public:
00101
00102 XmlReader(std::istream& stream);
00103
00104 enum State {
00105 Initial,
00106 Interactive,
00107 Error,
00108 EndOfFile,
00109 Closed
00110 };
00111
00116 bool Read() throw(XmlException);
00117
00118 int AttributeCount() { return attrs.size(); }
00119
00120 bool MoveToFirstAttribute();
00121
00122 bool MoveToNextAttribute();
00124 bool Eof() { return state == EndOfFile; }
00125
00126 int Depth() { return depth; }
00127
00128 XmlNodeType NodeType;
00129 std::string Name;
00130 std::string Value;
00131 bool isEmptyElement;
00132
00133 struct NodeAttr {
00134 NodeAttr(std::string& name, std::string& value) :
00135 name(name),
00136 value(value)
00137 { }
00138 std::string name;
00139 std::string value;
00140 };
00141
00142 private:
00143 XmlScanner scanner;
00144 State state;
00145 int depth;
00146 std::string tagName;
00147 std::vector<NodeAttr> attrs;
00148 unsigned attrIndex;
00149 };
00150
00151 }
00152 }
00153
00154 #endif // Tanl_TEXT_XmlReader_H