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/04/22 09:10:05 UTC
svn commit: r1740454 - in /webservices/axiom/trunk:
aspects/core-aspects/src/main/java/org/apache/axiom/core/
aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/
implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ tes...
Author: veithen
Date: Fri Apr 22 07:10:04 2016
New Revision: 1740454
URL: http://svn.apache.org/viewvc?rev=1740454&view=rev
Log:
Check allowed child types in the core model.
Added:
webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/ChildNotAllowedException.java (with props)
Modified:
webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeType.java
webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/CoreDocumentSupport.aj
webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/CoreParentNodeSupport.aj
webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java
webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/container/TestGetDescendants.java
Added: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/ChildNotAllowedException.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/ChildNotAllowedException.java?rev=1740454&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/ChildNotAllowedException.java (added)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/ChildNotAllowedException.java Fri Apr 22 07:10:04 2016
@@ -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.core;
+
+/**
+ * Indicates that an attempt was made to insert a child node where it is not allowed. This exception
+ * may be thrown because the node type is not allowed (e.g. a processing instruction as a child of
+ * an attribute) or because the parent node only allows a single child of a given type (e.g. an
+ * element as a child of a document) and a child of that type already exists.
+ */
+public class ChildNotAllowedException extends HierarchyException {
+ private static final long serialVersionUID = 1L;
+
+ public ChildNotAllowedException() {
+ }
+
+ public ChildNotAllowedException(String message) {
+ super(message);
+ }
+}
Propchange: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/ChildNotAllowedException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeType.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeType.java?rev=1740454&r1=1740453&r2=1740454&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeType.java (original)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/NodeType.java Fri Apr 22 07:10:04 2016
@@ -18,6 +18,8 @@
*/
package org.apache.axiom.core;
+import java.util.EnumSet;
+
public enum NodeType {
/**
* The node is a {@link CoreDocument}.
@@ -84,7 +86,21 @@ public enum NodeType {
*/
ENTITY_REFERENCE(CoreEntityReference.class);
+ static {
+ COMMENT.allowedChildTypes = EnumSet.of(CHARACTER_DATA);
+ DOCUMENT.allowedChildTypes = EnumSet.of(CHARACTER_DATA, COMMENT, DOCUMENT_TYPE_DECLARATION, NS_AWARE_ELEMENT, NS_UNAWARE_ELEMENT, PROCESSING_INSTRUCTION);
+ DOCUMENT_FRAGMENT.allowedChildTypes = EnumSet.allOf(NodeType.class);
+ EnumSet<NodeType> s = EnumSet.of(CHARACTER_DATA, ENTITY_REFERENCE);
+ NS_AWARE_ATTRIBUTE.allowedChildTypes = s;
+ NS_UNAWARE_ATTRIBUTE.allowedChildTypes = s;
+ NAMESPACE_DECLARATION.allowedChildTypes = s;
+ s = EnumSet.of(CDATA_SECTION, CHARACTER_DATA, COMMENT, ENTITY_REFERENCE, NS_AWARE_ELEMENT, NS_UNAWARE_ELEMENT, PROCESSING_INSTRUCTION);
+ NS_AWARE_ELEMENT.allowedChildTypes = s;
+ NS_UNAWARE_ELEMENT.allowedChildTypes = s;
+ }
+
private final Class<? extends CoreNode> iface;
+ private EnumSet<NodeType> allowedChildTypes;
private NodeType(Class<? extends CoreNode> iface) {
this.iface = iface;
@@ -93,4 +109,11 @@ public enum NodeType {
public Class<? extends CoreNode> getInterface() {
return iface;
}
+
+ public boolean isChildTypeAllowed(NodeType childType) {
+ if (allowedChildTypes == null) {
+ throw new UnsupportedOperationException();
+ }
+ return allowedChildTypes.contains(childType);
+ }
}
Modified: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/CoreDocumentSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/CoreDocumentSupport.aj?rev=1740454&r1=1740453&r2=1740454&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/CoreDocumentSupport.aj (original)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/CoreDocumentSupport.aj Fri Apr 22 07:10:04 2016
@@ -18,6 +18,7 @@
*/
package org.apache.axiom.core.impl.mixin;
+import org.apache.axiom.core.ChildNotAllowedException;
import org.apache.axiom.core.ClonePolicy;
import org.apache.axiom.core.CoreChildNode;
import org.apache.axiom.core.CoreDocument;
@@ -106,4 +107,10 @@ public aspect CoreDocumentSupport {
public final void CoreDocument.serializeEndEvent(XmlHandler handler) throws StreamException {
handler.completed();
}
+
+ final void CoreDocument.internalCheckNewChild0(CoreChildNode newChild, CoreChildNode replacedChild) throws CoreModelException {
+ if (newChild instanceof CoreElement && !(replacedChild instanceof CoreElement) && coreGetDocumentElement() != null) {
+ throw new ChildNotAllowedException();
+ }
+ }
}
Modified: webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/CoreParentNodeSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/CoreParentNodeSupport.aj?rev=1740454&r1=1740453&r2=1740454&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/CoreParentNodeSupport.aj (original)
+++ webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/impl/mixin/CoreParentNodeSupport.aj Fri Apr 22 07:10:04 2016
@@ -21,6 +21,7 @@ package org.apache.axiom.core.impl.mixin
import org.apache.axiom.core.Axis;
import org.apache.axiom.core.Builder;
import org.apache.axiom.core.CharacterData;
+import org.apache.axiom.core.ChildNotAllowedException;
import org.apache.axiom.core.ClonePolicy;
import org.apache.axiom.core.Content;
import org.apache.axiom.core.CoreCDATASection;
@@ -195,6 +196,13 @@ public aspect CoreParentNodeSupport {
break;
}
} while (current != null);
+ if (!coreGetNodeType().isChildTypeAllowed(newChild.coreGetNodeType())) {
+ throw new ChildNotAllowedException();
+ }
+ internalCheckNewChild0(newChild, replacedChild);
+ }
+
+ void CoreParentNode.internalCheckNewChild0(CoreChildNode newChild, CoreChildNode replacedChild) throws CoreModelException {
}
public final void CoreParentNode.coreAppendChild(CoreChildNode child) throws CoreModelException {
Modified: webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java?rev=1740454&r1=1740453&r2=1740454&view=diff
==============================================================================
--- webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java (original)
+++ webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java Fri Apr 22 07:10:04 2016
@@ -21,6 +21,7 @@ package org.apache.axiom.om.impl.dom;
import static org.apache.axiom.dom.DOMExceptionUtil.newDOMException;
+import org.apache.axiom.core.CoreCharacterDataNode;
import org.apache.axiom.core.CoreChildNode;
import org.apache.axiom.core.CoreDocumentFragment;
import org.apache.axiom.core.CoreModelException;
@@ -29,7 +30,6 @@ import org.apache.axiom.dom.DOMParentNod
import org.apache.axiom.dom.DOMSemantics;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
-import org.w3c.dom.Element;
import org.w3c.dom.Node;
public abstract class ParentNode extends NodeImpl implements DOMParentNode {
@@ -54,26 +54,12 @@ public abstract class ParentNode extends
}
private void checkNewChild(Node newChild, Node replacedChild) {
- try {
- NodeImpl newDomChild = (NodeImpl) newChild;
-
- checkSameOwnerDocument(newDomChild);
-
- if (this instanceof Document) {
- if (newDomChild instanceof ElementImpl) {
- if (!(replacedChild instanceof Element) && ((DocumentImpl) this).coreGetDocumentElement() != null) {
- // Throw exception since there cannot be two document elements
- throw newDOMException(DOMException.HIERARCHY_REQUEST_ERR);
- }
- } else if (!(newDomChild instanceof CommentImpl
- || newDomChild instanceof ProcessingInstructionImpl
- || newDomChild instanceof DocumentFragmentImpl
- || newDomChild instanceof DocumentTypeImpl)) {
- throw newDOMException(DOMException.HIERARCHY_REQUEST_ERR);
- }
- }
- } catch (CoreModelException ex) {
- throw DOMExceptionUtil.toUncheckedException(ex);
+ NodeImpl newDomChild = (NodeImpl) newChild;
+
+ checkSameOwnerDocument(newDomChild);
+
+ if (this instanceof Document && newDomChild instanceof CoreCharacterDataNode) {
+ throw newDOMException(DOMException.HIERARCHY_REQUEST_ERR);
}
}
Modified: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/container/TestGetDescendants.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/container/TestGetDescendants.java?rev=1740454&r1=1740453&r2=1740454&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/container/TestGetDescendants.java (original)
+++ webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/container/TestGetDescendants.java Fri Apr 22 07:10:04 2016
@@ -28,6 +28,7 @@ import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMMetaFactory;
import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMProcessingInstruction;
import org.apache.axiom.om.OMSerializable;
import org.apache.axiom.om.OMText;
import org.apache.axiom.ts.AxiomTestCase;
@@ -49,7 +50,7 @@ public class TestGetDescendants extends
OMFactory factory = metaFactory.getOMFactory();
OMContainer root = containerFactory.create(factory);
OMElement child1 = factory.createOMElement("child", null, root);
- OMText child2 = factory.createOMText(root, "text", OMNode.CDATA_SECTION_NODE);
+ OMProcessingInstruction child2 = factory.createOMProcessingInstruction(root, "test", "data");
OMText grandchild1 = factory.createOMText(child1, "text");
OMComment grandchild2 = factory.createOMComment(child1, "text");
Iterator<? extends OMSerializable> it = root.getDescendants(includeSelf);