ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r1701358 - in /webservices/axiom/trunk: aspects/core-aspects/src/main/java/org/apache/axiom/core/ aspects/dom-aspects/src/main/java/org/apache/axiom/dom/ aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/ axiom-api/sr...
Date Sat, 05 Sep 2015 08:46:05 GMT
Author: veithen
Date: Sat Sep  5 08:46:04 2015
New Revision: 1701358

URL: http://svn.apache.org/r1701358
Log:
Implement a smarter way to skip document whitespace in DOOM.

Added:
    webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeFilter.java
  (with props)
    webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DocumentWhitespaceFilter.java
  (with props)
Modified:
    webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreChildNodeSupport.aj
    webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreParentNodeSupport.aj
    webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMChildNodeSupport.aj
    webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMParentNodeSupport.aj
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/AxiomNodeFactorySupport.aj
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMHierarchyException.java
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
    webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
    webservices/axiom/trunk/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestWhitespaceAroundDocumentElement.java

Modified: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreChildNodeSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreChildNodeSupport.aj?rev=1701358&r1=1701357&r2=1701358&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreChildNodeSupport.aj
(original)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreChildNodeSupport.aj
Sat Sep  5 08:46:04 2015
@@ -89,6 +89,14 @@ public aspect CoreChildNodeSupport {
         return previousSibling;
     }
     
+    public final CoreChildNode CoreChildNode.coreGetPreviousSibling(NodeFilter filter) {
+        CoreChildNode sibling = coreGetPreviousSibling();
+        while (sibling != null && !filter.accept(sibling)) {
+            sibling = sibling.coreGetPreviousSibling();
+        }
+        return sibling;
+    }
+    
     public final void CoreChildNode.coreSetPreviousSibling(CoreChildNode previousSibling)
{
         this.previousSibling = previousSibling;
     }
@@ -113,6 +121,14 @@ public aspect CoreChildNodeSupport {
         return nextSibling;
     }
 
