You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by "Bishop, Michael W. CONTR J9C880" <Mi...@je.jfcom.mil> on 2007/07/25 18:41:14 UTC

Loading from file to SVGDocument and ignoring whitespace?

I use the following to load a file to an SVGDocument:

private static String className =
        XMLResourceDescriptor.getXMLParserClassName();
private static SAXSVGDocumentFactory factory =
        new SAXSVGDocumentFactory(className);
...
factory.createSVGDocument(svgFile.toURI().toString());

The problem is, when I walk the DOM tree to discover information about
each Node and its siblings, I end up with a bunch of empty whitespace
nodes.  When I build an SVGDocument in memory, I don't have this
problem.  How can I load an SVGDocument to a File and have it ignore
whitespace?

<rect .../>
*** LOADING FROM A FILE REPORTS WHITESPACE HERE
<circle .../>

I want the Node representing the <circle> element's previous sibling to
be the <rect>:

Node rectNode = circleNode.getPreviousSibling();

Michael Bishop

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


Re: Loading from file to SVGDocument and ignoring whitespace?

Posted by "V. de Weger" <ba...@beno-delft.nl>.
Starting from <svg> ... </svg> it seemed strange, however from <text> ..
</text> it perfectly clear. 

Still learning every day...

Vincent
-- 
View this message in context: http://www.nabble.com/Loading-from-file-to-SVGDocument-and-ignoring-whitespace--tf4143370.html#a11825284
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: Loading from file to SVGDocument and ignoring whitespace?

Posted by Archie Cobbs <ar...@awarix.com>.
The text surrounding XML elements is part of the XML... and therefore part
of the DOM... even if it's only "whitespace".

On 7/26/07, V. de Weger <ba...@beno-delft.nl> wrote:
>
> Ok, strange however... XML should be formatting independent.



__________________________________________________________________________
Archie Cobbs      *        CTO, Awarix        *      http://www.awarix.com

Re: Loading from file to SVGDocument and ignoring whitespace?

Posted by th...@kodak.com.
Hi Vincent,

"V. de Weger" <ba...@beno-delft.nl> wrote on 07/26/2007 06:33:07 AM:

> Ok, strange however... XML should be formatting independent.

  There is another small XML format called XHTML where I
think you would agree the content between nodes is quite 
important!   Even in SVG spaces between nodes can mean
something, for example within <text> elements especially
if xml:space is set to 'preserve'.

> I guess this gives (only) a memory penalty to Batik ?

    Right.

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


Re: Loading from file to SVGDocument and ignoring whitespace?

Posted by "V. de Weger" <ba...@beno-delft.nl>.
Ok, strange however... XML should be formatting independent.

I guess this gives (only) a memory penalty to Batik ?

Vincent  
-- 
View this message in context: http://www.nabble.com/Loading-from-file-to-SVGDocument-and-ignoring-whitespace--tf4143370.html#a11807974
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: Loading from file to SVGDocument and ignoring whitespace?

Posted by th...@kodak.com.
Hi Vincent,

"V. de Weger" <ba...@beno-delft.nl> wrote on 07/26/2007 06:05:59 AM:

> Cameron, how can that be? Shouldn't a dom tree be a dom tree, 
> pretty printed or not?

   If you add text between the elements to pretty print the
DOM then you have changed the DOM in the processes of pretty
printing it.  There are indenting styles that avoid adding
text nodes between elements:

        <g id="foo"
          ><rect x="10" y="10" width="40" height="20
               fill="#FF0"
       /><text x="15" y="25" ....
   /></g
    ><g id="bar"
       ><use xlink:href="..."  ....

   But a lot of people don't like the 'junk' at the
front of the line.
 
> Or did you loose me somewhere and am I just getting it wrong...
 

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


Re: Loading from file to SVGDocument and ignoring whitespace?

Posted by "V. de Weger" <ba...@beno-delft.nl>.
Cameron, how can that be? Shouldn't a dom tree be a dom tree, pretty printed
or not?

Or did you loose me somewhere and am I just getting it wrong...

Vincent


-- 
View this message in context: http://www.nabble.com/Loading-from-file-to-SVGDocument-and-ignoring-whitespace--tf4143370.html#a11807660
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: Loading from file to SVGDocument and ignoring whitespace?

Posted by Cameron McCormack <ca...@mcc.id.au>.
Hi Michael.

Bishop, Michael W. CONTR J9C880:
> I use the following to load a file to an SVGDocument:
> 
> private static String className =
>         XMLResourceDescriptor.getXMLParserClassName();
> private static SAXSVGDocumentFactory factory =
>         new SAXSVGDocumentFactory(className);
> ...
> factory.createSVGDocument(svgFile.toURI().toString());
> 
> The problem is, when I walk the DOM tree to discover information about
> each Node and its siblings, I end up with a bunch of empty whitespace
> nodes.  When I build an SVGDocument in memory, I don't have this
> problem.  How can I load an SVGDocument to a File and have it ignore
> whitespace?

