From 029cc1ebe96b81b993dad0ce5a8175206a47b9ad Mon Sep 17 00:00:00 2001 From: ishmal Date: Wed, 23 Apr 2008 20:30:09 +0000 Subject: [PATCH] Further cleanup. Minidom superseded by pedrodom. --- src/dom/CMakeLists.txt | 1 - src/dom/minidom.cpp | 704 ----------------------------------------- src/dom/minidom.h | 281 ---------------- 3 files changed, 986 deletions(-) delete mode 100644 src/dom/minidom.cpp delete mode 100644 src/dom/minidom.h diff --git a/src/dom/CMakeLists.txt b/src/dom/CMakeLists.txt index 503f9d0cc..30685be03 100644 --- a/src/dom/CMakeLists.txt +++ b/src/dom/CMakeLists.txt @@ -4,7 +4,6 @@ domimpl.cpp domptr.cpp domstring.cpp lsimpl.cpp -minidom.cpp prop-css2.cpp prop-css.cpp prop-svg.cpp diff --git a/src/dom/minidom.cpp b/src/dom/minidom.cpp deleted file mode 100644 index a0d24279f..000000000 --- a/src/dom/minidom.cpp +++ /dev/null @@ -1,704 +0,0 @@ - - - -#include -#include -#include -#include -#include -#include - - -#include "minidom.h" - -namespace MiniDom -{ - - - -//######################################################################## -//# E L E M E N T -//######################################################################## - -void Element::findElementsRecursive(std::vector&res, const DOMString &name) -{ - if (getName() == name) - res.push_back(this); - for (unsigned int i=0; ifindElementsRecursive(res, name); -} - -std::vector Element::findElements(const DOMString &name) -{ - std::vector res; - findElementsRecursive(res, name); - return res; -} - -DOMString Element::getAttribute(const DOMString &name) -{ - for (unsigned int i=0 ; i\n"); - - //Between the tags - if (value.size() > 0) - { - for (int i=0;iwriteIndentedRecursive(f, indent+2); - - //Closing tag - for (int i=0; i\n", name.c_str()); -} - -void Element::writeIndented(FILE *f) -{ - writeIndentedRecursive(f, 0); -} - -void Element::print() -{ - writeIndented(stdout); -} - - -//######################################################################## -//# P A R S E R -//######################################################################## - - - -typedef struct - { - char *escaped; - char value; - } EntityEntry; - -static EntityEntry entities[] = -{ - { "&" , '&' }, - { "<" , '<' }, - { ">" , '>' }, - { "'", '\'' }, - { """, '"' }, - { NULL , '\0' } -}; - - - -int Parser::countLines(int begin, int end) -{ - int count = 0; - for (int i=begin ; i= parselen) - return -1; - currentPosition = pos; - int ch = parsebuf[pos]; - //printf("ch:%c\n", ch); - return ch; -} - - - -int Parser::match(int p0, const char *text) -{ - int p = p0; - while (*text) - { - if (peek(p) != *text) - return p0; - p++; text++; - } - return p; -} - - - -int Parser::skipwhite(int p) -{ - - while (p p) - { - p = p2; - while (p"); - if (p2 > p) - { - p = p2; - break; - } - p++; - } - } - XMLCh b = peek(p); - if (!isspace(b)) - break; - p++; - } - return p; -} - -/* modify this to allow all chars for an element or attribute name*/ -int Parser::getWord(int p0, DOMString &buf) -{ - int p = p0; - while (p' || b=='=') - break; - buf.push_back(b); - p++; - } - return p; -} - -int Parser::getQuoted(int p0, DOMString &buf, int do_i_parse) -{ - - int p = p0; - if (peek(p) != '"' && peek(p) != '\'') - return p0; - p++; - - while ( pvalue ; ee++) - { - int p2 = match(p, ee->escaped); - if (p2>p) - { - buf.push_back(ee->value); - p = p2; - found = true; - break; - } - } - if (!found) - { - error("unterminated entity"); - return false; - } - } - else - { - buf.push_back(b); - p++; - } - } - return p; -} - -int Parser::parseVersion(int p0) -{ - //printf("### parseVersion: %d\n", p0); - - int p = p0; - - p = skipwhite(p0); - - if (peek(p) != '<') - return p0; - - p++; - if (p>=parselen || peek(p)!='?') - return p0; - - p++; - - DOMString buf; - - while (p=parselen || peek(p)!='<') - return p0; - - p++; - - if (peek(p)!='!' || peek(p+1)=='-') - return p0; - p++; - - DOMString buf; - while (p') - { - p++; - break; - } - buf.push_back(ch); - p++; - } - - //printf("Got doctype:%s\n",buf.c_str()); - return p; -} - -int Parser::parseElement(int p0, Element *par,int lineNr) -{ - - int p = p0; - - int p2 = p; - - p = skipwhite(p); - - //## Get open tag - XMLCh ch = peek(p); - if (ch!='<') - return p0; - - p++; - - DOMString openTagName; - p = skipwhite(p); - p = getWord(p, openTagName); - //printf("####tag :%s\n", openTagName.c_str()); - p = skipwhite(p); - - //Add element to tree - Element *n = new Element(openTagName); - n->parent = par; - n->line = lineNr + countLines(p0, p); - par->addChild(n); - - // Get attributes - if (peek(p) != '>') - { - while (p') - break; - else if (ch=='/' && p') - { - p++; - //printf("quick close\n"); - return p; - } - } - DOMString attrName; - p2 = getWord(p, attrName); - if (p2==p) - break; - //printf("name:%s",buf); - p=p2; - p = skipwhite(p); - ch = peek(p); - //printf("ch:%c\n",ch); - if (ch!='=') - break; - p++; - p = skipwhite(p); - // ch = parsebuf[p]; - // printf("ch:%c\n",ch); - DOMString attrVal; - p2 = getQuoted(p, attrVal, true); - p=p2+1; - //printf("name:'%s' value:'%s'\n",attrName.c_str(),attrVal.c_str()); - char *namestr = (char *)attrName.c_str(); - if (strncmp(namestr, "xmlns:", 6)==0) - n->addNamespace(attrName, attrVal); - else - n->addAttribute(attrName, attrVal); - } - } - - bool cdata = false; - - p++; - // ### Get intervening data ### */ - DOMString data; - while (pp) - { - p = p2; - while (p"); - if (p2 > p) - { - p = p2; - break; - } - p++; - } - } - - ch = peek(p); - //# END TAG - if (ch=='<' && !cdata && peek(p+1)=='/') - { - break; - } - //# CDATA - p2 = match(p, " p) - { - cdata = true; - p = p2; - continue; - } - - //# CHILD ELEMENT - if (ch == '<') - { - p2 = parseElement(p, n, lineNr + countLines(p0, p)); - if (p2 == p) - { - /* - printf("problem on element:%s. p2:%d p:%d\n", - openTagName.c_str(), p2, p); - */ - return p0; - } - p = p2; - continue; - } - //# ENTITY - if (ch=='&' && !cdata) - { - bool found = false; - for (EntityEntry *ee = entities ; ee->value ; ee++) - { - int p2 = match(p, ee->escaped); - if (p2>p) - { - data.push_back(ee->value); - p = p2; - found = true; - break; - } - } - if (!found) - { - error("unterminated entity"); - return -1; - } - continue; - } - - //# NONE OF THE ABOVE - data.push_back(ch); - p++; - }/*while*/ - - - n->value = data; - //printf("%d : data:%s\n",p,data.c_str()); - - //## Get close tag - p = skipwhite(p); - ch = peek(p); - if (ch != '<') - { - error("no < for end tag\n"); - return p0; - } - p++; - ch = peek(p); - if (ch != '/') - { - error("no / on end tag"); - return p0; - } - p++; - ch = peek(p); - p = skipwhite(p); - DOMString closeTagName; - p = getWord(p, closeTagName); - if (openTagName != closeTagName) - { - error("Mismatched closing tag. Expected . Got '%s'.", - openTagName.c_str(), closeTagName.c_str()); - return p0; - } - p = skipwhite(p); - if (peek(p) != '>') - { - error("no > on end tag for '%s'", closeTagName.c_str()); - return p0; - } - p++; - // printf("close element:%s\n",closeTagName.c_str()); - p = skipwhite(p); - return p; -} - - - - -Element *Parser::parse(XMLCh *buf,int pos,int len) -{ - parselen = len; - parsebuf = buf; - Element *rootNode = new Element("root"); - pos = parseVersion(pos); - pos = parseDoctype(pos); - pos = parseElement(pos, rootNode, 1); - return rootNode; -} - - -Element *Parser::parse(const char *buf, int pos, int len) -{ - - XMLCh *charbuf = (XMLCh *)malloc((len+1) * sizeof(XMLCh)); - long i = 0; - while (i< len) - { - charbuf[i] = (XMLCh)buf[i]; - i++; - } - charbuf[i] = '\0'; - Element *n = parse(charbuf, 0, len-1); - free(charbuf); - return n; -} - -Element *Parser::parse(const DOMString &buf) -{ - long len = buf.size(); - XMLCh *charbuf = (XMLCh *)malloc((len+1) * sizeof(XMLCh)); - long i = 0; - while (i< len) - { - charbuf[i] = (XMLCh)buf[i]; - i++; - } - charbuf[i] = '\0'; - Element *n = parse(charbuf, 0, len-1); - free(charbuf); - return n; -} - -Element *Parser::parseFile(const char *fileName) -{ - - //##### LOAD INTO A CHAR BUF, THEN CONVERT TO XMLCh - if (!fileName) - return NULL; - - FILE *f = fopen(fileName, "rb"); - if (!f) - return NULL; - - struct stat statBuf; - if (fstat(fileno(f),&statBuf)<0) - { - fclose(f); - return NULL; - } - long filelen = statBuf.st_size; - - //printf("length:%d\n",filelen); - XMLCh *charbuf = (XMLCh *)malloc((filelen+1) * sizeof(XMLCh)); - for (XMLCh *p=charbuf ; !feof(f) ; p++) - { - *p = (XMLCh)fgetc(f); - } - fclose(f); - charbuf[filelen] = '\0'; - - - /* - printf("nrbytes:%d\n",wc_count); - printf("buf:%ls\n======\n",charbuf); - */ - Element *n = parse(charbuf, 0, filelen-1); - free(charbuf); - return n; -} - - - - - - - -}//namespace MiniDom -//######################################################################## -//# T E S T -//######################################################################## - -bool doTest(char *fileName) -{ - MiniDom::Parser parser; - - MiniDom::Element *elem = parser.parseFile(fileName); - - if (!elem) - { - printf("Parsing failed\n"); - return false; - } - - elem->print(); - - delete elem; - - return true; -} - - - -int main(int argc, char **argv) -{ - if (argc != 2) - { - printf("usage: %s \n", argv[0]); - return 1; - } - - if (!doTest(argv[1])) - return 1; - - return 0; -} - - - -//######################################################################## -//# E N D O F F I L E -//######################################################################## - - diff --git a/src/dom/minidom.h b/src/dom/minidom.h deleted file mode 100644 index 41af805fb..000000000 --- a/src/dom/minidom.h +++ /dev/null @@ -1,281 +0,0 @@ -#include - -#include -#include - - -namespace MiniDom -{ -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) - { - prefix = other.prefix; - namespaceURI = other.namespaceURI; - } - - virtual ~Namespace() - {} - - virtual DOMString getPrefix() - { return prefix; } - - virtual DOMString getNamespaceURI() - { return namespaceURI; } - -protected: - - DOMString prefix; - DOMString namespaceURI; - -}; - -class Attribute -{ -public: - Attribute() - {} - - Attribute(const DOMString &nameArg, const DOMString &valueArg) - { - name = nameArg; - value = valueArg; - } - - Attribute(const Attribute &other) - { - name = other.name; - value = other.value; - } - - virtual ~Attribute() - {} - - virtual DOMString getName() - { return name; } - - virtual DOMString getValue() - { return value; } - -protected: - - 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) - { - parent = other.parent; - children = other.children; - attributes = other.attributes; - namespaces = other.namespaces; - name = other.name; - value = other.value; - } - - virtual ~Element() - { - for (unsigned int i=0 ; i getChildren() - { return children; } - - int getLine() - { return line; } - - std::vector findElements(const DOMString &name); - - DOMString getAttribute(const DOMString &name); - - 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 findElementsRecursive(std::vector&res, const DOMString &name); - - void writeIndentedRecursive(FILE *f, int indent); - - Element *parent; - - std::vectorchildren; - - std::vector attributes; - std::vector namespaces; - - DOMString name; - DOMString value; - - int line; - -}; - - - - - -class Parser -{ -public: - - 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); - - - - -private: - - void init() - { - keepGoing = true; - currentNode = NULL; - parselen = 0; - parsebuf = NULL; - currentPosition = 0; - } - - int countLines(int begin, int end); - - void getLineAndColumn(int pos, int *lineNr, int *colNr); - - void error(char *fmt, ...) G_GNUC_PRINTF(2,3); - - int peek(int pos); - - int match(int pos, const char *text); - - int skipwhite(int 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; - int parselen; - XMLCh *parsebuf; - DOMString cdatabuf; - int currentPosition; - int colNr; - - -}; - - - -}//namespace MiniDom - - -//######################################################################## -//# E N D O F F I L E -//######################################################################## - -- 2.30.2