Search in sources :

Example 11 with Schema

use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.

the class SchemaModelGenerator method handleFragNamespace.

/**
 * This method will generate a global element if required (based in URI and elementFormDefault) and
 * set a reference to it on a given element accordingly, or set an anonymous complex type on a given
 * element.  This method will typically be used by composite mappings.
 */
protected Element handleFragNamespace(XPathFragment frag, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties, Element element, ComplexType ctype, Descriptor refDesc) {
    String fragUri = frag.getNamespaceURI();
    // may need to add a global element
    Element globalElement = null;
    Schema s = getSchema(fragUri, null, schemaForNamespace, properties);
    String targetNS = workingSchema.getTargetNamespace();
    if ((s.isElementFormDefault() && !fragUri.equals(targetNS)) || (!s.isElementFormDefault() && fragUri.length() > 0)) {
        globalElement = s.getTopLevelElements().get(frag.getLocalName());
        if (globalElement == null) {
            globalElement = new Element();
            globalElement.setName(frag.getLocalName());
            if (ctype != null) {
                globalElement.setComplexType(ctype);
            } else {
                globalElement.setType(getSchemaTypeString(refDesc.getSchemaReference().getSchemaContextAsQName(workingSchema.getNamespaceResolver()), workingSchema));
            }
            s.addTopLevelElement(globalElement);
        }
        element = new Element();
        element.setMaxOccurs(Occurs.UNBOUNDED);
        element.setRef(frag.getShortName());
    }
    if (globalElement == null && ctype != null) {
        element.setComplexType(ctype);
    }
    return element;
}
Also used : Element(org.eclipse.persistence.internal.oxm.schema.model.Element) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema)

Example 12 with Schema

use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.

the class SchemaModelGenerator method buildSchemaComponentsForXPath.

/**
 * This method will build element/complexType/sequence components for a given XPath,
 * and return the sequence that the target element of the mapping should be added
 * to. For example, if the XPath was "contact-info/address/street/text()", street
 * would be the target.  This method defers processing of the target path element
 * to the calling method, allowing for differences in handling, such as direct
 * mappings versus composite mappings, etc.
 */
protected Sequence buildSchemaComponentsForXPath(XPathFragment frag, Sequence seq, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties) {
    // the mapping will handle processing of the target fragment; return the sequence it will be added to
    if (frag.getNextFragment() == null || frag.getNextFragment().nameIsText()) {
        return seq;
    }
    Sequence currentSequence = seq;
    // if the current element exists, use it; otherwise create a new one
    Element currentElement = elementExistsInSequence(frag.getLocalName(), frag.getShortName(), currentSequence);
    boolean currentElementExists = (currentElement != null);
    if (currentElement == null) {
        currentElement = new Element();
        // don't set the element name yet, as it may end up being a ref
        ComplexType cType = new ComplexType();
        Sequence sequence = new Sequence();
        cType.setSequence(sequence);
        currentElement.setComplexType(cType);
    }
    Element globalElement = null;
    String fragUri = frag.getNamespaceURI();
    if (fragUri != null) {
        Schema s = getSchema(fragUri, null, schemaForNamespace, properties);
        String targetNS = workingSchema.getTargetNamespace();
        if ((s.isElementFormDefault() && !fragUri.equals(targetNS)) || (!s.isElementFormDefault() && fragUri.length() > 0)) {
            // must generate a global element are create a reference to it
            // if the global element exists, use it; otherwise create a new one
            globalElement = s.getTopLevelElements().get(frag.getLocalName());
            if (globalElement == null) {
                globalElement = new Element();
                globalElement.setName(frag.getLocalName());
                ComplexType gCType = new ComplexType();
                Sequence gSequence = new Sequence();
                gCType.setSequence(gSequence);
                globalElement.setComplexType(gCType);
                s.addTopLevelElement(globalElement);
            }
            // if the current element doesn't exist set a ref and add it to the sequence
            if (!currentElementExists) {
                // ref won't have a complex type
                currentElement.setComplexType(null);
                currentElement.setRef(frag.getShortName());
                currentSequence.addElement(currentElement);
                currentElementExists = true;
            }
            // make the global element current
            currentElement = globalElement;
        }
    }
    // if we didn't process a global element, and the current element isn't already in the sequence, add it
    if (!currentElementExists && globalElement == null) {
        currentElement.setName(frag.getLocalName());
        currentSequence.addElement(currentElement);
    }
    // set the correct sequence to use/return
    currentSequence = currentElement.getComplexType().getSequence();
    // don't process the last element in the path - let the calling mapping process it
    frag = frag.getNextFragment();
    if (frag.getNextFragment() != null && !frag.getNextFragment().nameIsText()) {
        Element childElt = buildElement(frag, null, Occurs.ZERO, null);
        currentSequence.addElement(childElt);
    }
    // call back into this method to process the next path element
    return buildSchemaComponentsForXPath(frag, currentSequence, schemaForNamespace, workingSchema, properties);
}
Also used : Element(org.eclipse.persistence.internal.oxm.schema.model.Element) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Sequence(org.eclipse.persistence.internal.oxm.schema.model.Sequence) ComplexType(org.eclipse.persistence.internal.oxm.schema.model.ComplexType)

