xmlbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ra...@apache.org
Subject svn commit: r149295 - in xmlbeans/trunk: src/typeimpl/org/apache/xmlbeans/impl/schema/ src/xmlcomp/org/apache/xmlbeans/impl/tool/ test/src/xmlobject/checkin/
Date Mon, 31 Jan 2005 20:41:40 GMT
Author: radup
Date: Mon Jan 31 12:41:37 2005
New Revision: 149295

URL: http://svn.apache.org/viewcvs?view=rev&rev=149295
Log:
Fix for http://issues.apache.org/jira/browse/XMLBEANS-35


Modified:
    xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeImpl.java
    xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/StscState.java
    xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/StscTranslator.java
    xmlbeans/trunk/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java
    xmlbeans/trunk/test/src/xmlobject/checkin/InstanceValidationTests.java

Modified: xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeImpl.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeImpl.java?view=diff&r1=149294&r2=149295
==============================================================================
--- xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeImpl.java (original)
+++ xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeImpl.java Mon Jan
31 12:41:37 2005
@@ -78,6 +78,7 @@
     private volatile SchemaField _containerField;
     private volatile int _containerFieldCode;
     private volatile int _containerFieldIndex;
+    private volatile QName[] _groupReferenceContext;
     private SchemaType.Ref[] _anonymousTyperefs;
     private boolean _isDocumentType;
     private boolean _isAttributeType;
@@ -498,6 +499,15 @@
         _containerFieldCode = code;
         _containerFieldIndex = index;
     }
+
+    /* package */ void setGroupReferenceContext(QName[] groupNames)
+    {
+        assertUnresolved();
+        _groupReferenceContext = groupNames;
+    }
+
+    /* package */ QName[] getGroupReferenceContext()
+    { return _groupReferenceContext; }
 
     public SchemaType getOuterType()
         { return _outerSchemaTypeRef == null ? null : _outerSchemaTypeRef.get(); }

Modified: xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/StscState.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/StscState.java?view=diff&r1=149294&r2=149295
==============================================================================
--- xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/StscState.java (original)
+++ xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/StscState.java Mon Jan 31
12:41:37 2005
@@ -84,7 +84,7 @@
     private Map _attributeTypes     = new LinkedHashMap();
     private Map _typesByClassname   = new LinkedHashMap();
     private Map _misspelledNames    = new HashMap();
-    private Set _processingGroups   = new HashSet();
+    private Set _processingGroups   = new LinkedHashSet();
     private Map _idConstraints      = new LinkedHashMap();
     private Set _namespaces         = new HashSet();
     private List _annotations       = new ArrayList();
@@ -1137,6 +1137,11 @@
     {
         assert(_processingGroups.contains(obj));
         _processingGroups.remove(obj);
+    }
+
+    Object[] getCurrentProcessing()
+    {
+        return _processingGroups.toArray();
     }
 
     /* JAVAIZATION ====================================================*/

Modified: xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/StscTranslator.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/StscTranslator.java?view=diff&r1=149294&r2=149295
==============================================================================
--- xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/StscTranslator.java (original)
+++ xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/StscTranslator.java Mon Jan
31 12:41:37 2005
@@ -773,10 +773,21 @@
 
         if (typedef != null)
         {
+            Object[] grps = state.getCurrentProcessing();
+            QName[] context = new QName[grps.length];
+            for (int i = 0; i < context.length; i++)
+                if (grps[i] instanceof SchemaModelGroupImpl)
+                    context[i] = ((SchemaModelGroupImpl ) grps[i]).getName();
+            SchemaType repeat = checkRecursiveGroupReference(context, qname, (SchemaTypeImpl)outerType);
+            if (repeat != null)
+                sType = repeat;
+            else
+            {
             SchemaTypeImpl sTypeImpl = new SchemaTypeImpl(state.getContainer(targetNamespace));
             sType = sTypeImpl;
             sTypeImpl.setContainerField(impl);
             sTypeImpl.setOuterSchemaTypeRef(outerType == null ? null : outerType.getRef());
+            sTypeImpl.setGroupReferenceContext(context);
             // leave the anonymous type unresolved: it will be resolved later.
             anonymousTypes.add(sType);
             sTypeImpl.setSimpleType(simpleTypedef);
@@ -784,6 +795,7 @@
                  elemFormDefault, attFormDefault, false);
             sTypeImpl.setAnnotation(SchemaAnnotationImpl.getAnnotation(state.getContainer(targetNamespace),
typedef));
             sTypeImpl.setUserData(getUserData(typedef));
+            }
         }
 
         if (sType == null)
@@ -901,6 +913,62 @@
         }
 
         return impl;
