use of eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition 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.GroupPropertyDefinition in project hale by halestudio.
the class PropertyBean method findChild.
/**
* The function to look for a child as ChildDefinition or as Group
*
* @param parent the starting point to traverse from
* @param childName the name of the parent's child
* @return a pair of child and a list with the full path from parent to the
* child or <code>null</code> if no such child was found
*/
public static Pair<ChildDefinition<?>, List<ChildDefinition<?>>> findChild(DefinitionGroup parent, QName childName) {
ChildDefinition<?> child = parent.getChild(childName);
if (child == null) {
// if the namespace is not null
if (childName.getNamespaceURI().equals(XMLConstants.NULL_NS_URI)) {
// get all children and iterate over them
Collection<? extends ChildDefinition<?>> children = DefinitionUtil.getAllChildren(parent);
for (ChildDefinition<?> _child : children) {
// different namespace we overwrite child
if (_child.getName().getLocalPart().equals(childName.getLocalPart())) {
child = _child;
break;
}
}
}
}
if (child != null) {
return new Pair<ChildDefinition<?>, List<ChildDefinition<?>>>(child, null);
}
Collection<? extends ChildDefinition<?>> children = DefinitionUtil.getAllChildren(parent);
for (ChildDefinition<?> groupChild : children) {
if (groupChild.asGroup() != null) {
GroupPropertyDefinition temp = groupChild.asGroup();
if (findChild(temp, childName) != null) {
Pair<ChildDefinition<?>, List<ChildDefinition<?>>> recTemp = findChild(temp, childName);
if (recTemp.getSecond() == null) {
List<ChildDefinition<?>> second = new ArrayList<ChildDefinition<?>>();
second.add(temp);
ChildDefinition<?> first = recTemp.getFirst();
return new Pair<ChildDefinition<?>, List<ChildDefinition<?>>>(first, second);
} else {
recTemp.getSecond().add(0, temp);
}
}
}
}
return null;
}
Aggregations