rdonkin     2005/01/20 15:09:45

  Modified:    betwixt/src/java/org/apache/commons/betwixt/schema
                        ComplexType.java Element.java ElementReference.java
                        GlobalComplexType.java GlobalElement.java
                        LocalComplexType.java Schema.java
               betwixt/src/test/org/apache/commons/betwixt/schema
                        TestSchemaGeneration.java
               betwixt/src/test/org/apache/commons/betwixt/xmlunit
                        XmlTestCase.java
  Log:
  Fixed buggy behaviour when creating schema for beans with cyclic graphs. Unit 
tests contributed by Susan Liu. Issue#33168
  
  Revision  Changes    Path
  1.3       +20 -6     
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/ComplexType.java
  
  Index: ComplexType.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/ComplexType.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ComplexType.java  16 Jun 2004 11:21:26 -0000      1.2
  +++ ComplexType.java  20 Jan 2005 23:09:44 -0000      1.3
  @@ -25,6 +25,9 @@
   import org.apache.commons.betwixt.XMLBeanInfo;
   
   /**
  + * Models a <code>complexType</code>.
  + * Global (top level) complex types are represented by [EMAIL PROTECTED] 
GlobalComplexType}.
  + * Locally defined or referenced complex types are represented by [EMAIL 
PROTECTED] LocalComplexType}. 
    * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
    * @version $Revision$
    */
  @@ -37,6 +40,17 @@
       public ComplexType() {}
   
       public ComplexType(TranscriptionConfiguration configuration, 
ElementDescriptor elementDescriptor, Schema schema) throws 
IntrospectionException {
  +        elementDescriptor = fillDescriptor(elementDescriptor, schema);
  +        init(configuration, elementDescriptor, schema);      
  +    }
  +
  +    /**
  +     * @param elementDescriptor
  +     * @param schema
  +     * @return
  +     * @throws IntrospectionException
  +     */
  +    protected ElementDescriptor fillDescriptor(ElementDescriptor 
elementDescriptor, Schema schema) throws IntrospectionException {
           if (elementDescriptor.isHollow()) {
               // need to introspector for filled descriptor
               Class type = elementDescriptor.getSingularPropertyType();
  @@ -46,7 +60,7 @@
               XMLBeanInfo filledBeanInfo = schema.introspect(type);
               elementDescriptor = filledBeanInfo.getElementDescriptor();
           }
  -        init(configuration, elementDescriptor, schema);      
  +        return elementDescriptor;
       }
   
       protected void init(TranscriptionConfiguration configuration, 
ElementDescriptor elementDescriptor, Schema schema) throws 
IntrospectionException {
  @@ -79,7 +93,7 @@
         * @return 
         */
       public List getElements() {
  -     return elements;
  +             return elements;
       }
   
       /**
  @@ -87,7 +101,7 @@
         * @param element
         */
       public void addElement(ElementReference element) {
  -     elements.add(element);
  +        elements.add(element);
       }
       
       /**
  @@ -104,7 +118,7 @@
         * @return
         */
       public List getAttributes() {
  -     return attributes;
  +        return attributes;
       }
   
       /**
  @@ -112,7 +126,7 @@
         * @param attribute
         */
       public void addAttribute(Attribute attribute) {
  -     attributes.add(attribute);
  +             attributes.add(attribute);
       }
   
   }
  
  
  
  1.3       +2 -1      
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Element.java
  
  Index: Element.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Element.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Element.java      16 Jun 2004 11:21:26 -0000      1.2
  +++ Element.java      20 Jan 2005 23:09:44 -0000      1.3
  @@ -17,6 +17,7 @@
   package org.apache.commons.betwixt.schema;
   
   /**
  + * Implmented by <code>element</code> definition.
    * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
    * @version $Revision$
    */
  
  
  
  1.3       +2 -3      
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/ElementReference.java
  
  Index: ElementReference.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/ElementReference.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ElementReference.java     16 Jun 2004 11:21:26 -0000      1.2
  +++ ElementReference.java     20 Jan 2005 23:09:44 -0000      1.3
  @@ -42,8 +42,7 @@
       public ElementReference(TranscriptionConfiguration configuration, 
ElementDescriptor elementDescriptor, Schema schema) throws 
IntrospectionException {
           setName(elementDescriptor.getLocalName());
           if (elementDescriptor.isHollow()) {
  -            setComplexType( new GlobalComplexType(configuration, 
elementDescriptor, schema));
  -            schema.addComplexType(getComplexType());
  +            setComplexType( schema.addGlobalComplexType( configuration, 
elementDescriptor ));
               if (elementDescriptor.isCollective()) {
                   maxOccurs = "unbounded";
               }
  
  
  
  1.4       +29 -3     
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/GlobalComplexType.java
  
  Index: GlobalComplexType.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/GlobalComplexType.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- GlobalComplexType.java    4 Oct 2004 22:27:12 -0000       1.3
  +++ GlobalComplexType.java    20 Jan 2005 23:09:44 -0000      1.4
  @@ -43,10 +43,36 @@
       }
   
       protected void init(TranscriptionConfiguration configuration, 
ElementDescriptor elementDescriptor, Schema schema) throws 
IntrospectionException {
  -        setName(elementDescriptor.getPropertyType().getName());
  -        super.init(configuration, elementDescriptor, schema);
  +        setName(nameFromDescriptor( elementDescriptor ));
       }
   
  +    /**
  +     * Fills the complex type description.
  +     * @param configuration
  +     * @param elementDescriptor
  +     * @param schema
  +     * @throws IntrospectionException
  +     */
  +    protected void fill(TranscriptionConfiguration configuration, 
ElementDescriptor elementDescriptor, Schema schema) throws 
IntrospectionException
  +    {
  +        elementDescriptor = fillDescriptor(elementDescriptor, schema);
  +        super.init(configuration, elementDescriptor, schema);
  +    }
  +    
  +    private String nameFromDescriptor( ElementDescriptor elementDescriptor ) 
{
  +        return elementDescriptor.getPropertyType().getName();
  +    }
  +    
  +    /**
  +     * Does the given element descriptor match this complex type?
  +     * @param elementDescriptor
  +     * @return true if the descriptor matches
  +     */
  +    public boolean matches(ElementDescriptor elementDescriptor) {
  +        String nameFromDescriptor = nameFromDescriptor ( elementDescriptor );
  +        return nameFromDescriptor.equals(getName());
  +    }
  +    
        /**
        * Gets the name of this type.
        * @return
  
  
  
  1.3       +2 -2      
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/GlobalElement.java
  
  Index: GlobalElement.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/GlobalElement.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- GlobalElement.java        16 Jun 2004 11:21:26 -0000      1.2
  +++ GlobalElement.java        20 Jan 2005 23:09:44 -0000      1.3
  @@ -19,7 +19,7 @@
   
   
   /**
  - * Models the Element tag in the XML schema.
  + * Models a global definition of an <code>element</code>.
    * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
    * @version $Revision$
    */
  
  
  
  1.4       +2 -1      
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/LocalComplexType.java
  
  Index: LocalComplexType.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/LocalComplexType.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- LocalComplexType.java     4 Oct 2004 22:27:12 -0000       1.3
  +++ LocalComplexType.java     20 Jan 2005 23:09:44 -0000      1.4
  @@ -23,6 +23,7 @@
   import org.apache.commons.betwixt.ElementDescriptor;
   
   /**
  + * Models a local <code>complexType</code> definition.
    * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
    * @version $Revision$
    */
  
  
  
  1.4       +28 -4     
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Schema.java
  
  Index: Schema.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/schema/Schema.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Schema.java       4 Oct 2004 22:27:12 -0000       1.3
  +++ Schema.java       20 Jan 2005 23:09:44 -0000      1.4
  @@ -120,10 +120,34 @@
                               elementDescriptor.getLocalName(), 
                               elementDescriptor.getPropertyType().getName());
           addElement(element);
  -        
  -        GlobalComplexType type = new GlobalComplexType(configuration, 
elementDescriptor, this);
  -        addComplexType(type);
  +        addGlobalComplexType(configuration, elementDescriptor);
       }        
  +    
  +    /**
  +     * Adds a new global complex type definition matching the given element 
descriptor.
  +     * If this element descriptor has already been mapped to a global type 
then 
  +     * that is returned.
  +     * @param configuration <code>TranscriptionConfiguration</code>, not null
  +     * @param elementDescriptor <code>ElementDescriptor</code>, not null
  +     * @return <code>GlobalComplexType</code>
  +     * @throws IntrospectionException
  +     */
  +    public GlobalComplexType addGlobalComplexType(TranscriptionConfiguration 
configuration, ElementDescriptor elementDescriptor) throws 
IntrospectionException  {
  +        GlobalComplexType type = null;
  +        for (Iterator it = complexTypes.iterator(); it.hasNext();) {
  +            GlobalComplexType complexType = (GlobalComplexType) it.next();
  +            if (complexType.matches( elementDescriptor )) {
  +                type = complexType;
  +                break;
  +            }
  +        }
  +        if (type == null) {
  +             type = new GlobalComplexType(configuration, elementDescriptor, 
this);
  +                     addComplexType(type);
  +                     type.fill(configuration, elementDescriptor, this);
  +        }
  +        return type;
  +    }
        
       public boolean equals(Object obj) {
        boolean result = false;
  
  
  
  1.4       +53 -47    
jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java
  
  Index: TestSchemaGeneration.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/schema/TestSchemaGeneration.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestSchemaGeneration.java 4 Oct 2004 22:27:13 -0000       1.3
  +++ TestSchemaGeneration.java 20 Jan 2005 23:09:44 -0000      1.4
  @@ -131,21 +131,22 @@
           String xsd = out.getBuffer().toString();
           
           String expected ="<?xml version='1.0'?><xsd:schema 
xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
  -        "<xsd:element name='OrderLineBean' 
type='org.apache.commons.betwixt.schema.OrderLineBean'/>" +
  -        "<xsd:complexType 
name='org.apache.commons.betwixt.schema.ProductBean'>" +
  -        "<xsd:sequence/>" +
  -        "<xsd:attribute name='barcode' type='xsd:string'/>" +
  -        "<xsd:attribute name='code' type='xsd:string'/>" +
  -        "<xsd:attribute name='display-name' type='xsd:string'/>" +
  -        "<xsd:attribute name='name' type='xsd:string'/>" +
  -        "</xsd:complexType>" +
  -        "<xsd:complexType 
name='org.apache.commons.betwixt.schema.OrderLineBean'>" +
  -        "<xsd:sequence>" +
  -        "<xsd:element name='product' 
type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' 
maxOccurs='1'/>" +
  -        "</xsd:sequence>" +
  -        "<xsd:attribute name='quantity' type='xsd:string'/>" +
  -        "</xsd:complexType>" +
  -        "</xsd:schema>";
  +                     "<xsd:element name='OrderLineBean' 
type='org.apache.commons.betwixt.schema.OrderLineBean'/>" +
  +                     "<xsd:complexType 
name='org.apache.commons.betwixt.schema.OrderLineBean'>" +
  +                     "<xsd:sequence>" +
  +                     "<xsd:element name='product' 
type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' 
maxOccurs='1'/>" +
  +                     "</xsd:sequence>" +
  +                     "<xsd:attribute name='quantity' type='xsd:string'/>" +
  +                     "</xsd:complexType>" +
  +                     "<xsd:complexType 
name='org.apache.commons.betwixt.schema.ProductBean'>" +
  +                     "<xsd:sequence/>" +
  +                     "<xsd:attribute name='barcode' type='xsd:string'/>" +
  +                     "<xsd:attribute name='code' type='xsd:string'/>" +
  +                     "<xsd:attribute name='display-name' 
type='xsd:string'/>" +
  +                     "<xsd:attribute name='name' type='xsd:string'/>" +
  +                     "</xsd:complexType>" +
  +                     "</xsd:schema>";
  +
               
           xmlAssertIsomorphicContent(parseString(expected), parseString(xsd), 
true);
       }
  @@ -168,37 +169,42 @@
           String xsd = out.getBuffer().toString();
           
           String expected = "<?xml version='1.0'?><xsd:schema 
xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
  -            "        <xsd:element name='order-bean' 
type='org.apache.commons.betwixt.schema.OrderBean'/>" +
  -            "        <xsd:complexType 
name='org.apache.commons.betwixt.schema.CustomerBean'>" +
  -            "        <xsd:sequence/>" +
  -            "        <xsd:attribute name='code' type='xsd:string'/>" +
  -            "        <xsd:attribute name='country' type='xsd:string'/>" +
  -            "        <xsd:attribute name='name' type='xsd:string'/>" +
  -            "        <xsd:attribute name='postcode' type='xsd:string'/>" +
  -            "        <xsd:attribute name='street' type='xsd:string'/>" +
  -            "        <xsd:attribute name='town' type='xsd:string'/>" +
  -            "        </xsd:complexType>" +
  -            "        <xsd:complexType 
name='org.apache.commons.betwixt.schema.ProductBean'>" +
  -            "        <xsd:sequence/>" +
  -            "        <xsd:attribute name='barcode' type='xsd:string'/>" +
  -            "        <xsd:attribute name='code' type='xsd:string'/>" +
  -            "        <xsd:attribute name='display-name' type='xsd:string'/>" 
+
  -            "        <xsd:attribute name='name' type='xsd:string'/>" +
  -            "        </xsd:complexType>" +
  -            "        <xsd:complexType 
