Search in sources :

Example 11 with DefaultInstance

use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance in project hale by halestudio.

the class StreamGmlHelper method parseInstance.

/**
 * Parses an instance with the given type from the given XML stream reader.
 *
 * @param reader the XML stream reader, the current event must be the start
 *            element of the instance
 * @param type the definition of the instance type
 * @param indexInStream the index of the instance in the stream or
 *            <code>null</code>
 * @param strict if associating elements with properties should be done
 *            strictly according to the schema, otherwise a fall-back is
 *            used trying to populate values also on invalid property paths
 * @param srsDimension the dimension of the instance or <code>null</code>
 * @param crsProvider CRS provider in case no CRS is specified, may be
 *            <code>null</code>
 * @param parentType the type of the topmost instance
 * @param propertyPath the property path down from the topmost instance, may
 *            be <code>null</code>
 * @param allowNull if a <code>null</code> result is allowed
 * @param ignoreNamespaces if parsing of the XML instances should allow
 *            types and properties with namespaces that differ from those
 *            defined in the schema
 * @param ioProvider the I/O Provider to get value
 * @param crs The <code>CRSDefinition</code> to use for geometries
 * @return the parsed instance, may be <code>null</code> if allowNull is
 *         <code>true</code>
 * @throws XMLStreamException if parsing the instance failed
 */
