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