use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace in project hale by halestudio.
the class AbstractPropertyTransformationHandler method handleAsXmlAttribute.
/**
* This method is invoked when the target property is an XML attribute (
* {@link XmlAttributeFlag} constraint is set).
*
* <p>
* The property transformation is translated to:
*
* <pre>
* <code><ClientProperty>
* <name>[target property name]</name>
* <value>[CQL expression]</value>
* </ClientProperty></code>
* </pre>
*
* and added to the attribute mapping generated for the XML element owning
* the attribute.
* </p>
*
* @param featureType the target feature type
* @param mappingName the target feature type's mapping name (may be
* <code>null</code>)
* @param context the app-schema mapping context
*/
protected void handleAsXmlAttribute(TypeDefinition featureType, String mappingName, AppSchemaMappingContext context) {
PropertyEntityDefinition targetPropertyEntityDef = targetProperty.getDefinition();
PropertyDefinition targetPropertyDef = targetPropertyEntityDef.getDefinition();
// fetch attribute mapping for parent property
EntityDefinition parentDef = AlignmentUtil.getParent(targetPropertyEntityDef);
if (parentDef != null) {
List<ChildContext> parentPropertyPath = parentDef.getPropertyPath();
PropertyDefinition parentPropertyDef = parentPropertyPath.get(parentPropertyPath.size() - 1).getChild().asProperty();
if (parentPropertyDef != null) {
attributeMapping = context.getOrCreateAttributeMapping(featureType, mappingName, parentPropertyPath);
// set targetAttribute if empty
if (attributeMapping.getTargetAttribute() == null || attributeMapping.getTargetAttribute().isEmpty()) {
attributeMapping.setTargetAttribute(mapping.buildAttributeXPath(featureType, parentPropertyPath));
}
Namespace targetPropNS = context.getOrCreateNamespace(targetPropertyDef.getName().getNamespaceURI(), targetPropertyDef.getName().getPrefix());
String unqualifiedName = targetPropertyDef.getName().getLocalPart();
boolean isQualified = targetPropNS != null && !Strings.isNullOrEmpty(targetPropNS.getPrefix());
// encode attribute as <ClientProperty>
ClientProperty clientProperty = new ClientProperty();
@SuppressWarnings("null") String clientPropName = (isQualified) ? targetPropNS.getPrefix() + ":" + unqualifiedName : unqualifiedName;
clientProperty.setName(clientPropName);
clientProperty.setValue(getSourceExpressionAsCQL());
setEncodeIfEmpty(clientProperty);
// don't add client property if it already exists
if (!hasClientProperty(clientProperty.getName())) {
attributeMapping.getClientProperty().add(clientProperty);
// when nilReason is null and viceversa)
if (isNilReason(targetPropertyDef) && isNillable(parentPropertyDef) && attributeMapping.getSourceExpression() == null) {
addOrReplaceXsiNilAttribute(clientProperty.getValue(), true, context);
}
}
}
}
}
use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace in project hale by halestudio.
the class AppSchemaMappingWrapper method getOrCreateNamespace.
/**
* Return a namespace object with the provided URI and prefix.
*
* <p>
* If a namespace object for the same URI already exists, it is returned.
* Otherwise, a new one is created.
* </p>
* <p>
* If the prefix is empty, a non-empty prefix is automatically generated.
* If, in a subsequent call to this method, a non-empty prefix is provided,
* the user-provided prefix will replace the generated one.
* </p>
*
* @param namespaceURI the namespace URI
* @param prefix the namespace prefix
* @return the created namespace object
*/
Namespace getOrCreateNamespace(String namespaceURI, String prefix) {
if (namespaceURI != null && !namespaceURI.isEmpty()) {
if (!namespaceUriMap.containsKey(namespaceURI)) {
String basePrefix, uniquePrefix;
if (prefix == null || prefix.trim().isEmpty()) {
basePrefix = defaultPrefix;
uniquePrefix = basePrefix + prefixCounter;
prefixCounter++;
} else {
basePrefix = prefix;
uniquePrefix = basePrefix;
}
// make sure prefix is unique
while (namespacePrefixMap.containsKey(uniquePrefix)) {
uniquePrefix = basePrefix + prefixCounter;
prefixCounter++;
}
Namespace ns = new Namespace();
ns.setPrefix(uniquePrefix);
ns.setUri(namespaceURI);
namespaceUriMap.put(namespaceURI, ns);
namespacePrefixMap.put(uniquePrefix, ns);
appSchemaMapping.getNamespaces().getNamespace().add(ns);
return ns;
} else {
// update prefix if provided prefix is not empty and currently
// assigned prefix was made up
Namespace ns = namespaceUriMap.get(namespaceURI);
if (prefix != null && !prefix.isEmpty() && ns.getPrefix().startsWith(defaultPrefix)) {
// // check prefix is unique
// if (!namespacePrefixMap.containsKey(prefix)) {
// remove old prefix-NS mapping from namespacePrefixMap
namespacePrefixMap.remove(ns.getPrefix());
// add new prefix-NS mapping to namespacePrefixMap
ns.setPrefix(prefix);
namespacePrefixMap.put(prefix, ns);
// }
}
return ns;
}
} else {
return null;
}
}
use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace in project hale by halestudio.
the class AppSchemaMappingWrapper method cloneNamespace.
static Namespace cloneNamespace(Namespace ns) {
if (ns == null) {
return null;
}
Namespace clone = new Namespace();
clone.setPrefix(ns.getPrefix());
clone.setUri(ns.getUri());
return clone;
}
use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace in project hale by halestudio.
the class AppSchemaMappingWrapper method cloneMapping.
static AppSchemaDataAccessType cloneMapping(AppSchemaDataAccessType mapping) {
AppSchemaDataAccessType clone = new AppSchemaDataAccessType();
initMapping(clone);
clone.setCatalog(mapping.getCatalog());
clone.getIncludedTypes().getInclude().addAll(mapping.getIncludedTypes().getInclude());
for (Namespace ns : mapping.getNamespaces().getNamespace()) {
clone.getNamespaces().getNamespace().add(cloneNamespace(ns));
}
for (DataStore ds : mapping.getSourceDataStores().getDataStore()) {
clone.getSourceDataStores().getDataStore().add(cloneDataStore(ds));
}
clone.getTargetTypes().getFeatureType().getSchemaUri().addAll(mapping.getTargetTypes().getFeatureType().getSchemaUri());
for (FeatureTypeMapping ftMapping : mapping.getTypeMappings().getFeatureTypeMapping()) {
clone.getTypeMappings().getFeatureTypeMapping().add(cloneFeatureTypeMapping(ftMapping));
}
return clone;
}
use of eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.NamespacesPropertyType.Namespace in project hale by halestudio.
the class AppSchemaMappingWrapper method buildAttributeXPath.
/**
* Build an XPath expression to be used as <targetAttribute> for the
* provided target property definition.
*
* <p>
* The algorithm to build the path is as follows:
* <ol>
* <li>the property path is traversed backwards, from end to beginning</li>
* <li>on each step, a new path segment is added at the top of the list, but
* only if the child definition describes a property and not a group</li>
* <li>on each step, if a non-null context name is defined on the child
* context, <code>[<context name>]</code> string is appended to the
* path segment</li>
* <li>the traversal stops when the parent type of the last visited property
* equals to the provided owning type</li>
* </ol>
*
* @param owningType the type owning the target property
* @param propertyPath the target property path
* @return the XPath expression pointing to the target property
*/
public String buildAttributeXPath(TypeDefinition owningType, List<ChildContext> propertyPath) {
List<String> pathSegments = new ArrayList<String>();
for (int i = propertyPath.size() - 1; i >= 0; i--) {
ChildContext childContext = propertyPath.get(i);
// TODO: how to handle conditions?
Integer contextId = childContext.getContextName();
ChildDefinition<?> child = childContext.getChild();
// the xpath expression
if (child.asProperty() != null) {
String namespaceURI = child.getName().getNamespaceURI();
String prefix = child.getName().getPrefix();
String name = child.getName().getLocalPart();
Namespace ns = getOrCreateNamespace(namespaceURI, prefix);
String path = ns.getPrefix() + ":" + name;
if (contextId != null) {
// XPath indices start from 1, whereas contextId starts from
// 0 --> add 1
path = String.format("%s[%d]", path, contextId + 1);
}
// insert path segment at the first position
pathSegments.add(0, path);
}
if (child.getParentType() != null && child.getParentType().getName().equals(owningType.getName())) {
// I reached the owning type: stop walking the path
break;
}
}
String xPath = Joiner.on("/").join(pathSegments);
return xPath;
}
Aggregations