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:org.kalypso.ogc.sensor.tableview.TableViewUtils.java

/**
 * Saves the given template (binding). Closes the writer.
 *//* w ww  .java2s .  co m*/
public static void saveTableTemplateXML(final Obstableview xml, final OutputStreamWriter writer)
        throws JAXBException {
    try {
        final Marshaller m = JaxbUtilities.createMarshaller(OTT_JC);
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_ENCODING, writer.getEncoding());
        m.marshal(xml, writer);
    } finally {
        IOUtils.closeQuietly(writer);
    }
}

From source file:org.omnaest.utils.xml.JAXBXMLHelper.java

/**
 * Stores a given JAXB annotated object to the given {@link OutputStream} using the given {@link MarshallingConfiguration}
 * /*from   ww w  .j a  v a 2s .c  o  m*/
 * @see #storeObjectAsXML(Object, OutputStream)
 * @param object
 * @param outputStream
 * @param marshallingConfiguration
 *          {@link MarshallingConfiguration}
 */
public static void storeObjectAsXML(Object object, OutputStream outputStream,
        MarshallingConfiguration marshallingConfiguration) {
    //
    marshallingConfiguration = MarshallingConfiguration
            .defaultMarshallingConfiguration(marshallingConfiguration);

    //
    final String encoding = marshallingConfiguration.getEncoding();
    final ExceptionHandler exceptionHandler = marshallingConfiguration.getExceptionHandler();
    final Class<?>[] knownTypes = marshallingConfiguration.getKnownTypes();
    final boolean formattingOutput = marshallingConfiguration.isFormattingOutput();
    final MarshallingConfiguration.Configurator configurator = ObjectUtils.defaultIfNull(
            marshallingConfiguration.getConfigurator(), new MarshallingConfiguration.Configurator() {
            });

    // 
    try {
        //
        final Class<? extends Object> objectType = object.getClass();
        final Class<?>[] contextTypes = !(object instanceof JAXBElement)
                ? ArrayUtils.add(knownTypes, objectType)
                : (knownTypes != null ? knownTypes : new Class[0]);

        //
        final JAXBContext jaxbContext = JAXBContext.newInstance(contextTypes);
        configurator.configure(jaxbContext);

        final Marshaller marshaller = jaxbContext.createMarshaller();
        {
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattingOutput);

            //
            if (encoding != null) {
                marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
            }

            //
            configurator.configure(marshaller);
        }

        //
        marshaller.marshal(object, outputStream);
        outputStream.flush();
    } catch (Exception e) {
        if (exceptionHandler != null) {
            exceptionHandler.handleException(e);
        }
    }
}

From source file:org.opencds.knowledgeRepository.SimpleKnowledgeRepository.java

public static synchronized Marshaller getRequiredMarshallerInstanceForMarshallerClassCache(String className,
        JAXBContext jaxbContext) throws DSSRuntimeExceptionFault {
    Marshaller marshallerInstance = myPayloadCreatorClassNameToMarshallerInstanceCache.get(className);
    if (marshallerInstance == null) {
        log.debug(className + ": creating marshaller instance");
        try {//from  w  w  w.  j  a  v  a 2s  .  c o  m

            marshallerInstance = jaxbContext.createMarshaller();
            marshallerInstance.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshallerInstance.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        } catch (JAXBException e) {
            throw new DSSRuntimeExceptionFault("requested Marshaller for className: " + className
                    + " created JAXBException: " + e.getMessage());
        }
        myPayloadCreatorClassNameToMarshallerInstanceCache.putIfAbsent(className, marshallerInstance);
        return marshallerInstance;
    } else {
        log.debug(className + ": using marshaller instance from cache");
        return marshallerInstance;
    }
}

From source file:org.openengsb.connector.promreport.internal.ProcessFileStore.java

