diff --git a/src/dom/xmlreader.cpp b/src/dom/xmlreader.cpp
--- a/src/dom/xmlreader.cpp
+++ b/src/dom/xmlreader.cpp
* Authors:
* Bob Jamison
*
- * Copyright (C) 2005 Bob Jamison
+ * Copyright (C) 2005-2008 Bob Jamison
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
#include "xmlreader.h"
-#include "svgimpl.h"
+#include "ucd.h"
+#include "domimpl.h"
+#include <stdio.h>
#include <stdarg.h>
-namespace org {
-namespace w3c {
-namespace dom {
+namespace org
+{
+namespace w3c
+{
+namespace dom
+{
//#########################################################################
//#########################################################################
struct EntityInfo
{
- char *escape;
+ const char *escape;
int escapeLength;
- char *value;
+ const char *value;
};
/**
*
*/
-void XmlReader::error(char *fmt, ...)
+void XmlReader::error(const char *fmt, ...)
{
va_list args;
fprintf(stderr, "XmlReader:error at line %d, column %d:", lineNr, colNr);
* Test if the given substring exists at the given position
* in parsebuf. Use peek() in case of out-of-bounds
*/
-bool XmlReader::match(int pos, char *str)
+bool XmlReader::match(int pos, const char *str)
{
while (*str)
{
while (p < len)
{
int b = get(p);
- if (!isspace(b))
+ if (!uni_is_space(b))
break;
p++;
}
int b = get(p);
if (b<=' ' || b=='/' || b=='>' || b=='=')
break;
- result.push_back(b);
+ result.push_back((XMLCh)b);
p++;
}
return p;
shortWord = "";
}
else
- shortWord.push_back(b);
+ shortWord.push_back((XMLCh)b);
p++;
}
if (prefix.size() > 0)
}
else
{
- buf.push_back(b);
+ buf.push_back((XMLCh)b);
}
}
colNr += 5;
bool quickCloseDummy;
- Node *node = new NodeImpl();
+ NodePtr node = new NodeImpl();
int p2 = parseAttributes(p, node, &quickCloseDummy);
if (p2 < p)
{
- delete node;
+ //smart ptr!!do not delete node;
return p0;
}
p = p2;
//get the attributes that we need
NamedNodeMap attributes = node->getAttributes();
- Node *attr = attributes.getNamedItem("version");
- if (attr)
+ NodePtr attr = attributes.getNamedItem("version");
+ if (attr.get())
document->setXmlVersion(attr->getNodeValue());
attr = attributes.getNamedItem("encoding");
- if (attr)
+ if (attr.get())
{ /*document->setXmlEncoding(attr->getNodeValue());*/ }
attr = attributes.getNamedItem("standalone");
- if (attr)
+ if (attr.get())
document->setXmlStandalone((attr->getNodeValue() == "yes"));
- delete node;
//#now we should be pointing at '?>'
if (!match(p, "?>"))
p += 9;
colNr += 9;
- DocumentType *doctype = document->getDoctype();
+ DocumentTypePtr doctype = document->getDoctype();
if (!doctype)
return p0;
/**
* Expects '<' on startup, ends on char after '>'
*/
-int XmlReader::parseComment(int p0, Comment *comment)
+int XmlReader::parseComment(int p0, CommentPtr comment)
{
int p = p0;
break;
}
int ch = get(p++);
- buf.push_back(ch);
+ buf.push_back((XMLCh)ch);
}
comment->setNodeValue(buf);
/**
*
*/
-int XmlReader::parseCDATA(int p0, CDATASection *cdata)
+int XmlReader::parseCDATA(int p0, CDATASectionPtr cdata)
{
int p = p0;
break;
}
int ch = get(p++);
- buf.push_back(ch);
+ buf.push_back((XMLCh)ch);
}
/*printf("Got CDATA:%s\n",buf.c_str());*/
/**
*
*/
-int XmlReader::parseText(int p0, Text *text)
+int XmlReader::parseText(int p0, TextPtr text)
{
int p = p0;
else
{
int ch = get(p++);
- buf.push_back(ch);
+ buf.push_back((XMLCh)ch);
}
}
* Parses attributes of a node. Should end pointing at either the
* '?' of a version or doctype tag, or a '>' of a normal tag
*/
-int XmlReader::parseAttributes(int p0, Node *node, bool *quickClose)
+int XmlReader::parseAttributes(int p0, NodePtr node, bool *quickClose)
{
*quickClose = false;
namespaceURI = XMLNSNAME;
//## Now let us make the attribute and give it to the node
- Attr *attr = document->createAttributeNS(namespaceURI, qualifiedName);
+ AttrPtr attr = document->createAttributeNS(namespaceURI, qualifiedName);
attr->setValue(attrValue);
node->getAttributes().setNamedItemNS(attr);
* Parse as a document, preserving the original structure as much as
* possible
*/
-int XmlReader::parseNode(int p0, Node *node, int depth)
+int XmlReader::parseNode(int p0, NodePtr node, int depth)
{
int p = p0;
//### COMMENT
if (match(p, "<!--"))
{
- Comment *comment = document->createComment("");
+ CommentPtr comment = document->createComment("");
p2 = parseComment(p, comment);
if (p2 <= p)
return p0;
p = p2;
if (parseAsData)
{ //throw away
- delete comment;
+ //delete comment;
}
else
{
//### CDATA
else if (match(p, "<![CDATA["))
{
- CDATASection *cdata = document->createCDATASection("");
+ CDATASectionPtr cdata = document->createCDATASection("");
p2 = parseCDATA(p, cdata);
if (p2 <= p)
return p0;
if (parseAsData)
{
nodeValue += cdata->getNodeValue();
- delete cdata;
+ //delete cdata;
}
else
{
else
{
/*Add element to tree*/
- Element *elem = document->createElement(""); //fill in name later
+ ElementPtr elem = document->createElement(""); //fill in name later
node->appendChild(elem);
p2 = parseNode(p, elem, depth+1);
if (p2 <= p)
//### TEXT
else
{
- Text *text = document->createTextNode("");
+ TextPtr text = document->createTextNode("");
p2 = parseText(p, text);
if (p2 <= p)
return p0;
if (parseAsData)
{
nodeValue += text->getNodeValue();
- delete text;
+ //delete text;
}
else
{
closeTagQualifiedName);
if (openTagQualifiedName != closeTagQualifiedName)
{
- error("Mismatched closing tag. Expected </%S>. Got '%S'.",
+ error("Mismatched closing tag. Expected </%s>. Got '%s'.",
openTagQualifiedName.c_str(), closeTagQualifiedName.c_str());
return p0;
}
/**
*
*/
-org::w3c::dom::Document *
+org::w3c::dom::DocumentPtr
XmlReader::parse(const DOMString &buf, int bufferOffset, int parseLen)
{
len = parseLen;
parsebuf = buf;
+ keepGoing = true;
+
DOMImplementationSourceImpl source;
DOMImplementation *domImpl = source.getDOMImplementation("");
- keepGoing = true;
-
document = domImpl->createDocument("", "", NULL);
//document = new svg::SVGDocumentImpl(domImpl, "", "", NULL);
//### COMMENT
if (match(p, "<!--"))
{
- Comment *comment = document->createComment("");
+ CommentPtr comment = document->createComment("");
p2 = parseComment(p, comment);
if (p2 <= p)
return document;
p = p2;
if (parseAsData)
{ //throw away
- delete comment;
+ //delete comment;
}
else
{
/**
*
*/
-org::w3c::dom::Document *
+org::w3c::dom::DocumentPtr
XmlReader::parse(const DOMString &str)
{
- Document *doc = parse(str, 0, str.size());
+ DocumentPtr doc = parse(str, 0, str.size());
+ if (!doc)
+ return doc;
doc->normalizeDocument();
-
return doc;
}
/**
*
*/
-org::w3c::dom::Document *
-XmlReader::parseFile(char *fileName)
+org::w3c::dom::DocumentPtr
+XmlReader::parseFile(const DOMString &fileName)
{
-
+ DocumentPtr doc;
+
DOMString buf = loadFile(fileName);
+ if (buf.size() == 0)
+ return doc; /*doc still null*/
- Document *doc = parse(buf, 0, buf.size());
+ doc = parse(buf, 0, buf.size());
return doc;
}
*
*/
org::w3c::dom::DOMString
-XmlReader::loadFile(char *fileName)
+XmlReader::loadFile(const DOMString &fileName)
{
+ DOMString buf;
- if (!fileName)
- return NULL;
- FILE *f = fopen(fileName, "rb");
+ if (fileName.size() == 0)
+ return buf;
+ FILE *f = fopen(fileName.c_str(), "rb");
if (!f)
- return NULL;
+ {
+ //error here
+ return buf;
+ }
- DOMString buf;
while (!feof(f))
{
int ch = fgetc(f);
if (ch<0)
break;
- buf.push_back(ch);
+ buf.push_back((XMLCh)ch);
}
fclose(f);