Search in sources :

Example 16 with Group

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

the class GmlInstanceCollectionTest method testLoadChoice.

/**
 * Test loading a simple XML file with one instance including a choice.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testLoadChoice() throws Exception {
    GmlInstanceCollection instances = loadInstances(getClass().getResource("/data/group/choice.xsd").toURI(), getClass().getResource("/data/group/choice.xml").toURI(), false);
    ResourceIterator<Instance> it = instances.iterator();
    try {
        assertTrue(it.hasNext());
        Instance instance = it.next();
        assertNotNull(instance);
        // choice
        Object[] choice_1 = instance.getProperty(new QName("/ItemsType", "choice_1"));
        assertNotNull(choice_1);
        assertEquals(5, choice_1.length);
        String[] expectedProperties = new String[] { "shirt", "hat", "shirt", "umbrella", "hat" };
        for (int i = 0; i < choice_1.length; i++) {
            assertTrue(choice_1[i] instanceof Group);
            Group choice = (Group) choice_1[i];
            String expectedProperty = expectedProperties[i];
            int num = 0;
            for (QName name : choice.getPropertyNames()) {
                // expecting only one property
                assertEquals(0, num++);
                assertEquals(new QName(expectedProperty), name);
            }
        }
        // only one object
        assertFalse(it.hasNext());
    } finally {
        it.close();
    }
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) Test(org.junit.Test)

Example 17 with Group

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

the class StreamGmlWriter method writeProperties.

/**
 * Write attribute or element properties.
 *
 * @param parent the parent group
 * @param children the child definitions
 * @param attributes <code>true</code> if attribute properties shall be
 *            written, <code>false</code> if element properties shall be
 *            written
 * @param parentIsNil if the parent property is nil
 * @param report the reporter
 * @throws XMLStreamException if writing the attributes/elements fails
 */
private void writeProperties(Group parent, Collection<? extends ChildDefinition<?>> children, boolean attributes, boolean parentIsNil, IOReporter report) throws XMLStreamException {
    if (parent == null) {
        return;
    }
    boolean parentIsChoice = parent.getDefinition() instanceof GroupPropertyDefinition && ((GroupPropertyDefinition) parent.getDefinition()).getConstraint(ChoiceFlag.class).isEnabled();
    for (ChildDefinition<?> child : children) {
        Object[] values = parent.getProperty(child.getName());
        if (child.asProperty() != null) {
            PropertyDefinition propDef = child.asProperty();
            boolean isAttribute = propDef.getConstraint(XmlAttributeFlag.class).isEnabled();
            if (attributes && isAttribute) {
                if (values != null && values.length > 0) {
                    boolean allowWrite = true;
                    // special case handling: omit nilReason
                    if (getParameter(PARAM_OMIT_NIL_REASON).as(Boolean.class, true)) {
                        Cardinality propCard = propDef.getConstraint(Cardinality.class);
                        if ("nilReason".equals(propDef.getName().getLocalPart()) && propCard.getMinOccurs() < 1) {
                            allowWrite = parentIsNil;
                        }
                    }
                    // write attribute
                    if (allowWrite) {
                        // nilReason "unpopulated"
                        if ("nilReason".equals(propDef.getName().getLocalPart()) && "unpopulated".equals(values[0])) {
                            // TODO more strict check to ensure that this is
                            // a GML nilReason? (check property type and
                            // parent types)
                            writeAttribute("other:unpopulated", propDef);
                        } else {
                            // default
                            writeAttribute(values[0], propDef);
                        }
                    }
                    if (values.length > 1) {
                    // TODO warning?!
                    }
                }
            } else if (!attributes && !isAttribute) {
                int numValues = 0;
                if (values != null) {
                    // write element
                    for (Object value : values) {
                        writeElement(value, propDef, report);
                    }
                    numValues = values.length;
                }
                // only if parent is not a choice
                if (!parentIsChoice) {
                    Cardinality cardinality = propDef.getConstraint(Cardinality.class);
                    if (cardinality.getMinOccurs() > numValues) {
                        if (propDef.getConstraint(NillableFlag.class).isEnabled()) {
                            // nillable element
                            for (int i = numValues; i < cardinality.getMinOccurs(); i++) {
                                // write nil element
                                writeElement(null, propDef, report);
                            }
                        } else {
                            for (int i = numValues; i < cardinality.getMinOccurs(); i++) {
                                // write empty element
                                GmlWriterUtil.writeEmptyElement(writer, propDef.getName());
                            }
                        // TODO add warning to report
                        }
                    }
                }
            }
        } else if (child.asGroup() != null) {
            // handle to child groups
            if (values != null) {
                for (Object value : values) {
                    if (value instanceof Group) {
                        writeProperties((Group) value, DefinitionUtil.getAllChildren(child.asGroup()), attributes, parentIsNil, report);
                    } else {
                    // TODO warning/error?
                    }
                }
            }
        }
    }
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) Group(eu.esdihumboldt.hale.common.instance.model.Group) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) XmlAttributeFlag(eu.esdihumboldt.hale.io.xsd.constraint.XmlAttributeFlag) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)

