1 #ifndef __PEDRODOM_H__
2 #define __PEDRODOM_H__
3 /*
4 * API for the Pedro mini-DOM parser and tree
5 *
6 * Authors:
7 * Bob Jamison
8 *
9 * Copyright (C) 2005-2006 Bob Jamison
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
26 #include <string>
27 #include <vector>
30 namespace Pedro
31 {
33 typedef std::string DOMString;
34 typedef unsigned int XMLCh;
37 class Namespace
38 {
39 public:
40 Namespace()
41 {}
43 Namespace(const DOMString &prefixArg, const DOMString &namespaceURIArg)
44 {
45 prefix = prefixArg;
46 namespaceURI = namespaceURIArg;
47 }
49 Namespace(const Namespace &other)
50 {
51 assign(other);
52 }
54 Namespace &operator=(const Namespace &other)
55 {
56 assign(other);
57 return *this;
58 }
60 virtual ~Namespace()
61 {}
63 virtual DOMString getPrefix()
64 { return prefix; }
66 virtual DOMString getNamespaceURI()
67 { return namespaceURI; }
69 protected:
71 void assign(const Namespace &other)
72 {
73 prefix = other.prefix;
74 namespaceURI = other.namespaceURI;
75 }
77 DOMString prefix;
78 DOMString namespaceURI;
80 };
82 class Attribute
83 {
84 public:
85 Attribute()
86 {}
88 Attribute(const DOMString &nameArg, const DOMString &valueArg)
89 {
90 name = nameArg;
91 value = valueArg;
92 }
94 Attribute(const Attribute &other)
95 {
96 assign(other);
97 }
99 Attribute &operator=(const Attribute &other)
100 {
101 assign(other);
102 return *this;
103 }
105 virtual ~Attribute()
106 {}
108 virtual DOMString getName()
109 { return name; }
111 virtual DOMString getValue()
112 { return value; }
114 protected:
116 void assign(const Attribute &other)
117 {
118 name = other.name;
119 value = other.value;
120 }
122 DOMString name;
123 DOMString value;
125 };
128 class Element
129 {
130 friend class Parser;
132 public:
133 Element()
134 {
135 parent = NULL;
136 }
138 Element(const DOMString &nameArg)
139 {
140 parent = NULL;
141 name = nameArg;
142 }
144 Element(const DOMString &nameArg, const DOMString &valueArg)
145 {
146 parent = NULL;
147 name = nameArg;
148 value = valueArg;
149 }
151 Element(const Element &other)
152 {
153 assign(other);
154 }
156 Element &operator=(const Element &other)
157 {
158 assign(other);
159 return *this;
160 }
162 virtual Element *clone();
164 virtual ~Element()
165 {
166 for (unsigned int i=0 ; i<children.size() ; i++)
167 delete children[i];
168 }
170 virtual DOMString getName()
171 { return name; }
173 virtual DOMString getValue()
174 { return value; }
176 Element *getParent()
177 { return parent; }
179 std::vector<Element *> getChildren()
180 { return children; }
182 std::vector<Element *> findElements(const DOMString &name);
184 DOMString getAttribute(const DOMString &name);
186 DOMString getTagAttribute(const DOMString &tagName, const DOMString &attrName);
188 DOMString getTagValue(const DOMString &tagName);
190 void addChild(Element *child);
192 void addAttribute(const DOMString &name, const DOMString &value);
194 void addNamespace(const DOMString &prefix, const DOMString &namespaceURI);
197 /**
198 * Prettyprint an XML tree to an output stream. Elements are indented
199 * according to element hierarchy.
200 * @param f a stream to receive the output
201 * @param elem the element to output
202 */
203 void writeIndented(FILE *f);
205 /**
206 * Prettyprint an XML tree to standard output. This is the equivalent of
207 * writeIndented(stdout).
208 * @param elem the element to output
209 */
210 void print();
212 protected:
214 void assign(const Element &other)
215 {
216 parent = other.parent;
217 children = other.children;
218 attributes = other.attributes;
219 namespaces = other.namespaces;
220 name = other.name;
221 value = other.value;
222 }
224 void findElementsRecursive(std::vector<Element *>&res, const DOMString &name);
226 void writeIndentedRecursive(FILE *f, int indent);
228 Element *parent;
230 std::vector<Element *>children;
232 std::vector<Attribute> attributes;
233 std::vector<Namespace> namespaces;
235 DOMString name;
236 DOMString value;
238 };
244 class Parser
245 {
246 public:
247 /**
248 * Constructor
249 */
250 Parser()
251 { init(); }
253 virtual ~Parser()
254 {}
256 /**
257 * Parse XML in a char buffer.
258 * @param buf a character buffer to parse
259 * @param pos position to start parsing
260 * @param len number of chars, from pos, to parse.
261 * @return a pointer to the root of the XML document;
262 */
263 Element *parse(const char *buf,int pos,int len);
265 /**
266 * Parse XML in a char buffer.
267 * @param buf a character buffer to parse
268 * @param pos position to start parsing
269 * @param len number of chars, from pos, to parse.
270 * @return a pointer to the root of the XML document;
271 */
272 Element *parse(const DOMString &buf);
274 /**
275 * Parse a named XML file. The file is loaded like a data file;
276 * the original format is not preserved.
277 * @param fileName the name of the file to read
278 * @return a pointer to the root of the XML document;
279 */
280 Element *parseFile(const char *fileName);
282 /**
283 * Utility method to preprocess a string for XML
284 * output, escaping its entities.
285 * @param str the string to encode
286 */
287 static DOMString encode(const DOMString &str);
290 private:
292 void init()
293 {
294 keepGoing = true;
295 currentNode = NULL;
296 parselen = 0;
297 parsebuf = NULL;
298 currentPosition = 0;
299 }
301 void getLineAndColumn(long pos, long *lineNr, long *colNr);
303 void error(char *fmt, ...);
305 int peek(long pos);
307 int match(long pos, const char *text);
309 int skipwhite(long p);
311 int getWord(int p0, DOMString &buf);
313 int getQuoted(int p0, DOMString &buf, int do_i_parse);
315 int parseVersion(int p0);
317 int parseDoctype(int p0);
319 int parseElement(int p0, Element *par,int depth);
321 Element *parse(XMLCh *buf,int pos,int len);
323 bool keepGoing;
324 Element *currentNode;
325 long parselen;
326 XMLCh *parsebuf;
327 DOMString cdatabuf;
328 long currentPosition;
329 int colNr;
331 };
335 }//namespace Pedro
338 #endif /* __PEDRODOM_H__ */
340 //########################################################################
341 //# E N D O F F I L E
342 //########################################################################