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