1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package ca.uhn.hl7v2.parser;
25
26 import ca.uhn.hl7v2.HL7Exception;
27 import ca.uhn.hl7v2.Version;
28 import ca.uhn.hl7v2.model.Group;
29 import ca.uhn.hl7v2.model.Message;
30 import ca.uhn.hl7v2.model.Segment;
31 import ca.uhn.hl7v2.model.Type;
32 import ca.uhn.hl7v2.util.ReflectionUtil;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class CanonicalModelClassFactory extends DefaultModelClassFactory {
48
49 private static final long serialVersionUID = -1795680089524220526L;
50
51 private Class<? extends Message> myMessageClass;
52
53 private String myVersion;
54
55
56
57
58 public CanonicalModelClassFactory() {
59 myVersion = getHighestKnownVersion();
60 }
61
62
63
64
65
66
67
68 public CanonicalModelClassFactory(Class<? extends Message> theClass) {
69 if (theClass == null) {
70 throw new NullPointerException("Class may not be null");
71 }
72 myMessageClass = theClass;
73 }
74
75
76
77
78
79
80
81 public CanonicalModelClassFactory(String theVersion) {
82 if (theVersion == null || !Version.supportsVersion(theVersion)) {
83 throw new IllegalArgumentException("Unknown version: " + theVersion);
84 }
85 myVersion = theVersion;
86 }
87
88
89
90
91 @Override
92 public Class<? extends Group> getGroupClass(String theName, String theVersion) throws HL7Exception {
93 return super.getGroupClass(theName, myVersion);
94 }
95
96
97
98
99 @Override
100 public Class<? extends Message> getMessageClass(String theName, String theVersion, boolean theIsExplicit) throws HL7Exception {
101 if (myMessageClass != null) {
102 return myMessageClass;
103 }
104 initVersionIfNeeded();
105 return super.getMessageClass(theName, myVersion, theIsExplicit);
106 }
107
108
109
110
111 @Override
112 public Class<? extends Segment> getSegmentClass(String theName, String theVersion) throws HL7Exception {
113 initVersionIfNeeded();
114 return super.getSegmentClass(theName, myVersion);
115 }
116
117
118
119
120 @Override
121 public Class<? extends Type> getTypeClass(String theName, String theVersion) throws HL7Exception {
122 initVersionIfNeeded();
123 return super.getTypeClass(theName, myVersion);
124 }
125
126 private void initVersionIfNeeded() throws HL7Exception {
127 if (myMessageClass != null && myVersion == null) {
128 myVersion = ReflectionUtil.instantiateMessage(myMessageClass, this).getVersion();
129 }
130 }
131
132 }