+    }
+
+    /**
+     * We need to do this because of the following kind of Schemas:
+     * <xs:group name="e">
+     *     <xs:sequence>
+     *         <xs:element name="error">
+     *             <xs:complexType>
+     *                 <xs:group ref="e"/>
+     *             </xs:complexType>
+     *         </xs:element>
+     *     </xs:sequence>
+     * </xs:group>
+     * (see JIRA bug XMLBEANS-35)
+     * Even though this should not be allowed because it produces an infinite
+     * number of anonymous types and local elements nested within each other,
+     * the de facto consensus among Schema processors is that this should be
+     * valid, therefore we have to detect this situation and "patch up" the
+     * Schema object model so that instead of creating a new anonymous type,
+     * we refer to the one that was already created earlier.
+     * In order to accomplish that, we store inside every anonymous type the
+     * list of groups that were dereferenced at the moment the type was created
+     * and if the same pattern is about to repeat, it means that we are in a
+     * case similar to the above.
+     */
+    private static SchemaType checkRecursiveGroupReference(QName[] context, QName containingElement,
SchemaTypeImpl outerType)
+    {
+        if (context.length < 1)
+            return null;
+        SchemaTypeImpl type = outerType;
+
+        while (type != null)
+        {
+            if (type.getName() != null || type.isDocumentType())
+                return null; // not anonymous
+            if (containingElement.equals(type.getContainerField().getName()))
+            {
+                QName[] outerContext = type.getGroupReferenceContext();
+                if (outerContext != null && outerContext.length == context.length)
+                {
+                    // Smells like trouble
+                    boolean equal = true;
+                    for (int i = 0; i < context.length; i++)
+                        if (!(context[i] == null && outerContext[i] == null ||
+                              context[i] != null && context[i].equals(outerContext[i])))
+                        {
+                            equal = false;
+                            break;
+                        }
+                    if (equal)
+                        return type;
+                }
+            }
+            type = (SchemaTypeImpl) type.getOuterType();
+        }
+        return null;
     }
 
     private static String removeWhitespace(String xpath)

Modified: xmlbeans/trunk/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java?view=diff&r1=149294&r2=149295
==============================================================================
--- xmlbeans/trunk/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java (original)
+++ xmlbeans/trunk/src/xmlcomp/org/apache/xmlbeans/impl/tool/SchemaCodeGenerator.java Mon
Jan 31 12:41:37 2005
@@ -87,10 +87,12 @@
 
     private static void deleteDirRecursively(File root, File dir)
     {
-        while (dir.list().length == 0 && !dir.equals(root))
+        String[] list = dir.list();
+        while (list != null && list.length == 0 && !dir.equals(root))
         {
             dir.delete();
             dir = dir.getParentFile();
+            list = dir.list();
         }
     }
 
@@ -142,9 +144,10 @@
         {
             if (dir.isDirectory())
             {
-                String[] list = dir.list();
-                for (int i = 0; i < list.length; i++)
-                    tryToDelete(new File(dir, list[i]));
+                String[] list = dir.list(); // can return null if I/O error
+                if (list != null)
+                    for (int i = 0; i < list.length; i++)
+                        tryToDelete(new File(dir, list[i]));
             }
             if (!dir.delete())
                 return; // don't try very hard, because we're just deleting tmp

Modified: xmlbeans/trunk/test/src/xmlobject/checkin/InstanceValidationTests.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/test/src/xmlobject/checkin/InstanceValidationTests.java?view=diff&r1=149294&r2=149295
==============================================================================
--- xmlbeans/trunk/test/src/xmlobject/checkin/InstanceValidationTests.java (original)
+++ xmlbeans/trunk/test/src/xmlobject/checkin/InstanceValidationTests.java Mon Jan 31 12:41:37
2005
@@ -1880,6 +1880,62 @@
             doTest(schemas, null, valid, invalid);
         }
 
+    public void testValidateNestedGroups ( )
+        throws Exception
+    {
+        // This is a weird Schema, inspired from JIRA bug XMLBEANS-35
+        // Make sure we compile it and then validate correctly
+        String schemas[] = {
+            "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" targetNamespace=\"http://tempuri.org/f_up_groups\"
xmlns:tns=\"http://tempuri.org/f_up_groups\">\n" +
+            "\n" +
+            "<xs:group name=\"d\">\n" +
+            "	<xs:sequence>\n" +
+            "	    <xs:element name=\"error\">\n" +
+            "	    	<xs:complexType>\n" +
+            "	    		<xs:group ref=\"tns:e\"/>\n" +
+            "	    	</xs:complexType>\n" +
+            "	    </xs:element>\n" +
+            "	</xs:sequence>\n" +
+            "</xs:group>\n" +
+            "\n" +
+            "<xs:group name=\"e\">\n" +
+            "	<xs:sequence>\n" +
+            "		<xs:element name=\"error\" minOccurs=\"0\">\n" +
+            "			<xs:complexType>\n" +
+            "				<xs:group ref=\"tns:d\"/>\n" +
+            "			</xs:complexType>\n" +
+            "		</xs:element>\n" +
+            "	</xs:sequence>\n" +
+            "</xs:group>\n" +
+            "\n" +
+            "<xs:element name=\"root\">\n" +
+            "	<xs:complexType>\n" +
+            " 	<xs:group ref=\"tns:d\"/>\n" +
+            "	</xs:complexType>\n" +
+            "</xs:element>\n" +
+            "\n" +
+            "</xs:schema>\n"};
+
+        String valid[] = {
+            "<ns:root xmlns:ns=\"http://tempuri.org/f_up_groups\">\n" +
+            "   <error>\n" +
+            "      <error>\n" +
+            "         <error/>" +
+	    "      </error>\n" +
+            "    </error>\n" +
+            "</ns:root>\n"};
+
+        String invalid[] = {
+            "<ns:root xmlns:ns=\"http://tempuri.org/f_up_groups\">\n" +
+            "   <error>\n" +
+            "      <error>\n" +
+	    "      </error>\n" +
+            "    </error>\n" +
+            "</ns:root>\n"};
+
+        doTest(schemas, null, valid, invalid);
+    }
+
     public void doTest (
           String[] schemas, QName docType,
           String[] validInstances, String[] invalidInstances)



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: commits-help@xmlbeans.apache.org


Mime
View raw message