Example usage for javax.xml.bind Unmarshaller setSchema

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

Introduction

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

Prototype

public void setSchema(javax.xml.validation.Schema schema);

Source Link

Document

Specify the JAXP 1.3 javax.xml.validation.Schema Schema object that should be used to validate subsequent unmarshal operations against.

Usage

From source file:org.apache.nifi.web.security.spring.LoginIdentityProviderFactoryBean.java

private LoginIdentityProviders loadLoginIdentityProvidersConfiguration() throws Exception {
    final File loginIdentityProvidersConfigurationFile = properties.getLoginIdentityProviderConfigurationFile();

    // load the users from the specified file
    if (loginIdentityProvidersConfigurationFile.exists()) {
        try {/*from   ww  w.jav  a  2 s .c  o m*/
            // find the schema
            final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            final Schema schema = schemaFactory
                    .newSchema(LoginIdentityProviders.class.getResource(LOGIN_IDENTITY_PROVIDERS_XSD));

            // attempt to unmarshal
            final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
            unmarshaller.setSchema(schema);
            final JAXBElement<LoginIdentityProviders> element = unmarshaller.unmarshal(
                    new StreamSource(loginIdentityProvidersConfigurationFile), LoginIdentityProviders.class);
            return element.getValue();
        } catch (SAXException | JAXBException e) {
            throw new Exception("Unable to load the login identity provider configuration file at: "
                    + loginIdentityProvidersConfigurationFile.getAbsolutePath());
        }
    } else {
        throw new Exception("Unable to find the login identity provider configuration file at "
                + loginIdentityProvidersConfigurationFile.getAbsolutePath());
    }
}

From source file:org.artifactory.jaxb.JaxbHelper.java

@SuppressWarnings({ "unchecked" })
public T read(InputStream stream, Class clazz, URL schemaUrl) {
    T o = null;/*from  w ww . j  a va2 s.  c  om*/
    try {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        if (schemaUrl != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(schemaUrl);
            unmarshaller.setSchema(schema);
        }
        o = (T) unmarshaller.unmarshal(stream);
    } catch (Throwable t) {
        // The following code resolves the errors from JAXB result.
        // Just throwing new RuntimeException doesn't shows the root cause of the failure and it is almost impossible
        // to conclude it without this log.
        if (t instanceof IllegalAnnotationsException) {
            List<IllegalAnnotationException> errors = ((IllegalAnnotationsException) t).getErrors();
            if (errors != null) {
                for (IllegalAnnotationException error : errors) {
                    log.error("Failed to read object from stream, error:", error);
                }
            }
        }
        throw new RuntimeException("Failed to read object from stream.", t);
    } finally {
        IOUtils.closeQuietly(stream);
    }
    return o;
}

From source file:org.artifactory.version.ConfigXmlConversionTest.java

