Example usage for javax.xml.bind.util JAXBSource JAXBSource

List of usage examples for javax.xml.bind.util JAXBSource JAXBSource

Introduction

In this page you can find the example usage for javax.xml.bind.util JAXBSource JAXBSource.

Prototype

public JAXBSource(Marshaller marshaller, Object contentObject) throws JAXBException 

Source Link

Document

Creates a new javax.xml.transform.Source for the given content object.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("customer.xsd"));

    JAXBContext jc = JAXBContext.newInstance(Customer.class);

    Customer customer = new Customer();
    // populate the customer object
    JAXBSource source = new JAXBSource(jc, customer);
    schema.newValidator().validate(source);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Customer customer = new Customer();
    customer.setName("abcabcabcabcabcabcabc");
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());

    JAXBContext jc = JAXBContext.newInstance(Customer.class);
    JAXBSource source = new JAXBSource(jc, customer);

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("customer.xsd"));

    Validator validator = schema.newValidator();
    validator.setErrorHandler(new MyErrorHandler());
    validator.validate(source);/*from   w  w  w  .ja v a2 s  .c  o  m*/
}

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

/**
 * JAXB object translation methods      
 *///from   w ww  . j  av  a 2  s  .c o m

public static Object transformXmlObj2XmlObj(Class sourceClass, Class destClass, Object source) {
    try {
        JAXBContext sourceContext = JAXBContext.newInstance(sourceClass);
        JAXBSource jaxbSource = new JAXBSource(sourceContext, source);
        JAXBContext destContext = JAXBContext.newInstance(destClass);
        Unmarshaller unmarshaller = destContext.createUnmarshaller();
        return unmarshaller.unmarshal(jaxbSource);
    } catch (JAXBException ex) {
        System.err.println(ex.getMessage());
        return null;
    }
}

From source file:com.healthcit.cacure.export.ExportToExcel.java

@Test
public void export() throws JAXBException, TransformerException {
    JAXBContext jc = JAXBContext.newInstance("com.healthcit.cacure.export.model");
    File iFile = new File("C:\\temp\\moduleTest1.xml");
    //File iFile = new File("C:\\temp\\formExportTest.xml");
    //File iFile = new File("C:\\temp\\section1.1.xml");
    //File iFile = new File("C:\\temp\\complexSkip2.xml");
    //File iFile = new File("C:\\temp\\section3.1.xml");
    File oFile = new File("C:\\temp\\Book2.xml");
    Unmarshaller m = jc.createUnmarshaller();
    Cure xml = (Cure) m.unmarshal(iFile);
    StreamSource xslSource = new StreamSource("src//main//resources//xls.xsl");
    //long formId = 9979;

    //Cure xml = dataExporter.constructFormXML(formId);
    JAXBSource xmlSource = new JAXBSource(jc, xml);
    Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
    transformer.transform(xmlSource, new StreamResult(oFile));
}

From source file:no.digipost.api.xml.Marshalling.java

public static void marshal(final Jaxb2Marshaller jaxb2Marshaller, final Object element,
        final Result payloadResult) {
    try {/* w  w w .java 2 s  .  com*/
        JAXBSource jaxbSource = new JAXBSource(jaxb2Marshaller.getJaxbContext().createMarshaller(), element);
        TransformerUtil.transform(jaxbSource, payloadResult);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.healthcit.cacure.web.controller.ModuleImportExportController.java

@RequestMapping(method = RequestMethod.GET)
public void exportModule(@RequestParam(value = "moduleId", required = true) long id,
        @RequestParam(value = "format", required = true) String format, HttpServletResponse response) {

    try {//from w  w  w.  j  a va  2 s  .c  om
        response.setContentType("text/xml");

        OutputStream oStream = response.getOutputStream();
        Cure cureXml = dataExporter.constructModuleXML(id);
        JAXBContext jc = JAXBContext.newInstance("com.healthcit.cacure.export.model");
        if (ExportFormat.XML.name().equals(format)) {
            String fileNameHeader = String.format("attachment; filename=form-%d.xml;", id);

            response.setHeader("Content-Disposition", fileNameHeader);
            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(cureXml, oStream);
        } else if (ExportFormat.EXCEL.name().equals(format)) {
            String fileNameHeader = String.format("attachment; filename=form-%d.xlxml;", id);

            response.setHeader("Content-Disposition", fileNameHeader);
            response.setContentType("application/xml");
            StreamSource xslSource = new StreamSource(this.getClass().getClassLoader()
                    .getResourceAsStream(AppConfig.getString(Constants.EXPORT_EXCEL_XSLT_FILE)));
            JAXBSource xmlSource = new JAXBSource(jc, cureXml);
            Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
            transformer.transform(xmlSource, new StreamResult(oStream));
        }
        oStream.flush();
    } catch (IOException e) {
        log.error("Unable to obtain output stream from the response");
        log.error(e.getMessage(), e);
    } catch (JAXBException e) {
        log.error("Unable to marshal the object");
        log.error(e.getMessage(), e);
    } catch (TransformerException e) {
        log.error("XSLT transformation failed");
        log.error(e.getMessage(), e);
    }
}

From source file:mx.bigdata.sat.cfdi.TFDv1.java

public void validar(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(getClass().getResource(XSD));
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }// www. ja v  a  2  s . c o  m
    validator.validate(new JAXBSource(CONTEXT, tfd));
}

From source file:com.healthcit.cacure.web.controller.FormExportController.java

@RequestMapping(method = RequestMethod.GET)
public void exportForm(@RequestParam(value = "id", required = true) Long formId,
        @RequestParam(value = "format", required = true) String format, HttpServletResponse response) {

    try {// w  w w . j a  v a 2 s  .c om
        OutputStream oStream = response.getOutputStream();
        Cure cureXml = dataExporter.constructFormXML(formId);
        JAXBContext jc = JAXBContext.newInstance("com.healthcit.cacure.export.model");

        if (ExportFormat.XML.name().endsWith(format)) {
            String fileNameHeader = String.format("attachment; filename=form-%d.xml;", formId);
            response.setHeader("Content-Disposition", fileNameHeader);
            response.setContentType("application/xml");

            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(cureXml, oStream);
            oStream.flush();
        } else if (ExportFormat.EXCEL.name().equals(format)) {
            String fileNameHeader = String.format("attachment; filename=form-%d.xlxml;", formId);
            response.setHeader("Content-Disposition", fileNameHeader);
            response.setContentType("application/xml");
            StreamSource xslSource = new StreamSource(this.getClass().getClassLoader()
                    .getResourceAsStream(AppConfig.getString(Constants.EXPORT_EXCEL_XSLT_FILE)));
            JAXBSource xmlSource = new JAXBSource(jc, cureXml);
            Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
            transformer.transform(xmlSource, new StreamResult(oStream));
        }
    } catch (IOException e) {
        log.error("Unable to obtain output stream from the response");
        log.error(e.getMessage(), e);
    } catch (JAXBException e) {
        log.error("Unable to marshal the object");
        log.error(e.getMessage(), e);
    } catch (TransformerException e) {
        log.error("XSLT transformation failed");
        log.error(e.getMessage(), e);
    }
}

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

public void validate(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(getClass().getResource(XSD));
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }/*  w w w. j a v a2s. c om*/
    validator.validate(new JAXBSource(CONTEXT, document));
}

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

public void validate(ErrorHandler handler) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(getClass().getResource(XSD));
    Validator validator = schema.newValidator();
    if (handler != null) {
        validator.setErrorHandler(handler);
    }//from   w  w  w. j  av a2 s.c  om
    validator.validate(new JAXBSource(CONTEXT, tfd));
}