Example usage for javax.xml.bind Marshaller setProperty

List of usage examples for javax.xml.bind Marshaller setProperty

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller setProperty.

Prototype

public void setProperty(String name, Object value) throws PropertyException;

Source Link

Document

Set the particular property in the underlying implementation of Marshaller .

Usage

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

public void guardar(OutputStream out) throws Exception {
    Marshaller m = CONTEXT.createMarshaller();
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl(CFDv3.PREFIXES));
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
            "http://www.sat.gob.mx/cfd/3  " + "http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv3.xsd");
    m.marshal(document.getComprobante(), out);
}

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

public void guardar(OutputStream out) throws Exception {
    Marshaller m = CONTEXT.createMarshaller();
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl(CFDv32.PREFIXES));
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
            "http://www.sat.gob.mx/cfd/3  " + "http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd");
    m.marshal(document.getComprobante(), out);
}

From source file:tools.xor.logic.DefaultStoredProcedure.java

private void outputSP(AggregateView view) throws JAXBException, UnsupportedEncodingException {
    javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(AggregateView.class);
    javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
    marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
    marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(view, System.out);

    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    marshaller.marshal(view, bOut);// w  w  w.  j av  a2 s  .  c o m
    bOut.toString("UTF-8");
}

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

private Element marshalTFD() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*w ww. j  ava2  s .c  om*/
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    Marshaller m = CONTEXT.createMarshaller();
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl(CFDv3.PREFIXES));
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
            "http://www.sat.gob.mx/TimbreFiscalDigital TimbreFiscalDigital.xsd");
    m.marshal(tfd, doc);
    return doc.getDocumentElement();
}

From source file:gov.nih.nci.cabig.caaers.web.study.ExportStudyController.java

@Override
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object o,
        BindException e) throws Exception {

    Integer studyID = Integer.valueOf(request.getParameter("id"));
    Study study = studyDao.getById(studyID);
    studyDao.initialize(study);/*from w ww.  j  a  v  a 2  s. co m*/

    // START study export pre-population
    study.setCoordinatingCenter(new CoordinatingCenter());
    study.getCoordinatingCenter().setStudyCoordinatingCenter(study.getStudyCoordinatingCenter());

    study.setFundingSponsor(new FundingSponsor());
    study.getFundingSponsor().setStudyFundingSponsor(study.getStudyFundingSponsors().get(0));

    for (OrganizationAssignedIdentifier id : study.getOrganizationAssignedIdentifiers()) {
        if (id.getOrganization().equals(study.getFundingSponsor().getStudyFundingSponsor().getOrganization())) {
            study.getFundingSponsor().setOrganizationAssignedIdentifier(id);
            study.getFundingSponsor().getOrganizationAssignedIdentifier().setPrimaryIndicator(true);
            break;
        }
    }
    for (OrganizationAssignedIdentifier id : study.getOrganizationAssignedIdentifiers()) {
        if (id.getOrganization()
                .equals(study.getCoordinatingCenter().getStudyCoordinatingCenter().getOrganization())) {
            study.getCoordinatingCenter().setOrganizationAssignedIdentifier(id);
            study.getCoordinatingCenter().getOrganizationAssignedIdentifier().setPrimaryIndicator(false);
            break;
        }
    }
    // END study export pre-population

    gov.nih.nci.cabig.caaers.integration.schema.study.Studies studies = converter
            .convertStudyDomainToStudyDto(study);

    //Marshall the Data Transfer Object according to Study.xsd schema,
    //and download it to the client machine.
    try {
        String tempDir = System.getProperty("java.io.tmpdir");
        String fileName = "ExportedStudy_" + study.getPrimaryIdentifierValue();
        fileName = RuleUtil.getStringWithoutSpaces(fileName);

        StringWriter sw = new StringWriter();
        JAXBContext jaxbContext = JAXBContext.newInstance("gov.nih.nci.cabig.caaers.integration.schema.study");

        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(studies, sw);
        BufferedWriter out = new BufferedWriter(new FileWriter(tempDir + fileName + ".xml"));
        out.write(sw.toString());
        out.flush();
        out.close();

        response.setContentType("application/xml");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xml");
        response.setHeader("Content-length", String.valueOf(sw.toString().length()));
        response.setHeader("Pragma", "private");
        response.setHeader("Cache-control", "private, must-revalidate");

        OutputStream outputStream = response.getOutputStream();
        File file = new File(tempDir + fileName + ".xml");
        FileInputStream fileIn = new FileInputStream(file);
        byte[] buffer = new byte[2048];
        int bytesRead = fileIn.read(buffer);
        while (bytesRead >= 0) {
            if (bytesRead > 0)
                outputStream.write(buffer, 0, bytesRead);
            bytesRead = fileIn.read(buffer);
        }
        outputStream.flush();
        outputStream.close();
        fileIn.close();

    } catch (Exception ex) {
        log.error(ex);
        ex.printStackTrace();
    }
    return null;
}

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

public void guardar(OutputStream out) throws Exception {
    Marshaller m = CONTEXT.createMarshaller();
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl(CFDv3.PREFIXES));
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
            "http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd");
    m.marshal(document.getComprobante(), out);
}

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

public void guardar(OutputStream out) throws Exception {
    Marshaller m = CONTEXT.createMarshaller();
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl(CFDv32.PREFIXES));
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
            "http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd");
    m.marshal(document.getComprobante(), out);
}

From source file:com.wavemaker.tools.project.upgrade.swamis.ServiceBeanFileUpgrade.java

