001package ca.uhn.hl7v2.hoh.encoder;
002
003import java.util.HashMap;
004import java.util.Map;
005
006public enum EncodingStyle {
007
008        /**
009         * <p>
010         * ER7 (Pipe and Hat, or Vertical Bar encoding)
011         * </p>
012         * <p>
013         * Content type: <code>application/hl7-v2</code>
014         * </p>
015         */
016        ER7("application/hl7-v2"),
017
018        /**
019         * <p>
020         * XML encoding
021         * </p>
022         * <p>
023         * Content type: <code>application/hl7-v2+xml</code>
024         * </p>
025         */
026        XML("application/hl7-v2+xml");
027
028        private static final Map<String, EncodingStyle> ourContentTypeToEncodingStyles = new HashMap<String, EncodingStyle>();
029
030        static {
031                for (EncodingStyle next : values()) {
032                        ourContentTypeToEncodingStyles.put(next.myContentType, next);
033                }
034        }
035
036        private String myContentType;
037
038        EncodingStyle(String theContentType) {
039                myContentType = theContentType;
040        }
041
042        /**
043         * Returns the encoding style (e.g. ER7) for a given content type (e.g.
044         * application/hl7-v2), or <code>null</code> if content type does not match
045         * an HL7 definition.
046         * 
047         * @param theContentType
048         *            The content type (case insensitive)
049         * @return Returns null if no matching
050         * @throws NullPointerException
051         *             If theContentType is null
052         */
053        public static EncodingStyle getEncodingStyleForContentType(String theContentType) {
054                return ourContentTypeToEncodingStyles.get(theContentType.toLowerCase());
055        }
056
057        /**
058         * Detect the encoding style of a given message
059         * 
060         * @throws NullPointerException If theMessage is null
061         * @throws IllegalArgumentException If the message is not ER7 or XML
062         */
063        public static EncodingStyle detect(String theMessage) {
064                if (theMessage == null) {
065                        throw new NullPointerException("Message can not be null");
066                }
067
068                for (int i = 0; i < theMessage.length(); i++) {
069                        char nextChar = theMessage.charAt(i);
070                        if (Character.isLetter(nextChar)) {
071                                return ER7;
072                        }
073                        if (Character.isWhitespace(nextChar)) {
074                                continue;
075                        }
076                        if (nextChar == '<') {
077                                return XML;
078                        }
079                }
080
081                throw new IllegalArgumentException("Message does not appear to be ER7 or XML");
082
083        }
084
085        /**
086         * Returns the MIME type (content-type) associated with this encoding
087         */
088        public String getContentType() {
089                return myContentType;
090        }
091
092}