View Javadoc

1   package ca.uhn.hl7v2.hoh.encoder;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   public enum EncodingStyle {
7   
8   	/**
9   	 * <p>
10  	 * ER7 (Pipe and Hat, or Vertical Bar encoding)
11  	 * </p>
12  	 * <p>
13  	 * Content type: <code>application/hl7-v2</code>
14  	 * </p>
15  	 */
16  	ER7("application/hl7-v2"),
17  
18  	/**
19  	 * <p>
20  	 * XML encoding
21  	 * </p>
22  	 * <p>
23  	 * Content type: <code>application/hl7-v2+xml</code>
24  	 * </p>
25  	 */
26  	XML("application/hl7-v2+xml");
27  
28  	private static final Map<String, EncodingStyle> ourContentTypeToEncodingStyles = new HashMap<String, EncodingStyle>();
29  
30  	static {
31  		for (EncodingStyle next : values()) {
32  			ourContentTypeToEncodingStyles.put(next.myContentType, next);
33  		}
34  	}
35  
36  	private String myContentType;
37  	private static final Map<String, EncodingStyle> ourContentTypes = new HashMap<String, EncodingStyle>();
38  
39  	static {
40  		for (EncodingStyle next : values()) {
41  			ourContentTypes.put(next.getContentType(), next);
42  		}
43  	}
44  	
45  	EncodingStyle(String theContentType) {
46  		myContentType = theContentType;
47  	}
48  
49  	/**
50  	 * Returns the encoding style (e.g. ER7) for a given content type (e.g.
51  	 * application/hl7-v2), or <code>null</code> if content type does not match
52  	 * an HL7 definition.
53  	 * 
54  	 * @param theContentType
55  	 *            The content type (case insensitive)
56  	 * @return Returns null if no matching
57  	 * @throws NullPointerException
58  	 *             If theContentType is null
59  	 */
60  	public static EncodingStyle getEncodingStyleForContentType(String theContentType) {
61  		return ourContentTypeToEncodingStyles.get(theContentType.toLowerCase());
62  	}
63  
64  	/**
65  	 * @throws NullPointerException if the name is null
66  	 */
67  	public static EncodingStyle withNameCaseInsensitive(String theName) {
68  		return ourContentTypes.get(theName.toLowerCase());
69  	}
70  	
71  	/**
72  	 * Detect the encoding style of a given message
73  	 * 
74  	 * @throws NullPointerException If theMessage is null
75  	 * @throws IllegalArgumentException If the message is not ER7 or XML
76  	 */
77  	public static EncodingStyle detect(String theMessage) {
78  		if (theMessage == null) {
79  			throw new NullPointerException("Message can not be null");
80  		}
81  
82  		for (int i = 0; i < theMessage.length(); i++) {
83  			char nextChar = theMessage.charAt(i);
84  			if (Character.isLetter(nextChar)) {
85  				return ER7;
86  			}
87  			if (Character.isWhitespace(nextChar)) {
88  				continue;
89  			}
90  			if (nextChar == '<') {
91  				return XML;
92  			}
93  		}
94  
95  		throw new IllegalArgumentException("Message does not appear to be ER7 or XML");
96  
97  	}
98  
99  	/**
100 	 * Returns the MIME type (content-type) associated with this encoding
101 	 */
102 	public String getContentType() {
103 		return myContentType;
104 	}
105 
106 }