1 #include <string>
2 #include <vector>
5 namespace MiniDom
6 {
7 typedef std::string DOMString;
8 typedef unsigned int XMLCh;
11 class Namespace
12 {
13 public:
14 Namespace()
15 {}
17 Namespace(const DOMString &prefixArg, const DOMString &namespaceURIArg)
18 {
19 prefix = prefixArg;
20 namespaceURI = namespaceURIArg;
21 }
23 Namespace(const Namespace &other)
24 {
25 prefix = other.prefix;
26 namespaceURI = other.namespaceURI;
27 }
29 virtual ~Namespace()
30 {}
32 virtual DOMString getPrefix()
33 { return prefix; }
35 virtual DOMString getNamespaceURI()
36 { return namespaceURI; }
38 protected:
40 DOMString prefix;
41 DOMString namespaceURI;
43 };
45 class Attribute
46 {
47 public:
48 Attribute()
49 {}
51 Attribute(const DOMString &nameArg, const DOMString &valueArg)
52 {
53 name = nameArg;
54 value = valueArg;
55 }
57 Attribute(const Attribute &other)
58 {
59 name = other.name;
60 value = other.value;
61 }
63 virtual ~Attribute()
64 {}
66 virtual DOMString getName()
67 { return name; }
69 virtual DOMString getValue()
70 { return value; }
72 protected:
74 DOMString name;
75 DOMString value;
77 };
80 class Element
81 {
82 friend class Parser;
84 public:
85 Element()
86 {
87 parent = NULL;
88 }
90 Element(const DOMString &nameArg)
91 {
92 parent = NULL;
93 name = nameArg;
94 }
96 Element(const DOMString &nameArg, const DOMString &valueArg)
97 {
98 parent = NULL;
99 name = nameArg;
100 value = valueArg;
101 }
103 Element(const Element &other)
104 {
105 parent = other.parent;
106 children = other.children;
107 attributes = other.attributes;
108 namespaces = other.namespaces;
109 name = other.name;
110 value = other.value;
111 }
113 virtual ~Element()
114 {
115 for (unsigned int i=0 ; i<children.size() ; i++)
116 delete children[i];
117 }
119 virtual DOMString getName()
120 { return name; }
122 virtual DOMString getValue()
123 { return value; }
125 Element *getParent()
126 { return parent; }
128 std::vector<Element *> getChildren()
129 { return children; }
131 std::vector<Element *> findElements(const DOMString &name);
133 DOMString getAttribute(const DOMString &name);
135 void addChild(Element *child);
137 void addAttribute(const DOMString &name, const DOMString &value);
139 void addNamespace(const DOMString &prefix, const DOMString &namespaceURI);
142 /**
143 * Prettyprint an XML tree to an output stream. Elements are indented
144 * according to element hierarchy.
145 * @param f a stream to receive the output
146 * @param elem the element to output
147 */
148 void writeIndented(FILE *f);
150 /**
151 * Prettyprint an XML tree to standard output. This is the equivalent of
152 * writeIndented(stdout).
153 * @param elem the element to output
154 */
155 void print();
157 protected:
160 void findElementsRecursive(std::vector<Element *>&res, const DOMString &name);
162 void writeIndentedRecursive(FILE *f, int indent);
164 Element *parent;
166 std::vector<Element *>children;
168 std::vector<Attribute> attributes;
169 std::vector<Namespace> namespaces;
171 DOMString name;
172 DOMString value;
174 };
180 class Parser
181 {
182 public:
183 Parser()
184 { init(); }
186 virtual ~Parser()
187 {}
189 /**
190 * Parse XML in a char buffer.
191 * @param buf a character buffer to parse
192 * @param pos position to start parsing
193 * @param len number of chars, from pos, to parse.
194 * @return a pointer to the root of the XML document;
195 */
196 Element *parse(const char *buf,int pos,int len);
198 /**
199 * Parse XML in a char buffer.
200 * @param buf a character buffer to parse
201 * @param pos position to start parsing
202 * @param len number of chars, from pos, to parse.
203 * @return a pointer to the root of the XML document;
204 */
205 Element *parse(const DOMString &buf);
207 /**
208 * Parse a named XML file. The file is loaded like a data file;
209 * the original format is not preserved.
210 * @param fileName the name of the file to read
211 * @return a pointer to the root of the XML document;
212 */
213 Element *parseFile(const char *fileName);
218 private:
220 void init()
221 {
222 keepGoing = true;
223 currentNode = NULL;
224 parselen = 0;
225 parsebuf = NULL;
226 currentPosition = 0;
227 }
229 void getLineAndColumn(long pos, long *lineNr, long *colNr);
231 void error(char *fmt, ...);
233 int peek(long pos);
235 int match(long pos, const char *text);
237 int skipwhite(long p);
239 int getWord(int p0, DOMString &buf);
241 int getQuoted(int p0, DOMString &buf, int do_i_parse);
243 int parseVersion(int p0);
245 int parseDoctype(int p0);
247 int parseElement(int p0, Element *par,int depth);
249 Element *parse(XMLCh *buf,int pos,int len);
251 bool keepGoing;
252 Element *currentNode;
253 long parselen;
254 XMLCh *parsebuf;
255 DOMString cdatabuf;
256 long currentPosition;
257 int colNr;
260 };
264 }//namespace MiniDom
267 //########################################################################
268 //# E N D O F F I L E
269 //########################################################################