@Override
public void doUpgrade(Project project, UpgradeInfo upgradeInfo) {

    DesignServiceManager dsm = DesignTimeUtils.getDesignServiceManager(project);
    List<String> touchedProjects = new ArrayList<String>();

    for (Service service : dsm.getServices()) {
        if (service.getSpringFile() == null) {
            // create the service bean file
            File serviceBeanFile = dsm.getServiceBeanXmlFile(service.getId());
            if (!serviceBeanFile.exists()) {
                try {
                    DesignServiceManager.generateSpringServiceConfig(service.getId(), service.getClazz(),
                            dsm.getDesignServiceType(service.getType()), serviceBeanFile, project);
                } catch (JAXBException e) {
                    throw new WMRuntimeException(e);
                } catch (IOException e) {
                    throw new WMRuntimeException(e);
                }//from  w w w.  ja  v  a2  s . c o  m

            }

            // edit the servicedef
            File serviceDefFile = dsm.getServiceDefXmlFile(service.getId());
            service.setSpringFile(serviceBeanFile.getName());

            Marshaller marshaller;
            try {
                marshaller = definitionsContext.createMarshaller();
                marshaller.setProperty("jaxb.formatted.output", true);
                Writer writer = serviceDefFile.getContent().asWriter();
                try {
                    marshaller.marshal(service, writer);
                } finally {
                    writer.close();
                }
            } catch (Exception e) {
                throw new WMRuntimeException(e);
            }

            // finally, add this to the list of modified services
            touchedProjects.add(service.getId());
        }
    }

    if (!touchedProjects.isEmpty()) {
        upgradeInfo.addVerbose("Converted bean definitions to a separate file for services: "
                + StringUtils.join(touchedProjects, ", "));
    }
}

From source file:de.thorstenberger.examServer.dao.xml.AbstractJAXBDao.java

/**
 * Serializes the object into xml. Will wrap any exception into a {@link RuntimeException}.
 *
 * @param obj//from  w  w  w.  jav  a2s. c  om
 *            the object to save.
 * @throws RuntimeException
 *             wrapping {@link JAXBException} of {@link IOException}
 */
synchronized protected void save(final Object obj) {
    log.debug(String.format("Trying to save xml package to file '%s'", workingPath + "/" + xmlFileName));
    final String txId = startTransaction();
    Marshaller marshaller = null;
    try {

        marshaller = JAXBUtils.getJAXBMarshaller(jc);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
        final BufferedOutputStream bos = new BufferedOutputStream(
                getFRM().writeResource(txId, this.xmlFileName));
        marshaller.marshal(obj, bos);

        bos.close();
        commitTransaction(txId);
    } catch (final JAXBException e) {
        rollback(txId, e);
        throw new RuntimeException(e);
    } catch (final IOException e) {
        rollback(txId, e);
        throw new RuntimeException(e);
    } catch (final ResourceManagerException e) {
        rollback(txId, e);
        throw new RuntimeException(e);
    } catch (RuntimeException e) {
        rollback(txId, e);
        throw e;
    } finally {
        if (marshaller != null)
            JAXBUtils.releaseJAXBMarshaller(jc, marshaller);
    }
}

From source file:org.opennms.features.vaadin.pmatrix.manual.AppContextSpecificationMarshalTest.java

public void testJaxbFromContext() {
    System.out.println("start of test:testJaxbFromContext()");
    try {/*w w w . ja va 2 s .  c  o m*/
        String testFileName = this.getClass().getSimpleName() + "_File.xml";
        File file = new File("target/" + testFileName);
        PrintWriter writer = new PrintWriter(file, "UTF-8");
        writer.close();
        System.out.println("file location:" + file.getAbsolutePath());

        // see http://stackoverflow.com/questions/1043109/why-cant-jaxb-find-my-jaxb-index-when-running-inside-apache-felix
        // need to provide bundles class loader
        ClassLoader cl = org.opennms.features.vaadin.pmatrix.model.DataPointDefinition.class.getClassLoader();
        JAXBContext jaxbContext = JAXBContext.newInstance("org.opennms.features.vaadin.pmatrix.model", cl);

        //JAXBContext jaxbContext = JAXBContext.newInstance("org.opennms.features.vaadin.pmatrix.model");

        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        PmatrixSpecificationList pmatrixSpecificationList_context = (PmatrixSpecificationList) appContext
                .getBean("pmatrixSpecificationList");
        //PmatrixSpecification pmatrixSpec_Context = (PmatrixSpecification) appContext.getBean("pmatrixSpecification");

        //System.out.println("list to be marshalled:");
        System.out.println(pmatrixSpecificationList_context);

        System.out.println("marshalled list:");
        //jaxbMarshaller.marshal(testDatalist, file);

        //jaxbMarshaller.marshal(pmatrixSpec, System.out); // works
        //jaxbMarshaller.marshal(pmatrixSpecificationList, System.out); //works

        //test of marshaling context data
        //jaxbMarshaller.marshal(pmatrixSpecificationList_context, System.out);
        jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
                "http://xmlns.opennms.org/xsd/config/pmatrix pmatrixConfig.xsd");

        jaxbMarshaller.marshal(pmatrixSpecificationList_context, file);
        //jaxbMarshaller.marshal(pmatrixSpecificationList_context, file);

        //unmarshal test file

        Unmarshaller jaxbUnMarshaller = jaxbContext.createUnmarshaller();

        //Object o = jaxbUnMarshaller.unmarshal( new StringReader( marshalledXml )  );

        Object o = jaxbUnMarshaller.unmarshal(file);

        System.out.println("o.tostring:" + o.toString());
        if (o instanceof PmatrixSpecificationList) {
            System.out.println("unmarshalled list:");
            System.out.println((PmatrixSpecificationList) o);

        } else
            System.out.println("cant unmarshal object:");

    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("end of test:testAppContext()");
}