public static Instance parseInstance(XMLStreamReader reader, TypeDefinition type, Integer indexInStream, boolean strict, Integer srsDimension, CRSProvider crsProvider, TypeDefinition parentType, List<QName> propertyPath, boolean allowNull, boolean ignoreNamespaces, IOProvider ioProvider, CRSDefinition crs) throws XMLStreamException {
    checkState(reader.getEventType() == XMLStreamConstants.START_ELEMENT);
    if (propertyPath == null) {
        propertyPath = Collections.emptyList();
    }
    if (srsDimension == null) {
        String dim = reader.getAttributeValue(null, "srsDimension");
        if (dim != null)
            srsDimension = Integer.parseInt(dim);
    }
    // extract additional settings from I/O provider
    boolean suppressParsingGeometry = ioProvider.getParameter(StreamGmlReader.PARAM_SUPPRESS_PARSE_GEOMETRY).as(Boolean.class, false);
    MutableInstance instance;
    if (indexInStream == null) {
        // not necessary to associate data set
        instance = new DefaultInstance(type, null);
    } else {
        instance = new StreamGmlInstance(type, indexInStream);
    }
    // If the current instance has an srsName attribute, try to resolve the
    // corresponding CRS and pass it down the hierarchy and use it for
    // nested geometries that don't have their own srsName.
    CRSDefinition lastCrs = crs;
    String srsName = reader.getAttributeValue(null, "srsName");
    if (srsName != null) {
        lastCrs = CodeDefinition.tryResolve(srsName);
        if (lastCrs == null && crsProvider != null) {
            // In case the srsName value could not be resolved to a CRS, try
            // to resolve the CRS via the crsProvider.
            CRSDefinition unresolvedCrs = new CodeDefinition(srsName);
            CRSDefinition resolvedCrs = crsProvider.getCRS(parentType, propertyPath, unresolvedCrs);
            // unresolvedCrs unchanged
            if (resolvedCrs != null && !resolvedCrs.equals(unresolvedCrs)) {
                lastCrs = resolvedCrs;
            }
        }
    // If the provided CRS could not be resolved, it will be ignored
    // here silently, so that use cases that don't need the CRS will not
    // fail.
    }
    boolean mixed = type.getConstraint(XmlMixedFlag.class).isEnabled();
    if (!mixed) {
        // mixed types are treated special (see else)
        // check if xsi:nil attribute is there and set to true
        String nilString = reader.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil");
        boolean isNil = nilString != null && "true".equalsIgnoreCase(nilString);
        // instance properties
        parseProperties(reader, instance, strict, srsDimension, crsProvider, lastCrs, parentType, propertyPath, false, ignoreNamespaces, ioProvider);
        // nil instance w/o properties
        if (allowNull && isNil && Iterables.isEmpty(instance.getPropertyNames())) {
        // no value should be created
        /*
				 * XXX returning null here then results in problems during
				 * adding other properties to the parent group, as mandatory
				 * elements are expected to appear, and it will warn about
				 * possible invalid data loaded
				 */
        // return null;
        }
        // instance value
        if (!hasElements(type)) {
            /*
				 * Value can only be determined if there are no documents,
				 * because otherwise elements have already been processed in
				 * parseProperties and we are already past END_ELEMENT.
				 */
            if (type.getConstraint(HasValueFlag.class).isEnabled()) {
                // try to get text value
                String value = reader.getElementText();
                if (!isNil && value != null) {
                    instance.setValue(convertSimple(type, value));
                }
            }
        }
    } else {
        /*
			 * XXX For a mixed type currently ignore elements and parse only
			 * attributes and text.
			 */
        // instance properties (attributes only)
        parseProperties(reader, instance, strict, srsDimension, crsProvider, lastCrs, parentType, propertyPath, true, ignoreNamespaces, ioProvider);
        // combined text
        String value = readText(reader);
        if (value != null) {
            instance.setValue(convertSimple(type, value));
        }
    }
    // augmented value XXX should this be an else if?
    if (!suppressParsingGeometry && type.getConstraint(AugmentedValueFlag.class).isEnabled()) {
        // add geometry as a GeometryProperty value where applicable
        GeometryFactory geomFactory = type.getConstraint(GeometryFactory.class);
        Object geomValue = null;
        // the default value for the srsDimension
        int defaultValue = 2;
        try {
            if (srsDimension != null) {
                geomValue = geomFactory.createGeometry(instance, srsDimension, ioProvider);
            } else {
                // srsDimension is not set
                geomValue = geomFactory.createGeometry(instance, defaultValue, ioProvider);
            }
        } catch (Exception e) {
            /*
				 * Catch IllegalArgumentException that e.g. occurs if a linear
				 * ring has to few points. NullPointerExceptions may occur
				 * because an internal geometry could not be created.
				 * 
				 * XXX a problem is that these messages will not appear in the
				 * report
				 */
            log.error("Error creating geometry", e);
        }
        if (geomValue != null && crsProvider != null && propertyPath != null) {
            // check if CRS are set, and if not, try determining them using
            // the CRS provider
            Collection<?> values;
            if (geomValue instanceof Collection) {
                values = (Collection<?>) geomValue;
            } else {
                values = Collections.singleton(geomValue);
            }
            List<Object> resultVals = new ArrayList<Object>();
            for (Object value : values) {
                if (value instanceof Geometry || (value instanceof GeometryProperty<?> && ((GeometryProperty<?>) value).getCRSDefinition() == null)) {
                    // try to resolve value of srsName attribute
                    CRSDefinition geometryCrs = crsProvider.getCRS(parentType, propertyPath, lastCrs);
                    if (geometryCrs != null) {
                        Geometry geom = (value instanceof Geometry) ? ((Geometry) value) : (((GeometryProperty<?>) value).getGeometry());
                        resultVals.add(new DefaultGeometryProperty<Geometry>(geometryCrs, geom));
                        continue;
                    }
                }
                resultVals.add(value);
            }
            if (resultVals.size() == 1) {
                geomValue = resultVals.get(0);
            } else {
                geomValue = resultVals;
            }
        }
        if (geomValue != null) {
            instance.setValue(geomValue);
        }
    }
    return instance;
}
Also used : DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) GeometryFactory(eu.esdihumboldt.hale.io.gml.geometry.constraint.GeometryFactory) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) ArrayList(java.util.ArrayList) XMLStreamException(javax.xml.stream.XMLStreamException) Geometry(com.vividsolutions.jts.geom.Geometry) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) XmlMixedFlag(eu.esdihumboldt.hale.io.xsd.constraint.XmlMixedFlag) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Collection(java.util.Collection)

Example 12 with DefaultInstance

use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance in project hale by halestudio.

the class GroovyRetypePage method validate.

