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 2014/07/26 22:46:37 UTC

svn commit: r1613722 - in /webservices/axiom/trunk: aspects/dom-aspects/src/main/java/org/apache/axiom/dom/ aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/ implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ implemen...

Author: veithen
Date: Sat Jul 26 20:46:37 2014
New Revision: 1613722

URL: http://svn.apache.org/r1613722
Log:
Duplicate code reduction.

Added:
    webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMElementSupport.aj   (with props)
Modified:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/NamespaceIterator.java
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/OMElementSupport.aj
    webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
    webservices/axiom/trunk/implementations/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
    webservices/axiom/trunk/implementations/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java

Added: webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMElementSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMElementSupport.aj?rev=1613722&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMElementSupport.aj (added)
+++ webservices/axiom/trunk/aspects/dom-aspects/src/main/java/org/apache/axiom/dom/DOMElementSupport.aj Sat Jul 26 20:46:37 2014
@@ -0,0 +1,110 @@
+/*
+ * 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 javax.xml.XMLConstants;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.TypeInfo;
+
+public aspect DOMElementSupport {
+    public final short DOMElement.getNodeType() {
+        return Node.ELEMENT_NODE;
+    }
+    
+    public final String DOMElement.getNodeName() {
+        return getTagName();
+    }
+
+    public final TypeInfo DOMElement.getSchemaTypeInfo() {
+        throw new UnsupportedOperationException();
+    }
+
+    public final String DOMElement.lookupNamespaceURI(String specifiedPrefix) {
+        String namespace = this.getNamespaceURI();
+        String prefix = this.getPrefix();
+        // First check for namespaces implicitly defined by the namespace prefix/URI of the element
+        // TODO: although the namespace != null condition conforms to the specs, it is likely incorrect; see XERCESJ-1586
+        if (namespace != null
+                && (prefix == null && specifiedPrefix == null
+                        || prefix != null && prefix.equals(specifiedPrefix))) {
+            return namespace;
+        }
+        // looking in attributes
+        if (this.hasAttributes()) {
+            NamedNodeMap map = this.getAttributes();
+            int length = map.getLength();
+            for (int i = 0; i < length; i++) {
+                Node attr = map.item(i);
+                namespace = attr.getNamespaceURI();
+                if (namespace != null && namespace.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
+                    // At this point we know that either the prefix of the attribute is null and
+                    // the local name is "xmlns" or the prefix is "xmlns" and the local name is the
+                    // namespace prefix declared by the namespace declaration. We check that constraint
+                    // when the attribute is created.
+                    String attrPrefix = attr.getPrefix();
+                    if ((specifiedPrefix == null && attrPrefix == null)
+                            || (specifiedPrefix != null && attrPrefix != null
+                                    && attr.getLocalName().equals(specifiedPrefix))) {
+                        String value = attr.getNodeValue();
+                        return value.length() > 0 ? value : null;
+                    }
+                }
+            }
+        }
+        // looking in ancestor
+        DOMParentNode parent = (DOMParentNode)coreGetParent();
+        return parent instanceof Element ? parent.lookupNamespaceURI(specifiedPrefix) : null;
+    }
+
+    public final String DOMElement.lookupPrefix(String namespaceURI) {
+        return lookupPrefix(namespaceURI, this);
+    }
+    
+    private final String DOMElement.lookupPrefix(String namespaceURI, Element originalElement) {
+        if (namespaceURI == null || namespaceURI.length() == 0) {
+            return null;
+        }
+        if (namespaceURI.equals(getNamespaceURI())) {
+            String prefix = getPrefix();
+            if (namespaceURI.equals(originalElement.lookupNamespaceURI(prefix))) {
+                return prefix;
+            }
+        }
+        if (this.hasAttributes()) {
+            NamedNodeMap map = this.getAttributes();
+            int length = map.getLength();
+            for (int i = 0; i < length; i++) {
+                Node attr = map.item(i);
+                String attrPrefix = attr.getPrefix();
+                if (attrPrefix != null && attrPrefix.equals(XMLConstants.XMLNS_ATTRIBUTE)
+                        && attr.getNodeValue().equals(namespaceURI)) {
+                    String prefix = attr.getLocalName();
+                    if (namespaceURI.equals(originalElement.lookupNamespaceURI(prefix))) {
+                        return prefix;
+                    }
+                }
+            }
+        }
+        DOMParentNode parent = (DOMParentNode)coreGetParent();
+        return parent instanceof Element ? ((DOMElement)parent).lookupPrefix(namespaceURI, originalElement) : null;
+    }
+}

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

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/NamespaceIterator.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/NamespaceIterator.java?rev=1613722&r1=1613721&r2=1613722&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/NamespaceIterator.java (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/NamespaceIterator.java Sat Jul 26 20:46:37 2014
@@ -30,7 +30,7 @@ import org.apache.axiom.om.OMNamespace;
 /**
  * Iterator implementation used by {@link OMElement#getNamespacesInScope()}.
  */
-public class NamespaceIterator implements Iterator {
+final class NamespaceIterator implements Iterator {
     private final Set/*<String>*/ seenPrefixes = new HashSet();
     private OMElement element;
     private Iterator declaredNamespaces;

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/OMElementSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/OMElementSupport.aj?rev=1613722&r1=1613721&r2=1613722&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/OMElementSupport.aj (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/OMElementSupport.aj Sat Jul 26 20:46:37 2014
@@ -62,6 +62,26 @@ public aspect OMElementSupport {
         internalSetNamespace(handleNamespace(this, namespace, false, decl));
     }
 
+    public final OMElement IElement.getFirstElement() {
+        OMNode node = getFirstOMChild();
+        while (node != null) {
+            if (node.getType() == OMNode.ELEMENT_NODE) {
+                return (OMElement) node;
+            } else {
+                node = node.getNextOMSibling();
+            }
+        }
+        return null;
+    }
+
+    public final Iterator IElement.getChildElements() {
+        return new OMChildElementIterator(getFirstElement());
+    }
+
+    public final Iterator IElement.getNamespacesInScope() {
+        return new NamespaceIterator(this);
+    }
+
     public NamespaceContext IElement.getNamespaceContext(boolean detached) {
         if (detached) {
             Map namespaces = new HashMap();
@@ -75,6 +95,18 @@ public aspect OMElementSupport {
         }
     }
     
+    public final QName IElement.resolveQName(String qname) {
+        int idx = qname.indexOf(':');
+        if (idx == -1) {
+            OMNamespace ns = getDefaultNamespace();
+            return ns == null ? new QName(qname) : new QName(ns.getNamespaceURI(), qname, "");
+        } else {
+            String prefix = qname.substring(0, idx);
+            OMNamespace ns = findNamespace(null, prefix);
+            return ns == null ? null : new QName(ns.getNamespaceURI(), qname.substring(idx+1), prefix);
+        }
+    }
+
     public String IElement.getText() {
         String childText = null;
         StringBuffer buffer = null;

Modified: webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java?rev=1613722&r1=1613721&r2=1613722&view=diff
==============================================================================
--- webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java (original)
+++ webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java Sat Jul 26 20:46:37 2014
@@ -35,8 +35,6 @@ import org.apache.axiom.om.OMOutputForma
 import org.apache.axiom.om.OMXMLParserWrapper;
 import org.apache.axiom.om.impl.common.IContainer;
 import org.apache.axiom.om.impl.common.IElement;
-import org.apache.axiom.om.impl.common.NamespaceIterator;
-import org.apache.axiom.om.impl.common.OMChildElementIterator;
 import org.apache.axiom.om.impl.common.OMNamespaceImpl;
 import org.apache.axiom.om.impl.common.serializer.push.OutputException;
 import org.apache.axiom.om.impl.common.serializer.push.Serializer;
@@ -46,10 +44,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.w3c.dom.Attr;
 import org.w3c.dom.DOMException;
-import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-import org.w3c.dom.TypeInfo;
 
 import javax.xml.XMLConstants;
 import javax.xml.namespace.QName;
@@ -110,21 +105,7 @@ public class ElementImpl extends ParentN
     // /org.w3c.dom.Node methods
     // /
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.w3c.dom.Node#getNodeType()
-     */
-    public short getNodeType() {
-        return Node.ELEMENT_NODE;
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.w3c.dom.Node#getNodeName()
-     */ 
-    public String getNodeName() {
+    public String getTagName() {
         OMNamespace namespace = getNamespace();
         String localName = getLocalName();
         if (namespace != null) {
@@ -156,15 +137,6 @@ public class ElementImpl extends ParentN
     // / org.w3c.dom.Element methods
     // /
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.w3c.dom.Element#getTagName()
-     */
-    public String getTagName() {
-        return this.getNodeName();
-    }
-
     /**
      * Removes an attribute by name.
      *
@@ -646,23 +618,6 @@ public class ElementImpl extends ParentN
         return (attr == null) ? null : attr.getAttributeValue();
     }
 
-    /**
-     * Returns the first Element node.
-     *
-     * @see org.apache.axiom.om.OMElement#getFirstElement()
-     */
-    public OMElement getFirstElement() {
-        OMNode node = getFirstOMChild();
-        while (node != null) {
-            if (node.getType() == Node.ELEMENT_NODE) {
-                return (OMElement) node;
-            } else {
-                node = node.getNextOMSibling();
-            }
-        }
-        return null;
-    }
-
     public void removeAttribute(OMAttribute attr) {
         if (attr.getOwner() != this) {
             throw new OMException("The attribute is not owned by this element");
@@ -704,24 +659,11 @@ public class ElementImpl extends ParentN
         return new String(baos.toByteArray());
     }
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.axiom.om.OMElement#getChildElements()
-     */
-    public Iterator getChildElements() {
-        return new OMChildElementIterator(getFirstElement());
-    }
-
     /** @see org.apache.axiom.om.OMElement#getAllDeclaredNamespaces() */
     public Iterator getAllDeclaredNamespaces() throws OMException {
         return new NSDeclIterator(attributes);
     }
 
-    public Iterator getNamespacesInScope() {
-        return new NamespaceIterator(this);
-    }
-
     /** @see org.apache.axiom.om.OMElement#getAllAttributes() */
     public Iterator getAllAttributes() {
         if (attributes == null) {
@@ -740,18 +682,6 @@ public class ElementImpl extends ParentN
         return list.iterator();
     }
 
-    public QName resolveQName(String qname) {
-        int idx = qname.indexOf(':');
-        if (idx == -1) {
-            OMNamespace ns = getDefaultNamespace();
-            return ns == null ? new QName(qname) : new QName(ns.getNamespaceURI(), qname, "");
-        } else {
-            String prefix = qname.substring(0, idx);
-            OMNamespace ns = findNamespace(null, prefix);
-            return ns == null ? null : new QName(ns.getNamespaceURI(), qname.substring(idx+1), prefix);
-        }
-    }
-
     public OMElement cloneOMElement() {
         return (OMElement)clone(new OMCloneOptions());
     }
@@ -860,11 +790,6 @@ public class ElementImpl extends ParentN
         }
     }
 
-    public TypeInfo getSchemaTypeInfo() {
-        // TODO TODO
-        throw new UnsupportedOperationException("TODO");
-    }
-
     /* (non-Javadoc)
       * @see org.apache.axiom.om.OMNode#buildAll()
       */
@@ -912,76 +837,6 @@ public class ElementImpl extends ParentN
         defaultBuild();
     }
 
-    public final String lookupNamespaceURI(String specifiedPrefix) {
-        String namespace = this.getNamespaceURI();
-        String prefix = this.getPrefix();
-        // First check for namespaces implicitly defined by the namespace prefix/URI of the element
-        // TODO: although the namespace != null condition conforms to the specs, it is likely incorrect; see XERCESJ-1586
-        if (namespace != null
-                && (prefix == null && specifiedPrefix == null
-                        || prefix != null && prefix.equals(specifiedPrefix))) {
-            return namespace;
-        }
-        // looking in attributes
-        if (this.hasAttributes()) {
-            NamedNodeMap map = this.getAttributes();
-            int length = map.getLength();
-            for (int i = 0; i < length; i++) {
-                Node attr = map.item(i);
-                namespace = attr.getNamespaceURI();
-                if (namespace != null && namespace.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
-                    // At this point we know that either the prefix of the attribute is null and
-                    // the local name is "xmlns" or the prefix is "xmlns" and the local name is the
-                    // namespace prefix declared by the namespace declaration. We check that constraint
-                    // when the attribute is created.
-                    String attrPrefix = attr.getPrefix();
-                    if ((specifiedPrefix == null && attrPrefix == null)
-                            || (specifiedPrefix != null && attrPrefix != null
-                                    && attr.getLocalName().equals(specifiedPrefix))) {
-                        String value = attr.getNodeValue();
-                        return value.length() > 0 ? value : null;
-                    }
-                }
-            }
-        }
-        // looking in ancestor
-        ParentNode parent = (ParentNode)coreGetParent();
-        return parent instanceof Element ? parent.lookupNamespaceURI(specifiedPrefix) : null;
-    }
-
-    public final String lookupPrefix(String namespaceURI) {
-        return lookupPrefix(namespaceURI, this);
-    }
-    
-    final String lookupPrefix(String namespaceURI, Element originalElement) {
-        if (namespaceURI == null || namespaceURI.length() == 0) {
-            return null;
-        }
-        if (namespaceURI.equals(getNamespaceURI())) {
-            String prefix = getPrefix();
-            if (namespaceURI.equals(originalElement.lookupNamespaceURI(prefix))) {
-                return prefix;
-            }
-        }
-        if (this.hasAttributes()) {
-            NamedNodeMap map = this.getAttributes();
-            int length = map.getLength();
-            for (int i = 0; i < length; i++) {
-                Node attr = map.item(i);
-                String attrPrefix = attr.getPrefix();
-                if (attrPrefix != null && attrPrefix.equals(XMLConstants.XMLNS_ATTRIBUTE)
-                        && attr.getNodeValue().equals(namespaceURI)) {
-                    String prefix = attr.getLocalName();
-                    if (namespaceURI.equals(originalElement.lookupNamespaceURI(prefix))) {
-                        return prefix;
-                    }
-                }
-            }
-        }
-        ParentNode parent = (ParentNode)coreGetParent();
-        return parent instanceof Element ? ((ElementImpl)parent).lookupPrefix(namespaceURI, originalElement) : null;
-    }
-
     public final void checkChild(OMNode child) {
     }
 }

Modified: webservices/axiom/trunk/implementations/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java?rev=1613722&r1=1613721&r2=1613722&view=diff
==============================================================================
--- webservices/axiom/trunk/implementations/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java (original)
+++ webservices/axiom/trunk/implementations/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java Sat Jul 26 20:46:37 2014
@@ -32,8 +32,6 @@ import org.apache.axiom.om.OMOutputForma
 import org.apache.axiom.om.OMXMLParserWrapper;
 import org.apache.axiom.om.impl.common.IContainer;
 import org.apache.axiom.om.impl.common.IElement;
-import org.apache.axiom.om.impl.common.NamespaceIterator;
-import org.apache.axiom.om.impl.common.OMChildElementIterator;
 import org.apache.axiom.om.impl.common.OMNamespaceImpl;
 import org.apache.axiom.om.impl.common.serializer.push.OutputException;
 import org.apache.axiom.om.impl.common.serializer.push.Serializer;
@@ -156,15 +154,6 @@ public class OMElementImpl extends OMNod
     public void checkChild(OMNode child) {
     }
 
-    /**
-     * Returns a filtered list of children - just the elements.
-     *
-     * @return Returns an iterator of the child elements.
-     */
-    public Iterator getChildElements() {
-        return new OMChildElementIterator(getFirstElement());
-    }
-
     public OMNamespace declareNamespace(String uri, String prefix) {
         if ("".equals(prefix)) {
             log.warn("Deprecated usage of OMElement#declareNamespace(String,String) with empty prefix");
@@ -358,10 +347,6 @@ public class OMElementImpl extends OMNod
         return namespaces.values().iterator();
     }
 
-    public Iterator getNamespacesInScope() {
-        return new NamespaceIterator(this);
-    }
-
     /**
      * Returns a List of OMAttributes.
      *
@@ -521,23 +506,6 @@ public class OMElementImpl extends OMNod
 ////////////////////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////////////////////
 
-    /**
-     * Gets first element.
-     *
-     * @return Returns element.
-     */
-    public OMElement getFirstElement() {
-        OMNode node = getFirstOMChild();
-        while (node != null) {
-            if (node.getType() == OMNode.ELEMENT_NODE) {
-                return (OMElement) node;
-            } else {
-                node = node.getNextOMSibling();
-            }
-        }
-        return null;
-    }
-
     public String getNamespaceURI() {
         OMNamespace ns = getNamespace();
         if (ns == null) {
@@ -580,18 +548,6 @@ public class OMElementImpl extends OMNod
         return writer.toString();
     }
 
-    public QName resolveQName(String qname) {
-        int idx = qname.indexOf(':');
-        if (idx == -1) {
-            OMNamespace ns = getDefaultNamespace();
-            return ns == null ? new QName(qname) : new QName(ns.getNamespaceURI(), qname, "");
-        } else {
-            String prefix = qname.substring(0, idx);
-            OMNamespace ns = findNamespace(null, prefix);
-            return ns == null ? null : new QName(ns.getNamespaceURI(), qname.substring(idx+1), prefix);
-        }
-    }
-
     public OMElement cloneOMElement() {
         
         if (log.isDebugEnabled()) {

Modified: webservices/axiom/trunk/implementations/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java?rev=1613722&r1=1613721&r2=1613722&view=diff
==============================================================================
--- webservices/axiom/trunk/implementations/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java (original)
+++ webservices/axiom/trunk/implementations/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java Sat Jul 26 20:46:37 2014
@@ -338,10 +338,6 @@ public class OMSourcedElementImpl extend
         return isExpanded;
     }
 
-    public Iterator getChildElements() {
-        return super.getChildElements();
-    }
-
     public OMNamespace declareNamespace(String uri, String prefix) {
         forceExpand();
         return super.declareNamespace(uri, prefix);
@@ -390,11 +386,6 @@ public class OMSourcedElementImpl extend
         return super.getAllDeclaredNamespaces();
     }
 
-    public Iterator getNamespacesInScope() throws OMException {
-        forceExpand();
-        return super.getNamespacesInScope();
-    }
-
     public Iterator getAllAttributes() {
         forceExpand();
         return super.getAllAttributes();
@@ -429,10 +420,6 @@ public class OMSourcedElementImpl extend
         super.removeAttribute(attr);
     }
 
-    public OMElement getFirstElement() {
-        return super.getFirstElement();
-    }
-
     public XMLStreamReader getXMLStreamReader(boolean cache) {
         return getXMLStreamReader(cache, new OMXMLStreamReaderConfiguration());
     }
@@ -572,11 +559,6 @@ public class OMSourcedElementImpl extend
         }
     }
     
-    public QName resolveQName(String qname) {
-        forceExpand();
-        return super.resolveQName(qname);
-    }
-
     public OMElement cloneOMElement() {
         return super.cloneOMElement();
     }