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:org.kemri.wellcome.dhisreport.api.model.HttpDhis2Server.java

@Override
public ImportSummary postReport(DataValueSet report) throws DHIS2ReportingException {
    log.debug("Posting datavalueset report");
    ImportSummary summary = null;//from ww w. j a v a 2  s  . c o  m

    StringWriter xmlReport = new StringWriter();
    try {
        JAXBContext jaxbDataValueSetContext = JAXBContext.newInstance(DataValueSet.class);
        Marshaller dataValueSetMarshaller = jaxbDataValueSetContext.createMarshaller();
        // output pretty printed
        dataValueSetMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        dataValueSetMarshaller.marshal(report, xmlReport);
    } catch (JAXBException ex) {
        log.error(ex.getMessage());
        throw new Dxf2Exception("Problem marshalling dataValueSet", ex);
    }

    String host = getUrl().getHost();
    int port = getUrl().getPort();

    HttpHost targetHost = new HttpHost(host, port, getUrl().getProtocol());
    DefaultHttpClient httpclient = new DefaultHttpClient();
    BasicHttpContext localcontext = new BasicHttpContext();

    try {
        String postUrl = getUrl().toString() + DATAVALUESET_PATH;
        log.error("Post URL: " + postUrl);
        HttpPost httpPost = new HttpPost(postUrl);
        Credentials creds = new UsernamePasswordCredentials(username, password);
        Header bs = new BasicScheme().authenticate(creds, httpPost, localcontext);
        httpPost.addHeader("Authorization", bs.getValue());
        httpPost.addHeader("Content-Type", "application/xml; charset=utf-8");
        httpPost.addHeader("Accept", "application/xml");

        httpPost.setEntity(new StringEntity(xmlReport.toString()));
        HttpResponse response = httpclient.execute(targetHost, httpPost, localcontext);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            JAXBContext jaxbImportSummaryContext = JAXBContext.newInstance(ImportSummary.class);
            Unmarshaller importSummaryUnMarshaller = jaxbImportSummaryContext.createUnmarshaller();
            summary = (ImportSummary) importSummaryUnMarshaller.unmarshal(entity.getContent());
        }
    } catch (Exception ex) {
        log.error(ex.getMessage());
        throw new Dhis2Exception(this, "Problem accessing Dhis2 server", ex);
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
    return summary;
}

From source file:edu.duke.cabig.c3pr.domain.scheduler.runtime.job.CCTSNotificationMessageJob.java

