Skip to content

Commit

Permalink
use xsi:nil for nillable complex properties w/ only nilReason
Browse files Browse the repository at this point in the history
Before, the presence of nilReason would result in xsi:nil not
being written.

Change-Id: I8251da27f6644ac526d0df43f21c3f5e3abe276f
  • Loading branch information
stempler committed Oct 4, 2015
1 parent f2c9b8d commit fe4ced8
Showing 1 changed file with 57 additions and 2 deletions.
Expand Up @@ -1068,19 +1068,74 @@ private void writeElement(Object value, PropertyDefinition propDef, IOReporter r
writeGeometry(pair.getFirst(), propDef, srsName, report);
}
else {
// write all children (no elements if there is a value)
writeProperties(group, group.getDefinition(), !hasValue, report);
boolean hasOnlyNilReason = hasOnlyNilReason(group);

// write no elements if there is a value or only a nil reason
boolean writeElements = !hasValue && !hasOnlyNilReason;

// write all children
writeProperties(group, group.getDefinition(), writeElements, 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();
}
}

/**
* Determines if a group has as its only property the nilReason attribute.
*
* @param group the group to test
* @return <code>true</code> if the group has the nilReason attribute and no
* other children, or no children at all, <code>false</code>
* otherwise
*/
private boolean hasOnlyNilReason(Group group) {
int count = 0;
QName nilReasonName = null;
for (QName name : group.getPropertyNames()) {
if (count > 0)
// more than one property
return false;
if (!name.getLocalPart().equals("nilReason"))
// a property different from nilReason
return false;
nilReasonName = name;
count++;
}

if (nilReasonName != null) {
// make sure it is an attribute
DefinitionGroup parent = group.getDefinition();
ChildDefinition<?> child = parent.getChild(nilReasonName);
if (child.asProperty() == null) {
// is a group
return false;
}
if (!child.asProperty().getConstraint(XmlAttributeFlag.class).isEnabled()) {
// not an attribute
return false;
}
}

return true;
}

/**
* Write an element value, either as element content or as <code>nil</code>.
*
Expand Down

0 comments on commit fe4ced8

Please sign in to comment.