Example usage for javax.xml.bind Marshaller JAXB_FRAGMENT

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

Introduction

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

Prototype

String JAXB_FRAGMENT

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

Click Source Link

Document

The name of the property used to specify whether or not the marshaller will generate document level events (ie calling startDocument or endDocument).

Usage

From source file:com.jaspersoft.jasperserver.rest.utils.Utils.java

public Marshaller getMarshaller(Class... docClass) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(docClass);
    Marshaller m = context.createMarshaller();
    m.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    return m;// ww  w  .  jav a2s  . c om
}

From source file:com.checkmarx.jenkins.CxWebService.java

private Pair<byte[], byte[]> createScanSoapMessage(Object request, Class inputType,
        ProjectSettings projectSettings, LocalCodeContainer localCodeContainer, boolean visibleToOtherUsers,
        boolean isPublicScan) {
    final String soapMessageHead = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
            + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
            + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
            + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" + "  <soap:Body>\n";

    final String soapMessageTail = "\n  </soap:Body>\n</soap:Envelope>";
    final String zippedFileOpenTag = "<ZippedFile>";
    final String zippedFileCloseTag = "</ZippedFile>";

    try {//from   w w w .j ava  2 s. com
        final JAXBContext context = JAXBContext.newInstance(inputType);
        final Marshaller marshaller = context.createMarshaller();

        StringWriter scanMessage = new StringWriter();
        scanMessage.write(soapMessageHead);

        // Nullify the zippedFile field, and save its old value for
        // restoring later
        final byte[] oldZippedFileValue = localCodeContainer.getZippedFile();
        localCodeContainer.setZippedFile(new byte[] {});

        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.marshal(request, scanMessage);
        localCodeContainer.setZippedFile(oldZippedFileValue); // Restore the
        // old value

        scanMessage.write(soapMessageTail);
        // Here we split the message around <ZippedFile></ZippedFile>
        // substring. We know that the opening
        // and closing tag are adjacent because the zippedFile property was
        // set to empty byte array
        final String[] parts = scanMessage.toString().split(zippedFileOpenTag + zippedFileCloseTag);
        assert parts.length == 2;
        final String startPart = parts[0] + zippedFileOpenTag;
        final String endPart = zippedFileCloseTag + parts[1];

        return Pair.of(startPart.getBytes("UTF-8"), endPart.getBytes("UTF-8"));
    } catch (JAXBException | UnsupportedEncodingException e) {

        // Getting here indicates a bug
        logger.error(e.getMessage(), e);
        throw new RuntimeException("Eror creating SOAP message", e);
    }
}

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

