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 "GroupPointer.java".  Description:
010"A GroupPointer is used when parsing 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 extends Object 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 encodingCharacters consists of the characters that appear in
055     *      MSH-2 (see section 2.8 of the HL7 spec).  The characters are
056     *      Component Separator, Repetition Separator, Escape Character, and
057     *      Subcomponent Separator (in that order).
058     */
059    
060    public EncodingCharacters(char fieldSeparator, String encodingCharacters) {
061        this.fieldSep = fieldSeparator;
062        this.encChars = new char[4];
063        
064        if (encodingCharacters == null) {
065            this.encChars[0] = '^';            
066            this.encChars[1] = '~';            
067            this.encChars[2] = '\\';
068            this.encChars[3] = '&';
069        } else {
070            encodingCharacters.getChars(0, 4, this.encChars, 0);
071        }
072        
073    }
074
075    /**
076     * Returns an instance using the MSH-1 and MSH-2 values of the given message
077     * 
078     * @throws HL7Exception If either MSH-1 or MSH-2 are not populated
079     * @since 1.0
080     */
081    public static EncodingCharacters getInstance(Message message) throws HL7Exception {
082
083        final String encodingCharactersValue = message.getEncodingCharactersValue();
084        if (encodingCharactersValue == null || encodingCharactersValue.length() == 0) {
085            throw new HL7Exception("encoding characters not populated");
086        }
087
088        final Character fieldSeparatorValue = message.getFieldSeparatorValue();
089        if (fieldSeparatorValue == null) {
090            throw new HL7Exception("Field separator not populated");
091        }
092
093        return new EncodingCharacters(fieldSeparatorValue, encodingCharactersValue);
094    }
095
096    
097    
098    public EncodingCharacters(char fieldSeparator, char componentSeparator, char repetitionSeparator, char escapeCharacter, char subcomponentSeparator)
099    {
100        this(fieldSeparator, String.valueOf(componentSeparator) + repetitionSeparator + escapeCharacter + subcomponentSeparator);
101    }
102    
103    
104    
105    /** copies contents of "other" */
106    
107    public EncodingCharacters(EncodingCharacters other)
108    {
109        this.fieldSep = other.getFieldSeparator();
110        this.encChars = new char[4];
111        this.encChars[0] = other.getComponentSeparator();
112        this.encChars[1] = other.getRepetitionSeparator();
113        this.encChars[2] = other.getEscapeCharacter();
114        this.encChars[3] = other.getSubcomponentSeparator();
115    }
116    
117    /**
118     *
119     * Returns the field separator.
120     *
121     */
122    public char getFieldSeparator() {
123        return this.fieldSep;
124    }
125    
126    /**
127     *
128     * Returns the component separator.
129     *
130     */
131    public char getComponentSeparator() {
132        return this.encChars[0];
133    }
134    
135    /**
136     *
137     * Returns the repetition separator.
138     *
139     */
140    public char getRepetitionSeparator() {
141        return this.encChars[1];
142    }
143    
144    /**
145     * Returns the escape character.
146     */
147    public char getEscapeCharacter() {
148        return this.encChars[2];
149    }
150    
151    /**
152     * Returns the subcomponent separator.
153     */
154    public char getSubcomponentSeparator() {
155        return this.encChars[3];
156    }
157    
158    /**
159     * Returns the encoding characters (not including field separator)
160     * as a string.
161     */
162    public String toString() {
163        StringBuffer ret = new StringBuffer();
164        for (int i = 0; i < this.encChars.length; i++) {
165            ret.append(this.encChars[i]);
166        }
167        
168        return ret.toString();
169    }
170    
171    public Object clone()
172    {
173        return new EncodingCharacters(this);
174    }
175    
176    public void setFieldSeparator(char newFieldSep) {
177        this.fieldSep = newFieldSep;
178    }
179    
180    public void setComponentSeparator(char newComponentSep) {
181        this.encChars[0] = newComponentSep;
182    }
183    
184    public void setRepetitionSeparator(char newRepetitionSep) {
185        this.encChars[1] = newRepetitionSep;
186    }
187    
188    public void setEscapeCharacter(char newEscapeChar) {
189        this.encChars[2] = newEscapeChar;
190    }
191    
192    public void setSubcomponentSeparator(char newSubcomponentSep) {
193        this.encChars[3] = newSubcomponentSep;
194    }
195    
196    /** @see java.lang.Object#equals */
197    public boolean equals(Object o) {
198        if (o instanceof EncodingCharacters) {
199            EncodingCharacters other = (EncodingCharacters) o;
200            if (this.getFieldSeparator() == other.getFieldSeparator() 
201                && this.getComponentSeparator() == other.getComponentSeparator()
202                && this.getEscapeCharacter() == other.getEscapeCharacter() 
203                && this.getRepetitionSeparator() == other.getRepetitionSeparator()
204                && this.getSubcomponentSeparator() == other.getSubcomponentSeparator()) {
205                    return true;
206            } else {
207                return false;
208            }
209        } else {
210            return false;
211        }   
212    }
213    
214    /** @see java.lang.Object#hashCode */
215    public int hashCode() {
216        return 7 * (int) this.getComponentSeparator()
217            * (int) this.getEscapeCharacter()
218            * (int) this.getFieldSeparator()
219            * (int) this.getRepetitionSeparator()
220            * (int) this.getSubcomponentSeparator();
221    }        
222    
223    /**
224     *
225     * Test harness ...
226     *
227     */
228    
229    /*
230     
231    public static void main(String args[]) {
232     
233        String testChars = "^~\\&";
234     
235        String testChars2 = "$%*+";
236     
237     
238     
239        EncodingCharacters ec = new EncodingCharacters('|', testChars);
240     
241        System.out.println("test 1: " + ec.getFieldSeparator() + ec.toString());
242     
243        ec = new EncodingCharacters('|', testChars2);
244     
245        System.out.println("test 2: " + ec.getFieldSeparator() + ec.getComponentSeparator() + ec.getRepetitionSeparator() + ec.getEscapeCharacter() + ec.getSubcomponentSeparator());
246     
247        ec = new EncodingCharacters('[', null);
248     
249        System.out.println("test 3: " + ec.getFieldSeparator() + ec.toString());
250     
251    }*/
252    
253}
254