jaxbcontextimpl.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 927 行 · 第 1/2 页
JAVA
927 行
/* * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source 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, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Emil Ong, Adam Megacz */package com.caucho.jaxb;import com.caucho.jaxb.skeleton.*;import com.caucho.jaxb.property.*;import com.caucho.server.util.CauchoSystem;import com.caucho.util.L10N;import com.caucho.xml.QNode;import org.w3c.dom.Node;import javax.activation.DataHandler;import javax.xml.bind.*;import javax.xml.bind.annotation.*;import javax.xml.datatype.*;import javax.xml.namespace.QName;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLOutputFactory;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.XMLStreamWriter;import javax.xml.transform.Source;import javax.xml.transform.Result;import java.awt.Image;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.LineNumberReader;import java.lang.reflect.GenericArrayType;import java.lang.reflect.Method;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.math.BigDecimal;import java.math.BigInteger;import java.net.URI;import java.util.*;/** * Entry point to API */public class JAXBContextImpl extends JAXBContext { public static final String XML_SCHEMA_NS = "http://www.w3.org/2001/XMLSchema"; public static final String TARGET_NAMESPACE = "com.caucho.jaxb.targetNamespace"; static final ValidationEventHandler DEFAULT_VALIDATION_EVENT_HANDLER = new DefaultValidationEventHandler(); private static final L10N L = new L10N(JAXBContextImpl.class); private static final HashSet<Class> _specialClasses = new HashSet<Class>(); static { _specialClasses.add(Object.class); _specialClasses.add(Class.class); _specialClasses.add(String.class); _specialClasses.add(Double.class); _specialClasses.add(Float.class); _specialClasses.add(Integer.class); _specialClasses.add(Long.class); _specialClasses.add(Boolean.class); _specialClasses.add(Character.class); _specialClasses.add(Short.class); _specialClasses.add(Byte.class); _specialClasses.add(BigDecimal.class); _specialClasses.add(BigInteger.class); _specialClasses.add(QName.class); _specialClasses.add(Date.class); _specialClasses.add(Calendar.class); _specialClasses.add(XMLGregorianCalendar.class); _specialClasses.add(UUID.class); _specialClasses.add(DataHandler.class); } private String _targetNamespace = null; private String[] _packages; private JAXBIntrospector _jaxbIntrospector; private XMLInputFactory _staxInputFactory; private XMLOutputFactory _staxOutputFactory; private final ArrayList<ObjectFactorySkeleton> _objectFactories = new ArrayList<ObjectFactorySkeleton>(); private final HashMap<String,Object> _properties = new HashMap<String,Object>(); private final LinkedHashMap<Class,ClassSkeleton> _classSkeletons = new LinkedHashMap<Class,ClassSkeleton>(); private final LinkedHashMap<Class,JAXBElementSkeleton> _jaxbElementSkeletons = new LinkedHashMap<Class,JAXBElementSkeleton>(); private final HashMap<QName,ClassSkeleton> _roots = new HashMap<QName,ClassSkeleton>(); private final HashMap<QName,ClassSkeleton> _types = new HashMap<QName,ClassSkeleton>(); private final ArrayList<EnumProperty> _enums = new ArrayList<EnumProperty>(); private HashSet<Class> _pendingSkeletons = new HashSet<Class>(); private DynamicJAXBElementSkeleton _dynamicSkeleton; private Property _laxAnyTypeProperty = null; private boolean _isDiscoveryFinished = false; public static JAXBContext createContext(String contextPath, ClassLoader classLoader, Map<String,?> properties) throws JAXBException { return new JAXBContextImpl(contextPath, classLoader, properties); } public JAXBContextImpl(String contextPath, ClassLoader classLoader, Map<String,?> properties) throws JAXBException { _jaxbIntrospector = new JAXBIntrospectorImpl(this); StringTokenizer st = new StringTokenizer(contextPath, ":"); if (properties != null) _targetNamespace = (String) properties.get(TARGET_NAMESPACE); do { String packageName = st.nextToken(); loadPackage(packageName, classLoader); } while (st.hasMoreTokens()); init(properties); } public static JAXBContext createContext(Class []classes, Map<String,?> properties) throws JAXBException { return new JAXBContextImpl(classes, properties); } public JAXBContextImpl(Class[] classes, Map<String,?> properties) throws JAXBException { _jaxbIntrospector = new JAXBIntrospectorImpl(this); _packages = new String[0]; if (properties != null) _targetNamespace = (String) properties.get(TARGET_NAMESPACE); for(Class c : classes) { if (c.getName().endsWith(".ObjectFactory")) introspectObjectFactory(c); else if (! c.isPrimitive() && // XXX pull out to JAX-WS? ! c.isArray() && ! _specialClasses.contains(c)) createSkeleton(c); } init(properties); } private void init(Map<String,?> properties) throws JAXBException { if (properties != null) { for(Map.Entry<String,?> e : properties.entrySet()) setProperty(e.getKey(), e.getValue()); } DatatypeConverter.setDatatypeConverter(new DatatypeConverterImpl()); _dynamicSkeleton = new DynamicJAXBElementSkeleton(this); _isDiscoveryFinished = true; for (ClassSkeleton skeleton : _classSkeletons.values()) skeleton.postProcess(); } public boolean isDiscoveryFinished() { return _isDiscoveryFinished; } public Marshaller createMarshaller() throws JAXBException { return new MarshallerImpl(this); } public Unmarshaller createUnmarshaller() throws JAXBException { return new UnmarshallerImpl(this); } public Validator createValidator() throws JAXBException { throw new UnsupportedOperationException(); } XMLStreamReader getXMLStreamReader(InputStream is) throws XMLStreamException { if (_staxInputFactory == null) _staxInputFactory = XMLInputFactory.newInstance(); return _staxInputFactory.createXMLStreamReader(is); } public XMLInputFactory getXMLInputFactory() { if (_staxInputFactory == null) _staxInputFactory = XMLInputFactory.newInstance(); return _staxInputFactory; } public XMLOutputFactory getXMLOutputFactory() { if (_staxOutputFactory == null) { _staxOutputFactory = XMLOutputFactory.newInstance(); _staxOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); } return _staxOutputFactory; } public String getTargetNamespace() { return _targetNamespace; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("JAXBContext["); for (Class c : _classSkeletons.keySet()) sb.append(c.getName() + ":"); for (int i = 0; i < _packages.length; i++) { String p = _packages[i]; sb.append(p + (i < _packages.length - 1 ? ":" : "")); } sb.append("]"); return sb.toString(); } private void setProperty(String key, Object val) { _properties.put(key, val); } public Binder<Node> createBinder() { return (Binder<Node>) new BinderImpl(this); } public <T> Binder<T> createBinder(Class<T> domType) { if (! domType.equals(QNode.class)) throw new UnsupportedOperationException("Unsupported implementation: " + domType); return (Binder) new BinderImpl(this); } public JAXBIntrospector createJAXBIntrospector() { return _jaxbIntrospector; } public void generateSchema(SchemaOutputResolver outputResolver) throws IOException { Result result = outputResolver.createOutput("", "schema1.xsd"); XMLStreamWriter out = null; try { XMLOutputFactory factory = getXMLOutputFactory(); out = factory.createXMLStreamWriter(result); out.writeStartDocument("UTF-8", "1.0"); out.writeStartElement("xsd", "schema", XML_SCHEMA_NS); out.writeAttribute("version", "1.0"); generateSchemaWithoutHeader(out); out.writeEndElement(); // schema } catch (Exception e) { IOException ioException = new IOException(); ioException.initCause(e); throw ioException; } finally { try { out.close(); } catch (XMLStreamException e) { throw new IOException(e.toString()); } } } public void generateSchemaWithoutHeader(XMLStreamWriter out) throws JAXBException, XMLStreamException { for (ClassSkeleton skeleton : _classSkeletons.values()) skeleton.generateSchema(out); for (int i = 0; i < _enums.size(); i++) ((EnumProperty) _enums.get(i)).generateSchema(out); } public ClassSkeleton createSkeleton(Class c) throws JAXBException { ClassSkeleton skeleton = _classSkeletons.get(c); if (skeleton != null) return skeleton; if (Object.class.equals(c)) { skeleton = new AnyTypeSkeleton(this); _classSkeletons.put(c, skeleton); } else { // XXX if (c.isEnum() || c.isInterface()) { return null; /* throw new IllegalStateException(L.l("{0}: Can't create skeleton for an interface or enum", c.getName())); */ } skeleton = new ClassSkeleton(this, c); // Breadcrumb to prevent problems with recursion _classSkeletons.put(c, skeleton); _pendingSkeletons.add(c); skeleton.init(); _pendingSkeletons.remove(c); } return skeleton; } public ClassSkeleton getSkeleton(Class c) throws JAXBException { return createSkeleton(c); } public boolean hasSkeleton(Class c) { return _classSkeletons.containsKey(c); } public ClassSkeleton findSkeletonForClass(Class cl) throws JAXBException { return findSkeletonForClass(cl, _jaxbElementSkeletons); } public ClassSkeleton findSkeletonForClass(Class cl, Map<Class,? extends ClassSkeleton> map) { Class givenClass = cl; while (! cl.equals(Object.class)) { ClassSkeleton skeleton = map.get(cl); if (skeleton != null) return skeleton; cl = cl.getSuperclass(); } return null; } public ClassSkeleton findSkeletonForObject(Object obj) { if (obj instanceof JAXBElement) { JAXBElement element = (JAXBElement) obj; obj = element.getValue(); ClassSkeleton skeleton = findSkeletonForClass(obj.getClass(), _jaxbElementSkeletons); if (skeleton == null) skeleton = _dynamicSkeleton; return skeleton; } else return findSkeletonForClass(obj.getClass(), _classSkeletons); } /** * Finds all ClassSkeletons that are subclasses of the given class and * are root elements. **/ public List<ClassSkeleton> getRootElements(Class cl) { ArrayList<ClassSkeleton> list = new ArrayList<ClassSkeleton>(); for (Map.Entry<Class,ClassSkeleton> entry : _classSkeletons.entrySet()) { if (cl.isAssignableFrom(entry.getKey()) && entry.getValue().isRootElement()) list.add(entry.getValue()); } return list; } public Property getLaxAnyTypeProperty() throws JAXBException
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?