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 mgenov <mg...@gmail.com> on 2009/01/02 19:09:35 UTC

SVGOMDocument and getChildNodes ordering

Hello guys, 
  I'm encountering problem with the ordering of the tspan nodes in an svg
document. I have a document with text node and several child nodes of type
tspan. My svg file looks similar to this one:

<text stroke="blue" font-size="20" transform="matrix(1 0 0 1 20 40)">1

			<tspan dy="20">2</tspan>

			<tspan y="20">3</tspan>

			<tspan dy="20">4</tspan>

			<tspan y="60">5</tspan>

			<tspan dy="0">6</tspan>

</text>

The problem is that when I get all child nodes of the text node, the
resulted order is: 2 4 6 3 5. Does anyone know how can I get the same order
as it is in the svg file? 

Thanks in advance.
-- 
View this message in context: http://www.nabble.com/SVGOMDocument-and-getChildNodes-ordering-tp21254983p21254983.html
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: SVGOMDocument and getChildNodes ordering

Posted by mgenov <mg...@gmail.com>.
Hello again,  
  You were right, the problem was with the code :).

  Thanks a lot of for the support.


thomas.deweese wrote:
> 
> Hi Mgenov,
> 
> mgenov <mg...@gmail.com> wrote on 01/03/2009 10:58:57 AM:
> 
>> Hello Thomas, 
>>    Here is the whole code of the walk method:
> 
>     No it isn't, if you want more help please submit a 
> _complete_ standalone example that shows the bug.  Please 
> check my small exaple that works for me:
> 
> <svg width="655" height="600" viewBox="0 0 655 600"
>      xmlns="http://www.w3.org/2000/svg" 
>      xmlns:xlink="http://www.w3.org/1999/xlink" 
>      onload="checkChildren('foo', 'bar')">
> 
> <script type="text/ecmascript">
> <![CDATA[
> function checkChildren(src, dst) {
>    var s = document.getElementById(src);
>    var d = document.getElementById(dst);
>    var clst = s.childNodes;
>    var str = "";
>    for (var i=0; i<clst.length; i++) {
>       var c = clst.item(i);
>       if (c.localName == "tspan")
>          str += c.textContent + " ";
>    }
>    d.appendChild(document.createTextNode(str));
> }
>  ]]></script>
> 
> <text id="foo" font-size="20" transform="translate(20 40)">QUICK
> <tspan dy="20">BROWN</tspan>
> <tspan y="20">FOX</tspan>
> <tspan dy="20">JUMPS</tspan>
> <tspan y="60">OVER</tspan>
> <tspan dy="0">THE LAZY</tspan>
> </text>
> 
> <text id="bar" font-size="20" fill="blue" x="20" y="160"/>
> </svg>
> 
>>       Note that there are no any additional inserts or movements of the
>> nodes. Just walking through the tree. About the ordering probably this 
> is a
>> typing mistake. I made another test with similar xml. This is the result
>> from the application:
> 
>    I am fairly certain you are inserting/deleting elements somplace. 
> Perhaps this is hidden in the visitor code or something.
> 
>> It seems that there is some attribute sorting - first are all elements 
> with
>> dy attribute and then are these with y. Any idea where is the problem ? 
> 
>     Since the above example works for me I suspect the problem is in
> your code someplace.
> 
>> thomas.deweese wrote:
>> > 
>> > 
>> > 
>> >     Is your walker method moving the children around the child list 
>> > for the parent?  The DOM specifies that getChildNodes 
>> > returns a 'live' list of node children so that modifications to 
>> > the children list is immediately visible:
>> > 
>> > http://www.w3.org/TR/DOM-Level-2-Core/core.html#td-live
>> > 
>> >     This would mostly explain the odd ordering of children
>> > you see: 2 4 6 3 5
>> > 
>> >     Two is the first tspan, When it is removed/moved the 
>> > second child is '4' (since '3' became the first), then
>> > '6' is the third child ('3', '5', '6').  I'm not so sure 
>> > how you wind up with the '3' and '5'.
>> > 
>> 
>> -- 
>> View this message in context: http://www.nabble.com/SVGOMDocument-
>> and-getChildNodes-ordering-tp21254983p21266183.html
>> 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
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/SVGOMDocument-and-getChildNodes-ordering-tp21254983p21281044.html
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: SVGOMDocument and getChildNodes ordering

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

