| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
package ca.uhn.hl7v2.parser; |
| 29 | |
|
| 30 | |
import java.util.ArrayList; |
| 31 | |
import java.util.Arrays; |
| 32 | |
import java.util.HashMap; |
| 33 | |
import java.util.List; |
| 34 | |
import java.util.Map; |
| 35 | |
import java.util.Set; |
| 36 | |
import java.util.StringTokenizer; |
| 37 | |
|
| 38 | |
import ca.uhn.hl7v2.validation.ValidationContext; |
| 39 | |
import org.slf4j.Logger; |
| 40 | |
import org.slf4j.LoggerFactory; |
| 41 | |
|
| 42 | |
import ca.uhn.hl7v2.DefaultHapiContext; |
| 43 | |
import ca.uhn.hl7v2.ErrorCode; |
| 44 | |
import ca.uhn.hl7v2.HL7Exception; |
| 45 | |
import ca.uhn.hl7v2.HapiContext; |
| 46 | |
import ca.uhn.hl7v2.Version; |
| 47 | |
import ca.uhn.hl7v2.model.AbstractSuperMessage; |
| 48 | |
import ca.uhn.hl7v2.model.DoNotCacheStructure; |
| 49 | |
import ca.uhn.hl7v2.model.Group; |
| 50 | |
import ca.uhn.hl7v2.model.Message; |
| 51 | |
import ca.uhn.hl7v2.model.Primitive; |
| 52 | |
import ca.uhn.hl7v2.model.Segment; |
| 53 | |
import ca.uhn.hl7v2.model.Structure; |
| 54 | |
import ca.uhn.hl7v2.model.SuperStructure; |
| 55 | |
import ca.uhn.hl7v2.model.Type; |
| 56 | |
import ca.uhn.hl7v2.model.Varies; |
| 57 | |
import ca.uhn.hl7v2.util.ReflectionUtil; |
| 58 | |
import ca.uhn.hl7v2.util.Terser; |
| 59 | |
import ca.uhn.hl7v2.validation.impl.NoValidation; |
| 60 | |
import ca.uhn.hl7v2.validation.impl.ValidationContextFactory; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public class PipeParser extends Parser { |
| 71 | |
|
| 72 | 5 | private static final Logger log = LoggerFactory.getLogger(PipeParser.class); |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
final static String SEGMENT_DELIMITER = "\r"; |
| 78 | |
|
| 79 | 5275 | private final HashMap<Class<? extends Message>, HashMap<String, StructureDefinition>> myStructureDefinitions = new HashMap<Class<? extends Message>, HashMap<String, StructureDefinition>>(); |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public static final String DEFAULT_LEGACY_MODE_PROPERTY = "ca.uhn.hl7v2.parser.PipeParser.default_legacy_mode"; |
| 88 | |
|
| 89 | 5275 | private Boolean myLegacyMode = null; |
| 90 | |
|
| 91 | |
public PipeParser() { |
| 92 | 4580 | super(); |
| 93 | 4580 | } |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public PipeParser(HapiContext context) { |
| 100 | 675 | super(context); |
| 101 | 675 | } |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
public PipeParser(ModelClassFactory theFactory) { |
| 110 | 20 | super(theFactory); |
| 111 | 20 | } |
| 112 | |
|
| 113 | |
@Override |
| 114 | |
public void setValidationContext(ValidationContext context) { |
| 115 | 0 | super.setValidationContext(context); |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public String getEncoding(String message) { |
| 128 | 6654 | return EncodingDetector.isEr7Encoded(message) ? getDefaultEncoding() : null; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public String getDefaultEncoding() { |
| 135 | 11569 | return "VB"; |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
public String getMessageStructure(String message) throws HL7Exception { |
| 145 | 0 | return getStructure(message).messageStructure; |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
private MessageStructure getStructure(String message) throws HL7Exception { |
| 152 | 3046 | EncodingCharacters ec = getEncodingChars(message); |
| 153 | |
String messageStructure; |
| 154 | 3046 | boolean explicityDefined = true; |
| 155 | |
String wholeFieldNine; |
| 156 | |
try { |
| 157 | 3046 | String[] fields = split(message.substring(0, Math.max(message.indexOf(SEGMENT_DELIMITER), message.length())), String.valueOf(ec.getFieldSeparator())); |
| 158 | 3046 | wholeFieldNine = fields[8]; |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | 3046 | String[] comps = split(wholeFieldNine, String.valueOf(ec.getComponentSeparator())); |
| 165 | 3046 | if (comps.length >= 3) { |
| 166 | 740 | messageStructure = comps[2]; |
| 167 | 2306 | } else if (comps.length > 0 && comps[0] != null && comps[0].equals("ACK")) { |
| 168 | 583 | messageStructure = "ACK"; |
| 169 | 1723 | } else if (comps.length == 2) { |
| 170 | 1723 | explicityDefined = false; |
| 171 | 1723 | messageStructure = comps[0] + "_" + comps[1]; |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
else { |
| 179 | 0 | StringBuilder buf = new StringBuilder("Can't determine message structure from MSH-9: "); |
| 180 | 0 | buf.append(wholeFieldNine); |
| 181 | 0 | if (comps.length < 3) { |
| 182 | 0 | buf.append(" HINT: there are only "); |
| 183 | 0 | buf.append(comps.length); |
| 184 | 0 | buf.append(" of 3 components present"); |
| 185 | |
} |
| 186 | 0 | throw new HL7Exception(buf.toString(), ErrorCode.UNSUPPORTED_MESSAGE_TYPE); |
| 187 | |
} |
| 188 | 0 | } catch (IndexOutOfBoundsException e) { |
| 189 | 0 | throw new HL7Exception("Can't find message structure (MSH-9-3): " + e.getMessage(), ErrorCode.UNSUPPORTED_MESSAGE_TYPE); |
| 190 | 3046 | } |
| 191 | |
|
| 192 | 3046 | return new MessageStructure(messageStructure, explicityDefined); |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
private static EncodingCharacters getEncodingChars(String message) throws HL7Exception { |
| 205 | 14895 | if (message.length() < 9) { |
| 206 | 10 | throw new HL7Exception("Invalid message content: \"" + message + "\""); |
| 207 | |
} |
| 208 | 14885 | return new EncodingCharacters(message.charAt(3), message.substring(4, 9)); |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
protected Message doParse(String message, String version) throws HL7Exception { |
| 221 | |
|
| 222 | |
|
| 223 | 2801 | MessageStructure structure = getStructure(message); |
| 224 | 2801 | Message m = instantiateMessage(structure.messageStructure, version, structure.explicitlyDefined); |
| 225 | 2801 | m.setParser(this); |
| 226 | 2801 | parse(m, message); |
| 227 | 2764 | return m; |
| 228 | |
} |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
protected Message doParseForSpecificPackage(String message, String version, String packageName) throws HL7Exception { |
| 234 | |
|
| 235 | |
|
| 236 | 0 | MessageStructure structure = getStructure(message); |
| 237 | 0 | Message m = instantiateMessageInASpecificPackage(structure.messageStructure, version, structure.explicitlyDefined, packageName); |
| 238 | |
|
| 239 | 0 | parse(m, message); |
| 240 | |
|
| 241 | 0 | return m; |
| 242 | |
} |
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
private IStructureDefinition getStructureDefinition(Message theMessage) throws HL7Exception { |
| 248 | |
|
| 249 | 3391 | Class<? extends Message> clazz = theMessage.getClass(); |
| 250 | 3391 | HashMap<String, StructureDefinition> definitions = myStructureDefinitions.get(clazz); |
| 251 | |
|
| 252 | |
StructureDefinition retVal; |
| 253 | 3391 | if (definitions != null) { |
| 254 | 1988 | retVal = definitions.get(theMessage.getName()); |
| 255 | 1988 | if (retVal != null) { |
| 256 | 1988 | return retVal; |
| 257 | |
} |
| 258 | |
} |
| 259 | |
|
| 260 | 1403 | if (theMessage instanceof SuperStructure) { |
| 261 | 245 | Set<String> appliesTo = ((SuperStructure) theMessage).getStructuresWhichChildAppliesTo("MSH"); |
| 262 | 245 | if (!appliesTo.contains(theMessage.getName())) { |
| 263 | 0 | throw new HL7Exception("Superstructure " + theMessage.getClass().getSimpleName() + " does not apply to message " + theMessage.getName() + ", can not parse."); |
| 264 | |
} |
| 265 | |
} |
| 266 | |
|
| 267 | 1403 | if (clazz.isAnnotationPresent(DoNotCacheStructure.class)) { |
| 268 | 0 | Holder<StructureDefinition> previousLeaf = new Holder<StructureDefinition>(); |
| 269 | 0 | retVal = createStructureDefinition(theMessage, previousLeaf, theMessage.getName()); |
| 270 | 0 | } else { |
| 271 | 1403 | Message message = ReflectionUtil.instantiateMessage(clazz, getFactory()); |
| 272 | 1403 | Holder<StructureDefinition> previousLeaf = new Holder<StructureDefinition>(); |
| 273 | 1403 | retVal = createStructureDefinition(message, previousLeaf, theMessage.getName()); |
| 274 | |
|
| 275 | 1403 | if (!myStructureDefinitions.containsKey(clazz)) { |
| 276 | 1380 | myStructureDefinitions.put(clazz, new HashMap<String, StructureDefinition>()); |
| 277 | |
} |
| 278 | 1403 | myStructureDefinitions.get(clazz).put(theMessage.getName(), retVal); |
| 279 | |
} |
| 280 | |
|
| 281 | 1403 | return retVal; |
| 282 | |
} |
| 283 | |
|
| 284 | |
private StructureDefinition createStructureDefinition(Structure theStructure, Holder<StructureDefinition> thePreviousLeaf, String theStructureName) throws HL7Exception { |
| 285 | |
|
| 286 | 26856 | StructureDefinition retVal = new StructureDefinition(); |
| 287 | 26856 | retVal.setName(theStructure.getName()); |
| 288 | |
|
| 289 | 26856 | if (theStructure instanceof Group) { |
| 290 | 5033 | retVal.setSegment(false); |
| 291 | 5033 | Group group = (Group) theStructure; |
| 292 | 5033 | int index = 0; |
| 293 | 5033 | List<String> childNames = Arrays.asList(group.getNames()); |
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | 5033 | if (theStructure instanceof SuperStructure) { |
| 301 | 245 | String struct = theStructureName; |
| 302 | 245 | Map<String, String> evtMap = new DefaultModelClassFactory().getEventMapForVersion(Version.versionOf(theStructure.getMessage().getVersion())); |
| 303 | 245 | if (evtMap.containsKey(struct)) { |
| 304 | 15 | struct = evtMap.get(struct); |
| 305 | |
} |
| 306 | 245 | childNames = ((SuperStructure) theStructure).getChildNamesForStructure(struct); |
| 307 | |
} |
| 308 | |
|
| 309 | 5033 | for (String nextName : childNames) { |
| 310 | 25453 | Structure nextChild = group.get(nextName); |
| 311 | 25453 | StructureDefinition structureDefinition = createStructureDefinition(nextChild, thePreviousLeaf, theStructureName); |
| 312 | 25453 | structureDefinition.setNameAsItAppearsInParent(nextName); |
| 313 | 25453 | structureDefinition.setRepeating(group.isRepeating(nextName)); |
| 314 | 25453 | structureDefinition.setRequired(group.isRequired(nextName)); |
| 315 | 25453 | structureDefinition.setChoiceElement(group.isChoiceElement(nextName)); |
| 316 | 25453 | structureDefinition.setPosition(index++); |
| 317 | 25453 | structureDefinition.setParent(retVal); |
| 318 | 25453 | retVal.addChild(structureDefinition); |
| 319 | 25453 | } |
| 320 | 5033 | } else { |
| 321 | 21823 | if (thePreviousLeaf.getObject() != null) { |
| 322 | 20420 | thePreviousLeaf.getObject().setNextLeaf(retVal); |
| 323 | |
} |
| 324 | 21823 | thePreviousLeaf.setObject(retVal); |
| 325 | 21823 | retVal.setSegment(true); |
| 326 | |
} |
| 327 | |
|
| 328 | 26856 | return retVal; |
| 329 | |
} |
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
public void parse(Segment destination, String segment, EncodingCharacters encodingChars) throws HL7Exception { |
| 343 | 2010 | parse(destination, segment, encodingChars, 0); |
| 344 | 2010 | } |
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
public void parse(Segment destination, String segment, EncodingCharacters encodingChars, int theRepetition) throws HL7Exception { |
| 359 | 13849 | int fieldOffset = 0; |
| 360 | 13849 | if (isDelimDefSegment(destination.getName())) { |
| 361 | 3360 | fieldOffset = 1; |
| 362 | |
|
| 363 | 3360 | Terser.set(destination, 1, 0, 1, 1, String.valueOf(encodingChars.getFieldSeparator())); |
| 364 | |
} |
| 365 | |
|
| 366 | 13849 | String[] fields = split(segment, String.valueOf(encodingChars.getFieldSeparator())); |
| 367 | |
|
| 368 | 175668 | for (int i = 1; i < fields.length; i++) { |
| 369 | 161854 | String[] reps = split(fields[i], String.valueOf(encodingChars.getRepetitionSeparator())); |
| 370 | |
|
| 371 | |
|
| 372 | 161854 | boolean isMSH2 = isDelimDefSegment(destination.getName()) && i + fieldOffset == 2; |
| 373 | 161854 | if (isMSH2) { |
| 374 | 3360 | reps = new String[1]; |
| 375 | 3360 | reps[0] = fields[i]; |
| 376 | |
} |
| 377 | |
|
| 378 | 235405 | for (int j = 0; j < reps.length; j++) { |
| 379 | |
try { |
| 380 | 73586 | log.trace("Parsing field {} repetition {}", i + fieldOffset, j); |
| 381 | 73586 | Type field = destination.getField(i + fieldOffset, j); |
| 382 | 73586 | if (isMSH2) { |
| 383 | 3360 | Terser.getPrimitive(field, 1, 1).setValue(reps[j]); |
| 384 | |
} else { |
| 385 | 70226 | parse(field, reps[j], encodingChars); |
| 386 | |
} |
| 387 | 35 | } catch (HL7Exception e) { |
| 388 | |
|
| 389 | 35 | e.setFieldPosition(i); |
| 390 | 35 | if (theRepetition > 1) { |
| 391 | 0 | e.setSegmentRepetition(theRepetition); |
| 392 | |
} |
| 393 | 35 | e.setSegmentName(destination.getName()); |
| 394 | 35 | throw e; |
| 395 | 73551 | } |
| 396 | |
} |
| 397 | |
} |
| 398 | |
|
| 399 | |
|
| 400 | 13814 | if (destination.getClass().getName().contains("OBX")) { |
| 401 | 875 | FixFieldDataType.fixOBX5(destination, getFactory(), getHapiContext().getParserConfiguration()); |
| 402 | |
} |
| 403 | |
|
| 404 | 13809 | if (destination.getClass().getName().contains("MFE") && |
| 405 | 0 | Version.versionOf(destination.getMessage().getVersion()).isGreaterThan(Version.V23)) { |
| 406 | 0 | FixFieldDataType.fixMFE4(destination, getFactory(), getHapiContext().getParserConfiguration()); |
| 407 | |
} |
| 408 | |
|
| 409 | 13809 | } |
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
private static boolean isDelimDefSegment(String theSegmentName) { |
| 418 | 245022 | boolean is = false; |
| 419 | 245022 | if (theSegmentName.equals("MSH") || theSegmentName.equals("FHS") || theSegmentName.equals("BHS")) { |
| 420 | 71816 | is = true; |
| 421 | |
} |
| 422 | 245022 | return is; |
| 423 | |
} |
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
@Override |
| 437 | |
public void parse(Type destinationField, String data, EncodingCharacters encodingCharacters) throws HL7Exception { |
| 438 | 70436 | String[] components = split(data, String.valueOf(encodingCharacters.getComponentSeparator())); |
| 439 | 194404 | for (int i = 0; i < components.length; i++) { |
| 440 | 124003 | String[] subcomponents = split(components[i], String.valueOf(encodingCharacters.getSubcomponentSeparator())); |
| 441 | 222056 | for (int j = 0; j < subcomponents.length; j++) { |
| 442 | 98088 | String val = subcomponents[j]; |
| 443 | 98088 | if (val != null) { |
| 444 | 97748 | val = getParserConfiguration().getEscaping().unescape(val, encodingCharacters); |
| 445 | |
} |
| 446 | 98088 | Terser.getPrimitive(destinationField, i + 1, j + 1).setValue(val); |
| 447 | |
} |
| 448 | |
} |
| 449 | 70401 | } |
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
public static String[] split(String composite, String delim) { |
| 460 | 385572 | ArrayList<String> components = new ArrayList<String>(); |
| 461 | |
|
| 462 | |
|
| 463 | 385572 | if (composite == null) |
| 464 | 117368 | composite = ""; |
| 465 | 385572 | if (delim == null) |
| 466 | 0 | delim = ""; |
| 467 | |
|
| 468 | 385572 | StringTokenizer tok = new StringTokenizer(composite, delim, true); |
| 469 | 385572 | boolean previousTokenWasDelim = true; |
| 470 | 1294539 | while (tok.hasMoreTokens()) { |
| 471 | 908967 | String thisTok = tok.nextToken(); |
| 472 | 908967 | if (thisTok.equals(delim)) { |
| 473 | 430361 | if (previousTokenWasDelim) |
| 474 | 211528 | components.add(null); |
| 475 | 430361 | previousTokenWasDelim = true; |
| 476 | |
} else { |
| 477 | 478606 | components.add(thisTok); |
| 478 | 478606 | previousTokenWasDelim = false; |
| 479 | |
} |
| 480 | 908967 | } |
| 481 | |
|
| 482 | 385572 | String[] ret = new String[components.size()]; |
| 483 | 1075706 | for (int i = 0; i < components.size(); i++) { |
| 484 | 690134 | ret[i] = components.get(i); |
| 485 | |
} |
| 486 | |
|
| 487 | 385572 | return ret; |
| 488 | |
} |
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | |
@Override |
| 494 | |
public String doEncode(Segment structure, EncodingCharacters encodingCharacters) throws HL7Exception { |
| 495 | 4680 | return encode(structure, encodingCharacters, getParserConfiguration(), null); |
| 496 | |
} |
| 497 | |
|
| 498 | |
|
| 499 | |
|
| 500 | |
|
| 501 | |
@Override |
| 502 | |
public String doEncode(Type type, EncodingCharacters encodingCharacters) throws HL7Exception { |
| 503 | 335 | return encode(type, encodingCharacters, getParserConfiguration(), null); |
| 504 | |
} |
| 505 | |
|
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | |
|
| 513 | |
|
| 514 | |
|
| 515 | |
public static String encode(Type source, EncodingCharacters encodingChars) { |
| 516 | 2870 | return encode(source, encodingChars, source.getMessage().getParser().getParserConfiguration(), null); |
| 517 | |
} |
| 518 | |
|
| 519 | |
private static String encode(Type source, EncodingCharacters encodingChars, ParserConfiguration parserConfig, String currentTerserPath) { |
| 520 | 46480 | if (source instanceof Varies) { |
| 521 | 925 | Varies varies = (Varies) source; |
| 522 | 925 | if (varies.getData() != null) { |
| 523 | 925 | source = varies.getData(); |
| 524 | |
} |
| 525 | |
} |
| 526 | |
|
| 527 | 46480 | StringBuilder field = new StringBuilder(); |
| 528 | 206622 | for (int i = 1; i <= Terser.numComponents(source); i++) { |
| 529 | 160142 | StringBuilder comp = new StringBuilder(); |
| 530 | 413618 | for (int j = 1; j <= Terser.numSubComponents(source, i); j++) { |
| 531 | 253476 | Primitive p = Terser.getPrimitive(source, i, j); |
| 532 | 253476 | comp.append(encodePrimitive(p, parserConfig.getEscaping(), encodingChars)); |
| 533 | 253476 | comp.append(encodingChars.getSubcomponentSeparator()); |
| 534 | |
} |
| 535 | 160142 | field.append(stripExtraDelimiters(comp.toString(), encodingChars.getSubcomponentSeparator())); |
| 536 | 160142 | field.append(encodingChars.getComponentSeparator()); |
| 537 | |
} |
| 538 | |
|
| 539 | 46480 | int forceUpToFieldNum = 0; |
| 540 | 46480 | if (parserConfig != null && currentTerserPath != null) { |
| 541 | 43275 | for (String nextPath : parserConfig.getForcedEncode()) { |
| 542 | 490 | if (nextPath.startsWith(currentTerserPath + "-") && nextPath.length() > currentTerserPath.length()) { |
| 543 | 35 | int endOfFieldDef = nextPath.indexOf('-', currentTerserPath.length()); |
| 544 | 35 | if (endOfFieldDef == -1) { |
| 545 | 0 | forceUpToFieldNum = 0; |
| 546 | 0 | break; |
| 547 | |
} |
| 548 | 35 | String fieldNumString = nextPath.substring(endOfFieldDef + 1, nextPath.length()); |
| 549 | 35 | if (fieldNumString.length() > 0) { |
| 550 | 35 | forceUpToFieldNum = Math.max(forceUpToFieldNum, Integer.parseInt(fieldNumString)); |
| 551 | |
} |
| 552 | |
} |
| 553 | 490 | } |
| 554 | |
} |
| 555 | |
|
| 556 | 46480 | char componentSeparator = encodingChars.getComponentSeparator(); |
| 557 | 46480 | String retVal = stripExtraDelimiters(field.toString(), componentSeparator); |
| 558 | |
|
| 559 | 52060 | while (forceUpToFieldNum > 0 && (countInstancesOf(retVal, componentSeparator) + 1) < forceUpToFieldNum) { |
| 560 | 5580 | retVal = retVal + componentSeparator; |
| 561 | |
} |
| 562 | |
|
| 563 | 46480 | return retVal; |
| 564 | |
} |
| 565 | |
|
| 566 | |
private static String encodePrimitive(Primitive p, Escaping escaping, EncodingCharacters encodingChars) { |
| 567 | 253476 | String val = (p).getValue(); |
| 568 | 253476 | if (val == null) { |
| 569 | 197895 | val = ""; |
| 570 | |
} else { |
| 571 | 55581 | val = escaping.escape(val, encodingChars); |
| 572 | |
} |
| 573 | 253476 | return val; |
| 574 | |
} |
| 575 | |
|
| 576 | |
|
| 577 | |
|
| 578 | |
|
| 579 | |
|
| 580 | |
|
| 581 | |
private static String stripExtraDelimiters(String in, char delim) { |
| 582 | 219644 | char[] chars = in.toCharArray(); |
| 583 | |
|
| 584 | |
|
| 585 | 219644 | int c = chars.length - 1; |
| 586 | 219644 | boolean found = false; |
| 587 | 962640 | while (c >= 0 && !found) { |
| 588 | 742996 | if (chars[c--] != delim) |
| 589 | 106787 | found = true; |
| 590 | |
} |
| 591 | |
|
| 592 | 219644 | String ret = ""; |
| 593 | 219644 | if (found) |
| 594 | 106787 | ret = String.valueOf(chars, 0, c + 2); |
| 595 | 219644 | return ret; |
| 596 | |
} |
| 597 | |
|
| 598 | |
|
| 599 | |
|
| 600 | |
|
| 601 | |
|
| 602 | |
|
| 603 | |
|
| 604 | |
|
| 605 | |
|
| 606 | |
|
| 607 | |
|
| 608 | |
protected String doEncode(Message source, String encoding) throws HL7Exception { |
| 609 | 263 | if (!this.supportsEncoding(encoding)) |
| 610 | 0 | throw new EncodingNotSupportedException("This parser does not support the " + encoding + " encoding"); |
| 611 | |
|
| 612 | 263 | return encode(source); |
| 613 | |
} |
| 614 | |
|
| 615 | |
|
| 616 | |
|
| 617 | |
|
| 618 | |
|
| 619 | |
|
| 620 | |
|
| 621 | |
|
| 622 | |
|
| 623 | |
protected String doEncode(Message source) throws HL7Exception { |
| 624 | |
|
| 625 | 2386 | Segment msh = (Segment) source.get("MSH"); |
| 626 | 2386 | String fieldSepString = Terser.get(msh, 1, 0, 1, 1); |
| 627 | |
|
| 628 | 2386 | if (fieldSepString == null) |
| 629 | 0 | throw new HL7Exception("Can't encode message: MSH-1 (field separator) is missing"); |
| 630 | |
|
| 631 | 2386 | char fieldSep = '|'; |
| 632 | 2386 | if (fieldSepString.length() > 0) fieldSep = fieldSepString.charAt(0); |
| 633 | |
|
| 634 | 2386 | EncodingCharacters en = getValidEncodingCharacters(fieldSep, msh); |
| 635 | |
|
| 636 | |
|
| 637 | |
|
| 638 | 2386 | return encode(source, en, getParserConfiguration(), ""); |
| 639 | |
} |
| 640 | |
|
| 641 | |
private EncodingCharacters getValidEncodingCharacters(char fieldSep, Segment msh) throws HL7Exception { |
| 642 | 2386 | String encCharString = Terser.get(msh, 2, 0, 1, 1); |
| 643 | |
|
| 644 | 2386 | if (encCharString == null) { |
| 645 | 0 | throw new HL7Exception("Can't encode message: MSH-2 (encoding characters) is missing"); |
| 646 | |
} |
| 647 | |
|
| 648 | 2386 | if (Version.V27.isGreaterThan(Version.versionOf(msh.getMessage().getVersion())) && encCharString.length() != 4) { |
| 649 | 0 | throw new HL7Exception("Encoding characters (MSH-2) value '" + encCharString + "' invalid -- must be 4 characters", ErrorCode.DATA_TYPE_ERROR); |
| 650 | 2386 | } else if (encCharString.length() != 4 && encCharString.length() != 5) { |
| 651 | 0 | throw new HL7Exception("Encoding characters (MSH-2) value '" + encCharString + "' invalid -- must be 4 or 5 characters", ErrorCode.DATA_TYPE_ERROR); |
| 652 | |
} |
| 653 | |
|
| 654 | 2386 | return new EncodingCharacters(fieldSep, encCharString); |
| 655 | |
} |
| 656 | |
|
| 657 | |
|
| 658 | |
|
| 659 | |
|
| 660 | |
|
| 661 | |
|
| 662 | |
|
| 663 | |
|
| 664 | |
|
| 665 | |
|
| 666 | |
public static String encode(Group source, EncodingCharacters encodingChars) throws HL7Exception { |
| 667 | 0 | return encode(source, encodingChars, source.getMessage().getParser().getParserConfiguration(), ""); |
| 668 | |
} |
| 669 | |
|
| 670 | |
|
| 671 | |
|
| 672 | |
|
| 673 | |
|
| 674 | |
private static String encode(Group source, EncodingCharacters encodingChars, ParserConfiguration parserConfiguration, String currentTerserPath) throws HL7Exception { |
| 675 | 4486 | StringBuilder result = new StringBuilder(); |
| 676 | |
|
| 677 | 4486 | String[] names = source.getNames(); |
| 678 | |
|
| 679 | 4486 | String firstMandatorySegmentName = null; |
| 680 | 4486 | boolean haveEncounteredMandatorySegment = false; |
| 681 | 4486 | boolean haveEncounteredContent = false; |
| 682 | 4486 | boolean haveHadMandatorySegment = false; |
| 683 | 4486 | boolean haveHadSegmentBeforeMandatorySegment = false; |
| 684 | |
|
| 685 | 30152 | for (String nextName : names) { |
| 686 | |
|
| 687 | |
|
| 688 | 25666 | Structure[] reps = source.getAll(nextName); |
| 689 | 25666 | boolean nextNameIsRequired = source.isRequired(nextName); |
| 690 | |
|
| 691 | 25666 | boolean havePreviouslyEncounteredMandatorySegment = haveEncounteredMandatorySegment; |
| 692 | 25666 | haveEncounteredMandatorySegment |= nextNameIsRequired; |
| 693 | 25666 | if (nextNameIsRequired && !haveHadMandatorySegment) { |
| 694 | 4426 | if (!source.isGroup(nextName)) { |
| 695 | 3616 | firstMandatorySegmentName = nextName; |
| 696 | |
} |
| 697 | |
} |
| 698 | |
|
| 699 | 25666 | String nextTerserPath = currentTerserPath.length() > 0 ? currentTerserPath + "/" + nextName : nextName; |
| 700 | |
|
| 701 | |
|
| 702 | 36108 | for (Structure rep : reps) { |
| 703 | |
|
| 704 | 10442 | if (rep instanceof Group) { |
| 705 | |
|
| 706 | 2100 | String encodedGroup = encode((Group) rep, encodingChars, parserConfiguration, nextTerserPath); |
| 707 | 2100 | result.append(encodedGroup); |
| 708 | |
|
| 709 | 2100 | if (encodedGroup.length() > 0) { |
| 710 | 2055 | if (!haveHadMandatorySegment && !haveEncounteredMandatorySegment) { |
| 711 | 600 | haveHadSegmentBeforeMandatorySegment = true; |
| 712 | |
} |
| 713 | 2055 | if (nextNameIsRequired && !haveHadMandatorySegment && !havePreviouslyEncounteredMandatorySegment) { |
| 714 | 185 | haveHadMandatorySegment = true; |
| 715 | |
} |
| 716 | 2055 | haveEncounteredContent = true; |
| 717 | |
} |
| 718 | |
|
| 719 | 2100 | } else { |
| 720 | |
|
| 721 | |
|
| 722 | |
|
| 723 | 8342 | boolean encodeEmptySegments = parserConfiguration.determineForcedEncodeIncludesTerserPath(nextTerserPath); |
| 724 | 8342 | String segString = encode((Segment) rep, encodingChars, parserConfiguration, nextTerserPath); |
| 725 | 8342 | if (segString.length() >= 4 || encodeEmptySegments) { |
| 726 | 6282 | result.append(segString); |
| 727 | |
|
| 728 | 6282 | if (segString.length() == 3) { |
| 729 | 5 | result.append(encodingChars.getFieldSeparator()); |
| 730 | |
} |
| 731 | |
|
| 732 | 6282 | result.append(SEGMENT_DELIMITER); |
| 733 | |
|
| 734 | 6282 | haveEncounteredContent = true; |
| 735 | |
|
| 736 | 6282 | if (nextNameIsRequired) { |
| 737 | 4939 | haveHadMandatorySegment = true; |
| 738 | |
} |
| 739 | |
|
| 740 | 6282 | if (!haveHadMandatorySegment && !haveEncounteredMandatorySegment) { |
| 741 | 425 | haveHadSegmentBeforeMandatorySegment = true; |
| 742 | |
} |
| 743 | |
|
| 744 | |
} |
| 745 | |
|
| 746 | |
} |
| 747 | |
|
| 748 | |
} |
| 749 | |
|
| 750 | |
} |
| 751 | |
|
| 752 | 4486 | if (firstMandatorySegmentName != null && !haveHadMandatorySegment && !haveHadSegmentBeforeMandatorySegment && haveEncounteredContent && parserConfiguration.isEncodeEmptyMandatorySegments()) { |
| 753 | 80 | return firstMandatorySegmentName.substring(0, 3) + encodingChars.getFieldSeparator() + SEGMENT_DELIMITER + result; |
| 754 | |
} else { |
| 755 | 4406 | return result.toString(); |
| 756 | |
} |
| 757 | |
} |
| 758 | |
|
| 759 | |
|
| 760 | |
|
| 761 | |
|
| 762 | |
|
| 763 | |
|
| 764 | |
|
| 765 | |
|
| 766 | |
public static PipeParser getInstanceWithNoValidation() { |
| 767 | 115 | HapiContext context = new DefaultHapiContext(); |
| 768 | 115 | context.setValidationContext(ValidationContextFactory.noValidation()); |
| 769 | 115 | return new PipeParser(context); |
| 770 | |
} |
| 771 | |
|
| 772 | |
|
| 773 | |
|
| 774 | |
|
| 775 | |
|
| 776 | |
|
| 777 | |
|
| 778 | |
|
| 779 | |
public static String encode(Segment source, EncodingCharacters encodingChars) { |
| 780 | 0 | return encode(source, encodingChars, source.getMessage().getParser().getParserConfiguration(), null); |
| 781 | |
} |
| 782 | |
|
| 783 | |
private static String encode(Segment source, EncodingCharacters encodingChars, ParserConfiguration parserConfig, String currentTerserPath) { |
| 784 | 13022 | StringBuilder result = new StringBuilder(); |
| 785 | 13022 | result.append(source.getName()); |
| 786 | 13022 | result.append(encodingChars.getFieldSeparator()); |
| 787 | |
|
| 788 | |
|
| 789 | |
|
| 790 | 13022 | int startAt = 1; |
| 791 | 13022 | if (isDelimDefSegment(source.getName())) |
| 792 | 2881 | startAt = 2; |
| 793 | |
|
| 794 | |
|
| 795 | |
|
| 796 | 13022 | int numFields = source.numFields(); |
| 797 | |
|
| 798 | 13022 | int forceUpToFieldNum = 0; |
| 799 | 13022 | if (parserConfig != null && currentTerserPath != null) { |
| 800 | 8342 | forceUpToFieldNum = parserConfig.determineForcedFieldNumForTerserPath(currentTerserPath); |
| 801 | |
} |
| 802 | 13022 | numFields = Math.max(numFields, forceUpToFieldNum); |
| 803 | |
|
| 804 | 326044 | for (int i = startAt; i <= numFields; i++) { |
| 805 | |
|
| 806 | 313022 | String nextFieldTerserPath = currentTerserPath + "-" + i; |
| 807 | 313022 | if (parserConfig != null && currentTerserPath != null) { |
| 808 | 209082 | for (String nextPath : parserConfig.getForcedEncode()) { |
| 809 | 22645 | if (nextPath.startsWith(nextFieldTerserPath + "-")) { |
| 810 | |
try { |
| 811 | 35 | source.getField(i, 0); |
| 812 | 0 | } catch (HL7Exception e) { |
| 813 | 0 | log.error("Error while encoding segment: ", e); |
| 814 | 35 | } |
| 815 | |
} |
| 816 | 22645 | } |
| 817 | |
} |
| 818 | |
|
| 819 | |
try { |
| 820 | 313022 | Type[] reps = source.getField(i); |
| 821 | 356297 | for (int j = 0; j < reps.length; j++) { |
| 822 | 43275 | String fieldText = encode(reps[j], encodingChars, parserConfig, nextFieldTerserPath); |
| 823 | |
|
| 824 | |
|
| 825 | 43275 | if (isDelimDefSegment(source.getName()) && i == 2) |
| 826 | 2881 | fieldText = parserConfig.getEscaping().unescape(fieldText, encodingChars); |
| 827 | 43275 | result.append(fieldText); |
| 828 | 43275 | if (j < reps.length - 1) |
| 829 | 485 | result.append(encodingChars.getRepetitionSeparator()); |
| 830 | |
} |
| 831 | 0 | } catch (HL7Exception e) { |
| 832 | 0 | log.error("Error while encoding segment: ", e); |
| 833 | 313022 | } |
| 834 | 313022 | result.append(encodingChars.getFieldSeparator()); |
| 835 | |
} |
| 836 | |
|
| 837 | |
|
| 838 | 13022 | char fieldSeparator = encodingChars.getFieldSeparator(); |
| 839 | 13022 | String retVal = stripExtraDelimiters(result.toString(), fieldSeparator); |
| 840 | |
|
| 841 | 13022 | int offset = isDelimDefSegment(source.getName()) ? 1 : 0; |
| 842 | 18667 | while (forceUpToFieldNum > 0 && (countInstancesOf(retVal, fieldSeparator) + offset) < forceUpToFieldNum) { |
| 843 | 5645 | retVal = retVal + fieldSeparator; |
| 844 | |
} |
| 845 | |
|
| 846 | 13022 | return retVal; |
| 847 | |
} |
| 848 | |
|
| 849 | |
private static int countInstancesOf(String theString, char theCharToSearchFor) { |
| 850 | 11320 | int retVal = 0; |
| 851 | 5270355 | for (int i = 0; i < theString.length(); i++) { |
| 852 | 5259035 | if (theString.charAt(i) == theCharToSearchFor) { |
| 853 | 5039655 | retVal++; |
| 854 | |
} |
| 855 | |
} |
| 856 | 11320 | return retVal; |
| 857 | |
} |
| 858 | |
|
| 859 | |
|
| 860 | |
|
| 861 | |
|
| 862 | |
|
| 863 | |
|
| 864 | |
|
| 865 | |
|
| 866 | |
|
| 867 | |
|
| 868 | |
|
| 869 | |
|
| 870 | |
public static String stripLeadingWhitespace(String in) { |
| 871 | 1300 | StringBuilder out = new StringBuilder(); |
| 872 | 1300 | char[] chars = in.toCharArray(); |
| 873 | 1300 | int c = 0; |
| 874 | 2990 | while (c < chars.length) { |
| 875 | 2830 | if (!Character.isWhitespace(chars[c])) |
| 876 | 1140 | break; |
| 877 | 1690 | c++; |
| 878 | |
} |
| 879 | 94480 | for (int i = c; i < chars.length; i++) { |
| 880 | 93180 | out.append(chars[i]); |
| 881 | |
} |
| 882 | 1300 | return out.toString(); |
| 883 | |
} |
| 884 | |
|
| 885 | |
|
| 886 | |
|
| 887 | |
|
| 888 | |
|
| 889 | |
|
| 890 | |
|
| 891 | |
|
| 892 | |
|
| 893 | |
|
| 894 | |
|
| 895 | |
|
| 896 | |
|
| 897 | |
|
| 898 | |
|
| 899 | |
|
| 900 | |
|
| 901 | |
|
| 902 | |
|
| 903 | |
|
| 904 | |
public Segment getCriticalResponseData(String message) throws HL7Exception { |
| 905 | |
|
| 906 | 70 | int locStartMSH = message.indexOf("MSH"); |
| 907 | 70 | if (locStartMSH < 0) |
| 908 | 0 | throw new HL7Exception("Couldn't find MSH segment in message: " + message, ErrorCode.SEGMENT_SEQUENCE_ERROR); |
| 909 | 70 | int locEndMSH = message.indexOf('\r', locStartMSH + 1); |
| 910 | 70 | if (locEndMSH < 0) |
| 911 | 0 | locEndMSH = message.length(); |
| 912 | 70 | String mshString = message.substring(locStartMSH, locEndMSH); |
| 913 | |
|
| 914 | |
|
| 915 | 70 | char fieldSep = mshString.charAt(3); |
| 916 | |
|
| 917 | |
|
| 918 | 70 | String[] fields = split(mshString, String.valueOf(fieldSep)); |
| 919 | |
|
| 920 | |
try { |
| 921 | |
|
| 922 | 70 | String encChars = fields[1]; |
| 923 | 70 | char compSep = encChars.charAt(0); |
| 924 | 70 | String messControlID = fields[9]; |
| 925 | 70 | String[] procIDComps = split(fields[10], String.valueOf(compSep)); |
| 926 | |
|
| 927 | |
|
| 928 | 70 | String version = null; |
| 929 | |
try { |
| 930 | 70 | version = getVersion(message); |
| 931 | 0 | } catch (Exception e) { |
| 932 | 70 | } |
| 933 | |
|
| 934 | 70 | if (version == null) { |
| 935 | 0 | Version availableVersion = Version.highestAvailableVersionOrDefault(); |
| 936 | 0 | version = availableVersion.getVersion(); |
| 937 | |
} |
| 938 | |
|
| 939 | 70 | Segment msh = Parser.makeControlMSH(version, getFactory()); |
| 940 | 70 | Terser.set(msh, 1, 0, 1, 1, String.valueOf(fieldSep)); |
| 941 | 70 | Terser.set(msh, 2, 0, 1, 1, encChars); |
| 942 | 70 | Terser.set(msh, 10, 0, 1, 1, messControlID); |
| 943 | 70 | Terser.set(msh, 11, 0, 1, 1, procIDComps[0]); |
| 944 | 70 | Terser.set(msh, 12, 0, 1, 1, version); |
| 945 | 70 | return msh; |
| 946 | |
|
| 947 | 0 | } catch (Exception e) { |
| 948 | 0 | throw new HL7Exception("Can't parse critical fields from MSH segment (" + e.getClass().getName() + ": " + e.getMessage() + "): " + mshString, ErrorCode.REQUIRED_FIELD_MISSING, e); |
| 949 | |
} |
| 950 | |
|
| 951 | |
} |
| 952 | |
|
| 953 | |
|
| 954 | |
|
| 955 | |
|
| 956 | |
|
| 957 | |
|
| 958 | |
|
| 959 | |
|
| 960 | |
|
| 961 | |
|
| 962 | |
public String getAckID(String message) { |
| 963 | 1216 | String ackID = null; |
| 964 | 1216 | int startMSA = message.indexOf("\rMSA"); |
| 965 | 1216 | if (startMSA >= 0) { |
| 966 | 578 | int startFieldOne = startMSA + 5; |
| 967 | 578 | char fieldDelim = message.charAt(startFieldOne - 1); |
| 968 | 578 | int start = message.indexOf(fieldDelim, startFieldOne) + 1; |
| 969 | 578 | int end = message.indexOf(fieldDelim, start); |
| 970 | 578 | int segEnd = message.indexOf(String.valueOf(SEGMENT_DELIMITER), start); |
| 971 | 578 | if (segEnd > start && segEnd < end) |
| 972 | 5 | end = segEnd; |
| 973 | |
|
| 974 | |
|
| 975 | |
|
| 976 | 578 | if (end < 0) { |
| 977 | 318 | if (message.charAt(message.length() - 1) == '\r') { |
| 978 | 318 | end = message.length() - 1; |
| 979 | |
} else { |
| 980 | 0 | end = message.length(); |
| 981 | |
} |
| 982 | |
} |
| 983 | 578 | if (start > 0 && end > start) { |
| 984 | 578 | ackID = message.substring(start, end); |
| 985 | |
} |
| 986 | |
} |
| 987 | 1216 | log.trace("ACK ID: {}", ackID); |
| 988 | 1216 | return ackID; |
| 989 | |
} |
| 990 | |
|
| 991 | |
|
| 992 | |
|
| 993 | |
|
| 994 | |
|
| 995 | |
|
| 996 | |
|
| 997 | |
public void setLegacyMode(boolean legacyMode) { |
| 998 | 5 | this.myLegacyMode = legacyMode; |
| 999 | 5 | } |
| 1000 | |
|
| 1001 | |
|
| 1002 | |
|
| 1003 | |
|
| 1004 | |
@Override |
| 1005 | |
public String encode(Message source) throws HL7Exception { |
| 1006 | 1841 | if (myLegacyMode != null && myLegacyMode) { |
| 1007 | |
|
| 1008 | |
@SuppressWarnings("deprecation") |
| 1009 | 0 | OldPipeParser oldPipeParser = new OldPipeParser(getFactory()); |
| 1010 | |
|
| 1011 | 0 | return oldPipeParser.encode(source); |
| 1012 | |
} |
| 1013 | 1841 | return super.encode(source); |
| 1014 | |
} |
| 1015 | |
|
| 1016 | |
|
| 1017 | |
|
| 1018 | |
|
| 1019 | |
@Override |
| 1020 | |
public Message parse(String message) throws HL7Exception { |
| 1021 | 2066 | if (myLegacyMode != null && myLegacyMode) { |
| 1022 | |
|
| 1023 | |
@SuppressWarnings("deprecation") |
| 1024 | 5 | OldPipeParser oldPipeParser = new OldPipeParser(getFactory()); |
| 1025 | |
|
| 1026 | 5 | return oldPipeParser.parse(message); |
| 1027 | |
} |
| 1028 | 2061 | return super.parse(message); |
| 1029 | |
} |
| 1030 | |
|
| 1031 | |
|
| 1032 | |
|
| 1033 | |
|
| 1034 | |
|
| 1035 | |
|
| 1036 | |
|
| 1037 | |
|
| 1038 | |
|
| 1039 | |
|
| 1040 | |
|
| 1041 | |
|
| 1042 | |
|
| 1043 | |
|
| 1044 | |
|
| 1045 | |
|
| 1046 | |
|
| 1047 | |
|
| 1048 | |
|
| 1049 | |
|
| 1050 | |
|
| 1051 | |
|
| 1052 | |
|
| 1053 | |
|
| 1054 | |
|
| 1055 | |
public boolean isLegacyMode() { |
| 1056 | 0 | if (myLegacyMode == null) |
| 1057 | 0 | return (Boolean.parseBoolean(System.getProperty(DEFAULT_LEGACY_MODE_PROPERTY))); |
| 1058 | 0 | return this.myLegacyMode; |
| 1059 | |
} |
| 1060 | |
|
| 1061 | |
|
| 1062 | |
|
| 1063 | |
|
| 1064 | |
|
| 1065 | |
|
| 1066 | |
|
| 1067 | |
|
| 1068 | |
|
| 1069 | |
|
| 1070 | |
public String getVersion(String message) throws HL7Exception { |
| 1071 | 2911 | int startMSH = message.indexOf("MSH"); |
| 1072 | 2911 | int endMSH = message.indexOf(PipeParser.SEGMENT_DELIMITER, startMSH); |
| 1073 | 2911 | if (endMSH < 0) |
| 1074 | 0 | endMSH = message.length(); |
| 1075 | 2911 | String msh = message.substring(startMSH, endMSH); |
| 1076 | |
String fieldSep; |
| 1077 | 2911 | if (msh.length() > 3) { |
| 1078 | 2911 | fieldSep = String.valueOf(msh.charAt(3)); |
| 1079 | |
} else { |
| 1080 | 0 | throw new HL7Exception("Can't find field separator in MSH: " + msh, ErrorCode.UNSUPPORTED_VERSION_ID); |
| 1081 | |
} |
| 1082 | |
|
| 1083 | 2911 | String[] fields = split(msh, fieldSep); |
| 1084 | |
|
| 1085 | |
String compSep; |
| 1086 | 2911 | if (fields.length >= 2 && fields[1] != null && (fields[1].length() == 4 || fields[1].length() == 5)) { |
| 1087 | 2896 | compSep = String.valueOf(fields[1].charAt(0)); |
| 1088 | |
} else { |
| 1089 | 15 | throw new HL7Exception("Invalid or incomplete encoding characters - MSH-2 is " + fields[1], ErrorCode.REQUIRED_FIELD_MISSING); |
| 1090 | |
} |
| 1091 | |
|
| 1092 | |
String version; |
| 1093 | 2896 | if (fields.length >= 12) { |
| 1094 | 2896 | String[] comp = split(fields[11], compSep); |
| 1095 | 2896 | if (comp.length >= 1) { |
| 1096 | 2896 | version = comp[0]; |
| 1097 | |
} else { |
| 1098 | 0 | throw new HL7Exception("Can't find version ID - MSH.12 is " + fields[11], ErrorCode.REQUIRED_FIELD_MISSING); |
| 1099 | |
} |
| 1100 | 2896 | } else if (getParserConfiguration().isAllowUnknownVersions()) { |
| 1101 | 0 | return Version.highestAvailableVersionOrDefault().getVersion(); |
| 1102 | |
} else { |
| 1103 | 0 | throw new HL7Exception("Can't find version ID - MSH has only " + fields.length + " fields.", ErrorCode.REQUIRED_FIELD_MISSING); |
| 1104 | |
} |
| 1105 | 2896 | return version; |
| 1106 | |
} |
| 1107 | |
|
| 1108 | |
@Override |
| 1109 | |
public void parse(Message message, String string) throws HL7Exception { |
| 1110 | 3391 | if (message instanceof AbstractSuperMessage && message.getName() == null) { |
| 1111 | 245 | String structure = getStructure(string).messageStructure; |
| 1112 | 245 | ((AbstractSuperMessage) message).setName(structure); |
| 1113 | |
} |
| 1114 | |
|
| 1115 | 3391 | IStructureDefinition structureDef = getStructureDefinition(message); |
| 1116 | 3391 | MessageIterator messageIter = new MessageIterator(message, structureDef, "MSH", true); |
| 1117 | |
|
| 1118 | 3391 | String[] segments = split(string, SEGMENT_DELIMITER); |
| 1119 | |
|
| 1120 | 3391 | if (segments.length == 0) { |
| 1121 | 5 | throw new HL7Exception("Invalid message content: \"" + string + "\""); |
| 1122 | |
} |
| 1123 | |
|
| 1124 | 3386 | if (segments[0] == null || segments[0].length() < 4) { |
| 1125 | 35 | throw new HL7Exception("Invalid message content: \"" + string + "\""); |
| 1126 | |
} |
| 1127 | |
|
| 1128 | 3351 | char delim = '|'; |
| 1129 | 3351 | String prevName = null; |
| 1130 | 3351 | int repNum = 1; |
| 1131 | 15380 | for (int i = 0; i < segments.length; i++) { |
| 1132 | |
|
| 1133 | |
|
| 1134 | 12086 | if (segments[i] != null && segments[i].length() > 0 && Character.isWhitespace(segments[i].charAt(0))) |
| 1135 | 705 | segments[i] = stripLeadingWhitespace(segments[i]); |
| 1136 | |
|
| 1137 | |
|
| 1138 | 12086 | if (segments[i] != null && segments[i].length() >= 3) { |
| 1139 | |
|
| 1140 | |
final String name; |
| 1141 | 11856 | if (i == 0) { |
| 1142 | 3351 | if (segments[i].length() < 4) { |
| 1143 | 0 | throw new HL7Exception("Invalid message content: \"" + string + "\""); |
| 1144 | |
} |
| 1145 | 3351 | name = segments[i].substring(0, 3); |
| 1146 | 3351 | delim = segments[i].charAt(3); |
| 1147 | |
} else { |
| 1148 | 8505 | if (segments[i].indexOf(delim) >= 0) { |
| 1149 | 8495 | name = segments[i].substring(0, segments[i].indexOf(delim)); |
| 1150 | |
} else { |
| 1151 | 10 | name = segments[i]; |
| 1152 | |
} |
| 1153 | |
} |
| 1154 | |
|
| 1155 | 11856 | log.trace("Parsing segment {}", name); |
| 1156 | |
|
| 1157 | 11856 | if (name.equals(prevName)) { |
| 1158 | 965 | repNum++; |
| 1159 | |
} else { |
| 1160 | 10891 | repNum = 1; |
| 1161 | 10891 | prevName = name; |
| 1162 | |
} |
| 1163 | |
|
| 1164 | 11856 | messageIter.setDirection(name); |
| 1165 | |
|
| 1166 | |
try { |
| 1167 | 11856 | if (messageIter.hasNext()) { |
| 1168 | 11849 | Segment next = (Segment) messageIter.next(); |
| 1169 | 11849 | parse(next, segments[i], getEncodingChars(string), repNum); |
| 1170 | |
} |
| 1171 | 5 | } catch (Error e) { |
| 1172 | 5 | if (e.getCause() instanceof HL7Exception) { |
| 1173 | 5 | throw (HL7Exception)e.getCause(); |
| 1174 | |
} |
| 1175 | 0 | throw e; |
| 1176 | 11799 | } |
| 1177 | |
} |
| 1178 | |
} |
| 1179 | |
|
| 1180 | 3294 | applySuperStructureName(message); |
| 1181 | 3294 | } |
| 1182 | |
|
| 1183 | |
|
| 1184 | |
|
| 1185 | |
|
| 1186 | |
|
| 1187 | |
private static class MessageStructure { |
| 1188 | |
public String messageStructure; |
| 1189 | |
public boolean explicitlyDefined; |
| 1190 | |
|
| 1191 | 3046 | public MessageStructure(String theMessageStructure, boolean isExplicitlyDefined) { |
| 1192 | 3046 | messageStructure = theMessageStructure; |
| 1193 | 3046 | explicitlyDefined = isExplicitlyDefined; |
| 1194 | 3046 | } |
| 1195 | |
} |
| 1196 | |
|
| 1197 | 2806 | private static class Holder<T> { |
| 1198 | |
private T myObject; |
| 1199 | |
|
| 1200 | |
public T getObject() { |
| 1201 | 42243 | return myObject; |
| 1202 | |
} |
| 1203 | |
|
| 1204 | |
public void setObject(T theObject) { |
| 1205 | 21823 | myObject = theObject; |
| 1206 | 21823 | } |
| 1207 | |
} |
| 1208 | |
|
| 1209 | |
} |