0

Is it possible to have a binding to a string without an adapter?

I have a 3rd party schema with an attribute with the wrong type (date) and in an external file binding I transform it to string so the class is generated with the correct type. The "problem" is that it does so through a new class "Adapter1" which is very trivial and I prefer it not to be generated.

The attribute is as:

      <xs:attribute  type="xs:date" name="curso" use="required">
      </xs:attribute>

And the current binding is:

<jxb:bindings 
    version="1.0"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings 
            schemaLocation="file.xsd" 
            node="/xs:schema">
        <jxb:bindings node="//xs:element[@name='exportacion_horarios']//xs:attribute[@name='curso']" >
            <jxb:property>
                <jxb:baseType >
                    <jxb:javaType name="java.lang.String" />
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

The adapter class is:

package com.penalara.ghc_importar_exportar.modelo.personalizados.itaca3.exportacion;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class Adapter1
    extends XmlAdapter<String, String>
{


    public String unmarshal(String value) {
        return new String(value);
    }

    public String marshal(String value) {
        if (value == null) {
            return null;
        }
        return value.toString();
    }

}

Maybe something like telling "xjc" to use the internal default adapter for string-to-string.

2 Answers 2

0

You can change the <jxb:javaType name="java.lang.String" /> instruction with XJC's one, which allow defining the adapter parameter and avoid these weird adapter class.

This would give something like the following binding file :

<jxb:bindings 
    version="1.0"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    extensionBindingPrefixes="xjc">
    <jxb:bindings 
            schemaLocation="file.xsd" 
            node="/xs:schema">
        <jxb:bindings node="//xs:element[@name='exportacion_horarios']//xs:attribute[@name='curso']" >
            <jxb:property>
                <jxb:baseType >
                    <xjc:javaType name="java.lang.String"  adapter="com.penalara.ghc_importar_exportar.modelo.ToStringAdapter" />
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

Note that com.penalara.ghc_importar_exportar.modelo.ToStringAdapter stated here is a internal class of yours which will extend XmlAdapter<String, String>

Sign up to request clarification or add additional context in comments.

2 Comments

But still, you need an adapter. jxb:javatype also has a similar option with parseMethod and printMethod. But I want to use the internal conversion jaxb so no code is necessary from my project. Is there an already existing class you can point to in "adapter"?
The jxb:javaType will always generate adapter even if providing the methods. The advantage of xjc:javaType alternative is that you can point at any existing class to avoid generating the adapter. Found com.sun.xml.bind.v2.runtime.RuntimeUtil.ToStringAdapter but unmarshal is exception throwing so my best bet is for you to create the empty XmlAdapter needed
0

You can do it by not using jxb:javaType and putting the name directly in the jxb:baseType.

<jxb:bindings 
    version="1.0"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings 
            schemaLocation="file.xsd" 
            node="/xs:schema">
        <jxb:bindings node="//xs:element[@name='exportacion_horarios']//xs:attribute[@name='curso']" >
            <jxb:property>
                <jxb:baseType name="java.lang.String"/>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

This way it doesn't generate an adapter and uses Java String.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.