mgenov <mg...@gmail.com> wrote on 01/03/2009 10:58:57 AM:

> Hello Thomas, 
>    Here is the whole code of the walk method:

    No it isn't, if you want more help please submit a 
_complete_ standalone example that shows the bug.  Please 
check my small exaple that works for me:

<svg width="655" height="600" viewBox="0 0 655 600"
     xmlns="http://www.w3.org/2000/svg" 
     xmlns:xlink="http://www.w3.org/1999/xlink" 
     onload="checkChildren('foo', 'bar')">

<script type="text/ecmascript">
<![CDATA[
function checkChildren(src, dst) {
   var s = document.getElementById(src);
   var d = document.getElementById(dst);
   var clst = s.childNodes;
   var str = "";
   for (var i=0; i<clst.length; i++) {
      var c = clst.item(i);
      if (c.localName == "tspan")
         str += c.textContent + " ";
   }
   d.appendChild(document.createTextNode(str));
}
 ]]></script>

<text id="foo" font-size="20" transform="translate(20 40)">QUICK
<tspan dy="20">BROWN</tspan>
<tspan y="20">FOX</tspan>
<tspan dy="20">JUMPS</tspan>
<tspan y="60">OVER</tspan>
<tspan dy="0">THE LAZY</tspan>
</text>

<text id="bar" font-size="20" fill="blue" x="20" y="160"/>
</svg>

>       Note that there are no any additional inserts or movements of the
> nodes. Just walking through the tree. About the ordering probably this 
is a
> typing mistake. I made another test with similar xml. This is the result
> from the application:

   I am fairly certain you are inserting/deleting elements somplace. 
Perhaps this is hidden in the visitor code or something.

> It seems that there is some attribute sorting - first are all elements 
with
> dy attribute and then are these with y. Any idea where is the problem ? 

    Since the above example works for me I suspect the problem is in
your code someplace.

> thomas.deweese wrote:
> > 
> > 
> > 
> >     Is your walker method moving the children around the child list 
> > for the parent?  The DOM specifies that getChildNodes 
> > returns a 'live' list of node children so that modifications to 
> > the children list is immediately visible:
> > 
> > http://www.w3.org/TR/DOM-Level-2-Core/core.html#td-live
> > 
> >     This would mostly explain the odd ordering of children
> > you see: 2 4 6 3 5
> > 
> >     Two is the first tspan, When it is removed/moved the 
> > second child is '4' (since '3' became the first), then
> > '6' is the third child ('3', '5', '6').  I'm not so sure 
> > how you wind up with the '3' and '5'.
> > 
> 
> -- 
> View this message in context: http://www.nabble.com/SVGOMDocument-
> and-getChildNodes-ordering-tp21254983p21266183.html
> 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: SVGOMDocument and getChildNodes ordering

Posted by mgenov <mg...@gmail.com>.
Hello Thomas, 
   Here is the whole code from the walk method:
 public void walk(Node rootNode, NodeVisitor visitor) {
        if (rootNode == null || rootNode.getChildNodes().getLength() == 0) {
            return;
        }

        NodeList nodes = rootNode.getChildNodes();
        for (int i = 0;i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            boolean isVisitable = true;

            if (isValidTextNode(node)) {

                NodeList childNodes = node.getChildNodes();
                for (int j = 0;j < childNodes.getLength();j++) {
                    Node child = childNodes.item(j);
                    if (isTSpanNode(child)) { // print value.........
                        
                    }
                }


                
                visitor.visitTextNode(node);

                
            } else if (isGNode(node)) {
                Element n = (Element) node;

                String gAttrIdName = n.getAttribute(SVGOMDocument.ATTR_ID);

                 visitor.visitGNode(node);
                // empty groups shold be translated too
                if (translateEmptyGroups == true) {
                    visitor.visitGNode(node);
                } else  if (null != gAttrIdName || !"".equals(gAttrIdName))
{     // group is not empty
                    visitor.visitGNode(node);
                } else {

                    isVisitable = false;
                }

            }
            if (isVisitable) {
                walk(node, visitor);    
            }            
        }        

      Note that there are no any additional inserts or movements of the
nodes. Just walking through the tree. About the ordering probably this is a
typing mistake. I made another test with similar xml. This is the result
from the application:
    		<text stroke="blue" font-size="20" transform="matrix(1 0 0 1 20
40)">QUICK
			<tspan dy="20">BROWN</tspan>
			<tspan y="20">FOX</tspan>
			<tspan dy="20">JUMPS</tspan>
			<tspan y="60">OVER</tspan>
			<tspan dy="0">THE LAZY</tspan>
                </text>