Example 13 with Schema

use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.

the class SchemaModelGenerator method buildNewSchema.

/**
 * Create and return a new schema for the given namespace.  ElementFormDefault and
 * AttributeFormDefault can be set via SchemaModelGeneratorProperties object.  The
 * namespace resolver's default namespace will be set if non-null.
 */
protected Schema buildNewSchema(String uri, NamespaceResolver nr, int schemaCount, SchemaModelGeneratorProperties properties) {
    Schema schema = new Schema();
    schema.setName(SCHEMA_FILE_NAME + schemaCount + SCHEMA_FILE_EXT);
    String defaultNamespace = null;
    if (nr != null) {
        defaultNamespace = nr.getDefaultNamespaceURI();
        if (defaultNamespace != null) {
            schema.setDefaultNamespace(defaultNamespace);
            schema.getNamespaceResolver().setDefaultNamespaceURI(defaultNamespace);
        }
    }
    if (!uri.equals(Constants.EMPTY_STRING)) {
        schema.setTargetNamespace(uri);
        String prefix = null;
        if (nr != null) {
            prefix = nr.resolveNamespaceURI(uri);
        }
        if (prefix == null && !uri.equals(defaultNamespace)) {
            prefix = schema.getNamespaceResolver().generatePrefix();
            schema.getNamespaceResolver().put(prefix, uri);
        }
    }
    if (properties != null) {
        // set elementFormDefault and attributeFormDefault to qualified if necessary
        Properties props = properties.getProperties(uri);
        if (props != null) {
            if (props.containsKey(SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY)) {
                schema.setElementFormDefault((Boolean) props.get(SchemaModelGeneratorProperties.ELEMENT_FORM_QUALIFIED_KEY));
            }
            if (props.containsKey(SchemaModelGeneratorProperties.ATTRIBUTE_FORM_QUALIFIED_KEY)) {
                schema.setAttributeFormDefault((Boolean) props.get(SchemaModelGeneratorProperties.ATTRIBUTE_FORM_QUALIFIED_KEY));
            }
        }
    }
    return schema;
}
Also used : Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Properties(java.util.Properties)

Example 14 with Schema

use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.

the class SchemaModelGenerator method generateSchemas.

/**
 * Generates a Map of EclipseLink schema model Schema objects for a given list of XMLDescriptors.
 * The descriptors are assumed to have been initialized.  One Schema  object will be generated
 * per namespace.
 *
 * @param descriptorsToProcess list of XMLDescriptors which will be used to generate Schema objects
 * @param properties holds a namespace to Properties map containing schema settings, such as elementFormDefault
 * @param additionalGlobalElements a map of QName-Type entries identifying additional global elements to be added
 * @return a map of namespaces to EclipseLink schema model Schema objects
 * @throws DescriptorException if the reference descriptor for a composite mapping is not in the list of descriptors
 * @see Schema
 */
