Code

Deweirdification of user visible messages as proposed by Tav
[inkscape.git] / src / dom / xmlreader.cpp
old mode 100755 (executable)
new mode 100644 (file)
index d475afd..fc4eee5
@@ -10,7 +10,7 @@
  * 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
+{
 
 
 //#########################################################################
@@ -44,9 +49,9 @@ namespace dom {
 //#########################################################################
 struct EntityInfo
 {
-    char *escape;
+    const char *escape;
     int  escapeLength;
-    char *value;
+    const char *value;
 };
 
 
@@ -70,7 +75,7 @@ static EntityInfo entityTable[] =
 /**
  *
  */
-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);
@@ -151,7 +156,7 @@ int XmlReader::peek(int p)
  *  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)
        {
@@ -188,7 +193,7 @@ int XmlReader::skipwhite(int p)
   while (p < len)
     {
     int b = get(p);
-    if (!isspace(b))
+    if (!uni_is_space(b))
         break;
     p++;
     }
@@ -205,7 +210,7 @@ int XmlReader::getWord(int p, DOMString &result)
         int b = get(p);
         if (b<=' ' || b=='/' || b=='>' || b=='=')
             break;
-        result.push_back(b);
+        result.push_back((XMLCh)b);
         p++;
         }
     return p;
@@ -228,7 +233,7 @@ int XmlReader::getPrefixedWord(int p, DOMString &prefix,
             shortWord = "";
             }
         else
-            shortWord.push_back(b);
+            shortWord.push_back((XMLCh)b);
         p++;
         }
     if (prefix.size() > 0)
@@ -268,7 +273,7 @@ int XmlReader::getQuoted(int p0, DOMString &result)
             }
         else
             {
-            buf.push_back(b);
+            buf.push_back((XMLCh)b);
             }
         }
 
@@ -296,27 +301,26 @@ int XmlReader::parseVersion(int p0)
     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, "?>"))
@@ -346,7 +350,7 @@ int XmlReader::parseDoctype(int p0)
     p     += 9;
     colNr += 9;
 
-    DocumentType *doctype = document->getDoctype();
+    DocumentTypePtr doctype = document->getDoctype();
     if (!doctype)
         return p0;
 
@@ -421,7 +425,7 @@ int XmlReader::parseDoctype(int 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;
 
@@ -442,7 +446,7 @@ int XmlReader::parseComment(int p0, Comment *comment)
             break;
             }
         int ch = get(p++);
-        buf.push_back(ch);
+        buf.push_back((XMLCh)ch);
         }
 
     comment->setNodeValue(buf);
@@ -455,7 +459,7 @@ int XmlReader::parseComment(int p0, Comment *comment)
 /**
  *
  */
-int XmlReader::parseCDATA(int p0, CDATASection *cdata)
+int XmlReader::parseCDATA(int p0, CDATASectionPtr cdata)
 {
 
     int p = p0;
@@ -477,7 +481,7 @@ int XmlReader::parseCDATA(int p0, CDATASection *cdata)
             break;
             }
         int ch = get(p++);
-        buf.push_back(ch);
+        buf.push_back((XMLCh)ch);
         }
 
     /*printf("Got CDATA:%s\n",buf.c_str());*/
@@ -491,7 +495,7 @@ int XmlReader::parseCDATA(int p0, CDATASection *cdata)
 /**
  *
  */
-int XmlReader::parseText(int p0, Text *text)
+int XmlReader::parseText(int p0, TextPtr text)
 {
 
     int p = p0;
@@ -513,7 +517,7 @@ int XmlReader::parseText(int p0, Text *text)
         else
             {
             int ch = get(p++);
-            buf.push_back(ch);
+            buf.push_back((XMLCh)ch);
             }
         }
 
@@ -531,7 +535,7 @@ int XmlReader::parseText(int p0, Text *text)
  * 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;
 
@@ -589,7 +593,7 @@ int XmlReader::parseAttributes(int p0, Node *node, bool *quickClose)
             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);
 
@@ -628,7 +632,7 @@ int XmlReader::parseEntity(int p0, DOMString &buf)
  *  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;
@@ -671,14 +675,14 @@ int XmlReader::parseNode(int p0, Node *node, int depth)
         //### 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
                 {
@@ -702,7 +706,7 @@ int XmlReader::parseNode(int p0, Node *node, int depth)
         //### CDATA
         else if (match(p, "<![CDATA["))
             {
-            CDATASection *cdata = document->createCDATASection("");
+            CDATASectionPtr cdata = document->createCDATASection("");
             p2 = parseCDATA(p, cdata);
             if (p2 <= p)
                 return p0;
@@ -710,7 +714,7 @@ int XmlReader::parseNode(int p0, Node *node, int depth)
             if (parseAsData)
                 {
                 nodeValue += cdata->getNodeValue();
-                delete cdata;
+                //delete cdata;
                 }
             else
                 {
@@ -729,7 +733,7 @@ int XmlReader::parseNode(int p0, Node *node, int depth)
             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)
@@ -743,7 +747,7 @@ int XmlReader::parseNode(int p0, Node *node, int depth)
         //### TEXT
         else
             {
-            Text *text = document->createTextNode("");
+            TextPtr text = document->createTextNode("");
             p2 = parseText(p, text);
             if (p2 <= p)
                 return p0;
@@ -751,7 +755,7 @@ int XmlReader::parseNode(int p0, Node *node, int depth)
             if (parseAsData)
                 {
                 nodeValue += text->getNodeValue();
-                delete text;
+                //delete text;
                 }
             else
                 {
@@ -784,7 +788,7 @@ int XmlReader::parseNode(int p0, Node *node, int depth)
                         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;
         }
@@ -803,17 +807,17 @@ int XmlReader::parseNode(int p0, Node *node, int depth)
 /**
  *
  */
-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);
 
@@ -826,14 +830,14 @@ XmlReader::parse(const DOMString &buf, int bufferOffset, int parseLen)
         //### 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
                 {
@@ -874,26 +878,30 @@ XmlReader::parse(const DOMString &buf, int bufferOffset, int parseLen)
 /**
  *
  */
-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;
 }
@@ -908,22 +916,25 @@ XmlReader::parseFile(char *fileName)
  *
  */
 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);