Java tutorial
package com.faceye.feature.util; /******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ import java.io.StringReader; import java.io.StringWriter; import java.util.Collection; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlAnyElement; import javax.xml.namespace.QName; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Jaxb2.0XML<->Java ObjectMapper. * * ???RootClass. * ?RootCollection. * * @author calvin */ public class JaxbMapper { private static Logger logger = LoggerFactory.getLogger(JaxbMapper.class); private static ConcurrentMap<Class, JAXBContext> jaxbContexts = new ConcurrentHashMap<Class, JAXBContext>(); /** * Java Object->Xml without encoding. */ public static String toXml(Object root) { Class clazz = Reflections.getUserClass(root); String xml = toXml(root, clazz, null); return xml; } /** * Java Object->Xml with encoding. */ public static String toXml(Object root, String encoding) { Class clazz = Reflections.getUserClass(root); return toXml(root, clazz, encoding); } /** * Java Object->Xml with encoding. */ public static String toXml(Object root, Class clazz, String encoding) { try { StringWriter writer = new StringWriter(); createMarshaller(clazz, encoding).marshal(root, writer); return writer.toString(); } catch (JAXBException e) { throw Exceptions.unchecked(e); } } /** * Java Collection->Xml without encoding, ?Root ElementCollection. */ public static String toXml(Collection<?> root, String rootName, Class clazz) { return toXml(root, rootName, clazz, null); } /** * Java Collection->Xml with encoding, ?Root ElementCollection. */ public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) { try { CollectionWrapper wrapper = new CollectionWrapper(); wrapper.collection = root; JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName), CollectionWrapper.class, wrapper); StringWriter writer = new StringWriter(); createMarshaller(clazz, encoding).marshal(wrapperElement, writer); return writer.toString(); } catch (JAXBException e) { throw Exceptions.unchecked(e); } } /** * Xml->Java Object. */ public static <T> T fromXml(String xml, Class<T> clazz) { try { StringReader reader = new StringReader(xml); return (T) createUnmarshaller(clazz).unmarshal(reader); } catch (JAXBException e) { logger.error(">>FaceYe form xml 2 class exception:", e); throw Exceptions.unchecked(e); } } /** * Marshallerencoding(?null). * ???pooling */ public static Marshaller createMarshaller(Class clazz, String encoding) { try { JAXBContext jaxbContext = getJaxbContext(clazz); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); if (StringUtils.isNotBlank(encoding)) { marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); } // marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, ""); // marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, ""); // marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() { // @Override // public String[] getPreDeclaredNamespaceUris() { // return new String[] { WellKnownNamespace.XML_SCHEMA_INSTANCE }; // } // // @Override // public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { // if (namespaceUri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE)) // return "xsi"; // if (namespaceUri.equals(WellKnownNamespace.XML_SCHEMA)) // return "xs"; // if (namespaceUri.equals(WellKnownNamespace.XML_MIME_URI)) // return "xmime"; // return suggestion; // // } // }); return marshaller; } catch (JAXBException e) { logger.error(">>FaceYe form xml 2 class exception:", e); throw Exceptions.unchecked(e); } } /** * UnMarshaller. * ???pooling */ public static Unmarshaller createUnmarshaller(Class clazz) { try { JAXBContext jaxbContext = getJaxbContext(clazz); return jaxbContext.createUnmarshaller(); } catch (JAXBException e) { logger.error(">>FaceYe form xml 2 class exception:", e); throw Exceptions.unchecked(e); } } protected static JAXBContext getJaxbContext(Class clazz) { Validate.notNull(clazz, "'clazz' must not be null"); JAXBContext jaxbContext = jaxbContexts.get(clazz); if (jaxbContext == null) { try { jaxbContext = JAXBContext.newInstance(clazz, CollectionWrapper.class); // jaxbContext = JAXBContext.newInstance(clazz, clazz); jaxbContexts.putIfAbsent(clazz, jaxbContext); } catch (JAXBException ex) { logger.error(">>FaceYe form xml 2 class exception:", ex); throw new RuntimeException( "Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex); } } return jaxbContext; } /** * ?Root Element Collection. */ public static class CollectionWrapper { @XmlAnyElement protected Collection<?> collection; } }