private void createFile(File mxmlFile, String processId) {
    LOGGER.debug(String.format("create file %s for process %s", mxmlFile.getName(), processId));
    try {/*from   www .  j a v a2s  . c o m*/
        if (mxmlFile.createNewFile()) {
            Marshaller m = jaxbContext.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            m.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION,
                    "http://is.tm.tue.nl/research/processmining/WorkflowLog.xsd");
            m.setSchema(schema);
            m.marshal(createWorkflowLogTemplate(processId), mxmlFile);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.openengsb.connector.promreport.internal.ProcessFileStore.java

private Marshaller createMarshaller() throws JAXBException {
    Marshaller m = jaxbContext.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.setProperty(Marshaller.JAXB_FRAGMENT, true);
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    m.setSchema(schema);//from  ww w  . ja v a2 s. c  o  m
    return m;
}

From source file:org.openestate.io.casa_it.CasaItUtils.java

/**
 * Creates a {@link Marshaller} to write JAXB objects into XML.
 *
 * @param encoding//w  ww.  jav  a2  s .  c  om
 * encoding of written XML
 *
 * @param formatted
 * if written XML is pretty printed
 *
 * @return
 * created marshaller
 *
 * @throws JAXBException
 * if a problem with JAXB occured
 */
public static Marshaller createMarshaller(String encoding, boolean formatted) throws JAXBException {
    Marshaller m = getContext().createMarshaller();
    m.setProperty(Marshaller.JAXB_ENCODING, encoding);
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
    m.setEventHandler(new XmlValidationHandler());
    return m;
}

From source file:org.opennms.core.xml.JaxbUtils.java

public static Marshaller getMarshallerFor(final Object obj, final JAXBContext jaxbContext) {
    final Class<?> clazz = (Class<?>) (obj instanceof Class<?> ? obj : obj.getClass());

    Map<Class<?>, Marshaller> marshallers = m_marshallers.get();
    if (jaxbContext == null) {
        if (marshallers == null) {
            marshallers = new WeakHashMap<Class<?>, Marshaller>();
            m_marshallers.set(marshallers);
        }//from  w  w  w  . ja va  2 s. c  om
        if (marshallers.containsKey(clazz)) {
            LOG.trace("found unmarshaller for {}", clazz);
            return marshallers.get(clazz);
        }
    }
    LOG.trace("creating unmarshaller for {}", clazz);

    try {
        final JAXBContext context;
        if (jaxbContext == null) {
            context = getContextFor(clazz);
        } else {
            context = jaxbContext;
        }
        final Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        if (context.getClass().getName().startsWith("org.eclipse.persistence.jaxb")) {
            marshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER,
                    new EmptyNamespacePrefixMapper());
            marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, true);
        }
        final Schema schema = getValidatorFor(clazz);
        marshaller.setSchema(schema);
        if (jaxbContext == null)
            marshallers.put(clazz, marshaller);

        return marshaller;
    } catch (final JAXBException e) {
        throw EXCEPTION_TRANSLATOR.translate("creating XML marshaller", e);
    }
}

From source file:org.openo.sdnhub.common.restconf.SerializeUtil.java

/**
 * Change the object to XML.<br>//from  w w  w. j  a va 2 s.c  o m
 *
 * @param obj the object to be changed
 * @return the XML that object is changed to
 * @service ServiceException
 * @since SDNHUB 0.5
 */
public static String toXml(Object obj) throws ServiceException {
    String sError = "toXml failed.";
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        return writer.toString();
    } catch (JAXBException e) {
        LOGGER.error(sError, e);
        throw new ServiceException(sError, e);
    }
}

From source file:org.overlord.sramp.server.atom.workspaces.AbstractWorkspaceTest.java

/**
 * Marshalls the app service to XML./*  w  w w . j a  v  a  2  s  .c o m*/
 * @param appService
 */
public static String marshall(AppService appService) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(AppService.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$
    StringWriter writer = new StringWriter();
    JAXBElement<AppService> element = new JAXBElement<AppService>(new QName("", "app:service", "app"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            AppService.class, appService);
    marshaller.marshal(element, writer);
    return writer.toString();
}

From source file:org.ow2.aspirerfid.ide.masterdata.tools.MasterDataCaptureClient.java

/**
 * Send the given EPCISDocumentType to the repository for capturing.
 * //from   ww  w  . j  av  a2  s . c om
 * @param epcisDoc
 *            The EPCISDocument containing a list of events inside the
 *            EPCISBody element.
 * @return The HTTP response code from the repository.
 * @throws IOException
 *             If an error sending the document occurred.
 * @throws JAXBException
 *             If an error serializing the given document into XML occurred.
 */
public int capture(final EPCISMasterDataDocumentType epcisMasterDataDoc) throws IOException, JAXBException {
    StringWriter writer = new StringWriter();
    ObjectFactory objectFactory = new ObjectFactory();
    JAXBContext context = JAXBContext.newInstance("org.ow2.aspirerfid.commons.epcis.model");
    JAXBElement<EPCISMasterDataDocumentType> item = objectFactory
            .createEPCISMasterDataDocument(epcisMasterDataDoc);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(item, writer);

    log.debug("Jaxb data: \n" + writer.toString());

    return capture(writer.toString());
}