Example 18 with Group

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

the class StreamGmlWriter method writeElement.

/**
 * Write a property element.
 *
 * @param value the element value
 * @param propDef the property definition
 * @param report the reporter
 * @throws XMLStreamException if writing the element fails
 */
private void writeElement(Object value, PropertyDefinition propDef, IOReporter report) throws XMLStreamException {
    Group group = null;
    if (value instanceof Group) {
        group = (Group) value;
        if (value instanceof Instance) {
            // extract value from instance
            value = ((Instance) value).getValue();
        }
    }
    if (group == null) {
        if (value == null) {
            // null value
            if (propDef.getConstraint(Cardinality.class).getMinOccurs() > 0) {
                // write empty element
                GmlWriterUtil.writeEmptyElement(writer, propDef.getName());
                // mark as nil
                writeElementValue(null, propDef);
            }
        // otherwise just skip it
        } else {
            GmlWriterUtil.writeStartElement(writer, propDef.getName());
            Pair<Geometry, CRSDefinition> pair = extractGeometry(value, true, report);
            if (pair != null) {
                String srsName = extractCode(pair.getSecond());
                // write geometry
                writeGeometry(pair.getFirst(), propDef, srsName, report);
            } else {
                // simple element with value
                // write value as content
                writeElementValue(value, propDef);
            }
            writer.writeEndElement();
        }
    } else {
        // children and maybe a value
        GmlWriterUtil.writeStartElement(writer, propDef.getName());
        boolean hasValue = propDef.getPropertyType().getConstraint(HasValueFlag.class).isEnabled();
        Pair<Geometry, CRSDefinition> pair = extractGeometry(value, true, report);
        // handle about annotated geometries
        if (!hasValue && pair != null) {
            String srsName = extractCode(pair.getSecond());
            // write geometry
            writeGeometry(pair.getFirst(), propDef, srsName, report);
        } else {
            boolean hasOnlyNilReason = hasOnlyNilReason(group);
            // write no elements if there is a value or only a nil reason
            boolean writeElements = !hasValue && !hasOnlyNilReason;
            boolean isNil = !writeElements && (!hasValue || value == null);
            // write all children
            writeProperties(group, group.getDefinition(), writeElements, isNil, report);
            // write value
            if (hasValue) {
                writeElementValue(value, propDef);
            } else if (hasOnlyNilReason) {
                // complex element with a nil value -> write xsi:nil if
                // possible
                /*
					 * XXX open question: should xsi:nil be there also if there
					 * are other attributes than nilReason?
					 */
                writeElementValue(null, propDef);
            }
        }
        writer.writeEndElement();
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) Group(eu.esdihumboldt.hale.common.instance.model.Group) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)

Example 19 with Group

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

the class DatabaseHandle method addGroup.

/**
 * Augment a group and add a reference for the database connection. Makes
 * sure child instances or groups also reference the database connection.
 *
 * @param group the group to augment
 * @return the augmented group
 */
public Group addGroup(Group group) {
    if (group == null) {
        return null;
    }
    Group result = new GroupHandle(group);
    addReference(result);
    return result;
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group)

Example 20 with Group

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

the class OInstance method putMetaData.

/**
 * {@inheritDoc}
 *
 * The parameter "Object obj" may not be an ODocument
 */
@Override
public void putMetaData(String key, Object obj) {
    Preconditions.checkArgument(!(obj instanceof ODocument || obj instanceof Instance || obj instanceof Group));
    ODocument metaData;
    if (document.field(FIELD_METADATA) == null) {
        metaData = new ODocument();
        document.field(FIELD_METADATA, metaData);
    } else {
        metaData = (ODocument) document.field(FIELD_METADATA);
    }
    addProperty(new QName(key), obj, metaData);
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

Group (eu.esdihumboldt.hale.common.instance.model.Group)26 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)15 QName (javax.xml.namespace.QName)10 ArrayList (java.util.ArrayList)6 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)5 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)5 SourceNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.SourceNode)4 Pair (eu.esdihumboldt.util.Pair)4 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)3 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)3 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 CellNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.CellNode)2 TargetNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)2 TransformationContext (eu.esdihumboldt.hale.common.align.model.transformation.tree.context.TransformationContext)2 InstanceValidationReport (eu.esdihumboldt.hale.common.instance.extension.validation.report.InstanceValidationReport)2 ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)2 DefinitionGroup (eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)2 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)2 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)2 LinkedList (java.util.LinkedList)2