public Map<String, Schema> generateSchemas(List<Descriptor> descriptorsToProcess, SchemaModelGeneratorProperties properties, Map<QName, Type> additionalGlobalElements) throws DescriptorException {
    Map<String, Schema> schemaForNamespace = generateSchemas(descriptorsToProcess, properties);
    // process any additional global elements
    if (additionalGlobalElements != null) {
        for (Entry<QName, Type> entry : additionalGlobalElements.entrySet()) {
            QName qname = entry.getKey();
            Type type = entry.getValue();
            if (type instanceof Class) {
                Class<?> tClass = (Class) type;
                String nsKey = qname.getNamespaceURI();
                Schema schema = schemaForNamespace.get(nsKey);
                QName typeAsQName = conversionManager.schemaType(tClass);
                if (typeAsQName == null) {
                    // not a built in type - need to get the type via schema reference
                    Descriptor desc = getDescriptorByClass(tClass, descriptorsToProcess);
                    if (desc == null) {
                        // at this point we can't determine the element type, so don't add anything
                        continue;
                    }
                    // if the schema is null generate a new one and create an import
                    if (schema == null) {
                        schema = buildNewSchema(nsKey, new org.eclipse.persistence.oxm.NamespaceResolver(), schemaForNamespace.size(), properties);
                        schemaForNamespace.put(nsKey, schema);
                        typeAsQName = desc.getSchemaReference().getSchemaContextAsQName();
                        Schema schemaForUri = schemaForNamespace.get(typeAsQName.getNamespaceURI());
                        if (!importExists(schema, schemaForUri.getTargetNamespace())) {
                            Import newImport = new Import();
                            newImport.setNamespace(schemaForUri.getTargetNamespace());
                            newImport.setSchemaLocation(schemaForUri.getName());
                            schema.getImports().add(newImport);
                        }
                    } else {
                        typeAsQName = desc.getSchemaReference().getSchemaContextAsQName(schema.getNamespaceResolver());
                    }
                }
                if (schema == null) {
                    schema = buildNewSchema(nsKey, new org.eclipse.persistence.oxm.NamespaceResolver(), schemaForNamespace.size(), properties);
                    schemaForNamespace.put(nsKey, schema);
                }
                Element element = new Element();
                element.setName(qname.getLocalPart());
                element.setType(getSchemaTypeString(typeAsQName, schema));
                schema.addTopLevelElement(element);
            }
        }
    }
    return schemaForNamespace;
}
Also used : Import(org.eclipse.persistence.internal.oxm.schema.model.Import) QName(javax.xml.namespace.QName) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Element(org.eclipse.persistence.internal.oxm.schema.model.Element) ComplexType(org.eclipse.persistence.internal.oxm.schema.model.ComplexType) Type(java.lang.reflect.Type) SimpleType(org.eclipse.persistence.internal.oxm.schema.model.SimpleType) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver)

Example 15 with Schema

use of org.eclipse.persistence.internal.oxm.schema.model.Schema in project eclipselink by eclipse-ee4j.

the class XRServiceFactory method loadXMLSchema.

/**
 * <p>INTERNAL:
 * Read and unmarshal <code>XRService</code>'s <code>.xsd</code> file.
 * @param xrSchemaStream Stream resource for the <code>XRService</code>'s <code>.xsd</code> file.
 */
public void loadXMLSchema(InputStream xrSchemaStream) {
    SchemaModelProject schemaProject = new SchemaModelProject();
    XMLContext xmlContext = new XMLContext(schemaProject);
    XMLUnmarshaller unmarshaller = xmlContext.createUnmarshaller();
    Schema schema;
    try {
        schema = (Schema) unmarshaller.unmarshal(xrSchemaStream);
    } catch (XMLMarshalException e) {
        xmlContext.getSession().getSessionLog().log(SessionLog.WARNING, SessionLog.DBWS, "dbws_xml_schema_read_error", e.getLocalizedMessage());
        throw new DBWSException(OXM_PROCESSING_SCH, e);
    }
    NamespaceResolver nr = schema.getNamespaceResolver();
    String targetNamespace = schema.getTargetNamespace();
    nr.put(TARGET_NAMESPACE_PREFIX, targetNamespace);
    xrService.schema = schema;
    xrService.schemaNamespace = targetNamespace;
}
Also used : DBWSException(org.eclipse.persistence.exceptions.DBWSException) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) XMLContext(org.eclipse.persistence.oxm.XMLContext) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Aggregations

Schema (org.eclipse.persistence.internal.oxm.schema.model.Schema)44 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)18 SchemaModelProject (org.eclipse.persistence.internal.oxm.schema.SchemaModelProject)17 Project (org.eclipse.persistence.sessions.Project)17 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)16 Element (org.eclipse.persistence.internal.oxm.schema.model.Element)15 ComplexType (org.eclipse.persistence.internal.oxm.schema.model.ComplexType)14 SchemaModelGeneratorProperties (org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties)13 XmlVirtualAccessMethodsSchema (org.eclipse.persistence.jaxb.xmlmodel.XmlVirtualAccessMethodsSchema)12 XMLContext (org.eclipse.persistence.oxm.XMLContext)12 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)10 Document (org.w3c.dom.Document)10 QName (javax.xml.namespace.QName)7 StreamSource (javax.xml.transform.stream.StreamSource)7 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)7 Sequence (org.eclipse.persistence.internal.oxm.schema.model.Sequence)6 TypeDefParticle (org.eclipse.persistence.internal.oxm.schema.model.TypeDefParticle)6 IOException (java.io.IOException)5 StringWriter (java.io.StringWriter)5 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)5