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:org.apache.nifi.cluster.flow.impl.DataFlowDaoImpl.java

private void writeClusterMetadata(final ClusterMetadata clusterMetadata, final OutputStream os)
        throws IOException, JAXBException {
    // write cluster metadata to output stream
    final Marshaller marshaller = ClusterMetadata.jaxbCtx.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    marshaller.marshal(clusterMetadata, os);
}

From source file:org.apli.modelbeans.facturacion.cfdi.CFDv32.java

@Override
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/cfdv32.xsd");
    byte[] xmlHeaderBytes = XML_HEADER.getBytes("UTF8");
    out.write(xmlHeaderBytes);//  w  w  w  . j av a  2  s.co m
    m.marshal(document, out);
}

From source file:org.artificer.server.atom.workspaces.AbstractWorkspaceTest.java

/**
 * Marshalls the app service to XML./*w w w.j a va2 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");
    StringWriter writer = new StringWriter();
    JAXBElement<AppService> element = new JAXBElement<AppService>(new QName("", "app:service", "app"),
            AppService.class, appService);
    marshaller.marshal(element, writer);
    return writer.toString();
}

From source file:org.broadleafcommerce.common.sitemap.service.SiteMapBuilder.java

/**
 * Method takes in a valid JAXB object (e.g. has a RootElement) and persists it to 
 * the temporary directory associated with this builder. 
 * /*w w w . ja v  a  2  s.co  m*/
 * @param fileName
 */
protected void persistXMLDocument(String fileName, Object xmlObject) {

    try {
        JAXBContext context = JAXBContext.newInstance(xmlObject.getClass());
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.setProperty(Marshaller.JAXB_FRAGMENT, true);

        File file = new File(fileWorkArea.getFilePathLocation() + fileName);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Persisting SiteMap document " + file.getAbsolutePath());
        }

        if (!file.exists()) {
            file.createNewFile();
        }
        Writer writer = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
        writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        m.marshal(xmlObject, writer);
        writer.close();
    } catch (IOException ioe) {
        LOG.error("IOException occurred persisting XML Document", ioe);
        throw new RuntimeException("Error persisting XML document when trying to build Sitemap", ioe);
    } catch (JAXBException je) {
        LOG.error("JAXBException occurred persisting XML Document", je);
        throw new RuntimeException("Error persisting XML document when trying to build Sitemap", je);
    }
}

From source file:org.codehaus.enunciate.modules.xfire.EnunciatedJAXWSOperationBinding.java

public void writeMessage(OutMessage message, XMLStreamWriter writer, MessageContext context) throws XFireFault {
    if (this.responseInfo == null) {
        throw new XFireFault("Unable to write message: no response info was found!", XFireFault.SENDER);
    }/*from ww w  . j ava2s  .  co  m*/

    Class beanClass = this.responseInfo.getBeanClass();
    Object[] params = (Object[]) message.getBody();
    Object bean;
    if (this.responseInfo.isBare()) {
        //bare response.  we don't need to wrap it up.
        bean = new JAXBElement(this.responseInfo.getMessageInfo().getName(), this.responseInfo.getBeanClass(),
                params[0]);
    } else {
        try {
            bean = beanClass.newInstance();
        } catch (Exception e) {
            throw new XFireFault("Problem instantiating response wrapper " + beanClass.getName() + ".", e,
                    XFireFault.RECEIVER);
        }

        PropertyDescriptor[] properties = this.responseInfo.getPropertyOrder();
        if (properties.length > 0) { //no properties implies a void method...
            if (properties.length != params.length) {
                throw new XFireFault("There are " + params.length + " parameters to the out message but "
                        + properties.length + " properties on " + beanClass.getName(), XFireFault.RECEIVER);
            }

            for (int i = 0; i < properties.length; i++) {
                PropertyDescriptor descriptor = properties[i];
                try {
                    descriptor.getWriteMethod().invoke(bean, params[i]);
                } catch (IllegalAccessException e) {
                    throw new XFireFault("Problem with property " + descriptor.getName() + " on "
                            + beanClass.getName() + ".", e, XFireFault.RECEIVER);
                } catch (InvocationTargetException e) {
                    throw new XFireFault("Problem with property " + descriptor.getName() + " on "
                            + beanClass.getName() + ".", e, XFireFault.RECEIVER);
                }
            }
        }
    }

    try {
        Marshaller marshaller = this.jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.setAttachmentMarshaller(new AttachmentMarshaller(context));
        marshaller.marshal(bean, writer);
    } catch (JAXBException e) {
        throw new XFireRuntimeException("Unable to marshal type.", e);
    }
}

From source file:org.codehaus.enunciate.modules.xfire.EnunciatedJAXWSWebFaultHandler.java

