You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2016/10/09 20:53:11 UTC

svn commit: r1764013 - in /webservices/axiom/trunk: aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/ testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/ testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/bui...

Author: veithen
Date: Sun Oct  9 20:53:11 2016
New Revision: 1764013

URL: http://svn.apache.org/viewvc?rev=1764013&view=rev
Log:
AXIOM-487: Change the methods that create builders from DOM trees so that namespace unaware nodes are converted to namespace aware nodes (if this can be done without knowing the namespace context) or trigger meaningful exceptions.

Added:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilter.java   (with props)
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilterHandler.java   (with props)
    webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareNamespaceDeclaration.java   (with props)
    webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawarePrefixedAttribute.java   (with props)
    webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareUnprefixedAttribute.java   (with props)
Modified:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/BuilderSpec.java
    webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
    webservices/axiom/trunk/testing/axiom-truth/src/main/java/org/apache/axiom/truth/OMElementSubject.java

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/BuilderSpec.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/BuilderSpec.java?rev=1764013&r1=1764012&r2=1764013&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/BuilderSpec.java (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/BuilderSpec.java Sun Oct  9 20:53:11 2016
@@ -171,7 +171,13 @@ final class BuilderSpec {
     }
 
     static BuilderSpec from(Node node, boolean expandEntityReferences) {
-        return new BuilderSpec(new FilteredXmlInput(new DOMInput(node, expandEntityReferences), NamespaceRepairingFilter.DEFAULT), null);
+        return new BuilderSpec(
+                new FilteredXmlInput(
+                        new FilteredXmlInput(
+                                new DOMInput(node, expandEntityReferences),
+                                NSUnawareNodeFilter.INSTANCE),
+                        NamespaceRepairingFilter.DEFAULT),
+                null);
     }
 
     static BuilderSpec from(SAXSource source, boolean expandEntityReferences) {

Added: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilter.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilter.java?rev=1764013&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilter.java (added)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilter.java Sun Oct  9 20:53:11 2016
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.om.impl.common.factory;
+
+import org.apache.axiom.core.stream.XmlFilter;
+import org.apache.axiom.core.stream.XmlHandler;
+
+/**
+ * Filters out namespace unaware nodes, by triggering an exception or converting them to namespace
+ * aware nodes, if this can be done without knowledge of the namespace context. The latter is true
+ * for namespace declarations and unprefixed attributes.
+ */
+final class NSUnawareNodeFilter implements XmlFilter {
+    static final NSUnawareNodeFilter INSTANCE = new NSUnawareNodeFilter();
+    
+    private NSUnawareNodeFilter() {}
+
+    @Override
+    public XmlHandler createFilterHandler(XmlHandler parent) {
+        return new NSUnawareNodeFilterHandler(parent);
+    }
+}

Propchange: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilterHandler.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilterHandler.java?rev=1764013&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilterHandler.java (added)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilterHandler.java Sun Oct  9 20:53:11 2016
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.om.impl.common.factory;
+
+import javax.xml.XMLConstants;
+
+import org.apache.axiom.core.stream.StreamException;
+import org.apache.axiom.core.stream.XmlHandler;
+import org.apache.axiom.core.stream.XmlHandlerWrapper;
+import org.apache.axiom.om.OMException;
+
+final class NSUnawareNodeFilterHandler extends XmlHandlerWrapper {
+    NSUnawareNodeFilterHandler(XmlHandler parent) {
+        super(parent);
+    }
+
+    @Override
+    public void processAttribute(String name, String value, String type, boolean specified)
+            throws StreamException {
+        int idx = name.indexOf(':');
+        if (idx == -1) {
+            if (name.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
+                super.processNamespaceDeclaration("", value);
+            } else {
+                super.processAttribute("", name, "", value, type, specified);
+            }
+        } else {
+            if (idx == 5 && name.startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {
+                super.processNamespaceDeclaration(name.substring(6), value);
+            } else {
+                throw new OMException("Namespace unware attributes not supported");
+            }
+        }
+    }
+}

Propchange: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/NSUnawareNodeFilterHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java?rev=1764013&r1=1764012&r2=1764013&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java (original)
+++ webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java Sun Oct  9 20:53:11 2016
@@ -118,6 +118,10 @@ public class OMTestSuiteBuilder extends
             }
         }
         addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOMElement(metaFactory));
+        addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOMWithNSUnawareNamespaceDeclaration(metaFactory, ""));
+        addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOMWithNSUnawareNamespaceDeclaration(metaFactory, "p"));
+        addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOMWithNSUnawarePrefixedAttribute(metaFactory));
+        addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOMWithNSUnawareUnprefixedAttribute(metaFactory));
         for (XOPSample sample : getInstances(XOPSample.class)) {
             addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderXOP(metaFactory, sample, false));
             addTest(new org.apache.axiom.ts.om.builder.TestCreateOMBuilderXOP(metaFactory, sample, true));

