Search in sources :

Example 1 with JavaTypes

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes 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 2 with JavaTypes

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes 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 3 with JavaTypes

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

the class XMLBindingsPopulatedTestCases method getControlObject.

@Override
protected Object getControlObject() {
    XmlBindings xmlBindings = new XmlBindings();
    xmlBindings.setPackageName("test.model");
    xmlBindings.setXmlAccessorType(XmlAccessType.PUBLIC_MEMBER);
    xmlBindings.setXmlAccessorOrder(org.eclipse.persistence.jaxb.xmlmodel.XmlAccessOrder.ALPHABETICAL);
    XmlEnums enums = new XmlEnums();
    XmlEnum e1 = new XmlEnum();
    e1.setJavaEnum("A");
    e1.setValue("A_Value");
    XmlEnum e2 = new XmlEnum();
    e2.setJavaEnum("B");
    e2.setValue("B_Value");
    enums.getXmlEnum().add(e1);
    enums.getXmlEnum().add(e2);
    xmlBindings.setXmlEnums(enums);
    xmlBindings.setXmlNameTransformer("test.someNameTransformer");
    XmlSchema xmlSchema = new XmlSchema();
    xmlSchema.setAttributeFormDefault(XmlNsForm.UNQUALIFIED);
    xmlSchema.setElementFormDefault(XmlNsForm.UNQUALIFIED);
    xmlSchema.setLocation("someLocation");
    xmlSchema.setNamespace("testnamespace");
    xmlBindings.setXmlSchema(xmlSchema);
    XmlSchemaTypes xmlSchemaTypes = new XmlSchemaTypes();
    XmlSchemaType xmlSchemaType = new XmlSchemaType();
    xmlSchemaType.setName("someSchemaType");
    xmlSchemaType.setNamespace("someSchemaTypeNamespace");
    xmlSchemaType.setType("someSchemaType");
    XmlSchemaType xmlSchemaType2 = new XmlSchemaType();
    xmlSchemaType2.setName("someSchemaType2");
    xmlSchemaType2.setNamespace("someSchemaTypeNamespace2");
    xmlSchemaType2.setType("someSchemaType2");
    xmlSchemaTypes.getXmlSchemaType().add(xmlSchemaType);
    xmlSchemaTypes.getXmlSchemaType().add(xmlSchemaType2);
    xmlBindings.setXmlSchemaTypes(xmlSchemaTypes);
    xmlBindings.setXmlMappingMetadataComplete(Boolean.FALSE);
    XmlRegistries xmlRegistries = new XmlRegistries();
    XmlRegistry r1 = new XmlRegistry();
    r1.setName("someRegistry");
    xmlRegistries.getXmlRegistry().add(r1);
    xmlBindings.setXmlRegistries(xmlRegistries);
    XmlJavaTypeAdapters xmlJavaTypeAdapters = new XmlJavaTypeAdapters();
    XmlJavaTypeAdapter a1 = new XmlJavaTypeAdapter();
    a1.setType("someAdapterType");
    a1.setValue("someAdapterValue");
    a1.setValueType("somValueType");
    a1.setJavaAttribute("someJavaAttribute");
    xmlJavaTypeAdapters.getXmlJavaTypeAdapter().add(a1);
    xmlBindings.setXmlJavaTypeAdapters(xmlJavaTypeAdapters);
    JavaTypes types = new JavaTypes();
    JavaType javaType = new JavaType();
    javaType.setName("myType");
    javaType.setXmlTransient(Boolean.FALSE);
    javaType.setXmlAccessorOrder(org.eclipse.persistence.jaxb.xmlmodel.XmlAccessOrder.ALPHABETICAL);
    XmlProperties xmlProperties = new XmlProperties();
    XmlProperty p1 = new XmlProperty();
    p1.setName("prop1");
    p1.setValue("propValue");
    p1.setValueType("propValueType");
    xmlProperties.getXmlProperty().add(p1);
    javaType.setXmlProperties(xmlProperties);
    javaType.setXmlDiscriminatorNode("descriminator node");
    javaType.setXmlDiscriminatorValue("discriminator value");
    XmlClassExtractor extractor = new XmlClassExtractor();
    extractor.setClazz("a.b.c.someClass");
    javaType.setXmlClassExtractor(extractor);
    javaType.setXmlInlineBinaryData(Boolean.FALSE);
    XmlRootElement root = new XmlRootElement();
    root.setName("someRoot");
    root.setNamespace("someNamespace");
    javaType.setXmlRootElement(root);
    XmlType xmlType = new XmlType();
    xmlType.setFactoryClass("somepackage.someFactoryclass");
    xmlType.setName("someName");
    xmlType.setFactoryMethod("someMethod");
    xmlType.getPropOrder().add("p2");
    xmlType.getPropOrder().add("p1");
    javaType.setXmlType(xmlType);
    JavaAttributes javaAttributes = new JavaAttributes();
    XmlElement javaAttribute = new XmlElement();
    javaAttribute.setName("elementName");
    javaAttribute.setJavaAttribute("theJavaAttributeValue");
    JAXBElement<XmlElement> jbe = new JAXBElement<XmlElement>(new QName("http://www.eclipse.org/eclipselink/xsds/persistence/oxm", "xml-element"), XmlElement.class, javaAttribute);
    XmlElement javaAttribute2 = new XmlElement();
    javaAttribute2.setName("elementName2");
    javaAttribute2.setJavaAttribute("theJavaAttributeValue2");
    JAXBElement<XmlElement> jbe2 = new JAXBElement<XmlElement>(new QName("http://www.eclipse.org/eclipselink/xsds/persistence/oxm", "xml-element"), XmlElement.class, javaAttribute2);
    XmlAttribute javaAttribute3 = new XmlAttribute();
    javaAttribute3.setContainerType("someContainerType");
    javaAttribute3.setJavaAttribute("javaAttribute");
    javaAttribute3.setNamespace("somenamespace");
    javaAttribute3.setName("attributename1");
    javaAttribute3.setReadOnly(Boolean.TRUE);
    javaAttribute3.setRequired(Boolean.TRUE);
    JAXBElement<XmlAttribute> jbe3 = new JAXBElement<XmlAttribute>(new QName("http://www.eclipse.org/eclipselink/xsds/persistence/oxm", "xml-attribute"), XmlAttribute.class, javaAttribute3);
    javaAttributes.getJavaAttribute().add(jbe);
    javaAttributes.getJavaAttribute().add(jbe3);
    javaAttributes.getJavaAttribute().add(jbe2);
    javaType.setJavaAttributes(javaAttributes);
    types.getJavaType().add(javaType);
    xmlBindings.setJavaTypes(types);
    return xmlBindings;
}
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) XmlRootElement(org.eclipse.persistence.jaxb.xmlmodel.XmlRootElement) XmlProperties(org.eclipse.persistence.jaxb.xmlmodel.XmlProperties) XmlAttribute(org.eclipse.persistence.jaxb.xmlmodel.XmlAttribute) XmlProperty(org.eclipse.persistence.jaxb.xmlmodel.XmlProperties.XmlProperty) QName(javax.xml.namespace.QName) XmlJavaTypeAdapter(org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter) JAXBElement(jakarta.xml.bind.JAXBElement) XmlSchemaType(org.eclipse.persistence.jaxb.xmlmodel.XmlSchemaType) XmlRegistry(org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry) XmlSchemaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlSchemaTypes) XmlEnum(org.eclipse.persistence.jaxb.xmlmodel.XmlEnum) XmlClassExtractor(org.eclipse.persistence.jaxb.xmlmodel.XmlClassExtractor) XmlType(org.eclipse.persistence.jaxb.xmlmodel.XmlType) JavaType(org.eclipse.persistence.jaxb.xmlmodel.JavaType) JavaAttributes(org.eclipse.persistence.jaxb.xmlmodel.JavaType.JavaAttributes) XmlSchema(org.eclipse.persistence.jaxb.xmlmodel.XmlSchema) XmlRegistries(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.XmlRegistries) XmlElement(org.eclipse.persistence.jaxb.xmlmodel.XmlElement) XmlJavaTypeAdapters(org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapters)

