Search in sources :

Example 1 with Namespace

use of org.eclipse.persistence.internal.oxm.Namespace in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method generateWrapperForMapClass.

private Class<?> generateWrapperForMapClass(JavaClass mapClass, JavaClass keyClass, JavaClass valueClass, TypeMappingInfo typeMappingInfo) {
    String packageName = JAXB_DEV;
    NamespaceResolver combinedNamespaceResolver = new NamespaceResolver();
    if (!helper.isBuiltInJavaType(keyClass)) {
        String keyPackageName = keyClass.getPackageName();
        packageName = packageName + DOT_CHR + keyPackageName;
        NamespaceInfo keyNamespaceInfo = getPackageInfoForPackage(keyClass).getNamespaceInfo();
        if (keyNamespaceInfo != null) {
            java.util.Vector<Namespace> namespaces = keyNamespaceInfo.getNamespaceResolver().getNamespaces();
            for (Namespace n : namespaces) {
                combinedNamespaceResolver.put(n.getPrefix(), n.getNamespaceURI());
            }
        }
    }
    if (!helper.isBuiltInJavaType(valueClass)) {
        String valuePackageName = valueClass.getPackageName();
        packageName = packageName + DOT_CHR + valuePackageName;
        NamespaceInfo valueNamespaceInfo = getPackageInfoForPackage(valueClass).getNamespaceInfo();
        if (valueNamespaceInfo != null) {
            java.util.Vector<Namespace> namespaces = valueNamespaceInfo.getNamespaceResolver().getNamespaces();
            for (Namespace n : namespaces) {
                combinedNamespaceResolver.put(n.getPrefix(), n.getNamespaceURI());
            }
        }
    }
    String namespace = this.defaultTargetNamespace;
    if (namespace == null) {
        namespace = EMPTY_STRING;
    }
    PackageInfo packageInfo = packageToPackageInfoMappings.get(mapClass.getPackageName());
    if (packageInfo == null) {
        packageInfo = getPackageToPackageInfoMappings().get(packageName);
    } else {
        if (packageInfo.getNamespace() != null) {
            namespace = packageInfo.getNamespace();
        }
        getPackageToPackageInfoMappings().put(packageName, packageInfo);
    }
    if (packageInfo == null) {
        packageInfo = new PackageInfo();
        packageInfo.setNamespaceInfo(new NamespaceInfo());
        packageInfo.setNamespace(namespace);
        packageInfo.setNamespaceResolver(combinedNamespaceResolver);
        getPackageToPackageInfoMappings().put(packageName, packageInfo);
    }
    int beginIndex = keyClass.getName().lastIndexOf(DOT_CHR) + 1;
    String keyName = keyClass.getName().substring(beginIndex);
    int dollarIndex = keyName.indexOf(DOLLAR_SIGN_CHR);
    if (dollarIndex > -1) {
        keyName = keyName.substring(dollarIndex + 1);
    }
    beginIndex = valueClass.getName().lastIndexOf(DOT_CHR) + 1;
    String valueName = valueClass.getName().substring(beginIndex);
    dollarIndex = valueName.indexOf(DOLLAR_SIGN_CHR);
    if (dollarIndex > -1) {
        valueName = valueName.substring(dollarIndex + 1);
    }
    String collectionClassShortName = mapClass.getRawName().substring(mapClass.getRawName().lastIndexOf(DOT_CHR) + 1);
    String suggestedClassName = keyName + valueName + collectionClassShortName;
    String qualifiedClassName = packageName + DOT_CHR + suggestedClassName;
    qualifiedClassName = getNextAvailableClassName(qualifiedClassName);
    String qualifiedInternalClassName = qualifiedClassName.replace(DOT_CHR, SLASH_CHR);
    String internalKeyName = keyClass.getQualifiedName().replace(DOT_CHR, SLASH_CHR);
    String internalValueName = valueClass.getQualifiedName().replace(DOT_CHR, SLASH_CHR);
    Type mapType = Type.getType(L + mapClass.getRawName().replace(DOT_CHR, SLASH_CHR) + SEMI_COLON);
    EclipseLinkASMClassWriter cw = new EclipseLinkASMClassWriter();
    String sig = "Lorg/eclipse/persistence/internal/jaxb/many/MapValue<L" + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;>;";
    cw.visit(Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, qualifiedInternalClassName, sig, "org/eclipse/persistence/internal/jaxb/many/MapValue", null);
    // Write Field: @... public Map entry
    String fieldSig = L + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;";
    FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC, "entry", L + mapType.getInternalName() + SEMI_COLON, fieldSig, null);
    fv.visitAnnotation(Type.getDescriptor(XmlElement.class), true);
    if (typeMappingInfo != null) {
        Annotation[] annotations = typeMappingInfo.getAnnotations();
        if (annotations != null) {
            for (Annotation nextAnnotation : annotations) {
                if (nextAnnotation != null && !(nextAnnotation instanceof XmlElement) && !(nextAnnotation instanceof XmlJavaTypeAdapter)) {
                    String annotationClassName = nextAnnotation.annotationType().getName();
                    AnnotationVisitor av = fv.visitAnnotation(L + annotationClassName.replace(DOT_CHR, SLASH_CHR) + SEMI_COLON, true);
                    for (Method next : nextAnnotation.annotationType().getDeclaredMethods()) {
                        try {
                            Object nextValue = next.invoke(nextAnnotation);
                            if (nextValue instanceof Class) {
                                Type nextType = Type.getType(L + ((Class) nextValue).getName().replace(DOT_CHR, SLASH_CHR) + SEMI_COLON);
                                nextValue = nextType;
                            }
                            av.visit(next.getName(), nextValue);
                        } catch (InvocationTargetException ignored) {
                        // ignore the invocation target exception here.
                        } catch (IllegalAccessException ignored) {
                        // ignore the illegal access exception here.
                        }
                    }
                    av.visitEnd();
                }
            }
        }
    }
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "org/eclipse/persistence/internal/jaxb/many/MapValue", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    // Write: @XmlTransient public void setItem(???)
    String methodSig = "(L" + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;)V";
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setItem", "(L" + mapType.getInternalName() + ";)V", methodSig, null);
    // TODO: Verify that we really want to put @XmlTranient on setItem
    // method
    mv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlTransient;", true);
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, qualifiedInternalClassName, "entry", L + mapType.getInternalName() + SEMI_COLON);
    mv.visitInsn(Opcodes.RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    // Replacement?:LocalVariableTypeTableAttribute cvAttr = new
    // LocalVariableTypeTableAttribute();
    // mv.visitAttribute(cvAttr);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
    // Write @XmlTransient public ??? getItem()
    methodSig = "()L" + mapType.getInternalName() + "<L" + internalKeyName + ";L" + internalValueName + ";>;";
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getItem", "()L" + mapType.getInternalName() + SEMI_COLON, methodSig, null);
    mv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlTransient;", true);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, qualifiedInternalClassName, "entry", L + mapType.getInternalName() + SEMI_COLON);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_BRIDGE + Opcodes.ACC_SYNTHETIC, "getItem", "()Ljava/lang/Object;", null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, qualifiedInternalClassName, "getItem", "()L" + mapType.getInternalName() + SEMI_COLON, false);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_BRIDGE + Opcodes.ACC_SYNTHETIC, "setItem", "(Ljava/lang/Object;)V", null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitTypeInsn(Opcodes.CHECKCAST, mapType.getInternalName());
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, qualifiedInternalClassName, "setItem", "(L" + mapType.getInternalName() + ";)V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
    // Write @XmlType(namespace)
    AnnotationVisitor av = cw.visitAnnotation("Ljakarta/xml/bind/annotation/XmlType;", true);
    av.visit("namespace", namespace);
    cw.visitEnd();
    byte[] classBytes = cw.toByteArray();
    return generateClassFromBytes(qualifiedClassName, classBytes);
}
Also used : XmlJavaTypeAdapter(jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter) Label(org.eclipse.persistence.internal.libraries.asm.Label) EclipseLinkASMClassWriter(org.eclipse.persistence.internal.libraries.asm.EclipseLinkASMClassWriter) Method(java.lang.reflect.Method) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod) FieldVisitor(org.eclipse.persistence.internal.libraries.asm.FieldVisitor) Namespace(org.eclipse.persistence.internal.oxm.Namespace) JavaAnnotation(org.eclipse.persistence.jaxb.javamodel.JavaAnnotation) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodVisitor(org.eclipse.persistence.internal.libraries.asm.MethodVisitor) Type(org.eclipse.persistence.internal.libraries.asm.Type) XmlType(jakarta.xml.bind.annotation.XmlType) XmlAccessorType(jakarta.xml.bind.annotation.XmlAccessorType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlMimeType(jakarta.xml.bind.annotation.XmlMimeType) XmlAccessType(org.eclipse.persistence.jaxb.xmlmodel.XmlAccessType) XmlSchemaType(jakarta.xml.bind.annotation.XmlSchemaType) AnnotationVisitor(org.eclipse.persistence.internal.libraries.asm.AnnotationVisitor) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XmlElement(jakarta.xml.bind.annotation.XmlElement) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass)

