Coverage Report - ca.uhn.hl7v2.parser.EncodingCharacters
 
Classes in this File Line Coverage Branch Coverage Complexity
EncodingCharacters
74%
37/50
60%
12/20
1.632
 
 1  
 /**
 2  
 The contents of this file are subject to the Mozilla Public License Version 1.1
 3  
 (the "License"); you may not use this file except in compliance with the License.
 4  
 You may obtain a copy of the License at http://www.mozilla.org/MPL/
 5  
 Software distributed under the License is distributed on an "AS IS" basis,
 6  
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
 7  
 specific language governing rights and limitations under the License.
 8  
 
 9  
 The Original Code is "GroupPointer.java".  Description:
 10  
 "A GroupPointer is used when parsing traditionally encoded HL7 messages"
 11  
 
 12  
 The Initial Developer of the Original Code is University Health Network. Copyright (C)
 13  
 2001.  All Rights Reserved.
 14  
 
 15  
 Contributor(s): ______________________________________.
 16  
 
 17  
 Alternatively, the contents of this file may be used under the terms of the
 18  
 GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
 19  
 applicable instead of those above.  If you wish to allow use of your version of this
 20  
 file only under the terms of the GPL and not to allow others to use your version
 21  
 of this file under the MPL, indicate your decision by deleting  the provisions above
 22  
 and replace  them with the notice and other provisions required by the GPL License.
 23  
 If you do not delete the provisions above, a recipient may use your version of
 24  
 this file under either the MPL or the GPL.
 25  
 
 26  
 */
 27  
 
 28  
 
 29  
 
 30  
 package ca.uhn.hl7v2.parser;
 31  
 
 32  
 import ca.uhn.hl7v2.HL7Exception;
 33  
 import ca.uhn.hl7v2.model.Message;
 34  
 
 35  
 
 36  
 
 37  
 /**
 38  
  * Represents the set of special characters used to encode traditionally
 39  
  * encoded HL7 messages.
 40  
  *
 41  
  * @author Bryan Tripp (bryan_tripp@sourceforge.net)
 42  
  */
 43  
 
 44  
 public class EncodingCharacters implements Cloneable {
 45  
     
 46  
     private char fieldSep;
 47  
     private char[] encChars;
 48  
     
 49  
     /**
 50  
      * Creates new EncodingCharacters object with the given character
 51  
      * values. If the encodingCharacters argument is null, the default
 52  
      * values are used.
 53  
      *
 54  
      * @param fieldSeparator field seperator
 55  
      * @param encodingCharacters consists of the characters that appear in
 56  
      *      MSH-2 (see section 2.8 of the HL7 spec).  The characters are
 57  
      *      Component Separator, Repetition Separator, Escape Character, and
 58  
      *      Subcomponent Separator (in that order).
 59  
      */
 60  
     
 61  3694
     public EncodingCharacters(char fieldSeparator, String encodingCharacters) {
 62  3694
         this.fieldSep = fieldSeparator;
 63  3694
         this.encChars = new char[4];
 64  
         
 65  3694
         if (encodingCharacters == null) {
 66  18
             this.encChars[0] = '^';            
 67  18
             this.encChars[1] = '~';            
 68  18
             this.encChars[2] = '\\';
 69  18
             this.encChars[3] = '&';
 70  
         } else {
 71  3676
             encodingCharacters.getChars(0, 4, this.encChars, 0);
 72  
         }
 73  
         
 74  3694
     }
 75  
 
 76  
     /**
 77  
      * Returns an instance using the MSH-1 and MSH-2 values of the given message
 78  
      *
 79  
      * @param message the message
 80  
      * @return the encoding characters for this message
 81  
      * @throws HL7Exception If either MSH-1 or MSH-2 are not populated
 82  
      * @since 1.0
 83  
      */
 84  
     public static EncodingCharacters getInstance(Message message) throws HL7Exception {
 85  
 
 86  596
         final String encodingCharactersValue = message.getEncodingCharactersValue();
 87  596
         if (encodingCharactersValue == null || encodingCharactersValue.length() == 0) {
 88  1
             throw new HL7Exception("encoding characters not populated");
 89  
         }
 90  
 
 91  595
         final Character fieldSeparatorValue = message.getFieldSeparatorValue();
 92  595
         if (fieldSeparatorValue == null) {
 93  0
             throw new HL7Exception("Field separator not populated");
 94  
         }
 95  
 
 96  595
         return new EncodingCharacters(fieldSeparatorValue, encodingCharactersValue);
 97  
     }
 98  
 
 99  
     
 100  
     
 101  
     public EncodingCharacters(char fieldSeparator, char componentSeparator, char repetitionSeparator,
 102  
                               char escapeCharacter, char subcomponentSeparator) {
 103  0
         this(fieldSeparator, String.valueOf(componentSeparator) + repetitionSeparator + escapeCharacter + subcomponentSeparator);
 104  0
     }
 105  
     
 106  
     
 107  
     
 108  
     /** copies contents of "other" */
 109  
     
 110  0
     public EncodingCharacters(EncodingCharacters other) {
 111  0
         this.fieldSep = other.getFieldSeparator();
 112  0
         this.encChars = new char[4];
 113  0
         this.encChars[0] = other.getComponentSeparator();
 114  0
         this.encChars[1] = other.getRepetitionSeparator();
 115  0
         this.encChars[2] = other.getEscapeCharacter();
 116  0
         this.encChars[3] = other.getSubcomponentSeparator();
 117  0
     }
 118  
     
 119  
     /**
 120  
      * Returns the field separator.
 121  
      *
 122  
      * @return the field separator
 123  
      */
 124  
     public char getFieldSeparator() {
 125  168043
         return this.fieldSep;
 126  
     }
 127  
     
 128  
     /**
 129  
      * Returns the component separator.
 130  
      *
 131  
      * @return the component separator
 132  
      */
 133  
     public char getComponentSeparator() {
 134  74567
         return this.encChars[0];
 135  
     }
 136  
     
 137  
     /**
 138  
      * Returns the repetition separator.
 139  
      *
 140  
      * @return the repetition separator
 141  
      */
 142  
     public char getRepetitionSeparator() {
 143  56901
         return this.encChars[1];
 144  
     }
 145  
     
 146  
     /**
 147  
      * Returns the escape character.
 148  
      *
 149  
      * @return the escape character
 150  
      */
 151  
     public char getEscapeCharacter() {
 152  45830
         return this.encChars[2];
 153  
     }
 154  
     
 155  
     /**
 156  
      * Returns the subcomponent separator.
 157  
      *
 158  
      * @return the subcomponent separator
 159  
      */
 160  
     public char getSubcomponentSeparator() {
 161  113313
         return this.encChars[3];
 162  
     }
 163  
     
 164  
     /**
 165  
      * Returns the encoding characters (not including field separator)
 166  
      * as a string.
 167  
      */
 168  
     public String toString() {
 169  58
         return String.valueOf(encChars);
 170  
     }
 171  
     
 172  
     public Object clone() throws CloneNotSupportedException
 173  
     {
 174  0
         return new EncodingCharacters(this);
 175  
     }
 176  
     
 177  
     public void setFieldSeparator(char newFieldSep) {
 178  58
         this.fieldSep = newFieldSep;
 179  58
     }
 180  
     
 181  
     public void setComponentSeparator(char newComponentSep) {
 182  58
         this.encChars[0] = newComponentSep;
 183  58
     }
 184  
     
 185  
     public void setRepetitionSeparator(char newRepetitionSep) {
 186  58
         this.encChars[1] = newRepetitionSep;
 187  58
     }
 188  
     
 189  
     public void setEscapeCharacter(char newEscapeChar) {
 190  58
         this.encChars[2] = newEscapeChar;
 191  58
     }
 192  
     
 193  
     public void setSubcomponentSeparator(char newSubcomponentSep) {
 194  58
         this.encChars[3] = newSubcomponentSep;
 195  58
     }
 196  
     
 197  
     /** @see java.lang.Object#equals */
 198  
     public boolean equals(Object o) {
 199  9353
         if (o instanceof EncodingCharacters) {
 200  9353
             EncodingCharacters other = (EncodingCharacters) o;
 201  9353
             return (this.getFieldSeparator() == other.getFieldSeparator()
 202  
                 && this.getComponentSeparator() == other.getComponentSeparator()
 203  
                 && this.getEscapeCharacter() == other.getEscapeCharacter() 
 204  
                 && this.getRepetitionSeparator() == other.getRepetitionSeparator()
 205  
                 && this.getSubcomponentSeparator() == other.getSubcomponentSeparator());
 206  
         } else {
 207  0
             return false;
 208  
         }   
 209  
     }
 210  
     
 211  
     /** @see java.lang.Object#hashCode */
 212  
     public int hashCode() {
 213  9521
         return 7 * (int) this.getComponentSeparator()
 214  
             * (int) this.getEscapeCharacter()
 215  
             * (int) this.getFieldSeparator()
 216  
             * (int) this.getRepetitionSeparator()
 217  
             * (int) this.getSubcomponentSeparator();
 218  
     }
 219  
 
 220  
     /**
 221  
      * Returns an instance of encoding characters with the standard ER7 encoding characters
 222  
      * defined: |^~\&
 223  
      *
 224  
      * @return a default instance of encoding characters
 225  
      */
 226  
         public static EncodingCharacters defaultInstance() {
 227  1
                 return new EncodingCharacters('|', null);
 228  
         }        
 229  
     
 230  
     /**
 231  
      *
 232  
      * Test harness ...
 233  
      *
 234  
      */
 235  
     
 236  
     /*
 237  
      
 238  
     public static void main(String args[]) {
 239  
      
 240  
         String testChars = "^~\\&";
 241  
      
 242  
         String testChars2 = "$%*+";
 243  
      
 244  
      
 245  
      
 246  
         EncodingCharacters ec = new EncodingCharacters('|', testChars);
 247  
      
 248  
         System.out.println("test 1: " + ec.getFieldSeparator() + ec.toString());
 249  
      
 250  
         ec = new EncodingCharacters('|', testChars2);
 251  
      
 252  
         System.out.println("test 2: " + ec.getFieldSeparator() + ec.getComponentSeparator() + ec.getRepetitionSeparator() + ec.getEscapeCharacter() + ec.getSubcomponentSeparator());
 253  
      
 254  
         ec = new EncodingCharacters('[', null);
 255  
      
 256  
         System.out.println("test 3: " + ec.getFieldSeparator() + ec.toString());
 257  
      
 258  
     }*/
 259  
     
 260  
 }
 261