Example usage for javax.xml.bind Unmarshaller getSchema

List of usage examples for javax.xml.bind Unmarshaller getSchema

Introduction

In this page you can find the example usage for javax.xml.bind Unmarshaller getSchema.

Prototype

public javax.xml.validation.Schema getSchema();

Source Link

Document

Get the JAXP 1.3 javax.xml.validation.Schema Schema object being used to perform unmarshal-time validation.

Usage

From source file:org.apache.taverna.scufl2.translator.t2flow.T2FlowParser.java

public Unmarshaller getUnmarshaller() {
    Unmarshaller u = unmarshaller.get();
    if (!isValidating() && u.getSchema() != null) {
        u.setSchema(null);//from  w  w  w.j a v a 2s . com
    } else if (isValidating() && u.getSchema() == null) {
        // Load and set schema to validate against
        Schema schema;
        try {
            SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
            List<URI> schemas = getAdditionalSchemas();
            URL t2flowExtendedXSD = T2FlowParser.class.getResource(T2FLOW_EXTENDED_XSD);
            schemas.add(t2flowExtendedXSD.toURI());

            List<Source> schemaSources = new ArrayList<>();
            for (URI schemaUri : schemas)
                schemaSources.add(new StreamSource(schemaUri.toASCIIString()));
            Source[] sources = schemaSources.toArray(new Source[schemaSources.size()]);
            schema = schemaFactory.newSchema(sources);
        } catch (SAXException e) {
            throw new RuntimeException("Can't load schemas", e);
        } catch (URISyntaxException | NullPointerException e) {
            throw new RuntimeException("Can't find schemas", e);
        }
        u.setSchema(schema);
    }
    return u;
}

From source file:org.codehaus.enunciate.modules.xfire.EnunciatedJAXWSOperationBinding.java

public void readMessage(InMessage message, MessageContext context) throws XFireFault {
    if (this.requestInfo == null) {
        throw new XFireFault("Unable to read message: no request info was found!", XFireFault.RECEIVER);
    }/*from  www .j  a  va2  s.co  m*/

    Object bean;
    try {
        Unmarshaller unmarshaller = this.jaxbContext.createUnmarshaller();
        XMLStreamReader streamReader = message.getXMLStreamReader();
        if (this.requestInfo.isSchemaValidate()) {
            try {
                TransformerFactory xformFactory = TransformerFactory.newInstance();
                DOMResult domResult = new DOMResult();
                xformFactory.newTransformer().transform(new StAXSource(streamReader, true), domResult);
                unmarshaller.getSchema().newValidator().validate(new DOMSource(domResult.getNode()));
                streamReader = XMLInputFactory.newInstance()
                        .createXMLStreamReader(new DOMSource(domResult.getNode()));
            } catch (Exception e) {
                throw new XFireRuntimeException("Unable to validate the request against the schema.");
            }
        }
        unmarshaller.setEventHandler(getValidationEventHandler());
        unmarshaller.setAttachmentUnmarshaller(new AttachmentUnmarshaller(context));
        bean = unmarshaller.unmarshal(streamReader, this.requestInfo.getBeanClass()).getValue();
    } catch (JAXBException e) {
        throw new XFireRuntimeException("Unable to unmarshal type.", e);
    }

    List<Object> parameters = new ArrayList<Object>();
    if (this.requestInfo.isBare()) {
        //bare method, doesn't need to be unwrapped.
        parameters.add(bean);
    } else {
        for (PropertyDescriptor descriptor : this.requestInfo.getPropertyOrder()) {
            try {
                parameters.add(descriptor.getReadMethod().invoke(bean));
            } catch (IllegalAccessException e) {
                throw new XFireFault("Problem with property " + descriptor.getName() + " on "
                        + this.requestInfo.getBeanClass().getName() + ".", e, XFireFault.RECEIVER);
            } catch (InvocationTargetException e) {
                throw new XFireFault("Problem with property " + descriptor.getName() + " on "
                        + this.requestInfo.getBeanClass().getName() + ".", e, XFireFault.RECEIVER);
            }
        }
    }

    message.setBody(parameters);
}