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:at.ac.tuwien.dsg.quelle.cloudDescriptionParsers.impl.CloudFileDescriptionParser.java

public CloudProvider getCloudProviderDescription(String descriptionFile) {
    try {// ww w .j  a  va 2  s .c om
        JAXBContext jAXBContext = JAXBContext.newInstance(CloudProvider.class);
        InputStream fileStream = context.getResource(descriptionFile).getInputStream();
        return (CloudProvider) jAXBContext.createUnmarshaller().unmarshal(fileStream);
    } catch (Exception ex) {
        log.error("Cannot unmarshall : {}", ex.getMessage());
        ex.printStackTrace();
        return new CloudProvider("empty");
    }
}

From source file:org.castor.jaxb.CastorJAXBContextTest.java

/**
 * Sets up the test environment./*from   ww w  . ja  va2s .  c  o  m*/
 *
 * @throws javax.xml.bind.JAXBException
 *             if any error occurs
 */
@Before
public void setUp() throws JAXBException {

    context = JAXBContext.newInstance(Entity.class);
}

From source file:net.orpiske.sas.commons.xml.XmlWriterUtils.java

/**
 * Marshals an object into a formatted XML document
 * @param element the JAXB element object that represents an 'object' of
 * type T//from ww w.j ava2 s . c om
 * @param object the object to be transformed into XML
 * @param stream the output stream
 * @throws JAXBException if unable to transform the object to XML
 */
public static <T> void marshal(JAXBElement<T> element, T object, OutputStream stream) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(object.getClass().getPackage().getName());

    Marshaller m = context.createMarshaller();

    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(element, stream);
}

From source file:dk.statsbiblioteket.doms.licensemodule.integrationtest.LicenseModuleRestWSTester.java

@SuppressWarnings("all")
private static void testValidateAccess() throws Exception {
    // Test Validate Access
    ValidateAccessInputDTO input = new ValidateAccessInputDTO();

    ArrayList<UserObjAttributeDTO> userObjAttributes = createUserObjAttributeDTO();
    input.setAttributes(userObjAttributes);

    ArrayList<String> groups = new ArrayList<String>();
    groups.add("IndividueltForbud");
    groups.add("Klausuleret");
    input.setGroups(groups);//from w w  w  .j  a  v a2 s .  c o  m
    input.setPresentationType("images");

    JAXBContext context = JAXBContext.newInstance(ValidateAccessInputDTO.class);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(input, outputStream);

    // serialize to XML
    String inputXML = outputStream.toString();
    System.out.println("input xml:\n" + inputXML);

    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client
            .resource(UriBuilder.fromUri("http://localhost:8080/licensemodule/services/").build());

    // Call with XML
    ValidateAccessOutputDTO output = service.path("validateAccess").type(MediaType.TEXT_XML)
            .accept(MediaType.TEXT_XML).entity(inputXML).post(ValidateAccessOutputDTO.class);

    // Call with @XmlRootElement
    // output = service.path("validateAccess").type(MediaType.TEXT_XML).accept(MediaType.TEXT_XML).entity(input).post(ValidateAccessOutputDTO.class);

    context = JAXBContext.newInstance(ValidateAccessOutputDTO.class);
    outputStream = new ByteArrayOutputStream();
    m = context.createMarshaller();

    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(output, outputStream);

    // serialize to XML
    String outputXML = outputStream.toString();
    System.out.println(outputXML);

    // Access depends on the licenses in the DB.
    System.out.println("access :" + output.isAccess());
}

From source file:com.zaubersoftware.gnip4j.http.JSONDeserializationTest.java

/** setup test */
@Before/*from   w w  w. j a  v a  2 s .c o  m*/
public void setUp() throws Exception {

    mapper = DefaultGnipStream.getObjectMapper();
    ctx = JAXBContext.newInstance(Activity.class.getPackage().getName());
}

From source file:org.castor.jaxb.CastorJAXBIntrospectorTest.java

/**
 * Sets up the test environment./*w  w w .j a  va  2 s.  com*/
 *
 * @throws javax.xml.bind.JAXBException
 *             if any error occurs
 */
@Before
public void setUp() throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(Entity.class);
    introspector = context.createJAXBIntrospector();
}

From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java

public static Object unmarshallFromString(Class destClass, String xmlStr) {
    try {//from   w w w . j  av  a  2 s .  c o  m
        JAXBContext jaxbContext = JAXBContext.newInstance(destClass);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        StringReader reader = new StringReader(xmlStr);
        return unmarshaller.unmarshal(reader);
    } catch (JAXBException ex) {
        System.err.println(ex.getMessage());
        return null;
    }
}

From source file:Main.java

/**
 *
 * @param elmnt//w  w w. j  ava 2s. c om
 * @param cls
 * @return
 * @throws JAXBException
 */
public static Object deserialize(Element elmnt, Class cls) throws JAXBException {
    final Unmarshaller um = JAXBContext.newInstance(cls).createUnmarshaller();
    return um.unmarshal(elmnt);
}

From source file:se.inera.intyg.intygstjanst.web.integration.stub.RevokeMedicalCertificateResponderStubTest.java

@Test
public void testName() throws Exception {
    // read request from file
    JAXBContext jaxbContext = JAXBContext.newInstance(RevokeMedicalCertificateRequestType.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    RevokeMedicalCertificateRequestType request = unmarshaller.unmarshal(new StreamSource(
            new ClassPathResource("revoke-medical-certificate/revoke-medical-certificate-request.xml")
                    .getInputStream()),/*from w w  w. j ava 2s  .c  om*/
            RevokeMedicalCertificateRequestType.class).getValue();

    stub.revokeMedicalCertificate(null, request);

    verify(store).makulera(UTLATANDE_ID, REVOKE_MESSAGE);
}

From source file:com.vmware.identity.rest.core.server.authorization.token.saml.SAMLTokenBuilder.java

/**
 * @return {@link JAXBContext} for {@link Constants#ASSERTION_JAXB_PACKAGE}
 * @throws ServerException//from w  ww  .  j  a va 2s .  com
 */
private static JAXBContext createJAXBContext() throws ServerException {
    try {
        return JAXBContext.newInstance(Constants.ASSERTION_JAXB_PACKAGE);
    } catch (JAXBException e) {
        throw new ServerException("Cannot initialize JAXBContext.", e);
    }
}