/* * Created on 28-Apr-2004 */ package ca.uhn.hl7v2.preparser; import java.util.Properties; import java.util.StringTokenizer; import java.util.Vector; import ca.uhn.hl7v2.HL7Exception; import ca.uhn.hl7v2.parser.GenericParser; import ca.uhn.hl7v2.util.Terser; /** *

Extracts specified fields from unparsed messages. This class is a * facade for the ER7 and XML classes. Use it like this:

* * * String message = null; //... your ER7 or XML message string goes here * String[] fieldSpecs = {"MSH-9-1", "MSH-9-2", "MSH-12"}; * String[] fields = PreParser.getFields(message, fieldSpecs); * * * @author Bryan Tripp * @version $Revision: 1.1 $ updated on $Date: 2007/02/19 02:24:37 $ by $Author: jamesagnew $ */ public class PreParser { private static GenericParser ourParser = new GenericParser(); /** * Extracts selected fields from a message. * * @param theMessageText an unparsed message from which to get fields * @param thePathSpecs Terser-like paths to fields in the message. See documentation * for Terser. These paths are identical except that they start with the segment * name (search flags and group names are to be omitted as they are not relevant * with unparsed ER7 messages). * @return field values corresponding to the given paths * @throws HL7Exception */ public static String[] getFields(String theMessageText, String[] thePathSpecs) throws HL7Exception { DatumPath[] paths = new DatumPath[thePathSpecs.length]; for (int i = 0; i < thePathSpecs.length; i++) { StringTokenizer tok = new StringTokenizer(thePathSpecs[i], "-", false); String segSpec = tok.nextToken(); tok = new StringTokenizer(segSpec, "()", false); String segName = tok.nextToken(); if (segName.length() != 3) { throw new HL7Exception("In field path, " + segName + " is not a valid segment name"); } int segRep = 0; if (tok.hasMoreTokens()) { String rep = tok.nextToken(); try { segRep = Integer.parseInt(rep); } catch (NumberFormatException e) { throw new HL7Exception("In field path, segment rep" + rep + " is not valid", e); } } int[] indices = Terser.getIndices(thePathSpecs[i]); paths[i] = new DatumPath(); paths[i].add(segName).add(segRep); paths[i].add(indices[0]).add(indices[1]).add(indices[2]).add(indices[3]); } return getFields(theMessageText, paths); } /** * Gets selected fields from a message, as with String[] arg version but * using DatumPaths. */ private static String[] getFields(String theMessageText, DatumPath[] thePaths) throws HL7Exception { String[] fields = new String[thePaths.length]; String encoding = ourParser.getEncoding(theMessageText); Properties props = new Properties(); Vector mask = new Vector(); for (int i = 0; i < thePaths.length; i++) { mask.add(thePaths[i]); } if (encoding == null) { throw new HL7Exception("Message encoding is not recognized"); } boolean OK = false; if (encoding.equals("VB")) { OK = ER7.parseMessage(props, mask, theMessageText); } else if (encoding.equals("XML")) { OK = XML.parseMessage(props, theMessageText, null); } if (!OK) { throw new HL7Exception("Parse failed"); } for (int i = 0; i < fields.length; i++) { fields[i] = props.getProperty(thePaths[i].toString()); } return fields; } }