Java XML JAXB Marshaller getMarshaller(String jaxbContext, boolean pretty)

Here you can find the source of getMarshaller(String jaxbContext, boolean pretty)

Description

creates a marshaller on pooled JAXBContext instances.

License

Open Source License

Parameter

Parameter Description
jaxbContext the context on which to create a marshaller.
pretty if formatted or not.

Exception

Parameter Description
JAXBException when unable to create the marshaller.

Return

the marshaller.

Declaration

private static Marshaller getMarshaller(String jaxbContext, boolean pretty) throws JAXBException 

Method Source Code

//package com.java2s;
/*/* w  ww .  j  a  va  2  s . c om*/
 * Copyright (C) 2007 ETH Zurich
 *
 * This file is part of Fosstrak (www.fosstrak.org).
 *
 * Fosstrak is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * Fosstrak is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Fosstrak; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {
    private static final Map<String, JAXBContext> contexts = new ConcurrentHashMap<String, JAXBContext>();

    /**
     * creates a marshaller on pooled JAXBContext instances.
     * @param jaxbContext the context on which to create a marshaller.
     * @param pretty if formatted or not.
     * @return the marshaller.
     * @throws JAXBException when unable to create the marshaller.
     */
    private static Marshaller getMarshaller(String jaxbContext, boolean pretty) throws JAXBException {
        JAXBContext context = null;
        synchronized (contexts) {
            context = contexts.get(jaxbContext);
            if (null == context) {
                context = JAXBContext.newInstance(jaxbContext);
                contexts.put(jaxbContext, context);
            }
        }
        Marshaller marshaller = null;
        synchronized (context) {
            marshaller = context.createMarshaller();
        }
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(pretty));
        return marshaller;
    }
}

Related

  1. getMarshaller()
  2. getMarshaller(boolean prettyFormat)
  3. getMarshaller(Class c)
  4. getMarshaller(Class clazz)
  5. getMarshaller(final String contextPath)
  6. getSchemaLocation(@Nonnull final Marshaller aMarshaller)
  7. getSunNamespacePrefixMapper(@Nonnull final Marshaller aMarshaller)
  8. isInternalSunJAXB2Marshaller(@Nullable final Marshaller aMarshaller)
  9. isSunCanonicalization(@Nonnull final Marshaller aMarshaller)