Author: ajith
Date: Mon Oct 29 12:28:38 2007
New Revision: 589811

URL: http://svn.apache.org/viewvc?rev=589811&view=rev
Log:
1.Added two new methods to SchemaTypes and Elements (this would be discussed in 
the mailing list further)
2. Fixed the recursive search problem and added a test case to test it

Added:
    
webservices/commons/branches/modules/XmlSchema/1.3.3/src/test/java/tests/RecursiveImportTest.java
Modified:
    
webservices/commons/branches/modules/XmlSchema/1.3.3/src/main/java/org/apache/ws/commons/schema/XmlSchema.java
    
webservices/commons/branches/modules/XmlSchema/1.3.3/src/test/java/tests/ImportTest.java

Modified: 
webservices/commons/branches/modules/XmlSchema/1.3.3/src/main/java/org/apache/ws/commons/schema/XmlSchema.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/branches/modules/XmlSchema/1.3.3/src/main/java/org/apache/ws/commons/schema/XmlSchema.java?rev=589811&r1=589810&r2=589811&view=diff
==============================================================================
--- 
webservices/commons/branches/modules/XmlSchema/1.3.3/src/main/java/org/apache/ws/commons/schema/XmlSchema.java
 (original)
+++ 
webservices/commons/branches/modules/XmlSchema/1.3.3/src/main/java/org/apache/ws/commons/schema/XmlSchema.java
 Mon Oct 29 12:28:38 2007
@@ -33,6 +33,7 @@
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Stack;
 
 
 /**
@@ -132,58 +133,154 @@
         return elements;
     }
 
-    public XmlSchemaElement getElementByName(QName name) {
-        XmlSchemaElement element = (XmlSchemaElement) elements.getItem(name);
-        if (element == null){
-            //search the imports
-            for(Iterator includedItems = 
includes.getIterator();includedItems.hasNext();){
-               Object includeOrImport =  includedItems.next();
-                XmlSchema schema = null;
-                if (includeOrImport instanceof XmlSchemaImport){
-                    schema  =  ((XmlSchemaImport)includeOrImport).getSchema();
-                }else if (includeOrImport instanceof XmlSchemaInclude){
-                    schema  =  ((XmlSchemaInclude)includeOrImport).getSchema();
-                }else{
-                    //skip ?
-                    continue;
-                }
-                if (schema.getElementByName(name)!=null){
-                    return schema.getElementByName(name);
-                }
-            }
-        }else{
-            return element;
-        }
-
-        return null;
-    }
-
-    public XmlSchemaType getTypeByName(QName name) {
-        XmlSchemaType type = (XmlSchemaType) schemaTypes.getItem(name);
-        if (type == null){
-            //search the imports
-            for(Iterator includedItems = 
includes.getIterator();includedItems.hasNext();){
-                Object includeOrImport =  includedItems.next();
-                XmlSchema schema = null;
-                if (includeOrImport instanceof XmlSchemaImport){
-                    schema  =  ((XmlSchemaImport)includeOrImport).getSchema();
-                }else if (includeOrImport instanceof XmlSchemaInclude){
-                    schema  =  ((XmlSchemaInclude)includeOrImport).getSchema();
-                }else{
-                    //skip ?
-                    continue;
-                }
-
-                if (schema.getTypeByName(name)!=null){
-                    return schema.getTypeByName(name);
-                }
-            }
-        }else{
-            return type;
-        }
+    
+    protected XmlSchemaElement getElementByName(QName name, boolean deep,
+                       Stack schemaStack) {
+               if (schemaStack != null && schemaStack.contains(this)) {
+                       // recursive schema - just return null
+                       return null;
+               } else {
+                       XmlSchemaElement element = (XmlSchemaElement) elements
+                                       .getItem(name);
+                       if (deep) {
+                               if (element == 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);
+                                               element = 
schema.getElementByName(name, deep,
+                                                               schemaStack);
+                                               if (element != null) {
+                                                       return element;
+                                               }
+                                               }
+                                       }
+                               } else {
+                                       return element;
+                               }
+                       }
+
+                       return element;
+               }
+       }
+
+       /**
+        * get an element by the qname
+        * 
+        * @param name
+        * @param deep
+        * @return
+        */
+       public XmlSchemaElement getElementByName(QName name, boolean deep) {
+               return this.getElementByName(name, deep, null);
+       }
+
+       /**
+        * @deprecated use the [EMAIL PROTECTED] #getElementByName(QName, 
boolean)} method
+        * @param name
+        * @return
+        */
+       public XmlSchemaElement getElementByName(QName name) {
+               return this.getElementByName(name, false, null);
+       }
+
+       /**
+        * protected method that allows safe (non-recursive schema loading)
+        * 
+        * @param name
+        * @param deep
+        * @param schemaStack
+        * @return
+        */
+       protected XmlSchemaType getTypeByName(QName name, boolean deep,
+                       Stack schemaStack) {
+               if (schemaStack != null && schemaStack.contains(this)) {
+                       // recursive schema - just return null
+                       return null;
+               } else {
+                       XmlSchemaType type = (XmlSchemaType) 
schemaTypes.getItem(name);
+
+                       if (deep) {
+                               if (type == 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
+                                                       // use the protected 
method to process the schema
+                                                       if (schemaStack == 
null) {
+                                                               schemaStack = 
new Stack();
+                                                       }
+                                                       schemaStack.push(this);
+                                                       type = schema
+                                                                       
.getTypeByName(name, deep, schemaStack);
+                                                       if (type != null) {
+                                                               return type;
+                                                       }
+                                               }
+                                       }
+                               } else {
+                                       return type;
+                               }
+                       }
+
+                       return type;
+               }
+       }
+
+       /**
+        * @deprecated use the [EMAIL PROTECTED] #getTypeByName(QName, boolean)}
+        * @param name
+        * @return
+        */
+       public XmlSchemaType getTypeByName(QName name) {
+               return getTypeByName(name, false, null);
+       }
+
+       /**
+        * 
+        * @param name
+        * @param deep
+        * @return
+        */
+       public XmlSchemaType getTypeByName(QName name, boolean deep) {
+               return getTypeByName(name, deep, null);
+       }
+
+       /**
+        * Get a schema from an import
+        * 
+        * @param includeOrImport
+        * @return
+        */
+       private XmlSchema getSchema(Object includeOrImport) {
+               XmlSchema schema;
+               if (includeOrImport instanceof XmlSchemaImport) {
+                       schema = ((XmlSchemaImport) 
includeOrImport).getSchema();
+               } else if (includeOrImport instanceof XmlSchemaInclude) {
+                       schema = ((XmlSchemaInclude) 
includeOrImport).getSchema();
+               } else {
+                       // skip ?
+                       schema = null;
+               }
 
-        return null;
-    }
+               return schema;
+       }
+
+    
 
     public XmlSchemaDerivationMethod getFinalDefault() {
         return finalDefault;

Modified: 
webservices/commons/branches/modules/XmlSchema/1.3.3/src/test/java/tests/ImportTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/branches/modules/XmlSchema/1.3.3/src/test/java/tests/ImportTest.java?rev=589811&r1=589810&r2=589811&view=diff
==============================================================================
--- 
webservices/commons/branches/modules/XmlSchema/1.3.3/src/test/java/tests/ImportTest.java
 (original)
+++ 
webservices/commons/branches/modules/XmlSchema/1.3.3/src/test/java/tests/ImportTest.java
 Mon Oct 29 12:28:38 2007
@@ -83,7 +83,7 @@
         XmlSchema schema = schemaCol.read(doc,file.toURL().toString(),null);
         assertNotNull(schema);
 
-        assertNotNull(schema.getTypeByName(new 
QName("http://soapinterop.org/xsd2","SOAPStruct";)));
-        assertNotNull(schema.getElementByName(new 
QName("http://soapinterop.org/xsd2","SOAPWrapper";)));
+        assertNotNull(schema.getTypeByName(new 
QName("http://soapinterop.org/xsd2","SOAPStruct";),true));
+        assertNotNull(schema.getElementByName(new 
QName("http://soapinterop.org/xsd2","SOAPWrapper";),true));
     }
 }