name='org.apache.commons.betwixt.schema.OrderLineBean'>" +
  -            "        <xsd:sequence>" +
  -            "        <xsd:element name='product' 
type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' 
maxOccurs='1'/>" +
  -            "        </xsd:sequence>" +
  -            "        <xsd:attribute name='quantity' type='xsd:string'/>" +
  -            "        </xsd:complexType>" +
  -            "        <xsd:complexType 
name='org.apache.commons.betwixt.schema.OrderBean'>" +
  -            "        <xsd:sequence>" +
  -            "        <xsd:element name='customer' 
type='org.apache.commons.betwixt.schema.CustomerBean' minOccurs='0' 
maxOccurs='1'/>" +
  -            "        <xsd:element name='line' 
type='org.apache.commons.betwixt.schema.OrderLineBean' minOccurs='0' 
maxOccurs='unbounded'/>" +
  -            "        </xsd:sequence>" +
  -            "        <xsd:attribute name='code' type='xsd:string'/>" +
  -            "        </xsd:complexType>" +
  -            "        </xsd:schema>";
  +                     "<xsd:element name='order-bean' 
type='org.apache.commons.betwixt.schema.OrderBean'/>" +
  +                     "" +
  +                     "<xsd:complexType 
name='org.apache.commons.betwixt.schema.OrderBean'>" +
  +                     "       <xsd:sequence>" +
  +                     "               <xsd:element name='customer' 
