Search in sources :

Example 1 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class JAXBContextFactory method getXmlBindingsFromProperties.

/**
 * <p>Convenience method for processing a properties map and creating a map of
 * package names to XmlBindings instances.</p>
 *
 * <p>It is assumed that the given map's key will be JAXBContextProperties.OXM_METADATA_SOURCE,
 * and the value will be:</p>
 * <pre>
 * 1)  {@literal Map<String, Object>}
 *     - Object is one of those listed in 3) below
 * 2)  {@literal List<Object>}
 *     - Object is one of those listed in 3) below
 *     - Bindings file must contain package-name attribute on
 *       xml-bindings element
 * 3)  One of:
 *     - java.io.File
 *     - java.io.InputStream
 *     - java.io.Reader
 *     - java.lang.String
 *     - java.net.URL
 *     - javax.xml.stream.XMLEventReader
 *     - javax.xml.stream.XMLStreamReader
 *     - javax.xml.transform.Source
 *     - org.eclipse.persistence.jaxb.metadata.MetadataSource
 *     - org.w3c.dom.Node
 *     - org.xml.sax.InputSource
 *
 *     - Bindings file must contain package-name attribute on
 *       xml-bindings element
 * </pre>
 */
public static Map<String, XmlBindings> getXmlBindingsFromProperties(Map<String, Object> properties, ClassLoader classLoader) {
    Map<String, List<XmlBindings>> bindings = new HashMap<String, List<XmlBindings>>();
    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<String, Object>
        if (value instanceof Map) {
            Map<String, Object> metadataFiles = null;
            try {
                metadataFiles = (Map<String, Object>) value;
            } catch (ClassCastException x) {
                throw org.eclipse.persistence.exceptions.JAXBException.incorrectValueParameterTypeForOxmXmlKey();
            }
            for (Entry<String, Object> entry : metadataFiles.entrySet()) {
                String key = null;
                List<XmlBindings> xmlBindings = new ArrayList<XmlBindings>();
                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<Object>
        } else if (value instanceof List) {
            for (Object metadataSource : (List) value) {
                if (metadataSource == null) {
                    throw org.eclipse.persistence.exceptions.JAXBException.nullMetadataSource();
                }
                bindings = processBindingFile(bindings, metadataSource, classLoader, properties);
            }
        // handle Object
        } else {
            bindings = processBindingFile(bindings, value, classLoader, properties);
        }
    }
    Map<String, XmlBindings> mergedBindings = new HashMap<String, XmlBindings>(bindings.size());
    for (Entry<String, List<XmlBindings>> next : bindings.entrySet()) {
        mergedBindings.put(next.getKey(), XMLProcessor.mergeXmlBindings(next.getValue()));
    }
    return mergedBindings;
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class JAXBContextFactory method getXmlBindings.

/**
 * Convenience method for creating an XmlBindings object based on a given Object. The method
 * will load the eclipselink metadata model and unmarshal the Object. This assumes that the
 * Object represents the eclipselink-oxm.xml metadata file to be unmarshalled.
 *
 * @param metadata assumed to be one of:  File, InputSource, InputStream, Reader, Source
 */
private static XmlBindings getXmlBindings(Object metadata, ClassLoader classLoader, Map<String, Object> properties) {
    JAXBContext jaxbContext = CompilerHelper.getXmlBindingsModelContext();
    InputStream openedStream = null;
    try {
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
        unmarshaller.setProperty(UnmarshallerProperties.AUTO_DETECT_MEDIA_TYPE, true);
        unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
        unmarshaller.setEventHandler(new DefaultValidationEventHandler());
        if (metadata instanceof MetadataSource) {
            return ((MetadataSource) metadata).getXmlBindings(properties, classLoader);
        }
        JAXBElement<XmlBindings> bindingsJaxbElement = null;
        if (metadata instanceof XMLEventReader) {
            bindingsJaxbElement = unmarshaller.unmarshal((XMLEventReader) metadata, XmlBindings.class);
        } else if (metadata instanceof XMLStreamReader) {
            bindingsJaxbElement = unmarshaller.unmarshal((XMLStreamReader) metadata, XmlBindings.class);
        } else {
            Source source = null;
            if (metadata instanceof File) {
                source = new StreamSource(new FileInputStream((File) metadata));
            } else if (metadata instanceof InputSource) {
                if (((InputSource) metadata).getByteStream() != null) {
                    source = new StreamSource(((InputSource) metadata).getByteStream());
                } else if (((InputSource) metadata).getCharacterStream() != null) {
                    source = new StreamSource(((InputSource) metadata).getCharacterStream());
                }
            } else if (metadata instanceof InputStream) {
                source = new StreamSource((InputStream) metadata);
            } else if (metadata instanceof Node) {
                source = new DOMSource((Node) metadata);
            } else if (metadata instanceof Reader) {
                source = new StreamSource((Reader) metadata);
            } else if (metadata instanceof Source) {
                source = (Source) metadata;
            } else if (metadata instanceof URL) {
                openedStream = ((URL) metadata).openStream();
                source = new StreamSource(openedStream);
            } else if (metadata instanceof String) {
                StreamSource streamSource = new StreamSource((String) metadata);
                try {
                    bindingsJaxbElement = unmarshaller.unmarshal(streamSource, XmlBindings.class);
                } catch (JAXBException e) {
                    openedStream = classLoader.getResourceAsStream((String) metadata);
                    if (openedStream != null) {
                        bindingsJaxbElement = unmarshaller.unmarshal(new StreamSource(openedStream), XmlBindings.class);
                    } else {
                        throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(e);
                    }
                }
            } else {
                throw org.eclipse.persistence.exceptions.JAXBException.incorrectValueParameterTypeForOxmXmlKey();
            }
            if (bindingsJaxbElement == null) {
                if (source == null) {
                    throw org.eclipse.persistence.exceptions.JAXBException.incorrectValueParameterTypeForOxmXmlKey();
                } else {
                    bindingsJaxbElement = unmarshaller.unmarshal(source, XmlBindings.class);
                }
            }
        }
        if (bindingsJaxbElement != null) {
            return bindingsJaxbElement.getValue();
        }
        throw org.eclipse.persistence.exceptions.JAXBException.incorrectValueParameterTypeForOxmXmlKey();
    } catch (jakarta.xml.bind.JAXBException ex) {
        throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(ex);
    } catch (IOException ioException) {
        throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(ioException);
    } finally {
        if (openedStream != null) {
            try {
                openedStream.close();
            } catch (IOException e) {
                throw org.eclipse.persistence.exceptions.JAXBException.couldNotUnmarshalMetadata(e);
            }
        }
    }
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) JAXBException(jakarta.xml.bind.JAXBException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLEventReader(javax.xml.stream.XMLEventReader) Reader(java.io.Reader) IOException(java.io.IOException) MetadataSource(org.eclipse.persistence.jaxb.metadata.MetadataSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) MetadataSource(org.eclipse.persistence.jaxb.metadata.MetadataSource) FileInputStream(java.io.FileInputStream) URL(java.net.URL) JAXBException(jakarta.xml.bind.JAXBException) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(jakarta.xml.bind.Unmarshaller) DefaultValidationEventHandler(jakarta.xml.bind.helpers.DefaultValidationEventHandler) File(java.io.File)

Example 3 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class XMLProcessor method processXML.

/**
 * Process XmlBindings on a per package basis for a given
 * AnnotationsProcessor instance.
 */
public void processXML(AnnotationsProcessor annotationsProcessor, JavaModelInput jModelInput, TypeMappingInfo[] typeMappingInfos, JavaClass[] originalJavaClasses) {
    this.jModelInput = jModelInput;
    this.aProcessor = annotationsProcessor;
    this.aProcessor.setHasXmlBindings(true);
    Map<String, XmlEnum> xmlEnumMap = new HashMap<String, XmlEnum>();
    aProcessor.init(originalJavaClasses, typeMappingInfos);
    // build a map of packages to JavaClass so we only process the
    // JavaClasses for a given package additional classes - i.e. ones from
    // packages not listed in XML - will be processed later
    Map<String, ArrayList<JavaClass>> pkgToClassMap = buildPackageToJavaClassMap();
    // process each XmlBindings in the map
    XmlBindings xmlBindings;
    for (String packageName : xmlBindingMap.keySet()) {
        ArrayList<JavaClass> classesToProcess = pkgToClassMap.get(packageName);
        if (classesToProcess == null) {
            getLogger().logWarning("jaxb_metadata_warning_no_classes_to_process", new Object[] { packageName });
            continue;
        }
        xmlBindings = xmlBindingMap.get(packageName);
        // handle @XmlSchema override
        NamespaceInfo nsInfo = processXmlSchema(xmlBindings, packageName);
        if (nsInfo != null) {
            aProcessor.addPackageToNamespaceMapping(packageName, nsInfo);
        }
        // handle @XmlElementNillable override
        if (null != xmlBindings.getXmlElementNillable()) {
            aProcessor.addPackageToXmlElementNillable(packageName, xmlBindings.getXmlElementNillable());
        }
        // handle @XmlNullPolicy override
        if (null != xmlBindings.getXmlNullPolicy()) {
            aProcessor.addPackageToXmlNullPolicy(packageName, xmlBindings.getXmlNullPolicy());
        }
        // handle xml-registries
        // add an entry to the map of registries keyed on factory class name for each
        XmlRegistries xmlRegs = xmlBindings.getXmlRegistries();
        if (xmlRegs != null) {
            for (XmlRegistry xmlReg : xmlRegs.getXmlRegistry()) {
                aProcessor.addXmlRegistry(xmlReg.getName(), xmlReg);
            }
        }
        // build an array of JavaModel classes to process
        JavaClass[] javaClasses = classesToProcess.toArray(new JavaClass[classesToProcess.size()]);
        // handle xml-enums
        // build a map of enum class names to XmlEnum objects
        XmlEnums xmlEnums = xmlBindings.getXmlEnums();
        if (xmlEnums != null) {
            for (XmlEnum xmlEnum : xmlEnums.getXmlEnum()) {
                xmlEnumMap.put(getQualifiedJavaTypeName(xmlEnum.getJavaEnum(), packageName), xmlEnum);
            }
        }
        // handle superclass override
        if (xmlBindings.getJavaTypes() != null) {
            List<JavaType> types = xmlBindings.getJavaTypes().getJavaType();
            for (JavaType next : types) {
                JavaClass typeClass = jModelInput.getJavaModel().getClass(getQualifiedJavaTypeName(next.getName(), packageName));
                if (typeClass != null && typeClass.getClass() == JavaClassImpl.class) {
                    if (next.getSuperType() != null && !(next.getSuperType().equals(DEFAULT))) {
                        JavaClass newSuperClass = jModelInput.getJavaModel().getClass(next.getSuperType());
                        ((JavaClassImpl) typeClass).setSuperClassOverride(newSuperClass);
                    }
                }
            }
        }
        // pre-build the TypeInfo objects
        Map<String, TypeInfo> typeInfoMap = aProcessor.preBuildTypeInfo(javaClasses);
        // handle package-level xml-schema-types
        List<XmlSchemaType> xmlSchemaTypes = null;
        XmlSchemaTypes sTypes = xmlBindings.getXmlSchemaTypes();
        if (sTypes != null) {
            xmlSchemaTypes = sTypes.getXmlSchemaType();
        } else {
            xmlSchemaTypes = new ArrayList<XmlSchemaType>();
        }
        // handle package-level xml-schema-type
        if (xmlBindings.getXmlSchemaType() != null) {
            xmlSchemaTypes.add(xmlBindings.getXmlSchemaType());
        }
        // process each xml-schema-type entry
        for (XmlSchemaType sType : xmlSchemaTypes) {
            JavaClass jClass = aProcessor.getHelper().getJavaClass(sType.getType());
            if (jClass != null) {
                aProcessor.processSchemaType(sType.getName(), sType.getNamespace(), jClass.getQualifiedName());
            }
        }
        PackageInfo packageInfo = aProcessor.getPackageToPackageInfoMappings().get(packageName);
        if (packageInfo == null) {
            packageInfo = new PackageInfo();
        }
        if (xmlBindings.isSetXmlAccessorType()) {
            packageInfo.setAccessType(xmlBindings.getXmlAccessorType());
        }
        JavaTypes jTypes = xmlBindings.getJavaTypes();
        if (jTypes != null) {
            for (JavaType javaType : jTypes.getJavaType()) {
                TypeInfo info = typeInfoMap.get(getQualifiedJavaTypeName(javaType.getName(), packageName));
                // handle class-level @XmlJavaTypeAdapter override
                if (javaType.getXmlJavaTypeAdapter() != null) {
                    info.setXmlJavaTypeAdapter(javaType.getXmlJavaTypeAdapter());
                }
                // handle class-level @XmlNullPolicy override
                XmlNullPolicy xmlNullPolicy = javaType.getXmlNullPolicy();
                if (null != xmlNullPolicy) {
                    info.setXmlNullPolicy(xmlNullPolicy);
                }
                // handle class-level @XmlElementNillable override
                XmlElementNillable xmlElementNillable = javaType.getXmlElementNillable();
                if (null != xmlElementNillable) {
                    info.setXmlElementNillable(xmlElementNillable.isNillable());
                }
                // handle class-level @XmlNameTransformer
                String transformerClassName = javaType.getXmlNameTransformer();
                XMLNameTransformer transformer = getXMLNameTransformerClassFromString(transformerClassName);
                if (transformer != null) {
                    info.setXmlNameTransformer(transformer);
                }
                // handle class-level @XmlExtensible override
                if (javaType.getXmlVirtualAccessMethods() != null) {
                    info.setXmlVirtualAccessMethods(javaType.getXmlVirtualAccessMethods());
                }
                // handle class-level @XmlAccessorOrder override
                if (javaType.isSetXmlAccessorOrder()) {
                    info.setXmlAccessOrder(javaType.getXmlAccessorOrder());
                } else if (!info.isSetXmlAccessOrder()) {
                    // handle package-level @XmlAccessorOrder override
                    if (xmlBindings.isSetXmlAccessorOrder()) {
                        info.setXmlAccessOrder(xmlBindings.getXmlAccessorOrder());
                    } else {
                        // finally, check the NamespaceInfo
                        info.setXmlAccessOrder(packageInfo.getAccessOrder());
                    }
                }
                // handle class-level @XmlAccessorType override
                if (javaType.isSetXmlAccessorType()) {
                    info.setXmlAccessType(javaType.getXmlAccessorType());
                }
                // handle @XmlInlineBinaryData override
                if (javaType.isSetXmlInlineBinaryData()) {
                    info.setInlineBinaryData(javaType.isXmlInlineBinaryData());
                }
                // handle @XmlTransient override
                if (javaType.isSetXmlTransient()) {
                    info.setXmlTransient(javaType.isXmlTransient());
                }
                // handle @XmlRootElement
                if (javaType.getXmlRootElement() != null) {
                    info.setXmlRootElement(javaType.getXmlRootElement());
                }
                // handle @XmlSeeAlso override
                if (javaType.getXmlSeeAlso() != null && javaType.getXmlSeeAlso().size() > 0) {
                    info.setXmlSeeAlso(javaType.getXmlSeeAlso());
                }
                // handle @XmlType override
                if (javaType.getXmlType() != null) {
                    info.setXmlType(javaType.getXmlType());
                }
                // handle @XmlCustomizer override
                if (javaType.getXmlCustomizer() != null) {
                    info.setXmlCustomizer(javaType.getXmlCustomizer());
                }
                // handle @XmlClassExtractor override
                if (javaType.getXmlClassExtractor() != null) {
                    info.setClassExtractorName(javaType.getXmlClassExtractor().getClazz());
                }
                // handle @XmlProperties override
                if (javaType.getXmlProperties() != null && javaType.getXmlProperties().getXmlProperty().size() > 0) {
                    // may need to merge with @XmlProperties (xml wins in the case of a conflict)
                    if (info.getUserProperties() != null) {
                        info.setUserProperties(mergeUserPropertyMap(javaType.getXmlProperties().getXmlProperty(), info.getUserProperties()));
                    } else {
                        info.setUserProperties(createUserPropertyMap(javaType.getXmlProperties().getXmlProperty()));
                    }
                }
                // handle @XmlDiscriminatorNode override
                if (javaType.getXmlDiscriminatorNode() != null) {
                    info.setXmlDiscriminatorNode(javaType.getXmlDiscriminatorNode());
                }
                // handle @NamedObjectGraph/@NamedObjectGraphs override
                if (javaType.getXmlNamedObjectGraphs() != null) {
                    List<XmlNamedObjectGraph> currentGraphs = info.getObjectGraphs();
                    for (XmlNamedObjectGraph nextGraph : javaType.getXmlNamedObjectGraphs().getXmlNamedObjectGraph()) {
                        // if not, add the new one
                        for (XmlNamedObjectGraph nextExistingGraph : currentGraphs) {
                            if (nextGraph.getName().equals(nextExistingGraph.getName())) {
                                currentGraphs.remove(nextExistingGraph);
                                break;
                            }
                        }
                    }
                    currentGraphs.addAll(javaType.getXmlNamedObjectGraphs().getXmlNamedObjectGraph());
                }
                // handle @XmlDiscriminatorValue override
                if (javaType.getXmlDiscriminatorValue() != null) {
                    info.setXmlDiscriminatorValue(javaType.getXmlDiscriminatorValue());
                }
            }
        }
        // apply package-level @XmlNameTransformer
        Map<String, TypeInfo> typeInfos = aProcessor.getTypeInfosForPackage(packageName);
        String transformerClassName = xmlBindings.getXmlNameTransformer();
        XMLNameTransformer transformer = getXMLNameTransformerClassFromString(transformerClassName);
        if (transformer != null) {
            packageInfo.setXmlNameTransformer(transformer);
        }
        // apply package-level @XmlJavaTypeAdapters
        for (TypeInfo tInfo : typeInfos.values()) {
            if (xmlBindings.getXmlJavaTypeAdapters() != null) {
                List<XmlJavaTypeAdapter> adapters = xmlBindings.getXmlJavaTypeAdapters().getXmlJavaTypeAdapter();
                for (XmlJavaTypeAdapter xja : adapters) {
                    try {
                        JavaClass adapterClass = jModelInput.getJavaModel().getClass(xja.getValue());
                        JavaClass boundType = jModelInput.getJavaModel().getClass(xja.getType());
                        if (boundType != null) {
                            tInfo.addPackageLevelAdapterClass(adapterClass, boundType);
                            packageInfo.getPackageLevelAdaptersByClass().put(boundType.getQualifiedName(), adapterClass);
                        }
                    } catch (JAXBException e) {
                        throw JAXBException.invalidPackageAdapterClass(xja.getValue(), packageName);
                    }
                }
            }
        }
    }
    for (String packageName : xmlBindingMap.keySet()) {
        ArrayList<JavaClass> classesToProcess = pkgToClassMap.get(packageName);
        if (classesToProcess == null) {
            getLogger().logWarning("jaxb_metadata_warning_no_classes_to_process", new Object[] { packageName });
            continue;
        }
        xmlBindings = xmlBindingMap.get(packageName);
        JavaClass[] javaClasses = classesToProcess.toArray(new JavaClass[classesToProcess.size()]);
        // post-build the TypeInfo objects
        javaClasses = aProcessor.postBuildTypeInfo(javaClasses);
        // get the generated TypeInfo
        Map<String, TypeInfo> typeInfosForPackage = aProcessor.getTypeInfosForPackage(packageName);
        // update xml-enum info if necessary
        for (Entry<String, TypeInfo> entry : typeInfosForPackage.entrySet()) {
            TypeInfo tInfo = entry.getValue();
            if (tInfo.isEnumerationType()) {
                EnumTypeInfo etInfo = (EnumTypeInfo) tInfo;
                XmlEnum xmlEnum = xmlEnumMap.get(etInfo.getClassName());
                if (xmlEnum != null) {
                    JavaClass restrictionClass = aProcessor.getHelper().getJavaClass(xmlEnum.getValue());
                    // default to String if necessary
                    if (restrictionClass == null) {
                        restrictionClass = jModelInput.getJavaModel().getClass(String.class);
                    }
                    etInfo.setRestrictionBase(aProcessor.getSchemaTypeFor(restrictionClass));
                    for (XmlEnumValue xmlEnumValue : xmlEnum.getXmlEnumValue()) {
                        // overwrite any existing entries (from annotations)
                        etInfo.addJavaFieldToXmlEnumValuePair(true, xmlEnumValue.getJavaEnumValue(), xmlEnumValue.getValue());
                    }
                }
            }
        }
        // update TypeInfo objects based on the JavaTypes
        JavaTypes jTypes = xmlBindings.getJavaTypes();
        if (jTypes != null) {
            PackageInfo packageInfo = aProcessor.getPackageToPackageInfoMappings().get(packageName);
            NamespaceInfo nsInfo = null;
            if (null != packageInfo) {
                nsInfo = packageInfo.getNamespaceInfo();
            }
            for (JavaType javaType : jTypes.getJavaType()) {
                processJavaType(javaType, typeInfosForPackage.get(getQualifiedJavaTypeName(javaType.getName(), packageName)), nsInfo);
            }
        }
        // remove the entry for this package from the map
        pkgToClassMap.remove(packageName);
    }
    // now process remaining classes
    Iterator<ArrayList<JavaClass>> classIt = pkgToClassMap.values().iterator();
    while (classIt.hasNext()) {
        ArrayList<JavaClass> jClassList = classIt.next();
        JavaClass[] jClassArray = jClassList.toArray(new JavaClass[jClassList.size()]);
        aProcessor.buildNewTypeInfo(jClassArray);
        aProcessor.checkForCallbackMethods();
    }
    // need to ensure that any bound types (from XmlJavaTypeAdapter) have TypeInfo
    // objects built for them - SchemaGenerator will require a descriptor for each
    Map<String, TypeInfo> typeInfos = (Map<String, TypeInfo>) ((HashMap) aProcessor.getTypeInfos()).clone();
    for (Entry<String, TypeInfo> entry : typeInfos.entrySet()) {
        JavaClass[] jClassArray;
        for (Property prop : entry.getValue().getPropertyList()) {
            if (prop.isSetXmlJavaTypeAdapter()) {
                jClassArray = new JavaClass[] { prop.getActualType() };
                aProcessor.buildNewTypeInfo(jClassArray);
            }
        }
    }
    // now trigger the annotations processor to process the classes
    List<JavaClass> jClasses = aProcessor.getTypeInfoClasses();
    // (in case super and sub classes were in different packages)
    if (xmlBindingMap.size() > 1) {
        for (JavaClass c : jClasses) {
            TypeInfo ti = aProcessor.getTypeInfos().get(c.getQualifiedName());
            aProcessor.processPropertiesSuperClass(c, ti);
        }
    }
    aProcessor.processPropertyTypes(jClasses.toArray(new JavaClass[jClasses.size()]));
    aProcessor.finalizeProperties();
    aProcessor.createElementsForTypeMappingInfo();
    aProcessor.checkForCallbackMethods();
}
Also used : JavaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes) XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) HashMap(java.util.HashMap) XmlNamedObjectGraph(org.eclipse.persistence.jaxb.xmlmodel.XmlNamedObjectGraph) ArrayList(java.util.ArrayList) JavaClassImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaClassImpl) XMLNameTransformer(org.eclipse.persistence.oxm.XMLNameTransformer) XmlProperty(org.eclipse.persistence.jaxb.xmlmodel.XmlProperties.XmlProperty) XmlEnums(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.XmlEnums) XmlJavaTypeAdapter(org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter) JAXBException(org.eclipse.persistence.exceptions.JAXBException) XmlRegistry(org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry) XmlSchemaType(org.eclipse.persistence.jaxb.xmlmodel.XmlSchemaType) XmlSchemaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlSchemaTypes) XmlEnum(org.eclipse.persistence.jaxb.xmlmodel.XmlEnum) JavaType(org.eclipse.persistence.jaxb.xmlmodel.JavaType) XmlNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlNullPolicy) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XmlElementNillable(org.eclipse.persistence.jaxb.xmlmodel.XmlElementNillable) XmlRegistries(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.XmlRegistries) XmlEnumValue(org.eclipse.persistence.jaxb.xmlmodel.XmlEnumValue) XmlMap(org.eclipse.persistence.jaxb.xmlmodel.XmlMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class XMLProcessor method buildPackageToJavaClassMap.

/**
 * Convenience method for building a Map of package to classes.
 */
private Map<String, ArrayList<JavaClass>> buildPackageToJavaClassMap() {
    Map<String, ArrayList<JavaClass>> theMap = new HashMap<String, ArrayList<JavaClass>>();
    Map<String, ArrayList<JavaClass>> xmlBindingsMap = new HashMap<String, ArrayList<JavaClass>>();
    XmlBindings xmlBindings;
    for (String packageName : xmlBindingMap.keySet()) {
        xmlBindings = xmlBindingMap.get(packageName);
        ArrayList<JavaClass> classes = new ArrayList<JavaClass>();
        // add binding classes - the Java Model will be used to get a
        // JavaClass via class name
        JavaTypes jTypes = xmlBindings.getJavaTypes();
        if (jTypes != null) {
            for (JavaType javaType : jTypes.getJavaType()) {
                addClassToList(classes, javaType.getName(), packageName);
            }
        }
        // add any enum types to the class list
        XmlEnums xmlEnums = xmlBindings.getXmlEnums();
        if (xmlEnums != null) {
            for (XmlEnum xmlEnum : xmlEnums.getXmlEnum()) {
                addClassToList(classes, xmlEnum.getJavaEnum(), packageName);
            }
        }
        theMap.put(packageName, classes);
        xmlBindingsMap.put(packageName, new ArrayList(classes));
    }
    // add any other classes that aren't declared via external metadata
    for (JavaClass jClass : jModelInput.getJavaClasses()) {
        // need to verify that the class isn't already in the bindings file
        // list
        String pkg = jClass.getPackageName();
        ArrayList<JavaClass> existingXmlBindingsClasses = xmlBindingsMap.get(pkg);
        ArrayList<JavaClass> allExistingClasses = theMap.get(pkg);
        if (existingXmlBindingsClasses != null) {
            if (!classExistsInArray(jClass, existingXmlBindingsClasses)) {
                allExistingClasses.add(jClass);
            }
        } else {
            if (allExistingClasses != null) {
                allExistingClasses.add(jClass);
            } else {
                ArrayList<JavaClass> classes = new ArrayList<JavaClass>();
                classes.add(jClass);
                theMap.put(pkg, classes);
            }
        }
    }
    return theMap;
}
Also used : JavaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes) XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) XmlEnums(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.XmlEnums) JavaType(org.eclipse.persistence.jaxb.xmlmodel.JavaType) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) XmlEnum(org.eclipse.persistence.jaxb.xmlmodel.XmlEnum)