+    public final CoreChildNode CoreChildNode.coreGetNextSibling(NodeFilter filter) {
+        CoreChildNode sibling = coreGetNextSibling();
+        while (sibling != null && !filter.accept(sibling)) {
+            sibling = sibling.coreGetNextSibling();
+        }
+        return sibling;
+    }
+    
     public final void CoreChildNode.coreInsertSiblingAfter(CoreChildNode sibling) {
         CoreParentNode parent = coreGetParent();
         // TODO: don't use OMException here

Modified: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreParentNodeSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreParentNodeSupport.aj?rev=1701358&r1=1701357&r2=1701358&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreParentNodeSupport.aj
(original)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreParentNodeSupport.aj
Sat Sep  5 08:46:04 2015
@@ -118,11 +118,27 @@ public aspect CoreParentNodeSupport {
         return firstChild;
     }
 
+    public final CoreChildNode CoreParentNode.coreGetFirstChild(NodeFilter filter) {
+        CoreChildNode child = coreGetFirstChild();
+        while (child != null && !filter.accept(child)) {
+            child = child.coreGetNextSibling();
+        }
+        return child;
+    }
+    
     public final CoreChildNode CoreParentNode.coreGetLastChild() {
         build();
         return coreGetLastKnownChild();
     }
 
+    public final CoreChildNode CoreParentNode.coreGetLastChild(NodeFilter filter) {
+        CoreChildNode child = coreGetLastChild();
+        while (child != null && !filter.accept(child)) {
+            child = child.coreGetPreviousSibling();
+        }
+        return child;
+    }
+    
     public final void CoreParentNode.coreAppendChild(CoreChildNode child, boolean fromBuilder)
{
         CoreParentNode parent = child.coreGetParent();
         if (!fromBuilder) {

Added: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeFilter.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeFilter.java?rev=1701358&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeFilter.java
(added)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeFilter.java
Sat Sep  5 08:46:04 2015
@@ -0,0 +1,23 @@
+/*
+ * 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.core;
+
+public interface NodeFilter {
+    boolean accept(CoreNode node);
+}

Propchange: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMChildNodeSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMChildNodeSupport.aj?rev=1701358&r1=1701357&r2=1701358&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMChildNodeSupport.aj
(original)
+++ webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMChildNodeSupport.aj
Sat Sep  5 08:46:04 2015
@@ -26,10 +26,10 @@ public aspect DOMChildNodeSupport {
     }
     
     public final Node DOMChildNode.getNextSibling() {
-        return (Node)coreGetNextSibling();
+        return (Node)coreGetNextSibling(DocumentWhitespaceFilter.INSTANCE);
     }
     
     public final Node DOMChildNode.getPreviousSibling() {
-        return (Node)coreGetPreviousSibling();
+        return (Node)coreGetPreviousSibling(DocumentWhitespaceFilter.INSTANCE);
     }
 }

Modified: webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMParentNodeSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMParentNodeSupport.aj?rev=1701358&r1=1701357&r2=1701358&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMParentNodeSupport.aj
(original)
+++ webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMParentNodeSupport.aj
Sat Sep  5 08:46:04 2015
@@ -55,11 +55,11 @@ public aspect DOMParentNodeSupport {
     }
 
     public final Node DOMParentNode.getFirstChild() {
-        return (Node)coreGetFirstChild();
+        return (Node)coreGetFirstChild(DocumentWhitespaceFilter.INSTANCE);
     }
 
     public final Node DOMParentNode.getLastChild() {
-        return (Node)coreGetLastChild();
+        return (Node)coreGetLastChild(DocumentWhitespaceFilter.INSTANCE);
     }
 
     public final boolean DOMParentNode.hasChildNodes() {

Added: webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DocumentWhitespaceFilter.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DocumentWhitespaceFilter.java?rev=1701358&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DocumentWhitespaceFilter.java
(added)
+++ webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DocumentWhitespaceFilter.java
Sat Sep  5 08:46:04 2015
@@ -0,0 +1,36 @@
+/*
+ * 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.dom;
+
+import org.apache.axiom.core.CoreCharacterDataNode;
+import org.apache.axiom.core.CoreDocument;
+import org.apache.axiom.core.CoreNode;
+import org.apache.axiom.core.NodeFilter;
+import org.apache.axiom.core.NodeType;
+
+final class DocumentWhitespaceFilter implements NodeFilter {
+    final static DocumentWhitespaceFilter INSTANCE = new DocumentWhitespaceFilter();
+    
+    private DocumentWhitespaceFilter() {}
+
+    public boolean accept(CoreNode node) {
+        return node.coreGetNodeType() != NodeType.CHARACTER_DATA
+                || !(((CoreCharacterDataNode)node).coreGetParent() instanceof CoreDocument);
+    }
+}

Propchange: webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DocumentWhitespaceFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/AxiomNodeFactorySupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/AxiomNodeFactorySupport.aj?rev=1701358&r1=1701357&r2=1701358&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/AxiomNodeFactorySupport.aj
(original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/factory/AxiomNodeFactorySupport.aj
Sat Sep  5 08:46:04 2015
@@ -88,9 +88,6 @@ public aspect AxiomNodeFactorySupport {
         return node;
     }
 
-    public void AxiomNodeFactory.validateOMTextParent(OMContainer parent) {
-    }
-    
     private AxiomText AxiomNodeFactory.createAxiomText(OMContainer parent, Object content,
int type, boolean fromBuilder) {
         AxiomText node;
         switch (type) {
@@ -112,7 +109,6 @@ public aspect AxiomNodeFactorySupport {
                 throw new IllegalArgumentException("Invalid node type");
         }
         if (parent != null) {
-            validateOMTextParent(parent);
             ((OMContainerEx)parent).addChild(node, fromBuilder);
         }
         node.coreSetCharacterData(content, Policies.DETACH_POLICY);

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMHierarchyException.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMHierarchyException.java?rev=1701358&r1=1701357&r2=1701358&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMHierarchyException.java
(original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/OMHierarchyException.java
Sat Sep  5 08:46:04 2015
@@ -20,16 +20,7 @@
 package org.apache.axiom.om;
 
 /**
- * Thrown if an object model operation would lead to a hierarchy that is not allowed in the
- * given object model implementation.
- * <p>
- * If this exception is encountered by a builder when creating an OM node from an event received
- * by the parser and if the corresponding content can be ignored (i.e. is not semantically
- * relevant), the builder should ignore the exception and skip the event. An example is whitespace
- * appearing before or after the root element of a document. This would be represented as
an
- * {@link OMText} node below the {@link OMDocument}. If the OM implementation doesn't allow
text
- * nodes as children of a document (as for example in DOM), it should throw this exception
so
- * that the builder can discard the event. 
+ * Thrown if an object model operation would lead to a hierarchy that is not allowed.
  */
 public class OMHierarchyException extends OMException {
     private static final long serialVersionUID = 8391435427221729190L;

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java?rev=1701358&r1=1701357&r2=1701358&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
(original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
Sat Sep  5 08:46:04 2015
@@ -27,7 +27,6 @@ import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMFactory;
-import org.apache.axiom.om.OMHierarchyException;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMXMLBuilderFactory;
 import org.apache.axiom.om.impl.OMContainerEx;
@@ -237,17 +236,7 @@ public class StAXOMBuilder extends StAXB
                     target = null;
                     break;
                 case XMLStreamConstants.SPACE:
-                    try {
-                        OMNode node = createOMText(XMLStreamConstants.SPACE);
-                        if (node == null) {
-                            continue;
-                        }
-                    } catch (OMHierarchyException ex) {
-                        // The OM implementation doesn't allow text nodes at the current
-                        // position in the tree. Since it is only whitespace, we can safely
-                        // skip this event.
-                        continue;
-                    }
+                    createOMText(XMLStreamConstants.SPACE);
                     break;
                 case XMLStreamConstants.COMMENT:
                     createComment();

Modified: webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java?rev=1701358&r1=1701357&r2=1701358&view=diff
==============================================================================
--- webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
(original)
+++ webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
Sat Sep  5 08:46:04 2015
@@ -47,8 +47,6 @@ import org.apache.axiom.dom.DOMNamespace
 import org.apache.axiom.dom.DOMNodeFactory;
 import org.apache.axiom.dom.DOMProcessingInstruction;
 import org.apache.axiom.dom.DOMText;
-import org.apache.axiom.om.OMContainer;
-import org.apache.axiom.om.OMHierarchyException;
 import org.apache.axiom.om.OMMetaFactory;
 import org.apache.axiom.om.impl.common.AxiomAttribute;
 import org.apache.axiom.om.impl.common.AxiomCDATASection;
@@ -136,13 +134,6 @@ public class OMDOMFactory implements Axi
         return metaFactory;
     }
 
-    public final void validateOMTextParent(OMContainer parent) {
-        if (parent instanceof DocumentImpl) {
-            throw new OMHierarchyException(
-                    "DOM doesn't support text nodes as children of a document");
-        }
-    }
-    
     public final <T extends CoreNode> T createNode(Class<T> type) {
         CoreNode node;
         if (type == CoreCDATASection.class || type == AxiomCDATASection.class || type ==
DOMCDATASection.class) {

Modified: webservices/axiom/trunk/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestWhitespaceAroundDocumentElement.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestWhitespaceAroundDocumentElement.java?rev=1701358&r1=1701357&r2=1701358&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestWhitespaceAroundDocumentElement.java
(original)
+++ webservices/axiom/trunk/testing/dom-testsuite/src/main/java/org/apache/axiom/ts/dom/builder/TestWhitespaceAroundDocumentElement.java
Sat Sep  5 08:46:04 2015
@@ -18,6 +18,8 @@
  */
 package org.apache.axiom.ts.dom.builder;
 
+import static com.google.common.truth.Truth.assertThat;
+
 import java.io.StringReader;
 
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -25,13 +27,13 @@ import javax.xml.parsers.DocumentBuilder
 import org.apache.axiom.ts.dom.DOMTestCase;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
 import org.xml.sax.InputSource;
 
 /**
- * Test that whitespace around the document element is discarded. Indeed, DOM doesn't allow
text
- * nodes as children of a document and we need to check that the builder silently discards
the
- * corresponding events received from the parser.
+ * Test that whitespace around the document element is discarded (or not visible). Indeed,
DOM
+ * doesn't allow text nodes as children of a document.
  */
 public class TestWhitespaceAroundDocumentElement extends DOMTestCase {
     public TestWhitespaceAroundDocumentElement(DocumentBuilderFactory dbf) {
@@ -39,11 +41,21 @@ public class TestWhitespaceAroundDocumen
     }
 
     protected void runTest() throws Throwable {
-        Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader("<!--
--> <root/> ")));
+        Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader("
<!-- --> <root/> ")));
         Node child = doc.getFirstChild();
         do {
-            assertFalse(child instanceof Text);
+            assertThat(child).isNotInstanceOf(Text.class);
             child = child.getNextSibling();
         } while (child != null);
+        
+        child = doc.getLastChild();
+        do {
+            assertThat(child).isNotInstanceOf(Text.class);
+            child = child.getPreviousSibling();
+        } while (child != null);
+        
+        NodeList children = doc.getChildNodes();
+        assertThat(children.getLength()).isEqualTo(2);
+        assertThat(children.item(1)).isSameAs(doc.getDocumentElement());
     }
 }



Mime
View raw message