001/** 002The contents of this file are subject to the Mozilla Public License Version 1.1 003(the "License"); you may not use this file except in compliance with the License. 004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005Software distributed under the License is distributed on an "AS IS" basis, 006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007specific language governing rights and limitations under the License. 008 009The Original Code is "EncodingCharacters.java". Description: 010"Represents the set of special characters used to encode traditionally encoded HL7 messages." 011 012The Initial Developer of the Original Code is University Health Network. Copyright (C) 0132001. All Rights Reserved. 014 015Contributor(s): ______________________________________. 016 017Alternatively, the contents of this file may be used under the terms of the 018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 019applicable instead of those above. If you wish to allow use of your version of this 020file only under the terms of the GPL and not to allow others to use your version 021of this file under the MPL, indicate your decision by deleting the provisions above 022and replace them with the notice and other provisions required by the GPL License. 023If you do not delete the provisions above, a recipient may use your version of 024this file under either the MPL or the GPL. 025 026*/ 027 028 029 030package ca.uhn.hl7v2.parser; 031 032import ca.uhn.hl7v2.HL7Exception; 033import ca.uhn.hl7v2.model.Message; 034 035 036 037/** 038 * Represents the set of special characters used to encode traditionally 039 * encoded HL7 messages. 040 * 041 * @author Bryan Tripp (bryan_tripp@sourceforge.net) 042 */ 043 044public class EncodingCharacters implements Cloneable { 045 046 private char fieldSep; 047 private char[] encChars; 048 049 /** 050 * Creates new EncodingCharacters object with the given character 051 * values. If the encodingCharacters argument is null, the default 052 * values are used. 053 * 054 * @param fieldSeparator field seperator 055 * @param encodingCharacters consists of the characters that appear in 056 * MSH-2 (see section 2.8 of the HL7 spec). The characters are 057 * Component Separator, Repetition Separator, Escape Character, and 058 * Subcomponent Separator (in that order). 059 */ 060 061 public EncodingCharacters(char fieldSeparator, String encodingCharacters) { 062 this.fieldSep = fieldSeparator; 063 this.encChars = new char[4]; 064 065 if (encodingCharacters == null) { 066 this.encChars[0] = '^'; 067 this.encChars[1] = '~'; 068 this.encChars[2] = '\\'; 069 this.encChars[3] = '&'; 070 } else { 071 encodingCharacters.getChars(0, 4, this.encChars, 0); 072 } 073 074 } 075 076 /** 077 * Returns an instance using the MSH-1 and MSH-2 values of the given message 078 * 079 * @param message the message 080 * @return the encoding characters for this message 081 * @throws HL7Exception If either MSH-1 or MSH-2 are not populated 082 * @since 1.0 083 */ 084 public static EncodingCharacters getInstance(Message message) throws HL7Exception { 085 086 final String encodingCharactersValue = message.getEncodingCharactersValue(); 087 if (encodingCharactersValue == null || encodingCharactersValue.length() == 0) { 088 throw new HL7Exception("encoding characters not populated"); 089 } 090 091 final Character fieldSeparatorValue = message.getFieldSeparatorValue(); 092 if (fieldSeparatorValue == null) { 093 throw new HL7Exception("Field separator not populated"); 094 } 095 096 return new EncodingCharacters(fieldSeparatorValue, encodingCharactersValue); 097 } 098 099 100 101 public EncodingCharacters(char fieldSeparator, char componentSeparator, char repetitionSeparator, 102 char escapeCharacter, char subcomponentSeparator) { 103 this(fieldSeparator, String.valueOf(componentSeparator) + repetitionSeparator + escapeCharacter + subcomponentSeparator); 104 } 105 106 107 108 /** copies contents of "other" */ 109 110 public EncodingCharacters(EncodingCharacters other) { 111 this.fieldSep = other.getFieldSeparator(); 112 this.encChars = new char[4]; 113 this.encChars[0] = other.getComponentSeparator(); 114 this.encChars[1] = other.getRepetitionSeparator(); 115 this.encChars[2] = other.getEscapeCharacter(); 116 this.encChars[3] = other.getSubcomponentSeparator(); 117 } 118 119 /** 120 * Returns the field separator. 121 * 122 * @return the field separator 123 */ 124 public char getFieldSeparator() { 125 return this.fieldSep; 126 } 127 128 /** 129 * Returns the component separator. 130 * 131 * @return the component separator 132 */ 133 public char getComponentSeparator() { 134 return this.encChars[0]; 135 } 136 137 /** 138 * Returns the repetition separator. 139 * 140 * @return the repetition separator 141 */ 142 public char getRepetitionSeparator() { 143 return this.encChars[1]; 144 } 145 146 /** 147 * Returns the escape character. 148 * 149 * @return the escape character 150 */ 151 public char getEscapeCharacter() { 152 return this.encChars[2]; 153 } 154 155 /** 156 * Returns the subcomponent separator. 157 * 158 * @return the subcomponent separator 159 */ 160 public char getSubcomponentSeparator() { 161 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 return String.valueOf(encChars); 170 } 171 172 public Object clone() throws CloneNotSupportedException 173 { 174 return new EncodingCharacters(this); 175 } 176 177 public void setFieldSeparator(char newFieldSep) { 178 this.fieldSep = newFieldSep; 179 } 180 181 public void setComponentSeparator(char newComponentSep) { 182 this.encChars[0] = newComponentSep; 183 } 184 185 public void setRepetitionSeparator(char newRepetitionSep) { 186 this.encChars[1] = newRepetitionSep; 187 } 188 189 public void setEscapeCharacter(char newEscapeChar) { 190 this.encChars[2] = newEscapeChar; 191 } 192 193 public void setSubcomponentSeparator(char newSubcomponentSep) { 194 this.encChars[3] = newSubcomponentSep; 195 } 196 197 /** @see java.lang.Object#equals */ 198 public boolean equals(Object o) { 199 if (o instanceof EncodingCharacters) { 200 EncodingCharacters other = (EncodingCharacters) o; 201 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 return false; 208 } 209 } 210 211 /** @see java.lang.Object#hashCode */ 212 public int hashCode() { 213 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 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