View Javadoc
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 final 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      public EncodingCharacters(char fieldSeparator, String encodingCharacters) {
58          this.fieldSep = fieldSeparator;
59          this.encChars = new char[5];
60          
61          if (encodingCharacters == null) {
62              setComponentSeparator('^');            
63              setRepetitionSeparator('~');            
64              setEscapeCharacter('\\');
65              setSubcomponentSeparator('&');
66              setTruncationCharacter('#');
67          } else {
68              encodingCharacters.getChars(0, 4, this.encChars, 0);
69              // Add truncation character if available
70              if (encodingCharacters.length() > 4) {
71                  char extraChar = encodingCharacters.charAt(4);
72                  if (extraChar != fieldSeparator) {
73                      setTruncationCharacter(extraChar);
74                  }
75              }
76          }
77          
78      }
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          final String encodingCharactersValue = message.getEncodingCharactersValue();
91          if (encodingCharactersValue == null || encodingCharactersValue.length() == 0) {
92              throw new HL7Exception("encoding characters not populated");
93          }
94  
95          final Character fieldSeparatorValue = message.getFieldSeparatorValue();
96          if (fieldSeparatorValue == null) {
97              throw new HL7Exception("Field separator not populated");
98          }
99  
100         return new EncodingCharacters(fieldSeparatorValue, encodingCharactersValue);
101     }
102 
103     
104     
105     public EncodingCharacters(char fieldSeparator, char componentSeparator, char repetitionSeparator,
106                               char escapeCharacter, char subcomponentSeparator) {
107         this(fieldSeparator, String.valueOf(componentSeparator) + repetitionSeparator + 
108                 escapeCharacter + subcomponentSeparator);
109     }
110 
111     public EncodingCharacters(char fieldSeparator, char componentSeparator, char repetitionSeparator,
112                               char escapeCharacter, char subcomponentSeparator, char truncationCharacter) {
113         this(fieldSeparator, String.valueOf(componentSeparator) + repetitionSeparator + 
114                 escapeCharacter + subcomponentSeparator + truncationCharacter);
115     }    
116     
117     /** copies contents of "other" */
118     
119     public EncodingCharacters/../../../ca/uhn/hl7v2/parser/EncodingCharacters.html#EncodingCharacters">EncodingCharacters(EncodingCharacters other) {
120         this.fieldSep = other.getFieldSeparator();
121         this.encChars = new char[5];
122         setComponentSeparator(other.getComponentSeparator());
123         setRepetitionSeparator(other.getRepetitionSeparator());
124         setEscapeCharacter(other.getEscapeCharacter());
125         setSubcomponentSeparator(other.getSubcomponentSeparator());
126         setTruncationCharacter(other.getTruncationCharacter());
127     }
128     
129     /**
130      * Returns the field separator.
131      *
132      * @return the field separator
133      */
134     public char getFieldSeparator() {
135         return this.fieldSep;
136     }
137     
138     /**
139      * Returns the component separator.
140      *
141      * @return the component separator
142      */
143     public char getComponentSeparator() {
144         return this.encChars[0];
145     }
146     
147     /**
148      * Returns the repetition separator.
149      *
150      * @return the repetition separator
151      */
152     public char getRepetitionSeparator() {
153         return this.encChars[1];
154     }
155     
156     /**
157      * Returns the escape character.
158      *
159      * @return the escape character
160      */
161     public char getEscapeCharacter() {
162         return this.encChars[2];
163     }
164     
165     /**
166      * Returns the subcomponent separator.
167      *
168      * @return the subcomponent separator
169      */
170     public char getSubcomponentSeparator() {
171         return this.encChars[3];
172     }
173 
174     /**
175      * Returns the truncation character.
176      *
177      * @return the truncation character
178      */
179     public char getTruncationCharacter() {
180         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         return String.valueOf(encChars);
189     }
190     
191     public Object clone() throws CloneNotSupportedException
192     {
193         super.clone();
194         return new EncodingCharacters(this);
195     }
196     
197     public void setFieldSeparator(char newFieldSep) {
198         this.fieldSep = newFieldSep;
199     }
200     
201     public void setComponentSeparator(char newComponentSep) {
202         this.encChars[0] = newComponentSep;
203     }
204     
205     public void setRepetitionSeparator(char newRepetitionSep) {
206         this.encChars[1] = newRepetitionSep;
207     }
208     
209     public void setEscapeCharacter(char newEscapeChar) {
210         this.encChars[2] = newEscapeChar;
211     }
212     
213     public void setSubcomponentSeparator(char newSubcomponentSep) {
214         this.encChars[3] = newSubcomponentSep;
215     }
216 
217     public void setTruncationCharacter(char newTruncationChar) {
218         this.encChars[4] = newTruncationChar;
219     }
220     
221     /** @see java.lang.Object#equals */
222     public boolean equals(Object o) {
223         if (o instanceof EncodingCharacters) {
224             EncodingCharacters/ca/uhn/hl7v2/parser/EncodingCharacters.html#EncodingCharacters">EncodingCharacters other = (EncodingCharacters) o;
225             return (this.getFieldSeparator() == other.getFieldSeparator()
226                 && this.getComponentSeparator() == other.getComponentSeparator()
227                 && this.getEscapeCharacter() == other.getEscapeCharacter() 
228                 && this.getRepetitionSeparator() == other.getRepetitionSeparator()
229                 && this.getSubcomponentSeparator() == other.getSubcomponentSeparator()
230                 && this.getTruncationCharacter() == other.getTruncationCharacter());
231         } else {
232             return false;
233         }   
234     }
235     
236     /** @see java.lang.Object#hashCode */
237     public int hashCode() {
238         return 7 * (int) this.getComponentSeparator()
239             * (int) this.getEscapeCharacter()
240             * (int) this.getFieldSeparator()
241             * (int) this.getRepetitionSeparator()
242             * (int) this.getSubcomponentSeparator()
243             * (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 		return new EncodingCharacters('|', null);
254 	}
255     
256 }
257