Search in sources :

Example 61 with IOMessageImpl

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();
    }
}
Also used : IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) InputStream(java.io.InputStream) SAXParseException(org.xml.sax.SAXParseException) ArrayList(java.util.ArrayList) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) URI(java.net.URI) AbstractInstanceValidator(eu.esdihumboldt.hale.common.instance.io.impl.AbstractInstanceValidator) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Example 62 with IOMessageImpl

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);
    }
}
Also used : DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) XmlSchemaComplexContentExtension(org.apache.ws.commons.schema.XmlSchemaComplexContentExtension) XmlSchemaSimpleContentExtension(org.apache.ws.commons.schema.XmlSchemaSimpleContentExtension) QName(javax.xml.namespace.QName) AnonymousXmlType(eu.esdihumboldt.hale.io.xsd.reader.internal.AnonymousXmlType) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) XmlElementReferenceProperty(eu.esdihumboldt.hale.io.xsd.reader.internal.XmlElementReferenceProperty) XmlSchemaContentModel(org.apache.ws.commons.schema.XmlSchemaContentModel) XmlSchemaSimpleContentRestriction(org.apache.ws.commons.schema.XmlSchemaSimpleContentRestriction) SubstitutionGroupProperty(eu.esdihumboldt.hale.io.xsd.reader.internal.SubstitutionGroupProperty) XmlTypeDefinition(eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlSchemaContent(org.apache.ws.commons.schema.XmlSchemaContent) XmlSchemaComplexContentRestriction(org.apache.ws.commons.schema.XmlSchemaComplexContentRestriction) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType)

Example 63 with IOMessageImpl

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()));
    }
}
Also used : XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) QName(javax.xml.namespace.QName) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) XmlSchemaSimpleTypeList(org.apache.ws.commons.schema.XmlSchemaSimpleTypeList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 64 with IOMessageImpl

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));
            }
        }
    }
}
Also used : UnsupportedTransformationException(eu.esdihumboldt.hale.io.appschema.writer.internal.UnsupportedTransformationException) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) PropertyTransformationHandler(eu.esdihumboldt.hale.io.appschema.writer.internal.PropertyTransformationHandler) FeatureTypeMapping(eu.esdihumboldt.hale.io.appschema.impl.internal.generated.app_schema.TypeMappingsPropertyType.FeatureTypeMapping) Cell(eu.esdihumboldt.hale.common.align.model.Cell) TypeTransformationHandler(eu.esdihumboldt.hale.io.appschema.writer.internal.TypeTransformationHandler)

Example 65 with IOMessageImpl

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;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) Element(org.w3c.dom.Element) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Aggregations

IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)85 IOException (java.io.IOException)43 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)33 QName (javax.xml.namespace.QName)20 URI (java.net.URI)15 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)14 InputStream (java.io.InputStream)13 File (java.io.File)12 HashMap (java.util.HashMap)11 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)9 FileOutputStream (java.io.FileOutputStream)9 ArrayList (java.util.ArrayList)9 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)8 OutputStream (java.io.OutputStream)8 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)7 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)7 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)6 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)5 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)4 Value (eu.esdihumboldt.hale.common.core.io.Value)4