001    /**
002    The 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.
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/
005    Software distributed under the License is distributed on an "AS IS" basis,
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007    specific language governing rights and limitations under the License.
008    
009    The Original Code is "GroupPointer.java".  Description:
010    "A GroupPointer is used when parsing traditionally encoded HL7 messages"
011    
012    The Initial Developer of the Original Code is University Health Network. Copyright (C)
013    2001.  All Rights Reserved.
014    
015    Contributor(s): ______________________________________.
016    
017    Alternatively, the contents of this file may be used under the terms of the
018    GNU General Public License (the  ???GPL???), in which case the provisions of the GPL are
019    applicable instead of those above.  If you wish to allow use of your version of this
020    file only under the terms of the GPL and not to allow others to use your version
021    of this file under the MPL, indicate your decision by deleting  the provisions above
022    and replace  them with the notice and other provisions required by the GPL License.
023    If you do not delete the provisions above, a recipient may use your version of
024    this file under either the MPL or the GPL.
025    
026    */
027    
028    
029    
030    package ca.uhn.hl7v2.parser;
031    
032    import ca.uhn.hl7v2.HL7Exception;
033    import ca.uhn.hl7v2.model.Message;
034    
035    
036    
037    /**
038     *
039     * Represents the set of special characters used to encode traditionally
040     *
041     * encoded HL7 messages.
042     *
043     * @author Bryan Tripp (bryan_tripp@sourceforge.net)
044     *
045     */
046    
047    public class EncodingCharacters extends Object implements Cloneable {
048        
049        
050        
051        private char fieldSep;
052        
053        private char[] encChars;
054        
055        
056        
057        /**
058         *
059         * Creates new EncodingCharacters object with the given character
060         *
061         * values. If the encodingCharacters argument is null, the default
062         *
063         * values are used.
064         *
065         * @param encodingCharacters consists of the characters that appear in
066         *
067         *      MSH-2 (see section 2.8 of the HL7 spec).  The characters are
068         *
069         *      Component Separator, Repetition Separator, Escape Character, and
070         *
071         *      Subcomponent Separator (in that order).
072         *
073         */
074        
075        public EncodingCharacters(char fieldSeparator, String encodingCharacters) {
076            
077            this.fieldSep = fieldSeparator;
078            
079            this.encChars = new char[4];
080            
081            if (encodingCharacters == null) {
082                
083                this.encChars[0] = '^';
084                
085                this.encChars[1] = '~';
086                
087                this.encChars[2] = '\\';
088                
089                this.encChars[3] = '&';
090                
091            } else {
092                
093                encodingCharacters.getChars(0, 4, this.encChars, 0);
094                
095            }
096            
097        }
098    
099        /**
100         * Returns an instance using the MSH-1 and MSH-2 values of the given message
101         * 
102         * @throws HL7Exception If either MSH-1 or MSH-2 are not populated
103         * @since 1.0
104         */
105        public static EncodingCharacters getInstance(Message message) throws HL7Exception {
106    
107            final String encodingCharactersValue = message.getEncodingCharactersValue();
108            if (encodingCharactersValue == null || encodingCharactersValue.length() == 0) {
109                throw new HL7Exception("encoding characters not populated");
110            }
111    
112            final Character fieldSeparatorValue = message.getFieldSeparatorValue();
113            if (fieldSeparatorValue == null) {
114                throw new HL7Exception("Field separator not populated");
115            }
116    
117            return new EncodingCharacters(fieldSeparatorValue, encodingCharactersValue);
118        }
119    
120        
121        
122        public EncodingCharacters(char fieldSeparator, char componentSeparator,
123        
124        char repetitionSeparator, char escapeCharacter,
125        
126        char subcomponentSeparator)
127        
128        {
129            
130            this(fieldSeparator, String.valueOf(componentSeparator)
131            
132            + repetitionSeparator + escapeCharacter + subcomponentSeparator);
133            
134        }
135        
136        
137        
138        /** copies contents of "other" */
139        
140        public EncodingCharacters(EncodingCharacters other)
141        
142        {
143            
144            this.fieldSep = other.getFieldSeparator();
145            
146            this.encChars = new char[4];
147            
148            this.encChars[0] = other.getComponentSeparator();
149            
150            this.encChars[1] = other.getRepetitionSeparator();
151            
152            this.encChars[2] = other.getEscapeCharacter();
153            
154            this.encChars[3] = other.getSubcomponentSeparator();
155            
156        }
157        
158        
159        
160        /**
161         *
162         * Returns the field separator.
163         *
164         */
165        
166        public char getFieldSeparator() {
167            
168            return this.fieldSep;
169            
170        }
171        
172        
173        
174        /**
175         *
176         * Returns the component separator.
177         *
178         */
179        
180        public char getComponentSeparator() {
181            
182            return this.encChars[0];
183            
184        }
185        
186        
187        
188        /**
189         *
190         * Returns the repetition separator.
191         *
192         */
193        
194        public char getRepetitionSeparator() {
195            
196            return this.encChars[1];
197            
198        }
199        
200        
201        
202        /**
203         *
204         * Returns the escape character.
205         *
206         */
207        
208        public char getEscapeCharacter() {
209            
210            return this.encChars[2];
211            
212        }
213        
214        
215        
216        /**
217         *
218         * Returns the subcomponent separator.
219         *
220         */
221        
222        public char getSubcomponentSeparator() {
223            
224            return this.encChars[3];
225            
226        }
227        
228        
229        
230        /**
231         *
232         * Returns the encoding characters (not including field separator)
233         *
234         * as a string.
235         *
236         */
237        
238        public String toString() {
239            
240            StringBuffer ret = new StringBuffer();
241            
242            for (int i = 0; i < this.encChars.length; i++) {
243                
244                ret.append(this.encChars[i]);
245                
246            }
247            
248            return ret.toString();
249            
250        }
251        
252        
253        
254        public Object clone()
255        
256        {
257            
258            return new EncodingCharacters(this);
259            
260        }
261        
262        
263        
264        public void setFieldSeparator(char newFieldSep) {
265            
266            this.fieldSep = newFieldSep;
267            
268        }
269        
270        
271        
272        public void setComponentSeparator(char newComponentSep) {
273            
274            this.encChars[0] = newComponentSep;
275            
276        }
277        
278        
279        
280        public void setRepetitionSeparator(char newRepetitionSep) {
281            
282            this.encChars[1] = newRepetitionSep;
283            
284        }
285        
286        
287        
288        public void setEscapeCharacter(char newEscapeChar) {
289            
290            this.encChars[2] = newEscapeChar;
291            
292        }
293        
294        
295        
296        public void setSubcomponentSeparator(char newSubcomponentSep) {
297            
298            this.encChars[3] = newSubcomponentSep;
299            
300        }
301        
302        /** @see java.lang.Object#equals */
303        public boolean equals(Object o) {
304            if (o instanceof EncodingCharacters) {
305                EncodingCharacters other = (EncodingCharacters) o;
306                if (this.getFieldSeparator() == other.getFieldSeparator() 
307                    && this.getComponentSeparator() == other.getComponentSeparator()
308                    && this.getEscapeCharacter() == other.getEscapeCharacter() 
309                    && this.getRepetitionSeparator() == other.getRepetitionSeparator()
310                    && this.getSubcomponentSeparator() == other.getSubcomponentSeparator()) {
311                        return true;
312                } else {
313                    return false;
314                }
315            } else {
316                return false;
317            }   
318        }
319        
320        /** @see java.lang.Object#hashCode */
321        public int hashCode() {
322            return 7 * (int) this.getComponentSeparator()
323                * (int) this.getEscapeCharacter()
324                * (int) this.getFieldSeparator()
325                * (int) this.getRepetitionSeparator()
326                * (int) this.getSubcomponentSeparator();
327        }        
328        
329        /**
330         *
331         * Test harness ...
332         *
333         */
334        
335        /*
336         
337        public static void main(String args[]) {
338         
339            String testChars = "^~\\&";
340         
341            String testChars2 = "$%*+";
342         
343         
344         
345            EncodingCharacters ec = new EncodingCharacters('|', testChars);
346         
347            System.out.println("test 1: " + ec.getFieldSeparator() + ec.toString());
348         
349            ec = new EncodingCharacters('|', testChars2);
350         
351            System.out.println("test 2: " + ec.getFieldSeparator() + ec.getComponentSeparator() + ec.getRepetitionSeparator() + ec.getEscapeCharacter() + ec.getSubcomponentSeparator());
352         
353            ec = new EncodingCharacters('[', null);
354         
355            System.out.println("test 3: " + ec.getFieldSeparator() + ec.toString());
356         
357        }*/
358        
359    }
360