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
10
11
12
13
14
15
16 ER7("application/hl7-v2"),
17
18
19
20
21
22
23
24
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
38 EncodingStyle(String theContentType) {
39 myContentType = theContentType;
40 }
41
42
43
44
45
46
47
48
49
50
51
52
53 public static EncodingStyle getEncodingStyleForContentType(String theContentType) {
54 return ourContentTypeToEncodingStyles.get(theContentType.toLowerCase());
55 }
56
57
58
59
60
61
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
87
88 public String getContentType() {
89 return myContentType;
90 }
91
92 }