History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: CASTOR-414
Type: Bug Bug
Status: Open Open
Priority: Major Major
Assignee: Edward Kuns
Reporter: jfrancis
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
castor

Bug with nested sequence with minOccurs=0 on outer sequence

Created: 31/Mar/03 08:20 AM   Updated: 02/Nov/06 03:50 PM
Component/s: XML code generator
Affects Version/s: 0.9.4.2
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments: 1. Text File patch.c414.20061102.txt (3 kb)

Environment:
Operating System: Windows 2000
Platform: PC

Bugzilla Id: 1271


 Description  « Hide
I have generated from the following .xsd

<=====start
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:simpleType name="LiteralDescription">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="40"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:element name="Stuff" type="LiteralDescription"/>

<xsd:complexType name="OuterElType">
<xsd:sequence minOccurs="0" >
<xsd:sequence >
<xsd:element ref="Stuff"/>
</xsd:sequence>
</xsd:sequence>
</xsd:complexType>

<xsd:element name="OuterEl" type="OuterElType"/>

</xsd:schema>
==> finish

If I use the generated classes ( generation script is ...
java cp "C:\projects\castor\castor-0.9.4.3\castor
0.9.4.3.jar;C:\projects\castor\castor-0.9.4.3\castor-0.9.4.3-
xml.jar;C:\projects\xerces\xerces-2_0_2\xercesImpl.jar"
org.exolab.castor.builder.SourceGenerator -i Test.xsd -package myTest
... ) to Unmarshall the following file

<==== starts
<OuterElType
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='Test.xsd'></OuterElType>
===> ends

I get the following exception trace....

Oh no : ValidationException: error-if-this-is-used is a required field.;

  • location of error: XPATH: OuterEl{file: [not available]; line: 3; column: 58}
    ValidationException: error-if-this-is-used is a required field.;
  • location of error: XPATH: OuterEl
    at org.exolab.castor.xml.FieldValidator.validate
    (FieldValidator.java:195)
    at org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate
    (XMLClassDescriptorImpl.java:896)
    at org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate
    (XMLClassDescriptorImpl.java:764)
    at org.exolab.castor.xml.Validator.validate(Validator.java:127)
    at org.exolab.castor.xml.UnmarshalHandler.endElement
    (UnmarshalHandler.java:695)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement
    (AbstractSAXParser.java:572)
    at org.apache.xerces.impl.XMLNamespaceBinder.endElement
    (XMLNamespaceBinder.java:646)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement
    (XMLDTDValidator.java:1972)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement
    (XMLDTDValidator.java:878)
    at
    org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.handleEndElement
    (XMLDocumentFragmentScannerImpl.java:1144)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement
    (XMLDocumentFragmentScannerImpl.java:987)
    at
    org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.
    dispatch(XMLDocumentFragmentScannerImpl.java:1445)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument
    (XMLDocumentFragmentScannerImpl.java:333)
    at org.apache.xerces.parsers.DTDConfiguration.parse
    (DTDConfiguration.java:524)
    at org.apache.xerces.parsers.DTDConfiguration.parse
    (DTDConfiguration.java:580)
    at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)
    at org.apache.xerces.parsers.AbstractSAXParser.parse
    (AbstractSAXParser.java:1169)
    at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:555)
    at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:487)
    at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:627)
    at myTest.MainTest.main(MainTest.java:30)

Despite the fact that the XML is legal. I think this comes because
org.exolab.castor.xml.FieldValidator.validate does not check the value of
minOccurs before validating whether any thing as been found.

Also the constructor for the outerEl type descriptor sets the field descriptor
for the outer sequence as required even when ( minOccurs=0 ) it is not.

Okay the example is contrived but the real one we are using is much bigger..

Thanks for Castor it is useful all the same.... Hope this helps...



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
jfrancis - 01/Apr/03 12:28 AM
Althouigh I recorded this against 0.9.4.2 my version is actually 0.9.4.3 which
was the latest release version ( its not available on the drop downs ).

Here is the test program ....

/*

  • MainTest.java
    *
  • Created on 31 March 2003, 10:56
    */

package myTest;

import java.io.*;
import org.exolab.castor.xml.*;

/**
*

  • @author john
    */
    public class MainTest {

/** Creates a new instance of MainTest */
public MainTest() {
}

/**

  • @param args the command line arguments
    */
    public static void main(String[] args) {

try

{ FileReader reader = new FileReader("C:\\projects\\castor\\project- example\\bugzilla\\Test.xml"); OuterEl aMesg = (OuterEl)Unmarshaller.unmarshal(OuterEl.class, reader); System.out.println("Finished " + aMesg); }

catch(Exception ex)

{ System.err.println("Oh no : " + ex ); ex.printStackTrace(); }

}

}


jfrancis - 01/Apr/03 12:39 AM
Looking at the generated source I have OuterElDescriptor which is derived from
OuterElTypeDescriptor.

When an instance of OuterElTypeDescriptor is created then its child fields
don't seem to be set up correctly ( maybe this is work in progress ... I came
to this to help out with a customer in the last two days ). The child element
field descriptors are not set up with the correct minOccurs or alternatively
there required values are not correct.

//-- initialize element descriptors

//-- _outerElTypeSequence
desc = new org.exolab.castor.xml.util.XMLFieldDescriptorImpl
(myTest.OuterElTypeSequence.class, "_outerElTypeSequence", "error-if-this-is
used-", org.exolab.castor.xml.NodeType.Element);
handler = (new org.exolab.castor.xml.XMLFieldHandler() {
public java.lang.Object getValue( java.lang.Object object )
throws IllegalStateException

{ OuterElType target = (OuterElType) object; return target.getOuterElTypeSequence(); }

public void setValue( java.lang.Object object, java.lang.Object
value)
throws IllegalStateException, IllegalArgumentException
{
try { OuterElType target = (OuterElType) object; target.setOuterElTypeSequence( (myTest.OuterElTypeSequence) value); }
catch (java.lang.Exception ex) { throw new IllegalStateException(ex.toString()); }
}
public java.lang.Object newInstance( java.lang.Object parent ) { return new myTest.OuterElTypeSequence(); }
} );
desc.setHandler(handler);
desc.setContainer(true);
desc.setClassDescriptor(new myTest.OuterElTypeSequenceDescriptor());
// NOT REQUIRED ... OR MINOCCURS 0.
desc.setRequired(true);
desc.setMultivalued(false);
addFieldDescriptor(desc);

//-- validation code for: _outerElTypeSequence
fieldValidator = new org.exolab.castor.xml.FieldValidator();
//!!!!!!!!!!!!!!!!!!!!!! THIS SHOULD BE 0 ?
fieldValidator.setMinOccurs(1);

{ //-- local scope }

desc.setValidator(fieldValidator);

However in the code which throws the Exception
org.exolab.castor.xml.FieldValidator.validate, minOccurs is not checked until
after the code has checked that the descriptor has an associated "object
instance" from the xml instance being unmarshalled. If the descriptor has
required set to true then we get the exception...

if ((value == null) && (_descriptor.isRequired())) {


Keith Visco - 30/Apr/03 02:54 PM
Arnaud, any thoughts on this...do you have a chance to look into this one any
time soon?

Werner Guttmann - 02/Nov/06 03:46 PM
Why would one use a <sequence> of <sequence>'s, if I may ask ? This simply does not make sense to me semantically. Anybody ?

Werner Guttmann - 02/Nov/06 03:50 PM
JUnit test case (relative to src/bugs) to reproduce this problem.