Added: 
webservices/commons/branches/modules/XmlSchema/1.3.3/src/test/java/tests/RecursiveImportTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/branches/modules/XmlSchema/1.3.3/src/test/java/tests/RecursiveImportTest.java?rev=589811&view=auto
==============================================================================
--- 
webservices/commons/branches/modules/XmlSchema/1.3.3/src/test/java/tests/RecursiveImportTest.java
 (added)
+++ 
webservices/commons/branches/modules/XmlSchema/1.3.3/src/test/java/tests/RecursiveImportTest.java
 Mon Oct 29 12:28:38 2007
@@ -0,0 +1,53 @@
+/*
+ * 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 tests;
+
+import junit.framework.TestCase;
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.w3c.dom.Document;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.namespace.QName;
+import java.io.File;
+
+public class RecursiveImportTest extends TestCase {
+
+    public void testSchemaImport() throws Exception{
+        //create a DOM document
+        DocumentBuilderFactory documentBuilderFactory = 
DocumentBuilderFactory.newInstance();
+        documentBuilderFactory.setNamespaceAware(true);
+        Document doc = documentBuilderFactory.newDocumentBuilder().
+                parse(Resources.asURI("circular/a.xsd"));
+
+        XmlSchemaCollection schemaCol = new XmlSchemaCollection();
+        schemaCol.setBaseUri(Resources.TEST_RESOURCES + "/circular");
+        XmlSchema schema = schemaCol.read(doc,null);
+        assertNotNull(schema);
+
+      
+        
+        //these qnames are *not* there in these schemas
+        assertNull(schema.getTypeByName(new 
QName("http://soapinterop.org/xsd2","SOAPStruct";),true));
+        assertNull(schema.getElementByName(new 
QName("http://soapinterop.org/xsd2","SOAPWrapper";),true));
+    }
+
+    
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to