Example usage for javax.xml.bind JAXBContext newInstance

List of usage examples for javax.xml.bind JAXBContext newInstance

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext newInstance.

Prototype

public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException 

Source Link

Document

Create a new instance of a JAXBContext class.

Usage

From source file:com.wandrell.example.swss.test.util.SoapMessageUtils.java

/**
 * Creates an {@code Entity} from the received {@code SOAPMessage}.
 * <p>//from w w w. j  av  a2s.c  o m
 * The message should be valid and contain a {@code GetEntityResponse}.
 *
 * @param message
 *            the SOAP message
 * @return an {@code Entity} parsed from the {@code SOAPMessage}
 * @throws JAXBException
 *             if there is any problem when unmarshalling the data
 * @throws SOAPException
 *             if there is any problem when reading the SOAP message
 */
public static final Entity getEntity(final SOAPMessage message) throws JAXBException, SOAPException {
    final JAXBContext jc; // Context for unmarshalling
    final Unmarshaller um; // Unmarshaller for the SOAP message
    final GetEntityResponse response; // Unmarshalled response

    jc = JAXBContext.newInstance(GetEntityResponse.class);
    um = jc.createUnmarshaller();
    response = (GetEntityResponse) um.unmarshal(message.getSOAPBody().extractContentAsDocument());

    return response.getEntity();
}

From source file:com.quangphuong.crawler.util.XMLUtil.java

public <T> T unmarshallUtil(String xmlPath, Class<T> entityClass) throws Exception {
    //        try {
    JAXBContext cxt = JAXBContext.newInstance(entityClass);
    Unmarshaller unmarshaller = cxt.createUnmarshaller();
    File file = new File(rootPath, xmlPath);
    T result = (T) unmarshaller.unmarshal(file);

    return result;
    //        } catch (Exception e) {
    //            e.printStackTrace();
    //        }/*from   w w w  . jav a  2s  .  c o m*/
    //        return null;
}

From source file:com.wavemaker.tools.service.definitions.ServiceDefinitionsTest.java

@Override
public void setUp() throws Exception {

    this.jaxbContext = JAXBContext.newInstance("com.wavemaker.tools.service.definitions");
    this.unmarshaller = this.jaxbContext.createUnmarshaller();
    this.marshaller = this.jaxbContext.createMarshaller();
}

From source file:com.rabidgremlin.legalbeagle.util.HttpHelper.java

public HttpHelper() throws Exception {
    JAXBContext jc = JAXBContext.newInstance("com.rabidgremlin.legalbeagle.xmlbindings");
    u = jc.createUnmarshaller();//  ww  w.jav  a2s.  co m

    // -Dhttp.proxyHost=prox -Dhttp.proxyPort=1234
    // TODO: better error handling here
    if (System.getProperty("http.proxyHost") != null) {
        proxy = new HttpHost(System.getProperty("http.proxyHost"), Integer.getInteger("http.proxyPort"));
    }

}

From source file:mx.bigdata.cfdi.CFDv3.java

private static final JAXBContext createContext() {
    try {//  ww w  .  j  a va 2  s .  co  m
        return JAXBContext.newInstance("mx.bigdata.cfdi.schema");
    } catch (Exception e) {
        throw new Error(e);
    }
}

From source file:org.jasig.portlet.campuslife.athletics.dao.AthleticsDaoMockImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    try {/*w w  w.ja v  a  2s  . c o m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(AthleticsFeed.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        this.feed = (AthleticsFeed) unmarshaller.unmarshal(mockData.getInputStream());
    } catch (IOException e) {
        log.error("Failed to read mock data", e);
    } catch (JAXBException e) {
        log.error("Failed to unmarshall mock data", e);
    }
}

From source file:demo.jaxrs.util.Marshal.java

public static <T> T unmarshal(Class<T> xmlType, HttpResponse httpResponse) throws JAXBException {
    String namespace = "";
    try {// ww w. java2  s .  co m
        namespace = Util.getStringFromInputStream(httpResponse.getEntity().getContent());
        System.out.println(namespace);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

    System.out.println(namespace);
    namespace = namespace.replaceAll("xmlns=\"http://ws.wso2.org/dataservice\"", "");
    System.out.println(namespace);
    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlType);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    T doc = (T) unmarshaller.unmarshal(stream);
    return doc;
}

From source file:com.photon.phresco.plugins.util.WarConfigProcessor.java

public void save() throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Assembly.class);
    Marshaller marshal = jaxbContext.createMarshaller();
    marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshal.marshal(assembly, file);/*from  w ww.ja v a  2  s .c  o  m*/
}

From source file:org.javelin.sws.ext.bind.SweJaxbContextCreationTest.java

@Test
public void useOurImplementation() throws Exception {
    JAXBContext context = JAXBContext.newInstance(org.javelin.sws.ext.bind.context1.MyClass1.class);
    assertThat(context.getClass().getName(), equalTo("org.javelin.sws.ext.bind.SweJaxbContext"));

    context = JAXBContext.newInstance(org.javelin.sws.ext.bind.context1.MyClass1.class.getPackage().getName());
    assertThat(context.getClass().getName(), equalTo("org.javelin.sws.ext.bind.SweJaxbContext"));

    context = JAXBContext.newInstance(new Class<?>[] { org.javelin.sws.ext.bind.context1.MyClass1.class },
            new HashMap<String, Object>());
    assertThat(context.getClass().getName(), equalTo("org.javelin.sws.ext.bind.SweJaxbContext"));

    context = JAXBContext.newInstance(org.javelin.sws.ext.bind.context1.MyClass1.class.getPackage().getName(),
            this.getClass().getClassLoader());
    assertThat(context.getClass().getName(), equalTo("org.javelin.sws.ext.bind.SweJaxbContext"));

    context = JAXBContext.newInstance(org.javelin.sws.ext.bind.context1.MyClass1.class.getPackage().getName(),
            this.getClass().getClassLoader(), new HashMap<String, Object>());
    assertThat(context.getClass().getName(), equalTo("org.javelin.sws.ext.bind.SweJaxbContext"));
}

From source file:arxiv.xml.XMLParser.java

/**
 * Constructs a new XML parser by initializing the JAXB unmarshaller and setting up the XML validation.
 *
 * @throws HarvesterError if there are any problems
 *///from   www .  j  a  v  a  2s .  c  o  m
public XMLParser() {
    try {
        unmarshaller = JAXBContext.newInstance("org.openarchives.oai._2:org.arxiv.oai.arxivraw")
                .createUnmarshaller();
    } catch (JAXBException e) {
        throw new HarvesterError("Error creating JAXB unmarshaller", e);
    }

    ClassLoader classLoader = this.getClass().getClassLoader();
    List<Source> schemaSources = Lists.newArrayList();

    schemaSources.add(new StreamSource(classLoader.getResourceAsStream("OAI-PMH.xsd")));
    schemaSources.add(new StreamSource(classLoader.getResourceAsStream("arXivRaw.xsd")));

    try {
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(schemaSources.toArray(new Source[schemaSources.size()]));
        unmarshaller.setSchema(schema);
    } catch (SAXException e) {
        throw new HarvesterError("Error creating validation schema", e);
    }
}