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    package ca.uhn.hl7v2.parser;
029    
030    import java.util.StringTokenizer;
031    
032    /**
033     * Detects message encoding (ER7 / XML) without relying on any
034     * external dependencies
035     */
036    public class EncodingDetector {
037    
038            /**
039             * Non instantiable
040             */
041            private EncodingDetector() {
042                    // nothing
043            }
044            
045            /**
046             * Returns true if the message is ER7 (pipe-and-hat) encoded
047             */
048        public static boolean isEr7Encoded(String theMessage) {
049            // quit if the string is too short
050            if (theMessage.length() < 4)
051                return false;
052    
053            // see if it looks like this message is | encoded ...
054            boolean ok = true;
055    
056            // string should start with "MSH"
057            if (!theMessage.startsWith("MSH"))
058                return false;
059    
060            // 4th character of each segment should be field delimiter
061            char fourthChar = theMessage.charAt(3);
062            StringTokenizer st = new StringTokenizer(theMessage, String.valueOf(PipeParser.SEGMENT_DELIMITER), false);
063            while (st.hasMoreTokens()) {
064                String x = st.nextToken();
065                if (x.length() > 0) {
066                    if (Character.isWhitespace(x.charAt(0)))
067                        x = PipeParser.stripLeadingWhitespace(x);
068                    if (x.length() >= 4 && x.charAt(3) != fourthChar)
069                        return false;
070                }
071            }
072    
073            // should be at least 11 field delimiters (because MSH-12 is required)
074            int nextFieldDelimLoc = 0;
075            for (int i = 0; i < 11; i++) {
076                nextFieldDelimLoc = theMessage.indexOf(fourthChar, nextFieldDelimLoc + 1);
077                if (nextFieldDelimLoc < 0)
078                    return false;
079            }
080            
081            return ok;
082            }
083    
084        
085            /**
086             * Returns true if the message is XML encoded. Note that this 
087             * message does not perform a very robust check, and does not
088             * validate for well-formedness. It is only intended to perform
089             * a simple check for XML vs. ER7 messages.
090             */
091            public static boolean isXmlEncoded(String theMessage) {
092            //check for a number of expected strings 
093            String[] expected = { "<MSH.1", "<MSH.2", "</MSH>" };
094            for (int i = 0; i < expected.length; i++) {
095                if (theMessage.indexOf(expected[i]) < 0) {
096                    return false;
097                }
098            }
099            
100            return true;
101            }
102    
103            
104    }