Example 4 with JavaTypes

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

the class XMLBindingsTestCases method getControlObject.

@Override
protected Object getControlObject() {
    XmlBindings xmlBindings = new XmlBindings();
    xmlBindings.setPackageName("myPackage");
    JavaTypes types = new JavaTypes();
    JavaType javaType = new JavaType();
    javaType.setName("myType");
    JavaAttributes javaAttributes = new JavaAttributes();
    XmlElement javaAttribute = new XmlElement();
    javaAttribute.setName("elementName");
    javaAttribute.setJavaAttribute("theJavaAttributeValue");
    JAXBElement<XmlElement> jbe = new JAXBElement<XmlElement>(new QName("http://www.eclipse.org/eclipselink/xsds/persistence/oxm", "xml-element"), XmlElement.class, javaAttribute);
    javaAttributes.getJavaAttribute().add(jbe);
    javaType.setJavaAttributes(javaAttributes);
    types.getJavaType().add(javaType);
    xmlBindings.setJavaTypes(types);
    return xmlBindings;
}
Also used : JavaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes) XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) JavaType(org.eclipse.persistence.jaxb.xmlmodel.JavaType) JavaAttributes(org.eclipse.persistence.jaxb.xmlmodel.JavaType.JavaAttributes) QName(javax.xml.namespace.QName) XmlElement(org.eclipse.persistence.jaxb.xmlmodel.XmlElement) JAXBElement(jakarta.xml.bind.JAXBElement)

