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:nl.b3p.viewer.stripes.ArcQueryUtilActionBean.java

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

    try {//  www.j  a  v  a 2s.  co m
        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.accada.epcis.repository.query.QuerySubscription.java

/**
 * Marshals the given EPCIS query document into it's XML representation.
 * /*from w  w  w .ja va2s .  c  o m*/
 * @param epcisDoc
 *            The EPCISQueryDocumentType to marshal.
 * @return The marshaled EPCISQueryDocumentType XML String.
 */
private String marshalQueryDoc(EPCISQueryDocumentType epcisDoc) throws JAXBException {
    ObjectFactory objectFactory = new ObjectFactory();
    JAXBContext context = JAXBContext.newInstance("org.accada.epcis.model");
    JAXBElement<EPCISQueryDocumentType> item = objectFactory.createEPCISQueryDocument(epcisDoc);
    LOG.debug("Serializing " + item + " into XML");
    StringWriter writer = new StringWriter();
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(item, writer);
    return writer.toString();
}

From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java

/**
 * Marshal the jaxb object/*ww  w .  j  a  v a  2s  .  co  m*/
 * @param obj
 * @param writer
 * @param am AttachmentMarshaller, optional Attachment
 */
public void marshal(Object obj, XMLStreamWriter writer) throws JAXBException {
    if (log.isDebugEnabled()) {
        log.debug("enter marshal");
    }
    boolean installedFilter = false;

    try {
        // There may be a preferred classloader that should be used
        ClassLoader cl = getClassLoader();

        // Very easy, use the Context to get the Marshaller.
        // Use the marshaller to write the object.
        JAXBContext jbc = getJAXBContext(cl);
        Marshaller m = JAXBUtils.getJAXBMarshaller(jbc);
        if (writer instanceof MTOMXMLStreamWriter && ((MTOMXMLStreamWriter) writer).getOutputFormat() != null) {
            String encoding = ((MTOMXMLStreamWriter) writer).getOutputFormat().getCharSetEncoding();

            String marshallerEncoding = (String) m.getProperty(Marshaller.JAXB_ENCODING);

            // Make sure that the marshaller respects the encoding of the message.
            // This is accomplished by setting the encoding on the Marshaller's JAXB_ENCODING property.
            if (encoding == null && marshallerEncoding == null) {
                if (log.isDebugEnabled()) {
                    log.debug(
                            "The encoding and the marshaller's JAXB_ENCODING are both set to the default (UTF-8)");
                }
            } else {
                // Must set the encoding to an actual String to set it on the Marshaller
                if (encoding == null) {
                    encoding = "UTF-8";
                }
                if (!encoding.equalsIgnoreCase(marshallerEncoding)) {
                    if (log.isDebugEnabled()) {
                        log.debug("The Marshaller.JAXB_ENCODING is " + marshallerEncoding);
                        log.debug(
                                "The Marshaller.JAXB_ENCODING is changed to the message encoding " + encoding);
                    }
                    m.setProperty(Marshaller.JAXB_ENCODING, encoding);
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("The encoding and the marshaller's JAXB_ENCODING are both set to:"
                                + marshallerEncoding);
                    }
                }
            }
        }

        AttachmentMarshaller am = createAttachmentMarshaller(writer);
        if (am != null) {
            if (DEBUG_ENABLED) {
                log.debug("Adding JAXBAttachmentMarshaller to Marshaller");
            }
            m.setAttachmentMarshaller(am);
        }

        MessageContext mc = getMessageContext();

        // If requested install a filter to remove illegal characters
        installedFilter = installFilter(mc, writer);

        // Marshal the object
        if (getProcessType() == null) {
            marshalByElement(obj, m, writer, true);
            //!am.isXOPPackage());
        } else {
            marshalByType(obj, m, writer, getProcessType(), isxmlList(), getConstructionType(), true); // Attempt to optimize by writing to OutputStream
        }

        JAXBUtils.releaseJAXBMarshaller(jbc, m);

        if (log.isDebugEnabled()) {
            log.debug("exit marshal");
        }
    } finally {
        // Make sure the filter is uninstalled
        if (installedFilter) {
            uninstallFilter(writer);
        }
    }
}

From source file:org.apache.axis2.datasource.jaxb.JAXBDSContext.java

/**
 * If the writer is backed by an OutputStream, then return the OutputStream
 * @param writer/* www . jav a  2  s .co m*/
 * @param Marshaller
 * @return OutputStream or null
 */
