Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve annotations during schema creation #32

Closed
red-hood opened this issue Jun 25, 2016 · 3 comments
Closed

Preserve annotations during schema creation #32

red-hood opened this issue Jun 25, 2016 · 3 comments

Comments

@red-hood
Copy link

Hi,

as far as I understand, the JAXB-Annotations are used to control aspects of the schema creation, e.g. type mapping or namespace binding. Therefore, these kind of Java annotations are not stored in the schema, and will not be reconstructed when generating a Java class from the schema.

For my application, I would like to define some types in JAXB Scheme, and generate Java classes out of it, including third party annotations. Is there any way to store non-JAXB Java annotations in an JAXB Schema file, and have xjc create the classes accordingly?

@whummer
Copy link
Owner

whummer commented Jun 25, 2016

Hi, it's always better to give a concrete example of what you're trying to achieve. As far as I understand, you want to start from an XSD schema that contains <xs:annotation> tags, and then generate Java source code classes which contain the corresponding @Annotation annotations.

Java classes can currently only be generated from WSDL files which contain embedded XML Schemas, using the utility script bin/wsimport.sh. Create a bare minimum WSDL file with your schema as follows. Replace the MySchemaType complex type definition with your own type definition(s) and store the file as /tmp/tmpservice.wsdl .

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://example.com/service/TestService" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="TestService" targetNamespace="http://example.com/service/TestService">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.com/service/TestService" elementFormDefault="unqualified" targetNamespace="http://example.com/service/TestService" version="1.0">
  <xs:element name="testIn" type="xs:anyType"/>
  <xs:element name="testOut" type="xs:anyType"/>

  <xs:complexType name="MySchemaType">
    <xs:annotation>
      <xs:documentation>Your documentation goes here...!</xs:documentation>
    </xs:annotation>
    <xs:sequence/>
  </xs:complexType>

</xs:schema>
  </wsdl:types>
  <wsdl:message name="testOut">
    <wsdl:part element="tns:testOut" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="testIn">
    <wsdl:part element="tns:testIn" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="TestService">
    <wsdl:operation name="test">
      <wsdl:input message="tns:testIn" name="testIn">
    </wsdl:input>
      <wsdl:output message="tns:testOut" name="testOut">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="TestServiceSoapBinding" type="tns:TestService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="test">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="testIn">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="testOut">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="TestService">
    <wsdl:port binding="tns:TestServiceSoapBinding" name="TestServiceImplPort">
      <soap:address location="http://localhost:9001/TestService"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

Next, run these commands (from the directory where you cloned the jaxb-facets repository). I'm assuming that you are running a Unix operating system (e.g., Mac OS, Linux) and that you have Apache Maven (mvn) installed.

mvn install # to compile and install the code in this repo
mkdir /tmp/tmpservice
./bin/wsimport.sh -keep -B-jaxb-facets -d /tmp/tmpservice /tmp/tmpservice.wsdl

This will generate (among other files, which you can ignore), the following file:

/tmp/tmpservice/com/example/service/testservice/MySchemaType.java

Take a look at the file and you will find the desired XSD annotations:

@XmlType(name = "MySchemaType")
@Annotation(documentation = @Documentation("Your documentation goes here...!"))
public class MySchemaType {
}

Good luck, and let me know how it goes... :)

@red-hood
Copy link
Author

Hi,

thanks for your answer. What I would like is to be able to add any type of annotation to a field of a class when generating the .java files from the xsd. The schema files are actually generated from a proprietary description of a binary format, which should then be parsed and serialized with preon (https://github.com/preon/preon).

So, for example, the original class with annotations would look like:

public class FooTest {
    @BoundNumber(size="16", byteOrder = BigEndian)
    public int foo;
    @BoundString
    public String world = "Hello World!";
}

The schema, including the annotations, could then be something along these lines:

  <xs:complexType name="fooTest">
    <xs:sequence>
      <xs:element name="foo" type="xs:int">
         <xs:annotation>
           <xs:jannotation type=BoundNumber>
             <param name="size" type="xs:string" value="16"/>
             <param name="byteOrder" value="java:org.codehaus.preon.buffer.ByteOrder.BigEndian"/>
           <xs:jannotation/>
         </xs:annotation>
      </xs:element>
      <xs:element name="world" type="xs:string" minOccurs="0">
         <xs:annotation>
           <xs:jannotation type=BoundString/>
         </xs:annotation>
    </xs:sequence>
  </xs:complexType>

Additionally, I would also like to add annotations for JPA mappings for each field of a class. As there is no webservice involved, a WSDL would not make much sense it that case.

In the meantime, I think I found what i was looking for: https://github.com/highsource/jaxb2-annotate-plugin

It seems this xjc plugin allows to define arbitrary annotations on Classe/Types and Fields/Elements, by using the xsd:appinfo tag. I will try it and report back on success.

Thank you a lot for your answer so far :)

@red-hood red-hood changed the title Preserver annotations during schema creation Preserve annotations during schema creation Jun 27, 2016
@whummer
Copy link
Owner

whummer commented Jun 28, 2016

Thanks for your detailed answer. jaxb2-annotate-plugin seems to provide what you're looking for. Feel free to come back here if it doesn't, then we can try to work something out... Closing this for now.

@whummer whummer closed this as completed Jun 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants