Example usage for javax.xml.bind Marshaller JAXB_ENCODING

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

Introduction

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

Prototype

String JAXB_ENCODING

To view the source code for javax.xml.bind Marshaller JAXB_ENCODING.

Click Source Link

Document

The name of the property used to specify the output encoding in the marshalled XML data.

Usage

From source file:br.gov.frameworkdemoiselle.behave.integration.alm.objects.util.GenerateXMLString.java

public static String getTestplanString(String urlServer, String projectAreaAlias, String encoding,
        String testCaseId, Testplan currentPlan) throws JAXBException {

    // Adiciona o novo test case se no existir
    boolean exists = false;
    String newTestCaseId = urlServer + "resources/" + projectAreaAlias + "/testcase/" + testCaseId;

    if (currentPlan.getTestcase() != null) {
        for (com.ibm.rqm.xml.bind.Testplan.Testcase link : currentPlan.getTestcase()) {
            if (link.getHref().equals(newTestCaseId)) {
                exists = true;//w  ww .  j  av  a  2 s  .  c om
                break;
            }
        }
    }

    if (!exists) {
        com.ibm.rqm.xml.bind.Testplan.Testcase testcase = new com.ibm.rqm.xml.bind.Testplan.Testcase();
        testcase.setHref(newTestCaseId);
        currentPlan.getTestcase().add(testcase);
    }

    JAXBContext jaxb = JAXBContext.newInstance(Testplan.class);
    Marshaller marshaller = jaxb.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
    StringWriter testPlanString = new StringWriter();
    marshaller.marshal(currentPlan, testPlanString);

    return testPlanString.toString();
}

From source file:com.framework.infrastructure.mapper.JaxbMapper.java

/**
 * Marshallerencoding(null)./* w  ww  .  j  a  v  a2  s . com*/
 * pooling
 */
public Marshaller createMarshaller(String encoding) {
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        if (StringUtils.isNotBlank(encoding)) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        }

        return marshaller;
    } catch (JAXBException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:com.govsoft.framework.common.util.encode.JaxbBinder.java

/**
 * Marshallerencoding(?Null).// w w w  .j a  va  2 s .  c  o m
 */
public Marshaller createMarshaller(String encoding) {
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        if (StringUtils.isNotBlank(encoding)) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        }

        return marshaller;
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:cn.hxh.springside.mapper.JaxbMapper.java

/**
 * Marshallerencoding(?null)./*from  w ww.ja v  a2 s. c  o  m*/
 */
public Marshaller createMarshaller(String encoding) {
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        if (StringUtils.isNotBlank(encoding)) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        }

        return marshaller;
    } catch (JAXBException e) {
        throw ExceptionUtils.unchecked(e);
    }
}

From source file:gov.nih.nci.cabig.caaers.api.BlankFormGenerator.java

public String serialize(Study study, Epoch epoch) throws Exception {
    jaxbContext = JAXBContext.newInstance("gov.nih.nci.cabig.caaers.integration.schema.study");
    marshaller = jaxbContext.createMarshaller();
    StringWriter sw = new StringWriter();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    studies = new Studies();
    List<gov.nih.nci.cabig.caaers.integration.schema.study.Study> list = new ArrayList();
    gov.nih.nci.cabig.caaers.integration.schema.study.Study wsStudy = new gov.nih.nci.cabig.caaers.integration.schema.study.Study();

    wsStudy.setShortTitle(study.getShortTitle());
    wsStudy.setLongTitle(study.getLongTitle());
    wsStudy.setId(BigInteger.valueOf(study.getId()));

    gov.nih.nci.cabig.caaers.integration.schema.study.Study.Identifiers studyIdentifiers = new gov.nih.nci.cabig.caaers.integration.schema.study.Study.Identifiers();
    SystemAssignedIdentifierType i = new SystemAssignedIdentifierType();
    i.setPrimaryIndicator(true);//w w w.  jav  a2 s. c o  m
    i.setValue(study.getPrimaryIdentifierValue());
    List<SystemAssignedIdentifierType> iSystemAssigned = new ArrayList<SystemAssignedIdentifierType>();
    iSystemAssigned.add(i);
    studyIdentifiers.setSystemAssignedIdentifier(iSystemAssigned);
    wsStudy.setIdentifiers(studyIdentifiers);

    EvaluationPeriodType ept = new EvaluationPeriodType();
    EvaluationPeriodType.SolicitedAdverseEvents wsSAE = new EvaluationPeriodType.SolicitedAdverseEvents();
    List<SolicitedAdverseEventType> wsSAET = new ArrayList<SolicitedAdverseEventType>();
    if (epoch != null) {
        ept.setName(epoch.getName());
        ept.setDescriptionText(epoch.getDescriptionText());
        ept.setSolicitedAdverseEvents(new EvaluationPeriodType.SolicitedAdverseEvents());

        for (SolicitedAdverseEvent domainSAE : epoch.getArms().get(0).getSolicitedAdverseEvents()) {
            SolicitedAdverseEventType saet = new SolicitedAdverseEventType();
            if (domainSAE.getCtcterm() != null)
                saet.setName(domainSAE.getCtcterm().getTerm());
            if (domainSAE.getLowLevelTerm() != null)
                saet.setName(domainSAE.getLowLevelTerm().getMeddraTerm());
            wsSAET.add(saet);
        }
    }
    wsSAE.setSolicitedAdverseEvent(wsSAET);
    ept.setSolicitedAdverseEvents(wsSAE);

    gov.nih.nci.cabig.caaers.integration.schema.study.Study.EvaluationPeriods eps = new gov.nih.nci.cabig.caaers.integration.schema.study.Study.EvaluationPeriods();
    List<EvaluationPeriodType> eptl = new ArrayList<EvaluationPeriodType>();
    eptl.add(ept);
    eps.setEvaluationPeriod(eptl);

    wsStudy.setEvaluationPeriods(eps);

    list.add(wsStudy);
    studies.setStudy(list);
    marshaller.marshal(studies, sw);
    //        marshaller.marshal(studies, new FileOutputStream(XMLFile));
    return sw.toString();
}