private static void marshallToFile(Collection<Package> installPackages, String filePath) {
    try {//from ww  w.java2s. c o  m
        Marshaller marshaller = jaxbContext.createMarshaller();

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

            stringWriter.append("\n");
        }

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

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

private static void marshallToFile(Collection<Package> packages, String filePath) {
    try {/* w  w w.  ja v  a 2  s .  c om*/
        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:nl.b3p.viewer.stripes.ArcQueryUtilActionBean.java

@DefaultHandler
public Resolution arcXML() throws JSONException {
    JSONObject json = new JSONObject();

    try {/*  ww  w  .  jav a 2 s.c om*/
        AxlSpatialQuery aq = new AxlSpatialQuery();
        FilterToArcXMLSQL visitor = new FilterToArcXMLSQL(aq);

        Filter filter = CQL.toFilter(cql);
        String where = visitor.encodeToString(filter);

        if (whereOnly) {
            json.put("where", where);
        } else {
            if (where.trim().length() > 0 && !where.trim().equals("1=1")) {
                aq.setWhere(where);
            }

            StringWriter sw = new StringWriter();

            Marshaller m = ArcXML.getJaxbContext().createMarshaller();
            m.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8");
            m.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
            m.marshal(new JAXBElement(new QName(null, "SPATIALQUERY"), AxlSpatialQuery.class, aq), sw);
            sw.close();

            json.put("SPATIALQUERY", sw.toString());
        }
        json.put("success", true);
    } catch (Exception e) {
        json.put("success", false);

        String message = "Fout bij maken spatial query: " + e.toString();
        Throwable cause = e.getCause();
        while (cause != null) {
            message += "; " + cause.toString();
            cause = cause.getCause();
        }
        json.put("error", message);
    }

    return new StreamingResolution("application/json", new StringReader(json.toString(4)));
}

From source file:org.alex73.osm.converters.bel.Convert.java

public static void main(String[] args) throws Exception {
    loadStreetNamesForHouses();//w w  w  .j  a  v  a 2 s .  com

    InputStream in = new BZip2CompressorInputStream(
            new BufferedInputStream(new FileInputStream("tmp/belarus-latest.osm.bz2"), BUFFER_SIZE));

    // create xml event reader for input stream
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent newLine = eventFactory.createCharacters("\n");
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLEventReader reader = xif.createXMLEventReader(in);
    XMLEventWriter wrCyr = xof.createXMLEventWriter(
            new BufferedOutputStream(new FileOutputStream("tmp/belarus-bel.osm"), BUFFER_SIZE));
    XMLEventWriter wrInt = xof.createXMLEventWriter(
            new BufferedOutputStream(new FileOutputStream("tmp/belarus-intl.osm"), BUFFER_SIZE));

    // initialize jaxb
    JAXBContext jaxbCtx = JAXBContext.newInstance(Node.class, Way.class, Relation.class);
    Unmarshaller um = jaxbCtx.createUnmarshaller();
    Marshaller m = jaxbCtx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FRAGMENT, true);
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    XMLEvent e = null;
    while ((e = reader.peek()) != null) {
        boolean processed = false;
        if (e.isStartElement()) {
            StartElement se = (StartElement) e;
            switch (se.getName().getLocalPart()) {
            case "way":
                Way way = um.unmarshal(reader, Way.class).getValue();
                if (way.getId() == 25439425) {
                    System.out.println();
                }
                fixBel(way.getTag(), "name:be", "name");
                String nameBeHouse = houseStreetBe.get(way.getId());
                if (nameBeHouse != null) {
                    setTag(way.getTag(), "addr:street", nameBeHouse);
                }
                m.marshal(way, wrCyr);
                fixInt(way.getTag());
                m.marshal(way, wrInt);
                wrCyr.add(newLine);
                wrInt.add(newLine);
                processed = true;
                break;
            case "node":
                Node node = um.unmarshal(reader, Node.class).getValue();
                fixBel(node.getTag(), "name:be", "name");
                // fixBel(node.getTag(),"addr:street:be","addr:street");
                m.marshal(node, wrCyr);
                fixInt(node.getTag());
                m.marshal(node, wrInt);
                wrCyr.add(newLine);
                wrInt.add(newLine);
                processed = true;
                break;
            case "relation":
                Relation relation = um.unmarshal(reader, Relation.class).getValue();
                fixBel(relation.getTag(), "name:be", "name");
                // fixBel(relation.getTag(),"addr:street:be","addr:street");
                m.marshal(relation, wrCyr);
                fixInt(relation.getTag());
                m.marshal(relation, wrInt);
                wrCyr.add(newLine);
                wrInt.add(newLine);
                processed = true;
                break;
            }
        }
        if (!processed) {
            wrCyr.add(e);
            wrInt.add(e);
        }
        reader.next();
    }

    wrCyr.flush();
    wrCyr.close();
    wrInt.flush();
    wrInt.close();
    System.out.println("UniqueTranslatedTags: " + uniqueTranslatedTags);
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBUtils.java

/**
 * Get JAXBMarshaller/*w  w  w .  j  a va  2 s  .  c  om*/
 *
 * @param context JAXBContext
 * @return Marshaller
 * @throws JAXBException
 */
public static Marshaller getJAXBMarshaller(JAXBContext context) throws JAXBException {
    Marshaller m = null;

    if (!ENABLE_MARSHALL_POOLING) {
        if (log.isDebugEnabled()) {
            log.debug("Marshaller created [no pooling]");
        }
        m = internalCreateMarshaller(context);
    } else {
        m = mpool.get(context);
        if (m == null) {
            if (log.isDebugEnabled()) {
                log.debug("Marshaller created [not in pool]");
            }
            m = internalCreateMarshaller(context);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Marshaller obtained [from  pool]");
            }
        }
    }
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); // No PIs
    return m;
}

From source file:org.apache.cayenne.dbimport.DefaultReverseEngineeringWriter.java

@Override
public Resource write(ReverseEngineering reverseEngineering, Writer writer) throws CayenneRuntimeException {

    try {//from  w ww. j  a v a  2  s.  c o m
        JAXBContext context = JAXBContext.newInstance(reverseEngineering.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
                "http://cayenne.apache.org/schema/8/reverseEngineering http://cayenne.apache.org/schema/8/reverseEngineering.xsd");

        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.marshal(reverseEngineering, writer);
        writer.close();
        return reverseEngineering.getConfigurationSource();
    } catch (JAXBException | IOException e) {
        LOGGER.error(e.getMessage(), e);
        throw new CayenneRuntimeException(e);
    }
}

From source file:org.apache.cxf.jaxbplus.io.DataWriterImpl.java

public Marshaller createMarshaller(Object elValue, MessagePartInfo part) {
    Class<?> cls = null;/*from w  w w.  j  a v  a  2s. c o  m*/
    if (part != null) {
        cls = part.getTypeClass();
    }

    if (cls == null) {
        cls = null != elValue ? elValue.getClass() : null;
    }

    if (cls != null && cls.isArray() && elValue instanceof Collection) {
        Collection<?> col = (Collection<?>) elValue;
        elValue = col.toArray((Object[]) Array.newInstance(cls.getComponentType(), col.size()));
    }
    Marshaller marshaller;
    try {

        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
        marshaller.setListener(databinding.getMarshallerListener());
        if (databinding.getValidationEventHandler() != null) {
            marshaller.setEventHandler(databinding.getValidationEventHandler());
        }

        final Map<String, String> nspref = databinding.getDeclaredNamespaceMappings();
        if (nspref != null) {
            JAXBUtils.setNamespaceWrapper(nspref, marshaller);
        }
        if (databinding.getMarshallerProperties() != null) {
            for (Map.Entry<String, Object> propEntry : databinding.getMarshallerProperties().entrySet()) {
                try {
                    marshaller.setProperty(propEntry.getKey(), propEntry.getValue());
                } catch (PropertyException pe) {
                    LOG.log(Level.INFO, "PropertyException setting Marshaller properties", pe);
                }
            }
        }

        marshaller.setSchema(schema);
        AttachmentMarshaller atmarsh = getAttachmentMarshaller();
        marshaller.setAttachmentMarshaller(atmarsh);

        if (schema != null && atmarsh instanceof JAXBAttachmentMarshaller) {
            //we need a special even handler for XOP attachments 
            marshaller.setEventHandler(new MtomValidationHandler(marshaller.getEventHandler(),
                    (JAXBAttachmentMarshaller) atmarsh));
        }
    } catch (JAXBException ex) {
        if (ex instanceof javax.xml.bind.MarshalException) {
            javax.xml.bind.MarshalException marshalEx = (javax.xml.bind.MarshalException) ex;
            Message faultMessage = new Message("MARSHAL_ERROR", LOG,
                    marshalEx.getLinkedException().getMessage());
            throw new Fault(faultMessage, ex);
        } else {
            throw new Fault(new Message("MARSHAL_ERROR", LOG, ex.getMessage()), ex);
        }
    }
    return marshaller;
}

From source file:org.apache.juddi.jaxb.JAXBMarshaller.java

public static String marshallToString(Object object, String thePackage) {
    String rawObject = null;/*from www.j av a 2 s  . c o m*/

    try {
        JAXBContext jc = getContext(thePackage);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        marshaller.marshal(object, baos);
        rawObject = baos.toString();
    } catch (JAXBException e) {
        logger.error(e.getMessage(), e);
    }

    return rawObject;
}