You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Federico Rossetti <fe...@gmail.com> on 2013/11/05 17:29:03 UTC

Problem about generating Java Source Code with XMLBeans from xsd

Hi all


i have a starting xsd no editable, from which to generate the Java source
code with XMLBeans. My problem is on the generated java class SubInfo.java.


In the xsd, the element “Nome”, child of the element “SubInfo”, has as type
“Testo70Custom”, but in the generated java code the type is “Testo140”. Is
the xsd poorly written the problem? Or is the process of code generation
the problem?


The correct type is important because in my java code – client side – I use
it to get some attributes of the xsd elements (like max length of string,
to truncate strings in surplus) from the schemaytpe in order to perform the
validation before sending out the message. If the type is wrong, I could
send erroneous messages.


In binary file .xsb generated with XmlBeans,  i see that in the properties
of the element “Nome” there is a difference between the Type
(testo70custom8e3etype) and the Type for Java Signature (testo1405c59type).


The three files are attached to the message.


Thanks for a response


Federico

Re: Problem about generating Java Source Code with XMLBeans from xsd

Posted by Federico Rossetti <fe...@gmail.com>.
Hi Cezar


First of all thanks for your quick response.


If I understand correctly, using SubInfo.xsetNome with Testo70Custom object
as parameter, I still have to call the “validate” method to check if the
message is correct against the xsd, is it true?


What I wanted to do was something different like this:



String value = //String of more than 70 characters


SubInfo sub =
DocumentoDocument.Factory.newInstance().addNewDocumento().addNewCreditore();


//Maximum length of the element type "Nome", which should be "Testo70Custom"

int subInfoMaxLength =
Integer.parseInt(sub.xgetNome().type.getFacet(SchemaType.FACET_MAX_LENGTH).getStringValue());


String name = //substring of “value” with maxlength = “subInfoMaxLength”


sub.setNome(name);



However the length of "subInfoMaxLength" is not 70 but is 140, that is the
length of the type Testo140, because like you said java rules and schema
rules don’t match fully.


I thought of this strategy to automatically manage any "max length" changes
of elements in XSD.


Can I manage these changes automatically in any other way?


Thanks for your response

Federico


2013/11/5 Cezar Andrei <ce...@oracle.com>

> Federico,
>
> The schema looks fine, if you look closely at SubInfo type def:
>
> <xs:complexType name="SubInfo">
> <xs:complexContent>
> <xs:restriction base="Info">
> <xs:sequence>
> <xs:element name="Nome" type="Testo70Custom"/>
> </xs:sequence>
> </xs:restriction>
> </xs:complexContent>
> </xs:complexType>
> <xs:complexType name="Info">
> <xs:sequence>
> <xs:element name="Nome" type="Testo140" minOccurs="0"/>
> </xs:sequence>
> </xs:complexType>
> <xs:simpleType name="Testo70Custom">
> <xs:restriction base="Testo140">
> <xs:maxLength value="70"/>
> </xs:restriction>
> </xs:simpleType>
> <xs:simpleType name="Testo140">
> <xs:restriction base="xs:string">
> <xs:minLength value="1"/>
> <xs:maxLength value="140"/>
> </xs:restriction>
> </xs:simpleType>
>
> SubInfo is a restriction of type Info, i.e. it has to have a property
> called Nome of type Testo140.
> But in the same type the element Nome inside the SubInfo sequence is
> declared of type Testo70Custom, which is fine because Testo70Custom is a
> subtype of Testo140.
>
> This is a case where java rules and schema rules don't match fully. But if
> you're a little careful everything works fine,  make sure that you use
> SubInfo.xsetNome() only with objects of type Testo70Custom.
>
> Cezar
>
>
> On Nov 5, 2013, at 10:29 AM, Federico Rossetti <
> federico.rossetti.86@gmail.com> wrote:
>
> Hi all
>
>
> i have a starting xsd no editable, from which to generate the Java source
> code with XMLBeans. My problem is on the generated java class SubInfo.java.
>
>
> In the xsd, the element “Nome”, child of the element “SubInfo”, has as
> type “Testo70Custom”, but in the generated java code the type is
> “Testo140”. Is the xsd poorly written the problem? Or is the process of
> code generation the problem?
>
>
> The correct type is important because in my java code – client side – I
> use it to get some attributes of the xsd elements (like max length of
> string, to truncate strings in surplus) from the schemaytpe in order to
> perform the validation before sending out the message. If the type is
> wrong, I could send erroneous messages.
>
>
> In binary file .xsb generated with XmlBeans,  i see that in the properties
> of the element “Nome” there is a difference between the Type
> (testo70custom8e3etype) and the Type for Java Signature (testo1405c59type).
>
>
> The three files are attached to the message.
>
>
> Thanks for a response
>
>
> Federico
> <SubInfo.java><subinfo0d05type.xsb><xsdtest.xsd>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>
>

Re: Problem about generating Java Source Code with XMLBeans from xsd

Posted by Cezar Andrei <ce...@oracle.com>.
Federico,

The schema looks fine, if you look closely at SubInfo type def:

<xs:complexType name="SubInfo">
		<xs:complexContent>
			<xs:restriction base="Info">
				<xs:sequence>
					<xs:element name="Nome" type="Testo70Custom"/>
				</xs:sequence>
			</xs:restriction>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="Info">
		<xs:sequence>
			<xs:element name="Nome" type="Testo140" minOccurs="0"/>
		</xs:sequence>
	</xs:complexType>
	<xs:simpleType name="Testo70Custom">
		<xs:restriction base="Testo140">
			<xs:maxLength value="70"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="Testo140">
		<xs:restriction base="xs:string">
			<xs:minLength value="1"/>
			<xs:maxLength value="140"/>
		</xs:restriction>
	</xs:simpleType>

SubInfo is a restriction of type Info, i.e. it has to have a property called Nome of type Testo140.
But in the same type the element Nome inside the SubInfo sequence is declared of type Testo70Custom, which is fine because Testo70Custom is a subtype of Testo140.

This is a case where java rules and schema rules don't match fully. But if you're a little careful everything works fine,  make sure that you use SubInfo.xsetNome() only with objects of type Testo70Custom.

Cezar


On Nov 5, 2013, at 10:29 AM, Federico Rossetti <fe...@gmail.com> wrote:

> Hi all
> 
> 
> 
> i have a starting xsd no editable, from which to generate the Java source code with XMLBeans. My problem is on the generated java class SubInfo.java.
> 
> 
> 
> In the xsd, the element “Nome”, child of the element “SubInfo”, has as type “Testo70Custom”, but in the generated java code the type is “Testo140”. Is the xsd poorly written the problem? Or is the process of code generation the problem?
> 
> 
> 
> The correct type is important because in my java code – client side – I use it to get some attributes of the xsd elements (like max length of string, to truncate strings in surplus) from the schemaytpe in order to perform the validation before sending out the message. If the type is wrong, I could send erroneous messages.
> 
> 
> 
> In binary file .xsb generated with XmlBeans,  i see that in the properties of the element “Nome” there is a difference between the Type (testo70custom8e3etype) and the Type for Java Signature (testo1405c59type).
> 
> 
> 
> The three files are attached to the message.
> 
> 
> 
> Thanks for a response
> 
> 
> 
> Federico
> 
> <SubInfo.java><subinfo0d05type.xsb><xsdtest.xsd>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org