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:com.cloudera.api.model.ApiModelTest.java

static String objectToXml(Object object) throws JAXBException, UnsupportedEncodingException {
    JAXBContext jc = JAXBContext.newInstance(object.getClass());
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    m.marshal(object, baos);//from  w  w w. ja v a 2 s  . c  o m
    return baos.toString(TEXT_ENCODING);
}

From source file:fr.cls.atoll.motu.processor.wps.TestServiceMetadata.java

public static void testLoadOGCServiceMetadata() {
    // String xmlFile =
    // "J:/dev/atoll-v2/atoll-motu/atoll-motu-processor/src/test/resources/xml/TestServiceMetadata.xml";
    String xmlFile = "C:/Documents and Settings/dearith/Mes documents/Atoll/SchemaIso/TestServiceMetadataOK.xml";

    String schemaPath = "schema/iso19139";

    try {//w  w w. j  a  v a  2s  .  c o  m
        List<String> errors = validateServiceMetadataFromString(xmlFile, schemaPath);
        if (errors.size() > 0) {
            StringBuffer stringBuffer = new StringBuffer();
            for (String str : errors) {
                stringBuffer.append(str);
                stringBuffer.append("\n");
            }
            throw new MotuException(String.format("ERROR - XML file '%s' is not valid - See errors below:\n%s",
                    xmlFile, stringBuffer.toString()));
        } else {
            System.out.println(String.format("XML file '%s' is valid", xmlFile));
        }

    } catch (MotuException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    InputStream in = null;
    try {
        in = Organizer.getUriAsInputStream(xmlFile);
    } catch (MotuException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    JAXBContext jc = null;
    try {
        // jc = JAXBContext.newInstance("org.isotc211.iso19139.d_2006_05_04.srv");
        // jc = JAXBContext.newInstance("org.isotc211.iso19139.d_2006_05_04.srv");
        jc = JAXBContext
                .newInstance(new Class[] { org.isotc211.iso19139.d_2006_05_04.srv.ObjectFactory.class });
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Source srcFile = new StreamSource(xmlFile);
        JAXBElement<?> element = (JAXBElement<?>) unmarshaller.unmarshal(srcFile);
        // JAXBElement<?> element = (JAXBElement<?>) unmarshaller.unmarshal(in);
        SVServiceIdentificationType serviceIdentificationType = (SVServiceIdentificationType) element
                .getValue();
        // serviceIdentificationType = (SVServiceIdentificationType) unmarshaller.unmarshal(in);
        System.out.println(serviceIdentificationType.toString());

        List<SVOperationMetadataPropertyType> operationMetadataPropertyTypeList = serviceIdentificationType
                .getContainsOperations();

        for (SVOperationMetadataPropertyType operationMetadataPropertyType : operationMetadataPropertyTypeList) {

            SVOperationMetadataType operationMetadataType = operationMetadataPropertyType
                    .getSVOperationMetadata();
            System.out.println("---------------------------------------------");
            if (operationMetadataType == null) {
                continue;
            }
            System.out.println(operationMetadataType.getOperationName().getCharacterString().getValue());
            System.out.println(operationMetadataType.getInvocationName().getCharacterString().getValue());
            System.out.println(operationMetadataType.getOperationDescription().getCharacterString().getValue());

            CIOnlineResourcePropertyType onlineResourcePropertyType = operationMetadataType.getConnectPoint()
                    .get(0);
            if (onlineResourcePropertyType != null) {
                System.out.println(operationMetadataType.getConnectPoint().get(0).getCIOnlineResource()
                        .getLinkage().getURL());
            }

            List<SVParameterPropertyType> parameterPropertyTypeList = operationMetadataType.getParameters();

            for (SVParameterPropertyType parameterPropertyType : parameterPropertyTypeList) {
                SVParameterType parameterType = parameterPropertyType.getSVParameter();

                if (parameterType.getName().getAName().getCharacterString() != null) {
                    System.out.println(parameterType.getName().getAName().getCharacterString().getValue());
                } else {
                    System.out.println("WARNING - A parameter has no name");

                }
                if (parameterType.getDescription() != null) {
                    if (parameterType.getDescription().getCharacterString() != null) {
                        System.out.println(parameterType.getDescription().getCharacterString().getValue());
                    } else {
                        System.out.println("WARNING - A parameter has no description");

                    }
                } else {
                    System.out.println("WARNING - A parameter has no description");

                }
            }

        }
        FileWriter writer = new FileWriter("c:/tempVFS/test.xml");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(element, writer);

        writer.flush();
        writer.close();

        System.out.println("End testLoadOGCServiceMetadata");
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.virtualparadigm.packman.processor.JPackageManager.java

private static void marshallToFile(Collection<Package> packages, String filePath) {
    try {//from  w  w  w .  j av a  2 s.  c  o m
        Marshaller marshaller = jaxbContext.createMarshaller();

        // removes the xml header:
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter stringWriter = new StringWriter();
        for (Package installedPackage : packages) {
            marshaller.marshal(
                    new JAXBElement<Package>(new QName(null, "package"), Package.class, installedPackage),
                    stringWriter);

            stringWriter.append("\n");
        }

        FileUtils.writeStringToFile(new File(filePath), stringWriter.toString(), "UTF-8");
    } catch (Exception e) {
        logger.error("", e);
    }
}

From source file:org.eclipse.winery.repository.client.WineryRepositoryClient.java

/**
 * Creates a marshaller/*ww w .  j  av  a2s .c om*/
 *
 * @throws IllegalStateException if marshaller could not be instantiated
 */
private static Marshaller createMarshaller() {
    // code copied+adapted from JAXBSupport
    Marshaller m;
    try {
        m = WineryRepositoryClient.context.createMarshaller();
        // pretty printed output is required as the XML is sent 1:1 to the browser for editing
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        // not possible here: m.setProperty("com.sun.xml.bind.namespacePrefixMapper", JAXBSupport.prefixMapper);
    } catch (JAXBException e) {
        LOGGER.error("Could not instantiate marshaller", e);
        throw new IllegalStateException(e);
    }
    return m;
}

From source file:Main.java

public final String getMessage() throws Exception {

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty("jaxb.encoding", "ISO-8859-1");
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos,
            (String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING));
    xmlStreamWriter.writeStartDocument((String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
    jaxbMarshaller.marshal(this, xmlStreamWriter);
    xmlStreamWriter.writeEndDocument();//  w  w w. ja  va  2s .c om
    xmlStreamWriter.close();
    return new String(baos.toByteArray());
}

From source file:com.fujitsu.dc.test.utils.DavResourceUtils.java

/**
 * BoxACL?./*  w w  w.j  a  v  a2 s  .c o  m*/
 * <p>
 * 
 * <pre>
 * Acl acl = new Acl();
 * acl.getAce().add(DavResourceUtils.createAce(false, roleRead, &quot;read&quot;));
 * acl.getAce().add(DavResourceUtils.createAce(false, roleWrite, &quot;write&quot;));
 * acl.setXmlbase(String.format(&quot;%s/%s/__role/%s/&quot;,
 *         UrlUtils.getBaseUrl(), CELL_NAME, Box.DEFAULT_BOX_NAME));
 * DavResourceUtils.setAcl(MASTER_TOKEN, CELL_NAME, BOX_NAME, COL_NAME, acl, HttpStatus.SC_OK);
 * </pre>
 * </p>
 * @param token 
 * @param cell ??
 * @param box ??
 * @param col ??
 * @param acl ?ACL
 * @param code ?
 * @return ?
 * @throws JAXBException ACL????????
 */
public static TResponse setAcl(String token, String cell, String box, String col, Acl acl, int code)
        throws JAXBException {
    StringWriter writer = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(Acl.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(acl, writer);
    return Http.request("box/acl-setting-none-body.txt").with("cell", cell).with("box", box)
            .with("colname", col).with("token", token).with("body", writer.toString()).returns().debug()
            .statusCode(code);
}

From source file:StringAdapter.java

private void demo(String xml) throws JAXBException {
    StringReader stringReader = new StringReader(xml);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Root root = (Root) unmarshaller.unmarshal(stringReader);

    System.out.println("ITEM:   " + root.getItem());

    System.out.print("OUTPUT: ");
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    marshaller.marshal(root, System.out);
}

From source file:com.iflytek.edu.cloud.frame.spring.Jaxb2RootElementHttpMessageConverterExt.java

@Override
protected void customizeMarshaller(Marshaller marshaller) {
    try {//from   w w w .jav  a2  s  .c  o  m
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    } catch (PropertyException e) {
        LOGGER.error(e.getMessage(), e);
    }
}

From source file:com.tremolosecurity.openunison.util.OpenUnisonUtils.java

private static void storeMethod(String unisonXMLFile, TremoloType tt, String ksPath, KeyStore ks)
        throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException,
        FileNotFoundException, JAXBException, PropertyException {
    logger.info("Storing the keystore");
    ks.store(new FileOutputStream(ksPath), tt.getKeyStorePassword().toCharArray());

    logger.info("Saving the unison xml file");

    JAXBContext jc = JAXBContext.newInstance("com.tremolosecurity.config.xml");
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    OutputStream os = new FileOutputStream(unisonXMLFile);
    JAXBElement<TremoloType> root = new JAXBElement<TremoloType>(
            new QName("http://www.tremolosecurity.com/tremoloConfig", "tremoloConfig", "tns"),
            TremoloType.class, tt);
    marshaller.marshal(root, os);/*  w w w .ja v  a 2  s.  c  o  m*/
    os.flush();
    os.close();
}

From source file:org.mule.modules.rest.model.LeagueTransformers.java

@Transformer(resultMimeType = "text/xml")
public String toXml(League league) throws IOException, JAXBException {
    JAXBContext context = JAXBContext.newInstance(League.class);

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

    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    m.marshal(league, boas);//  w w w.  j  av a2 s .co m

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