private String serialize(Notification notification) {
    try {/*from  ww w  .j  a  v a  2  s .c om*/
        JAXBContext context = JAXBContext.newInstance(Notification.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        JAXBElement<Notification> jaxbElement = new JAXBElement<Notification>(
                new QName(CCTS_NOTIFICATIONS_NS, "notification"), Notification.class, notification);
        CharArrayWriter writer = new CharArrayWriter();
        m.marshal(jaxbElement, writer);
        return String.valueOf(writer.toCharArray());
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

}

From source file:net.ageto.gyrex.impex.persistence.cassandra.storage.CassandraRepositoryImpl.java

/**
 * Persist process configuration./*from  w w  w .j a  v a  2  s. c o m*/
 * 
 * @param process
 */
public void insertOrUpdateProcess(IProcessConfig process) {

    JAXBContext context;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    try {
        context = JAXBContext.newInstance(ProcessConfig.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        // process / process step configuration xml formatted
        m.marshal(process, outputStream);

    } catch (JAXBException e) {
        throw new IllegalArgumentException(e.getMessage());
    }

    Mutator<String> mutator = HFactory.createMutator(getImpexKeyspace(), StringSerializer.get());
    // insert process configuration
    mutator.insert(process.getId(), columnFamilyProcesses,
            HFactory.createStringColumn(columnNameProcess, outputStream.toString()));
}

From source file:alter.vitro.vgw.service.query.wrappers.ResponseAggrMsg.java

public String toString() {
    String retStr = "";
    try {//from  w  ww  .j  a v a  2 s.  com
        javax.xml.bind.JAXBContext jaxbContext = javax.xml.bind.JAXBContext
                .newInstance("alter.vitro.vgw.service.query.xmlmessages.response");
        ObjectFactory theFactory = new ObjectFactory();
        javax.xml.bind.Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        JAXBElement<QueryResponseType> myAggrResponseMsgEl = theFactory.createQueryResponse(response);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        marshaller.marshal(myAggrResponseMsgEl, baos);
        //            marshaller.marshal(myAggrResponseMsgEl, new java.io.FileOutputStream("ResponseTest.xml"));
        retStr = baos.toString(HTTP.UTF_8);

    } catch (javax.xml.bind.JAXBException je) {
        je.printStackTrace();
    } catch (UnsupportedEncodingException e2) {
        e2.printStackTrace();
    }
    return retStr;
}

From source file:io.mapzone.arena.csw.CswRequest.java

protected void writeObject(JAXBElement jaxb) throws Exception {
    Marshaller marshaller = jaxbContext.get().createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    marshaller.marshal(jaxb, out());//w w  w  . ja v a2s.  c  o m
}

From source file:com.u2apple.tool.dao.DeviceXmlDaoJaxbImpl.java

private void flushDevices() throws JAXBException, PropertyException {
    if (isDeviceChanged) {
        File file = new File(Configuration.getProperty(Constants.DEVICES_XML));
        JAXBContext jaxbContext = JAXBContext.newInstance(StaticMapFile.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(getStaticMapFile(), file);
        isDeviceChanged = false;/* w  ww. ja v  a2  s. co m*/
    }
}

From source file:com.u2apple.tool.dao.DeviceXmlDaoJaxbImpl.java

private void flushVids() throws JAXBException, PropertyException {
    String vidFileFormat = Configuration.getProperty(Constants.VID_FILE_FORMAT);
    for (String vid : changedVids) {
        File file = new File(String.format(vidFileFormat, vid));
        JAXBContext jaxbContext = JAXBContext.newInstance(VID.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        VID v = getVid(getStaticMapFile().getVids(), vid);
        jaxbMarshaller.marshal(v, file);
    }/*from   w w w  .  j a va2  s. c o  m*/
    changedVids.clear();
}

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

public void testJaxbManual() {
    System.out.println("start of test:testJaxb()");
    try {//from  w  w w .  java2s  .  co 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());

        JAXBContext jaxbContext = JAXBContext.newInstance("org.opennms.features.vaadin.pmatrix.model");
        // TODO these classes are listed in jaxb.index file
        //         JAXBContext jaxbContext = JAXBContext.newInstance(               
        //               PmatrixSpecificationImpl.class,
        //               DataPointDefinitionImpl.class,
        //               PmatrixSpecificationListImpl.class,
        //               NameValuePair.class,
        //               PmatrixDpdCalculatorConfigImpl.class);

        // **********************
        // marshal test file
        // **********************

        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");

        // manual create process
        PmatrixSpecificationImpl newspec1 = createNewSpecification();
        newspec1.setPmatrixName("spec1");
        PmatrixSpecificationImpl newspec2 = createNewSpecification();
        newspec2.setPmatrixName("spec2");

        //create new specfication list
        PmatrixSpecificationListImpl pmatrixSpecificationList = new PmatrixSpecificationListImpl();
        pmatrixSpecificationList.setRefreshRate(1000);

        //create and add new DpdCalculator specification
        PmatrixDpdCalculatorConfig pmatrixDpdCalculatorConfig = new PmatrixDpdCalculatorConfigImpl();
        pmatrixDpdCalculatorConfig
                .setPmatrixDpdCalculatorClassName(PmatrixDpdCalculatorEmaImpl.class.getName());

        List<NameValuePair> configuration = new ArrayList<NameValuePair>();
        configuration.add(new NameValuePair("file", "fred.txt"));
        pmatrixDpdCalculatorConfig.setConfiguration(configuration);

        pmatrixSpecificationList.setPmatrixDpdCalculatorConfig(pmatrixDpdCalculatorConfig);

        //add specifications of each matrix
        List<PmatrixSpecification> pmsl = new ArrayList<PmatrixSpecification>();

        pmsl.add(newspec1);
        pmsl.add(newspec2);
        pmatrixSpecificationList.setPmatrixSpecificationList(pmsl);

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

        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, 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:testJaxb()");
}

From source file:esg.common.security.PolicyGleaner.java

public synchronized boolean savePolicyAs(Policies policy, String policyFileLocation) {
    boolean success = false;
    if (policy == null) {
        log.error("Sorry internal policy representation is null ? [" + policy
                + "] perhaps you need to load policy file first?");
        return success;
    }/* ww w .jav  a2 s. c om*/
    log.info("Saving policy information to " + policyFileLocation);
    try {
        JAXBContext jc = JAXBContext.newInstance(Policies.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(policy, new FileOutputStream(policyFileLocation));
        success = true;
    } catch (Exception e) {
        log.error(e);
    }
    return success;
}

From source file:hydrograph.ui.engine.util.ConverterUtil.java

private void validateJobState(Graph graph, boolean validate, IFileStore externalOutputFile,
        ByteArrayOutputStream out) throws CoreException, JAXBException, IOException {
    JAXBContext jaxbContext = JAXBContext.newInstance(graph.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    out = new ByteArrayOutputStream();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(graph, out);// w  ww  .ja  v  a  2s.c  o  m
    out = ComponentXpath.INSTANCE.addParameters(out);
}