castor
  1. castor
  2. CASTOR-1177

If you read a schema and write it back out, a default="*" attribute is added to element declaration if a referring element contains the default="*" attribute.

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Critical Critical
    • Resolution: Unresolved
    • Affects Version/s: 0.9.6, 0.9.7
    • Fix Version/s: None
    • Component/s: XML
    • Labels:
      None
    • Environment:
      Windows XP Pro
    • Number of attachments :
      0

      Description

      Input schema:

      <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema
      targetNamespace="http://schemas.data.net/fields"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:dsf="http://schemas.data.net/fields"
      xmlns="http://schemas.data.net/fields"
      elementFormDefault="qualified">

      <xs:simpleType name="STRING30_Type">
      <xs:restriction base="xs:string">
      <xs:maxLength value="30"/>
      </xs:restriction>
      </xs:simpleType>

      <xs:simpleType name="STRING100_Type">
      <xs:restriction base="xs:string">
      <xs:maxLength value="100"/>
      </xs:restriction>
      </xs:simpleType>

      <xs:element name="DEPARTMENTCODE" type="STRING30_Type"/>
      <xs:element name="DESCRIPTION" type="STRING100_Type"/>

      <xs:complexType name="DEPARTMENTID_Type">
      <xs:sequence>
      <xs:element ref="DEPARTMENTCODE"/>
      <xs:element ref="DESCRIPTION" minOccurs="0"/>
      </xs:sequence>
      </xs:complexType>

      <xs:element name="DEPARTMENTID" type="DEPARTMENTID_Type"/>
      <xs:element name="TESTCODE" type="STRING30_Type" default="*"/>

      <xs:complexType name="TESTRATEID_Type">
      <xs:sequence>
      <xs:element ref="DEPARTMENTID"/>
      <xs:element ref="TESTCODE" minOccurs="0"/>
      </xs:sequence>
      </xs:complexType>

      <xs:element name="testEl" type="TESTRATEID_Type" />

      </xs:schema>

      is run through the following test code:

      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.OutputStreamWriter;

      import org.exolab.castor.xml.schema.Schema;
      import org.exolab.castor.xml.schema.reader.SchemaReader;
      import org.exolab.castor.xml.schema.writer.SchemaWriter;

      public class SchemaBugTest
      {

      public static void main(String[] aArgs) throws Exception

      { String xsdFileName = aArgs[0]; SchemaBugTest test = new SchemaBugTest(xsdFileName); test.runTest(); }

      private String mXsdFileName;

      public SchemaBugTest( String aXsdFileName )

      { mXsdFileName = aXsdFileName; }

      public void runTest() throws Exception

      { Schema schema = loadSchemaFromFile(); writeSchema( schema ); }

      protected Schema loadSchemaFromFile() throws Exception
      {
      File file = new File(mXsdFileName);
      if( !file.isFile() )

      { throw new FileNotFoundException(mXsdFileName); }

      SchemaReader reader = new SchemaReader( file.toURL().toExternalForm() );
      Schema schema = reader.read();
      schema.validate();
      return schema;
      }

      protected void writeSchema( Schema aSchema ) throws Exception
      {
      OutputStreamWriter out = null;
      try

      { File inputFile = new File(mXsdFileName); File outputFile = new File( inputFile.getParentFile(), "output_"+inputFile.getName() ); out = new OutputStreamWriter( new FileOutputStream( outputFile ) ); SchemaWriter sw = new SchemaWriter( out ); sw.write( aSchema ); }

      finally
      {
      try

      {out.close();}

      catch(IOException io)

      {/* eat me*/}

      }
      }
      }

      and results in the following output (indented and comment added manually):

      <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:dsf="http://schemas.data.net/fields"
      xmlns="http://schemas.data.net/fields"
      targetNamespace="http://schemas.data.net/fields"
      elementFormDefault="qualified">

      <xs:element name="TESTCODE" type="dsf:STRING30_Type" default="*"/>
      <xs:element name="DESCRIPTION" type="dsf:STRING100_Type"/>
      <xs:element name="DEPARTMENTCODE" type="dsf:STRING30_Type"/>
      <xs:element name="testEl" type="dsf:TESTRATEID_Type"/>
      <xs:element name="DEPARTMENTID" type="dsf:DEPARTMENTID_Type"/>

      <xs:complexType name="DEPARTMENTID_Type">
      <xs:sequence>
      <xs:element ref="dsf:DEPARTMENTCODE"/>
      <xs:element ref="dsf:DESCRIPTION" minOccurs="0"/>
      </xs:sequence>
      </xs:complexType>

      <xs:complexType name="TESTRATEID_Type">
      <xs:sequence>
      <xs:element ref="dsf:DEPARTMENTID"/>
      <!-- ************************** -->
      <!-- THIS IS THE PROBLEM -->
      <!-- ************************** -->
      <xs:element ref="dsf:TESTCODE" minOccurs="0" default="*"/>
      </xs:sequence>
      </xs:complexType>

      <xs:simpleType name="STRING30_Type">
      <xs:restriction base="xs:string">
      <xs:maxLength value="30"/>
      </xs:restriction>
      </xs:simpleType>

      <xs:simpleType name="STRING100_Type">
      <xs:restriction base="xs:string">
      <xs:maxLength value="100"/>
      </xs:restriction>
      </xs:simpleType>
      </xs:schema>

      Note that the default="*" attribute has been inserted (on deserialization). Validation against this schema fails in xerces, xml spy, etc.

      Sample xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <tns:testEl
      xmlns="http://schemas.data.net/fields"
      xmlns:tns="http://schemas.data.net/fields" >

      <tns:DEPARTMENTID>
      <tns:DEPARTMENTCODE>DeptCode</tns:DEPARTMENTCODE>
      <tns:DESCRIPTION>Dummy Desc</tns:DESCRIPTION>
      </tns:DEPARTMENTID>

      <tns:TESTCODE>mytestcode</tns:TESTCODE>

      </tns:testEl>

        Activity

        Hide
        Edwin Lee added a comment -

        Same issue encountered here. In addition, the "default" attribute can contain any value. The attribute will be "copied to" the referring element.

        Source XSD:

        SampleXsd.xsd
        <?xml version="1.0" encoding="UTF-8"?>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
            <xsd:element name="SampleElement" type="xsd:string" default="defaultValue"/>
            <xsd:element name="AnotherElement">
                <xsd:complexType>
                    <xsd:all>
                        <xsd:element ref="SampleElement"/>
                    </xsd:all>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
        

        Program Source Code:

        TestCastor.java
        import java.io.BufferedReader;
        import java.io.BufferedWriter;
        import java.io.ByteArrayOutputStream;
        import java.io.FileInputStream;
        import java.io.FileNotFoundException;
        import java.io.IOException;
        import java.io.InputStream;
        import java.io.InputStreamReader;
        import java.io.OutputStreamWriter;
        import java.io.Reader;
        import java.io.UnsupportedEncodingException;
        import java.io.Writer;
        
        import org.exolab.castor.xml.schema.Schema;
        import org.exolab.castor.xml.schema.reader.SchemaReader;
        import org.exolab.castor.xml.schema.writer.SchemaWriter;
        
        import org.xml.sax.InputSource;
        import org.xml.sax.SAXException;
        
        public class TestCastor
        {
            private static final String XSD_FILENAME = "SampleXsd.xsd";
            private static final String ENCODING = "UTF-8";
            
            public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, SAXException, IOException
            {
                InputStream fis = null;
                Reader isr = null;
                Reader reader = null;
                
                ByteArrayOutputStream baos = null;
                Writer osw = null;
                Writer writer = null;
                
                try
                {
                    fis = new FileInputStream(XSD_FILENAME);
                    isr = new InputStreamReader(fis, ENCODING);
                    reader = new BufferedReader(isr);
                    
                    InputSource inSrc = new InputSource(reader);
                    inSrc.setSystemId(XSD_FILENAME);
                    SchemaReader scReader = new SchemaReader(inSrc);
                    
                    Schema schema = scReader.read();
                    
                    baos = new ByteArrayOutputStream();
                    osw = new OutputStreamWriter(baos, ENCODING);
                    writer = new BufferedWriter(osw);
                    
                    SchemaWriter scWriter = new SchemaWriter(writer);
                    
                    scWriter.write(schema);
                    writer.flush();
                    
                    byte[] byteArr = baos.toByteArray();
                    
                    String serialized = new String(byteArr, ENCODING);
                    
                    System.out.println(serialized);
                }
                finally
                {
                    if (fis != null)
                    {
                        try
                        {
                            fis.close();
                        }
                        catch (IOException ignore)
                        {
                            //
                        }
                    }
                    
                    if (isr != null)
                    {
                        try
                        {
                            isr.close();
                        }
                        catch (IOException ignore)
                        {
                            //
                        }
                    }
                    
                    if (reader != null)
                    {
                        try
                        {
                            reader.close();
                        }
                        catch (IOException ignore)
                        {
                            //
                        }
                    }
                    
                    if (baos != null)
                    {
                        try
                        {
                            baos.close();
                        }
                        catch (IOException ignore)
                        {
                            //
                        }
                    }
                    
                    if (osw != null)
                    {
                        try
                        {
                            osw.close();
                        }
                        catch (IOException ignore)
                        {
                            //
                        }
                    }
                    
                    if (writer != null)
                    {
                        try
                        {
                            writer.close();
                        }
                        catch (IOException ignore)
                        {
                            //
                        }
                    }
                }
            }
        }
        

        Program Output (Manually Formatted and Indented):

        <?xml version="1.0" encoding="UTF-8"?>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
            <xsd:element name="SampleElement" type="xsd:string" default="defaultValue"/>
            <xsd:element name="AnotherElement">
                <xsd:complexType>
                    <xsd:all>
                        <xsd:element ref="SampleElement" default="defaultValue"/>
                    </xsd:all>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
        

        Will this bug be fixed in the next stable release / milestone build? Thanks.

        Show
        Edwin Lee added a comment - Same issue encountered here. In addition, the "default" attribute can contain any value. The attribute will be "copied to" the referring element. Source XSD: SampleXsd.xsd <?xml version= "1.0" encoding= "UTF-8" ?> <xsd:schema xmlns:xsd= "http: //www.w3.org/2001/XMLSchema" elementFormDefault= "qualified" > <xsd:element name= "SampleElement" type= "xsd:string" default = "defaultValue" /> <xsd:element name= "AnotherElement" > <xsd:complexType> <xsd:all> <xsd:element ref= "SampleElement" /> </xsd:all> </xsd:complexType> </xsd:element> </xsd:schema> Program Source Code: TestCastor.java import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.io.Writer; import org.exolab.castor.xml.schema.Schema; import org.exolab.castor.xml.schema.reader.SchemaReader; import org.exolab.castor.xml.schema.writer.SchemaWriter; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class TestCastor { private static final String XSD_FILENAME = "SampleXsd.xsd" ; private static final String ENCODING = "UTF-8" ; public static void main( String [] args) throws FileNotFoundException, UnsupportedEncodingException, SAXException, IOException { InputStream fis = null ; Reader isr = null ; Reader reader = null ; ByteArrayOutputStream baos = null ; Writer osw = null ; Writer writer = null ; try { fis = new FileInputStream(XSD_FILENAME); isr = new InputStreamReader(fis, ENCODING); reader = new BufferedReader(isr); InputSource inSrc = new InputSource(reader); inSrc.setSystemId(XSD_FILENAME); SchemaReader scReader = new SchemaReader(inSrc); Schema schema = scReader.read(); baos = new ByteArrayOutputStream(); osw = new OutputStreamWriter(baos, ENCODING); writer = new BufferedWriter(osw); SchemaWriter scWriter = new SchemaWriter(writer); scWriter.write(schema); writer.flush(); byte [] byteArr = baos.toByteArray(); String serialized = new String (byteArr, ENCODING); System .out.println(serialized); } finally { if (fis != null ) { try { fis.close(); } catch (IOException ignore) { // } } if (isr != null ) { try { isr.close(); } catch (IOException ignore) { // } } if (reader != null ) { try { reader.close(); } catch (IOException ignore) { // } } if (baos != null ) { try { baos.close(); } catch (IOException ignore) { // } } if (osw != null ) { try { osw.close(); } catch (IOException ignore) { // } } if (writer != null ) { try { writer.close(); } catch (IOException ignore) { // } } } } } Program Output (Manually Formatted and Indented): <?xml version= "1.0" encoding= "UTF-8" ?> <xsd:schema xmlns:xsd= "http: //www.w3.org/2001/XMLSchema" elementFormDefault= "qualified" > <xsd:element name= "SampleElement" type= "xsd:string" default = "defaultValue" /> <xsd:element name= "AnotherElement" > <xsd:complexType> <xsd:all> <xsd:element ref= "SampleElement" default = "defaultValue" /> </xsd:all> </xsd:complexType> </xsd:element> </xsd:schema> Will this bug be fixed in the next stable release / milestone build? Thanks.
        Edward Kuns made changes -
        Field Original Value New Value
        Assignee Edward Kuns [ edward.kuns@rockwellfirstpoint.com ]
        Edward Kuns made changes -
        Summary A default="*" attribute is added to element declaration if a referring element contains the default="*" attribute. This causes xml validation to fail. If you read a schema and write it back out, a default="*" attribute is added to element declaration if a referring element contains the default="*" attribute.
        Edward Kuns made changes -
        Assignee Edward Kuns [ edward.kuns@rockwellfirstpoint.com ]

          People

          • Assignee:
            Unassigned
            Reporter:
            Patrick Collins
          • Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated: