Coverage Report - ca.uhn.hl7v2.parser.EncodingCharacters
 
Classes in this File Line Coverage Branch Coverage Complexity
EncodingCharacters
76%
55/72
69%
18/26
1.682
 
 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 "EncodingCharacters.java".  Description:
 10  
 "Represents the set of special characters used to encode 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  
 package ca.uhn.hl7v2.parser;
 28  
 
 29  
 import ca.uhn.hl7v2.HL7Exception;
 30  
 import ca.uhn.hl7v2.model.Message;
 31  
 
 32  
 /**
 33  
  * Represents the set of special characters used to encode traditionally
 34  
  * encoded HL7 messages.
 35  
  *
 36  
  * @author Bryan Tripp (bryan_tripp@sourceforge.net)
 37  
  */
 38  
 
 39  
 public class EncodingCharacters implements Cloneable {
 40  
     
 41  
     private char fieldSep;
 42  
     private char[] encChars;
 43  
     
 44  
     /**
 45  
      * Creates new EncodingCharacters object with the given character
 46  
      * values. If the encodingCharacters argument is null, the default
 47  
      * values are used.
 48  
      *
 49  
      * @param fieldSeparator field seperator
 50  
      * @param encodingCharacters consists of the characters that appear in
 51  
      *      MSH-2 (see section 2.8 of the HL7 spec).  The characters are
 52  
      *      Component Separator, Repetition Separator, Escape Character,
 53  
      *      Subcomponent Separator, and, as of v2.7, the Truncation Character 
 54  
      *      (in that order).
 55  
      */
 56  
     
 57  27586
     public EncodingCharacters(char fieldSeparator, String encodingCharacters) {
 58  27586
         this.fieldSep = fieldSeparator;
 59  27586
         this.encChars = new char[5];
 60  
         
 61  27586
         if (encodingCharacters == null) {
 62  95
             setComponentSeparator('^');            
 63  95
             setRepetitionSeparator('~');            
 64  95
             setEscapeCharacter('\\');
 65  95
             setSubcomponentSeparator('&');
 66  95
             setTruncationCharacter('#');
 67  
         } else {
 68  27491
             encodingCharacters.getChars(0, 4, this.encChars, 0);
 69  
             // Add truncation character if available
 70  27491
             if (encodingCharacters.length() > 4) {
 71  14890
                 char extraChar = encodingCharacters.charAt(4);
 72  14890
                 if (extraChar != fieldSeparator) {
 73  215
                     setTruncationCharacter(extraChar);
 74  
                 }
 75  
             }
 76  
         }
 77  
         
 78  27586
     }
 79  
 
 80  
     /**
 81  
      * Returns an instance using the MSH-1 and MSH-2 values of the given message
 82  
      *
 83  
      * @param message the message
 84  
      * @return the encoding characters for this message
 85  
      * @throws HL7Exception If either MSH-1 or MSH-2 are not populated
 86  
      * @since 1.0
 87  
      */
 88  
     public static EncodingCharacters getInstance(Message message) throws HL7Exception {
 89  
 
 90  9805
         final String encodingCharactersValue = message.getEncodingCharactersValue();
 91  9805
         if (encodingCharactersValue == null || encodingCharactersValue.length() == 0) {
 92  5
             throw new HL7Exception("encoding characters not populated");
 93  
         }
 94  
 
 95  9800
         final Character fieldSeparatorValue = message.getFieldSeparatorValue();
 96  9800
         if (fieldSeparatorValue == null) {
 97  0
             throw new HL7Exception("Field separator not populated");
 98  
         }
 99  
 
 100  9800
         return new EncodingCharacters(fieldSeparatorValue, encodingCharactersValue);
 101  
     }
 102  
 
 103  
     
 104  
     
 105  
     public EncodingCharacters(char fieldSeparator, char componentSeparator, char repetitionSeparator,
 106  
                               char escapeCharacter, char subcomponentSeparator) {
 107  0
         this(fieldSeparator, String.valueOf(componentSeparator) + repetitionSeparator + 
 108  
                 escapeCharacter + subcomponentSeparator);
 109  0
     }
 110  
 
 111  
     public EncodingCharacters(char fieldSeparator, char componentSeparator, char repetitionSeparator,
 112  
                               char escapeCharacter, char subcomponentSeparator, char truncationCharacter) {
 113  0
         this(fieldSeparator, String.valueOf(componentSeparator) + repetitionSeparator + 
 114  
                 escapeCharacter + subcomponentSeparator + truncationCharacter);
 115  0
     }    
 116  
     
 117  
     /** copies contents of "other" */
 118  
     
 119  0
     public EncodingCharacters(EncodingCharacters other) {
 120  0
         this.fieldSep = other.getFieldSeparator();
 121  0
         this.encChars = new char[5];
 122  0
         setComponentSeparator(other.getComponentSeparator());
 123  0
         setRepetitionSeparator(other.getRepetitionSeparator());
 124  0
         setEscapeCharacter(other.getEscapeCharacter());
 125  0
         setSubcomponentSeparator(other.getSubcomponentSeparator());
 126  0
         setTruncationCharacter(other.getTruncationCharacter());
 127  0
     }
 128  
     
 129  
     /**
 130  
      * Returns the field separator.
 131  
      *
 132  
      * @return the field separator
 133  
      */
 134  
     public char getFieldSeparator() {
 135  544602
         return this.fieldSep;
 136  
     }
 137  
     
 138  
     /**
 139  
      * Returns the component separator.
 140  
      *
 141  
      * @return the component separator
 142  
      */
 143  
     public char getComponentSeparator() {
 144  467430
         return this.encChars[0];
 145  
     }
 146  
     
 147  
     /**
 148  
      * Returns the repetition separator.
 149  
      *
 150  
      * @return the repetition separator
 151  
      */
 152  
     public char getRepetitionSeparator() {
 153  350215
         return this.encChars[1];
 154  
     }
 155  
     
 156  
     /**
 157  
      * Returns the escape character.
 158  
      *
 159  
      * @return the escape character
 160  
      */
 161  
     public char getEscapeCharacter() {
 162  287360
         return this.encChars[2];
 163  
     }
 164  
     
 165  
     /**
 166  
      * Returns the subcomponent separator.
 167  
      *
 168  
      * @return the subcomponent separator
 169  
      */
 170  
     public char getSubcomponentSeparator() {
 171  726147
         return this.encChars[3];
 172  
     }
 173  
 
 174  
     /**
 175  
      * Returns the truncation character.
 176  
      *
 177  
      * @return the truncation character
 178  
      */
 179  
     public char getTruncationCharacter() {
 180  174716
         return this.encChars[4];
 181  
     }
 182  
     
 183  
     /**
 184  
      * Returns the encoding characters (not including field separator)
 185  
      * as a string.
 186  
      */
 187  
     public String toString() {
 188  385
         return String.valueOf(encChars);
 189  
     }
 190  
     
 191  
     public Object clone() throws CloneNotSupportedException
 192  
     {
 193  0
         super.clone();
 194  0
         return new EncodingCharacters(this);
 195  
     }
 196  
     
 197  
     public void setFieldSeparator(char newFieldSep) {
 198  385
         this.fieldSep = newFieldSep;
 199  385
     }
 200  
     
 201  
     public void setComponentSeparator(char newComponentSep) {
 202  480
         this.encChars[0] = newComponentSep;
 203  480
     }
 204  
     
 205  
     public void setRepetitionSeparator(char newRepetitionSep) {
 206  480
         this.encChars[1] = newRepetitionSep;
 207  480
     }
 208  
     
 209  
     public void setEscapeCharacter(char newEscapeChar) {
 210  480
         this.encChars[2] = newEscapeChar;
 211  480
     }
 212  
     
 213  
     public void setSubcomponentSeparator(char newSubcomponentSep) {
 214  480
         this.encChars[3] = newSubcomponentSep;
 215  480
     }
 216  
 
 217  
     public void setTruncationCharacter(char newTruncationChar) {
 218  310
         this.encChars[4] = newTruncationChar;
 219  310
     }
 220  
     
 221  
     /** @see java.lang.Object#equals */
 222  
     public boolean equals(Object o) {
 223  62527
         if (o instanceof EncodingCharacters) {
 224  62527
             EncodingCharacters other = (EncodingCharacters) o;
 225  125054
             return (this.getFieldSeparator() == other.getFieldSeparator()
 226  62527
                 && this.getComponentSeparator() == other.getComponentSeparator()
 227  62527
                 && this.getEscapeCharacter() == other.getEscapeCharacter() 
 228  62527
                 && this.getRepetitionSeparator() == other.getRepetitionSeparator()
 229  62527
                 && this.getSubcomponentSeparator() == other.getSubcomponentSeparator()
 230  58042
                 && this.getTruncationCharacter() == other.getTruncationCharacter());
 231  
         } else {
 232  0
             return false;
 233  
         }   
 234  
     }
 235  
     
 236  
     /** @see java.lang.Object#hashCode */
 237  
     public int hashCode() {
 238  117234
         return 7 * (int) this.getComponentSeparator()
 239  58617
             * (int) this.getEscapeCharacter()
 240  58617
             * (int) this.getFieldSeparator()
 241  58617
             * (int) this.getRepetitionSeparator()
 242  58617
             * (int) this.getSubcomponentSeparator()
 243  58617
             * (int) this.getTruncationCharacter();
 244  
     }  
 245  
 
 246  
     /**
 247  
      * Returns an instance of encoding characters with the standard ER7 encoding characters
 248  
      * defined: |^~\&
 249  
      *
 250  
      * @return a default instance of encoding characters
 251  
      */
 252  
         public static EncodingCharacters defaultInstance() {
 253  5
                 return new EncodingCharacters('|', null);
 254  
         }
 255  
     
 256  
 }
 257