org.eclipse.persistence.jaxb.JAXBContextFactory Maven / Gradle / Ivy
Show all versions of eclipselink Show documentation
/*
* Copyright (c) 1998, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.jaxb;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.helpers.DefaultValidationEventHandler;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContext.JAXBContextInput;
import org.eclipse.persistence.jaxb.JAXBContext.ContextPathInput;
import org.eclipse.persistence.jaxb.JAXBContext.TypeMappingInfoInput;
import org.eclipse.persistence.jaxb.compiler.CompilerHelper;
import org.eclipse.persistence.jaxb.compiler.XMLProcessor;
import org.eclipse.persistence.jaxb.metadata.MetadataSource;
import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings;
import org.eclipse.persistence.oxm.MediaType;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
/**
*
* Purpose:An EclipseLink specific JAXBContextFactory. This class can be specified in a
* jaxb.properties file to make use of EclipseLink's JAXB 2.1 implementation.
*
* Responsibilities:
*
* - Create a JAXBContext from an array of Classes and a Properties object
* - Create a JAXBContext from a context path and a classloader
*
*
* This class is the entry point into in EclipseLink's JAXB 2.1 Runtime. It provides the required
* factory methods and is invoked by javax.xml.bind.JAXBContext.newInstance() to create new
* instances of JAXBContext. When creating a JAXBContext from a contextPath, the list of classes is
* derived either from an ObjectFactory class (schema-to-java) or a jaxb.index file
* (java-to-schema).
*
* @author mmacivor
* @see javax.xml.bind.JAXBContext
* @see org.eclipse.persistence.jaxb.JAXBContext
* @see org.eclipse.persistence.jaxb.compiler.Generator
*/
public class JAXBContextFactory {
/**
* @deprecated As of release 2.4, replaced by JAXBContextProperties.OXM_METADATA_SOURCE
* @see org.eclipse.persistence.jaxb.JAXBContextProperties#OXM_METADATA_SOURCE
*/
@Deprecated
public static final String ECLIPSELINK_OXM_XML_KEY = "eclipselink-oxm-xml";
/**
* @deprecated As of release 2.4, replaced by JAXBContextProperties.DEFAULT_TARGET_NAMESPACE
* @see org.eclipse.persistence.jaxb.JAXBContextProperties#DEFAULT_TARGET_NAMESPACE
*/
@Deprecated
public static final String DEFAULT_TARGET_NAMESPACE_KEY = "defaultTargetNamespace";
/**
* @deprecated As of release 2.4, replaced by JAXBContextProperties.ANNOTATION_HELPER
* @see org.eclipse.persistence.jaxb.JAXBContextProperties#ANNOTATION_HELPER
*/
@Deprecated
public static final String ANNOTATION_HELPER_KEY = "annotationHelper";
public static final String PKG_SEPARATOR = ".";
/**
* Create a JAXBContext on the array of Class objects. The JAXBContext will
* also be aware of classes reachable from the classes in the array.
*/
public static javax.xml.bind.JAXBContext createContext(Class[] classesToBeBound, Map properties) throws JAXBException {
ClassLoader loader = null;
if (classesToBeBound.length > 0) {
loader = classesToBeBound[0].getClassLoader();
}
return createContext(classesToBeBound, properties, loader);
}
/**
* Create a JAXBContext on the array of Class objects. The JAXBContext will
* also be aware of classes reachable from the classes in the array.
*/
public static javax.xml.bind.JAXBContext createContext(Class[] classesToBeBound, Map properties, ClassLoader classLoader) throws JAXBException {
Type[] types = new Type[classesToBeBound.length];
System.arraycopy(classesToBeBound, 0, types, 0, classesToBeBound.length);
return createContext(types, properties, classLoader);
}
/**
* Create a JAXBContext on context path. The JAXBContext will
* also be aware of classes reachable from the classes on the context path.
*/
public static javax.xml.bind.JAXBContext createContext(String contextPath, ClassLoader classLoader) throws JAXBException {
return createContext(contextPath, classLoader, null);
}
/**
* Create a JAXBContext on context path. The JAXBContext will
* also be aware of classes reachable from the classes on the context path.
*/
public static javax.xml.bind.JAXBContext createContext(String contextPath, ClassLoader classLoader, Map properties) throws JAXBException {
JAXBContextInput contextInput = new ContextPathInput(contextPath, properties, classLoader);
JAXBContext context = new JAXBContext(contextInput);
if (context.isRefreshable()) {
context.postInitialize();
}
return context;
}
/**
* Create a JAXBContext on the array of Type objects. The JAXBContext will
* also be aware of classes reachable from the types in the array. The
* preferred means of creating a Type aware JAXBContext is to create the
* JAXBContext with an array of TypeMappingInfo objects.
*/
public static javax.xml.bind.JAXBContext createContext(Type[] typesToBeBound, Map properties, ClassLoader classLoader) throws JAXBException {
Map typeToTypeMappingInfo = new HashMap();
TypeMappingInfo[] typeMappingInfos = new TypeMappingInfo[typesToBeBound.length];
for(int i = 0; i < typesToBeBound.length; i++) {
TypeMappingInfo tmi = new TypeMappingInfo();
tmi.setType(typesToBeBound[i]);
typeToTypeMappingInfo.put(typesToBeBound[i], tmi);
typeMappingInfos[i] = tmi;
}
JAXBContext context = (JAXBContext)createContext(typeMappingInfos, properties, classLoader);
context.setTypeToTypeMappingInfo(typeToTypeMappingInfo);
return context;
}
/**
* Create a JAXBContext on the array of TypeMappingInfo objects. The
* JAXBContext will also be aware of classes reachable from the types in the
* array. This is the preferred means of creating a Type aware JAXBContext.
*/
public static javax.xml.bind.JAXBContext createContext(TypeMappingInfo[] typesToBeBound, Map properties, ClassLoader classLoader) throws JAXBException {
JAXBContextInput contextInput = new TypeMappingInfoInput(typesToBeBound, properties, classLoader);
JAXBContext context = new JAXBContext(contextInput);
if (context.isRefreshable()) {
context.postInitialize();
}
return context;
}
/**
* Convenience method for processing a properties map and creating a map of
* package names to XmlBindings instances.
*
* It is assumed that the given map's key will be JAXBContextProperties.OXM_METADATA_SOURCE,
* and the value will be:
*
* 1) {@literal Map}
* - Object is one of those listed in 3) below
* 2) {@literal List
*/
public static Map getXmlBindingsFromProperties(Map properties, ClassLoader classLoader) {
Map> bindings = new HashMap>();
Object value = null;
if (properties != null) {
if ((value = properties.get(JAXBContextProperties.OXM_METADATA_SOURCE)) == null) {
// try looking up the 'old' metadata source key
value = properties.get(ECLIPSELINK_OXM_XML_KEY);
}
}
if (value != null) {
// handle Map
if (value instanceof Map) {
Map metadataFiles = null;
try {
metadataFiles = (Map) value;
} catch (ClassCastException x) {
throw org.eclipse.persistence.exceptions.JAXBException.incorrectValueParameterTypeForOxmXmlKey();
}
for(Entry entry : metadataFiles.entrySet()) {
String key = null;
List xmlBindings = new ArrayList();
try {
key = entry.getKey();
if (key == null) {
throw org.eclipse.persistence.exceptions.JAXBException.nullMapKey();
}
} catch (ClassCastException cce) {
throw org.eclipse.persistence.exceptions.JAXBException.incorrectKeyParameterType();
}
Object metadataSource = entry.getValue();
if (metadataSource == null) {
throw org.eclipse.persistence.exceptions.JAXBException.nullMetadataSource(key);
}
if(metadataSource instanceof List) {
for(Object next: (List)metadataSource) {
XmlBindings binding = getXmlBindings(next, classLoader, properties);
if(binding != null) {
xmlBindings.add(binding);
}
}
} else {
XmlBindings binding = getXmlBindings(metadataSource, classLoader, properties);
if(binding != null) {
xmlBindings.add(binding);
}
}
if (xmlBindings != null) {
bindings.put(key, xmlBindings);
}
}
// handle List