use of org.eclipse.persistence.internal.oxm.XPathFragment in project eclipselink by eclipse-ee4j.
the class JsonSchemaGenerator method generateProperty.
private Property generateProperty(Mapping next, XMLDescriptor descriptor, Map<String, Property> properties) {
Property prop = null;
if (next.isCollectionMapping()) {
if (next instanceof CollectionReferenceMapping) {
CollectionReferenceMapping mapping = (CollectionReferenceMapping) next;
Set<XMLField> sourceFields = mapping.getSourceToTargetKeyFieldAssociations().keySet();
XMLDescriptor reference = (XMLDescriptor) mapping.getReferenceDescriptor();
for (XMLField nextField : sourceFields) {
XPathFragment frag = nextField.getXPathFragment();
String propertyName = getNameForFragment(frag);
XMLField targetField = (XMLField) mapping.getSourceToTargetKeyFieldAssociations().get(nextField);
Class<?> type = CoreClassConstants.STRING;
if (reference != null) {
type = getTypeForTargetField(targetField, reference);
}
prop = properties.get(propertyName);
if (prop == null) {
prop = new Property();
prop.setName(propertyName);
}
Property nestedProperty = getNestedPropertyForFragment(frag, prop);
nestedProperty.setType(JsonType.ARRAY);
nestedProperty.setItem(new Property());
nestedProperty.getItem().setType(getJsonTypeForJavaType(type));
if (!properties.containsKey(prop.getName())) {
properties.put(prop.getName(), prop);
}
}
return prop;
} else if (next.isAbstractCompositeCollectionMapping()) {
CompositeCollectionMapping mapping = (CompositeCollectionMapping) next;
XMLField field = (XMLField) mapping.getField();
XPathFragment frag = field.getXPathFragment();
String propName = getNameForFragment(frag);
// for paths, there may already be an existing property
prop = properties.get(propName);
if (prop == null) {
prop = new Property();
prop.setName(propName);
}
Property nestedProperty = getNestedPropertyForFragment(frag, prop);
nestedProperty.setType(JsonType.ARRAY);
nestedProperty.setItem(new Property());
XMLDescriptor referenceDescriptor = (XMLDescriptor) mapping.getReferenceDescriptor();
if (referenceDescriptor != null && referenceDescriptor.hasInheritance()) {
// handle inheritence
// schema.setAnyOf(new Property[descriptor.getInheritancePolicy().getAllChildDescriptors().size()]);
List<ClassDescriptor> descriptors = getAllDescriptorsForInheritance(referenceDescriptor);
Property[] props = new Property[descriptors.size()];
for (int i = 0; i < props.length; i++) {
XMLDescriptor nextDescriptor = null;
nextDescriptor = (XMLDescriptor) descriptors.get(i);
Property ref = new Property();
ref.setRef(getReferenceForDescriptor(nextDescriptor, true));
props[i] = ref;
}
nestedProperty.getItem().setAnyOf(props);
} else {
nestedProperty.getItem().setRef(getReferenceForDescriptor(referenceDescriptor, false));
// populateProperties(nestedProperty.getItem().getProperties(), (XMLDescriptor)mapping.getReferenceDescriptor());
}
} else if (next.isAbstractCompositeDirectCollectionMapping()) {
DirectCollectionMapping mapping = (DirectCollectionMapping) next;
XMLField field = (XMLField) mapping.getField();
XPathFragment frag = field.getXPathFragment();
List<String> enumeration = null;
if (mapping.getValueConverter() instanceof JAXBEnumTypeConverter) {
enumeration = getEnumeration((DatabaseMapping) next);
}
String propertyName = getNameForFragment(frag);
if (frag.nameIsText()) {
propertyName = (String) this.contextProperties.get(MarshallerProperties.JSON_VALUE_WRAPPER);
}
if (frag.isAttribute() && this.attributePrefix != null) {
propertyName = attributePrefix + propertyName;
}
prop = properties.get(propertyName);
if (prop == null) {
prop = new Property();
prop.setName(propertyName);
}
Property nestedProperty = getNestedPropertyForFragment(frag, prop);
nestedProperty.setType(JsonType.ARRAY);
nestedProperty.setItem(new Property());
if (enumeration != null) {
nestedProperty.getItem().setEnumeration(enumeration);
}
Class<?> type = mapping.getAttributeElementClass();
if (type == null) {
type = CoreClassConstants.STRING;
}
nestedProperty.getItem().setType(getJsonTypeForJavaType(type));
return prop;
} else if (next instanceof BinaryDataCollectionMapping) {
BinaryDataCollectionMapping mapping = (BinaryDataCollectionMapping) next;
XMLField field = (XMLField) mapping.getField();
XPathFragment frag = field.getXPathFragment();
String propertyName = getNameForFragment(frag);
if (frag.isSelfFragment()) {
propertyName = Constants.VALUE_WRAPPER;
if (this.contextProperties != null) {
String valueWrapper = (String) this.contextProperties.get(JAXBContextProperties.JSON_VALUE_WRAPPER);
if (valueWrapper != null) {
propertyName = valueWrapper;
}
}
}
if (frag.isAttribute() && this.attributePrefix != null) {
propertyName = attributePrefix + propertyName;
}
prop = properties.get(propertyName);
if (prop == null) {
prop = new Property();
prop.setName(propertyName);
}
Property nestedProperty = getNestedPropertyForFragment(frag, prop);
nestedProperty.setType(JsonType.ARRAY);
nestedProperty.setItem(new Property());
if (mapping.shouldInlineBinaryData()) {
nestedProperty.getItem().setType(JsonType.STRING);
} else {
nestedProperty.getItem().setAnyOf(getXopIncludeProperties());
}
return prop;
}
} else {
if (next.isAbstractDirectMapping()) {
// handle direct mapping
DirectMapping directMapping = (DirectMapping) next;
XMLField field = (XMLField) directMapping.getField();
XPathFragment frag = field.getXPathFragment();
List<String> enumeration = null;
if (directMapping.getConverter() instanceof JAXBEnumTypeConverter) {
enumeration = getEnumeration((DatabaseMapping) directMapping);
}
String propertyName = getNameForFragment(frag);
if (frag.nameIsText()) {
propertyName = Constants.VALUE_WRAPPER;
if (this.contextProperties != null) {
String valueWrapper = (String) this.contextProperties.get(JAXBContextProperties.JSON_VALUE_WRAPPER);
if (valueWrapper != null) {
propertyName = valueWrapper;
}
}
}
if (frag.isAttribute() && this.attributePrefix != null) {
propertyName = attributePrefix + propertyName;
}
prop = properties.get(propertyName);
if (prop == null) {
prop = new Property();
prop.setName(propertyName);
}
Property nestedProperty = getNestedPropertyForFragment(frag, prop);
if (enumeration != null) {
nestedProperty.setEnumeration(enumeration);
}
if (directMapping instanceof BinaryDataMapping) {
BinaryDataMapping binaryMapping = (BinaryDataMapping) directMapping;
if (binaryMapping.shouldInlineBinaryData() || binaryMapping.isSwaRef()) {
nestedProperty.setType(JsonType.STRING);
} else {
if (this.xopIncludeProp == null) {
initXopIncludeProp();
}
nestedProperty.setAnyOf(this.xopIncludeProp);
}
} else {
nestedProperty.setType(getJsonTypeForJavaType(directMapping.getAttributeClassification()));
}
return prop;
} else if (next instanceof ObjectReferenceMapping) {
ObjectReferenceMapping mapping = (ObjectReferenceMapping) next;
Set<XMLField> sourceFields = mapping.getSourceToTargetKeyFieldAssociations().keySet();
XMLDescriptor reference = (XMLDescriptor) mapping.getReferenceDescriptor();
for (XMLField nextField : sourceFields) {
XPathFragment frag = nextField.getXPathFragment();
String propName = getNameForFragment(frag);
XMLField targetField = (XMLField) mapping.getSourceToTargetKeyFieldAssociations().get(nextField);
Class<?> type = CoreClassConstants.STRING;
if (reference != null) {
type = getTypeForTargetField(targetField, reference);
}
prop = properties.get(propName);
if (prop == null) {
prop = new Property();
prop.setName(propName);
}
Property nestedProperty = getNestedPropertyForFragment(frag, prop);
// nestedProperty.setType(JsonType.ARRAY);
// nestedProperty.setItem(new Property());
nestedProperty.setType(getJsonTypeForJavaType(type));
if (!properties.containsKey(prop.getName())) {
properties.put(prop.getName(), prop);
}
}
return prop;
} else if (next.isAbstractCompositeObjectMapping()) {
CompositeObjectMapping mapping = (CompositeObjectMapping) next;
XMLDescriptor nextDescriptor = (XMLDescriptor) mapping.getReferenceDescriptor();
XMLField field = (XMLField) mapping.getField();
XPathFragment firstFragment = field.getXPathFragment();
if (firstFragment.isSelfFragment() || firstFragment.nameIsText()) {
if (nextDescriptor != null) {
populateProperties(properties, nextDescriptor);
}
} else {
String propName = getNameForFragment(firstFragment);
prop = properties.get(propName);
if (prop == null) {
prop = new Property();
prop.setName(propName);
}
// prop.setType(JsonType.OBJECT);
prop.setName(propName);
Property nestedProperty = getNestedPropertyForFragment(firstFragment, prop);
XMLDescriptor referenceDescriptor = (XMLDescriptor) mapping.getReferenceDescriptor();
if (referenceDescriptor != null && referenceDescriptor.hasInheritance()) {
// handle inheritence
// schema.setAnyOf(new Property[descriptor.getInheritancePolicy().getAllChildDescriptors().size()]);
List<ClassDescriptor> descriptors = getAllDescriptorsForInheritance(referenceDescriptor);
Property[] props = new Property[descriptors.size()];
for (int i = 0; i < props.length; i++) {
XMLDescriptor nextDesc = (XMLDescriptor) descriptors.get(i);
Property ref = new Property();
ref.setRef(getReferenceForDescriptor(nextDesc, true));
props[i] = ref;
}
nestedProperty.setAnyOf(props);
} else {
nestedProperty.setRef(getReferenceForDescriptor(referenceDescriptor, false));
// populateProperties(nestedProperty.getItem().getProperties(), (XMLDescriptor)mapping.getReferenceDescriptor());
}
// populateProperties(nestedProperty.getProperties(), nextDescriptor);
}
} else if (next instanceof BinaryDataMapping) {
BinaryDataMapping binaryMapping = (BinaryDataMapping) next;
XMLField field = (XMLField) binaryMapping.getField();
XPathFragment frag = field.getXPathFragment();
String propertyName = getNameForFragment(frag);
if (frag.isSelfFragment()) {
propertyName = Constants.VALUE_WRAPPER;
if (this.contextProperties != null) {
String valueWrapper = (String) this.contextProperties.get(MarshallerProperties.JSON_VALUE_WRAPPER);
if (valueWrapper != null) {
propertyName = valueWrapper;
}
}
}
if (frag.isAttribute() && this.attributePrefix != null) {
propertyName = attributePrefix + propertyName;
}
prop = properties.get(propertyName);
if (prop == null) {
prop = new Property();
prop.setName(propertyName);
}
Property nestedProperty = getNestedPropertyForFragment(frag, prop);
if (binaryMapping.shouldInlineBinaryData() || binaryMapping.isSwaRef()) {
nestedProperty.setType(JsonType.STRING);
} else {
if (this.xopIncludeProp == null) {
initXopIncludeProp();
}
nestedProperty.setAnyOf(this.xopIncludeProp);
}
return prop;
}
}
return prop;
}
use of org.eclipse.persistence.internal.oxm.XPathFragment in project eclipselink by eclipse-ee4j.
the class Property method isPositional.
/**
* Indicates if this property is mapped by position, i.e. 'name="data[1]"',
* or is mapped by attribute value (predicate mapping), i.e.
* 'personal-info[@pi-type='last-name']/name[@name-type='surname']/text()'
*/
public boolean isPositional() {
if (getXmlPath() == null) {
return false;
}
Field<XMLConversionManager, NamespaceResolver> field = new XMLField(getXmlPath());
XPathFragment frag = field.getXPathFragment();
// loop until we have the last non-null, non-attribute, non-text fragment
while (true) {
if (frag.getNextFragment() != null && !frag.getNextFragment().isAttribute() && !frag.getNextFragment().nameIsText()) {
frag = frag.getNextFragment();
} else {
break;
}
}
return frag.containsIndex() || frag.getPredicate() != null;
}
use of org.eclipse.persistence.internal.oxm.XPathFragment in project eclipselink by eclipse-ee4j.
the class SchemaGenerator method buildSchemaComponentsForXPath.
/**
* This method will build element/complexType/typedefparticle components for a given xml-path,
* and return an XmlPathResult instance containg the sequence that the target should be added
* to, as well as the current schema - which could be different than the working schema used
* before calling this method in the case of a prefixed path element from a different namespace.
* Regarding the path 'target', if the xml-path was "contact-info/address/street", "street"
* would be the target. In this case the sequence containing the "address" element would be
* set in the XmlPathResult to be returned.
*
* The exception case is an 'any', where we want to process the last path element before
* returning - this is necessary due to the fact that an Any will be added to the sequence
* in place of the last path element by the calling method.
*/
private AddToSchemaResult buildSchemaComponentsForXPath(XPathFragment frag, AddToSchemaResult xpr, boolean isChoice, Property next) {
boolean isAny = next.isAny() || next.isAnyAttribute();
TypeDefParticle currentParticle = xpr.particle;
Schema workingSchema = xpr.schema;
// each nested choice on a collection will be unbounded
boolean isUnbounded = false;
if (currentParticle != null) {
isUnbounded = (currentParticle.getMaxOccurs() != null && currentParticle.getMaxOccurs() == Occurs.UNBOUNDED);
}
// don't process the last frag; that will be handled by the calling method if necessary
// note that we may need to process the last frag if it has a namespace or is an 'any'
boolean lastFrag = (frag.getNextFragment() == null || frag.getNextFragment().nameIsText());
// if the element is already in the sequence we don't want the calling method to add a second one
if (lastFrag && (elementExistsInParticle(frag.getLocalName(), frag.getShortName(), currentParticle) != null)) {
xpr.particle = null;
return xpr;
}
// if the current element exists, use it; otherwise create a new one
Element currentElement = elementExistsInParticle(frag.getLocalName(), frag.getShortName(), currentParticle);
boolean currentElementExists = (currentElement != null);
if (!currentElementExists) {
currentElement = new Element();
// don't set the element name yet, as it may end up being a ref
ComplexType cType = new ComplexType();
TypeDefParticle particle = null;
if (isChoice) {
particle = new Choice();
if (isUnbounded) {
particle.setMaxOccurs(Occurs.UNBOUNDED);
}
} else {
XPathFragment nextFragment = frag.getNextFragment();
if (frag.containsIndex() || frag.getPredicate() != null || (!next.isXmlList() && null != nextFragment && nextFragment.isAttribute() && helper.isCollectionType(next.getType()))) {
currentElement.setMaxOccurs(Occurs.UNBOUNDED);
}
particle = new Sequence();
}
cType.setTypeDefParticle(particle);
currentElement.setComplexType(cType);
} else {
// if the current element already exists, we may need to change it's type
XPathFragment nextFrag = frag.getNextFragment();
if (nextFrag != null && nextFrag.isAttribute()) {
if (currentElement.getType() != null && currentElement.getComplexType() == null) {
// there's already a text mapping to this element, so
// attributes can be added by making it complex with
// simple content.
SimpleType type = currentElement.getSimpleType();
if (type != null) {
ComplexType cType = new ComplexType();
cType.setSimpleContent(new SimpleContent());
Extension extension = new Extension();
extension.setBaseType(type.getRestriction().getBaseType());
cType.getSimpleContent().setExtension(extension);
currentElement.setSimpleType(null);
currentElement.setComplexType(cType);
} else {
String eType = currentElement.getType();
ComplexType cType = new ComplexType();
SimpleContent sContent = new SimpleContent();
Extension extension = new Extension();
extension.setBaseType(eType);
sContent.setExtension(extension);
cType.setSimpleContent(sContent);
currentElement.setType(null);
currentElement.setComplexType(cType);
}
}
}
}
// may need to create a ref, depending on the namespace
Element globalElement = null;
String fragUri = frag.getNamespaceURI();
if (fragUri != null) {
Schema fragSchema = getSchemaForNamespace(fragUri);
String targetNS = workingSchema.getTargetNamespace();
// handle Attribute case
if (frag.isAttribute()) {
if (fragSchema == null || (fragSchema.isAttributeFormDefault() && !fragUri.equals(targetNS)) || (!fragSchema.isAttributeFormDefault() && fragUri.length() > 0)) {
// if fragSchema is null, just generate the ref
if (fragSchema != null) {
Attribute globalAttribute = null;
globalAttribute = fragSchema.getTopLevelAttributes().get(frag.getLocalName());
if (globalAttribute == null) {
globalAttribute = createGlobalAttribute(frag, workingSchema, fragSchema, next);
}
} else {
// may need to add an import
addImportIfRequired(workingSchema, null, fragUri);
}
// add the attribute ref to the current element
String attributeRefName;
if (fragUri.equals(targetNS)) {
String prefix = fragSchema.getNamespaceResolver().resolveNamespaceURI(fragUri);
attributeRefName = prefix + COLON + frag.getLocalName();
} else {
attributeRefName = frag.getShortName();
}
TypeDefParticleOwner type;
if (currentParticle != null) {
type = currentParticle.getOwner();
} else {
type = xpr.simpleContentType;
}
if (type instanceof ComplexType) {
createRefAttribute(attributeRefName, (ComplexType) type);
}
// set the frag's schema as it may be different than the current schema
xpr.schema = fragSchema;
// ref case - indicate to the calling method that there's nothing to do
xpr.particle = null;
}
// since we are dealing with an attribute, we are on the last fragment; return
return xpr;
}
// here we are dealing with an Element
if ((fragSchema.isElementFormDefault() && !fragUri.equals(targetNS)) || (!fragSchema.isElementFormDefault() && fragUri.length() > 0)) {
// must generate a global element and create a reference to it
// if the global element exists, use it; otherwise create a new one
globalElement = fragSchema.getTopLevelElements().get(frag.getLocalName());
if (globalElement == null) {
globalElement = createGlobalElement(frag, workingSchema, fragSchema, isChoice, isUnbounded, next, (lastFrag && !isAny));
}
// if the current element doesn't exist set a ref and add it to the sequence
if (!currentElementExists) {
// use prefix from the working schema's resolver - add prefix/uri pair if necessary
String fragPrefix = workingSchema.getNamespaceResolver().resolveNamespaceURI(fragUri);
if (fragPrefix == null) {
fragPrefix = workingSchema.getNamespaceResolver().generatePrefix(frag.getPrefix());
workingSchema.getNamespaceResolver().put(fragPrefix, fragUri);
}
currentElement = createRefElement(fragPrefix + COLON + frag.getLocalName(), currentParticle);
if (frag.containsIndex() || frag.getPredicate() != null || helper.isCollectionType(next.getType())) {
currentElement.setMaxOccurs(Occurs.UNBOUNDED);
}
currentElementExists = true;
}
// set the frag's schema as it may be different than the current schema
xpr.schema = fragSchema;
// at this point, if we are dealing with the last fragment we will need to return
if (lastFrag) {
// add a second one...unless we're dealing with an 'any'
if (isAny) {
// set the particle that the 'any' will be added to by the calling method
xpr.particle = globalElement.getComplexType().getTypeDefParticle();
return xpr;
}
// ref case - indicate to the calling method that there's nothing to do
xpr.particle = null;
return xpr;
}
// make the global element current
currentElement = globalElement;
}
}
if (!lastFrag || (lastFrag && isAny)) {
// 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());
Integer minOccurs = next.getMinOccurs();
if (minOccurs != null)
currentElement.setMinOccurs(String.valueOf(minOccurs));
else
currentElement.setMinOccurs(Occurs.ZERO);
currentParticle.addElement(currentElement);
}
// set the correct particle to use/return
if (currentElement.getComplexType() != null) {
if (currentElement.getComplexType().getTypeDefParticle() == null) {
// complexType with simple-content
xpr.simpleContentType = currentElement.getComplexType();
xpr.particle = null;
} else {
xpr.particle = currentElement.getComplexType().getTypeDefParticle();
}
} else {
// If there's no complex type, we're building the path through an element with
// a simple type. In order to build the path through this
// element, switch to a complex type with simple content.
SimpleType type = currentElement.getSimpleType();
if (type != null) {
ComplexType cType = new ComplexType();
cType.setSimpleContent(new SimpleContent());
Extension extension = new Extension();
extension.setBaseType(type.getRestriction().getBaseType());
cType.getSimpleContent().setExtension(extension);
currentElement.setSimpleType(null);
currentElement.setComplexType(cType);
xpr.particle = null;
xpr.simpleContentType = cType;
} else {
String eType = currentElement.getType();
ComplexType cType = new ComplexType();
SimpleContent sContent = new SimpleContent();
Extension extension = new Extension();
extension.setBaseType(eType);
sContent.setExtension(extension);
cType.setSimpleContent(sContent);
currentElement.setType(null);
currentElement.setComplexType(cType);
xpr.particle = null;
xpr.simpleContentType = cType;
}
}
}
// if we're on the last fragment, we're done
if (lastFrag) {
return xpr;
}
// call back into this method to process the next path element
return buildSchemaComponentsForXPath(frag.getNextFragment(), xpr, isChoice, next);
}
use of org.eclipse.persistence.internal.oxm.XPathFragment in project eclipselink by eclipse-ee4j.
the class SDOSequence method convertToSetting.
private Setting convertToSetting(DatabaseMapping mapping, Object value) {
XMLDescriptor xmlDescriptor = (XMLDescriptor) mapping.getDescriptor();
NamespaceResolver nsResolver = xmlDescriptor.getNamespaceResolver();
Setting rootSetting = new Setting();
XMLField xmlField = (XMLField) mapping.getField();
if (xmlField == null) {
if (mapping instanceof XMLObjectReferenceMapping) {
xmlField = (XMLField) ((XMLObjectReferenceMapping) mapping).getFields().get(0);
} else if (mapping instanceof XMLCollectionReferenceMapping) {
xmlField = (XMLField) ((XMLCollectionReferenceMapping) mapping).getFields().get(0);
}
}
Setting setting = rootSetting;
if (xmlField != null) {
XPathFragment xPathFragment = xmlField.getXPathFragment();
rootSetting = convertToSetting(xPathFragment, nsResolver);
setting = rootSetting;
while (xPathFragment.getNextFragment() != null) {
xPathFragment = xPathFragment.getNextFragment();
Setting childSetting = convertToSetting(xPathFragment, nsResolver);
setting.addChild(childSetting);
setting = childSetting;
}
}
setting.setObject(dataObject);
setting.setMapping(mapping);
setting.setValue(value, false);
return rootSetting;
}
use of org.eclipse.persistence.internal.oxm.XPathFragment in project eclipselink by eclipse-ee4j.
the class JAXBHelperContext method getObjectDescriptor.
/**
* Get the XML descriptor for the entity class corresponding to the SDO type.
*/
XMLDescriptor getObjectDescriptor(SDOType sdoType) {
QName xsdQName = sdoType.getXsdType();
if (null == xsdQName) {
xsdQName = sdoType.getQName();
}
XPathFragment xPathFragment = new XPathFragment(xsdQName.getLocalPart());
xPathFragment.setNamespaceURI(xsdQName.getNamespaceURI());
XMLDescriptor xmlDescriptor = jaxbContext.getXMLContext().getDescriptorByGlobalType(xPathFragment);
if (null == xmlDescriptor) {
xmlDescriptor = jaxbContext.getXMLContext().getDescriptor(xsdQName);
if (null == xmlDescriptor) {
throw SDOException.sdoJaxbNoDescriptorForType(sdoType.getQName(), xsdQName);
}
}
return xmlDescriptor;
}
Aggregations