My Program prints: 
BROWN
JUMPS
THE LAZY
FOX
OVER

It seems that there is some attribute sorting - first are all elements with
dy attribute and then are these with y. Any idea where is the problem ? 

Thanks in advance




thomas.deweese wrote:
> 
> 
> 
>     Is your walker method moving the children around the child list 
> for the parent?  The DOM specifies that getChildNodes 
> returns a 'live' list of node children so that modifications to 
> the children list is immediately visible:
> 
> http://www.w3.org/TR/DOM-Level-2-Core/core.html#td-live
> 
>     This would mostly explain the odd ordering of children
> you see: 2 4 6 3 5
> 
>     Two is the first tspan, When it is removed/moved the 
> second child is '4' (since '3' became the first), then
> '6' is the third child ('3', '5', '6').  I'm not so sure 
> how you wind up with the '3' and '5'.
> 

-- 
View this message in context: http://www.nabble.com/SVGOMDocument-and-getChildNodes-ordering-tp21254983p21266183.html
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: SVGOMDocument and getChildNodes ordering

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

mgenov <mg...@gmail.com> wrote on 01/03/2009 03:50:09 AM:

>   For walking through all nodes I made a NodeWallker of the svg document 
I
> made this class. This class has a walk method which traverses the whole 
tree
> and asks the visitor class to do something over some of the nodes. ( In 
my
> case these nodes should be tspan and text nodes. ).