Example 2 with Namespace

use of org.eclipse.persistence.internal.oxm.Namespace in project eclipselink by eclipse-ee4j.

the class SchemaGenerator method getSchemaForNamespace.

/**
 * Return the Schema for a given namespace.  If no schema exists for the
 * given namespace, one will be created.
 */
private Schema getSchemaForNamespace(String namespace, String packageName) {
    if (schemaForNamespace == null) {
        schemaForNamespace = new HashMap<>();
        allSchemas = new ArrayList<>();
    }
    Schema schema = schemaForNamespace.get(namespace);
    if (schema == null && !javax.xml.XMLConstants.XML_NS_URI.equals(namespace)) {
        schema = new Schema();
        String schemaName = SCHEMA + schemaCount + SCHEMA_EXT;
        NamespaceInfo namespaceInfo = getNamespaceInfoForNamespace(namespace, packageName);
        if (namespaceInfo != null) {
            if (namespaceInfo.getLocation() != null && !namespaceInfo.getLocation().equals(GENERATE)) {
                return null;
            }
            java.util.Vector<Namespace> namespaces = namespaceInfo.getNamespaceResolver().getNamespaces();
            for (int i = 0; i < namespaces.size(); i++) {
                Namespace nextNamespace = namespaces.get(i);
                schema.getNamespaceResolver().put(nextNamespace.getPrefix(), nextNamespace.getNamespaceURI());
            }
        }
        if (outputResolver != null) {
            try {
                Result res = outputResolver.createOutput(namespace, schemaName);
                // if the resolver returns null, schema generation for this namespace URI will be skipped
                if (res == null) {
                    return null;
                }
                schema.setResult(res);
                if (res.getSystemId() != null) {
                    schemaName = res.getSystemId();
                    // may use schema name to create a URI, which expects '/' as file separator
                    schemaName = schemaName.replace(SLASHES, SLASH);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        schema.setName(schemaName);
        schemaCount++;
        if (!namespace.equals(EMPTY_STRING)) {
            schema.setTargetNamespace(namespace);
            String prefix = null;
            if (namespaceInfo != null) {
                prefix = namespaceInfo.getNamespaceResolver().resolveNamespaceURI(namespace);
            }
            if (prefix == null) {
                prefix = schema.getNamespaceResolver().generatePrefix();
            }
            schema.getNamespaceResolver().put(prefix, namespace);
        }
        if (namespaceInfo != null) {
            schema.setAttributeFormDefault(namespaceInfo.isAttributeFormQualified());
            schema.setElementFormDefault(namespaceInfo.isElementFormQualified());
        }
        schemaForNamespace.put(namespace, schema);
        allSchemas.add(schema);
    }
    return schema;
}
Also used : Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) XmlVirtualAccessMethodsSchema(org.eclipse.persistence.jaxb.xmlmodel.XmlVirtualAccessMethodsSchema) IOException(java.io.IOException) Namespace(org.eclipse.persistence.internal.oxm.Namespace) JavaClassCompareByNamespace(org.eclipse.persistence.jaxb.javamodel.JavaClassCompareByNamespace) Result(javax.xml.transform.Result)

Example 3 with Namespace

use of org.eclipse.persistence.internal.oxm.Namespace in project eclipselink by eclipse-ee4j.

the class SDOMarshalListener method beforeMarshal.

@Override
public void beforeMarshal(Object obj) {
    if (obj instanceof SDOChangeSummary) {
        SDOChangeSummary changeSummary = ((SDOChangeSummary) obj);
        // CREATED - build a list of xpaths to write to the created attribute
        // this must be done dynamically because the xpath is relative to the marshalledObject
        // so it can't be calculated until we know what object is being marshalled
        List createdSet = changeSummary.getCreated();
        List xpaths = new ArrayList(createdSet.size());
        String rootElementName = this.marshalledObjectRootQName.getLocalPart();
        String rootNamespaceUri = this.marshalledObjectRootQName.getNamespaceURI();
        if (rootNamespaceUri != null && !rootNamespaceUri.equals(XMLConstants.EMPTY_STRING)) {
            org.eclipse.persistence.internal.oxm.NamespaceResolver resolver = getRootMarshalRecord().getNamespaceResolver();
            if (resolver != null) {
                String prefix = resolver.resolveNamespaceURI(this.marshalledObjectRootQName.getNamespaceURI());
                if (prefix != null) {
                    rootElementName = prefix + XMLConstants.COLON + rootElementName;
                }
            }
        }
        if ((createdSet != null) && (createdSet.size() > 0)) {
            Iterator anIterator = createdSet.iterator();
            SDODataObject nextCreatedDO = null;
            while (anIterator.hasNext()) {
                // get path to the changeSummaryRoot (may not be the root marshalled object - may be internal)
                nextCreatedDO = ((SDODataObject) anIterator.next());
                String nextPath = getPathFromAncestor(nextCreatedDO, (SDODataObject) marshalledObject, changeSummary);
                // Add sdoRef attribute...all modified objects written should have this
                if (nextPath == SDOConstants.EMPTY_STRING) {
                    // if this is the root, just put the root element
                    xpaths.add(SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + // 
                    SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName);
                } else {
                    xpaths.add(SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + // 
                    SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + nextPath);
                }
            }
        }
        changeSummary.setCreatedXPaths(xpaths);
        // Build xpathToCS
        String xpathMarshalledObjToCS = getPathFromAncestor(changeSummary.getRootObject(), (SDODataObject) marshalledObject, changeSummary);
        String xpathChangeSumProp = getXPathForProperty(changeSummary.getRootObject().getType().getChangeSummaryProperty());
        String xpathToCS = SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName;
        // check if the CS is at the local-cs-root or is in a child property
        if ((xpathMarshalledObjToCS != null) && !xpathMarshalledObjToCS.equals(SDOConstants.EMPTY_STRING)) {
            // SDO_XPATH_TO_ROOT)) {
            // CS is not on the root
            xpathToCS = xpathToCS + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + xpathMarshalledObjToCS;
        }
        xpathToCS = // 
        xpathToCS + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + xpathChangeSumProp + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT;
        // MODIFIED && DELETED
        List deletedXPaths = new ArrayList();
        Document document = XMLPlatformFactory.getInstance().getXMLPlatform().createDocument();
        Element csNode = null;
        List modifiedItems = changeSummary.getModified();
        int modifiedSize = modifiedItems.size();
        List newNodes = new ArrayList(modifiedSize);
        SDODataObject nextModifiedDO = null;
        // Iterate through CS modified items
        for (int i = 0; i < modifiedSize; i++) {
            nextModifiedDO = (SDODataObject) modifiedItems.get(i);
            String sdoPrefix = typeHelper.getPrefix(SDOConstants.SDO_URL);
            // List unsetPropNames = new ArrayList();
            String uri = getURI(nextModifiedDO);
            String qualifiedName = getQualifiedName(nextModifiedDO);
            String sdoRefPrefix = SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT;
            if (uri == null) {
                csNode = document.createElement(qualifiedName);
            } else {
                csNode = document.createElementNS(uri, qualifiedName);
            }
            String nextPath = getPathFromAncestor(nextModifiedDO, (SDODataObject) marshalledObject, changeSummary);
            // Add sdoRef attribute...all modified objects written should have this
            if (nextPath == SDOConstants.EMPTY_STRING) {
                // if this is the root, just put the root element
                csNode.setAttributeNS(SDOConstants.SDO_URL, // 
                sdoPrefix + // 
                SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + // 
                SDOConstants.CHANGESUMMARY_REF, sdoRefPrefix + rootElementName);
            } else {
                csNode.setAttributeNS(SDOConstants.SDO_URL, // 
                sdoPrefix + // 
                SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + // 
                SDOConstants.CHANGESUMMARY_REF, sdoRefPrefix + rootElementName + "/" + nextPath);
            }
            // Bug6346754 Add all namespaces if they are not yet declared above.
            Vector namespaces = nextModifiedDO.getType().getXmlDescriptor().getNonNullNamespaceResolver().getNamespaces();
            for (int j = 0; j < namespaces.size(); j++) {
                Namespace next = (Namespace) namespaces.get(j);
                if (declareNamespace(next.getNamespaceURI(), next.getPrefix(), changeSummary.getRootObject())) {
                    csNode.setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + XMLConstants.COLON + next.getPrefix(), next.getNamespaceURI());
                }
            }
            List nextDOSettings = changeSummary.getOldValues(nextModifiedDO);
            DOMRecord row = new DOMRecord(csNode);
            Session session = ((SDOXMLHelper) typeHelper.getHelperContext().getXMLHelper()).getXmlContext().getSession();
            row.setSession((AbstractSession) session);
            // Iterate through SDOSettings for the current modified Object
            SDOSetting nextSetting = null;
            for (int j = 0; j < nextDOSettings.size(); j++) {
                nextSetting = (SDOSetting) nextDOSettings.get(j);
                if (nextSetting.isSet()) {
                    if (!nextSetting.getProperty().getType().isDataType()) {
                        if (nextSetting.getProperty().isMany()) {
                            List values = (List) nextSetting.getValue();
                            for (int k = 0; k < values.size(); k++) {
                                doMarshal(// 
                                nextSetting.getProperty(), // 
                                (DataObject) values.get(k), changeSummary, csNode, nextModifiedDO, deletedXPaths, xpathToCS, sdoPrefix, rootElementName);
                            }
                        } else {
                            doMarshal(// 
                            nextSetting.getProperty(), // 
                            (DataObject) nextSetting.getValue(), changeSummary, csNode, nextModifiedDO, deletedXPaths, xpathToCS, sdoPrefix, rootElementName);
                        }
                    } else {
                        // This writes out simple values
                        Object value = nextSetting.getValue();
                        if (value == null) {
                            // Marshal out xsi:nil=true
                            marshalNilAttribute(nextSetting.getProperty(), row);
                        } else {
                            String xPath = getXPathForProperty(nextSetting.getProperty());
                            XMLField field = new XMLField(xPath);
                            field.setNamespaceResolver(typeHelper.getNamespaceResolver());
                            row.put(field, value);
                        }
                    }
                }
            }
            List unsetPropNames = changeSummary.getUnsetProps(nextModifiedDO);
            if (!unsetPropNames.isEmpty()) {
                XMLConversionManager xmlConversionManager = ((SDOXMLHelper) typeHelper.getHelperContext().getXMLHelper()).getXmlConversionManager();
                String unsetPropsString = xmlConversionManager.convertObject(unsetPropNames, String.class);
                csNode.setAttributeNS(SDOConstants.SDO_URL, // 
                sdoPrefix + // 
                SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + SDOConstants.CHANGESUMMARY_UNSET, unsetPropsString);
            }
            newNodes.add(csNode);
        }
        changeSummary.setDeletedXPaths(deletedXPaths);
        changeSummary.setModifiedDoms(newNodes);
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) SDOSetting(org.eclipse.persistence.sdo.SDOSetting) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) Namespace(org.eclipse.persistence.internal.oxm.Namespace) SDOChangeSummary(org.eclipse.persistence.sdo.SDOChangeSummary) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) Vector(java.util.Vector) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 4 with Namespace

