Generating parameterized constructor for Type/Class from XSD - proxy

I'm new in XSD side. Would anybody do let me know either there is a way to Generate a Class from XSD (ComplexType) with parameterized constructor.
For Example. I've following XSD with a ComplexType A and I want that when proxy generated from this XSD. type A contains a constructor with a string value.
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="A"> <xs:sequence> <xs:element name="property1" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema>
A _object = new A("this is value");
This is possible or not?
Any help in this regards will be highly appreciable.
/Rizwan

Seeing your other post that involves the .NET platform, I assume you're after the same here, too.
The answer is no; in general XSD to code generators use only default constructors (parameterless) for reasons related to deserialization.
You could use initializers.

Related

Proper ObjectFactory in MyBatis TypeHandler

I'm creating a project in Spring-ws with MyBatis.
I have many web services definitions in xsd.
Spring is creating POJO classes based on xsd.
The problem is that in xsd I have fields that have parameter nillable="true"
like this:
<xs:element name="numericParam" type="xs:int" minOccurs="0" maxOccurs="1" nillable="true"/>
That field in generated POJO looks like this:
protected JAXBElement<Integer> numericParam;
I want to create custom TypeHandler in MyBatis config, to handle JAXBElement<Integer> types, but how can I know which ObjectFactory to use.
Is it possible to know which mapper is calling my custom TypeHandler? In TypeHandler all I have is column name.
This is not possible currently (version 3.4.6) in mybatis. No information about the context (like enclosing type or mapper method) is passed to handler.
If you do want to use jaxb generated types you would need to create a handler per field (of cause it make sense to have common base that would implement all the logic).

Couldn't generate data contract code

Here's a sample of the code I'm trying to generate:
<xs:simpleType name="IVL">
<xs:restriction base="xs:duration"/>
</xs:simpleType>
<xs:complexType name="SF">
<xs:attribute name="varde" type="xs:boolean"/>
</xs:complexType>
The Complex type is generated but the simple types are left out.
Am I missing a setting or something or aren't WSCF.Blue able to handle simple types?
I am not aware of any commonnly used XSD-to-code binding approach that on .NET would generate classes for simple types, except when XSD simpleTypes are restricted using enumeration facets.
This article pretty much stands for WSCF as well at least in the area of Type/primitive mapping.
Just for trivia, JAXB - a Java based approach for binding XML to Java classes - has this option built in (i.e. to generate classes even for simple types).

request definition in wadl file

Is there any way the wadl can tells the request type. For example the following PUT method expect a xml data type of "setBlockRequest", is there any way I can reference it to the xml schema(xsd file) to define the content of "setBlockRequest"?
This wadl is generated by Jersey.
<resource path="/appliance/{device_id}/update_multiple_values">
<param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="device_id" style="template" type="xs:string"/>
<method id="setBlockValue" name="PUT">
<request>
<ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02" xmlns="" element="setBlockRequest" mediaType="application/xml"/>
</request>
</method>
</resource>}
yes, there is. See http://www.w3.org/Submission/wadl/#x3-40001.3 (<grammars> tag).
Jersey now generated it automatically for you but only for cases where your type is annotated with JAXB annotations. (This is valid since Jersey 1.13 if I remember correctly, so you might want to give it a try).

JAX-WS RI does not enforce XSD restrictions