@Override
protected boolean validate(String document) {
    super.validate(document);
    Type targetType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
    Type sourceType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getSource());
    if (sourceType == null || targetType == null) {
        // not yet selected (NewRelationWizard)
        return false;
    }
    InstanceBuilder builder = new InstanceBuilder(false);
    Instance instance = testValues.get(sourceType.getDefinition());
    if (instance == null) {
        // use an empty instance as input for the script
        instance = new DefaultInstance(sourceType.getDefinition().getDefinition(), DataSet.SOURCE);
    }
    FamilyInstance source = new FamilyInstanceImpl(instance);
    Cell cell = getWizard().getUnfinishedCell();
    CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
    ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
    Binding binding = GroovyRetype.createBinding(source, cell, builder, log, context, targetType.getDefinition().getDefinition());
    GroovyService service = HaleUI.getServiceProvider().getService(GroovyService.class);
    Script script = null;
    try {
        script = service.parseScript(document, binding);
        GroovyUtil.evaluateAll(script, builder, targetType.getDefinition().getDefinition(), service, log);
    } catch (final Exception e) {
        return handleValidationResult(script, e);
    }
    return handleValidationResult(script, null);
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) DefaultTransformationReporter(eu.esdihumboldt.hale.common.align.transformation.report.impl.DefaultTransformationReporter) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) Type(eu.esdihumboldt.hale.common.align.model.Type) ExecutionContext(eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Cell(eu.esdihumboldt.hale.common.align.model.Cell) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 13 with DefaultInstance

use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance in project hale by halestudio.

the class AlignmentUtil method matchCondition.

/**
 * Match a property condition against a property value.
 *
 * @param condition the property condition
 * @param value the property value
 * @param parent the parent of the property value, may be <code>null</code>
 *            if there is none
 * @return if the value matched the property condition
 */
public static boolean matchCondition(Condition condition, Object value, Object parent) {
    // create dummy instance
    MutableInstance dummy = new DefaultInstance(null, null);
    // add value as property
    dummy.addProperty(new QName("value"), value);
    // add parent value as property
    if (parent != null) {
        dummy.addProperty(new QName("parent"), parent);
    }
    return condition.getFilter().match(dummy);
}
Also used : QName(javax.xml.namespace.QName) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance)

Example 14 with DefaultInstance

use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance in project hale by halestudio.

the class FilterTest method simpleFilterTestECQL.

@Test
public void simpleFilterTestECQL() throws CQLException {
    DefaultTypeDefinition stringType = new DefaultTypeDefinition(new QName("StringType"));
    stringType.setConstraint(Binding.get(String.class));
    DefaultTypeDefinition personDef = new DefaultTypeDefinition(new QName("PersonType"));
    personDef.addChild(new DefaultPropertyDefinition(new QName("Name"), personDef, stringType));
    DefaultTypeDefinition autoDef = new DefaultTypeDefinition(new QName("AutoType"));
    autoDef.addChild(new DefaultPropertyDefinition(new QName("Name"), autoDef, stringType));
    autoDef.addChild(new DefaultPropertyDefinition(new QName("Besitzer"), autoDef, personDef));
    MutableInstance auto = new DefaultInstance(autoDef, null);
    auto.addProperty(new QName("Name"), "Mein Porsche");
    MutableInstance ich = new DefaultInstance(personDef, null);
    ich.addProperty(new QName("Name"), "Ich");
    auto.addProperty(new QName("Besitzer"), ich);
    Filter filter;
    filter = new FilterGeoECqlImpl("Name = 'Mein Porsche'");
    assertTrue(filter.match(auto));
    Filter filter1 = new FilterGeoECqlImpl("Name like '%Porsche%'");
    assertTrue(filter1.match(auto));
}
Also used : DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) DefaultPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) QName(javax.xml.namespace.QName) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Test(org.junit.Test)

Example 15 with DefaultInstance

use of eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance in project hale by halestudio.

the class PropertyResolver method getQueryPaths.

/**
 * Returns all possible fully qualified (with namespaces) paths matching the
 * given query.
 *
 * @param typeDef the type definition
 * @param dataSet the data set
 * @param query the query
 * @return a list of all possible paths matching the query (which may be
 *         empty)
 */
public static List<List<QName>> getQueryPaths(TypeDefinition typeDef, DataSet dataSet, String query) {
    Instance instance = new DefaultInstance(typeDef, dataSet);
    List<String> paths = getQueryPath(instance, query);
    ArrayList<List<QName>> result = new ArrayList<List<QName>>(paths.size());
    for (String path : paths) result.add(getQNamesFromPath(path));
    return Collections.unmodifiableList(result);
}
Also used : DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)18 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)11 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)9 QName (javax.xml.namespace.QName)7 ArrayList (java.util.ArrayList)5 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)4 Test (org.junit.Test)4 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)3 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)3 DefaultGroup (eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup)3 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)3 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)3 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)3 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)3 MultiValue (eu.esdihumboldt.cst.MultiValue)2 Cell (eu.esdihumboldt.hale.common.align.model.Cell)2 ParameterValue (eu.esdihumboldt.hale.common.align.model.ParameterValue)2 Type (eu.esdihumboldt.hale.common.align.model.Type)2 ExecutionContext (eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext)2 FamilyInstanceImpl (eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl)2