diff options
| author | Bob Jamison <ishmalius@gmail.com> | 2006-05-15 15:06:21 +0000 |
|---|---|---|
| committer | ishmal <ishmal@users.sourceforge.net> | 2006-05-15 15:06:21 +0000 |
| commit | 8325c242bbc39df39b5b2260d3e2aac289d8a930 (patch) | |
| tree | 9a910be0c78883d9beec6cece07f7f800e0713ae /src/pedro/pedrodom.h | |
| parent | fix potential (though currently impossible) crash when spatial mode is used w... (diff) | |
| download | inkscape-8325c242bbc39df39b5b2260d3e2aac289d8a930.tar.gz inkscape-8325c242bbc39df39b5b2260d3e2aac289d8a930.zip | |
Move from the jabber_whiteboard directory to its own, so that it can be updated in parallel.
(bzr r846)
Diffstat (limited to 'src/pedro/pedrodom.h')
| -rw-r--r-- | src/pedro/pedrodom.h | 343 |
1 files changed, 343 insertions, 0 deletions
diff --git a/src/pedro/pedrodom.h b/src/pedro/pedrodom.h new file mode 100644 index 000000000..9c6651259 --- /dev/null +++ b/src/pedro/pedrodom.h @@ -0,0 +1,343 @@ +#ifndef __PEDRODOM_H__ +#define __PEDRODOM_H__ +/* + * API for the Pedro mini-DOM parser and tree + * + * Authors: + * Bob Jamison + * + * Copyright (C) 2005-2006 Bob Jamison + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <string> +#include <vector> + + +namespace Pedro +{ + +typedef std::string DOMString; +typedef unsigned int XMLCh; + + +class Namespace +{ +public: + Namespace() + {} + + Namespace(const DOMString &prefixArg, const DOMString &namespaceURIArg) + { + prefix = prefixArg; + namespaceURI = namespaceURIArg; + } + + Namespace(const Namespace &other) + { + assign(other); + } + + Namespace &operator=(const Namespace &other) + { + assign(other); + return *this; + } + + virtual ~Namespace() + {} + + virtual DOMString getPrefix() + { return prefix; } + + virtual DOMString getNamespaceURI() + { return namespaceURI; } + +protected: + + void assign(const Namespace &other) + { + prefix = other.prefix; + namespaceURI = other.namespaceURI; + } + + DOMString prefix; + DOMString namespaceURI; + +}; + +class Attribute +{ +public: + Attribute() + {} + + Attribute(const DOMString &nameArg, const DOMString &valueArg) + { + name = nameArg; + value = valueArg; + } + + Attribute(const Attribute &other) + { + assign(other); + } + + Attribute &operator=(const Attribute &other) + { + assign(other); + return *this; + } + + virtual ~Attribute() + {} + + virtual DOMString getName() + { return name; } + + virtual DOMString getValue() + { return value; } + +protected: + + void assign(const Attribute &other) + { + name = other.name; + value = other.value; + } + + DOMString name; + DOMString value; + +}; + + +class Element +{ +friend class Parser; + +public: + Element() + { + parent = NULL; + } + + Element(const DOMString &nameArg) + { + parent = NULL; + name = nameArg; + } + + Element(const DOMString &nameArg, const DOMString &valueArg) + { + parent = NULL; + name = nameArg; + value = valueArg; + } + + Element(const Element &other) + { + assign(other); + } + + Element &operator=(const Element &other) + { + assign(other); + return *this; + } + + virtual Element *clone(); + + virtual ~Element() + { + for (unsigned int i=0 ; i<children.size() ; i++) + delete children[i]; + } + + virtual DOMString getName() + { return name; } + + virtual DOMString getValue() + { return value; } + + Element *getParent() + { return parent; } + + std::vector<Element *> getChildren() + { return children; } + + std::vector<Element *> findElements(const DOMString &name); + + DOMString getAttribute(const DOMString &name); + + DOMString getTagAttribute(const DOMString &tagName, const DOMString &attrName); + + DOMString getTagValue(const DOMString &tagName); + + void addChild(Element *child); + + void addAttribute(const DOMString &name, const DOMString &value); + + void addNamespace(const DOMString &prefix, const DOMString &namespaceURI); + + + /** + * Prettyprint an XML tree to an output stream. Elements are indented + * according to element hierarchy. + * @param f a stream to receive the output + * @param elem the element to output + */ + void writeIndented(FILE *f); + + /** + * Prettyprint an XML tree to standard output. This is the equivalent of + * writeIndented(stdout). + * @param elem the element to output + */ + void print(); + +protected: + + void assign(const Element &other) + { + parent = other.parent; + children = other.children; + attributes = other.attributes; + namespaces = other.namespaces; + name = other.name; + value = other.value; + } + + void findElementsRecursive(std::vector<Element *>&res, const DOMString &name); + + void writeIndentedRecursive(FILE *f, int indent); + + Element *parent; + + std::vector<Element *>children; + + std::vector<Attribute> attributes; + std::vector<Namespace> namespaces; + + DOMString name; + DOMString value; + +}; + + + + + +class Parser +{ +public: + /** + * Constructor + */ + Parser() + { init(); } + + virtual ~Parser() + {} + + /** + * Parse XML in a char buffer. + * @param buf a character buffer to parse + * @param pos position to start parsing + * @param len number of chars, from pos, to parse. + * @return a pointer to the root of the XML document; + */ + Element *parse(const char *buf,int pos,int len); + + /** + * Parse XML in a char buffer. + * @param buf a character buffer to parse + * @param pos position to start parsing + * @param len number of chars, from pos, to parse. + * @return a pointer to the root of the XML document; + */ + Element *parse(const DOMString &buf); + + /** + * Parse a named XML file. The file is loaded like a data file; + * the original format is not preserved. + * @param fileName the name of the file to read + * @return a pointer to the root of the XML document; + */ + Element *parseFile(const char *fileName); + + /** + * Utility method to preprocess a string for XML + * output, escaping its entities. + * @param str the string to encode + */ + static DOMString encode(const DOMString &str); + + +private: + + void init() + { + keepGoing = true; + currentNode = NULL; + parselen = 0; + parsebuf = NULL; + currentPosition = 0; + } + + void getLineAndColumn(long pos, long *lineNr, long *colNr); + + void error(char *fmt, ...); + + int peek(long pos); + + int match(long pos, const char *text); + + int skipwhite(long p); + + int getWord(int p0, DOMString &buf); + + int getQuoted(int p0, DOMString &buf, int do_i_parse); + + int parseVersion(int p0); + + int parseDoctype(int p0); + + int parseElement(int p0, Element *par,int depth); + + Element *parse(XMLCh *buf,int pos,int len); + + bool keepGoing; + Element *currentNode; + long parselen; + XMLCh *parsebuf; + DOMString cdatabuf; + long currentPosition; + int colNr; + +}; + + + +}//namespace Pedro + + +#endif /* __PEDRODOM_H__ */ + +//######################################################################## +//# E N D O F F I L E +//######################################################################## + |