I am currently developing a few Web services using the JAX-WS reference implementation (version 2.1.7). They are contract-based, that is, the WSDL and XSD files are not generated by wsgen.
This allows me to freely use XSD restrictions to strengthen validation of values passed to my services through SOAP messages. Here are two examples of such "restricted" XSD elements:
<xsd:element name="maxResults" minOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:positiveInteger">
<xsd:minInclusive value="1"/>
<xsd:maxInclusive value="1000"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="lastName" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
<xsd:maxLength value="25"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
I added the #SchemaValidation annotation to my service classes to enforce schema validation. However, JAX-WS does not enforce validation rules as expected. The behaviour is as follows:
Missing mandatory elements are correctly reported (e.g., missing maxResults).
Invalid values (e.g., character data in an integer field) are correctly reported too.
Interval restriction violations (e.g., maxResults > 1000 or maxResults < 1) pass through the validation process without being reported and are injected into my JAXB-generated Java structures. Even negative values are considered valid despite the xsd:positiveInteger type!
String length constraint violations (e.g., lastName length over 25 characters) are not reported either.
In other words, restrictions that appear in <xsd:element> tags are correctly enforced but <xsd:restriction> elements seem to be totally ignored by JAXB when used in a JAX-WS-based context.
I wrote a test class to check my XSD restrictions using bare JAXB (no JAX-WS). As a result, all restrictions are correctly enforced.
This gives me the feeling that there might be a bug in the usage of JAXB by JAX-WS... unless there is something I am doing incorrectly, of course...
Am I missing something fundamental here?!?
Thanks in advance for any help,
Jeff
I finally found what's wrong...
In order to get my Web services to work in a JUnit context, i.e. published through Endpoint.publish(), I had to remove the wsdlLocation attribute from my #WebService annotations. If I don't, the wsdlLocation = "WEB-INF/wsdl/SearchIndividualsV1_0.wsdl" passed to the #WebService annotation clashes with the URL value passed to the Endpoint.publish() method, http://127.0.0.1:9000/rpe-ws/SearchIndividuals.
After reading Glen Mazza's Weblog (http://www.jroller.com/gmazza/entry/soap_xml_schema_validation), Additional Notes section, I put back the wsdlLocation attribute and all restrictions are now properly enforced.
In other words, removing the wsdlLocation in a #WebService annotation does not prevent the service itself from working, but prevents restrictions declared in <xsd:restrictions> elements from being properly enforced. Restrictions declared in <xsd:element> elements, however, are still correctly enforced.
I am therefore getting back to having to solve that wsdlLocation compatibility problem to make my unit tests work properly, but this is way less critical than non-working validations in a production context...
Just in case... Anyone has an idea about this WSDL location incompatibility when running a Web service in a non-Web context?
Thanks,
Jeff
Oh brother!...
In order to override the wsdlLocation for my JUnit tests, I created derivations of my Web service implementations that override only the #WebService annotation. As a result, I ran into the same problem I finally solved this morning (ref. my first answer above).
After doing plenty of tests, I figured out that it's the presence of a #WebService-annotated class extending my Web service implementation that prevents XSD validation from properly handling <xsd:restriction> tags.
To illustrate this bizarre behaviour, let's suppose I have the following classes:
#WebService(...)
public interface JeffWebService {...}
#WebService(..., wsdlLocation = "path/myWsdl.wsdl", ...)
public class JeffWebServiceImpl implements JeffWebService {...}
where path/myWsdl.wsdl correctly locates the WSDL. Then XSD validation works properly, i.e. the content of my first answer above is totally valid.
I now add the following class that I use in my JUnit-based Endpoint.publish() calls:
#WebService(..., wsdlLocation = "alternatePath/myWsdl.wsdl", ...)
public class TestWebServiceImpl extends JeffWebServiceImpl {}
that overrides nothing but the #WebService annotation. Then XSD validation excludes <xsd:restriction> tags as it used to do before specifying the wsdlLocation attribute at all, despite the fact that I still use the JeffWebServiceImpl implementation in my non-JUnit code! If I comment out the annotation in TestWebServiceImpl, then everything works properly again, except for unit tests, of course.
In other words, as soon as there is some class extending my Web service implementation in the classpath, the #WebService annotation of the most specific class overrides all others, regardless of the actual class I use in a regular Web application context. Weird, isn't it?!?
Bottom line: I will disable Endpoint-based unit tests for now. If I (or anyone reading this thread) find a clean, non-bogus way to integrate both production and JUnit configurations, I will consider putting them back in my JUnit test suite.
I hope this thread will help anyone running into the same problem solve it faster than I did...
Jeff

How can I mix and match custom Spring schema types with traditional Spring schema types?

Let's say I have a class Person with properties name and age, and it can be configured with Spring like so:
<beans:bean id="person" class="com.mycompany.Person">
<beans:property name="name" value="Mike"/>
<beans:property name="age" value="38"/>
</beans:bean>
I'd like to have a custom Spring schema element for it, which is easy to do, allowing me to have this in my Spring configuration file:
<Person name="Mike" age="38"/>
The schema definition would look something like this:
<xsd:complexType name="Person">
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="age" type="xsd:int"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
Based on this, let's now say I would like the option of mixing and matching my custom schema element with traditional elements and attributes of Spring beans, so I could have the option of doing this:
<Person name="Mike">
<beans:property name="age" value="38"/>
</Person>
How would I go about doing that? Perhaps this is impossible without some major customization, but I'm hoping there is some fairly simple mechanism to achieve this. I thought extending "bean" might be the trick, but that doesn't look to be correct.
First of, if your example is really all you want to do, consider using p-namespace instead and save yourself some major headache:
<beans:bean id="person" class="com.mycompany.Person" p:name="Mike" p:age="38"/>
Yes, it doesn't look as pretty as <Person name= age= /> but there's no custom code to write :-)
If that does not satisfy your requirements, you're looking at implementing your own namespace / bean definition parser. Spring documentation has a chapter dedicated to that which will explain it better then I can. If you hit any issues, please post back specific questions and I'll try to help.

Resources