users@jaxb.java.net

Re: anyURI to java.net.URI

From: bugger <sexy_at_trash-mail.com>
Date: Fri, 23 Dec 2011 04:23:45 -0800 (PST)

I've found a method to achieve this:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0">
        <xs:annotation>
                <xs:appinfo>
                        <jaxb:globalBindings>
                                <jaxb:javaType name="java.net.URI" xmlType="xs:anyURI"
parseMethod="create" printMethod="toASCIIString"/>
                        </jaxb:globalBindings>
                </xs:appinfo>
        </xs:annotation>
        <xsd:element name="URI" type="xsd:anyURI"/>
</xs:schema>

The uncaught exception-error is resolved by specifying a parseMethod, in
this case 'create' which results in calling the static method
URI.create(String) which does not require Exception handling as it converts
those exceptions into Runtime exceptions. For the completeness I also
specified a printMethod which calls .toASCIIString() which guarentees a
string produced in ASCII format.


Kenny MacLeod wrote:
>
> You could write a subtype of XmlJavaTypeAdapter which did the
> conversion, and then using binding customisations to associate it with
> the URI type.
>
> chrisrk wrote:
>> Hi
>>
>> Seems this is a common question, but none of the methods I've tried seem
>> to
>> work.
>>
>> I have a schema containing some anyURI elements, I'd like JAXB to
>> automatically read them into java.net.URI objects rather than Strings.
>>
>> I found
>> https://metro.dev.java.net/guide/Customizing_XML_Schema_binding.html, and
>> modified my Ant script to suit. Simple mode is being enabled;
>> Unmarshaller.unmarshal(..) now returns a custom object rather than a
>> JAXBElement - but anyURIs are still Strings.
>>
>> I tried using
>>
>> <jaxb:javaType name="java.net.URI" xmlType="xs:anyURI"/>
>>
>> also, but that gives me problems with uncaught URISyntaxExceptions - who
>> is
>> supposed to catch those? Is there a way of having them flag as validation
>> errors?
>>
>> Thanks in advance,
>>
>> Chris
>>
>> --
>> View this message in context:
>> http://www.nabble.com/anyURI-to-java.net.URI-tp14949162p14949162.html
>> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>
>

-- 
View this message in context: http://old.nabble.com/anyURI-to-java.net.URI-tp14949162p33028500.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.