Example 5 with JavaTypes

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

the class XmlBindingsGenerator method generateXmlBindings.

/**
 * Generate an XmlBindings instance based on a list of XML descriptors.
 *
 * OXM metadata files are processed on a per package basis, hence it is
 * assumed that the given list of descriptors are from the same package.
 */
public static XmlBindings generateXmlBindings(String packageName, List<XMLDescriptor> descriptors) {
    String defaultNamespace = null;
    Map<String, String> prefixMap = new HashMap<String, String>();
    JavaTypes jTypes = new JavaTypes();
    for (XMLDescriptor xdesc : descriptors) {
        // mappings to the map
        if (xdesc.getNamespaceResolver() != null) {
            if (defaultNamespace == null) {
                defaultNamespace = xdesc.getNamespaceResolver().getDefaultNamespaceURI();
            }
            Map<String, String> preMap = xdesc.getNamespaceResolver().getPrefixesToNamespaces();
            for (String pfx : preMap.keySet()) {
                // ignore mime prefix/url for now
                if (!pfx.equals(XML_MIME_PREFIX)) {
                    prefixMap.put(pfx, preMap.get(pfx));
                }
            }
        }
        // generate a JavaType instance for the XML descriptor
        jTypes.getJavaType().add(generateJavaType(xdesc));
    }
    XmlBindings xmlBindings = null;
    // if there are no JavaTypes, there's nothing to do
    if (jTypes.getJavaType().size() > 0) {
        xmlBindings = new XmlBindings();
        xmlBindings.setJavaTypes(jTypes);
        xmlBindings.setPackageName(packageName);
        // handle XmlSchema
        if (defaultNamespace != null || !prefixMap.isEmpty()) {
            XmlSchema xSchema = new XmlSchema();
            xSchema.setNamespace(defaultNamespace == null ? EMPTY_STRING : defaultNamespace);
            xSchema.setElementFormDefault(XmlNsForm.QUALIFIED);
            // handle XmlNs
            if (!prefixMap.isEmpty()) {
                XmlNs xmlNs;
                for (String pfx : prefixMap.keySet()) {
                    xmlNs = new XmlNs();
                    xmlNs.setNamespaceUri(prefixMap.get(pfx));
                    xmlNs.setPrefix(pfx);
                    xSchema.getXmlNs().add(xmlNs);
                }
            }
            xmlBindings.setXmlSchema(xSchema);
        }
    }
    return xmlBindings;
}
Also used : JavaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes) XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) HashMap(java.util.HashMap) XmlSchema(org.eclipse.persistence.jaxb.xmlmodel.XmlSchema) XmlNs(org.eclipse.persistence.jaxb.xmlmodel.XmlSchema.XmlNs)

Aggregations

XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)5 JavaTypes (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes)5 JavaType (org.eclipse.persistence.jaxb.xmlmodel.JavaType)4 HashMap (java.util.HashMap)3 XmlEnums (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.XmlEnums)3 XmlEnum (org.eclipse.persistence.jaxb.xmlmodel.XmlEnum)3 JAXBElement (jakarta.xml.bind.JAXBElement)2 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)2 JavaAttributes (org.eclipse.persistence.jaxb.xmlmodel.JavaType.JavaAttributes)2 XmlRegistries (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.XmlRegistries)2 XmlElement (org.eclipse.persistence.jaxb.xmlmodel.XmlElement)2 XmlJavaTypeAdapter (org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter)2 XmlProperty (org.eclipse.persistence.jaxb.xmlmodel.XmlProperties.XmlProperty)2 XmlRegistry (org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry)2 XmlSchema (org.eclipse.persistence.jaxb.xmlmodel.XmlSchema)2 XmlSchemaType (org.eclipse.persistence.jaxb.xmlmodel.XmlSchemaType)2 XmlSchemaTypes (org.eclipse.persistence.jaxb.xmlmodel.XmlSchemaTypes)2 Map (java.util.Map)1