Search in sources :

Example 6 with GroupPropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition in project hale by halestudio.

the class JacksonMapper method skipChoice.

/**
 * Handles skipping choice groups.
 *
 * @param propertyName the start property name
 * @param obj the object to inspect
 * @param reporter the reporter
 * @return a pair of property name and value to use
 */
private Pair<String, Object> skipChoice(String propertyName, Object obj, IOReporter reporter) {
    if (obj instanceof Group) {
        Group group = (Group) obj;
        // For choices search for the (only!) child and skip the choice.
        if (group.getDefinition() instanceof GroupPropertyDefinition) {
            if (((GroupPropertyDefinition) group.getDefinition()).getConstraint(ChoiceFlag.class).isEnabled()) {
                Iterator<QName> childPropertyNames = group.getPropertyNames().iterator();
                if (!childPropertyNames.hasNext()) {
                    reporter.warn(new IOMessageImpl("Found an empty choice.", null));
                    return null;
                }
                QName childPropertyName = childPropertyNames.next();
                Object[] values = group.getProperty(childPropertyName);
                Object value = values[0];
                if (childPropertyNames.hasNext() || values.length > 1)
                    reporter.warn(new IOMessageImpl("Found a choice with multiple children. Using first.", null));
                // delegate to only value
                return skipChoice(childPropertyName.getLocalPart(), value, reporter);
            }
        }
    }
    return new Pair<>(propertyName, obj);
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) Group(eu.esdihumboldt.hale.common.instance.model.Group) QName(javax.xml.namespace.QName) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ChoiceFlag(eu.esdihumboldt.hale.common.schema.model.constraint.property.ChoiceFlag) Pair(eu.esdihumboldt.util.Pair)

Example 7 with GroupPropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition 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 8 with GroupPropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition 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 9 with GroupPropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition in project hale by halestudio.

the class GroupPath method createChildGroups.

/**
 * Create groups for the children in the path (which are only represented as
 * definitions). May only be called if the path is valid. This will also
 * update the path to include the groups instead of the definitions.
 *
 * @return the list of created groups
 *
 * @see #isValid()
 */
protected List<MutableGroup> createChildGroups() {
    MutableGroup parent = parents.get(parents.size() - 1);
    final List<MutableGroup> result = new ArrayList<MutableGroup>();
    for (DefinitionGroup child : children) {
        checkState(child instanceof GroupPropertyDefinition);
        // create group
        MutableGroup group = new DefaultGroup(child);
        // add to parent
        QName propertyName = ((GroupPropertyDefinition) child).getName();
        parent.addProperty(propertyName, group);
        // add to result
        result.add(group);
        // prepare for next iteration
        parent = group;
    }
    // update children and parents
    children.clear();
    parents.addAll(result);
    return result;
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) DefaultGroup(eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup) MutableGroup(eu.esdihumboldt.hale.common.instance.model.MutableGroup) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)

Example 10 with GroupPropertyDefinition

use of eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition in project hale by halestudio.

the class GroupPath method isValid.

/**
 * Determines if the group path in this configuration is valid in respect to
 * the creation of new groups based on the contained definition groups.
 *
 * @return if the path is valid
 */
public boolean isValid() {
    if (children == null || children.isEmpty()) {
        // parents is assumed to be valid
        return true;
    }
    MutableGroup parentGroup = parents.get(parents.size() - 1);
    DefinitionGroup parentDef = parentGroup.getDefinition();
    for (DefinitionGroup child : children) {
        if (child instanceof GroupPropertyDefinition) {
            if (!GroupUtil.allowAdd(parentGroup, parentDef, ((GroupPropertyDefinition) child).getName())) {
                return false;
            }
        } else {
            // XXX TypeDefinitions not supported in path
            return false;
        }
        // prepare next iteration
        parentGroup = null;
        parentDef = child;
    }
    return true;
}
Also used : GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) MutableGroup(eu.esdihumboldt.hale.common.instance.model.MutableGroup) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)

Aggregations

GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)12 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)7 QName (javax.xml.namespace.QName)7 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)6 Cardinality (eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)6 DefinitionGroup (eu.esdihumboldt.hale.common.schema.model.DefinitionGroup)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 MutableGroup (eu.esdihumboldt.hale.common.instance.model.MutableGroup)3 Pair (eu.esdihumboldt.util.Pair)3 ArrayList (java.util.ArrayList)3 Group (eu.esdihumboldt.hale.common.instance.model.Group)2 List (java.util.List)2 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 DataSet (eu.esdihumboldt.hale.common.instance.model.DataSet)1 DefaultGroup (eu.esdihumboldt.hale.common.instance.model.impl.DefaultGroup)1 Classification (eu.esdihumboldt.hale.common.schema.Classification)1 ChildDefinition (eu.esdihumboldt.hale.common.schema.model.ChildDefinition)1