use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.
the class XmlInstanceValidator method execute.
/**
* @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Validating XML", ProgressIndicator.UNKNOWN);
List<URI> schemaLocations = new ArrayList<URI>();
for (Locatable schema : getSchemas()) {
URI loc = schema.getLocation();
if (loc != null) {
schemaLocations.add(loc);
} else {
reporter.warn(new IOMessageImpl("No location for schema, may cause validation to fail.", null));
}
}
Validator val = ValidatorFactory.getInstance().createValidator(schemaLocations.toArray(new URI[schemaLocations.size()]));
InputStream in = getSource().getInput();
try {
Report report = val.validate(in);
// use the report information to populate reporter
for (SAXParseException warning : report.getWarnings()) {
reporter.warn(new //
IOMessageImpl(//
warning.getLocalizedMessage(), //
warning, //
warning.getLineNumber(), warning.getColumnNumber()));
}
for (SAXParseException error : report.getErrors()) {
reporter.error(new //
IOMessageImpl(//
error.getLocalizedMessage(), //
error, //
error.getLineNumber(), error.getColumnNumber()));
}
reporter.setSuccess(report.isValid());
return reporter;
} finally {
in.close();
progress.end();
}
}
use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl 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.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.
the class XmlTypeUtil method configureSimpleTypeList.
/**
* Configure a type definition for a simple type based on a simple type
* list.
*
* @param type the type definition
* @param list the simple type list
* @param index the XML index for resolving type definitions
* @param reporter the report
*/
private static void configureSimpleTypeList(XmlTypeDefinition type, XmlSchemaSimpleTypeList list, XmlIndex index, IOReporter reporter) {
XmlTypeDefinition elementType = null;
if (list.getItemType() != null) {
XmlSchemaSimpleType simpleType = list.getItemType();
if (simpleType.getQName() != null) {
// named type
elementType = index.getOrCreateType(simpleType.getQName());
} else {
// anonymous type
QName baseName = new QName(type.getName().getNamespaceURI() + "/" + type.getName().getLocalPart(), // $NON-NLS-1$ //$NON-NLS-2$
"AnonymousType");
elementType = new AnonymousXmlType(baseName);
}
configureSimpleType(elementType, simpleType, index, reporter);
} else if (list.getItemTypeName() != null) {
// named type
elementType = index.getOrCreateType(list.getItemTypeName());
}
if (elementType != null) {
// set constraints on type
// element type
type.setConstraint(ElementType.createFromType(elementType));
// list binding
type.setConstraint(Binding.get(List.class));
} else {
reporter.error(new IOMessageImpl("Unrecognized base type for simple type list", null, list.getLineNumber(), list.getLinePosition()));
}
}
use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.
the class AppSchemaMappingGenerator method createTypeMappings.
private void createTypeMappings(AppSchemaMappingContext context, IOReporter reporter) {
Collection<? extends Cell> typeCells = alignment.getTypeCells();
for (Cell typeCell : typeCells) {
String typeTransformId = typeCell.getTransformationIdentifier();
TypeTransformationHandler typeTransformHandler = null;
try {
typeTransformHandler = TypeTransformationHandlerFactory.getInstance().createTypeTransformationHandler(typeTransformId);
FeatureTypeMapping ftMapping = typeTransformHandler.handleTypeTransformation(typeCell, context);
if (ftMapping != null) {
Collection<? extends Cell> propertyCells = alignment.getPropertyCells(typeCell);
for (Cell propertyCell : propertyCells) {
String propertyTransformId = propertyCell.getTransformationIdentifier();
PropertyTransformationHandler propertyTransformHandler = null;
try {
propertyTransformHandler = PropertyTransformationHandlerFactory.getInstance().createPropertyTransformationHandler(propertyTransformId);
propertyTransformHandler.handlePropertyTransformation(typeCell, propertyCell, context);
} catch (UnsupportedTransformationException e) {
String errMsg = MessageFormat.format("Error processing property cell {0}", propertyCell.getId());
log.warn(errMsg, e);
if (reporter != null) {
reporter.warn(new IOMessageImpl(errMsg, e));
}
}
}
}
} catch (UnsupportedTransformationException e) {
String errMsg = MessageFormat.format("Error processing type cell{0}", typeCell.getId());
log.warn(errMsg, e);
if (reporter != null) {
reporter.warn(new IOMessageImpl(errMsg, e));
}
}
}
}
use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.
the class InspireInstanceWriter method findMetadata.
/**
* Loads the given file and tries to find a MD_Metadata element.
*
* @param input the metadata source
* @param reporter the reporter
* @return the metadata element or <code>null</code> if it couldn't be found
*/
private Element findMetadata(InputStream input, IOReporter reporter) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc;
try {
doc = dbf.newDocumentBuilder().parse(input);
} catch (SAXException | IOException | ParserConfigurationException e) {
reporter.warn(new IOMessageImpl("Couldn't parse specified metadata file.", e));
return null;
}
NodeList nl = doc.getElementsByTagNameNS("http://www.isotc211.org/2005/gmd", "MD_Metadata");
Element result = null;
if (nl.getLength() == 1)
result = (Element) nl.item(0);
else if (nl.getLength() == 0)
reporter.warn(new IOMessageImpl("Couldn't include specified metadata file, no MD_Metadata element found.", null));
else {
// XXX Maybe ask the user somehow? Or better not include it?
reporter.warn(new IOMessageImpl("Found multiple MD_Metadata elements. Using first one.", null));
result = (Element) nl.item(0);
}
return result;
}
Aggregations