@Override
protected void handleFault(MessageContext context, XFireFault fault, Throwable cause, MessagePartInfo faultPart)
        throws XFireFault {
    Object faultBean = getFaultBean(cause, faultPart, context);
    if (faultBean != null) {
        try {//from www.  jav a2 s .  c  om
            JAXBContext jaxbContext = JAXBContext.newInstance(faultBean.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
            marshaller.setAttachmentMarshaller(new AttachmentMarshaller(context));
            JDOMStreamWriter writer = new JDOMStreamWriter(fault.getDetail());
            marshaller.marshal(faultBean, writer);
        } catch (JAXBException e) {
            LOG.error("Unable to marshal the fault bean of type " + faultBean.getClass().getName() + ".", e);
            //fall through... let the fault be handled by something else...
        }
    }
}

From source file:org.codice.ddf.security.handler.anonymous.AnonymousHandler.java

/**
 * Returns the UsernameToken marshalled as a String so that it can be attached to the
 * {@link org.codice.ddf.security.handler.api.HandlerResult} object.
 *
 * @param usernameTokenType/*from   w ww . ja  v a  2 s .c o  m*/
 * @return String
 */
private synchronized String getUsernameTokenElement(UsernameTokenType usernameTokenType) {
    Writer writer = new StringWriter();
    Marshaller marshaller = null;
    if (utContext != null) {
        try {
            marshaller = utContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        } catch (JAXBException e) {
            LOGGER.error("Exception while creating UsernameToken marshaller.", e);
        }

        JAXBElement<UsernameTokenType> usernameTokenElement = new JAXBElement<UsernameTokenType>(
                new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
                        "UsernameToken"),
                UsernameTokenType.class, usernameTokenType);

        if (marshaller != null) {
            try {
                marshaller.marshal(usernameTokenElement, writer);
            } catch (JAXBException e) {
                LOGGER.error("Exception while writing username token.", e);
            }
        }
    }

    return writer.toString();
}

From source file:org.codice.ddf.security.handler.api.BSTAuthenticationToken.java

/**
 * Creates a binary security token based on the provided credential.
 *///from   ww w .  jav a 2s .  c om
private synchronized String getBinarySecurityToken(String credential) {
    Writer writer = new StringWriter();

    Marshaller marshaller = null;

    BinarySecurityTokenType binarySecurityTokenType = createBinarySecurityTokenType(credential);
    JAXBElement<BinarySecurityTokenType> binarySecurityTokenElement = new JAXBElement<BinarySecurityTokenType>(
            new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
                    "BinarySecurityToken"),
            BinarySecurityTokenType.class, binarySecurityTokenType);

    if (BINARY_TOKEN_CONTEXT != null) {
        try {
            marshaller = BINARY_TOKEN_CONTEXT.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        } catch (JAXBException e) {
            LOGGER.error("Exception while creating UsernameToken marshaller.", e);
        }

        if (marshaller != null) {
            try {
                marshaller.marshal(binarySecurityTokenElement, writer);
            } catch (JAXBException e) {
                LOGGER.error("Exception while writing username token.", e);
            }
        }
    }

    String binarySecurityToken = writer.toString();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Binary Security Token: " + binarySecurityToken);
    }

    return binarySecurityToken;
}

From source file:org.csstudio.diirt.util.core.preferences.pojo.ChannelAccess.java

/**
 * Store this instance into the given folder.
 *
 * @param confDir The current DIIRT configuration directory.
 * @throws IOException If problems occurred saving data into file or creating the folder structure.
 * @throws JAXBException In case the given instance cannot be marshalled.
 *///  w  w w.j  a va2s .c o m
public void toFile(File confDir) throws IOException {

    File dsDir = new File(confDir, DataSources.DATASOURCES_DIR);
    File caDir = new File(dsDir, CA_DIR);

    FileUtils.forceMkdir(caDir);
    final ClassLoader orig = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(JAXBContext.class.getClassLoader());
    try {
        JAXBContext context = JAXBContext.newInstance(ChannelAccess.class.getPackageName(),
                this.getClass().getClassLoader());
        Marshaller marshaller = context.createMarshaller();

        try (Writer writer = new FileWriter(new File(caDir, CA_FILE))) {

            writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n");

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            marshaller.marshal(this, writer);

        }

    } catch (JAXBException e) {
        throw new IOException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(orig);
    }

}

From source file:org.csstudio.diirt.util.core.preferences.pojo.DataSources.java

/**
 * Store this instance into the given folder.
 *
 * @param confDir The current DIIRT configuration directory.
 * @throws IOException   If problems occurred saving data into file or creating the folder structure.
 * @throws JAXBException In case the given instance cannot be marshalled.
 *//* ww w  . j  ava 2s  .  c  om*/
public void toFile(File confDir) throws IOException {

    File dsDir = new File(confDir, DATASOURCES_DIR);

    FileUtils.forceMkdir(dsDir);
    final ClassLoader orig = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(JAXBContext.class.getClassLoader());
    try {
        JAXBContext context = JAXBContext.newInstance(DataSources.class.getPackageName(),
                this.getClass().getClassLoader());
        Marshaller marshaller = context.createMarshaller();

        try (Writer writer = new FileWriter(new File(dsDir, DATASOURCES_FILE))) {

            writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n");

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            marshaller.marshal(this, writer);

        }
    } catch (JAXBException e) {
        throw new IOException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(orig);
    }

}