From source file:com.docdoku.server.rest.InstanceMessageBodyWriter.java

@Override
public void writeTo(InstanceCollection object, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    try {/*from  w w  w  .j ava 2  s. co m*/
        //Class<?> domainClass = getDomainClass(genericType);
        setEntityStream(entityStream);
        setMarshaller(((JSONJAXBContext) getJAXBContext(InstanceDTO.class, mediaType)).createJSONMarshaller());
        Map<String, String> mediaTypeParameters = mediaType.getParameters();
        String charSet = "UTF-8";
        if (mediaTypeParameters.containsKey(CHARSET)) {
            charSet = mediaTypeParameters.get(CHARSET);
            getMarshaller().setProperty(Marshaller.JAXB_ENCODING, charSet);
        }

        PartUsageLink rootUsageLink = object.getRootUsageLink();
        List<Integer> usageLinkPaths = object.getUsageLinkPaths();
        byte[] leftSquareBrace = "[".getBytes(charSet);
        byte[] rightSquareBrace = "]".getBytes(charSet);
        setComma(",".getBytes(charSet));

        setAddComma(false);
        getEntityStream().write(leftSquareBrace);
        generateInstanceStream(rootUsageLink, 0, 0, 0, 0, 0, 0, usageLinkPaths, new ArrayList<Integer>());
        getEntityStream().write(rightSquareBrace);
    } catch (JAXBException ex) {
        throw new WebApplicationException(ex);
    } finally {
        tlEntityStream.remove();
        tlMarshaller.remove();
        tlAddComma.remove();
        tlComma.remove();
    }

}

From source file:jails.http.converter.xml.Jaxb2RootElementHttpMessageConverter.java

private void setCharset(MediaType contentType, Marshaller marshaller) throws PropertyException {
    if (contentType != null && contentType.getCharSet() != null) {
        marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharSet().name());
    }//from w  w w.  j  a v a  2  s.  c  om
}

From source file:eu.openminted.toolkit.elsevier.retriever.JaxbMarshalingTest.java

@Test
public void shouldSerialise() {
    Marshaller marshaller = null;
    try {// w w w  .  j  av a  2  s . c o m
        marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        //            marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

        Writer writer = new StringWriter();
        marshaller.marshal(testObject, writer);

        String serialised = writer.toString();
        //            serialised.replaceAll("<\\?xml version=\"1.0\" encoding=\"UTF\\-8\" standalone=\"yes\"\\?>", "");
        String testXmlAsString = FileUtils.readFileToString(new File("src/test/resources/S0140673616313228"),
                "UTF-8");

        System.out.println("serialised test object :\n" + serialised);
        System.out.println("test xml :\n" + testXmlAsString);

        //            Assert.assertEquals(asString, testXmlAsString);
    } catch (PropertyException ex) {
        ex.printStackTrace();
    } catch (JAXBException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

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

public void testJaxbFromContext() {
    System.out.println("start of test:testJaxbFromContext()");
    try {/*  ww  w .ja v  a  2  s  . 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());

        // 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()");
}

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

public void testMarshalData() {
    System.out.println("start of test:testMarshalData()");

    // check slf4j settings
    LOG.debug("debug message");
    LOG.warn("warn message");
    LOG.info("info message");

    try {/*from  w ww.  j av  a  2s  .  c  om*/
        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.calculator");

        // *****************
        // create calculator
        // *****************
        PmatrixDpdCalculatorSimpleMovingAvgImpl simpleMovingAvgCalc = new PmatrixDpdCalculatorSimpleMovingAvgImpl();

        simpleMovingAvgCalc.setLatestDataValue(1010d);
        simpleMovingAvgCalc.setLatestTimestamp(new Date().getTime());
        simpleMovingAvgCalc.setPrevDataValue(3040d);
        simpleMovingAvgCalc.setPreviousTimestamp(new Date().getTime());

        NameValuePair property = new NameValuePair(
                PmatrixDpdCalculatorSimpleMovingAvgImpl.MAX_SAMPLE_NO_PROPERTY_NAME, "10");
        simpleMovingAvgCalc.getConfiguration().add(property);

        for (int i = 1; i < 15; i++) {
            Double latestDataValue = Double.valueOf(i);
            Long latestTimestamp = new Date().getTime() + i; // adding to show small increment in time
            simpleMovingAvgCalc.updateCalculation(latestDataValue, latestTimestamp);
        }

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

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

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

        jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
                "http://xmlns.opennms.org/xsd/config/pmatrix pmatrixConfig.xsd");

        jaxbMarshaller.marshal(simpleMovingAvgCalc, 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 PmatrixDpdCalculatorSimpleMovingAvgImpl) {
            System.out.println("unmarshalled list:");
            System.out.println((PmatrixDpdCalculatorSimpleMovingAvgImpl) o);
        } else
            System.out.println("cant unmarshal object:");

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

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

}