Search in sources :

Example 11 with Cardinality

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.

the class XmlSchemaReaderTest method testRead_definitive_groups.

/**
 * Test reading a simple XML schema containing groups and group references.
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_definitive_groups() throws Exception {
    URI location = getClass().getResource("/testdata/definitive/groups.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    // ShirtType
    TypeDefinition shirtType = schema.getType(new QName("ShirtType"));
    assertNotNull(shirtType);
    assertEquals(5, shirtType.getChildren().size());
    Iterator<? extends ChildDefinition<?>> it = shirtType.getChildren().iterator();
    // ProductPropertyGroup
    GroupPropertyDefinition prodGroup = it.next().asGroup();
    // cardinality
    Cardinality cc = prodGroup.getConstraint(Cardinality.class);
    assertEquals(0, cc.getMinOccurs());
    assertEquals(1, cc.getMaxOccurs());
    // name
    assertEquals("ProductPropertyGroup", prodGroup.getName().getLocalPart());
    assertEquals(4, prodGroup.getDeclaredChildren().size());
    Iterator<? extends ChildDefinition<?>> itProd = prodGroup.getDeclaredChildren().iterator();
    // not there any more because it is flattened away
    // // DescriptionGroup
    // GroupPropertyDefinition descGroup = itProd.next().asGroup();
    // assertNotNull(descGroup);
    // // cardinality
    // cc = descGroup.getConstraint(Cardinality.class);
    // assertEquals(1, cc.getMinOccurs());
    // assertEquals(1, cc.getMaxOccurs());
    // 
    // assertEquals(2, descGroup.getDeclaredChildren().size());
    // Iterator<? extends ChildDefinition<?>> itDesc = descGroup.getDeclaredChildren().iterator();
    // description
    PropertyDefinition description = itProd.next().asProperty();
    assertNotNull(description);
    assertEquals("description", description.getName().getLocalPart());
    // comment
    PropertyDefinition comment = itProd.next().asProperty();
    assertNotNull(comment);
    assertEquals("comment", comment.getName().getLocalPart());
    // number
    PropertyDefinition number = itProd.next().asProperty();
    assertNotNull(number);
    assertEquals("number", number.getName().getLocalPart());
    // name
    PropertyDefinition name = itProd.next().asProperty();
    assertNotNull(name);
    assertEquals("name", name.getName().getLocalPart());
    // size
    PropertyDefinition size = it.next().asProperty();
    assertNotNull(size);
    assertEquals("size", size.getName().getLocalPart());
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) QName(javax.xml.namespace.QName) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) URI(java.net.URI) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Example 12 with Cardinality

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.

the class XmlSchemaReaderTest method testRead_definitive_sequencegroup.

/**
 * Test reading a simple XML schema with sequences that have to be grouped.
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_definitive_sequencegroup() throws Exception {
    URI location = getClass().getResource("/testdata/definitive/sequencegroup.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    // ItemsType
    TypeDefinition itemsType = schema.getType(new QName("ItemsType"));
    assertNotNull(itemsType);
    assertEquals(1, itemsType.getChildren().size());
    // sequence group
    GroupPropertyDefinition sequence = itemsType.getChildren().iterator().next().asGroup();
    assertNotNull(sequence);
    // cardinality
    Cardinality cc = sequence.getConstraint(Cardinality.class);
    assertEquals(1, cc.getMinOccurs());
    assertEquals(Cardinality.UNBOUNDED, cc.getMaxOccurs());
    // choice flag (not a choice)
    assertFalse(sequence.getConstraint(ChoiceFlag.class).isEnabled());
    Iterator<? extends ChildDefinition<?>> it = sequence.getDeclaredChildren().iterator();
    // name
    PropertyDefinition name = it.next().asProperty();
    assertNotNull(name);
    assertEquals("name", name.getName().getLocalPart());
    // id
    PropertyDefinition id = it.next().asProperty();
    assertNotNull(id);
    assertEquals("id", id.getName().getLocalPart());
    // choice
    GroupPropertyDefinition choice = it.next().asGroup();
    assertNotNull(choice);
    // cardinality
    cc = choice.getConstraint(Cardinality.class);
    assertEquals(1, cc.getMinOccurs());
    assertEquals(1, cc.getMaxOccurs());
    // choice flag
    assertTrue(choice.getConstraint(ChoiceFlag.class).isEnabled());
    it = choice.getDeclaredChildren().iterator();
    // choice sequence
    GroupPropertyDefinition seqGroup = it.next().asGroup();
    assertNotNull(seqGroup);
    // choice flag (not a choice)
    assertFalse(seqGroup.getConstraint(ChoiceFlag.class).isEnabled());
    // sequence elements
    // one
    PropertyDefinition one = seqGroup.getChild(new QName("one")).asProperty();
    assertNotNull(one);
    // two
    PropertyDefinition two = seqGroup.getChild(new QName("two")).asProperty();
    assertNotNull(two);
    // choice element
    PropertyDefinition single = it.next().asProperty();
    assertNotNull(single);
    assertEquals("single", single.getName().getLocalPart());
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) QName(javax.xml.namespace.QName) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) URI(java.net.URI) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Example 13 with Cardinality

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality 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 14 with Cardinality

use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.

the class InstanceBuilder method populateGroup.

/**
 * Populates a group with values from its children.
 *
 * @param group the group
 * @param node the node associated with the group
 * @param typeLog the type transformation log
 * @return if any values were added to the group
 */
private boolean populateGroup(MutableGroup group, GroupNode node, TransformationLog typeLog) {
    boolean anyValue = false;
    for (TargetNode child : node.getChildren(true)) {
        Object value = getValue(child, typeLog);
        if (value != NoObject.NONE) {
            // add value to group
            if (value instanceof MultiValue) {
                MultiValue multiValue = (MultiValue) value;
                int toAdd = multiValue.size();
                // check cardinality
                Cardinality card = DefinitionUtil.getCardinality(child.getDefinition());
                if (card.getMaxOccurs() != Cardinality.UNBOUNDED && card.getMaxOccurs() < toAdd) {
                    toAdd = (int) card.getMaxOccurs();
                    typeLog.warn(typeLog.createMessage("Too many values present for " + child.getDefinition().getDisplayName() + ". Limiting to match cardinality.", null));
                }
                // add properties
                for (int i = 0; i < toAdd; i++) {
                    group.addProperty(child.getDefinition().getName(), multiValue.get(i));
                }
                if (toAdd > 0) {
                    anyValue = true;
                }
            } else {
                group.addProperty(child.getDefinition().getName(), value);
                anyValue = true;
            }
        }
    }
    return anyValue;
}
Also used : TargetNode(eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) MultiValue(eu.esdihumboldt.cst.MultiValue)

Aggregations

Cardinality (eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)14 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)7 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)6 QName (javax.xml.namespace.QName)5 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)4 XmlIndex (eu.esdihumboldt.hale.io.xsd.model.XmlIndex)4 URI (java.net.URI)4 Test (org.junit.Test)4 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)2 Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)2 Population (eu.esdihumboldt.hale.ui.common.service.population.Population)2 PopulationService (eu.esdihumboldt.hale.ui.common.service.population.PopulationService)2 MultiValue (eu.esdihumboldt.cst.MultiValue)1 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)1 TargetNode (eu.esdihumboldt.hale.common.align.model.transformation.tree.TargetNode)1 DataSet (eu.esdihumboldt.hale.common.instance.model.DataSet)1 Group (eu.esdihumboldt.hale.common.instance.model.Group)1 Classification (eu.esdihumboldt.hale.common.schema.Classification)1 ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)1