You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by bi...@apache.org on 2008/11/16 04:14:02 UTC

svn commit: r717971 - in /webservices/commons/trunk/modules/XmlSchema: ./ maint/ src/main/java/org/apache/ws/commons/schema/ src/main/java/org/apache/ws/commons/schema/utils/

Author: bimargulies
Date: Sat Nov 15 19:14:02 2008
New Revision: 717971

URL: http://svn.apache.org/viewvc?rev=717971&view=rev
Log:
Merged revisions 717963,717966-717968 via svnmerge from 
https://svn.apache.org/repos/asf/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH

........
  r717963 | bimargulies | 2008-11-15 21:27:24 -0500 (Sat, 15 Nov 2008) | 2 lines
  
  WSCOMMONS-358 and WSCOMMONS-361.
........
  r717966 | bimargulies | 2008-11-15 21:53:03 -0500 (Sat, 15 Nov 2008) | 2 lines
  
  WSCOMMONS-355 (missing anyType and anySimpleType).
........
  r717967 | bimargulies | 2008-11-15 21:57:25 -0500 (Sat, 15 Nov 2008) | 2 lines
  
  WSCOMMONS-396
........
  r717968 | bimargulies | 2008-11-15 22:01:29 -0500 (Sat, 15 Nov 2008) | 2 lines
  
  WSCOMMONS-364.
........

Added:
    webservices/commons/trunk/modules/XmlSchema/maint/
      - copied from r717968, webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/maint/
    webservices/commons/trunk/modules/XmlSchema/maint/DoMerges.java
      - copied unchanged from r717968, webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/maint/DoMerges.java
Modified:
    webservices/commons/trunk/modules/XmlSchema/   (props changed)
    webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
    webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchema.java
    webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java
    webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java

Propchange: webservices/commons/trunk/modules/XmlSchema/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Sat Nov 15 19:14:02 2008
@@ -1 +1 @@
-/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH:1-714211
+/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH:1-717970

