use of eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition in project hale by halestudio.
the class XmlIndex method getOrCreateType.
/**
* Get the type definition with the given name. If the type doesn't exist a
* new type definition will be created.
*
* @param name the type name
* @return the type definition
*/
public XmlTypeDefinition getOrCreateType(QName name) {
XmlTypeDefinition type = (XmlTypeDefinition) super.getType(name);
if (type == null) {
type = new XmlTypeDefinition(name);
XmlTypeUtil.configureType(type);
if (name.equals(XmlTypeUtil.NAME_ANY_TYPE)) {
type.setConstraint(AbstractFlag.ENABLED);
type.setConstraint(MappableFlag.DISABLED);
} else {
// set anyType as default super type
type.setSuperType(getOrCreateType(XmlTypeUtil.NAME_ANY_TYPE));
}
addType(type);
}
return type;
}
use of eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition in project hale by halestudio.
the class XmlSchemaReader method loadSchema.
/**
* Load the feature types defined by the given schema
*
* @param schemaLocation the schema location
* @param xmlSchema the schema
* @param imports the imports/includes that were already loaded or where
* loading has been started
* @param progress the progress indicator
* @param mainSchema states if this is a main schema and therefore elements
* declared here should be flagged mappable
*/
protected void loadSchema(String schemaLocation, XmlSchema xmlSchema, Set<String> imports, ProgressIndicator progress, boolean mainSchema) {
String namespace = xmlSchema.getTargetNamespace();
if (namespace == null) {
namespace = XMLConstants.NULL_NS_URI;
}
// add namespace prefixes
NamespacePrefixList namespaces = xmlSchema.getNamespaceContext();
addPrefixes(namespaces, namespace, mainSchema);
// the schema items
XmlSchemaObjectCollection items = xmlSchema.getItems();
// go through all schema items
for (int i = 0; i < items.getCount(); i++) {
XmlSchemaObject item = items.getItem(i);
if (item instanceof XmlSchemaElement) {
// global element declaration
XmlSchemaElement element = (XmlSchemaElement) item;
// determine type
XmlTypeDefinition elementType = null;
if (element.getSchemaTypeName() != null) {
// reference to type
elementType = index.getOrCreateType(element.getSchemaTypeName());
} else if (element.getSchemaType() != null) {
// element has internal type definition, generate anonymous
// type name
QName typeName = new QName(element.getQName().getNamespaceURI(), // $NON-NLS-1$
element.getQName().getLocalPart() + "_AnonymousType");
// create type
elementType = createType(element.getSchemaType(), typeName, schemaLocation, namespace, mainSchema);
} else if (element.getQName() != null) {
// element with no type
elementType = index.getOrCreateType(XmlTypeUtil.NAME_ANY_TYPE);
}
if (elementType != null) {
// the element name
// XXX use element QName instead?
QName elementName = new QName(namespace, element.getName());
// the substitution group
QName subGroup = element.getSubstitutionGroup();
// TODO do we also need an index for substitutions?
// create schema element
XmlElement schemaElement = new XmlElement(elementName, elementType, subGroup);
// set metadata
setMetadata(schemaElement, element, schemaLocation);
// extend XmlElements constraint
XmlElements xmlElements = elementType.getConstraint(XmlElements.class);
xmlElements.addElement(schemaElement);
// set custom display name
elementType.setConstraint(new ElementName(xmlElements));
// set Mappable constraint (e.g. Mappable)
// for types with an associated element it can be determined
// on the spot if it is mappable
configureMappingRelevant(elementType, mainSchema);
// XXX needed? may result in conflicts when defining
// mappable types manually XXX the element is also marked
// with the Mappable constraint, to help with cases where
// multiple elements are defined for one
// schemaElement.setConstraint(MappableFlag.get(mainSchema));
// store element in index
index.getElements().put(elementName, schemaElement);
} else {
reporter.error(new IOMessageImpl(MessageFormat.format("No type for element {0} found.", element.getName()), null, element.getLineNumber(), element.getLinePosition()));
}
} else if (item instanceof XmlSchemaType) {
// complex or simple type
createType((XmlSchemaType) item, null, schemaLocation, namespace, mainSchema);
} else if (item instanceof XmlSchemaAttribute) {
// schema attribute that might be referenced somewhere
XmlSchemaAttribute att = (XmlSchemaAttribute) item;
if (att.getQName() != null) {
XmlTypeDefinition type = getAttributeType(att, null, schemaLocation);
if (type == null) {
// XXX if this occurs we might need a attribute
// referencing attribute
reporter.error(new IOMessageImpl("Could not determine attribute type", null, att.getLineNumber(), att.getLinePosition()));
} else {
XmlAttribute attribute = new XmlAttribute(att.getQName(), type);
index.getAttributes().put(attribute.getName(), attribute);
}
} else {
reporter.warn(new IOMessageImpl(MessageFormat.format("Attribute could not be processed: {0}", att.getName()), null, att.getLineNumber(), att.getLinePosition()));
}
} else if (item instanceof XmlSchemaAttributeGroup) {
// schema attribute group that might be referenced somewhere
XmlSchemaAttributeGroup attributeGroup = (XmlSchemaAttributeGroup) item;
if (attributeGroup.getName() != null) {
String groupIdent = attributeGroup.getName().getNamespaceURI() + "/" + attributeGroup.getName().getLocalPart();
XmlAttributeGroup attGroup = new XmlAttributeGroup(groupIdent, true);
createAttributes(attributeGroup, attGroup, "", schemaLocation, namespace);
index.getAttributeGroups().put(attributeGroup.getName(), attGroup);
} else {
reporter.warn(new IOMessageImpl("Attribute group could not be processed", null, attributeGroup.getLineNumber(), attributeGroup.getLinePosition()));
}
} else if (item instanceof XmlSchemaGroup) {
// group that might be referenced somewhere
XmlSchemaGroup schemaGroup = (XmlSchemaGroup) item;
if (schemaGroup.getName() != null) {
String groupIdent = schemaGroup.getName().getNamespaceURI() + "/" + schemaGroup.getName().getLocalPart();
XmlGroup group = new XmlGroup(groupIdent, true);
createPropertiesFromParticle(group, schemaGroup.getParticle(), schemaLocation, namespace, false);
index.getGroups().put(schemaGroup.getName(), group);
} else {
reporter.warn(new IOMessageImpl("Group could not be processed", null, schemaGroup.getLineNumber(), schemaGroup.getLinePosition()));
}
} else if (item instanceof XmlSchemaImport || item instanceof XmlSchemaInclude) {
// ignore, is treated separately
} else if (item instanceof XmlSchemaNotation) {
// notations are ignored
} else {
reporter.error(new IOMessageImpl("Unrecognized global definition: " + item.getClass().getSimpleName(), null, item.getLineNumber(), item.getLinePosition()));
}
}
// Set of include locations
Set<String> includes = new HashSet<String>();
// handle imports
XmlSchemaObjectCollection externalItems = xmlSchema.getIncludes();
if (externalItems.getCount() > 0) {
// $NON-NLS-1$
_log.info("Loading includes and imports for schema at " + schemaLocation);
}
for (int i = 0; i < externalItems.getCount(); i++) {
try {
XmlSchemaExternal imp = (XmlSchemaExternal) externalItems.getItem(i);
XmlSchema importedSchema = imp.getSchema();
String location = importedSchema.getSourceURI();
if (!(imports.contains(location))) {
// only add schemas that
// were not already
// added
// place a marker in the map to
imports.add(location);
// prevent loading the location in
// the call to loadSchema
loadSchema(location, importedSchema, imports, progress, mainSchema && imp instanceof XmlSchemaInclude);
// is part of main schema if it's a main schema include
}
if (imp instanceof XmlSchemaInclude) {
includes.add(location);
}
} catch (Throwable e) {
reporter.error(new IOMessageImpl("Error adding imported schema from " + schemaLocation, // $NON-NLS-1$
e));
}
}
// $NON-NLS-1$
_log.info("Creating types for schema at " + schemaLocation);
progress.setCurrentTask(// $NON-NLS-1$
MessageFormat.format(Messages.getString("ApacheSchemaProvider.33"), namespace));
}
use of eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition in project hale by halestudio.
the class XmlSchemaReader method createAttribute.
private void createAttribute(XmlSchemaAttribute attribute, DefinitionGroup declaringGroup, String schemaLocation, String schemaNamespace) {
// create attributes
QName typeName = attribute.getSchemaTypeName();
if (typeName != null) {
QName attributeName = determineAttributeName(attribute, schemaNamespace);
// resolve type by name
XmlTypeDefinition type = getTypeForAttribute(typeName, declaringGroup, attributeName);
// create property
DefaultPropertyDefinition property = new DefaultPropertyDefinition(attributeName, declaringGroup, type);
// set metadata and constraints
setMetadataAndConstraints(property, attribute, schemaLocation);
} else if (attribute.getSchemaType() != null) {
XmlSchemaSimpleType simpleType = attribute.getSchemaType();
// create an anonymous type
QName anonymousName = new QName(getTypeIdentifier(declaringGroup) + "/" + attribute.getName(), // $NON-NLS-2$
"AnonymousType");
AnonymousXmlType anonymousType = new AnonymousXmlType(anonymousName);
configureSimpleType(anonymousType, simpleType, schemaLocation);
// create property
DefaultPropertyDefinition property = new DefaultPropertyDefinition(determineAttributeName(attribute, schemaNamespace), declaringGroup, anonymousType);
// set metadata and constraints
setMetadataAndConstraints(property, attribute, schemaLocation);
} else if (attribute.getRefName() != null) {
// <attribute ref="REF_NAME" />
// reference to a named attribute
QName attName = attribute.getRefName();
XmlAttributeReferenceProperty property = new XmlAttributeReferenceProperty(attName, declaringGroup, this.index, attName);
// set metadata and constraints
setMetadataAndConstraints(property, attribute, schemaLocation);
} else {
/*
* Attribute with no type given has anySimpleType, see
* "http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#cAttribute_Declarations"
*/
// resolve type by name
XmlTypeDefinition type = index.getOrCreateType(new QName(XMLConstants.W3C_XML_SCHEMA_NS_URI, "anySimpleType"));
// create property
DefaultPropertyDefinition property = new DefaultPropertyDefinition(determineAttributeName(attribute, schemaNamespace), declaringGroup, type);
// set metadata and constraints
setMetadataAndConstraints(property, attribute, schemaLocation);
}
}
use of eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition in project hale by halestudio.
the class XmlSchemaReader method createPropertyFromElement.
/**
* Create a property from an element
*
* @param element the schema element
* @param declaringGroup the definition of the declaring group
* @param schemaLocation the schema location
* @param schemaNamespace the schema namespace
*/
private void createPropertyFromElement(XmlSchemaElement element, DefinitionGroup declaringGroup, String schemaLocation, String schemaNamespace) {
if (element.getSchemaTypeName() != null) {
// element referencing a type
// <element name="ELEMENT_NAME" type="SCHEMA_TYPE_NAME" />
QName elementName = element.getQName();
SubstitutionGroupProperty substitutionGroup = new SubstitutionGroupProperty(new QName(elementName.getNamespaceURI() + "/" + elementName.getLocalPart(), // TODO
"choice"), // naming?
declaringGroup);
DefaultPropertyDefinition property = new DefaultPropertyDefinition(elementName, substitutionGroup, index.getOrCreateType(element.getSchemaTypeName()));
// set metadata and constraints
setMetadataAndConstraints(property, element, schemaLocation);
substitutionGroup.setProperty(property);
} else if (element.getRefName() != null) {
// references another element
// <element ref="REF_NAME" />
QName elementName = element.getRefName();
SubstitutionGroupProperty substitutionGroup = new SubstitutionGroupProperty(new QName(elementName.getNamespaceURI() + "/" + elementName.getLocalPart(), // TODO
"choice"), // naming?
declaringGroup);
XmlElementReferenceProperty property = new XmlElementReferenceProperty(elementName, substitutionGroup, index, elementName);
// set metadata and constraints FIXME can the constraints be set at
// this point? or must the property determine them from the
// SchemaElement?
setMetadataAndConstraints(property, element, schemaLocation);
substitutionGroup.setProperty(property);
} else if (element.getSchemaType() != null) {
// definition
if (element.getSchemaType() instanceof XmlSchemaComplexType) {
// <element ...>
// <complexType>
XmlSchemaComplexType complexType = (XmlSchemaComplexType) element.getSchemaType();
XmlSchemaContentModel model = complexType.getContentModel();
if (model != null) {
XmlSchemaContent content = model.getContent();
QName superTypeName = null;
if (content instanceof XmlSchemaComplexContentExtension || content instanceof XmlSchemaComplexContentRestriction) {
// <complexContent>
// <extension base="..."> / <restriction ...>
String nameExt;
if (content instanceof XmlSchemaComplexContentExtension) {
superTypeName = ((XmlSchemaComplexContentExtension) content).getBaseTypeName();
// $NON-NLS-1$
nameExt = "Extension";
} else {
superTypeName = ((XmlSchemaComplexContentRestriction) content).getBaseTypeName();
// $NON-NLS-1$
nameExt = "Restriction";
}
if (superTypeName != null) {
// try to get the type definition of the super type
XmlTypeDefinition superType = index.getOrCreateType(superTypeName);
// create an anonymous type that extends the super
// type
QName anonymousName = new QName(getTypeIdentifier(declaringGroup) + "/" + element.getName(), // $NON-NLS-1$
superTypeName.getLocalPart() + nameExt);
AnonymousXmlType anonymousType = new AnonymousXmlType(anonymousName);
anonymousType.setSuperType(superType);
// set metadata and constraints
setMetadataAndConstraints(anonymousType, complexType, schemaLocation);
// add properties to the anonymous type
createProperties(anonymousType, complexType, schemaLocation, schemaNamespace);
// create a property with the anonymous type
DefaultPropertyDefinition property = new DefaultPropertyDefinition(element.getQName(), declaringGroup, anonymousType);
// set metadata and constraints
setMetadataAndConstraints(property, element, schemaLocation);
} else {
reporter.error(new IOMessageImpl("Could not determine super type for complex content", null, content.getLineNumber(), content.getLinePosition()));
}
// </extension> / </restriction>
// </complexContent>
} else if (content instanceof XmlSchemaSimpleContentExtension || content instanceof XmlSchemaSimpleContentRestriction) {
// <simpleContent>
// <extension base="..."> / <restriction ...>
String nameExt;
if (content instanceof XmlSchemaSimpleContentExtension) {
superTypeName = ((XmlSchemaSimpleContentExtension) content).getBaseTypeName();
// $NON-NLS-1$
nameExt = "Extension";
} else {
superTypeName = ((XmlSchemaSimpleContentRestriction) content).getBaseTypeName();
// $NON-NLS-1$
nameExt = "Restriction";
}
if (superTypeName != null) {
// try to get the type definition of the super type
XmlTypeDefinition superType = index.getOrCreateType(superTypeName);
// create an anonymous type that extends the super
// type
QName anonymousName = new QName(getTypeIdentifier(declaringGroup) + "/" + element.getName(), // $NON-NLS-1$
superTypeName.getLocalPart() + nameExt);
AnonymousXmlType anonymousType = new AnonymousXmlType(anonymousName);
anonymousType.setSuperType(superType);
// set metadata and constraints
setMetadata(anonymousType, complexType, schemaLocation);
anonymousType.setConstraint(HasValueFlag.ENABLED);
// set no binding, inherit it from the super type
// XXX is this ok?
// add properties to the anonymous type
createProperties(anonymousType, complexType, schemaLocation, schemaNamespace);
// create a property with the anonymous type
DefaultPropertyDefinition property = new DefaultPropertyDefinition(element.getQName(), declaringGroup, anonymousType);
// set metadata and constraints
setMetadataAndConstraints(property, element, schemaLocation);
} else {
reporter.error(new IOMessageImpl("Could not determine super type for simple content", null, content.getLineNumber(), content.getLinePosition()));
}
// </extension>
// </simpleContent>
}
} else {
// this where we get when there is an anonymous complex type
// as property type
// create an anonymous type
QName anonymousName = new QName(getTypeIdentifier(declaringGroup) + "/" + element.getName(), "AnonymousType");
// create anonymous type with no super type
AnonymousXmlType anonymousType = new AnonymousXmlType(anonymousName);
// set metadata and constraints
setMetadataAndConstraints(anonymousType, complexType, schemaLocation);
// add properties to the anonymous type
createProperties(anonymousType, complexType, schemaLocation, schemaNamespace);
// create a property with the anonymous type
DefaultPropertyDefinition property = new DefaultPropertyDefinition(element.getQName(), declaringGroup, anonymousType);
// set metadata and constraints
setMetadataAndConstraints(property, element, schemaLocation);
}
// </complexType>
// </element>
} else if (element.getSchemaType() instanceof XmlSchemaSimpleType) {
// simple schema type
XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) element.getSchemaType();
// create an anonymous type
QName anonymousName = new QName(getTypeIdentifier(declaringGroup) + "/" + element.getName(), // $NON-NLS-1$
"AnonymousType");
AnonymousXmlType anonymousType = new AnonymousXmlType(anonymousName);
configureSimpleType(anonymousType, simpleType, schemaLocation);
// create a property with the anonymous type
DefaultPropertyDefinition property = new DefaultPropertyDefinition(element.getQName(), declaringGroup, anonymousType);
// set metadata and constraints
setMetadataAndConstraints(property, element, schemaLocation);
}
} else {
// <element name="..." />
// no type defined
reporter.warn(new IOMessageImpl("Element definition without an associated type: {0}", null, element.getLineNumber(), element.getLinePosition(), element.getQName()));
// assuming xsd:anyType as default type
QName elementName = element.getQName();
SubstitutionGroupProperty substitutionGroup = new SubstitutionGroupProperty(new QName(elementName.getNamespaceURI() + "/" + elementName.getLocalPart(), // TODO
"choice"), // naming?
declaringGroup);
DefaultPropertyDefinition property = new DefaultPropertyDefinition(elementName, substitutionGroup, index.getOrCreateType(XmlTypeUtil.NAME_ANY_TYPE));
// set metadata and constraints
setMetadataAndConstraints(property, element, schemaLocation);
substitutionGroup.setProperty(property);
}
}
use of eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition in project hale by halestudio.
the class XmlSchemaReader method getTypeForAttribute.
/**
* Get the named type to use for a specific XML attribute.
*
* @param typeName the name of the referenced type
* @param declaringGroup the declaring group of the attribute
* @param attributeName the attribute name
* @return the type definition that should be used as the attribute type
*/
private XmlTypeDefinition getTypeForAttribute(QName typeName, DefinitionGroup declaringGroup, QName attributeName) {
// detect nilReason attribute TODO check GML namespace?
if (attributeName.getLocalPart().equals("nilReason") && "NilReasonType".equals(typeName.getLocalPart()) && declaringGroup instanceof Definition<?>) {
Definition<?> parentDef = (Definition<?>) declaringGroup;
// determine if parent is defined in INSPIRE
if (parentDef.getName().getNamespaceURI() != null && parentDef.getName().getNamespaceURI().startsWith("http://inspire.ec.europa.eu/schemas")) {
// get or create custom INSPIRE NilReason type
XmlTypeDefinition customType = (XmlTypeDefinition) this.index.getType(INSPIRE_NILREASON_TYPENAME);
if (customType == null) {
// not yet created, configure now
customType = this.index.getOrCreateType(INSPIRE_NILREASON_TYPENAME);
// use the original type as super type
customType.setSuperType(this.index.getOrCreateType(typeName));
// description with documentation of the values
customType.setDescription("Virtual type representing the GML NilReasonType adapted for the valid values specified by INSPIRE:\n\n" + "unknown:\nThe correct value for the specific spatial object is not known to, and not computable by, the data provider. However, a correct value may exist.\n" + "NOTE 'unknown' is applied on an object-by-object basis in a spatial data set.\n\n" + "unpopulated:\nThe characteristic is not part of the dataset maintained by the data provider. However, the characteristic may exist in the real world.\n" + "NOTE The characteristic receives this value for all objects in the spatial data set.\n\n" + "withheld:\nThe characteristic may exist, but is confidential and not divulged by the data provider.");
// define a custom enumeration based on valid INSPIRE void
// reasons
customType.setConstraint(new Enumeration<String>(INSPIRE_NILREASON_VALUES, true));
}
return customType;
}
}
// default case
return this.index.getOrCreateType(typeName);
}
Aggregations