001/** 002 * The contents of this file are subject to the Mozilla Public License Version 1.1 003 * (the "License"); you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005 * Software distributed under the License is distributed on an "AS IS" basis, 006 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007 * specific language governing rights and limitations under the License. 008 * 009 * The Original Code is "PipeParser.java". Description: 010 * "An implementation of Parser that supports traditionally encoded (i.e" 011 * 012 * The Initial Developer of the Original Code is University Health Network. Copyright (C) 013 * 2001. All Rights Reserved. 014 * 015 * Contributor(s): Kenneth Beaton. 016 * 017 * Alternatively, the contents of this file may be used under the terms of the 018 * GNU General Public License (the "GPL"), in which case the provisions of the GPL are 019 * applicable instead of those above. If you wish to allow use of your version of this 020 * file only under the terms of the GPL and not to allow others to use your version 021 * of this file under the MPL, indicate your decision by deleting the provisions above 022 * and replace them with the notice and other provisions required by the GPL License. 023 * If you do not delete the provisions above, a recipient may use your version of 024 * this file under either the MPL or the GPL. 025 * 026 */ 027 028package ca.uhn.hl7v2.parser; 029 030import java.util.ArrayList; 031import java.util.Arrays; 032import java.util.HashMap; 033import java.util.List; 034import java.util.Map; 035import java.util.Set; 036import java.util.StringTokenizer; 037 038import ca.uhn.hl7v2.validation.ValidationContext; 039import org.slf4j.Logger; 040import org.slf4j.LoggerFactory; 041 042import ca.uhn.hl7v2.DefaultHapiContext; 043import ca.uhn.hl7v2.ErrorCode; 044import ca.uhn.hl7v2.HL7Exception; 045import ca.uhn.hl7v2.HapiContext; 046import ca.uhn.hl7v2.Version; 047import ca.uhn.hl7v2.model.AbstractSuperMessage; 048import ca.uhn.hl7v2.model.DoNotCacheStructure; 049import ca.uhn.hl7v2.model.Group; 050import ca.uhn.hl7v2.model.Message; 051import ca.uhn.hl7v2.model.Primitive; 052import ca.uhn.hl7v2.model.Segment; 053import ca.uhn.hl7v2.model.Structure; 054import ca.uhn.hl7v2.model.SuperStructure; 055import ca.uhn.hl7v2.model.Type; 056import ca.uhn.hl7v2.model.Varies; 057import ca.uhn.hl7v2.util.ReflectionUtil; 058import ca.uhn.hl7v2.util.Terser; 059import ca.uhn.hl7v2.validation.impl.NoValidation; 060import ca.uhn.hl7v2.validation.impl.ValidationContextFactory; 061 062/** 063 * An implementation of Parser that supports traditionally encoded (ie delimited 064 * with characters like |, ^, and ~) HL7 messages. Unexpected segments and 065 * fields are parsed into generic elements that are added to the message. 066 * 067 * @see ParserConfiguration for configuration options which may affect parser encoding and decoding behaviour 068 * @author Bryan Tripp (bryan_tripp@sourceforge.net) 069 */ 070public class PipeParser extends Parser { 071 072 private static final Logger log = LoggerFactory.getLogger(PipeParser.class); 073 074 /** 075 * The HL7 ER7 segment delimiter (see section 2.8 of spec) 076 */ 077 final static String SEGMENT_DELIMITER = "\r"; 078 079 private final HashMap<Class<? extends Message>, HashMap<String, StructureDefinition>> myStructureDefinitions = new HashMap<Class<? extends Message>, HashMap<String, StructureDefinition>>(); 080 081 /** 082 * System property key. If value is "true", legacy mode will default to true 083 * 084 * @see #isLegacyMode() 085 * @deprecated This will be removed in HAPI 3.0 086 */ 087 public static final String DEFAULT_LEGACY_MODE_PROPERTY = "ca.uhn.hl7v2.parser.PipeParser.default_legacy_mode"; 088 089 private Boolean myLegacyMode = null; 090 091 public PipeParser() { 092 super(); 093 } 094 095 /** 096 * @param context 097 * the context containing all configuration items to be used 098 */ 099 public PipeParser(HapiContext context) { 100 super(context); 101 } 102 103 /** 104 * Creates a new PipeParser 105 * 106 * @param theFactory 107 * custom factory to use for model class lookup 108 */ 109 public PipeParser(ModelClassFactory theFactory) { 110 super(theFactory); 111 } 112 113 @Override 114 public void setValidationContext(ValidationContext context) { 115 super.setValidationContext(context); 116 } 117 118 /** 119 * Returns a String representing the encoding of the given message, if the 120 * encoding is recognized. For example if the given message appears to be 121 * encoded using HL7 2.x XML rules then "XML" would be returned. If the 122 * encoding is not recognized then null is returned. That this method 123 * returns a specific encoding does not guarantee that the message is 124 * correctly encoded (e.g. well formed XML) - just that it is not encoded 125 * using any other encoding than the one returned. 126 */ 127 public String getEncoding(String message) { 128 return EncodingDetector.isEr7Encoded(message) ? getDefaultEncoding() : null; 129 } 130 131 /** 132 * @return the preferred encoding of this Parser 133 */ 134 public String getDefaultEncoding() { 135 return "VB"; 136 } 137 138 /** 139 * @deprecated this method should not be public 140 * @param message HL7 message 141 * @return message structure 142 * @throws HL7Exception 143 */ 144 public String getMessageStructure(String message) throws HL7Exception { 145 return getStructure(message).messageStructure; 146 } 147 148 /** 149 * @return the message structure from MSH-9-3 150 */ 151 private MessageStructure getStructure(String message) throws HL7Exception { 152 EncodingCharacters ec = getEncodingChars(message); 153 String messageStructure; 154 boolean explicityDefined = true; 155 String wholeFieldNine; 156 try { 157 String[] fields = split(message.substring(0, Math.max(message.indexOf(SEGMENT_DELIMITER), message.length())), String.valueOf(ec.getFieldSeparator())); 158 wholeFieldNine = fields[8]; 159 160 // message structure is component 3 but we'll accept a composite of 161 // 1 and 2 if there is no component 3 ... 162 // if component 1 is ACK, then the structure is ACK regardless of 163 // component 2 164 String[] comps = split(wholeFieldNine, String.valueOf(ec.getComponentSeparator())); 165 if (comps.length >= 3) { 166 messageStructure = comps[2]; 167 } else if (comps.length > 0 && comps[0] != null && comps[0].equals("ACK")) { 168 messageStructure = "ACK"; 169 } else if (comps.length == 2) { 170 explicityDefined = false; 171 messageStructure = comps[0] + "_" + comps[1]; 172 } 173 /* 174 * else if (comps.length == 1 && comps[0] != null && 175 * comps[0].equals("ACK")) { messageStructure = "ACK"; //it's common 176 * for people to only populate component 1 in an ACK msg } 177 */ 178 else { 179 StringBuilder buf = new StringBuilder("Can't determine message structure from MSH-9: "); 180 buf.append(wholeFieldNine); 181 if (comps.length < 3) { 182 buf.append(" HINT: there are only "); 183 buf.append(comps.length); 184 buf.append(" of 3 components present"); 185 } 186 throw new HL7Exception(buf.toString(), ErrorCode.UNSUPPORTED_MESSAGE_TYPE); 187 } 188 } catch (IndexOutOfBoundsException e) { 189 throw new HL7Exception("Can't find message structure (MSH-9-3): " + e.getMessage(), ErrorCode.UNSUPPORTED_MESSAGE_TYPE); 190 } 191 192 return new MessageStructure(messageStructure, explicityDefined); 193 } 194 195 /** 196 * Returns object that contains the field separator and encoding characters 197 * for this message. 198 * 199 * @throws HL7Exception 200 */ 201 private static EncodingCharacters getEncodingChars(String message) throws HL7Exception { 202 if (message.length() < 8) { 203 throw new HL7Exception("Invalid message content: \"" + message + "\""); 204 } 205 return new EncodingCharacters(message.charAt(3), message.substring(4, 8)); 206 } 207 208 /** 209 * Parses a message string and returns the corresponding Message object. 210 * Unexpected segments added at the end of their group. 211 * 212 * @throws HL7Exception 213 * if the message is not correctly formatted. 214 * @throws EncodingNotSupportedException 215 * if the message encoded is not supported by this parser. 216 */ 217 protected Message doParse(String message, String version) throws HL7Exception { 218 219 // try to instantiate a message object of the right class 220 MessageStructure structure = getStructure(message); 221 Message m = instantiateMessage(structure.messageStructure, version, structure.explicitlyDefined); 222 // Note: this will change in future to reuse the Parser's/HapiContext's 223 // ValidationContext. 224 m.setValidationContext(getValidationContext()); 225 226 m.setParser(this); 227 228 parse(m, message); 229 230 return m; 231 } 232 233 /** 234 * {@inheritDoc} 235 */ 236 protected Message doParseForSpecificPackage(String message, String version, String packageName) throws HL7Exception { 237 238 // try to instantiate a message object of the right class 239 MessageStructure structure = getStructure(message); 240 Message m = instantiateMessageInASpecificPackage(structure.messageStructure, version, structure.explicitlyDefined, packageName); 241 242 parse(m, message); 243 244 return m; 245 } 246 247 /** 248 * Generates (or returns the cached value of) the message 249 */ 250 private IStructureDefinition getStructureDefinition(Message theMessage) throws HL7Exception { 251 252 Class<? extends Message> clazz = theMessage.getClass(); 253 HashMap<String, StructureDefinition> definitions = myStructureDefinitions.get(clazz); 254 255 StructureDefinition retVal; 256 if (definitions != null) { 257 retVal = definitions.get(theMessage.getName()); 258 if (retVal != null) { 259 return retVal; 260 } 261 } 262 263 if (theMessage instanceof SuperStructure) { 264 Set<String> appliesTo = ((SuperStructure) theMessage).getStructuresWhichChildAppliesTo("MSH"); 265 if (!appliesTo.contains(theMessage.getName())) { 266 throw new HL7Exception("Superstructure " + theMessage.getClass().getSimpleName() + " does not apply to message " + theMessage.getName() + ", can not parse."); 267 } 268 } 269 270 if (clazz.isAnnotationPresent(DoNotCacheStructure.class)) { 271 Holder<StructureDefinition> previousLeaf = new Holder<StructureDefinition>(); 272 retVal = createStructureDefinition(theMessage, previousLeaf, theMessage.getName()); 273 } else { 274 Message message = ReflectionUtil.instantiateMessage(clazz, getFactory()); 275 Holder<StructureDefinition> previousLeaf = new Holder<StructureDefinition>(); 276 retVal = createStructureDefinition(message, previousLeaf, theMessage.getName()); 277 278 if (!myStructureDefinitions.containsKey(clazz)) { 279 myStructureDefinitions.put(clazz, new HashMap<String, StructureDefinition>()); 280 } 281 myStructureDefinitions.get(clazz).put(theMessage.getName(), retVal); 282 } 283 284 return retVal; 285 } 286 287 private StructureDefinition createStructureDefinition(Structure theStructure, Holder<StructureDefinition> thePreviousLeaf, String theStructureName) throws HL7Exception { 288 289 StructureDefinition retVal = new StructureDefinition(); 290 retVal.setName(theStructure.getName()); 291 292 if (theStructure instanceof Group) { 293 retVal.setSegment(false); 294 Group group = (Group) theStructure; 295 int index = 0; 296 List<String> childNames = Arrays.asList(group.getNames()); 297 298 /* 299 * For SuperStructures, which can hold more than one type of structure, 300 * we only actually bring in the child names that are actually a part 301 * of the structure we are trying to parse 302 */ 303 if (theStructure instanceof SuperStructure) { 304 String struct = theStructureName; 305 Map<String, String> evtMap = new DefaultModelClassFactory().getEventMapForVersion(Version.versionOf(theStructure.getMessage().getVersion())); 306 if (evtMap.containsKey(struct)) { 307 struct = evtMap.get(struct); 308 } 309 childNames = ((SuperStructure) theStructure).getChildNamesForStructure(struct); 310 } 311 312 for (String nextName : childNames) { 313 Structure nextChild = group.get(nextName); 314 StructureDefinition structureDefinition = createStructureDefinition(nextChild, thePreviousLeaf, theStructureName); 315 structureDefinition.setNameAsItAppearsInParent(nextName); 316 structureDefinition.setRepeating(group.isRepeating(nextName)); 317 structureDefinition.setRequired(group.isRequired(nextName)); 318 structureDefinition.setChoiceElement(group.isChoiceElement(nextName)); 319 structureDefinition.setPosition(index++); 320 structureDefinition.setParent(retVal); 321 retVal.addChild(structureDefinition); 322 } 323 } else { 324 if (thePreviousLeaf.getObject() != null) { 325 thePreviousLeaf.getObject().setNextLeaf(retVal); 326 } 327 thePreviousLeaf.setObject(retVal); 328 retVal.setSegment(true); 329 } 330 331 return retVal; 332 } 333 334 /** 335 * Parses a segment string and populates the given Segment object. 336 * Unexpected fields are added as Varies' at the end of the segment. 337 * 338 * @param destination segment to parse the segment string into 339 * @param segment encoded segment 340 * @param encodingChars encoding characters to be used 341 * @throws HL7Exception 342 * if the given string does not contain the given segment or if 343 * the string is not encoded properly 344 */ 345 public void parse(Segment destination, String segment, EncodingCharacters encodingChars) throws HL7Exception { 346 parse(destination, segment, encodingChars, 0); 347 } 348 349 /** 350 * Parses a segment string and populates the given Segment object. 351 * Unexpected fields are added as Varies' at the end of the segment. 352 * 353 * @param destination segment to parse the segment string into 354 * @param segment encoded segment 355 * @param encodingChars encoding characters to be used 356 * @param theRepetition the repetition number of this segment within its group 357 * @throws HL7Exception 358 * if the given string does not contain the given segment or if 359 * the string is not encoded properly 360 */ 361 public void parse(Segment destination, String segment, EncodingCharacters encodingChars, int theRepetition) throws HL7Exception { 362 int fieldOffset = 0; 363 if (isDelimDefSegment(destination.getName())) { 364 fieldOffset = 1; 365 // set field 1 to fourth character of string 366 Terser.set(destination, 1, 0, 1, 1, String.valueOf(encodingChars.getFieldSeparator())); 367 } 368 369 String[] fields = split(segment, String.valueOf(encodingChars.getFieldSeparator())); 370 // destination.setName(fields[0]); 371 for (int i = 1; i < fields.length; i++) { 372 String[] reps = split(fields[i], String.valueOf(encodingChars.getRepetitionSeparator())); 373 374 // MSH-2 will get split incorrectly so we have to fudge it ... 375 boolean isMSH2 = isDelimDefSegment(destination.getName()) && i + fieldOffset == 2; 376 if (isMSH2) { 377 reps = new String[1]; 378 reps[0] = fields[i]; 379 } 380 381 for (int j = 0; j < reps.length; j++) { 382 try { 383 log.trace("Parsing field {} repetition {}", i + fieldOffset, j); 384 Type field = destination.getField(i + fieldOffset, j); 385 if (isMSH2) { 386 Terser.getPrimitive(field, 1, 1).setValue(reps[j]); 387 } else { 388 parse(field, reps[j], encodingChars); 389 } 390 } catch (HL7Exception e) { 391 // set the field location and throw again ... 392 e.setFieldPosition(i); 393 if (theRepetition > 1) { 394 e.setSegmentRepetition(theRepetition); 395 } 396 e.setSegmentName(destination.getName()); 397 throw e; 398 } 399 } 400 } 401 402 // set data type of OBX-5 403 if (destination.getClass().getName().contains("OBX")) { 404 Varies.fixOBX5(destination, getFactory(), getHapiContext().getParserConfiguration()); 405 } 406 407 } 408 409 /** 410 * @return true if the segment is MSH, FHS, or BHS. These need special 411 * treatment because they define delimiters. 412 * @param theSegmentName 413 * segment name 414 */ 415 private static boolean isDelimDefSegment(String theSegmentName) { 416 boolean is = false; 417 if (theSegmentName.equals("MSH") || theSegmentName.equals("FHS") || theSegmentName.equals("BHS")) { 418 is = true; 419 } 420 return is; 421 } 422 423 /** 424 * Fills a field with values from an unparsed string representing the field. 425 * 426 * @param destinationField 427 * the field Type 428 * @param data 429 * the field string (including all components and subcomponents; 430 * not including field delimiters) 431 * @param encodingCharacters 432 * the encoding characters used in the message 433 */ 434 @Override 435 public void parse(Type destinationField, String data, EncodingCharacters encodingCharacters) throws HL7Exception { 436 String[] components = split(data, String.valueOf(encodingCharacters.getComponentSeparator())); 437 for (int i = 0; i < components.length; i++) { 438 String[] subcomponents = split(components[i], String.valueOf(encodingCharacters.getSubcomponentSeparator())); 439 for (int j = 0; j < subcomponents.length; j++) { 440 String val = subcomponents[j]; 441 if (val != null) { 442 val = getParserConfiguration().getEscaping().unescape(val, encodingCharacters); 443 } 444 Terser.getPrimitive(destinationField, i + 1, j + 1).setValue(val); 445 } 446 } 447 } 448 449 /** 450 * Splits the given composite string into an array of components using the 451 * given delimiter. 452 * 453 * @param composite encoded composite string 454 * @param delim delimiter to split upon 455 * @return split string 456 */ 457 public static String[] split(String composite, String delim) { 458 ArrayList<String> components = new ArrayList<String>(); 459 460 // defend against evil nulls 461 if (composite == null) 462 composite = ""; 463 if (delim == null) 464 delim = ""; 465 466 StringTokenizer tok = new StringTokenizer(composite, delim, true); 467 boolean previousTokenWasDelim = true; 468 while (tok.hasMoreTokens()) { 469 String thisTok = tok.nextToken(); 470 if (thisTok.equals(delim)) { 471 if (previousTokenWasDelim) 472 components.add(null); 473 previousTokenWasDelim = true; 474 } else { 475 components.add(thisTok); 476 previousTokenWasDelim = false; 477 } 478 } 479 480 String[] ret = new String[components.size()]; 481 for (int i = 0; i < components.size(); i++) { 482 ret[i] = components.get(i); 483 } 484 485 return ret; 486 } 487 488 /** 489 * {@inheritDoc } 490 */ 491 @Override 492 public String doEncode(Segment structure, EncodingCharacters encodingCharacters) throws HL7Exception { 493 return encode(structure, encodingCharacters, getParserConfiguration(), null); 494 } 495 496 /** 497 * {@inheritDoc } 498 */ 499 @Override 500 public String doEncode(Type type, EncodingCharacters encodingCharacters) throws HL7Exception { 501 return encode(type, encodingCharacters, getParserConfiguration(), null); 502 } 503 504 /** 505 * Encodes the given Type, using the given encoding characters. It is 506 * assumed that the Type represents a complete field rather than a 507 * component. 508 * 509 * @param source type to be encoded 510 * @param encodingChars encoding characters to be used 511 * @return encoded type 512 */ 513 public static String encode(Type source, EncodingCharacters encodingChars) { 514 return encode(source, encodingChars, source.getMessage().getParser().getParserConfiguration(), null); 515 } 516 517 private static String encode(Type source, EncodingCharacters encodingChars, ParserConfiguration parserConfig, String currentTerserPath) { 518 if (source instanceof Varies) { 519 Varies varies = (Varies) source; 520 if (varies.getData() != null) { 521 source = varies.getData(); 522 } 523 } 524 525 StringBuilder field = new StringBuilder(); 526 for (int i = 1; i <= Terser.numComponents(source); i++) { 527 StringBuilder comp = new StringBuilder(); 528 for (int j = 1; j <= Terser.numSubComponents(source, i); j++) { 529 Primitive p = Terser.getPrimitive(source, i, j); 530 comp.append(encodePrimitive(p, parserConfig.getEscaping(), encodingChars)); 531 comp.append(encodingChars.getSubcomponentSeparator()); 532 } 533 field.append(stripExtraDelimiters(comp.toString(), encodingChars.getSubcomponentSeparator())); 534 field.append(encodingChars.getComponentSeparator()); 535 } 536 537 int forceUpToFieldNum = 0; 538 if (parserConfig != null && currentTerserPath != null) { 539 for (String nextPath : parserConfig.getForcedEncode()) { 540 if (nextPath.startsWith(currentTerserPath + "-") && nextPath.length() > currentTerserPath.length()) { 541 int endOfFieldDef = nextPath.indexOf('-', currentTerserPath.length()); 542 if (endOfFieldDef == -1) { 543 forceUpToFieldNum = 0; 544 break; 545 } 546 String fieldNumString = nextPath.substring(endOfFieldDef + 1, nextPath.length()); 547 if (fieldNumString.length() > 0) { 548 forceUpToFieldNum = Math.max(forceUpToFieldNum, Integer.parseInt(fieldNumString)); 549 } 550 } 551 } 552 } 553 554 char componentSeparator = encodingChars.getComponentSeparator(); 555 String retVal = stripExtraDelimiters(field.toString(), componentSeparator); 556 557 while (forceUpToFieldNum > 0 && (countInstancesOf(retVal, componentSeparator) + 1) < forceUpToFieldNum) { 558 retVal = retVal + componentSeparator; 559 } 560 561 return retVal; 562 } 563 564 private static String encodePrimitive(Primitive p, Escaping escaping, EncodingCharacters encodingChars) { 565 String val = (p).getValue(); 566 if (val == null) { 567 val = ""; 568 } else { 569 val = escaping.escape(val, encodingChars); 570 } 571 return val; 572 } 573 574 /** 575 * Removes unecessary delimiters from the end of a field or segment. This 576 * seems to be more convenient than checking to see if they are needed while 577 * we are building the encoded string. 578 */ 579 private static String stripExtraDelimiters(String in, char delim) { 580 char[] chars = in.toCharArray(); 581 582 // search from back end for first occurance of non-delimiter ... 583 int c = chars.length - 1; 584 boolean found = false; 585 while (c >= 0 && !found) { 586 if (chars[c--] != delim) 587 found = true; 588 } 589 590 String ret = ""; 591 if (found) 592 ret = String.valueOf(chars, 0, c + 2); 593 return ret; 594 } 595 596 /** 597 * Formats a Message object into an HL7 message string using the given 598 * encoding. 599 * 600 * @throws HL7Exception 601 * if the data fields in the message do not permit encoding 602 * (e.g. required fields are null) 603 * @throws EncodingNotSupportedException 604 * if the requested encoding is not supported by this parser. 605 */ 606 protected String doEncode(Message source, String encoding) throws HL7Exception { 607 if (!this.supportsEncoding(encoding)) 608 throw new EncodingNotSupportedException("This parser does not support the " + encoding + " encoding"); 609 610 return encode(source); 611 } 612 613 /** 614 * Formats a Message object into an HL7 message string using this parser's 615 * default encoding ("VB"). 616 * 617 * @throws HL7Exception 618 * if the data fields in the message do not permit encoding 619 * (e.g. required fields are null) 620 */ 621 protected String doEncode(Message source) throws HL7Exception { 622 // get encoding characters ... 623 Segment msh = (Segment) source.get("MSH"); 624 String fieldSepString = Terser.get(msh, 1, 0, 1, 1); 625 626 if (fieldSepString == null) 627 throw new HL7Exception("Can't encode message: MSH-1 (field separator) is missing"); 628 629 char fieldSep = '|'; 630 if (fieldSepString.length() > 0) 631 fieldSep = fieldSepString.charAt(0); 632 633 String encCharString = Terser.get(msh, 2, 0, 1, 1); 634 635 if (encCharString == null) 636 throw new HL7Exception("Can't encode message: MSH-2 (encoding characters) is missing"); 637 638 if (encCharString.length() != 4) 639 throw new HL7Exception("Encoding characters (MSH-2) value '" + encCharString + "' invalid -- must be 4 characters", ErrorCode.DATA_TYPE_ERROR); 640 EncodingCharacters en = new EncodingCharacters(fieldSep, encCharString); 641 642 // pass down to group encoding method which will operate recursively on 643 // children ... 644 return encode(source, en, getParserConfiguration(), ""); 645 } 646 647 /** 648 * Returns given group serialized as a pipe-encoded string - this method is 649 * called by encode(Message source, String encoding). 650 * 651 * @param source group to be encoded 652 * @param encodingChars encoding characters to be used 653 * @throws HL7Exception if an error occurred while encoding 654 * @return encoded group 655 */ 656 public static String encode(Group source, EncodingCharacters encodingChars) throws HL7Exception { 657 return encode(source, encodingChars, source.getMessage().getParser().getParserConfiguration(), ""); 658 } 659 660 /** 661 * Returns given group serialized as a pipe-encoded string - this method is 662 * called by encode(Message source, String encoding). 663 */ 664 private static String encode(Group source, EncodingCharacters encodingChars, ParserConfiguration parserConfiguration, String currentTerserPath) throws HL7Exception { 665 StringBuilder result = new StringBuilder(); 666 667 String[] names = source.getNames(); 668 669 String firstMandatorySegmentName = null; 670 boolean haveEncounteredMandatorySegment = false; 671 boolean haveEncounteredContent = false; 672 boolean haveHadMandatorySegment = false; 673 boolean haveHadSegmentBeforeMandatorySegment = false; 674 675 for (String nextName : names) { 676 677 source.get(nextName, 0); 678 Structure[] reps = source.getAll(nextName); 679 boolean nextNameIsRequired = source.isRequired(nextName); 680 681 boolean havePreviouslyEncounteredMandatorySegment = haveEncounteredMandatorySegment; 682 haveEncounteredMandatorySegment |= nextNameIsRequired; 683 if (nextNameIsRequired && !haveHadMandatorySegment) { 684 if (!source.isGroup(nextName)) { 685 firstMandatorySegmentName = nextName; 686 } 687 } 688 689 String nextTerserPath = currentTerserPath.length() > 0 ? currentTerserPath + "/" + nextName : nextName; 690 691 // Add all reps of the next segment/group 692 for (Structure rep : reps) { 693 694 if (rep instanceof Group) { 695 696 String encodedGroup = encode((Group) rep, encodingChars, parserConfiguration, nextTerserPath); 697 result.append(encodedGroup); 698 699 if (encodedGroup.length() > 0) { 700 if (!haveHadMandatorySegment && !haveEncounteredMandatorySegment) { 701 haveHadSegmentBeforeMandatorySegment = true; 702 } 703 if (nextNameIsRequired && !haveHadMandatorySegment && !havePreviouslyEncounteredMandatorySegment) { 704 haveHadMandatorySegment = true; 705 } 706 haveEncounteredContent = true; 707 } 708 709 } else { 710 711 // Check if we are configured to force the encoding of this 712 // segment 713 boolean encodeEmptySegments = parserConfiguration.determineForcedEncodeIncludesTerserPath(nextTerserPath); 714 String segString = encode((Segment) rep, encodingChars, parserConfiguration, nextTerserPath); 715 if (segString.length() >= 4 || encodeEmptySegments) { 716 result.append(segString); 717 718 if (segString.length() == 3) { 719 result.append(encodingChars.getFieldSeparator()); 720 } 721 722 result.append(SEGMENT_DELIMITER); 723 724 haveEncounteredContent = true; 725 726 if (nextNameIsRequired) { 727 haveHadMandatorySegment = true; 728 } 729 730 if (!haveHadMandatorySegment && !haveEncounteredMandatorySegment) { 731 haveHadSegmentBeforeMandatorySegment = true; 732 } 733 734 } 735 736 } 737 738 } 739 740 } 741 742 if (firstMandatorySegmentName != null && !haveHadMandatorySegment && !haveHadSegmentBeforeMandatorySegment && haveEncounteredContent && parserConfiguration.isEncodeEmptyMandatorySegments()) { 743 return firstMandatorySegmentName.substring(0, 3) + encodingChars.getFieldSeparator() + SEGMENT_DELIMITER + result; 744 } else { 745 return result.toString(); 746 } 747 } 748 749 /** 750 * Convenience factory method which returns an instance that has a new 751 * {@link DefaultHapiContext} initialized with a {@link NoValidation 752 * NoValidation validation context}. 753 * 754 * @return PipeParser with disabled validation 755 */ 756 public static PipeParser getInstanceWithNoValidation() { 757 HapiContext context = new DefaultHapiContext(); 758 context.setValidationContext(ValidationContextFactory.noValidation()); 759 return new PipeParser(context); 760 } 761 762 /** 763 * Returns given segment serialized as a pipe-encoded string. 764 * 765 * @param source segment to be encoded 766 * @param encodingChars encoding characters to be used 767 * @return encoded group 768 */ 769 public static String encode(Segment source, EncodingCharacters encodingChars) { 770 return encode(source, encodingChars, source.getMessage().getParser().getParserConfiguration(), null); 771 } 772 773 private static String encode(Segment source, EncodingCharacters encodingChars, ParserConfiguration parserConfig, String currentTerserPath) { 774 StringBuilder result = new StringBuilder(); 775 result.append(source.getName()); 776 result.append(encodingChars.getFieldSeparator()); 777 778 // start at field 2 for MSH segment because field 1 is the field 779 // delimiter 780 int startAt = 1; 781 if (isDelimDefSegment(source.getName())) 782 startAt = 2; 783 784 // loop through fields; for every field delimit any repetitions and add 785 // field delimiter after ... 786 int numFields = source.numFields(); 787 788 int forceUpToFieldNum = 0; 789 if (parserConfig != null && currentTerserPath != null) { 790 forceUpToFieldNum = parserConfig.determineForcedFieldNumForTerserPath(currentTerserPath); 791 } 792 numFields = Math.max(numFields, forceUpToFieldNum); 793 794 for (int i = startAt; i <= numFields; i++) { 795 796 String nextFieldTerserPath = currentTerserPath + "-" + i; 797 if (parserConfig != null && currentTerserPath != null) { 798 for (String nextPath : parserConfig.getForcedEncode()) { 799 if (nextPath.startsWith(nextFieldTerserPath + "-")) { 800 try { 801 source.getField(i, 0); 802 } catch (HL7Exception e) { 803 log.error("Error while encoding segment: ", e); 804 } 805 } 806 } 807 } 808 809 try { 810 Type[] reps = source.getField(i); 811 for (int j = 0; j < reps.length; j++) { 812 String fieldText = encode(reps[j], encodingChars, parserConfig, nextFieldTerserPath); 813 // if this is MSH-2, then it shouldn't be escaped, so 814 // unescape it again 815 if (isDelimDefSegment(source.getName()) && i == 2) 816 fieldText = parserConfig.getEscaping().unescape(fieldText, encodingChars); 817 result.append(fieldText); 818 if (j < reps.length - 1) 819 result.append(encodingChars.getRepetitionSeparator()); 820 } 821 } catch (HL7Exception e) { 822 log.error("Error while encoding segment: ", e); 823 } 824 result.append(encodingChars.getFieldSeparator()); 825 } 826 827 // strip trailing delimiters ... 828 char fieldSeparator = encodingChars.getFieldSeparator(); 829 String retVal = stripExtraDelimiters(result.toString(), fieldSeparator); 830 831 int offset = isDelimDefSegment(source.getName()) ? 1 : 0; 832 while (forceUpToFieldNum > 0 && (countInstancesOf(retVal, fieldSeparator) + offset) < forceUpToFieldNum) { 833 retVal = retVal + fieldSeparator; 834 } 835 836 return retVal; 837 } 838 839 private static int countInstancesOf(String theString, char theCharToSearchFor) { 840 int retVal = 0; 841 for (int i = 0; i < theString.length(); i++) { 842 if (theString.charAt(i) == theCharToSearchFor) { 843 retVal++; 844 } 845 } 846 return retVal; 847 } 848 849 /** 850 * Removes leading whitespace from the given string. This method was created 851 * to deal with frequent problems parsing messages that have been 852 * hand-written in windows. The intuitive way to delimit segments is to hit 853 * <ENTER> at the end of each segment, but this creates both a carriage 854 * return and a line feed, so to the parser, the first character of the next 855 * segment is the line feed. 856 * 857 * @param in input string 858 * @return string with leading whitespaces removed 859 */ 860 public static String stripLeadingWhitespace(String in) { 861 StringBuilder out = new StringBuilder(); 862 char[] chars = in.toCharArray(); 863 int c = 0; 864 while (c < chars.length) { 865 if (!Character.isWhitespace(chars[c])) 866 break; 867 c++; 868 } 869 for (int i = c; i < chars.length; i++) { 870 out.append(chars[i]); 871 } 872 return out.toString(); 873 } 874 875 /** 876 * <p> 877 * Returns a minimal amount of data from a message string, including only 878 * the data needed to send a response to the remote system. This includes 879 * the following fields: 880 * <ul> 881 * <li>field separator</li> 882 * <li>encoding characters</li> 883 * <li>processing ID</li> 884 * <li>message control ID</li> 885 * </ul> 886 * This method is intended for use when there is an error parsing a message, 887 * (so the Message object is unavailable) but an error message must be sent 888 * back to the remote system including some of the information in the 889 * inbound message. This method parses only that required information, 890 * hopefully avoiding the condition that caused the original error. The 891 * other fields in the returned MSH segment are empty. 892 * </p> 893 */ 894 public Segment getCriticalResponseData(String message) throws HL7Exception { 895 // try to get MSH segment 896 int locStartMSH = message.indexOf("MSH"); 897 if (locStartMSH < 0) 898 throw new HL7Exception("Couldn't find MSH segment in message: " + message, ErrorCode.SEGMENT_SEQUENCE_ERROR); 899 int locEndMSH = message.indexOf('\r', locStartMSH + 1); 900 if (locEndMSH < 0) 901 locEndMSH = message.length(); 902 String mshString = message.substring(locStartMSH, locEndMSH); 903 904 // find out what the field separator is 905 char fieldSep = mshString.charAt(3); 906 907 // get field array 908 String[] fields = split(mshString, String.valueOf(fieldSep)); 909 910 try { 911 // parse required fields 912 String encChars = fields[1]; 913 char compSep = encChars.charAt(0); 914 String messControlID = fields[9]; 915 String[] procIDComps = split(fields[10], String.valueOf(compSep)); 916 917 // fill MSH segment 918 String version = null; 919 try { 920 version = getVersion(message); 921 } catch (Exception e) { /* use the default */ 922 } 923 924 if (version == null) { 925 Version availableVersion = Version.highestAvailableVersionOrDefault(); 926 version = availableVersion.getVersion(); 927 } 928 929 Segment msh = Parser.makeControlMSH(version, getFactory()); 930 Terser.set(msh, 1, 0, 1, 1, String.valueOf(fieldSep)); 931 Terser.set(msh, 2, 0, 1, 1, encChars); 932 Terser.set(msh, 10, 0, 1, 1, messControlID); 933 Terser.set(msh, 11, 0, 1, 1, procIDComps[0]); 934 Terser.set(msh, 12, 0, 1, 1, version); 935 return msh; 936 937 } catch (Exception e) { 938 throw new HL7Exception("Can't parse critical fields from MSH segment (" + e.getClass().getName() + ": " + e.getMessage() + "): " + mshString, ErrorCode.REQUIRED_FIELD_MISSING, e); 939 } 940 941 } 942 943 /** 944 * For response messages, returns the value of MSA-2 (the message ID of the 945 * message sent by the sending system). This value may be needed prior to 946 * main message parsing, so that (particularly in a multi-threaded scenario) 947 * the message can be routed to the thread that sent the request. We need 948 * this information first so that any parse exceptions are thrown to the 949 * correct thread. Returns null if MSA-2 can not be found (e.g. if the 950 * message is not a response message). 951 */ 952 public String getAckID(String message) { 953 String ackID = null; 954 int startMSA = message.indexOf("\rMSA"); 955 if (startMSA >= 0) { 956 int startFieldOne = startMSA + 5; 957 char fieldDelim = message.charAt(startFieldOne - 1); 958 int start = message.indexOf(fieldDelim, startFieldOne) + 1; 959 int end = message.indexOf(fieldDelim, start); 960 int segEnd = message.indexOf(String.valueOf(SEGMENT_DELIMITER), start); 961 if (segEnd > start && segEnd < end) 962 end = segEnd; 963 964 // if there is no field delim after MSH-2, need to go to end of 965 // message, but not including end seg delim if it exists 966 if (end < 0) { 967 if (message.charAt(message.length() - 1) == '\r') { 968 end = message.length() - 1; 969 } else { 970 end = message.length(); 971 } 972 } 973 if (start > 0 && end > start) { 974 ackID = message.substring(start, end); 975 } 976 } 977 log.trace("ACK ID: {}", ackID); 978 return ackID; 979 } 980 981 /** 982 * Defaults to <code>false</code> 983 * 984 * @see #isLegacyMode() 985 * @deprecated This will be removed in HAPI 3.0 986 */ 987 public void setLegacyMode(boolean legacyMode) { 988 this.myLegacyMode = legacyMode; 989 } 990 991 /** 992 * {@inheritDoc } 993 */ 994 @Override 995 public String encode(Message source) throws HL7Exception { 996 if (myLegacyMode != null && myLegacyMode) { 997 998 @SuppressWarnings("deprecation") 999 OldPipeParser oldPipeParser = new OldPipeParser(getFactory()); 1000 1001 return oldPipeParser.encode(source); 1002 } 1003 return super.encode(source); 1004 } 1005 1006 /** 1007 * {@inheritDoc } 1008 */ 1009 @Override 1010 public Message parse(String message) throws HL7Exception { 1011 if (myLegacyMode != null && myLegacyMode) { 1012 1013 @SuppressWarnings("deprecation") 1014 OldPipeParser oldPipeParser = new OldPipeParser(getFactory()); 1015 1016 return oldPipeParser.parse(message); 1017 } 1018 return super.parse(message); 1019 } 1020 1021 /** 1022 * <p> 1023 * Returns <code>true</code> if legacy mode is on. 1024 * </p> 1025 * <p> 1026 * Prior to release 1.0, when an unexpected segment was encountered in a 1027 * message, HAPI would recurse to the deepest nesting in the last group it 1028 * encountered after the current position in the message, and deposit the 1029 * segment there. This could lead to unusual behaviour where all segments 1030 * afterward would not be in an expected spot within the message. 1031 * </p> 1032 * <p> 1033 * This should normally be set to false, but any code written before the 1034 * release of HAPI 1.0 which depended on this behaviour might need legacy 1035 * mode to be set to true. 1036 * </p> 1037 * <p> 1038 * Defaults to <code>false</code>. Note that this method only overrides 1039 * behaviour of the {@link #parse(java.lang.String)} and 1040 * {@link #encode(ca.uhn.hl7v2.model.Message) } methods 1041 * </p> 1042 * 1043 * @deprecated This will be removed in HAPI 3.0 1044 */ 1045 public boolean isLegacyMode() { 1046 if (myLegacyMode == null) 1047 return (Boolean.parseBoolean(System.getProperty(DEFAULT_LEGACY_MODE_PROPERTY))); 1048 return this.myLegacyMode; 1049 } 1050 1051 /** 1052 * Returns the version ID (MSH-12) from the given message, without fully 1053 * parsing the message. The version is needed prior to parsing in order to 1054 * determine the message class into which the text of the message should be 1055 * parsed. 1056 * 1057 * @throws HL7Exception 1058 * if the version field can not be found. 1059 */ 1060 public String getVersion(String message) throws HL7Exception { 1061 int startMSH = message.indexOf("MSH"); 1062 int endMSH = message.indexOf(PipeParser.SEGMENT_DELIMITER, startMSH); 1063 if (endMSH < 0) 1064 endMSH = message.length(); 1065 String msh = message.substring(startMSH, endMSH); 1066 String fieldSep; 1067 if (msh.length() > 3) { 1068 fieldSep = String.valueOf(msh.charAt(3)); 1069 } else { 1070 throw new HL7Exception("Can't find field separator in MSH: " + msh, ErrorCode.UNSUPPORTED_VERSION_ID); 1071 } 1072 1073 String[] fields = split(msh, fieldSep); 1074 1075 String compSep; 1076 if (fields.length >= 2 && fields[1] != null && fields[1].length() == 4) { 1077 compSep = String.valueOf(fields[1].charAt(0)); // get component 1078 // separator as 1st 1079 // encoding char 1080 } else { 1081 throw new HL7Exception("Invalid or incomplete encoding characters - MSH-2 is " + fields[1], ErrorCode.REQUIRED_FIELD_MISSING); 1082 } 1083 1084 String version; 1085 if (fields.length >= 12) { 1086 String[] comp = split(fields[11], compSep); 1087 if (comp.length >= 1) { 1088 version = comp[0]; 1089 } else { 1090 throw new HL7Exception("Can't find version ID - MSH.12 is " + fields[11], ErrorCode.REQUIRED_FIELD_MISSING); 1091 } 1092 } else if (getParserConfiguration().isAllowUnknownVersions()) { 1093 return Version.highestAvailableVersionOrDefault().getVersion(); 1094 } else { 1095 throw new HL7Exception("Can't find version ID - MSH has only " + fields.length + " fields.", ErrorCode.REQUIRED_FIELD_MISSING); 1096 } 1097 return version; 1098 } 1099 1100 @Override 1101 public void parse(Message message, String string) throws HL7Exception { 1102 if (message instanceof AbstractSuperMessage && message.getName() == null) { 1103 String structure = getStructure(string).messageStructure; 1104 ((AbstractSuperMessage) message).setName(structure); 1105 } 1106 1107 IStructureDefinition structureDef = getStructureDefinition(message); 1108 MessageIterator messageIter = new MessageIterator(message, structureDef, "MSH", true); 1109 1110 String[] segments = split(string, SEGMENT_DELIMITER); 1111 1112 if (segments.length == 0) { 1113 throw new HL7Exception("Invalid message content: \"" + string + "\""); 1114 } 1115 1116 if (segments[0] == null || segments[0].length() < 4) { 1117 throw new HL7Exception("Invalid message content: \"" + string + "\""); 1118 } 1119 1120 char delim = '|'; 1121 String prevName = null; 1122 int repNum = 1; 1123 for (int i = 0; i < segments.length; i++) { 1124 1125 // get rid of any leading whitespace characters ... 1126 if (segments[i] != null && segments[i].length() > 0 && Character.isWhitespace(segments[i].charAt(0))) 1127 segments[i] = stripLeadingWhitespace(segments[i]); 1128 1129 // sometimes people put extra segment delimiters at end of msg ... 1130 if (segments[i] != null && segments[i].length() >= 3) { 1131 1132 final String name; 1133 if (i == 0) { 1134 if (segments[i].length() < 4) { 1135 throw new HL7Exception("Invalid message content: \"" + string + "\""); 1136 } 1137 name = segments[i].substring(0, 3); 1138 delim = segments[i].charAt(3); 1139 } else { 1140 if (segments[i].indexOf(delim) >= 0) { 1141 name = segments[i].substring(0, segments[i].indexOf(delim)); 1142 } else { 1143 name = segments[i]; 1144 } 1145 } 1146 1147 log.trace("Parsing segment {}", name); 1148 1149 if (name.equals(prevName)) { 1150 repNum++; 1151 } else { 1152 repNum = 1; 1153 prevName = name; 1154 } 1155 1156 messageIter.setDirection(name); 1157 1158 try { 1159 if (messageIter.hasNext()) { 1160 Segment next = (Segment) messageIter.next(); 1161 parse(next, segments[i], getEncodingChars(string), repNum); 1162 } 1163 } catch (Error e) { 1164 if (e.getCause() instanceof HL7Exception) { 1165 throw (HL7Exception)e.getCause(); 1166 } 1167 throw e; 1168 } 1169 } 1170 } 1171 1172 applySuperStructureName(message); 1173 } 1174 1175 /** 1176 * A struct for holding a message class string and a boolean indicating 1177 * whether it was defined explicitly. 1178 */ 1179 private static class MessageStructure { 1180 public String messageStructure; 1181 public boolean explicitlyDefined; 1182 1183 public MessageStructure(String theMessageStructure, boolean isExplicitlyDefined) { 1184 messageStructure = theMessageStructure; 1185 explicitlyDefined = isExplicitlyDefined; 1186 } 1187 } 1188 1189 private static class Holder<T> { 1190 private T myObject; 1191 1192 public T getObject() { 1193 return myObject; 1194 } 1195 1196 public void setObject(T theObject) { 1197 myObject = theObject; 1198 } 1199 } 1200 1201}