[ 
https://issues.apache.org/jira/browse/AXIS2-565?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12500213
 ] 

Michael Krumpus commented on AXIS2-565:
---------------------------------------

I know that the root problem here was closed, but I do think that the issue of 
unnecessarily *adding* numerous namespace declarations to a document is a 
problem.  Real web services may need to produce very long documents, and the 
network and CPU overhead of dealing with thousands of duplicate namespace 
declarations is significant.  (Yes, in the real world there may be thousands!). 
 

It looks to me like the problem is in 
StAXOMBuilder.processNamespaceData(OMElement element).  Here is the relevant 
part of the code:

        if (namespaceURI != null && namespaceURI.length() > 0) {
            OMNamespace namespace = node.findNamespace(namespaceURI, prefix);
            if (namespace == null || namespace.getPrefix() != prefix) {
                if (prefix == null || "".equals(prefix)) {
                    namespace = node.declareDefaultNamespace(namespaceURI);
                } else {
                    namespace = node.declareNamespace(namespaceURI, prefix);
                }
            } 
            node.setNamespaceWithNoFindInCurrentScope(namespace);
        }

The conditional
if (namespace == null || namespace.getPrefix() != prefix) {

says that if a namespace declaration is found somewhere in an ancestor element, 
the prefix of the local element be equal to the prefix of the namespace found.  
Why?  Typically the local prefix returned from the StAX parser is "".  In this 
case, this code will declare a default namespace for the element instead of 
reusing the prefix already defined.  If the conditional is changed to:
if (namespace == null)

then the namespace found with findNamespace(uri, prefix) will be used and we 
get nice re-use of the existing prefix without declaring more namespaces 
(default or otherwise).  Is this the right change to make?  Will it break other 
things?  In the cases I've looked at, this change results in equivalent but 
less verbose documents.

Also, the API documentation for findNamespace(uri, prefix) states that either 
uri or prefix should be null, yet this code happily calls findNamespace with 
non-null values for each.  Is that a bug?


> Namespace vanishes using StAXOMBuilder
> --------------------------------------
>
>                 Key: AXIS2-565
>                 URL: https://issues.apache.org/jira/browse/AXIS2-565
>             Project: Axis 2.0 (Axis2)
>          Issue Type: Bug
>    Affects Versions: 0.95
>         Environment: Windows XP, Java 1.5
>            Reporter: Ant Grinyer
>            Assignee: Eran Chinthaka
>
> Below is the simple Junit test code which demonstrates the bug comments:
> package com.mytest;
> import org.apache.ws.commons.om.impl.builder.StAXOMBuilder;
> import org.apache.ws.commons.om.impl.llom.factory.OMXMLBuilderFactory;
> import org.apache.ws.commons.om.OMAbstractFactory;
> import org.jdom.Document;
> import org.jdom.output.XMLOutputter;
> import org.jdom.input.SAXBuilder;
> import javax.xml.stream.XMLStreamReader;
> import javax.xml.stream.XMLInputFactory;
> import java.io.StringReader;
> import junit.framework.TestCase;
> /**
>  * Test class to demonstrate namespace problem.
>  *
>  * Date: 11-Apr-2006
>  */
> public class Axis2Test extends TestCase {
>     public Axis2Test(String string) { super(string); }
>     protected void setUp() throws Exception { super.setUp(); }
>     protected void tearDown() throws Exception { super.tearDown(); }
>     public void testNameSpaces() {
>         StringBuffer sb = new StringBuffer();
>         sb.append("<root>");
>         sb.append("  <node>Some text</node>");
>         sb.append("  <node>More test</node>");
>         sb.append("  <items>");
>         sb.append("    <item shape=\"rectangle\" 
> xmlns:col=\"urn:mycolor\"><col:color rgb=\"#FF0000\">red</col:color><name>My 
> fine <b>item</b></name></item>");
>         sb.append("    <item shape=\"circle\" 
> xmlns:col=\"urn:mycolor\"><col:color 
> rgb=\"#0000FF\">blue</col:color><name>Something else</name></item>");
>         sb.append("  </items>");
>         sb.append("  <shapes>");
>         sb.append("    <shape>rectangle</shape>");
>         sb.append("    <shape>circle</shape>");
>         sb.append("    <shape basedOn=\"rectangle\">square</shape>");
>         sb.append("  </shapes>");
>         sb.append("</root>");
>         try {
>             // TRY WITH AXIS2 (loses xmlns:col="urn:mycolor" namespace on
>             // the <item shape="circle" xmlns:col="urn:mycolor"> element
>             XMLStreamReader parser = 
> XMLInputFactory.newInstance().createXMLStreamReader(new 
> StringReader(sb.toString()));
>             StAXOMBuilder builder = 
> OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(),parser);
>             System.out.println(builder.getDocumentElement().toString());
>             // TRY JDOM (works fine)
>             Document d = new SAXBuilder().build(new 
> StringReader(sb.toString()));
>             new XMLOutputter().output(d, System.out);
>         } catch (Exception e) {
>             e.printStackTrace(System.out);
>         }
>     }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to