private CentralConfigDescriptor getConfigValid(String configXml) throws Exception {
    JAXBContext context = JAXBContext.newInstance(CentralConfigDescriptorImpl.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL xsdUrl = getClass().getResource("/artifactory.xsd");
    Schema schema = sf.newSchema(xsdUrl);
    unmarshaller.setSchema(schema);
    return (CentralConfigDescriptor) unmarshaller.unmarshal(new StringReader(configXml));
}

From source file:org.atlasapi.remotesite.itv.ItvCatchupClient.java

public List<ItvProgramme> get(String uri) throws Exception {
    Reader in = new StringReader(httpClient.getContentsOf(uri));
    Unmarshaller u = context.createUnmarshaller();
    u.setSchema(null);
    ItvProgrammes itvProgrammes = (ItvProgrammes) u.unmarshal(in);
    for (ItvProgramme program : itvProgrammes.programmeList()) {
        log.info("fetching details for: " + program.url());
        fetchEpisodeDetails(program);//from  w  w w  . j a  v  a2 s  .  c  om
    }
    return itvProgrammes.programmeList();
}

From source file:org.bibsonomy.rest.renderer.impl.JAXBRenderer.java

/**
 * Unmarshalls the document from the reader to the generated java
 * model.//  ww  w .j  a  v a2  s . c  o m
 * 
 * @return A BibsonomyXML object that contains the unmarshalled content
 * @throws InternServerException
 *             if the content can't be unmarshalled
 */
private BibsonomyXML parse(final Reader reader) throws InternServerException {
    // first: check the reader 
    this.checkReader(reader);
    try {
        // initialize JAXB context. We provide the classloader here because we experienced that under
        // certain circumstances (e.g. when used within JabRef as a JPF-Plugin), the wrong classloader is
        // used which has the following exception as consequence:
        //
        //   javax.xml.bind.JAXBException: "org.bibsonomy.rest.renderer.xml" doesnt contain ObjectFactory.class or jaxb.index
        //
        // (see also http://ws.apache.org/jaxme/apidocs/javax/xml/bind/JAXBContext.html)
        final JAXBContext jc = this.getJAXBContext();

        // create an Unmarshaller
        final Unmarshaller u = jc.createUnmarshaller();

        // set schema to validate input documents
        if (this.validateXMLInput) {
            u.setSchema(schema);
        }

        /*
         * unmarshal a xml instance document into a tree of Java content
         * objects composed of classes from the restapi package.
         */
        final JAXBElement<BibsonomyXML> xmlDoc = unmarshal(u, reader);
        return xmlDoc.getValue();
    } catch (final JAXBException e) {
        if (e.getLinkedException() != null && e.getLinkedException().getClass() == SAXParseException.class) {
            final SAXParseException ex = (SAXParseException) e.getLinkedException();
            throw new BadRequestOrResponseException("Error while parsing XML (Line " + ex.getLineNumber()
                    + ", Column " + ex.getColumnNumber() + ": " + ex.getMessage());
        }
        throw new InternServerException(e.toString());
    }
}

From source file:org.bonitasoft.engine.io.IOUtil.java

public static <T> T unmarshallXMLtoObject(final byte[] xmlObject, final Class<T> objectClass,
        final URL schemaURL) throws JAXBException, IOException, SAXException {
    if (xmlObject == null) {
        return null;
    }// w  w w.j  a va 2s . c  om
    if (schemaURL == null) {
        throw new IllegalArgumentException("schemaURL is null");
    }
    final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final Schema schema = sf.newSchema(schemaURL);
    final JAXBContext contextObj = JAXBContext.newInstance(objectClass);
    final Unmarshaller um = contextObj.createUnmarshaller();
    um.setSchema(schema);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(xmlObject)) {
        final StreamSource ss = new StreamSource(bais);
        final JAXBElement<T> jaxbElement = um.unmarshal(ss, objectClass);
        return jaxbElement.getValue();
    }
}

From source file:org.collectionspace.services.client.CollectionSpaceClientUtils.java

/**
 * getObjectFromStream get object of given class from given inputstream
 * @param jaxbClass//from  w  w w  . j  a va  2 s. c o  m
 * @param is stream to read to construct the object
 * @return
 * @throws Exception
 */
static public Object getObjectFromStream(Class<?> jaxbClass, InputStream is) throws Exception {
    JAXBContext context = JAXBContext.newInstance(jaxbClass);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    //note: setting schema to null will turn validator off
    unmarshaller.setSchema(null);
    return jaxbClass.cast(unmarshaller.unmarshal(is));
}

From source file:org.collectionspace.services.client.test.BaseServiceTest.java

/**
 * getObjectFromFile get object of given class from given file (in classpath)
 * @param jaxbClass//www. j  a v  a  2s.c  o m
 * @param fileName of the file to read to construct the object
 * @return
 * @throws Exception
 */
static protected Object getObjectFromFile(Class<?> jaxbClass, String fileName) throws Exception {

    JAXBContext context = JAXBContext.newInstance(jaxbClass);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    //note: setting schema to null will turn validator off
    unmarshaller.setSchema(null);
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    InputStream is = tccl.getResourceAsStream(fileName);
    return getObjectFromStream(jaxbClass, is);
}

From source file:org.collectionspace.services.client.test.BaseServiceTest.java

/**
 * getObjectFromStream get object of given class from given inputstream
 * @param jaxbClass/*from   w ww  .java2s. co  m*/
 * @param is stream to read to construct the object
 * @return
 * @throws Exception
 */
static protected Object getObjectFromStream(Class<?> jaxbClass, InputStream is) throws Exception {
    JAXBContext context = JAXBContext.newInstance(jaxbClass);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    //note: setting schema to null will turn validator off
    unmarshaller.setSchema(null);
    return jaxbClass.cast(unmarshaller.unmarshal(is));
}

From source file:org.echocat.adam.configuration.ConfigurationMarshaller.java

@Nonnull
protected static Unmarshaller unmarshallerFor(@Nonnull Object element, @Nullable String systemId) {
    final Unmarshaller unmarshaller;
    try {//from  ww  w. jav  a 2 s  .  co  m
        unmarshaller = JAXB_CONTEXT.createUnmarshaller();
        unmarshaller.setSchema(SCHEMA);
    } catch (final JAXBException e) {
        throw new ConfigurationException(
                "Could not create unmarshaller to unmarshall " + systemId != null ? systemId
                        : element.toString() + ".",
                e);
    }
    return unmarshaller;
}