Added: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareNamespaceDeclaration.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareNamespaceDeclaration.java?rev=1764013&view=auto
==============================================================================
--- webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareNamespaceDeclaration.java (added)
+++ webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareNamespaceDeclaration.java Sun Oct  9 20:53:11 2016
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.ts.om.builder;
+
+import static org.apache.axiom.truth.AxiomTruth.assertThat;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.ts.AxiomTestCase;
+import org.apache.axiom.ts.jaxp.DOMImplementation;
+import org.w3c.dom.Element;
+
+public class TestCreateOMBuilderFromDOMWithNSUnawareNamespaceDeclaration extends AxiomTestCase {
+    private final String prefix;
+
+    public TestCreateOMBuilderFromDOMWithNSUnawareNamespaceDeclaration(OMMetaFactory metaFactory, String prefix) {
+        super(metaFactory);
+        this.prefix = prefix;
+        addTestParameter("prefix", prefix);
+    }
+
+    @Override
+    protected void runTest() throws Throwable {
+        Element domElement = DOMImplementation.XERCES.newDocument().createElementNS(null, "test");
+        domElement.setAttribute(prefix.isEmpty() ? "xmlns" : "xmlns:" + prefix, "urn:ns1");
+        OMElement element = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), domElement, false).getDocumentElement();
+        assertThat(element).hasNamespaceDeclaration(prefix, "urn:ns1");
+    }
+}

Propchange: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareNamespaceDeclaration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawarePrefixedAttribute.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawarePrefixedAttribute.java?rev=1764013&view=auto
==============================================================================
--- webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawarePrefixedAttribute.java (added)
+++ webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawarePrefixedAttribute.java Sun Oct  9 20:53:11 2016
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.ts.om.builder;
+
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.ts.AxiomTestCase;
+import org.apache.axiom.ts.jaxp.DOMImplementation;
+import org.w3c.dom.Element;
+
+public class TestCreateOMBuilderFromDOMWithNSUnawarePrefixedAttribute extends AxiomTestCase {
+    public TestCreateOMBuilderFromDOMWithNSUnawarePrefixedAttribute(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    @Override
+    protected void runTest() throws Throwable {
+        Element domElement = DOMImplementation.XERCES.newDocument().createElementNS(null, "test");
+        domElement.setAttribute("p:attr", "value");
+        try {
+            OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), domElement, false).getDocument().build();
+            fail("Expected OMException");
+        } catch (OMException ex) {
+            // Expected
+        }
+    }
+}

Propchange: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawarePrefixedAttribute.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareUnprefixedAttribute.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareUnprefixedAttribute.java?rev=1764013&view=auto
==============================================================================
--- webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareUnprefixedAttribute.java (added)
+++ webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareUnprefixedAttribute.java Sun Oct  9 20:53:11 2016
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.ts.om.builder;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.ts.AxiomTestCase;
+import org.apache.axiom.ts.jaxp.DOMImplementation;
+import org.w3c.dom.Element;
+
+public class TestCreateOMBuilderFromDOMWithNSUnawareUnprefixedAttribute extends AxiomTestCase {
+    public TestCreateOMBuilderFromDOMWithNSUnawareUnprefixedAttribute(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    @Override
+    protected void runTest() throws Throwable {
+        Element domElement = DOMImplementation.XERCES.newDocument().createElementNS(null, "test");
+        domElement.setAttribute("attr", "value");
+        OMElement element = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), domElement, false).getDocumentElement();
+        assertThat(element.getAttributeValue(new QName("attr"))).isEqualTo("value");
+    }
+}

Propchange: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/builder/TestCreateOMBuilderFromDOMWithNSUnawareUnprefixedAttribute.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/testing/axiom-truth/src/main/java/org/apache/axiom/truth/OMElementSubject.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-truth/src/main/java/org/apache/axiom/truth/OMElementSubject.java?rev=1764013&r1=1764012&r2=1764013&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/axiom-truth/src/main/java/org/apache/axiom/truth/OMElementSubject.java (original)
+++ webservices/axiom/trunk/testing/axiom-truth/src/main/java/org/apache/axiom/truth/OMElementSubject.java Sun Oct  9 20:53:11 2016
@@ -45,4 +45,15 @@ public final class OMElementSubject exte
         fail("has namespace declaration for namespace URI \"" + ns.getNamespaceURI()
                 + "\" and prefix \"" + ns.getPrefix() + "\"");
     }
+
+    public void hasNamespaceDeclaration(String prefix, String namespaceURI) {
+        for (Iterator<OMNamespace> it = actual().getAllDeclaredNamespaces(); it.hasNext(); ) {
+            OMNamespace ns = it.next();
+            if (ns.getPrefix().equals(prefix) && ns.getNamespaceURI().equals(namespaceURI)) {
+                return;
+            }
+        }
+        fail("has namespace declaration for namespace URI \"" + namespaceURI
+                + "\" and prefix \"" + prefix + "\"");
+    }
 }