/** * Formats a Message object into an HL7 message string using the given encoding. * * @throws HL7Exception if the data fields in the message do not permit encoding (e.g. required * fields are null) * @throws EncodingNotSupportedException if the requested encoding is not supported by this * parser. */ protected String doEncode(Message source, String encoding) throws HL7Exception { if (!encoding.equals("XML")) throw new EncodingNotSupportedException("XMLParser supports only XML encoding"); return encode(source); }
/** * Populates the given Element with data from the given Type, by inserting Elements * corresponding to the Type's components and values. Returns true if the given type contains a * value (i.e. for Primitives, if getValue() doesn't return null, and for Composites, if at * least one underlying Primitive doesn't return null). */ private boolean encode(Type datatypeObject, Element datatypeElement) throws DataTypeException { boolean hasData = false; if (datatypeObject instanceof Varies) { hasData = encodeVaries((Varies) datatypeObject, datatypeElement); } else if (datatypeObject instanceof Primitive) { hasData = encodePrimitive((Primitive) datatypeObject, datatypeElement); } else if (datatypeObject instanceof Composite) { hasData = encodeComposite((Composite) datatypeObject, datatypeElement); } return hasData; }
/** * Returns a String representing the encoding of the given message, if the encoding is * recognized. For example if the given message appears to be encoded using HL7 2.x XML rules * then "XML" would be returned. If the encoding is not recognized then null is returned. That * this method returns a specific encoding does not guarantee that the message is correctly * encoded (e.g. well formed XML) - just that it is not encoded using any other encoding than * the one returned. Returns null if the encoding is not recognized. */ public String getEncoding(String message) { return EncodingDetector.isXmlEncoded(message) ? getDefaultEncoding() : null; }
/** * <p> * Returns a minimal amount of data from a message string, including only the data needed to * send a response to the remote system. This includes the following fields: * <ul> * <li>field separator</li> * <li>encoding characters</li> * <li>processing ID</li> * <li>message control ID</li> * </ul> * This method is intended for use when there is an error parsing a message, (so the Message * object is unavailable) but an error message must be sent back to the remote system including * some of the information in the inbound message. This method parses only that required * information, hopefully avoiding the condition that caused the original error. * </p> */ public Segment getCriticalResponseData(String message) throws HL7Exception { String version = getVersion(message); Segment criticalData = Parser.makeControlMSH(version, getFactory()); Terser.set(criticalData, 1, 0, 1, 1, parseLeaf(message, "MSH.1", 0)); Terser.set(criticalData, 2, 0, 1, 1, parseLeaf(message, "MSH.2", 0)); Terser.set(criticalData, 10, 0, 1, 1, parseLeaf(message, "MSH.10", 0)); String procID = parseLeaf(message, "MSH.11", 0); if (procID == null || procID.length() == 0) { procID = parseLeaf(message, "PT.1", message.indexOf("MSH.11")); // this field is a composite in later versions } Terser.set(criticalData, 11, 0, 1, 1, procID); return criticalData; }
assertNamespaceURI(all.item(i).getNamespaceURI()); done.add(elementName); String fieldNumString = elementName.substring(index + 1); int fieldNum = Integer.parseInt(fieldNumString); parseReps(segmentObject, segmentElement, elementName, fieldNum); } else { log.debug("Child of segment {} doesn't look like a field {}", FixFieldDataType.fixOBX5(segmentObject, getFactory(), getHapiContext().getParserConfiguration()); FixFieldDataType.fixMFE4(segmentObject, getFactory(), getHapiContext().getParserConfiguration());
/** * Encodes a Composite in XML by looping through it's components, creating new children for each * of them (with the appropriate names) and populating them by calling encode(Type, Element) * using these children. Returns true if at least one component contains a value. */ private boolean encodeComposite(Composite datatypeObject, Element datatypeElement) throws DataTypeException { Type[] components = datatypeObject.getComponents(); boolean hasValue = false; for (int i = 0; i < components.length; i++) { String name = makeElementName(datatypeObject, i + 1); Element newNode = datatypeElement.getOwnerDocument().createElement(name); boolean componentHasValue = encode(components[i], newNode); if (componentHasValue) { try { datatypeElement.appendChild(newNode); } catch (DOMException e) { throw new DataTypeException("DOMException encoding Composite: ", e); } hasValue = true; } } return hasValue; }
String value = child.getNodeValue(); if (value != null && value.length() > 0) { if (keepAsOriginal(child.getParentNode())) { builder.append(value); } else { builder.append(removeWhitespace(value)); assertNamespaceURI(child.getNamespaceURI()); EncodingCharacters ec = EncodingCharacters.getInstance(datatypeObject .getMessage());
if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { Element nextElement = (Element) children.item(i); assertNamespaceURI(nextElement.getNamespaceURI()); String localName = nextElement.getLocalName(); int dotIndex = localName.indexOf("."); parse(nextComponent, nextElement); compNum++; Type[] children = datatypeObject.getComponents(); for (int i = 0; i < children.length; i++) { NodeList matchingElements = datatypeElement.getElementsByTagNameNS(NS, makeElementName( datatypeObject, i + 1)); if (matchingElements.getLength() > 0) { parse(children[i], (Element) matchingElements.item(0)); do { foundExtraComponent = false; NodeList matchingElements = datatypeElement.getElementsByTagNameNS(NS, makeElementName(datatypeObject, children.length + nextExtraCmpIndex + 1)); if (matchingElements.getLength() > 0) { parse(datatypeObject.getExtraComponents().getComponent(nextExtraCmpIndex), (Element) matchingElements.item(0)); foundExtraComponent = true;
/** * Formats a Message object into an HL7 message string using this parser's default encoding (XML * encoding). This method calls the abstract method <code>encodeDocument(...)</code> in order to * obtain XML Document object representation of the Message, then serializes it to a String. * * @throws HL7Exception if the data fields in the message do not permit encoding (e.g. required * fields are null) */ protected String doEncode(Message source) throws HL7Exception { if (source instanceof GenericMessage) { throw new HL7Exception( "Can't XML-encode a GenericMessage. Message must have a recognized structure."); } Document doc = encodeDocument(source); // Element documentElement = doc.getDocumentElement(); // if (!documentElement.hasAttribute("xmlns")) // documentElement.setAttribute("xmlns", "urn:hl7-org:v2xml"); try { return XMLUtils.serialize(doc, getParserConfiguration().isPrettyPrintWhenEncodingXml()); } catch (Exception e) { throw new HL7Exception("Exception serializing XML document to string", e); } }
/** * Formats a Message object into an HL7 message string using the given encoding. * * @throws HL7Exception if the data fields in the message do not permit encoding (e.g. required * fields are null) * @throws EncodingNotSupportedException if the requested encoding is not supported by this * parser. */ protected String doEncode(Message source, String encoding) throws HL7Exception, EncodingNotSupportedException { String ret = null; if (encoding == null) encoding = ""; // prevent null pointer exception if (encoding.equalsIgnoreCase("VB")) { ret = pipeParser.doEncode(source); } else if (encoding.equalsIgnoreCase("XML")) { ret = xmlParser.doEncode(source); } else { throw new EncodingNotSupportedException("The encoding " + encoding + " is not supported by " + this.getClass().getName()); } return ret; }
outParser = xp; else if (xp.getEncoding(messString) != null) { inParser = xp; outParser = pp;
assertNamespaceURI(all.item(i).getNamespaceURI()); done.add(elementName); String fieldNumString = elementName.substring(index + 1); int fieldNum = Integer.parseInt(fieldNumString); parseReps(segmentObject, segmentElement, elementName, fieldNum); } else { log.debug("Child of segment {} doesn't look like a field {}", FixFieldDataType.fixOBX5(segmentObject, getFactory(), getHapiContext().getParserConfiguration()); FixFieldDataType.fixMFE4(segmentObject, getFactory(), getHapiContext().getParserConfiguration());
/** * Encodes a Composite in XML by looping through it's components, creating new children for each * of them (with the appropriate names) and populating them by calling encode(Type, Element) * using these children. Returns true if at least one component contains a value. */ private boolean encodeComposite(Composite datatypeObject, Element datatypeElement) throws DataTypeException { Type[] components = datatypeObject.getComponents(); boolean hasValue = false; for (int i = 0; i < components.length; i++) { String name = makeElementName(datatypeObject, i + 1); Element newNode = datatypeElement.getOwnerDocument().createElement(name); boolean componentHasValue = encode(components[i], newNode); if (componentHasValue) { try { datatypeElement.appendChild(newNode); } catch (DOMException e) { throw new DataTypeException("DOMException encoding Composite: ", e); } hasValue = true; } } return hasValue; }
/** * <p> * Returns a minimal amount of data from a message string, including only the data needed to * send a response to the remote system. This includes the following fields: * <ul> * <li>field separator</li> * <li>encoding characters</li> * <li>processing ID</li> * <li>message control ID</li> * </ul> * This method is intended for use when there is an error parsing a message, (so the Message * object is unavailable) but an error message must be sent back to the remote system including * some of the information in the inbound message. This method parses only that required * information, hopefully avoiding the condition that caused the original error. * </p> */ public Segment getCriticalResponseData(String message) throws HL7Exception { String version = getVersion(message); Segment criticalData = Parser.makeControlMSH(version, getFactory()); Terser.set(criticalData, 1, 0, 1, 1, parseLeaf(message, "MSH.1", 0)); Terser.set(criticalData, 2, 0, 1, 1, parseLeaf(message, "MSH.2", 0)); Terser.set(criticalData, 10, 0, 1, 1, parseLeaf(message, "MSH.10", 0)); String procID = parseLeaf(message, "MSH.11", 0); if (procID == null || procID.length() == 0) { procID = parseLeaf(message, "PT.1", message.indexOf("MSH.11")); // this field is a composite in later versions } Terser.set(criticalData, 11, 0, 1, 1, procID); return criticalData; }
String value = child.getNodeValue(); if (value != null && value.length() > 0) { if (keepAsOriginal(child.getParentNode())) { builder.append(value); } else { builder.append(removeWhitespace(value)); assertNamespaceURI(child.getNamespaceURI()); EncodingCharacters ec = EncodingCharacters.getInstance(datatypeObject .getMessage());
if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { Element nextElement = (Element) children.item(i); assertNamespaceURI(nextElement.getNamespaceURI()); String localName = nextElement.getLocalName(); int dotIndex = localName.indexOf("."); parse(nextComponent, nextElement); compNum++; Type[] children = datatypeObject.getComponents(); for (int i = 0; i < children.length; i++) { NodeList matchingElements = datatypeElement.getElementsByTagNameNS(NS, makeElementName( datatypeObject, i + 1)); if (matchingElements.getLength() > 0) { parse(children[i], (Element) matchingElements.item(0)); do { foundExtraComponent = false; NodeList matchingElements = datatypeElement.getElementsByTagNameNS(NS, makeElementName(datatypeObject, children.length + nextExtraCmpIndex + 1)); if (matchingElements.getLength() > 0) { parse(datatypeObject.getExtraComponents().getComponent(nextExtraCmpIndex), (Element) matchingElements.item(0)); foundExtraComponent = true;
/** * Formats a Message object into an HL7 message string using this parser's default encoding (XML * encoding). This method calls the abstract method <code>encodeDocument(...)</code> in order to * obtain XML Document object representation of the Message, then serializes it to a String. * * @throws HL7Exception if the data fields in the message do not permit encoding (e.g. required * fields are null) */ protected String doEncode(Message source) throws HL7Exception { if (source instanceof GenericMessage) { throw new HL7Exception( "Can't XML-encode a GenericMessage. Message must have a recognized structure."); } Document doc = encodeDocument(source); // Element documentElement = doc.getDocumentElement(); // if (!documentElement.hasAttribute("xmlns")) // documentElement.setAttribute("xmlns", "urn:hl7-org:v2xml"); try { return XMLUtils.serialize(doc, getParserConfiguration().isPrettyPrintWhenEncodingXml()); } catch (Exception e) { throw new HL7Exception("Exception serializing XML document to string", e); } }
/** * Formats a Message object into an HL7 message string using the given encoding. * * @throws HL7Exception if the data fields in the message do not permit encoding (e.g. required * fields are null) * @throws EncodingNotSupportedException if the requested encoding is not supported by this * parser. */ protected String doEncode(Message source, String encoding) throws HL7Exception, EncodingNotSupportedException { String ret = null; if (encoding == null) encoding = ""; // prevent null pointer exception if (encoding.equalsIgnoreCase("VB")) { ret = pipeParser.doEncode(source); } else if (encoding.equalsIgnoreCase("XML")) { ret = xmlParser.doEncode(source); } else { throw new EncodingNotSupportedException("The encoding " + encoding + " is not supported by " + this.getClass().getName()); } return ret; }
outParser = xp; else if (xp.getEncoding(messString) != null) { inParser = xp; outParser = pp;
int n = segmentObject.numFields(); for (int i = 1; i <= n; i++) { String name = makeElementName(segmentObject, i); Type[] reps = segmentObject.getField(i); for (Type rep : reps) { Element newNode = segmentElement.getOwnerDocument().createElement(name); boolean componentHasValue = encode(rep, newNode); if (componentHasValue) { try {