users@jaxb.java.net

Re: Does jaxb 2.1 enforce xs:restriction

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Thu, 8 Jan 2009 15:33:51 +0100

First, you create this schema object by setting up a schema
factory for the schema language of your choice. Then you
create the Schema object by calling the factory's method newSchema:

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

Schema mySchema;
SchemaFactory sf =
    SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
try {
    mySchema = sf.newSchema( file );
} catch( SAXException saxe ){
    // ...(error handling)
    mySchema = null;
}

After the Unmarshaller object has been established, you pass it the schema.

JAXBContext jc = JAXBContext.newInstance( sb.toString() );
Unmarshaller u = jc.createUnmarshaller();
u.setSchema( mySchema );

Basically that's all there is to it. If the XML data validation fails,
an UnmarshalException (from javax.xml.bind) is thrown. Make sure to let
the user of your program see the exception message so that the problem can
be fixed.

-W


On Thu, Jan 8, 2009 at 3:00 PM, chamibuddhika <chamibuddhika_at_gmail.com>wrote:

>
> Hi,
>
> By this do you mean that I should pass the schema to a my implementation of
> unmarshaller which does extra validations such as range checking?
>
>
> Wolfgang Laun-2 wrote:
> >
> >
> > You might consider passing the XML schema to your marhaller or
> > unmarshaller
> > to achieve the more meticulous schema validation.
> >
> > -W
> >
> > Regards
> >
> > On Thu, Jan 8, 2009 at 10:29 AM, chamibuddhika
> > <chamibuddhika_at_gmail.com>wrote:
> >
> >>
> >> Hi,
> >>
> >> I've got a schema with following element.
> >>
> >> <xs:element name="Amount">
> >> <xs:simpleType>
> >> <xs:restriction base="xs:int">
> >> <xs:minExclusive
> >> value="1"></xs:minExclusive>
> >> <xs:maxExclusive
> >> value="1000000"></xs:maxExclusive>
> >> </xs:restriction>
> >> </xs:simpleType>
> >> </xs:element>
> >>
> >> In the generated java class the setter method does not enforce the
> >> restriction when setting a value to the amount variable allowing any
> >> value
> >> to be assigned to Amount.
> >>
> >> public void setAmount(int value) {
> >> this.amount = value;
> >> }
> >>
> >> I'm programmatically generating source files from SchemaCompiler class
> >> and
> >> using jaxb 2.1. I'm new to JAXB and may be missing some obvious point.
> >> Can
> >> anybody please explain how this restriction can be enforced. I've
> >> attached
> >> the generated source file for reference as well.
> >> http://www.nabble.com/file/p21348458/LoanServiceRequest.java
> >> LoanServiceRequest.java
> >>
> >> Thanks in advance.
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/Does-jaxb-2.1-enforce-xs%3Arestriction-tp21348458p21348458.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
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Does-jaxb-2.1-enforce-xs%3Arestriction-tp21348458p21352301.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
>
>