Example usage for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT

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

Introduction

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

Prototype

String JAXB_FORMATTED_OUTPUT

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

Click Source Link

Document

The name of the property used to specify whether or not the marshalled XML data is formatted with linefeeds and indentation.

Usage

From source file:org.javelin.sws.ext.bind.jaxb.JaxbTest.java

@Test(expected = IllegalAnnotationsException.class)
public void marshalIllegalProperties() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1 mc5 = new org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1();
    mc5.setC(String.class);
    m.marshal(new JAXBElement<org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1>(new QName("a", "a"),
            org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1.class, mc5), System.out);
}

From source file:cz.lbenda.dataman.rc.DbConfigFactory.java

public static String storeToString(Callback<DbConfig, String> cacheDbStructureWriteFactory) {
    cz.lbenda.dataman.schema.dataman.ObjectFactory of = new cz.lbenda.dataman.schema.dataman.ObjectFactory();
    DatamanType config = of.createDatamanType();
    config.setSessions(of.createSessionsType());

    for (DbConfig sc : getConfigurations()) {
        config.getSessions().getSession()
                .add(sc.storeToSessionType(null, cacheDbStructureWriteFactory.call(sc)));
    }// w  w w  . j av  a  2  s .co  m

    try {
        JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.dataman.ObjectFactory.class);
        Marshaller m = jc.createMarshaller();
        StringWriter sw = new StringWriter();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(of.createDataman(config), sw);
        return sw.toString();
    } catch (JAXBException e) {
        LOG.error("Problem with write configuration: " + e.toString(), e);
        throw new RuntimeException("Problem with write configuration: " + e.toString(), e);
    }
}

From source file:hermes.impl.DefaultXMLHelper.java

public void saveContent(MessageSet messages, Writer writer) throws Exception {
    JAXBContext jc = JAXBContext.newInstance("hermes.xml");
    Marshaller m = jc.createMarshaller();

    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(new JAXBElement<MessageSet>(new QName("", "content"), MessageSet.class, messages), writer);
    writer.flush();//from   w  w w . ja v a2 s.co m
}

From source file:com.esri.geoevent.solutions.adapter.cot.CoTAdapterOutbound.java