private static OutputStream getOutputStream(XMLStreamWriter writer, Marshaller m) throws XMLStreamException {
    if (log.isDebugEnabled()) {
        log.debug("XMLStreamWriter is " + writer);
    }
    OutputStream os = null;
    if (writer.getClass() == MTOMXMLStreamWriter.class) {
        os = ((MTOMXMLStreamWriter) writer).getOutputStream();
        if (log.isDebugEnabled()) {
            log.debug("OutputStream accessible from MTOMXMLStreamWriter is " + os);
        }
    }
    if (writer.getClass() == XMLStreamWriterWithOS.class) {
        os = ((XMLStreamWriterWithOS) writer).getOutputStream();
        if (log.isDebugEnabled()) {
            log.debug("OutputStream accessible from XMLStreamWriterWithOS is " + os);
        }
    }
    if (os != null) {
        String marshallerEncoding = null;
        try {
            marshallerEncoding = (String) m.getProperty(Marshaller.JAXB_ENCODING);
        } catch (PropertyException e) {
            if (DEBUG_ENABLED) {
                log.debug("Could not query JAXB_ENCODING..Continuing. " + e);
            }
        }
        if (marshallerEncoding != null && !marshallerEncoding.equalsIgnoreCase("UTF-8")) {
            if (DEBUG_ENABLED) {
                log.debug("Wrapping output stream to remove BOM");
            }
            os = new BOMOutputStreamFilter(marshallerEncoding, os);
        }
    }

    return os;
}

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

/**
 * releaseJAXBMarshalller //from   w  ww .ja  v a 2  s.c  om
 * Do not call this method if an exception occurred while using the
 * Marshaller. We don't want an object in an invalid state.
 *
 * @param context    JAXBContext
 * @param marshaller Marshaller
 */
public static void releaseJAXBMarshaller(JAXBContext context, Marshaller marshaller) {
    if (log.isDebugEnabled()) {
        log.debug("Marshaller placed back into pool");
        log.debug("  Marshaller = " + JavaUtils.getObjectIdentity(marshaller));
        log.debug("  JAXBContext = " + JavaUtils.getObjectIdentity(context));
    }
    if (ENABLE_MARSHALL_POOLING) {
        // Make sure to clear any state or properties

        try {
            marshaller.setAttachmentMarshaller(null);
            // Set the JAXB_ENCODING back to the default value UTF-8
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mpool.put(context, marshaller);
        } catch (Throwable t) {
            // Log the problem, and continue without pooling
            if (log.isDebugEnabled()) {
                log.debug("The following exception is ignored. Processing continues " + t);
            }
        }

    }
}

From source file:org.apache.camel.converter.jaxb.FallbackTypeConverter.java

protected <T> T marshall(Class<T> type, Exchange exchange, Object value)
        throws JAXBException, XMLStreamException, FactoryConfigurationError {
    T answer = null;/*from w  w w . java2 s.c  om*/
    if (parentTypeConverter != null) {
        // lets convert the object to a JAXB source and try convert that to
        // the required source
        JAXBContext context = createContext(value.getClass());
        // must create a new instance of marshaller as its not thread safe
        Marshaller marshaller = context.createMarshaller();
        Writer buffer = new StringWriter();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                isPrettyPrint() ? Boolean.TRUE : Boolean.FALSE);
        if (exchange != null && exchange.getProperty(Exchange.CHARSET_NAME, String.class) != null) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING,
                    exchange.getProperty(Exchange.CHARSET_NAME, String.class));
        }
        if (needFiltering(exchange)) {
            XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer);
            FilteringXmlStreamWriter filteringWriter = new FilteringXmlStreamWriter(writer);
            marshaller.marshal(value, filteringWriter);
        } else {
            marshaller.marshal(value, buffer);
        }
        answer = parentTypeConverter.convertTo(type, buffer.toString());
    }

    return answer;
}

From source file:org.apache.camel.converter.jaxb.JaxbDataFormat.java

public void marshal(Exchange exchange, Object graph, OutputStream stream) throws IOException {
    try {/* w  w w  .j  a  v a2s  .c o  m*/
        // must create a new instance of marshaller as its not thread safe
        Marshaller marshaller = getContext().createMarshaller();
        if (isPrettyPrint()) {
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        }
        // exchange take precedence over encoding option
        String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
        if (charset == null) {
            charset = encoding;
        }
        if (charset != null) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
        }

        marshal(exchange, graph, stream, marshaller);

    } catch (JAXBException e) {
        throw IOHelper.createIOException(e);
    } catch (XMLStreamException e) {
        throw IOHelper.createIOException(e);
    }
}

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

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

    try {/*from   ww w .j  a  v  a2 s. co  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 ww .  j  av a 2s  .  com
    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;//w  w w .  ja  v a2s . 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;
}