Search in sources :

Example 11 with XmlBindings

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;
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) JAXBContext(jakarta.xml.bind.JAXBContext) SimpleXMLFormatProject(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatProject) Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DBWSException(org.eclipse.persistence.exceptions.DBWSException) Unmarshaller(jakarta.xml.bind.Unmarshaller) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with XmlBindings

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;
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)

Example 13 with XmlBindings

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;
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) ArrayList(java.util.ArrayList) OXMObjectFactoryImpl(org.eclipse.persistence.jaxb.javamodel.oxm.OXMObjectFactoryImpl) OXMJavaClassImpl(org.eclipse.persistence.jaxb.javamodel.oxm.OXMJavaClassImpl) JavaClassImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaClassImpl) XmlRegistry(org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry) JAXBException(jakarta.xml.bind.JAXBException) XmlEnum(org.eclipse.persistence.jaxb.xmlmodel.XmlEnum) JavaType(org.eclipse.persistence.jaxb.xmlmodel.JavaType) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XmlEnumValue(org.eclipse.persistence.jaxb.xmlmodel.XmlEnumValue) OXMJavaClassImpl(org.eclipse.persistence.jaxb.javamodel.oxm.OXMJavaClassImpl)

Example 14 with XmlBindings

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;
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) XMLStreamReaderInputSource(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource) InputSource(org.xml.sax.InputSource) MalformedURLException(java.net.MalformedURLException) XMLStreamReader(javax.xml.stream.XMLStreamReader) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) JAXBException(jakarta.xml.bind.JAXBException) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) XMLStreamReaderReader(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader) XMLReader(org.xml.sax.XMLReader) XMLEventReader(javax.xml.stream.XMLEventReader) StringReader(java.io.StringReader) JAXBContext(jakarta.xml.bind.JAXBContext) XMLStreamReaderInputSource(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) URL(java.net.URL) BufferedInputStream(java.io.BufferedInputStream) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(jakarta.xml.bind.Unmarshaller) File(java.io.File)

Example 15 with 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;
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(org.eclipse.persistence.jaxb.JAXBContext) Unmarshaller(jakarta.xml.bind.Unmarshaller) URL(java.net.URL)

Aggregations

XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)30 HashMap (java.util.HashMap)18 JAXBException (jakarta.xml.bind.JAXBException)16 StreamSource (javax.xml.transform.stream.StreamSource)16 JAXBContext (jakarta.xml.bind.JAXBContext)13 Unmarshaller (jakarta.xml.bind.Unmarshaller)12 Map (java.util.Map)11 StringReader (java.io.StringReader)10 XMLContext (org.eclipse.persistence.oxm.XMLContext)10 Project (org.eclipse.persistence.sessions.Project)10 XmlBindingsModel (org.eclipse.persistence.internal.xr.XmlBindingsModel)9 InputStream (java.io.InputStream)8 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)8 ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)8 MetadataProcessor (org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor)8 DatabaseSessionImpl (org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)8 XRDynamicClassLoader (org.eclipse.persistence.internal.xr.XRDynamicClassLoader)8 XMLPlatform (org.eclipse.persistence.platform.xml.XMLPlatform)8 DatabaseLogin (org.eclipse.persistence.sessions.DatabaseLogin)8 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)8