Java XML JAXB Marshaller createMarshaller(Class clazz, Map settings)

Here you can find the source of createMarshaller(Class clazz, Map settings)

Description

create Marshaller

License

Open Source License

Parameter

Parameter Description
clazz a parameter
encoding a parameter

Declaration

public static Marshaller createMarshaller(Class<?> clazz, Map<String, Object> settings) 

Method Source Code

//package com.java2s;
/*//from   w ww.j ava  2 s.com
* Project Name: xinyunlian-ecom
* File Name: JaxbUtils.java
* Class Name: JaxbUtils
*
* Copyright 2014 Hengtian Software Inc
*
* Licensed under the Hengtiansoft
*
* http://www.hengtiansoft.com
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

public class Main {
    private static ConcurrentMap<Class<?>, JAXBContext> jaxbMap = new ConcurrentHashMap<Class<?>, JAXBContext>();
    public static final String JAXB_ENCODING = "jaxb.encoding";

    public static final String JAXB_FRAGMENT = "jaxb.fragment";

    /**
     * @param clazz
     * @param encoding
     * @return
     */
    public static Marshaller createMarshaller(Class<?> clazz, Map<String, Object> settings) {
        try {
            JAXBContext jaxbContext = getJaxbContext(clazz);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, (String) settings.get(JAXB_ENCODING));
            //            if (StringUtils.isNotEmpty((String)settings.get(JAXB_ENCODING))) {
            //                marshaller.setProperty(Marshaller.JAXB_ENCODING, (String)settings.get(JAXB_ENCODING));
            //            }
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            //            if( null != settings.get(JAXB_FRAGMENT)){
            //                marshaller.setProperty(Marshaller.JAXB_FRAGMENT, (Boolean)settings.get(JAXB_FRAGMENT));
            //            }
            return marshaller;
        } catch (JAXBException e) {
            // LOGGER.error("Error occured when creating marshaller.", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * @param clazz
     * @return
     */
    private static JAXBContext getJaxbContext(Class<?> clazz) {
        JAXBContext jaxbContext = jaxbMap.get(clazz);
        if (jaxbContext == null) {
            try {
                jaxbContext = JAXBContext.newInstance(clazz);
                jaxbMap.putIfAbsent(clazz, jaxbContext);
            } catch (JAXBException ex) {
                throw new RuntimeException("Could not instantiate JAXBContext.", ex);
            }
        }
        return jaxbContext;
    }
}

Related

  1. createJAXBMarshaller(JAXBContext jaxbContext)
  2. createMarshall(String pkgName)
  3. createMarshaller()
  4. createMarshaller(Class clazz)
  5. createMarshaller(JAXBContext context)
  6. createMarshaller(JAXBContext ctx, boolean indent)
  7. createMarshaller(Object object)