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 "EncodingDetector.java".  Description:
010"Detects message encoding (ER7 / XML)"
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
028package ca.uhn.hl7v2.parser;
029
030import java.util.StringTokenizer;
031
032/**
033 * Detects message encoding (ER7 / XML) without relying on any
034 * external dependencies
035 */
036public final class EncodingDetector {
037
038        /**
039         * Non instantiable
040         */
041        private EncodingDetector() {
042                // nothing
043        }
044
045    /**
046     * Throws an exception if the message is not ER7 encoded
047     *
048     * @param theMessage message to be examined
049     * @throws RuntimeException if message is not ER7-encoded
050     */
051        public static void assertEr7Encoded(String theMessage) {
052        // quit if the string is too short
053        if (theMessage.length() < 4)
054            throw new RuntimeException("The message is less than 4 characters long");
055
056        // string should start with "MSH"
057        if (!theMessage.startsWith("MSH"))
058            throw new RuntimeException("The message does not start with MSH");
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                    throw new RuntimeException(String.format(
070                            "The 4th character should have been a %c, but it was a %c", x.charAt(3), fourthChar));
071            }
072        }
073
074        // should be at least 11 field delimiters (because MSH-12 is required)
075        int nextFieldDelimLoc = 0;
076        for (int i = 0; i < 11; i++) {
077            nextFieldDelimLoc = theMessage.indexOf(fourthChar, nextFieldDelimLoc + 1);
078            if (nextFieldDelimLoc < 0)
079                throw new RuntimeException("Expected to find required field MSH-12");
080        }
081            
082        }
083        
084        /**
085         * Returns true if the message is ER7 (pipe-and-hat) encoded
086     *
087     * @param theMessage message to be examined
088     * @return true if message is ER7-encoded
089     */
090    public static boolean isEr7Encoded(String theMessage) {
091        try {
092            assertEr7Encoded(theMessage);
093            return true;
094        } catch (Exception e) {
095            return false;
096        }
097        }
098
099    /**
100     * Throws an exception if the message is not XML encoded
101     *
102     * @param theMessage message to be examined
103     * @throws RuntimeException if message is not XML-encoded
104     */
105    public static void assertXmlEncoded(String theMessage) {
106        if (!theMessage.contains("MSH.1>")) throw new RuntimeException("Expected to find MSH.1");
107        if (!theMessage.contains("MSH.2>")) throw new RuntimeException("Expected to find MSH.2");
108    }
109
110    /**
111     * Returns true if the message is XML encoded. Note that this 
112     * message does not perform a very robust check, and does not
113     * validate for well-formedness. It is only intended to perform
114     * a simple check for XML vs. ER7 messages.
115     *
116     * @param theMessage message to be examined
117     * @return true if message is XML-encoded
118     */
119        public static boolean isXmlEncoded(String theMessage) {
120        try {
121            assertXmlEncoded(theMessage);
122            return true;
123        } catch (Exception e) {
124            return false;
125        }
126        }
127
128        
129}