use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl 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.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.
the class XmlSchemaReader method createAttributesFromCollection.
private void createAttributesFromCollection(XmlSchemaObjectCollection attributeCollection, DefinitionGroup declaringType, String indexPrefix, String schemaLocation, String schemaNamespace) {
if (indexPrefix == null) {
// $NON-NLS-1$
indexPrefix = "";
}
for (int index = 0; index < attributeCollection.getCount(); index++) {
XmlSchemaObject object = attributeCollection.getItem(index);
if (object instanceof XmlSchemaAttribute) {
// <attribute ... />
XmlSchemaAttribute attribute = (XmlSchemaAttribute) object;
createAttribute(attribute, declaringType, schemaLocation, schemaNamespace);
} else if (object instanceof XmlSchemaAttributeGroup) {
XmlSchemaAttributeGroup group = (XmlSchemaAttributeGroup) object;
createAttributes(group, declaringType, indexPrefix + index, schemaLocation, schemaNamespace);
} else if (object instanceof XmlSchemaAttributeGroupRef) {
XmlSchemaAttributeGroupRef groupRef = (XmlSchemaAttributeGroupRef) object;
if (groupRef.getRefName() != null) {
QName groupName = groupRef.getRefName();
// XXX extend group name with namespace?
XmlAttributeGroupReferenceProperty property = new XmlAttributeGroupReferenceProperty(groupName, declaringType, this.index, groupName, true);
// TODO add constraints?
// set metadata
setMetadata(property, groupRef, schemaLocation);
} else {
reporter.error(new IOMessageImpl("Unrecognized attribute group reference", null, object.getLineNumber(), object.getLinePosition()));
}
}
}
}
use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.
the class XmlSchemaReader method createPropertiesFromParticle.
/**
* Extracts attribute definitions from a {@link XmlSchemaParticle}.
*
* @param declaringGroup the definition of the declaring group
* @param particle the particle
* @param schemaLocation the schema location
* @param schemaNamespace the schema namespace
* @param forceGroup force creating a group (e.g. if the parent is a choice)
*/
private void createPropertiesFromParticle(DefinitionGroup declaringGroup, XmlSchemaParticle particle, String schemaLocation, String schemaNamespace, boolean forceGroup) {
// particle:
if (particle instanceof XmlSchemaSequence) {
// <sequence>
XmlSchemaSequence sequence = (XmlSchemaSequence) particle;
// once will result in no group if not forced)
if (forceGroup || sequence.getMinOccurs() != 1 || sequence.getMaxOccurs() != 1) {
// create a sequence group
QName sequenceName = createGroupName(declaringGroup, "sequence");
DefaultGroupPropertyDefinition sequenceGroup = new DefaultGroupPropertyDefinition(sequenceName, declaringGroup, false);
// set cardinality
long max = (sequence.getMaxOccurs() == Long.MAX_VALUE) ? (Cardinality.UNBOUNDED) : (sequence.getMaxOccurs());
sequenceGroup.setConstraint(Cardinality.get(sequence.getMinOccurs(), max));
// set choice constraint (no choice)
sequenceGroup.setConstraint(ChoiceFlag.DISABLED);
// set metadata
setMetadata(sequenceGroup, sequence, schemaLocation);
// use group as parent
declaringGroup = sequenceGroup;
}
for (int j = 0; j < sequence.getItems().getCount(); j++) {
XmlSchemaObject object = sequence.getItems().getItem(j);
if (object instanceof XmlSchemaElement) {
// <element>
createPropertyFromElement((XmlSchemaElement) object, declaringGroup, schemaLocation, schemaNamespace);
// </element>
} else if (object instanceof XmlSchemaParticle) {
// contained particles, e.g. a choice
// content doesn't need to be grouped, it can be decided in
// the method
createPropertiesFromParticle(declaringGroup, (XmlSchemaParticle) object, schemaLocation, schemaNamespace, false);
}
}
// </sequence>
} else if (particle instanceof XmlSchemaChoice) {
// <choice>
XmlSchemaChoice choice = (XmlSchemaChoice) particle;
// create a choice group
QName choiceName = createGroupName(declaringGroup, "choice");
DefaultGroupPropertyDefinition choiceGroup = new DefaultGroupPropertyDefinition(choiceName, declaringGroup, // no flatten allowed
false);
// because of choice
// set custom display name
choiceGroup.setConstraint(DISPLAYNAME_CHOICE);
// set cardinality
long max = (choice.getMaxOccurs() == Long.MAX_VALUE) ? (Cardinality.UNBOUNDED) : (choice.getMaxOccurs());
choiceGroup.setConstraint(Cardinality.get(choice.getMinOccurs(), max));
// set choice constraint
choiceGroup.setConstraint(ChoiceFlag.ENABLED);
// set metadata
setMetadata(choiceGroup, choice, schemaLocation);
// create properties with choiceGroup as parent
for (int j = 0; j < choice.getItems().getCount(); j++) {
XmlSchemaObject object = choice.getItems().getItem(j);
if (object instanceof XmlSchemaElement) {
// <element>
createPropertyFromElement((XmlSchemaElement) object, choiceGroup, schemaLocation, schemaNamespace);
} else if (object instanceof XmlSchemaParticle) {
// contained particles, e.g. a choice or sequence
// inside a choice they must form a group
createPropertiesFromParticle(choiceGroup, (XmlSchemaParticle) object, schemaLocation, schemaNamespace, true);
}
}
// </choice>
} else if (particle instanceof XmlSchemaGroupRef) {
// <group ref="..." />
XmlSchemaGroupRef groupRef = (XmlSchemaGroupRef) particle;
QName groupName = groupRef.getRefName();
long max = (groupRef.getMaxOccurs() == Long.MAX_VALUE) ? (Cardinality.UNBOUNDED) : (groupRef.getMaxOccurs());
long min = groupRef.getMinOccurs();
/*
* Only allow flatten if group is not forced and appears exactly
* once
*/
XmlGroupReferenceProperty property = new XmlGroupReferenceProperty(groupName, declaringGroup, index, groupName, !forceGroup && min == 1 && max == 1);
// set cardinality constraint
property.setConstraint(Cardinality.get(min, max));
// set metadata
setMetadata(property, groupRef, schemaLocation);
} else if (particle instanceof XmlSchemaAny) {
// XXX ignore for now
reporter.info(new IOMessageImpl("Particle that allows any element is not supported.", null, particle.getLineNumber(), particle.getLinePosition()));
} else {
reporter.error(new IOMessageImpl("Unrecognized particle: " + particle.getClass().getSimpleName(), null, particle.getLineNumber(), particle.getLinePosition()));
}
}
use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.
the class XmlTypeUtil method configureSimpleTypeRestriction.
/**
* Configure a type definition for a simple type based on a simple type
* restriction.
*
* @param type the type definition
* @param restriction the simple type restriction
* @param index the XML index for resolving type definitions
* @param reporter the report
*/
private static void configureSimpleTypeRestriction(XmlTypeDefinition type, XmlSchemaSimpleTypeRestriction restriction, XmlIndex index, IOReporter reporter) {
QName baseTypeName = restriction.getBaseTypeName();
XmlTypeDefinition baseTypeDef;
if (baseTypeName != null) {
// resolve super type
baseTypeDef = index.getOrCreateType(baseTypeName);
} else if (restriction.getBaseType() != null) {
// simple schema type
XmlSchemaSimpleType simpleType = restriction.getBaseType();
// create an anonymous type
QName anonymousName = new QName(type.getName().getNamespaceURI() + "/" + type.getName().getLocalPart(), // $NON-NLS-1$
"AnonymousSuperType");
baseTypeDef = new AnonymousXmlType(anonymousName);
XmlTypeUtil.configureSimpleType(type, simpleType, index, reporter);
// set metadata
// no schema
XmlSchemaReader.setMetadata(type, simpleType, null);
// location
// available
// at this
// point
} else {
reporter.error(new IOMessageImpl("Simple type restriction without base type, skipping type configuration.", null, restriction.getLineNumber(), restriction.getLinePosition()));
return;
}
// set super type
type.setSuperType(baseTypeDef);
// mark as restriction
type.setConstraint(RestrictionFlag.ENABLED);
// assign no binding, inherit from super type
// The following code expects schema to be valid.
List<Validator> validators = new LinkedList<Validator>();
// patterns and enumerations in one step are ORed together!
List<String> values = new LinkedList<String>();
List<Validator> patternValidators = new LinkedList<Validator>();
// TODO different handling for date/time/g.../duration in
// (min|max)(In|Ex)clusive
// XXX only for date, time, duration, dateTime, gMonthDay, gYearMonth?
// no also for some cases of gYear, gMonth, gDay (they can have a
// timezone!)
// but only need to handle cases where isDecimal() is false...
XmlSchemaObjectCollection facets = restriction.getFacets();
for (int i = 0; i < facets.getCount(); i++) {
XmlSchemaFacet facet = (XmlSchemaFacet) facets.getItem(i);
if (facet instanceof XmlSchemaEnumerationFacet) {
values.add(facet.getValue().toString());
} else if (facet instanceof XmlSchemaFractionDigitsFacet) {
validators.add(new DigitCountValidator(DigitCountValidator.Type.FRACTIONDIGITS, Integer.parseInt(facet.getValue().toString())));
} else if (facet instanceof XmlSchemaLengthFacet) {
validators.add(new LengthValidator(LengthValidator.Type.EXACT, Integer.parseInt(facet.getValue().toString())));
} else if (facet instanceof XmlSchemaMaxExclusiveFacet) {
if (// number or date?
isDecimal(facet.getValue().toString()))
validators.add(new NumberValidator(NumberValidator.Type.MAXEXCLUSIVE, new BigDecimal(facet.getValue().toString())));
else
reporter.warn(new IOMessageImpl("(min|max)(In|Ex)clusive not supported for non-number types", null, facet.getLineNumber(), facet.getLinePosition()));
} else if (facet instanceof XmlSchemaMaxInclusiveFacet) {
if (// number or date?
isDecimal(facet.getValue().toString()))
validators.add(new NumberValidator(NumberValidator.Type.MAXINCLUSIVE, new BigDecimal(facet.getValue().toString())));
else
reporter.warn(new IOMessageImpl("(min|max)(In|Ex)clusive not supported for non-number types", null, facet.getLineNumber(), facet.getLinePosition()));
} else if (facet instanceof XmlSchemaMaxLengthFacet) {
validators.add(new LengthValidator(LengthValidator.Type.MAXIMUM, Integer.parseInt(facet.getValue().toString())));
} else if (facet instanceof XmlSchemaMinLengthFacet) {
validators.add(new LengthValidator(LengthValidator.Type.MINIMUM, Integer.parseInt(facet.getValue().toString())));
} else if (facet instanceof XmlSchemaMinExclusiveFacet) {
if (// number or date?
isDecimal(facet.getValue().toString()))
validators.add(new NumberValidator(NumberValidator.Type.MINEXCLUSIVE, new BigDecimal(facet.getValue().toString())));
else
reporter.warn(new IOMessageImpl("(min|max)(In|Ex)clusive not supported for non-number types", null, facet.getLineNumber(), facet.getLinePosition()));
} else if (facet instanceof XmlSchemaMinInclusiveFacet) {
if (// number or date?
isDecimal(facet.getValue().toString()))
validators.add(new NumberValidator(NumberValidator.Type.MININCLUSIVE, new BigDecimal(facet.getValue().toString())));
else
reporter.warn(new IOMessageImpl("(min|max)(In|Ex)clusive not supported for non-number types", null, facet.getLineNumber(), facet.getLinePosition()));
} else if (facet instanceof XmlSchemaPatternFacet) {
patternValidators.add(new PatternValidator(facet.getValue().toString()));
} else if (facet instanceof XmlSchemaTotalDigitsFacet) {
validators.add(new DigitCountValidator(DigitCountValidator.Type.TOTALDIGITS, Integer.parseInt(facet.getValue().toString())));
} else if (facet instanceof XmlSchemaWhiteSpaceFacet) {
reporter.info(new IOMessageImpl("White space facet not supported", null, facet.getLineNumber(), facet.getLinePosition()));
// Nothing to validate according to w3.
// Values should be processed according to rule?
} else {
reporter.error(new IOMessageImpl("Unrecognized facet: " + facet.getClass().getSimpleName(), null, facet.getLineNumber(), facet.getLinePosition()));
}
}
if (!patternValidators.isEmpty())
validators.add(new OrValidator(patternValidators));
if (!values.isEmpty()) {
// set enumeration constraint
// no check of which values are okay, they must be validated
// somewhere else.
// XXX conversion to be done?
type.setConstraint(new Enumeration<String>(values, false));
validators.add(new EnumerationValidator(values));
}
if (!validators.isEmpty())
type.setConstraint(new ValidationConstraint(new AndValidator(validators), type));
}
use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.
the class XmlTypeUtil method configureSimpleType.
/**
* Configure a type definition for a simple type based on the
* {@link XmlSchemaSimpleType}.
*
* @param type the type definition
* @param simpleType the schema simple type
* @param index the XML index for resolving type definitions
* @param reporter the report
*/
public static void configureSimpleType(XmlTypeDefinition type, XmlSchemaSimpleType simpleType, XmlIndex index, IOReporter reporter) {
XmlSchemaSimpleTypeContent content = simpleType.getContent();
// it's a simple type
type.setConstraint(HasValueFlag.ENABLED);
if (content instanceof XmlSchemaSimpleTypeUnion) {
// simple type union
configureSimpleTypeUnion(type, (XmlSchemaSimpleTypeUnion) content, index, reporter);
} else if (content instanceof XmlSchemaSimpleTypeList) {
// simple type list
configureSimpleTypeList(type, (XmlSchemaSimpleTypeList) content, index, reporter);
} else if (content instanceof XmlSchemaSimpleTypeRestriction) {
// simple type restriction
configureSimpleTypeRestriction(type, (XmlSchemaSimpleTypeRestriction) content, index, reporter);
} else {
reporter.error(new IOMessageImpl(MessageFormat.format("Unrecognized simple type {0}", type.getName()), null, simpleType.getLineNumber(), simpleType.getLinePosition()));
}
}
Aggregations