Search in sources :

Example 16 with Element

use of org.eclipse.persistence.internal.oxm.schema.model.Element 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 17 with Element

use of org.eclipse.persistence.internal.oxm.schema.model.Element 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 18 with Element

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

the class SchemaModelGenerator method processXMLCompositeDirectCollectionMapping.

/**
 * Process a given XMLCompositeDirectCollectionMapping.
 */
protected void processXMLCompositeDirectCollectionMapping(DirectCollectionMapping mapping, Sequence seq, ComplexType ct, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties) {
    Field xmlField = ((Field) (mapping).getField());
    XPathFragment frag = xmlField.getXPathFragment();
    seq = buildSchemaComponentsForXPath(frag, seq, schemaForNamespace, workingSchema, properties);
    frag = getTargetXPathFragment(frag);
    String schemaTypeString = getSchemaTypeForElement(xmlField, mapping.getAttributeElementClass(), workingSchema);
    Element element = null;
    if (xmlField.usesSingleNode()) {
        SimpleType st = new SimpleType();
        org.eclipse.persistence.internal.oxm.schema.model.List list = new org.eclipse.persistence.internal.oxm.schema.model.List();
        if (schemaTypeString == null) {
            schemaTypeString = getSchemaTypeString(Constants.ANY_SIMPLE_TYPE_QNAME, workingSchema);
        }
        list.setItemType(schemaTypeString);
        st.setList(list);
        element = buildElement(xmlField.getXPathFragment(), null, Occurs.ZERO, null);
        element.setSimpleType(st);
    } else {
        if (frag.getNamespaceURI() != null) {
            element = handleFragNamespace(frag, schemaForNamespace, workingSchema, properties, element, schemaTypeString);
            element.setMaxOccurs(Occurs.UNBOUNDED);
        } else {
            element = buildElement(frag, schemaTypeString, Occurs.ZERO, Occurs.UNBOUNDED);
        }
    }
    if (mapping.getNullPolicy().isNullRepresentedByXsiNil()) {
        element.setNillable(true);
    }
    if (xmlField.isRequired()) {
        element.setMinOccurs("1");
    }
    seq.addElement(element);
}
Also used : Element(org.eclipse.persistence.internal.oxm.schema.model.Element) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) Field(org.eclipse.persistence.internal.oxm.mappings.Field) SimpleType(org.eclipse.persistence.internal.oxm.schema.model.SimpleType) List(java.util.List) ArrayList(java.util.ArrayList)

Example 19 with Element

use of org.eclipse.persistence.internal.oxm.schema.model.Element 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 20 with Element

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

the class SchemaModelGenerator method processEnumeration.

/**
 * Process information contained within an EnumTypeConverter.  This will generate a simple
 * type containing enumeration info.
 */
protected void processEnumeration(String schemaTypeString, XPathFragment frag, DirectMapping mapping, Sequence seq, ComplexType ct, Schema workingSchema, CoreConverter converter) {
    Element elem = null;
    Attribute attr = null;
    if (frag.isAttribute()) {
        attr = buildAttribute(mapping, schemaTypeString);
    } else {
        elem = buildElement(frag, schemaTypeString, Occurs.ZERO, null);
    }
    Collection<String> fieldValues = ((EnumTypeConverter) converter).getAttributeToFieldValues().values();
    List<String> facets = new ArrayList<>(fieldValues);
    Restriction restriction = new Restriction();
    restriction.setEnumerationFacets(facets);
    SimpleType st = new SimpleType();
    st.setRestriction(restriction);
    if (frag.isAttribute()) {
        attr.setSimpleType(st);
        ct.getOrderedAttributes().add(attr);
    } else {
        elem.setSimpleType(st);
        seq.addElement(elem);
    }
}
Also used : Restriction(org.eclipse.persistence.internal.oxm.schema.model.Restriction) SimpleType(org.eclipse.persistence.internal.oxm.schema.model.SimpleType) AnyAttribute(org.eclipse.persistence.internal.oxm.schema.model.AnyAttribute) Attribute(org.eclipse.persistence.internal.oxm.schema.model.Attribute) Element(org.eclipse.persistence.internal.oxm.schema.model.Element) ArrayList(java.util.ArrayList)

Aggregations

Element (org.eclipse.persistence.internal.oxm.schema.model.Element)36 ComplexType (org.eclipse.persistence.internal.oxm.schema.model.ComplexType)15 Schema (org.eclipse.persistence.internal.oxm.schema.model.Schema)15 QName (javax.xml.namespace.QName)9 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)8 Field (org.eclipse.persistence.internal.oxm.mappings.Field)7 Sequence (org.eclipse.persistence.internal.oxm.schema.model.Sequence)7 SimpleType (org.eclipse.persistence.internal.oxm.schema.model.SimpleType)7 XmlVirtualAccessMethodsSchema (org.eclipse.persistence.jaxb.xmlmodel.XmlVirtualAccessMethodsSchema)7 Attribute (org.eclipse.persistence.internal.oxm.schema.model.Attribute)5 Choice (org.eclipse.persistence.internal.oxm.schema.model.Choice)5 TypeDefParticle (org.eclipse.persistence.internal.oxm.schema.model.TypeDefParticle)5 ArrayList (java.util.ArrayList)4 AnyAttribute (org.eclipse.persistence.internal.oxm.schema.model.AnyAttribute)4 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)4 List (java.util.List)3 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)3 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)3 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2