Code

add line number to Element
[inkscape.git] / src / dom / minidom.h
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:
86     Element()
87         {
88         parent = NULL;
89         }
91     Element(const DOMString &nameArg)
92         {
93         parent = NULL;
94         name   = nameArg;
95         }
97     Element(const DOMString &nameArg, const DOMString &valueArg)
98         {
99         parent = NULL;
100         name  = nameArg;
101         value = valueArg;
102         }
104     Element(const Element &other)
105         {
106         parent     = other.parent;
107         children   = other.children;
108         attributes = other.attributes;
109         namespaces = other.namespaces;
110         name       = other.name;
111         value      = other.value;
112         }
114     virtual ~Element()
115         {
116         for (unsigned int i=0 ; i<children.size() ; i++)
117             delete children[i];
118         }
120     virtual DOMString getName()
121         { return name; }
123     virtual DOMString getValue()
124         { return value; }
126     Element *getParent()
127         { return parent; }
129     std::vector<Element *> getChildren()
130         { return children; }
131         
132     int getLine()
133         { return line; }
135     std::vector<Element *> findElements(const DOMString &name);
137     DOMString getAttribute(const DOMString &name);
139     void addChild(Element *child);
141     void addAttribute(const DOMString &name, const DOMString &value);
143     void addNamespace(const DOMString &prefix, const DOMString &namespaceURI);
146     /**
147      * Prettyprint an XML tree to an output stream.  Elements are indented
148      * according to element hierarchy.
149      * @param f a stream to receive the output
150      * @param elem the element to output
151      */
152     void writeIndented(FILE *f);
154     /**
155      * Prettyprint an XML tree to standard output.  This is the equivalent of
156      * writeIndented(stdout).
157      * @param elem the element to output
158      */
159     void print();
161 protected:
164     void findElementsRecursive(std::vector<Element *>&res, const DOMString &name);
166     void writeIndentedRecursive(FILE *f, int indent);
168     Element *parent;
170     std::vector<Element *>children;
172     std::vector<Attribute> attributes;
173     std::vector<Namespace> namespaces;
175     DOMString name;
176     DOMString value;
177     
178     int line;
180 };
186 class Parser
188 public:
190     Parser()
191         { init(); }
193     virtual ~Parser()
194         {}
196     /**
197      * Parse XML in a char buffer.
198      * @param buf a character buffer to parse
199      * @param pos position to start parsing
200      * @param len number of chars, from pos, to parse.
201      * @return a pointer to the root of the XML document;
202      */
203     Element *parse(const char *buf,int pos,int len);
205     /**
206      * Parse XML in a char buffer.
207      * @param buf a character buffer to parse
208      * @param pos position to start parsing
209      * @param len number of chars, from pos, to parse.
210      * @return a pointer to the root of the XML document;
211      */
212     Element *parse(const DOMString &buf);
214     /**
215      * Parse a named XML file.  The file is loaded like a data file;
216      * the original format is not preserved.
217      * @param fileName the name of the file to read
218      * @return a pointer to the root of the XML document;
219      */
220     Element *parseFile(const char *fileName);
225 private:
227     void init()
228         {
229         keepGoing       = true;
230         currentNode     = NULL;
231         parselen        = 0;
232         parsebuf        = NULL;
233         currentPosition = 0;
234         }
236     int countLines(int begin, int end);
238     void getLineAndColumn(int pos, int *lineNr, int *colNr);
240     void error(char *fmt, ...);
242     int peek(int pos);
244     int match(int pos, const char *text);
246     int skipwhite(int p);
248     int getWord(int p0, DOMString &buf);
250     int getQuoted(int p0, DOMString &buf, int do_i_parse);
252     int parseVersion(int p0);
254     int parseDoctype(int p0);
256     int parseElement(int p0, Element *par,int depth);
258     Element *parse(XMLCh *buf,int pos,int len);
260     bool         keepGoing;
261     Element      *currentNode;
262     int          parselen;
263     XMLCh        *parsebuf;
264     DOMString    cdatabuf;
265     int          currentPosition;
266     int          colNr;
269 };
273 }//namespace MiniDom
276 //########################################################################
277 //#  E N D    O F    F I L E
278 //########################################################################