>    And here is the code which I'm using for the retrieving of all tspan
> nodes:
>       NodeList childNodes = node.getChildNodes();
>       for (int j = 0;j < childNodes.getLength();j++) {
>                     Node childNode = childNodes.item(j);
>                     if (isTSpanNode(childNode)) {

    Is your walker method moving the children around the child list 
for the parent?  The DOM specifies that getChildNodes 
returns a 'live' list of node children so that modifications to 
the children list is immediately visible:

http://www.w3.org/TR/DOM-Level-2-Core/core.html#td-live

    This would mostly explain the odd ordering of children
you see: 2 4 6 3 5

    Two is the first tspan, When it is removed/moved the 
second child is '4' (since '3' became the first), then
'6' is the third child ('3', '5', '6').  I'm not so sure 
how you wind up with the '3' and '5'.

>     Hope that this information will help. If you thing that the 
information
> shown above is not enough, please let me know and I'll try to explain 
the
> situation in better way.
> 
> 
> Archie Cobbs-3 wrote:
> > 
> > I've never heard of this happening... they should always be returned 
in
> > proper order.
> > 
> > There's may be something wrong with how you're accessing the child 
nodes.
> > What does your loop look like?
> > 
> > -Archie
> > 
> > On Fri, Jan 2, 2009 at 12:09 PM, mgenov <mg...@gmail.com> wrote:
> > 
> >>
> >> Hello guys,
> >>  I'm encountering problem with the ordering of the tspan nodes in an 
svg
> >> document. I have a document with text node and several child nodes of
> >> type
> >> tspan. My svg file looks similar to this one:
> >>
> >> <text stroke="blue" font-size="20" transform="matrix(1 0 0 1 20 
40)">1
> >>
> >>                        <tspan dy="20">2</tspan>
> >>
> >>                        <tspan y="20">3</tspan>
> >>
> >>                        <tspan dy="20">4</tspan>
> >>
> >>                        <tspan y="60">5</tspan>
> >>
> >>                        <tspan dy="0">6</tspan>
> >>
> >> </text>
> >>
> >> The problem is that when I get all child nodes of the text node, the
> >> resulted order is: 2 4 6 3 5. Does anyone know how can I get the same
> >> order
> >> as it is in the svg file?
> >>
> >> Thanks in advance.
> >> --
> >> View this message in context:
> >> http://www.nabble.com/SVGOMDocument-and-getChildNodes-ordering-
> tp21254983p21254983.html
> >> 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
> >>
> >>
> > 
> > 
> > -- 
> > Archie L. Cobbs
> > 
> > 
> 
> -- 
> View this message in context: http://www.nabble.com/SVGOMDocument-
> and-getChildNodes-ordering-tp21254983p21263112.html
> 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: SVGOMDocument and getChildNodes ordering

Posted by mgenov <mg...@gmail.com>.
Hello, 
  Here is are some snippets from my code and also a little explanation which
part what is doing and what is it's purpose.
  
  For walking through all nodes I made a NodeWallker of the svg document I
made this class. This class has a walk method which traverses the whole tree
and asks the visitor class to do something over some of the nodes. ( In my
case these nodes should be tspan and text nodes. ).
   
     Here is the sugnature of the walk method:
     public void walk(Node rootNode, NodeVisitor visitor) {
      ..........
     }
   Here is the invoking of the walk method:
   walker.walk(m_svgDocument.getRootElement(), ruleVisitor);
   
   Where the m_svgDocument is created from the code shown below:
       SAXSVGDocumentFactory df = new SAXSVGDocumentFactory(
       XMLResourceDescriptor.getXMLParserClassName() );
       df.setValidating(false);
       StringReader reader = new StringReader( svgContent );
                
       m_svgDocument = ( SVGOMDocument ) df.createSVGDocument(
fileAbsoluthPath, reader );
    
   And here is the code which I'm using for the retrieving of all tspan
nodes:
      NodeList childNodes = node.getChildNodes();
      for (int j = 0;j < childNodes.getLength();j++) {
                    Node childNode = childNodes.item(j);
                    if (isTSpanNode(childNode)) {
                        ...
                    }
       }
   
    Hope that this information will help. If you thing that the information
shown above is not enough, please let me know and I'll try to explain the
situation in better way.


Archie Cobbs-3 wrote:
> 
> I've never heard of this happening... they should always be returned in
> proper order.
> 
> There's may be something wrong with how you're accessing the child nodes.
> What does your loop look like?
> 
> -Archie
> 
> On Fri, Jan 2, 2009 at 12:09 PM, mgenov <mg...@gmail.com> wrote:
> 
>>
>> Hello guys,
>>  I'm encountering problem with the ordering of the tspan nodes in an svg
>> document. I have a document with text node and several child nodes of
>> type
>> tspan. My svg file looks similar to this one:
>>
>> <text stroke="blue" font-size="20" transform="matrix(1 0 0 1 20 40)">1
>>
>>                        <tspan dy="20">2</tspan>
>>
>>                        <tspan y="20">3</tspan>
>>
>>                        <tspan dy="20">4</tspan>
>>
>>                        <tspan y="60">5</tspan>
>>
>>                        <tspan dy="0">6</tspan>
>>
>> </text>
>>
>> The problem is that when I get all child nodes of the text node, the
>> resulted order is: 2 4 6 3 5. Does anyone know how can I get the same
>> order
>> as it is in the svg file?
>>
>> Thanks in advance.
>> --
>> View this message in context:
>> http://www.nabble.com/SVGOMDocument-and-getChildNodes-ordering-tp21254983p21254983.html
>> 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
>>
>>
> 
> 
> -- 
> Archie L. Cobbs
> 
> 

-- 
View this message in context: http://www.nabble.com/SVGOMDocument-and-getChildNodes-ordering-tp21254983p21263112.html
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: SVGOMDocument and getChildNodes ordering

Posted by Archie Cobbs <ar...@gmail.com>.
I've never heard of this happening... they should always be returned in
proper order.

There's may be something wrong with how you're accessing the child nodes.
What does your loop look like?

-Archie

On Fri, Jan 2, 2009 at 12:09 PM, mgenov <mg...@gmail.com> wrote:

>
> Hello guys,
>  I'm encountering problem with the ordering of the tspan nodes in an svg
> document. I have a document with text node and several child nodes of type
> tspan. My svg file looks similar to this one:
>
> <text stroke="blue" font-size="20" transform="matrix(1 0 0 1 20 40)">1
>
>                        <tspan dy="20">2</tspan>
>
>                        <tspan y="20">3</tspan>
>
>                        <tspan dy="20">4</tspan>
>
>                        <tspan y="60">5</tspan>
>
>                        <tspan dy="0">6</tspan>
>
> </text>
>
> The problem is that when I get all child nodes of the text node, the
> resulted order is: 2 4 6 3 5. Does anyone know how can I get the same order
> as it is in the svg file?
>
> Thanks in advance.
> --
> View this message in context:
> http://www.nabble.com/SVGOMDocument-and-getChildNodes-ordering-tp21254983p21254983.html
> 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
>
>


-- 
Archie L. Cobbs