use of org.eclipse.persistence.internal.oxm.Namespace in project eclipselink by eclipse-ee4j.

the class SDOSchemaGenerator method generate.

/**
 * <p>Method to generate an XSD. Note the following:<ul>
 * <li> All types must have same URI
 * <li> Referenced types in same URI will also be generated in schema
 * <li> Includes will never be generated
 * <li> Imports will be generated for referenced types in other URIs
 * </ul>
 * @param types The list of commonj.sdo.Type objects to generate the XSD from
 * @param aSchemaLocationResolver implementation of the org.eclipse.persistence.sdo.helper.SchemaLocationResolver interface
 * used for getting the value of the schemaLocation attribute of generated imports and includes
 * @return String The generated XSD.
 */
public String generate(List types, SchemaLocationResolver aSchemaLocationResolver) {
    schemaLocationResolver = aSchemaLocationResolver;
    if ((types == null) || (types.size() == 0)) {
        throw new IllegalArgumentException("No Schema was generated from null or empty list of types.");
    }
    String uri = null;
    Type firstType = (Type) types.get(0);
    uri = firstType.getURI();
    allTypes = types;
    generateSchema(uri, types);
    // Now we have a built schema model
    Project p = new SchemaModelProject();
    Vector<Namespace> generatedNamespaces = generatedSchema.getNamespaceResolver().getNamespaces();
    XMLDescriptor desc = ((XMLDescriptor) p.getDescriptor(Schema.class));
    for (int i = 0; i < generatedNamespaces.size(); i++) {
        Namespace next = generatedNamespaces.get(i);
        desc.getNamespaceResolver().put(next.getPrefix(), next.getNamespaceURI());
        if (next.getNamespaceURI().equals(SDOConstants.SDO_URL) || next.getNamespaceURI().equals(SDOConstants.SDOXML_URL) || next.getNamespaceURI().equals(SDOConstants.SDOJAVA_URL)) {
            if (!importExists(generatedSchema.getImports(), next.getNamespaceURI())) {
                Import theImport = new Import();
                theImport.setNamespace(next.getNamespaceURI());
                String schemaLocation = "classpath:/xml/";
                String customLocation = null;
                if (next.getNamespaceURI().equals(SDOConstants.SDO_URL)) {
                    if (schemaLocationResolver != null) {
                        customLocation = schemaLocationResolver.resolveSchemaLocation(firstType, SDOConstants.SDO_BOOLEAN);
                    }
                    if (customLocation != null) {
                        schemaLocation = customLocation;
                    } else {
                        schemaLocation += "sdoModel.xsd";
                    }
                } else if (next.getNamespaceURI().equals(SDOConstants.SDOXML_URL)) {
                    if (schemaLocationResolver != null) {
                        customLocation = schemaLocationResolver.resolveSchemaLocation(firstType, new SDOType(SDOConstants.SDOXML_URL, "XMLInfo"));
                    }
                    if (customLocation != null) {
                        schemaLocation = customLocation;
                    } else {
                        schemaLocation += "sdoXML.xsd";
                    }
                } else if (next.getNamespaceURI().equals(SDOConstants.SDOJAVA_URL)) {
                    if (schemaLocationResolver != null) {
                        customLocation = schemaLocationResolver.resolveSchemaLocation(firstType, SDOConstants.SDO_BOOLEANOBJECT);
                    }
                    if (customLocation != null) {
                        schemaLocation = customLocation;
                    } else {
                        schemaLocation += "sdoJava.xsd";
                    }
                }
                theImport.setSchemaLocation(schemaLocation);
                generatedSchema.getImports().add(theImport);
            }
        }
    }
    XMLLogin login = new XMLLogin();
    login.setDatasourcePlatform(new DOMPlatform());
    p.setDatasourceLogin(login);
    XMLContext context = new XMLContext(p);
    XMLMarshaller marshaller = context.createMarshaller();
    StringWriter generatedSchemaWriter = new StringWriter();
    marshaller.marshal(generatedSchema, generatedSchemaWriter);
    return generatedSchemaWriter.toString();
}
Also used : DOMPlatform(org.eclipse.persistence.oxm.platform.DOMPlatform) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) SDOType(org.eclipse.persistence.sdo.SDOType) XMLLogin(org.eclipse.persistence.oxm.XMLLogin) Namespace(org.eclipse.persistence.internal.oxm.Namespace) Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) Type(commonj.sdo.Type) SDOType(org.eclipse.persistence.sdo.SDOType) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) StringWriter(java.io.StringWriter)