I don’t think there’s a way to tell the SAX parser to ignore such
whitespace nodes.  You can, of course, write a post-processing step to
remove all those text nodes from the document, e.g.:

  boolean isWhitespace(Node n) {
      if (n.getNodeType() != Node.TEXT_NODE) {
          return false;
      }
      String s = n.getNodeValue();
      for (int i = 0; i < s.length(); i++) {
          char c = s.charAt(i)
	  if (c != '\n' && c != '\r' && c != '\t' && c != ' ') {
	      return false;
	  }
      }
      return true;
  }

  void removeWhitespaceNodes(Node n) {
      while (n != null) {
          Node next = n.getNextSibling();
	  if (isWhitespace(n)) {
	      n.getParentNode().removeChild(n);
	  } else {
	      Node child = n.getFirstChild();
	      if (child != null) {
	          removeWhitespaceNodes(child);
	      }
	  }
	  n = next;
      }
  }

(completely untested)

then call removeWhitespaceNodes on the document element.

-- 
Cameron McCormack, http://mcc.id.au/
	xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au

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


RE: Loading from file to SVGDocument and ignoring whitespace?

Posted by "Bishop, Michael W. CONTR J9C880" <Mi...@je.jfcom.mil>.
I believe pretty-printing is the problem.  I use Batik's Transcoder to
go from SVG to String and I save the resulting String to a file.  I
disabled formatting and I get the expected relationships between the
Nodes.  
Formatting appears to be enabled in the Transcoder by default, so I did
the following:

transcoder.addTranscodingHint(SVGTranscoder.KEY_FORMAT,
SVGTranscoder.VALUE_FORMAT_OFF);

I imagine this method will also preserve the previously discussed
necessary spaces such as in <text> nodes.  Thanks for the whitespace
method Cameron, I will have to employ that to maintain backwards
compatibility.

Michael Bishop


-----Original Message-----
From: Tonny Kohar [mailto:tonny@kiyut.com] 
Sent: Wednesday, July 25, 2007 1:17 PM
To: batik-users@xmlgraphics.apache.org
Subject: Re: Loading from file to SVGDocument and ignoring whitespace?

Hi,

On Wed, 2007-07-25 at 12:41 -0400, Bishop, Michael W. CONTR J9C880
wrote:
> I use the following to load a file to an SVGDocument:
> 
> private static String className =
>         XMLResourceDescriptor.getXMLParserClassName();
> private static SAXSVGDocumentFactory factory =
>         new SAXSVGDocumentFactory(className); ...
> factory.createSVGDocument(svgFile.toURI().toString());
> 
> The problem is, when I walk the DOM tree to discover information about

> each Node and its siblings, I end up with a bunch of empty whitespace 
> nodes.  When I build an SVGDocument in memory, I don't have this 
> problem.  How can I load an SVGDocument to a File and have it ignore 
> whitespace?

IMHO, any conforming svg parser should have a whitespace node in the DOM
if the document is structured like your example. We experience this
problem as well, so we do the workaround by put node filter (that check
for whitespace outside <text> node with xml space preserve) every time
we traverse the DOM Tree.

Another note: when you save the document into file, don't do pretty
formating or you will get this whitespace problem. Or if you really want
to pretty format the xml source, break at attribute level, do not break
at element level, eg: <rect ...><circle ..break here at attribute level
so the >< (end tag and open tag) is side by side

Regards
Tonny Kohar
--
Kiyut
imagine, design, create ...
http://www.kiyut.com


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

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


Re: Loading from file to SVGDocument and ignoring whitespace?

Posted by Tonny Kohar <to...@kiyut.com>.
Hi,

On Wed, 2007-07-25 at 12:41 -0400, Bishop, Michael W. CONTR J9C880
wrote:
> I use the following to load a file to an SVGDocument:
> 
> private static String className =
>         XMLResourceDescriptor.getXMLParserClassName();
> private static SAXSVGDocumentFactory factory =
>         new SAXSVGDocumentFactory(className);
> ...
> factory.createSVGDocument(svgFile.toURI().toString());
> 
> The problem is, when I walk the DOM tree to discover information about
> each Node and its siblings, I end up with a bunch of empty whitespace
> nodes.  When I build an SVGDocument in memory, I don't have this
> problem.  How can I load an SVGDocument to a File and have it ignore
> whitespace?

IMHO, any conforming svg parser should have a whitespace node in the DOM
if the document is structured like your example. We experience this
problem as well, so we do the workaround by put node filter (that check
for whitespace outside <text> node with xml space preserve) every time
we traverse the DOM Tree.

Another note: when you save the document into file, don't do pretty
formating or you will get this whitespace problem. Or if you really want
to pretty format the xml source, break at attribute level, do not break
at element level, eg: <rect ...><circle ..break here at attribute level
so the >< (end tag and open tag) is side by side

Regards
Tonny Kohar
-- 
Kiyut
imagine, design, create ...
http://www.kiyut.com


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