You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by Mikael St�ldal <mi...@home.se> on 2001/09/08 17:03:19 UTC

Building SVG DOM from SAX

I want to build a SVG DOM from a SAX event stream, but I realized that it 
wasn't possible using org.apache.batik.dom.svg.SAXSVGDocumentFactory. So I 
modified that class and org.apache.batik.dom.util.SAXDocumentFactory a bit 
to make it possible (see the attached patches). It seems to work, and I can 
put the generated DOM into the transcoder. However, if the SVG contains an 
external xlink, I get this strange error message from the Crimson parser 
during transcoding (which is made visible by my patch to 
org.apache.batik.dom.util.SAXDocumentFactory):

org.xml.sax.SAXParseException: org.apache.crimson.parser/P-001 3c
         at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3030)
         at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3024)
         at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:505)
         at org.apache.crimson.parser.Parser2.parse(Parser2.java:304)
         at 
org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:433)
         at 
org.apache.batik.dom.util.SAXDocumentFactory.createDocument(SAXDocumentFactory.java:229)

What does it mean?


The patch:

Index: org/apache/batik/dom/util/SAXDocumentFactory.java
===================================================================
RCS file: 
/home/cvspublic/xml-batik/sources/org/apache/batik/dom/util/SAXDocumentFactory.java,v
retrieving revision 1.3
diff -c -r1.3 SAXDocumentFactory.java
*** org/apache/batik/dom/util/SAXDocumentFactory.java	2001/02/13 19:45:17	1.3
--- org/apache/batik/dom/util/SAXDocumentFactory.java	2001/09/08 14:58:20
***************
*** 126,131 ****
--- 126,152 ----
       }

       /**
+      * Creates a new SAXDocumentFactory object.
+      * No document descriptor will be created while generating a document.
+      * @param impl The DOM implementation to use for building the DOM tree.
+      */
+     public SAXDocumentFactory(DOMImplementation impl) {
+ 		this(impl, false);
+     }
+
+     /**
+      * Creates a new SAXDocumentFactory object.
+      * @param impl The DOM implementation to use for building the DOM tree.
+      * @param dd Whether a document descriptor must be generated.
+      */
+     public SAXDocumentFactory(DOMImplementation impl,
+                               boolean dd) {
+ 	implementation           = impl;
+ 	parserClassName          = null;
+         createDocumentDescriptor = dd;
+     }
+
+     /**
        * Creates a Document instance.
        * @param ns The namespace URI of the root element of the document.
        * @param root The name of the root element of the document.
***************
*** 168,173 ****
--- 189,204 ----
       }

       /**
+      * Prepares a GenericDocument. Use this method before firing SAX events.
+      * @param ns The namespace URI of the root element.
+      * @param root The name of the root element.
+      */
+     public void prepareDocument(String ns, String root)
+ 	{
+ 		document = implementation.createDocument(ns, root, null);
+ 	}
+
+     /**
        * Creates a GenericDocument.
        * @param ns The namespace URI of the root element.
        * @param root The name of the root element.
***************
*** 178,186 ****
       protected Document createDocument(String ns, String root, String uri,
                                         InputSource is)
   	throws IOException {
! 	document = implementation.createDocument(ns, root, null);

! 	try {
               XMLReader parser = 
XMLReaderFactory.createXMLReader(parserClassName);

               parser.setContentHandler(this);
--- 209,217 ----
       protected Document createDocument(String ns, String root, String uri,
                                         InputSource is)
   	throws IOException {
! 		prepareDocument(ns, root);

! 		try {
               XMLReader parser = 
XMLReaderFactory.createXMLReader(parserClassName);

               parser.setContentHandler(this);
***************
*** 197,202 ****
--- 228,234 ----

               parser.parse(is);
   	} catch (SAXException e) {
+ 			e.printStackTrace();
               Exception ex = e.getException();
               if (ex != null && ex instanceof InterruptedIOException) {
                   throw (InterruptedIOException)ex;
***************
*** 205,210 ****
--- 237,251 ----
   	}

   	return document;
+     }
+ 	
+     /**
+      * Returns the created document. Use this method after firing SAX events.
+ 	 *
+      * @return null if no document was previously generated.
+      */
+     public Document getDocument() {
+         return document;
       }

       /**
Index: org/apache/batik/dom/svg/SAXSVGDocumentFactory.java
===================================================================
RCS file: 
/home/cvspublic/xml-batik/sources/org/apache/batik/dom/svg/SAXSVGDocumentFactory.java,v
retrieving revision 1.9
diff -c -r1.9 SAXSVGDocumentFactory.java
*** org/apache/batik/dom/svg/SAXSVGDocumentFactory.java	2001/07/18 22:04:53	1.9
--- org/apache/batik/dom/svg/SAXSVGDocumentFactory.java	2001/09/08 14:58:20
***************
*** 60,65 ****
--- 60,80 ----

       /**
        * Creates a new SVGDocumentFactory object.
+      */
+     public SAXSVGDocumentFactory() {
+         super(SVGDOMImplementation.getDOMImplementation());
+     }
+
+     /**
+      * Creates a new SVGDocumentFactory object.
+      * @param dd Whether a document descriptor must be generated.
+      */
+     public SAXSVGDocumentFactory(boolean dd) {
+         super(SVGDOMImplementation.getDOMImplementation(), dd);
+     }
+
+     /**
+      * Creates a new SVGDocumentFactory object.
        * @param parser The SAX2 parser classname.
        */
       public SAXSVGDocumentFactory(String parser) {
***************
*** 76,81 ****
--- 91,104 ----
       }

       /**
+      * Prepares a SVGOMDocument. Use this method before firing SAX events.
+      */
+     public void prepareDocument()
+ 	{
+ 		super.prepareDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg");
+ 	}
+
+     /**
        * Creates a SVGOMDocument instance.<br>
        * This method supports gzipped sources.
        * @param uri The document URI.
***************
*** 231,236 ****
--- 254,288 ----
               throw new RuntimeException("Bad root element");
           }
           return createDocument(uri, r);
+     }
+
+     /**
+      * Returns the created document. Use this method after firing SAX events.
+      * @param uri The document URI (used to resolve links).
+ 	 *
+      * @return <code>null</code> if no document was previously generated.
+      */
+     public SVGOMDocument getDocument(String uri)
+ 		throws IOException {
+         try {
+ 			return getDocument(new URL(uri));
+         } catch (MalformedURLException e) {
+             throw new IOException(e.getMessage());
+         }
+     }
+
+     /**
+      * Returns the created document. Use this method after firing SAX events.
+      * @param uri The document URI (used to resolve links).
+ 	 *
+      * @return <code>null</code> if no document was previously generated.
+      */
+     public SVGOMDocument getDocument(URL uri)
+ 		throws IOException {
+         SVGOMDocument doc = (SVGOMDocument)super.getDocument();
+ 		if (doc == null) return null;
+         doc.setURLObject(uri);
+ 		return doc;
       }

       /**


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org


Re: Building SVG DOM from SAX

Posted by Mikael St�ldal <mi...@home.se>.
At 08:36 2001-09-10 +0200, Stephane Hillion wrote:
>I suggest you subclass SAXSVGDocumentFactory instead of patching it - all the
>fields are protected to allow this.

That might be possible, but I suspect that more people than I will find 
this useful. So I propose you include my patch (or something eqvivalent) in 
the main distribution.

>I just had a look at crimson's sources
>(http://cvs.apache.org/viewcvs.cgi/xml-crimson/src/org/apache/crimson/parser/Parser2.java?rev=1.13&content-type=text/vnd.viewcvs-markup)
>and it seems that this error is caused by an invalid char at the end of 
>your  XML file.

Strange, there are only an LF at the end of my XML files.



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org


Re: Building SVG DOM from SAX

Posted by Stephane Hillion <sh...@ilog.fr>.
On Saturday 08 September 2001 17:03, Mikael Ståldal wrote:
> I want to build a SVG DOM from a SAX event stream, but I realized that it
> wasn't possible using org.apache.batik.dom.svg.SAXSVGDocumentFactory. So I
> modified that class and org.apache.batik.dom.util.SAXDocumentFactory a bit
> to make it possible (see the attached patches).

I suggest you subclass SAXSVGDocumentFactory instead of patching it - all the 
fields are protected to allow this.

> It seems to work, and I can
> put the generated DOM into the transcoder. However, if the SVG contains an
> external xlink, I get this strange error message from the Crimson parser
> during transcoding (which is made visible by my patch to
> org.apache.batik.dom.util.SAXDocumentFactory):
>
> org.xml.sax.SAXParseException: org.apache.crimson.parser/P-001 3c
>          at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3030)
>          at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3024)
>          at
> org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:505) at
> org.apache.crimson.parser.Parser2.parse(Parser2.java:304) at
> org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:433)
>          at
> org.apache.batik.dom.util.SAXDocumentFactory.createDocument(SAXDocumentFact
>ory.java:229)
>
> What does it mean?

I just had a look at crimson's sources 
(http://cvs.apache.org/viewcvs.cgi/xml-crimson/src/org/apache/crimson/parser/Parser2.java?rev=1.13&content-type=text/vnd.viewcvs-markup)
and it seems that this error is caused by an invalid char at the end of your 
XML file.
--
  Stephane.

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org