Java tutorial
/* This file is part of Moose. Moose is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Moose 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 Moose. If not, see <http://www.gnu.org/licenses/>. */ package com.quigley.moose.mapping; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.Reader; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.jdom.Document; import org.jdom.Element; import org.jdom.Namespace; import org.jdom.input.DOMBuilder; import org.jdom.input.SAXBuilder; import org.springframework.beans.factory.annotation.Required; import com.quigley.moose.handler.EnumHandler; import com.quigley.moose.handler.TypeHandler; import com.quigley.moose.handler.TypeHandlerCollection; import com.quigley.moose.mapping.naming.DefaultMappingNameGenerator; import com.quigley.moose.mapping.naming.MappingNameGenerator; import com.quigley.moose.schema.naming.DefaultSchemaNameGenerator; import com.quigley.moose.schema.naming.SchemaNameGenerator; public class Mapping { public Mapping() { mappingNameGenerator = new DefaultMappingNameGenerator(); schemaNameGenerator = new DefaultSchemaNameGenerator(); } public void marshall(Object object, MarshallingContext ctx) { marshall(object, null, ctx); } public void marshall(Object object, String alternateName, MarshallingContext ctx) { if (hasMappedType(object.getClass())) { MappedType mappedType = getMappedType(object.getClass()); mappedType.marshall(object, alternateName, this, ctx); } else { throw new MappingException("No class mapping for class named '" + object.getClass().getName() + "'."); } } public Object unmarshall(org.w3c.dom.Element domElement) { DOMBuilder domBuilder = new DOMBuilder(); Element element; try { element = domBuilder.build((org.w3c.dom.Element) domElement); } catch (Exception e) { throw new MappingException("There was an error in the input document.", e); } return unmarshall(element); } public Object unmarshall(String text) { SAXBuilder saxBuilder = new SAXBuilder(); ByteArrayInputStream baiStream = new ByteArrayInputStream(text.getBytes()); Document document; try { document = saxBuilder.build(baiStream); } catch (Exception e) { throw new MappingException("There was an error in the input document.", e); } return unmarshall(document.getRootElement()); } public Object unmarshall(InputStream inputStream) { SAXBuilder saxBuilder = new SAXBuilder(); Document document; try { document = saxBuilder.build(inputStream); } catch (Exception e) { throw new MappingException("There was an error in the input document.", e); } return unmarshall(document.getRootElement()); } public Object unmarshall(Reader reader) { SAXBuilder saxBuilder = new SAXBuilder(); Document document; try { document = saxBuilder.build(reader); } catch (Exception e) { throw new MappingException("There was an error in the input document.", e); } return unmarshall(document.getRootElement()); } protected Object unmarshall(Element element) { return unmarshall(element, null); } public Object unmarshall(Element rootElement, Class<?> clazz) { String xmlName = rootElement.getName(); if (!rootElement.getNamespace().equals(getNamespace())) { throw new MappingException("Input namespace and mapped namespace do not match. {" + rootElement.getNamespaceURI() + " != " + getNamespace().getURI() + "}"); } // Find class mapping. MappedType mappedType = null; if (hasMappedType(xmlName)) { mappedType = getMappedType(xmlName); } else { if (clazz != null && hasMappedType(clazz)) { mappedType = getMappedType(clazz); } } if (mappedType == null) { throw new MappingException("Could not find a mapped class for XML name '" + xmlName + "'."); } return mappedType.unmarshall(rootElement, this); } public Set<Class<?>> getMappedClasses() { return classIndex.keySet(); } public String namespaceify(String name) { String outputName = name; if (getXmlPrefix() != null) { outputName = getXmlPrefix() + ":" + outputName; } return outputName; } public boolean hasMappedType(String xmlName) { if (xmlNameIndex.containsKey(xmlName)) { return true; } else { return false; } } public MappedType getMappedType(String xmlName) { if (hasMappedType(xmlName)) { return xmlNameIndex.get(xmlName); } else { throw new MappingException("No mapped class for XML name '" + xmlName + "'."); } } public boolean hasMappedType(Class<?> clazz) { if (classIndex.containsKey(clazz)) { return true; } else { return false; } } public MappedType getMappedType(Class<?> clazz) { if (hasMappedType(clazz)) { return classIndex.get(clazz); } else { throw new MappingException("No mapped class for class name '" + clazz.getName() + "'."); } } public boolean hasTypeHandler(Class<?> clazz) { if (clazz.isEnum()) { return true; } if (typeHandlers.containsKey(clazz)) { return true; } else { return false; } } public TypeHandler getTypeHandler(Class<?> clazz) { if (!clazz.isEnum()) { if (hasTypeHandler(clazz)) { return typeHandlers.get(clazz); } else { throw new MappingException("No type handler for class name '" + clazz.getName() + "'."); } } else { if (!typeHandlers.containsKey(clazz)) { TypeHandler typeHandler = new EnumHandler(clazz, mappingNameGenerator); typeHandlers.put(clazz, typeHandler); return typeHandler; } return typeHandlers.get(clazz); } } public Namespace getNamespace() { Namespace namespace = Namespace.NO_NAMESPACE; if (xmlNamespace != null) { if (xmlPrefix != null) { namespace = Namespace.getNamespace(xmlPrefix, xmlNamespace); } else { namespace = Namespace.getNamespace(xmlNamespace); } } return namespace; } public String getXmlNamespace() { return xmlNamespace; } public void setXmlNamespace(String xmlNamespace) { this.xmlNamespace = xmlNamespace; } public String getXmlPrefix() { return xmlPrefix; } public void setXmlPrefix(String xmlPrefix) { this.xmlPrefix = xmlPrefix; } public boolean isUseXsiType() { return useXsiType; } public void setUseXsiType(boolean useXsiType) { this.useXsiType = useXsiType; } public MappingNameGenerator getMappingNameGenerator() { return mappingNameGenerator; } public void setMappingNameGenerator(MappingNameGenerator mappingNameGenerator) { this.mappingNameGenerator = mappingNameGenerator; } public SchemaNameGenerator getSchemaNameGenerator() { return schemaNameGenerator; } public void setSchemaNameGenerator(SchemaNameGenerator schemaNameGenerator) { this.schemaNameGenerator = schemaNameGenerator; } public List<MappedType> getMappedTypes() { return mappedTypes; } @Required public void setMappedTypes(List<MappedType> mappedTypes) { this.mappedTypes = mappedTypes; index(); } private void index() { xmlNameIndex = new HashMap<String, MappedType>(); for (MappedType mappedType : mappedTypes) { if (mappedType instanceof MappedClass) { MappedClass mappedClass = (MappedClass) mappedType; if (mappedClass.getXmlName() != null) { xmlNameIndex.put(mappedClass.getXmlName(), mappedType); } } } classIndex = new HashMap<Class<?>, MappedType>(); for (MappedType mappedType : mappedTypes) { classIndex.put(mappedType.getClazz(), mappedType); } } public List<TypeHandlerCollection> getTypeHandlerCollections() { return typeHandlerCollections; } @Required public void setTypeHandlerCollections(List<TypeHandlerCollection> typeHandlerCollections) { this.typeHandlerCollections = typeHandlerCollections; configureTypeHandlers(); } protected void configureTypeHandlers() { typeHandlers = new HashMap<Class<?>, TypeHandler>(); for (TypeHandlerCollection typeHandlerCollection : typeHandlerCollections) { typeHandlers.putAll(typeHandlerCollection.getTypeHandlers()); } } protected String xmlNamespace; protected String xmlPrefix; protected boolean useXsiType; protected MappingNameGenerator mappingNameGenerator; protected SchemaNameGenerator schemaNameGenerator; protected List<MappedType> mappedTypes; protected Map<String, MappedType> xmlNameIndex; protected Map<Class<?>, MappedType> classIndex; protected List<TypeHandlerCollection> typeHandlerCollections; protected Map<Class<?>, TypeHandler> typeHandlers; public static Namespace xsiNamespace = Namespace.getNamespace("http://www.w3.org/2001/XMLSchema-instance"); }