001: /**
002: *
003: */package com.bostechcorp.cbesb.runtime.parser.impl;
004:
005: import org.w3c.dom.Document;
006: import org.w3c.dom.Element;
007: import org.w3c.dom.Node;
008:
009: import com.bostechcorp.cbesb.common.mdl.IMessageDefinition;
010: import com.bostechcorp.cbesb.runtime.parser.ParserException;
011:
012: /**
013: * @author mpreston
014: *
015: */
016: public class Hl7Serializer extends VariantSerializer {
017:
018: /* (non-Javadoc)
019: * @see com.bostechcorp.cbesb.runtime.parser.impl.VariantSerializer#preSerialize(org.w3c.dom.Document, com.bostechcorp.cbesb.common.mdl.IMessageDefinition)
020: */
021: @Override
022: public void preSerialize(Document doc,
023: IMessageDefinition messageDefinition)
024: throws ParserException {
025: char fieldSeparator = 0;
026: char componentSeparator = 0;
027: char subComponentSeparator = 0;
028: char repetitionSeparator = 0;
029: char escapeChar = 0;
030:
031: //Get the special characters from the document
032: Element root = doc.getDocumentElement();
033: if (root == null) {
034: throw new ParserException(
035: "HL7Serializer - Document Element is null.");
036: }
037: String messageName = root.getLocalName();
038: Element MSH = getFirstChildElement(root);
039: if (MSH == null) {
040: throw new ParserException(
041: "HL7Serializer - Root element has no children.");
042: }
043: if (!"MSH".equals(MSH.getLocalName())) {
044: throw new ParserException("HL7Serializer - '" + messageName
045: + "' Document must start with MSH element.");
046: }
047: Element MSH01 = getFirstChildElement(MSH);
048: if (MSH01 != null && "MSH01".equals(MSH01.getLocalName())) {
049: String value = MSH01.getTextContent();
050: if (value != null && value.length() == 1) {
051: fieldSeparator = value.charAt(0);
052: }
053: }
054: Element MSH02 = getNextSiblingElement(MSH01);
055: if (MSH02 != null && "MSH02".equals(MSH02.getLocalName())) {
056: String value = MSH02.getTextContent();
057: if (value != null && value.length() == 4) {
058: componentSeparator = value.charAt(0);
059: repetitionSeparator = value.charAt(1);
060: escapeChar = value.charAt(2);
061: subComponentSeparator = value.charAt(3);
062: }
063: }
064:
065: //Override the special characters in the message definition
066: overrideMsgDefProperty("FieldSeparator", String
067: .valueOf(fieldSeparator));
068: overrideMsgDefProperty("ComponentSeparator", String
069: .valueOf(componentSeparator));
070: overrideMsgDefProperty("SubcomponentSeparator", String
071: .valueOf(subComponentSeparator));
072: overrideMsgDefProperty("RepetitionSeparator", String
073: .valueOf(repetitionSeparator));
074: overrideMsgDefProperty("EscapeCharacter", String
075: .valueOf(escapeChar));
076:
077: }
078:
079: /* (non-Javadoc)
080: * @see com.bostechcorp.cbesb.runtime.parser.impl.VariantSerializer#postSerialize(java.lang.StringBuffer, com.bostechcorp.cbesb.common.mdl.IMessageDefinition)
081: */
082: @Override
083: public void postSerialize(StringBuffer msgData,
084: IMessageDefinition messageDefinition)
085: throws ParserException {
086: //Fix the MSH segment - remove unneeded escape chars
087: msgData.deleteCharAt(10);
088: msgData.deleteCharAt(8);
089: msgData.delete(4, 7);
090:
091: //Remove the escape chars from the MSH09 field
092: //and make sure the correct component separator is used.
093: char fieldSeparator = msgData.charAt(3);
094: char componentSeparator = msgData.charAt(4);
095: char escapeChar = msgData.charAt(6);
096:
097: int index = 0;
098: for (int fieldIndex = 1; fieldIndex < 9; fieldIndex++) {
099: index = msgData.indexOf(String.valueOf(fieldSeparator),
100: index + 1);
101: }
102: int startIndex = index + 1;
103: int endIndex = msgData.indexOf(String.valueOf(fieldSeparator),
104: startIndex);
105: String msh09 = msgData.substring(startIndex, endIndex);
106: StringBuffer buffer = new StringBuffer();
107: for (int i = 0; i < msh09.length(); i++) {
108: if (msh09.charAt(i) != escapeChar) {
109: buffer.append(msh09.charAt(i));
110: } else {
111: //Found escape char. Make sure next
112: //char is the correct component separator
113: i++;
114: buffer.append(componentSeparator);
115: }
116: }
117: msgData.replace(startIndex, endIndex, buffer.toString());
118:
119: }
120:
121: private Element getFirstChildElement(Element elem) {
122: Node child = elem.getFirstChild();
123: while (child != null && !(child instanceof Element)) {
124: child = child.getNextSibling();
125: }
126: return (Element) child;
127: }
128:
129: private Element getNextSiblingElement(Element elem) {
130: Node child = elem.getNextSibling();
131: while (child != null && !(child instanceof Element)) {
132: child = child.getNextSibling();
133: }
134: return (Element) child;
135: }
136: }
|