Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
901 views
in Technique[技术] by (71.8m points)

xml - Set an XJC javaType adapter in external binding file

I've an XSD file containing this:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc"
elementFormDefault="qualified"
targetNamespace="http://example.org/">

<xsd:complexType name="Certificate">
    <xsd:sequence>
        <xsd:element name="certificate" type="xsd:base64Binary">
            <xsd:annotation>
                <xsd:appinfo>
                    <xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
                </xsd:appinfo>
            </xsd:annotation>
        </xsd:element>
    </xsd:sequence>
</xsd:complexType>

</xsd:schema>

<xsd:complexType name="User">
    <xsd:sequence>
        <xsd:element name="certificate" type="xsd:base64Binary">
            <xsd:annotation>
                <xsd:appinfo>
                    <xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
                </xsd:appinfo>
            </xsd:annotation>
        </xsd:element>
        <xsd:element name="name" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Server">
    <xsd:sequence>
        <xsd:element name="document" type="xsd:base64Binary" />
    </xsd:sequence>
</xsd:complexType>

I would like to export xjc javaType adapter to an external .xjb file. In this file, I would like to set this adapter for all certificate elements with xsd:base64Binary type but not for document element in Server complex type which also has xsd:base64Binary type.

How can I do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change your xsd in

<xsd:complexType name="Certificate">
    <xsd:sequence>
        <xsd:element name="certificate" type="xsd:base64Binary"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="User">
    <xsd:sequence>
        <xsd:element name="certificate" type="xsd:base64Binary"/>
        <xsd:element name="name" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Server">
    <xsd:sequence>
        <xsd:element name="document" type="xsd:base64Binary" />
    </xsd:sequence>
</xsd:complexType>

and add this binding configuration

<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:annox="http://annox.dev.java.net"
    xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
    <bindings schemaLocation="yourXSD.xsd">

            <bindings node="//xsd:complexType[@name='Certificate']//xsd:sequence//xsd:element[@name='certificate']">
                <xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
            </bindings>
            <bindings node="//xsd:complexType[@name='User']//xsd:sequence//xsd:element[@name='certificate']">
                <xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
            </bindings>

    </bindings>
</bindings>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...