santhosh-tekuri/jlibs

Option: generateDefaultAttributes not working 100% for me

PiQ99 opened this issue · 8 comments

PiQ99 commented

Hello guys,

my problem is the following: The option of the xsInstance: generateDefaultAttributes does not work for me. Its more or less working randomly.
Sometimes it creates the default-value in the generated xml-document, but in 50% it does not [30 cases where default-values are assigned in a given xsd-document, resulting in 15 assigned values in a generated xml-document].

In addition, I already set this option
xsInstance.generateDefaultAttributes = Boolean.TRUE;
to true and the 2nd approach via
Properties props = new Properties(); props.put("generateDefaultAttributes", "always"); xsInstance.loadOptions(props);.

But none of these approaches generates 100 % of the default-values.

Do you have any other hint for me to get it work or is it a bug in jlibs?
I am currently working with the latest released version 2.1.

xsInstance.generateDefaultAttributes = Boolean.TRUE;

this should generate default attributes always.
can you attach sample xsd to reproduce this issue also sample code

the reason may be the attribute you are thinking having default value, jlibs might be assuming it has no default value. If you can give sample schema where this is reproducable it would be helpful

PiQ99 commented

Well, lets have a look at this wellformed and valid xsd file (I can not attach this file-type):

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema 
    targetNamespace="http://testdata/testObject_Ok1_V02" 
    xmlns="http://testdata/testObject_Ok1_V02" 
    xmlns:testObject_Ok1_V02="http://testdata/testObject_Ok1_V02" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="testObject_Ok1_V02_Typ">
        <xs:all>
            <xs:element name="refObj1_V02" type="testObject_Ok1_V02:refObj1_V02_Typ" minOccurs="0" maxOccurs="1">
            </xs:element>
            <xs:element name="refObj2_V02" type="testObject_Ok1_V02:refObj2_V02_Typ" minOccurs="0" maxOccurs="1">
            </xs:element>
            <xs:element name="refObj3_V02" type="testObject_Ok1_V02:refObj3_V02_Typ" minOccurs="1" maxOccurs="1">
            </xs:element>
            <xs:element name="refObj4_V02" type="testObject_Ok1_V02:refObj4_V02_Typ" minOccurs="1" maxOccurs="1">
            </xs:element>
        </xs:all>
    </xs:complexType>
    <xs:element name="testObject_Ok1_V02">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="testObject_Ok1_V02:testObject_Ok1_V02_Typ">
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="refObj1_V02_Typ">
        <xs:all>
            <xs:element name="Uuid_refObj1_V02" type="testObject_Ok1_V02:Uuid_refObj1_V02_Typ" minOccurs="1" maxOccurs="1">
            </xs:element>
        </xs:all>
    </xs:complexType>
    <xs:complexType name="Uuid_refObj1_V02_Typ">
        <xs:all>
            <xs:element name="ObjektNamespace_V02" type="testObject_Ok1_V02:ObjektNamespace_V02_Typ" minOccurs="1" maxOccurs="1" default="http://testdata/default1/0/0/0">
            </xs:element>
            <xs:element name="ObjektUuid_V02" type="testObject_Ok1_V02:ObjektUuid_V02_Typ" minOccurs="1" maxOccurs="1">
            </xs:element>
        </xs:all>
    </xs:complexType>
    <xs:complexType name="refObj4_V02_Typ">
        <xs:all>
            <xs:element name="Uuid_refObj4_V02" type="testObject_Ok1_V02:Uuid_refObj4_V02_Typ" minOccurs="1" maxOccurs="1">
            </xs:element>
        </xs:all>
    </xs:complexType>
    <xs:complexType name="Uuid_refObj4_V02_Typ">
        <xs:all>
            <xs:element name="ObjektNamespace_V02" type="testObject_Ok1_V02:ObjektNamespace_V02_Typ" minOccurs="1" maxOccurs="1" default="http://testdata/default2/0/0/0">
            </xs:element>
            <xs:element name="ObjektUuid_V02" type="testObject_Ok1_V02:ObjektUuid_V02_Typ" minOccurs="1" maxOccurs="1">
            </xs:element>
        </xs:all>
    </xs:complexType>
    <xs:complexType name="Uuid_refObj2_V02_Typ">
        <xs:all>
            <xs:element name="ObjektNamespace_V02" type="testObject_Ok1_V02:ObjektNamespace_V02_Typ" minOccurs="1" maxOccurs="1" default="http://testdata/default3/0/0/0">
            </xs:element>
            <xs:element name="ObjektUuid_V02" type="testObject_Ok1_V02:ObjektUuid_V02_Typ" minOccurs="1" maxOccurs="1">
            </xs:element>
        </xs:all>
    </xs:complexType>
    <xs:complexType name="refObj2_V02_Typ">
        <xs:all>
            <xs:element name="Uuid_refObj2_V02" type="testObject_Ok1_V02:Uuid_refObj2_V02_Typ" minOccurs="1" maxOccurs="1">
            </xs:element>
        </xs:all>
    </xs:complexType>
    <xs:complexType name="Uuid_refObj3_V02_Typ">
        <xs:all>
            <xs:element name="ObjektNamespace_V02" type="testObject_Ok1_V02:ObjektNamespace_V02_Typ" minOccurs="1" maxOccurs="1" default="http://testdata/default4/0/0/0">
            </xs:element>
            <xs:element name="ObjektUuid_V02" type="testObject_Ok1_V02:ObjektUuid_V02_Typ" minOccurs="1" maxOccurs="1">
            </xs:element>
        </xs:all>
    </xs:complexType>
    <xs:complexType name="refObj3_V02_Typ">
        <xs:all>
            <xs:element name="Uuid_refObj3_V02" type="testObject_Ok1_V02:Uuid_refObj3_V02_Typ" minOccurs="1" maxOccurs="1">
            </xs:element>
        </xs:all>
    </xs:complexType>

    <xs:simpleType name="ObjektUuid_V02_Typ">
        <xs:annotation>
            <xs:documentation>              
                UUID v4 Definition
            </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:pattern value="[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-4[0-9A-Fa-f]{3}-[89ABab][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}">
            </xs:pattern>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="ObjektNamespace_V02_Typ">
        <xs:restriction base="xs:anyURI">
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

In addition I use the following source-code to reproduce the scenario:

public static void createXsd() throws TransformerConfigurationException {
        XSModel xsModel = new XSParser().parse("testdata.xsd");
        XSInstance xsInstance = new XSInstance();
        xsInstance.showContentModel = Boolean.FALSE;
        xsInstance.generateDefaultAttributes = Boolean.TRUE;
        xsInstance.generateOptionalElements = Boolean.TRUE;
        QName rootElement = new QName("http://testdata/testObject_Ok1_V02", "testObject_Ok1_V02", "tns");
        XMLDocument sampleXml = new XMLDocument(new StreamResult(System.out), true, 4, null);
        xsInstance.generate(xsModel, rootElement, sampleXml);
    }

public static void createXsd2() throws TransformerConfigurationException {
        XSModel xsModel = new XSParser().parse("testdata.xsd");
        XSInstance xsInstance = new XSInstance();
        Properties props = new Properties();
        props.put("generateDefaultAttributes", "always");
        props.put("generateFixedAttributes", "always");
        props.put("showContentModel", "always");
        xsInstance.loadOptions(props);
        QName rootElement = new QName("http://testdata/testObject_Ok1_V02", "testObject_Ok1_V02", "tns");
        XMLDocument sampleXml = new XMLDocument(new StreamResult(System.out), true, 4, null);
        xsInstance.generate(xsModel, rootElement, sampleXml);
    }

public static void main(String[] args) {
        try {
            createXsd();
            createXsd2();
            System.exit(1);
        }
        catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

All ObjektNamespaces have default-values, but the are not generated each time - sometimes there is only one generated value, sometimes zero and sometimes three.

Thanks for your help :-)

looks like you are referring:

<xs:element name="ObjektNamespace_V02" type="testObject_Ok1_V02:ObjektNamespace_V02_Typ" minOccurs="1" maxOccurs="1" default="http://testdata/default3/0/0/0">
            </xs:element>

here element has default value http://testdata/default3/0/0/0
generateDefaultAttributes hold only for attributes (not for elements)
i.e it applies for

<xs:attribute name="at1" default="some defaultValue" type="xs:string"/>

currently if element has a default value, it randomly picks default value or generate random value.
there is no flag currently to tweak this one. will add a flag for this.

new flag is added:

xsInstnce.generateDefaultElementValues = Boolean.TRUE;

this flag lets you pick default values for elements always

I am uploading the fix to 2.2-SNAPSHOT version. will notify once it is uploaded

<dependencies>
    <dependency>
        <groupId>in.jlibs</groupId>
        <artifactId>jlibs-xsd</artifactId>
        <version>2.2-SNAPSHOT</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
      <id>sonatype-snapshots</id>
      <name>sonatype snapshots repository</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    </repository>
</repositories>

use 2.2-SNAPSHOT as shown above. Do not forget to add repository to your pom.
after above changes, your build should pick up the changes