Modified: webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java?rev=717971&r1=717970&r2=717971&view=diff
==============================================================================
--- webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java (original)
+++ webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java Sat Nov 15 19:14:02 2008
@@ -128,7 +128,7 @@
 	 */
 	XmlSchema handleXmlSchemaElement(Element schemaEl, String uri) {
 		// get all the attributes along with the namespace declns
-		schema.setNamespaceContext(new NodeNamespaceContext(schemaEl));
+		schema.setNamespaceContext(NodeNamespaceContext.getNamespaceContext(schemaEl));
 		setNamespaceAttributes(schema, schemaEl);
 
 		XmlSchemaCollection.SchemaKey schemaKey = new XmlSchemaCollection.SchemaKey(
@@ -431,7 +431,7 @@
 					restrictionEl, XmlSchema.SCHEMA_NS, "simpleType");
 
 			if (restrictionEl.hasAttribute("base")) {
-				NamespaceContext ctx = new NodeNamespaceContext(restrictionEl);
+				NamespaceContext ctx = NodeNamespaceContext.getNamespaceContext(restrictionEl);
 				restriction.baseTypeName = getRefQName(restrictionEl
 						.getAttribute("base"), ctx);
 			} else if (inlineSimpleType != null) {
@@ -558,7 +558,7 @@
 	}
 
 	private QName getRefQName(String pName, Node pNode) {
-		return getRefQName(pName, new NodeNamespaceContext(pNode));
+		return getRefQName(pName, NodeNamespaceContext.getNamespaceContext(pNode));
 	}
 
 	private QName getRefQName(String pName, NamespaceContext pContext) {
@@ -1311,7 +1311,7 @@
 					// there is a possiblily of some namespace mapping
 					String prefix = value.substring(0, value.indexOf(":"));
 					if (ctx == null) {
-						ctx = new NodeNamespaceContext(attrEl);
+						ctx = NodeNamespaceContext.getNamespaceContext(attrEl);
 					}
 					String namespace = ctx.getNamespaceURI(prefix);
 					if (!Constants.NULL_NS_URI.equals(namespace)) {
@@ -1863,7 +1863,7 @@
         
 		//use the entity resolver provided if the schema location is present null
 		if (schemaLocation != null && !"".equals(schemaLocation)) {
-			InputSource source = collection.schemaResolver.resolveEntity(
+			InputSource source = collection.getSchemaResolver().resolveEntity(
 					targetNamespace, schemaLocation, baseUri);
 
 			//the entity resolver was unable to resolve this!!
@@ -1956,12 +1956,10 @@
 			}
 
 			//process elements
-			NodeList allChildren = parentElement.getChildNodes();
-			for (int i = 0; i < allChildren.getLength(); i++) {
-				if (allChildren.item(i).getNodeType() == Node.ELEMENT_NODE) {
-
-					Element extElement = (Element) allChildren.item(i);
-
+			Node child = parentElement.getFirstChild();
+			while (child != null) {
+				if (child.getNodeType() == Node.ELEMENT_NODE) {
+					Element extElement = (Element) child;
 					String namespaceURI = extElement.getNamespaceURI();
 					String name = extElement.getLocalName();
 
@@ -1973,9 +1971,9 @@
 						QName qName = new QName(namespaceURI, name);
 						extReg.deserializeExtension(schemaObject, qName,
 								extElement);
-
 					}
 				}
+				child = child.getNextSibling();
 			}
 		}
 

Modified: webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchema.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchema.java?rev=717971&r1=717970&r2=717971&view=diff
==============================================================================
--- webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchema.java (original)
+++ webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchema.java Sat Nov 15 19:14:02 2008
@@ -210,6 +210,45 @@
 			return element;
 		}
 	}
+    
+    protected XmlSchemaAttribute getAttributeByName(QName name, boolean deep, Stack schemaStack) {
+                                        if (schemaStack != null && schemaStack.contains(this)) {
+                                                // recursive schema - just return null
+                                                return null;
+                                        } else {
+                                                XmlSchemaAttribute attribute = (XmlSchemaAttribute) attributes
+                                                                .getItem(name);
+                                                if (deep) {
+                                                        if (attribute == null) {
+                                                                // search the imports
+                                                                for (Iterator includedItems = includes.getIterator(); includedItems
+                                                                                .hasNext();) {
+                                                                        
+                                                                        XmlSchema schema = getSchema(includedItems.next());
+                                                                        
+                                                                        if (schema != null) {
+                                                                        // create an empty stack - push the current parent in
+                                                                        // and
+                                                                        // use the protected method to process the schema
+                                                                        if (schemaStack == null) {
+                                                                                schemaStack = new Stack();
+                                                                        }
+                                                                        schemaStack.push(this);
+                                                                        attribute = schema.getAttributeByName(name, deep,
+                                                                                        schemaStack);
+                                                                        if (attribute != null) {
+                                                                                return attribute;
+                                                                        }
+                                                                        }
+                                                                }
+                                                        } else {
+                                                                return attribute;
+                                                        }
+                                                }
+
+                                                return attribute;
+                                        }
+                                }
 
 	/**
 	 * get an element by the name in the local schema
@@ -230,6 +269,15 @@
 	public XmlSchemaElement getElementByName(QName name) {
 		return this.getElementByName(name, true, null);
 	}
+	
+	/**
+	 * Look for a global attribute by its QName. Searches through all schemas.
+	 * @param name
+	 * @return
+	 */
+	public XmlSchemaAttribute getAttributeByName(QName name) {
+	    return this.getAttributeByName(name, true, null);
+	}
 
 	/**
 	 * protected method that allows safe (non-recursive schema loading)

Modified: webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java?rev=717971&r1=717970&r2=717971&view=diff
==============================================================================
--- webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java (original)
+++ webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java Sat Nov 15 19:14:02 2008
@@ -144,9 +144,9 @@
      * An org.xml.sax.EntityResolver that is used to
      * resolve the imports/includes
      */
-    URIResolver schemaResolver = new DefaultURIResolver();
+    private URIResolver schemaResolver = new DefaultURIResolver();
 
-    XmlSchema xsd = new XmlSchema(XmlSchema.SCHEMA_NS, this);
+	XmlSchema xsd = new XmlSchema(XmlSchema.SCHEMA_NS, this);
 
     /**
      * stack to track imports (to prevent recursion)
@@ -171,12 +171,27 @@
     }
 
     /**
+     * Retrieve the custom URI resolver, if any.
+     * @return the current resolver.
+     */
+    public URIResolver getSchemaResolver() {
+		return schemaResolver;
+	}
+
+    /**
      * This section should comply to the XMLSchema specification; see
      * <a href="http://www.w3.org/TR/2004/PER-xmlschema-2-20040318/datatypes.html#built-in-datatypes">
      *  http://www.w3.org/TR/2004/PER-xmlschema-2-20040318/datatypes.html#built-in-datatypes</a>.
      * This needs to be inspected by another pair of eyes
      */
     public void init() {
+    	
+    	/*
+    	 * Defined in section 4.
+    	 */
+    	addSimpleType(xsd, Constants.XSD_ANYSIMPLETYPE.getLocalPart());
+    	addSimpleType(xsd, Constants.XSD_ANYTYPE.getLocalPart());
+    	
         /*
         Primitive types
 
@@ -506,6 +521,40 @@
         }
         return null;
     }
+    
+    /**
+     * Find a global attribute by QName in this collection of schemas.
+     * @param schemaAttributeName the name of the attribute.
+     * @return the attribute or null.
+     */
+    public XmlSchemaAttribute getAttributeByQName(QName schemaAttributeName) {
+        String uri = schemaAttributeName.getNamespaceURI();
+        for (Iterator iter = schemas.entrySet().iterator();  iter.hasNext();  ) {
+            Map.Entry entry = (Map.Entry) iter.next();
+            if (((SchemaKey) entry.getKey()).getNamespace().equals(uri)) {
+                XmlSchemaAttribute attribute = ((XmlSchema) entry.getValue()).getAttributeByName(schemaAttributeName);
+                if (attribute != null) {
+                    return attribute;
+                }
+        }
+        }
+        return null;
+    }
+    
+    /**
+     * Return the schema from this collection for a particular targetNamespace.
+     * @param uri target namespace URI.
+     * @return
+     */
+    public XmlSchema schemaForNamespace(String uri) {
+        for (Iterator iter = schemas.entrySet().iterator();  iter.hasNext();  ) {
+            Map.Entry entry = (Map.Entry) iter.next();
+            if (((SchemaKey) entry.getKey()).getNamespace().equals(uri)) {
+                return (XmlSchema) entry.getValue();
+            }
+        }
+        return null;
+    }
 
     Map unresolvedTypes = new HashMap();
 

Modified: webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java?rev=717971&r1=717970&r2=717971&view=diff
==============================================================================
--- webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java (original)
+++ webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java Sat Nov 15 19:14:02 2008
@@ -20,42 +20,94 @@
 package org.apache.ws.commons.schema.utils;
 
 import org.apache.ws.commons.schema.constants.Constants;
+
+import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 
 import javax.xml.namespace.NamespaceContext;
+
+import java.lang.reflect.Method;
 import java.util.*;
 
 /**
  * Implementation of {@link NamespaceContext}, which is based on a DOM node.
  */
 public class NodeNamespaceContext implements NamespacePrefixList {
+    private static final String NODE_NAMSPACE_CONTEXT = NamespacePrefixList.class.getName();
     private static final Collection XML_NS_PREFIX_COLLECTION = Collections.singletonList(Constants.XML_NS_PREFIX);
     private static final Collection XMLNS_ATTRIBUTE_COLLECTION = Collections.singletonList(Constants.XMLNS_ATTRIBUTE);
-    private Node node;
-    private Map declarations;
+    
+    static Method getUserData;
+    static Method setUserData;
+    static {
+        try {
+            Class cls = Class.forName("org.w3c.dom.UserDataHandler", false, Node.class.getClassLoader());
+            getUserData = Node.class.getMethod("getUserData", new Class[]{String.class});
+            setUserData = Node.class.getMethod("setUserData", new Class[]{String.class, Object.class, cls});
+        } catch (Throwable e) {
+            getUserData = null;
+            setUserData = null;
+        }
+    }
+    
+    
+    
+    private final Map declarations;
     private String[] prefixes;
 
     /**
      * Creates a new instance with the given nodes context.
      */
-    public NodeNamespaceContext(Node pNode) {
-        node = pNode;
+    private NodeNamespaceContext(Map decls) {
+        declarations = decls;
     }
-
-    private Map getDeclarations() {
-        if (declarations == null) {
-            declarations = new HashMap();
-            //FIXME: Do we really need to add this mapping? shows up in the serialized schema as xmlns="" 
-            //declarations.put(Constants.DEFAULT_NS_PREFIX, Constants.NULL_NS_URI);
-            new PrefixCollector(){
-                protected void declare(String pPrefix, String pNamespaceURI) {
-                    declarations.put(pPrefix, pNamespaceURI);
+    
+    public static NodeNamespaceContext getNamespaceContext(Node pNode) {
+        if (getUserData != null) {
+            try {
+                NodeNamespaceContext ctx = (NodeNamespaceContext)getUserData.invoke(pNode, new Object[] {NODE_NAMSPACE_CONTEXT});
+                if (ctx == null) {
+                    Map declarations = new HashMap();
+        
+                    Node parentNode = pNode.getParentNode();
+                    if (parentNode != null) {
+                        NodeNamespaceContext parent = 
+                            (NodeNamespaceContext)getUserData.invoke(parentNode, new Object[] {NODE_NAMSPACE_CONTEXT});
+                        if (parent == null) {
+                            parent = getNamespaceContext(parentNode);
+                        }
+                        declarations.putAll(parent.declarations);
+                    }
+                    
+                    NamedNodeMap map = pNode.getAttributes();
+                    if (map != null) {
+                        for (int i = 0; i < map.getLength(); i++) {
+                            Node attr = map.item(i);
+                            final String uri = attr.getNamespaceURI();
+                            if (Constants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) {
+                                String localName = attr.getLocalName();
+                                String prefix = Constants.XMLNS_ATTRIBUTE.equals(localName) ? Constants.DEFAULT_NS_PREFIX : localName;
+                                declarations.put(prefix, attr.getNodeValue());
+                            }
+                        }
+                    }
+                    ctx = new NodeNamespaceContext(declarations);
+                    setUserData.invoke(pNode, new Object[] {NODE_NAMSPACE_CONTEXT, ctx, null});
                 }
-            }.searchAllPrefixDeclarations(node);
-            Collection keys = declarations.keySet();
-            prefixes = (String[]) keys.toArray(new String[keys.size()]);
+                return ctx;
+            } catch (Throwable t) {
+                //ignore.  DOM level 2 implementation would not have the getUserData stuff.   
+                //Thus, fall back to the old, slower method.
+            }
         }
-        return declarations;
+        
+        final Map declarations = new HashMap();
+        new PrefixCollector(){
+            protected void declare(String pPrefix, String pNamespaceURI) {
+                declarations.put(pPrefix, pNamespaceURI);
+            }
+        }.searchAllPrefixDeclarations(pNode);
+        return new NodeNamespaceContext(declarations);
     }
 
     public String getNamespaceURI(String pPrefix) {
@@ -68,7 +120,7 @@
         if (Constants.XMLNS_ATTRIBUTE.equals(pPrefix)) {
             return Constants.XMLNS_ATTRIBUTE_NS_URI;
         }
-        final String uri = (String) getDeclarations().get(pPrefix);
+        final String uri = (String) declarations.get(pPrefix);
         return uri == null ? Constants.NULL_NS_URI : uri;
     }
 
@@ -82,8 +134,7 @@
         if (Constants.XMLNS_ATTRIBUTE_NS_URI.equals(pNamespaceURI)) {
             return Constants.XMLNS_ATTRIBUTE;
         }
-        Map decl = getDeclarations();
-        for (Iterator iter = decl.entrySet().iterator();  iter.hasNext();  ) {
+        for (Iterator iter = declarations.entrySet().iterator();  iter.hasNext();  ) {
             Map.Entry entry = (Map.Entry) iter.next();
             if (pNamespaceURI.equals(entry.getValue())) {
                 return (String) entry.getKey();
@@ -103,7 +154,7 @@
             return XMLNS_ATTRIBUTE_COLLECTION.iterator();
         }
         final List list = new ArrayList();
-        for (Iterator iter = getDeclarations().entrySet().iterator();  iter.hasNext();  ) {
+        for (Iterator iter = declarations.entrySet().iterator();  iter.hasNext();  ) {
             Map.Entry entry = (Map.Entry) iter.next();
             if (pNamespaceURI.equals(entry.getValue())) {
                 list.add(entry.getKey());
@@ -113,7 +164,10 @@
     }
 
     public String[] getDeclaredPrefixes() {
-        getDeclarations(); // Make sure, that the prefixes array is valid
+        if (prefixes == null) {
+            Collection keys = declarations.keySet();
+            prefixes = (String[]) keys.toArray(new String[keys.size()]);
+        }
         return prefixes;
     }
 }