| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package ca.uhn.hl7v2.preparser; |
| 5 | |
|
| 6 | |
import java.util.Arrays; |
| 7 | |
import java.util.List; |
| 8 | |
import java.util.Properties; |
| 9 | |
import java.util.StringTokenizer; |
| 10 | |
|
| 11 | |
import ca.uhn.hl7v2.HL7Exception; |
| 12 | |
import ca.uhn.hl7v2.parser.EncodingDetector; |
| 13 | |
import ca.uhn.hl7v2.util.Terser; |
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | 0 | public class PreParser { |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public static String[] getFields(String theMessageText, String... thePathSpecs) throws HL7Exception { |
| 42 | 425 | DatumPath[] paths = new DatumPath[thePathSpecs.length]; |
| 43 | 1150 | for (int i = 0; i < thePathSpecs.length; i++) { |
| 44 | 725 | StringTokenizer tok = new StringTokenizer(thePathSpecs[i], "-", false); |
| 45 | 725 | String segSpec = tok.nextToken(); |
| 46 | 725 | tok = new StringTokenizer(segSpec, "()", false); |
| 47 | 725 | String segName = tok.nextToken(); |
| 48 | 725 | if (segName.length() != 3) { |
| 49 | 0 | throw new HL7Exception("In field path, " + segName + " is not a valid segment name"); |
| 50 | |
} |
| 51 | 725 | int segRep = 0; |
| 52 | 725 | if (tok.hasMoreTokens()) { |
| 53 | 10 | String rep = tok.nextToken(); |
| 54 | |
try { |
| 55 | 10 | segRep = Integer.parseInt(rep); |
| 56 | 0 | } catch (NumberFormatException e) { |
| 57 | 0 | throw new HL7Exception("In field path, segment rep" + rep + " is not valid", e); |
| 58 | 10 | } |
| 59 | |
} |
| 60 | |
|
| 61 | 725 | int[] indices = Terser.getIndices(thePathSpecs[i]); |
| 62 | 725 | paths[i] = new DatumPath(); |
| 63 | 725 | paths[i].add(segName).add(segRep); |
| 64 | 725 | paths[i].add(indices[0]).add(indices[1]).add(indices[2]).add(indices[3]); |
| 65 | |
|
| 66 | |
} |
| 67 | 425 | return getFields(theMessageText, paths); |
| 68 | |
} |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
private static String[] getFields(String theMessageText, DatumPath[] thePaths) throws HL7Exception { |
| 75 | 425 | String[] fields = new String[thePaths.length]; |
| 76 | 425 | Properties props = new Properties(); |
| 77 | |
|
| 78 | 425 | List<DatumPath> mask = Arrays.asList(thePaths); |
| 79 | |
|
| 80 | 425 | boolean OK = false; |
| 81 | 425 | if (EncodingDetector.isEr7Encoded(theMessageText)) { |
| 82 | 385 | OK = ER7.parseMessage(props, mask, theMessageText); |
| 83 | 40 | } else if (EncodingDetector.isXmlEncoded(theMessageText)) { |
| 84 | 35 | OK = XML.parseMessage(props, theMessageText, null); |
| 85 | |
} else { |
| 86 | 5 | throw new HL7Exception("Message encoding is not recognized"); |
| 87 | |
} |
| 88 | |
|
| 89 | 420 | if (!OK) { |
| 90 | 5 | throw new HL7Exception("Parse failed"); |
| 91 | |
} |
| 92 | |
|
| 93 | 1115 | for (int i = 0; i < fields.length; i++) { |
| 94 | 700 | fields[i] = props.getProperty(thePaths[i].toString()); |
| 95 | |
} |
| 96 | 415 | return fields; |
| 97 | |
} |
| 98 | |
|
| 99 | |
} |