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;
}
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);
}
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);
}
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;
}
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);
}
}
Aggregations