Code

Make pedro dom list simpler
[inkscape.git] / src / pedro / pedrodom.h
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-2008 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 <glib.h>
28 #include <string>
29 #include <vector>
32 namespace Pedro
33 {
35 typedef std::string DOMString;
36 typedef unsigned int XMLCh;
39 class Namespace
40 {
41 public:
42     Namespace()
43         {}
45     Namespace(const DOMString &prefixArg, const DOMString &namespaceURIArg)
46         {
47         prefix       = prefixArg;
48         namespaceURI = namespaceURIArg;
49         }
51     Namespace(const Namespace &other)
52         {
53         assign(other);
54         }
56     Namespace &operator=(const Namespace &other)
57         {
58         assign(other);
59         return *this;
60         }
62     virtual ~Namespace()
63         {}
65     virtual DOMString getPrefix()
66         { return prefix; }
68     virtual DOMString getNamespaceURI()
69         { return namespaceURI; }
71 protected:
73     void assign(const Namespace &other)
74         {
75         prefix       = other.prefix;
76         namespaceURI = other.namespaceURI;
77         }
79     DOMString prefix;
80     DOMString namespaceURI;
82 };
84 class Attribute
85 {
86 public:
87     Attribute()
88         {}
90     Attribute(const DOMString &nameArg, const DOMString &valueArg)
91         {
92         name  = nameArg;
93         value = valueArg;
94         }
96     Attribute(const Attribute &other)
97         {
98         assign(other);
99         }
101     Attribute &operator=(const Attribute &other)
102         {
103         assign(other);
104         return *this;
105         }
107     virtual ~Attribute()
108         {}
110     virtual DOMString getName()
111         { return name; }
113     virtual DOMString getValue()
114         { return value; }
116 protected:
118     void assign(const Attribute &other)
119         {
120         name  = other.name;
121         value = other.value;
122         }
124     DOMString name;
125     DOMString value;
127 };
130 //#Define a list of elements. (Children, search results, etc)
131 class Element;
132 typedef std::vector<Element *> ElementList;
136 class Element
138 friend class Parser;
140 public:
141     Element()
142         {
143         parent = NULL;
144         }
146     Element(const DOMString &nameArg)
147         {
148         parent = NULL;
149         name   = nameArg;
150         }
152     Element(const DOMString &nameArg, const DOMString &valueArg)
153         {
154         parent = NULL;
155         name   = nameArg;
156         value  = valueArg;
157         }
159     Element(const Element &other)
160         {
161         assign(other);
162         }
164     Element &operator=(const Element &other)
165         {
166         assign(other);
167         return *this;
168         }
170     virtual Element *clone();
172     virtual ~Element()
173         {
174         for (unsigned int i=0 ; i<children.size() ; i++)
175             delete children[i];
176         }
178     virtual DOMString getName()
179         { return name; }
181     virtual DOMString getValue()
182         { return value; }
184     Element *getParent()
185         { return parent; }
187     Element *getFirstChild()
188         { return (children.size() == 0) ? NULL : children[0]; }
190     ElementList getChildren()
191         { return children; }
193     ElementList findElements(const DOMString &name);
195     DOMString getAttribute(const DOMString &name);
197     std::vector<Attribute> &getAttributes()
198         { return attributes; } 
200     DOMString getTagAttribute(const DOMString &tagName, const DOMString &attrName);
202     DOMString getTagValue(const DOMString &tagName);
204     void addChild(Element *child);
206     void addAttribute(const DOMString &name, const DOMString &value);
208     void addNamespace(const DOMString &prefix, const DOMString &namespaceURI);
210     bool exists(const DOMString &name)
211         { return (findElements(name).size() > 0); }
213     /**
214      * Prettyprint an XML tree to an output stream.  Elements are indented
215      * according to element hierarchy.
216      * @param f a stream to receive the output
217      * @param elem the element to output
218      */
219     void writeIndented(FILE *f);
221     /**
222      * Prettyprint an XML tree to standard output.  This is the equivalent of
223      * writeIndented(stdout).
224      * @param elem the element to output
225      */
226     void print();
228 protected:
230     void assign(const Element &other)
231         {
232         parent     = other.parent;
233         children   = other.children;
234         attributes = other.attributes;
235         namespaces = other.namespaces;
236         name       = other.name;
237         value      = other.value;
238         }
240     void findElementsRecursive(std::vector<Element *>&res, const DOMString &name);
242     void writeIndentedRecursive(FILE *f, int indent);
244     Element *parent;
246     ElementList children;
248     std::vector<Attribute> attributes;
249     std::vector<Namespace> namespaces;
251     DOMString name;
252     DOMString value;
254 };
260 class Parser
262 public:
263     /**
264      * Constructor
265      */
266     Parser()
267         { init(); }
269     virtual ~Parser()
270         {}
272     /**
273      * Parse XML in a char buffer.
274      * @param buf a character buffer to parse
275      * @param pos position to start parsing
276      * @param len number of chars, from pos, to parse.
277      * @return a pointer to the root of the XML document;
278      */
279     Element *parse(const char *buf,int pos,int len);
281     /**
282      * Parse XML in a char buffer.
283      * @param buf a character buffer to parse
284      * @param pos position to start parsing
285      * @param len number of chars, from pos, to parse.
286      * @return a pointer to the root of the XML document;
287      */
288     Element *parse(const DOMString &buf);
290     /**
291      * Parse a named XML file.  The file is loaded like a data file;
292      * the original format is not preserved.
293      * @param fileName the name of the file to read
294      * @return a pointer to the root of the XML document;
295      */
296     Element *parseFile(const DOMString &fileName);
298     /**
299      * Utility method to preprocess a string for XML
300      * output, escaping its entities.
301      * @param str the string to encode
302      */
303     static DOMString encode(const DOMString &str);
305     /**
306      *  Removes whitespace from beginning and end of a string
307      */
308     DOMString trim(const DOMString &s);
310 private:
312     void init()
313         {
314         keepGoing       = true;
315         currentNode     = NULL;
316         parselen        = 0;
317         parsebuf        = NULL;
318         currentPosition = 0;
319         }
321     void getLineAndColumn(long pos, long *lineNr, long *colNr);
323     void error(char *fmt, ...) G_GNUC_PRINTF(2,3);
325     int peek(long pos);
327     int match(long pos, const char *text);
329     int skipwhite(long p);
331     int getWord(int p0, DOMString &buf);
333     int getQuoted(int p0, DOMString &buf, int do_i_parse);
335     int parseVersion(int p0);
337     int parseDoctype(int p0);
339     int parseElement(int p0, Element *par,int depth);
341     Element *parse(XMLCh *buf,int pos,int len);
343     bool       keepGoing;
344     Element    *currentNode;
345     long       parselen;
346     XMLCh      *parsebuf;
347     DOMString  cdatabuf;
348     long       currentPosition;
349     int        colNr;
351 };
355 }//namespace Pedro
358 #endif /* __PEDRODOM_H__ */
360 //########################################################################
361 //#  E N D    O F    F I L E
362 //########################################################################