41af805fbeed8460b1d8f77844a7f829d011897a
1 #include <glib.h>
3 #include <string>
4 #include <vector>
7 namespace MiniDom
8 {
9 typedef std::string DOMString;
10 typedef unsigned int XMLCh;
13 class Namespace
14 {
15 public:
16 Namespace()
17 {}
19 Namespace(const DOMString &prefixArg, const DOMString &namespaceURIArg)
20 {
21 prefix = prefixArg;
22 namespaceURI = namespaceURIArg;
23 }
25 Namespace(const Namespace &other)
26 {
27 prefix = other.prefix;
28 namespaceURI = other.namespaceURI;
29 }
31 virtual ~Namespace()
32 {}
34 virtual DOMString getPrefix()
35 { return prefix; }
37 virtual DOMString getNamespaceURI()
38 { return namespaceURI; }
40 protected:
42 DOMString prefix;
43 DOMString namespaceURI;
45 };
47 class Attribute
48 {
49 public:
50 Attribute()
51 {}
53 Attribute(const DOMString &nameArg, const DOMString &valueArg)
54 {
55 name = nameArg;
56 value = valueArg;
57 }
59 Attribute(const Attribute &other)
60 {
61 name = other.name;
62 value = other.value;
63 }
65 virtual ~Attribute()
66 {}
68 virtual DOMString getName()
69 { return name; }
71 virtual DOMString getValue()
72 { return value; }
74 protected:
76 DOMString name;
77 DOMString value;
79 };
82 class Element
83 {
84 friend class Parser;
86 public:
88 Element()
89 {
90 parent = NULL;
91 }
93 Element(const DOMString &nameArg)
94 {
95 parent = NULL;
96 name = nameArg;
97 }
99 Element(const DOMString &nameArg, const DOMString &valueArg)
100 {
101 parent = NULL;
102 name = nameArg;
103 value = valueArg;
104 }
106 Element(const Element &other)
107 {
108 parent = other.parent;
109 children = other.children;
110 attributes = other.attributes;
111 namespaces = other.namespaces;
112 name = other.name;
113 value = other.value;
114 }
116 virtual ~Element()
117 {
118 for (unsigned int i=0 ; i<children.size() ; i++)
119 delete children[i];
120 }
122 virtual DOMString getName()
123 { return name; }
125 virtual DOMString getValue()
126 { return value; }
128 Element *getParent()
129 { return parent; }
131 std::vector<Element *> getChildren()
132 { return children; }
134 int getLine()
135 { return line; }
137 std::vector<Element *> findElements(const DOMString &name);
139 DOMString getAttribute(const DOMString &name);
141 void addChild(Element *child);
143 void addAttribute(const DOMString &name, const DOMString &value);
145 void addNamespace(const DOMString &prefix, const DOMString &namespaceURI);
148 /**
149 * Prettyprint an XML tree to an output stream. Elements are indented
150 * according to element hierarchy.
151 * @param f a stream to receive the output
152 * @param elem the element to output
153 */
154 void writeIndented(FILE *f);
156 /**
157 * Prettyprint an XML tree to standard output. This is the equivalent of
158 * writeIndented(stdout).
159 * @param elem the element to output
160 */
161 void print();
163 protected:
166 void findElementsRecursive(std::vector<Element *>&res, const DOMString &name);
168 void writeIndentedRecursive(FILE *f, int indent);
170 Element *parent;
172 std::vector<Element *>children;
174 std::vector<Attribute> attributes;
175 std::vector<Namespace> namespaces;
177 DOMString name;
178 DOMString value;
180 int line;
182 };
188 class Parser
189 {
190 public:
192 Parser()
193 { init(); }
195 virtual ~Parser()
196 {}
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 char *buf,int pos,int len);
207 /**
208 * Parse XML in a char buffer.
209 * @param buf a character buffer to parse
210 * @param pos position to start parsing
211 * @param len number of chars, from pos, to parse.
212 * @return a pointer to the root of the XML document;
213 */
214 Element *parse(const DOMString &buf);
216 /**
217 * Parse a named XML file. The file is loaded like a data file;
218 * the original format is not preserved.
219 * @param fileName the name of the file to read
220 * @return a pointer to the root of the XML document;
221 */
222 Element *parseFile(const char *fileName);
227 private:
229 void init()
230 {
231 keepGoing = true;
232 currentNode = NULL;
233 parselen = 0;
234 parsebuf = NULL;
235 currentPosition = 0;
236 }
238 int countLines(int begin, int end);
240 void getLineAndColumn(int pos, int *lineNr, int *colNr);
242 void error(char *fmt, ...) G_GNUC_PRINTF(2,3);
244 int peek(int pos);
246 int match(int pos, const char *text);
248 int skipwhite(int p);
250 int getWord(int p0, DOMString &buf);
252 int getQuoted(int p0, DOMString &buf, int do_i_parse);
254 int parseVersion(int p0);
256 int parseDoctype(int p0);
258 int parseElement(int p0, Element *par,int depth);
260 Element *parse(XMLCh *buf,int pos,int len);
262 bool keepGoing;
263 Element *currentNode;
264 int parselen;
265 XMLCh *parsebuf;
266 DOMString cdatabuf;
267 int currentPosition;
268 int colNr;
271 };
275 }//namespace MiniDom
278 //########################################################################
279 //# E N D O F F I L E
280 //########################################################################