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<>();
29  
30  	static {
31  		for (EncodingStyle next : values()) {
32  			ourContentTypeToEncodingStyles.put(next.myContentType, next);
33  		}
34  	}
35  
36  	private final String myContentType;
37  
38  	EncodingStyle(String theContentType) {
39  		myContentType = theContentType;
40  	}
41  
42  	/**
43  	 * Returns the encoding style (e.g. ER7) for a given content type (e.g.
44  	 * application/hl7-v2), or <code>null</code> if content type does not match
45  	 * an HL7 definition.
46  	 * 
47  	 * @param theContentType
48  	 *            The content type (case insensitive)
49  	 * @return Returns null if no matching
50  	 * @throws NullPointerException
51  	 *             If theContentType is null
52  	 */
53  	public static EncodingStyle getEncodingStyleForContentType(String theContentType) {
54  		return ourContentTypeToEncodingStyles.get(theContentType.toLowerCase());
55  	}
56  
57  	/**
58  	 * Detect the encoding style of a given message
59  	 * 
60  	 * @throws NullPointerException If theMessage is null
61  	 * @throws IllegalArgumentException If the message is not ER7 or XML
62  	 */
63  	public static EncodingStyle detect(String theMessage) {
64  		if (theMessage == null) {
65  			throw new NullPointerException("Message can not be null");
66  		}
67  
68  		for (int i = 0; i < theMessage.length(); i++) {
69  			char nextChar = theMessage.charAt(i);
70  			if (Character.isLetter(nextChar)) {
71  				return ER7;
72  			}
73  			if (Character.isWhitespace(nextChar)) {
74  				continue;
75  			}
76  			if (nextChar == '<') {
77  				return XML;
78  			}
79  		}
80  
81  		throw new IllegalArgumentException("Message does not appear to be ER7 or XML");
82  
83  	}
84  
85  	/**
86  	 * Returns the MIME type (content-type) associated with this encoding
87  	 */
88  	public String getContentType() {
89  		return myContentType;
90  	}
91  
92  }