Example 5 with Namespace

use of org.eclipse.persistence.internal.oxm.Namespace in project eclipselink by eclipse-ee4j.

the class AbstractMarshalRecordImpl method removeExtraNamespacesFromNamespaceResolver.

@Override
public void removeExtraNamespacesFromNamespaceResolver(List<Namespace> extraNamespaces, CoreAbstractSession session) {
    if (extraNamespaces == null) {
        return;
    }
    for (int i = 0; i < extraNamespaces.size(); i++) {
        Namespace nextExtraNamespace = extraNamespaces.get(i);
        String uri = namespaceResolver.resolveNamespacePrefix(nextExtraNamespace.getPrefix());
        if ((uri != null) && uri.equals(nextExtraNamespace.getNamespaceURI())) {
            namespaceResolver.removeNamespace(nextExtraNamespace.getPrefix());
        }
    }
}
Also used : Namespace(org.eclipse.persistence.internal.oxm.Namespace)

Aggregations

Namespace (org.eclipse.persistence.internal.oxm.Namespace)15 SchemaModelProject (org.eclipse.persistence.internal.oxm.schema.SchemaModelProject)6 XMLContext (org.eclipse.persistence.oxm.XMLContext)6 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)6 Project (org.eclipse.persistence.sessions.Project)6 StringWriter (java.io.StringWriter)4 Vector (java.util.Vector)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)3 Type (commonj.sdo.Type)2 DataObject (commonj.sdo.DataObject)1 XmlAccessorType (jakarta.xml.bind.annotation.XmlAccessorType)1 XmlElement (jakarta.xml.bind.annotation.XmlElement)1 XmlMimeType (jakarta.xml.bind.annotation.XmlMimeType)1 XmlSchemaType (jakarta.xml.bind.annotation.XmlSchemaType)1 XmlType (jakarta.xml.bind.annotation.XmlType)1 XmlJavaTypeAdapter (jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1