use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.
the class XRServiceFactory method loadOXMetadata.
/**
* <p>INTERNAL:
* Create a Project using OXM metadata. The given classloader is expected
* to successfully load 'META-INF/eclipselink-dbws-ox.xml'.
* @param xrdecl {@link ClassLoader} used to search for {@code eclipselink-dbws-ox.xml}.
* @param session OXM session (only for logging).
*/
protected Project loadOXMetadata(final ClassLoader xrdecl, final Session session) {
Project oxProject = null;
InputStream inStream = null;
String searchPath;
// try "META-INF/" and "/META-INF/"
for (String prefix : META_INF_PATHS) {
searchPath = prefix + Util.DBWS_OX_XML;
inStream = xrdecl.getResourceAsStream(searchPath);
if (inStream != null) {
break;
}
}
if (inStream != null) {
Map<String, OXMMetadataSource> metadataMap = null;
StreamSource xml = new StreamSource(inStream);
try {
JAXBContext jc = JAXBContext.newInstance(XmlBindingsModel.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<XmlBindingsModel> jaxbElt = unmarshaller.unmarshal(xml, XmlBindingsModel.class);
XmlBindingsModel model = jaxbElt.getValue();
if (model.getBindingsList() != null) {
metadataMap = new HashMap<>();
for (XmlBindings xmlBindings : model.getBindingsList()) {
metadataMap.put(xmlBindings.getPackageName(), new OXMMetadataSource(xmlBindings));
}
}
} catch (JAXBException jaxbex) {
/* could be legacy project, or none set, so just log */
session.getSessionLog().log(SessionLog.FINE, SessionLog.DBWS, "dbws_oxm_metadata_read_error", jaxbex.getLocalizedMessage());
return null;
}
if (metadataMap != null) {
Map<String, Map<String, OXMMetadataSource>> properties = new HashMap<>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataMap);
try {
DynamicJAXBContext jCtx = DynamicJAXBContextFactory.createContextFromOXM(xrdecl, properties);
oxProject = jCtx.getXMLContext().getSession(0).getProject();
oxProject.setName(xrService.getName().concat(OX_PRJ_SUFFIX));
} catch (JAXBException e) {
throw new DBWSException(OXM_PROCESSING_EX, e);
}
}
}
return oxProject;
}
use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.
the class XMLProcessor method mergeXmlBindings.
/**
* This method is used to merge several bindings files into one XMLBindings object.
* @param bindings the list of XmlBindings objects to merge.
* @return XmlBindings object representing the merged files.
*/
public static XmlBindings mergeXmlBindings(List<XmlBindings> bindings) {
if (bindings.size() == 0) {
return null;
}
XmlBindings rootBindings = bindings.get(0);
for (int i = 1; i < bindings.size(); i++) {
XmlBindings nextBindings = bindings.get(i);
if (nextBindings.isSetXmlAccessorOrder()) {
rootBindings.setXmlAccessorOrder(nextBindings.getXmlAccessorOrder());
}
if (nextBindings.isSetXmlAccessorType()) {
rootBindings.setXmlAccessorType(nextBindings.getXmlAccessorType());
}
if (nextBindings.isSetXmlMappingMetadataComplete()) {
rootBindings.setXmlMappingMetadataComplete(nextBindings.isXmlMappingMetadataComplete());
}
mergeJavaTypes(rootBindings.getJavaTypes(), nextBindings.getJavaTypes());
if (rootBindings.getXmlEnums() == null) {
rootBindings.setXmlEnums(nextBindings.getXmlEnums());
} else {
mergeXmlEnums(rootBindings.getXmlEnums(), nextBindings.getXmlEnums());
}
if (rootBindings.getXmlSchema() == null) {
rootBindings.setXmlSchema(nextBindings.getXmlSchema());
} else if (nextBindings.getXmlSchema() != null) {
mergeXmlSchema(rootBindings.getXmlSchema(), nextBindings.getXmlSchema());
}
if (rootBindings.getXmlSchemaType() == null) {
rootBindings.setXmlSchemaTypes(nextBindings.getXmlSchemaTypes());
} else if (nextBindings.getXmlSchemaTypes() != null) {
mergeXmlSchemaTypes(rootBindings.getXmlSchemaTypes(), nextBindings.getXmlSchemaTypes());
}
if (rootBindings.getXmlJavaTypeAdapters() == null) {
rootBindings.setXmlJavaTypeAdapters(nextBindings.getXmlJavaTypeAdapters());
} else if (nextBindings.getXmlJavaTypeAdapters() != null) {
mergeXmlJavaTypeAdapters(rootBindings.getXmlJavaTypeAdapters(), nextBindings.getXmlJavaTypeAdapters());
}
if (rootBindings.getXmlNullPolicy() == null) {
rootBindings.setXmlNullPolicy(nextBindings.getXmlNullPolicy());
} else if (nextBindings.getXmlNullPolicy() != null) {
mergeXmlNullPolicy(rootBindings.getXmlNullPolicy(), nextBindings.getXmlNullPolicy());
}
if (rootBindings.getXmlElementNillable() == null) {
rootBindings.setXmlElementNillable(nextBindings.getXmlElementNillable());
} else if (nextBindings.getXmlElementNillable() != null) {
mergeXmlElementNillable(rootBindings.getXmlElementNillable(), nextBindings.getXmlElementNillable());
}
}
return rootBindings;
}
use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.
the class OXMMetadata method createClassModelFromOXM.
private JavaClass[] createClassModelFromOXM(DynamicClassLoader dynamicClassLoader) throws JAXBException {
List<JavaClass> oxmJavaClasses = new ArrayList<JavaClass>();
Iterator<String> keys = bindings.keySet().iterator();
while (keys.hasNext()) {
String pkgName = keys.next();
XmlBindings b = bindings.get(pkgName);
if (b.getJavaTypes() != null) {
List<JavaType> javaTypes = b.getJavaTypes().getJavaType();
for (Iterator<JavaType> iterator = javaTypes.iterator(); iterator.hasNext(); ) {
JavaType type = iterator.next();
// Check to see if it's a static class or if should be treated as dynamic
try {
Class<?> staticClass = dynamicClassLoader.getParent().loadClass(Helper.getQualifiedJavaTypeName(type.getName(), pkgName));
oxmJavaClasses.add(new JavaClassImpl(staticClass, null));
} catch (Exception ex) {
type.setName(Helper.getQualifiedJavaTypeName(type.getName(), pkgName));
oxmJavaClasses.add(new OXMJavaClassImpl(type));
}
}
}
if (b.getXmlRegistries() != null) {
List<XmlRegistry> registries = b.getXmlRegistries().getXmlRegistry();
for (Iterator<XmlRegistry> iterator = registries.iterator(); iterator.hasNext(); ) {
XmlRegistry reg = iterator.next();
oxmJavaClasses.add(new OXMObjectFactoryImpl(reg));
}
}
if (b.getXmlEnums() != null) {
List<XmlEnum> enums = b.getXmlEnums().getXmlEnum();
for (Iterator<XmlEnum> iterator = enums.iterator(); iterator.hasNext(); ) {
XmlEnum xmlEnum = iterator.next();
List<XmlEnumValue> enumValues = xmlEnum.getXmlEnumValue();
List<String> enumValueStrings = new ArrayList<String>();
for (Iterator<XmlEnumValue> iterator2 = enumValues.iterator(); iterator2.hasNext(); ) {
XmlEnumValue xmlEnumValue = iterator2.next();
enumValueStrings.add(xmlEnumValue.getJavaEnumValue());
}
oxmJavaClasses.add(new OXMJavaClassImpl(xmlEnum.getJavaEnum(), enumValueStrings));
// Trigger a dynamic class generation, because we won't
// be creating a descriptor for this
dynamicClassLoader.addEnum(xmlEnum.getJavaEnum(), enumValueStrings.toArray());
}
}
}
JavaClass[] javaClasses = new JavaClass[oxmJavaClasses.size()];
for (int i = 0; i < javaClasses.length; i++) {
javaClasses[i] = oxmJavaClasses.get(i);
}
return javaClasses;
}
use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.
the class JAXBTestCases method getXmlBindings.
private static XmlBindings getXmlBindings(Object metadata) {
XmlBindings xmlBindings = null;
Unmarshaller unmarshaller;
// only create the JAXBContext for our XmlModel once
JAXBContext jaxbContext = CompilerHelper.getXmlBindingsModelContext();
try {
unmarshaller = jaxbContext.createUnmarshaller();
if (metadata instanceof File) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal((File) metadata);
} else if (metadata instanceof InputSource) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal((InputSource) metadata);
} else if (metadata instanceof BufferedInputStream) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal((BufferedInputStream) metadata);
} else if (metadata instanceof InputStream) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal((InputStream) metadata);
} else if (metadata instanceof Node) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal((Node) metadata);
} else if (metadata instanceof Reader) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal((Reader) metadata);
} else if (metadata instanceof Source) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal((Source) metadata);
} else if (metadata instanceof URL) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal((URL) metadata);
} else if (metadata instanceof XMLEventReader) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal((XMLEventReader) metadata);
} else if (metadata instanceof XMLStreamReader) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal((XMLStreamReader) metadata);
} else if (metadata instanceof String) {
if (((String) metadata).length() == 0) {
throw org.eclipse.persistence.exceptions.JAXBException.unableToLoadMetadataFromLocation((String) metadata);
}
URL url = null;
try {
url = new URL((String) metadata);
} catch (MalformedURLException ex) {
url = Thread.currentThread().getContextClassLoader().getResource((String) metadata);
}
if (url != null) {
xmlBindings = (XmlBindings) unmarshaller.unmarshal(url);
} else {
// throw exception
throw org.eclipse.persistence.exceptions.JAXBException.unableToLoadMetadataFromLocation((String) metadata);
}
} else {
throw org.eclipse.persistence.exceptions.JAXBException.incorrectValueParameterTypeForOxmXmlKey();
}
} catch (JAXBException jaxbEx) {
throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(jaxbEx);
}
return xmlBindings;
}
use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.
the class XMLMetadataSource method getXmlBindings.
@Override
public XmlBindings getXmlBindings(Map<String, ?> properties, ClassLoader classLoader) {
try {
JAXBContext jaxbContext = CompilerHelper.getXmlBindingsModelContext();
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
if (null != xmlBindingsSource) {
return (XmlBindings) unmarshaller.unmarshal(xmlBindingsSource);
}
if (null != xmlBindingsURL) {
return (XmlBindings) unmarshaller.unmarshal(xmlBindingsURL);
}
if (null != xmlBindingsLocation) {
URL url = classLoader.getResource(xmlBindingsLocation);
if (url == null) {
// throw exception
throw org.eclipse.persistence.exceptions.JAXBException.unableToLoadMetadataFromLocation(xmlBindingsLocation);
}
return (XmlBindings) unmarshaller.unmarshal(url);
}
} catch (JAXBException e) {
throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(e);
}
return null;
}
Aggregations