type='org.apache.commons.betwixt.schema.CustomerBean' minOccurs='0' 
maxOccurs='1'/>" +
  +                     "               <xsd:element name='line' 
type='org.apache.commons.betwixt.schema.OrderLineBean' minOccurs='0' 
maxOccurs='unbounded'/>" +
  +                     "       </xsd:sequence>" +
  +                     "       <xsd:attribute name='code' type='xsd:string'/>" 
+
  +                     "</xsd:complexType>" +
  +                     "" +
  +                     "<xsd:complexType 
name='org.apache.commons.betwixt.schema.CustomerBean'>" +
  +                     "       <xsd:sequence/>" +
  +                     "       <xsd:attribute name='code' type='xsd:string'/>" 
+
  +                     "       <xsd:attribute name='country' 
type='xsd:string'/>" +
  +                     "       <xsd:attribute name='name' type='xsd:string'/>" 
+
  +                     "       <xsd:attribute name='postcode' 
type='xsd:string'/>" +
  +                     "       <xsd:attribute name='street' 
type='xsd:string'/>" +
  +                     "       <xsd:attribute name='town' type='xsd:string'/>" 
+
  +                     "</xsd:complexType>" +
  +                     "" +
  +                     "<xsd:complexType 
name='org.apache.commons.betwixt.schema.OrderLineBean'>" +
  +                     "       <xsd:sequence>" +
  +                     "               <xsd:element name='product' 
type='org.apache.commons.betwixt.schema.ProductBean' minOccurs='0' 
maxOccurs='1'/>" +
  +                     "       </xsd:sequence>" +
  +                     "       <xsd:attribute name='quantity' 
type='xsd:string'/>" +
  +                     "</xsd:complexType>" +
  +                     "" +
  +                     "<xsd:complexType 
name='org.apache.commons.betwixt.schema.ProductBean'>" +
  +                     "       <xsd:sequence/>" +
  +                     "               <xsd:attribute name='barcode' 
type='xsd:string'/>" +
  +                     "               <xsd:attribute name='code' 
type='xsd:string'/>" +
  +                     "               <xsd:attribute name='display-name' 
type='xsd:string'/>" +
  +                     "               <xsd:attribute name='name' 
type='xsd:string'/>" +
  +                     "       </xsd:complexType>" +
  +                     "" +
  +                     "</xsd:schema>";
       
            xmlAssertIsomorphicContent(parseString(xsd), parseString(expected));
       }
  
  
  
  1.14      +7 -0      
jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/xmlunit/XmlTestCase.java
  
  Index: XmlTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/xmlunit/XmlTestCase.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- XmlTestCase.java  13 Jun 2004 21:32:49 -0000      1.13
  +++ XmlTestCase.java  20 Jan 2005 23:09:44 -0000      1.14
  @@ -534,6 +534,13 @@
           return result;
       }
       
  +    
  +    public void xmlAssertIsValid(String document, String schema) 
  +        throws ParserConfigurationException, IOException
  +    {
  +        xmlAssertIsValid(new InputSource(new StringReader(document)), new 
InputSource(new StringReader(schema))); 
  +    }
  +    
       public void xmlAssertIsValid(InputSource documentSource, InputSource 
schemaSource) 
           throws ParserConfigurationException, IOException
       {
  
  
  

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

Reply via email to