@Override
public void receive(GeoEvent geoEvent) {

    stringBuffer.setLength(0);/* w  w w  . ja  v  a 2s.c o m*/
    GeoEventDefinition definition = geoEvent.getGeoEventDefinition();
    System.out.println("Creating Event to marshal...");
    Event event = new Event();
    for (FieldDefinition fieldDefinition : definition.getFieldDefinitions()) {
        try {
            String attributeName = fieldDefinition.getName();
            Object value;
            if ((value = geoEvent.getField(attributeName)) != null) {

                if (attributeName.equalsIgnoreCase("version")) {
                    event.setVersion((Double) value);
                } else if (attributeName.equalsIgnoreCase("uid")) {
                    event.setUid(value.toString());
                } else if (attributeName.equalsIgnoreCase("type")) {
                    event.setType(value.toString());
                } else if (attributeName.equalsIgnoreCase("how")) {
                    event.setHow(value.toString());
                } else if (attributeName.equalsIgnoreCase("time")) {
                    event.setTime((Date) value);
                } else if (attributeName.equalsIgnoreCase("start")) {
                    event.setStart((Date) value);
                } else if (attributeName.equalsIgnoreCase("stale")) {
                    event.setStale((Date) value);
                } else if (attributeName.equalsIgnoreCase("access")) {
                    event.setAccess(value.toString());
                } else if (attributeName.equalsIgnoreCase("opex")) {
                    event.setOpex(value.toString());
                } else if (attributeName.equalsIgnoreCase("qos")) {
                    event.setQos(value.toString());
                } else if (attributeName.equalsIgnoreCase("detail")) {
                    event.setDetail(this.unpackDetial(fieldDefinition, geoEvent.getFieldGroup("detail")));

                    // GETALLFIELDS
                    // CHECK ITS TYPE IF GROUP THEN INSPECT THEM
                } else if (attributeName.equalsIgnoreCase("point")) {
                    Point p = pointFromJson(value);
                    event.setPoint(pointFromJson(p));
                }
            }

        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

    }

    /////////////////////////
    String xmlResult = null;
    StringBuilder myResult = new StringBuilder();
    int content;

    System.out.println("Event created.");

    System.out.println("Marshalling Event into XML.");

    try {

        JAXBContext contextObj = JAXBContext.newInstance(Event.class);
        Marshaller marshallerObj = contextObj.createMarshaller();
        marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        marshallerObj.marshal(event, os);
        ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray());

        while ((content = bais.read()) != -1) {
            myResult.append((char) content);
        }

        xmlResult = fixEscapeCharacters(myResult.toString());
        System.out.println("**** XML RESULTS ***");
        System.out.println(xmlResult);
        //this.byteListener.receive(ByteBuffer.wrap(xmlResult.getBytes()), "");
        super.receive(ByteBuffer.wrap(xmlResult.getBytes()), "", geoEvent);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Done");
}

From source file:com.moss.nomad.core.packager.Packager.java

public void write(OutputStream o) throws Exception {

    JarOutputStream out = new JarOutputStream(o);

    {//from  ww  w .  ja v  a2 s  . co m
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(container, bao);

        byte[] containerIndex = bao.toByteArray();

        JarEntry entry = new JarEntry("META-INF/container.xml");
        out.putNextEntry(entry);
        out.write(containerIndex);
    }

    final byte[] buffer = new byte[1024 * 10]; //10k buffer

    for (String path : dependencies.keySet()) {
        ResolvedDependencyInfo info = dependencies.get(path);

        JarEntry entry = new JarEntry(path);
        out.putNextEntry(entry);

        InputStream in = new FileInputStream(info.file());
        for (int numRead = in.read(buffer); numRead != -1; numRead = in.read(buffer)) {
            out.write(buffer, 0, numRead);
        }
        in.close();
    }

    out.close();
}

From source file:com.mondora.chargify.controller.HttpsXmlChargify.java

/**
 * Creates a new HttpsXmlChargify instance.
 * /*  w  w w  .  j  a va2  s. c  o m*/
 * @param apiKey
 *            Your API key from Chargify.
 * @param fqChargifySubdomain
 *            Your fully qualified Chargify subdomain. Example: bubbles.chargify.com
 */
public HttpsXmlChargify(String apiKey, String fqChargifySubdomain) {
    httpClient = new DefaultHttpClient();
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(fqChargifySubdomain, 443),
            new UsernamePasswordCredentials(apiKey, "x"));

    base = HTTPS + fqChargifySubdomain;
    productsHttpGet = new HttpGet(base + "/products.xml");
    subscriptionsHttpGet = new HttpGet(base + "/subscriptions.xml");
    customersHttpGet = new HttpGet(base + "/customers.xml");

    try {
        context = JAXBContext.newInstance(Components.class, Products.class, Subscriptions.class, Errors.class,
                CreateSubscription.class, Customers.class);
        unmarshaller = context.createUnmarshaller();
        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

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

public void guardar(OutputStream out) throws Exception {
    Marshaller m = context.createMarshaller();
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl(localPrefixes));
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    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");
    byte[] xmlHeaderBytes = XML_HEADER.getBytes("UTF8");
    out.write(xmlHeaderBytes);// w ww  .  ja  va2 s . c o m
    m.marshal(document, out);
}

From source file:edu.isi.misd.scanner.network.worker.webapp.ResultsReleaseDelegate.java

private void writeRejectedServiceResponse(String id, String url, String siteName, String nodeName,
        String comments, File outputFile) throws Exception {
    // 1. Populate the rejected ServiceResponse
    ServiceResponse response = new ServiceResponse();
    ServiceResponseMetadata responseMetadata = new ServiceResponseMetadata();
    responseMetadata.setRequestID(id);//w w w . j a v  a  2s  .c  o  m
    responseMetadata.setRequestURL(url);
    responseMetadata.setRequestState(ServiceRequestStateType.REJECTED);
    responseMetadata.setRequestStateDetail("Reason: " + (comments.isEmpty() ? "not specified." : comments));
    responseMetadata.setRequestSiteName(siteName);
    responseMetadata.setRequestNodeName(nodeName);
    response.setServiceResponseMetadata(responseMetadata);

    // 2. Write out the result response using JAXB
    JAXBContext jaxbContext = JAXBContext.newInstance(ServiceResponse.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    if (outputFile.exists()) {
        try {
            FileUtils.forceDelete(outputFile);
        } catch (Exception e) {
            log.warn("Could not delete output file: " + outputFile.getCanonicalPath());
            throw e;
        }
    }
    jaxbMarshaller.marshal(response, outputFile);
}

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

public void testJaxbManual() {
    System.out.println("start of test:testJaxb()");
    try {//from www .j  av  a 2s.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());

        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:mx.bigdata.sat.cfd.CFDv2.java

public void guardar(OutputStream out) throws Exception {
    Marshaller m = context.createMarshaller();
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl(localPrefixes));
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
            "http://www.sat.gob.mx/cfd/2 " + "http://www.sat.gob.mx/sitio_internet/cfd/2/cfdv2.xsd");
    byte[] xmlHeaderBytes = XML_HEADER.getBytes("UTF8");
    out.write(xmlHeaderBytes);/*w w w. j  av  a 2  s  .  com*/
    m.marshal(document, out);
}