private void addNonStandardSegmentAtCurrentPosition() throws Error {
log.debug("Creating non standard segment {} on group: {}",
myDirection, getCurrentPosition().getStructureDefinition().getParent().getName());
List<Position> parentDefinitionPath;
Group parentStructure;
switch (myMessage.getParser().getParserConfiguration().getUnexpectedSegmentBehaviour()) {
case ADD_INLINE:
default:
parentDefinitionPath = new ArrayList<Position>(myCurrentDefinitionPath.subList(0, myCurrentDefinitionPath.size() - 1));
parentStructure = (Group) navigateToStructure(parentDefinitionPath);
break;
case DROP_TO_ROOT:
parentDefinitionPath = new ArrayList<Position>(myCurrentDefinitionPath.subList(0, 1));
parentStructure = myMessage;
myCurrentDefinitionPath = myCurrentDefinitionPath.subList(0, 2);
break;
case THROW_HL7_EXCEPTION:
throw new Error(new HL7Exception("Found unknown segment: " + myDirection));
}
// Current position within parent
Position currentPosition = getCurrentPosition();
String nameAsItAppearsInParent = currentPosition.getStructureDefinition().getNameAsItAppearsInParent();
int index = Arrays.asList(parentStructure.getNames()).indexOf(nameAsItAppearsInParent) + 1;
String newSegmentName;
// Check if the structure already has a non-standard segment in the appropriate
// position
String[] currentNames = parentStructure.getNames();
if (index < currentNames.length && currentNames[index].startsWith(myDirection)) {
newSegmentName = currentNames[index];
} else {
try {
newSegmentName = parentStructure.addNonstandardSegment(myDirection, index);
} catch (HL7Exception e) {
throw new Error("Unable to add nonstandard segment " + myDirection + ": ", e);
}
}