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);
}
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());
}
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());
}
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;
}
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;
}
Aggregations