Coverage Report - ca.uhn.hl7v2.preparser.PreParser
 
Classes in this File Line Coverage Branch Coverage Complexity
PreParser
88%
31/35
92%
13/14
7
 
 1  
 /*
 2  
  * Created on 28-Apr-2004
 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  
  * <p>Extracts specified fields from unparsed messages.  This class is a 
 17  
  * facade for the ER7 and XML classes.  Use it like this: </p>
 18  
  * 
 19  
  * <code>
 20  
  * String message = null; //... your ER7 or XML message string goes here
 21  
  * String[] fieldSpecs = {"MSH-9-1", "MSH-9-2", "MSH-12"};
 22  
  * String[] fields = PreParser.getFields(message, fieldSpecs);
 23  
  * </code>
 24  
  * 
 25  
  * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
 26  
  * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:37 $ by $Author: jamesagnew $
 27  
  */
 28  0
 public class PreParser {
 29  
 
 30  
     /**
 31  
      * Extracts selected fields from a message.  
 32  
      *   
 33  
      * @param theMessageText an unparsed message from which to get fields 
 34  
      * @param thePathSpecs Terser-like paths to fields in the message.  See documentation
 35  
      *      for Terser.  These paths are identical except that they start with the segment
 36  
      *      name (search flags and group names are to be omitted as they are not relevant 
 37  
      *      with unparsed ER7 messages).  
 38  
      * @return field values corresponding to the given paths
 39  
      * @throws HL7Exception
 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  
      * Gets selected fields from a message, as with String[] arg version but 
 72  
      * using DatumPaths. 
 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  
 }