Example 5 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class JAXBContextTransformationMappingTestCases method testInvalidMethod.

/**
 * Test exception handling:  in this case the method name that has been
 * set is invalid (wrong number or type of params. An exception should
 * be thrown.
 *
 * Negative test.
 */
public void testInvalidMethod() {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(METADATA_FILE_BAD_METHOD);
    HashMap<String, Source> metadataSourceMap = new HashMap<>();
    metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.externalizedmetadata.mappings.xmltransformation", new StreamSource(inputStream));
    Map<String, Object> properties = new HashMap<>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    int exceptionCount = 0;
    try {
        JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class<?>[] { Employee.class }, properties, Thread.currentThread().getContextClassLoader());
    } catch (JAXBException e) {
        exceptionCount++;
    }
    assertTrue("The expected exception was never thrown.", exceptionCount > 0);
    exceptionCount--;
    // test exception from SchemaGenerator
    try {
        Map<String, XmlBindings> bindings = JAXBContextFactory.getXmlBindingsFromProperties(properties, Thread.currentThread().getContextClassLoader());
        JavaModelInputImpl jModelInput = new JavaModelInputImpl(new Class<?>[] { Employee.class }, new JavaModelImpl(new JaxbClassLoader(Thread.currentThread().getContextClassLoader(), new Class<?>[] { Employee.class })));
        Generator generator = new Generator(jModelInput, bindings, Thread.currentThread().getContextClassLoader(), "", false);
        generator.generateSchema();
    } catch (Exception e) {
        exceptionCount++;
    }
    assertTrue("The expected exception was never thrown.", exceptionCount > 0);
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBException(jakarta.xml.bind.JAXBException) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

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