Code

b9fb98b92faaee8fad161b80b3a118947e654214
[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-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
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     std::vector<Attribute> &getAttributes()
187         { return attributes; } 
189     DOMString getTagAttribute(const DOMString &tagName, const DOMString &attrName);
191     DOMString getTagValue(const DOMString &tagName);
193     void addChild(Element *child);
195     void addAttribute(const DOMString &name, const DOMString &value);
197     void addNamespace(const DOMString &prefix, const DOMString &namespaceURI);
200     /**
201      * Prettyprint an XML tree to an output stream.  Elements are indented
202      * according to element hierarchy.
203      * @param f a stream to receive the output
204      * @param elem the element to output
205      */
206     void writeIndented(FILE *f);
208     /**
209      * Prettyprint an XML tree to standard output.  This is the equivalent of
210      * writeIndented(stdout).
211      * @param elem the element to output
212      */
213     void print();
215 protected:
217     void assign(const Element &other)
218         {
219         parent     = other.parent;
220         children   = other.children;
221         attributes = other.attributes;
222         namespaces = other.namespaces;
223         name       = other.name;
224         value      = other.value;
225         }
227     void findElementsRecursive(std::vector<Element *>&res, const DOMString &name);
229     void writeIndentedRecursive(FILE *f, int indent);
231     Element *parent;
233     std::vector<Element *>children;
235     std::vector<Attribute> attributes;
236     std::vector<Namespace> namespaces;
238     DOMString name;
239     DOMString value;
241 };
247 class Parser
249 public:
250     /**
251      * Constructor
252      */
253     Parser()
254         { init(); }
256     virtual ~Parser()
257         {}
259     /**
260      * Parse XML in a char buffer.
261      * @param buf a character buffer to parse
262      * @param pos position to start parsing
263      * @param len number of chars, from pos, to parse.
264      * @return a pointer to the root of the XML document;
265      */
266     Element *parse(const char *buf,int pos,int len);
268     /**
269      * Parse XML in a char buffer.
270      * @param buf a character buffer to parse
271      * @param pos position to start parsing
272      * @param len number of chars, from pos, to parse.
273      * @return a pointer to the root of the XML document;
274      */
275     Element *parse(const DOMString &buf);
277     /**
278      * Parse a named XML file.  The file is loaded like a data file;
279      * the original format is not preserved.
280      * @param fileName the name of the file to read
281      * @return a pointer to the root of the XML document;
282      */
283     Element *parseFile(const DOMString &fileName);
285     /**
286      * Utility method to preprocess a string for XML
287      * output, escaping its entities.
288      * @param str the string to encode
289      */
290     static DOMString encode(const DOMString &str);
292     /**
293      *  Removes whitespace from beginning and end of a string
294      */
295     DOMString trim(const DOMString &s);
297 private:
299     void init()
300         {
301         keepGoing       = true;
302         currentNode     = NULL;
303         parselen        = 0;
304         parsebuf        = NULL;
305         currentPosition = 0;
306         }
308     void getLineAndColumn(long pos, long *lineNr, long *colNr);
310     void error(char *fmt, ...);
312     int peek(long pos);
314     int match(long pos, const char *text);
316     int skipwhite(long p);
318     int getWord(int p0, DOMString &buf);
320     int getQuoted(int p0, DOMString &buf, int do_i_parse);
322     int parseVersion(int p0);
324     int parseDoctype(int p0);
326     int parseElement(int p0, Element *par,int depth);
328     Element *parse(XMLCh *buf,int pos,int len);
330     bool       keepGoing;
331     Element    *currentNode;
332     long       parselen;
333     XMLCh      *parsebuf;
334     DOMString  cdatabuf;
335     long       currentPosition;
336     int        colNr;
338 };
342 }//namespace Pedro
345 #endif /* __PEDRODOM_H__ */
347 //########################################################################
348 //#  E N D    O F    F I L E
349 //########################################################################