this.untSegment = untSegment;
this.hierarchyChangeListener = hierarchyChangeListener;
}
public void process(InterchangeContext interchangeContext) throws IOException, SAXException {
BufferedSegmentReader segmentReader = interchangeContext.getSegmentReader();
MappingsRegistry registry = interchangeContext.getRegistry();
// Move to the end of the UNH segment and map it's fields..
segmentReader.moveToNextSegment(false);
// Select the mapping model to use for this message...
String[] fields = segmentReader.getCurrentSegmentFields();
String messageName = fields[2];
EdifactModel mappingModel = registry.getMappingModel(messageName, segmentReader.getDelimiters());
Edimap ediMap = mappingModel.getEdimap();
Description description = ediMap.getDescription();
AttributesImpl attrs = new AttributesImpl();
String namespace = description.getNamespace();
String commonNS = null;
String messageNSPrefix = null;
if(namespace != null && !namespace.equals(XMLConstants.NULL_NS_URI)) {
int nameComponentIndex = namespace.lastIndexOf(":");
if(nameComponentIndex != -1) {
commonNS = namespace.substring(0, nameComponentIndex) + ":common";
messageNSPrefix = description.getName().toLowerCase();
attrs.addAttribute(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "c", "xmlns:c", "CDATA", commonNS);
attrs.addAttribute(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, messageNSPrefix, "xmlns:" + messageNSPrefix, "CDATA", namespace);
}
}
interchangeContext.getControlSegmentParser().startElement(InterchangeContext.INTERCHANGE_MESSAGE_BLOCK_ELEMENT_NAME, unhSegment.getNamespace(), true, attrs);
interchangeContext.mapControlSegment(unhSegment, false);
// Map the message... stopping at the UNT segment...
try {
EDIParser parser = interchangeContext.newParser(mappingModel);
segmentReader.setSegmentListener(untSegmentListener);
if(hierarchyChangeListener != null) {
hierarchyChangeListener.attachXMLReader(parser);
} else if (!interchangeContext.isContainerManagedNamespaceStack()) {
interchangeContext.getNamespaceDeclarationStack().pushReader(parser);
}
parser.parse();
} finally {
segmentReader.setSegmentListener(null);
if(hierarchyChangeListener != null) {
hierarchyChangeListener.detachXMLReader();
} else if (!interchangeContext.isContainerManagedNamespaceStack()) {
interchangeContext.getNamespaceDeclarationStack().popReader();
}
}
// We're at the end of the UNT segment now. See the UNTSegmentListener below.
// Map the UNT segment...
interchangeContext.mapControlSegment(untSegment, true);
segmentReader.getSegmentBuffer().setLength(0);
interchangeContext.getControlSegmentParser().endElement(InterchangeContext.INTERCHANGE_MESSAGE_BLOCK_ELEMENT_NAME, unhSegment.getNamespace(), true);
}