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:gov.hhs.fha.nhinc.lift.proxy.util.ProxyUtil.java

public static org.w3c.dom.Element marshal(Object obj) throws JAXBException {
    try {/*from  w  ww .  j  a  v a  2s  .co  m*/
        Document doc = null;
        JAXBContext jc = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = jc.createMarshaller();
        javax.xml.parsers.DocumentBuilderFactory dbf;
        dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        doc = dbf.newDocumentBuilder().newDocument();
        marshaller.marshal(obj, doc);
        log.info("Marshal: " + doc.getNodeValue());
        return doc.getDocumentElement();
    } catch (ParserConfigurationException ex) {
        throw new JAXBException("Unable to create document: " + ex.getMessage());
    }
}

From source file:Main.java

/**
 *
 * @param obj/* ww w.java2  s .  c  om*/
 * @param file
 * @throws JAXBException
 * @throws FileNotFoundException
 */
public static void serialize(Object obj, File file) throws JAXBException, FileNotFoundException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
    m.marshal(obj, new FileOutputStream(file));
}

From source file:gov.nih.nci.cacis.sa.mirthconnect.SemanticAdapterServerSystemTest.java

@Test
public void invoke() throws Exception {

    InputStream sampleMessageIS = FileUtils
            .openInputStream(new File(getClass().getClassLoader().getResource("SARequestSample.xml").toURI()));

    JAXBContext jc = JAXBContext.newInstance(CaCISRequest.class);
    final CaCISRequest request = (CaCISRequest) jc.createUnmarshaller().unmarshal(sampleMessageIS);
    semanticAdapter.acceptSource(request);

}

From source file:com.amazonaws.sqs.util.SQSClient.java

public SQSClient(String endpoint) {
    this.endpoint = endpoint;
    this.queryParams = new HashMap<String, String>();
    try {/*from  w  w w  .  j a va  2  s .c o  m*/
        this.jaxbContext = JAXBContext.newInstance("com.amazonaws.sqs.generated");
    } catch (JAXBException e) {
        throw new RuntimeException("Unexpected exception while creating JAXBContext", e);
    }
}

From source file:it.polimi.modaclouds.qos_models.util.XMLHelper.java

@SuppressWarnings("unchecked")
public static <T> T deserialize(FileInputStream xmlPath, Class<T> targetClass)
        throws JAXBException, SAXException {
    // SchemaFactory schemaFactory =
    // SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    // Schema schema = schemaFactory.newSchema();
    Unmarshaller unmarshaller = JAXBContext.newInstance(targetClass).createUnmarshaller();
    // unmarshaller.setSchema(schema);
    T object = (T) unmarshaller.unmarshal(xmlPath);
    return object;
}

From source file:org.mule.module.apikit.leagues.Leagues.java

@Transformer(resultMimeType = "text/xml")
public String toXml(Leagues leagues) throws IOException, JAXBException {
    JAXBContext context = JAXBContext.newInstance(getClass());

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    m.marshal(leagues, boas);//from w  w  w.j ava2 s  .c o  m

    return new String(boas.toByteArray());
}

From source file:org.openregistry.core.repository.xml.XmlBasedDisclosureRecalculationStrategyRepositoryImpl.java

public XmlBasedDisclosureRecalculationStrategyRepositoryImpl(final Resource resource)
        throws JAXBException, IOException {

    File disclosureCalculationStrategyFile = resource.getFile();
    JAXBContext jaxbContext = JAXBContext.newInstance(XmlBasedDisclosureRecalculationStrategyImpl.class);
    Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();

    logger.info("Attempting to load Xml Disclosure recalculation strategy spec from ["
            + disclosureCalculationStrategyFile.getAbsolutePath() + "]");

    Assert.isTrue(disclosureCalculationStrategyFile.isFile() && disclosureCalculationStrategyFile.canRead()
            && disclosureCalculationStrategyFile.getName().endsWith(".xml"));

    final FileReader fileReader = new FileReader(disclosureCalculationStrategyFile);
    disclosureRecalcualationStrategy = (DisclosureRecalculationStrategy) unMarshaller.unmarshal(fileReader);
    logger.info("Loaded Xml Disclosure recalculation strategy spec with name ["
            + disclosureRecalcualationStrategy.getName() + "] description ["
            + disclosureRecalcualationStrategy.getDescription() + "]");
}

From source file:eu.impress.impressplatform.IntegrationLayer.ResourcesMgmt.BedAvailabilityServiceBean.java

@Override
public String getBedAvailablityHAVE(String hospitalname) {

    String hospitalstatushave;/*from   ww w.  j  a va 2 s  .c  o  m*/
    BedStats bedStats = bedService.getHospitalAvailableBeds(hospitalname);
    HospitalStatus hospitalStatus = beansTransformation.BedStatstoHAVE(bedStats);

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(HospitalStatus.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        //jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter sw = new StringWriter();
        //marshal the envelope 
        jaxbMarshaller.marshal(hospitalStatus, sw);
        hospitalstatushave = sw.toString();

    } catch (JAXBException e) {
        e.printStackTrace();
        return "Error Marshalling XML Object" + HttpStatus.INTERNAL_SERVER_ERROR;
    }

    return hospitalstatushave;

}

From source file:org.hisp.dhis.dxf2.importsummary.ImportSummaryTest.java

@Test
public void unMarshallImportSummary() throws Exception {
    ClassPathResource resource = new ClassPathResource("importSummary.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(ImportSummary.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    ImportSummary importSummary = (ImportSummary) jaxbUnmarshaller.unmarshal(resource.getInputStream());
    assertEquals(3, importSummary.getDataValueCount().getImported());
    assertEquals(0, importSummary.getDataValueCount().getUpdated());
    assertEquals(1, importSummary.getDataValueCount().getIgnored());
}

From source file:Main.java

public static synchronized Object deserialize(Source source, InputStream xsltSource, Class cls)
        throws TransformerConfigurationException, JAXBException, TransformerException {
    Object obj = null;/*from w w  w. java  2 s .c  om*/
    JAXBContext jc = JAXBContext.newInstance(cls);

    if (xsltSource != null) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = factory.newTransformer(new StreamSource(xsltSource));

        JAXBResult result = new JAXBResult(jc);
        transformer.transform(source, result);
        obj = result.getResult();
    } else {
        obj = jc.createUnmarshaller().unmarshal(source);
    }
    return obj;
}