1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
package ca.uhn.hl7v2.model; |
29 | |
|
30 | |
import java.util.Collection; |
31 | |
|
32 | |
import ca.uhn.hl7v2.HL7Exception; |
33 | |
import ca.uhn.hl7v2.Location; |
34 | |
import ca.uhn.hl7v2.parser.EncodingCharacters; |
35 | |
import ca.uhn.hl7v2.parser.Parser; |
36 | |
import ca.uhn.hl7v2.validation.PrimitiveTypeRule; |
37 | |
import ca.uhn.hl7v2.validation.ValidationContext; |
38 | |
import ca.uhn.hl7v2.validation.ValidationException; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
@SuppressWarnings("serial") |
46 | |
public abstract class AbstractPrimitive extends AbstractType implements Primitive { |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public AbstractPrimitive(Message message) { |
52 | 641103 | super(message); |
53 | 641103 | } |
54 | |
|
55 | |
private String myValue; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public String toString() { |
62 | 5 | return this.getValue(); |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public String getValue() { |
69 | 351599 | return myValue; |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public void setValue(String theValue) throws DataTypeException { |
84 | 135053 | Message message = getMessage(); |
85 | |
|
86 | 135053 | if (message != null) { |
87 | 134713 | ValidationContext context = message.getParser().getValidationContext(); |
88 | 134713 | String version = message.getVersion(); |
89 | |
|
90 | 134713 | if (context != null) { |
91 | 134453 | Collection<PrimitiveTypeRule> rules = context.getPrimitiveRules(version, getName(), this); |
92 | |
|
93 | 134453 | for (PrimitiveTypeRule rule : rules) { |
94 | 119428 | theValue = rule.correct(theValue); |
95 | 119428 | ValidationException[] ve = rule.apply(theValue); |
96 | 119428 | if (ve.length > 0) { |
97 | 560 | throw new DataTypeException(ve[0]); |
98 | |
} |
99 | 118868 | } |
100 | |
} |
101 | |
} |
102 | |
|
103 | 134493 | myValue = theValue; |
104 | 134493 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
@Override |
111 | |
public String encode() throws HL7Exception { |
112 | 115 | Parser p = getMessage().getParser(); |
113 | 115 | return p.doEncode(this, EncodingCharacters.getInstance(getMessage())); |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
@Override |
121 | |
public void parse(String string) throws HL7Exception { |
122 | 125 | if (string == null) { |
123 | 5 | clear(); |
124 | 5 | return; |
125 | |
} |
126 | |
|
127 | 120 | EncodingCharacters encodingCharacters = EncodingCharacters.getInstance(getMessage()); |
128 | 120 | char subc = encodingCharacters.getSubcomponentSeparator(); |
129 | 120 | char cmpc = encodingCharacters.getComponentSeparator(); |
130 | |
|
131 | 120 | clear(); |
132 | |
|
133 | |
|
134 | |
|
135 | 120 | int subcIndex = string.indexOf(subc); |
136 | 120 | int cmpcIndex = string.indexOf(cmpc); |
137 | 120 | if (subcIndex != -1 || cmpcIndex != -1) { |
138 | |
|
139 | |
|
140 | |
|
141 | |
int index; |
142 | |
char escapeChar; |
143 | 30 | if (cmpcIndex != -1) { |
144 | 25 | index = cmpcIndex; |
145 | 25 | escapeChar = cmpc; |
146 | |
} else { |
147 | 5 | index = subcIndex; |
148 | 5 | escapeChar = subc; |
149 | |
} |
150 | |
|
151 | 30 | setValue(string.substring(0, index)); |
152 | 95 | while (index != -1) { |
153 | 65 | int prevIndex = index + 1; |
154 | 65 | index = string.indexOf(escapeChar, prevIndex); |
155 | 65 | if (index != -1) { |
156 | 35 | String nextSubComponent = string.substring(prevIndex, index); |
157 | 35 | getExtraComponents().getComponent(getExtraComponents().numComponents()).parse(nextSubComponent); |
158 | 35 | } else { |
159 | 30 | String nextSubComponent = string.substring(prevIndex); |
160 | 30 | if (nextSubComponent.length() > 0) { |
161 | 20 | getExtraComponents().getComponent(getExtraComponents().numComponents()).parse(nextSubComponent); |
162 | |
} |
163 | |
} |
164 | 65 | } |
165 | |
|
166 | 30 | } else { |
167 | |
|
168 | 90 | String escaped = getMessage().getParser().getParserConfiguration() |
169 | 90 | .getEscaping().unescape(string, encodingCharacters); |
170 | 90 | setValue(escaped); |
171 | |
|
172 | |
} |
173 | 120 | } |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
@Override |
180 | |
public void clear() { |
181 | 1160 | super.clear(); |
182 | 1160 | myValue = null; |
183 | 1160 | } |
184 | |
|
185 | |
@Override |
186 | |
public boolean isEmpty() throws HL7Exception { |
187 | 4180 | return (myValue == null || myValue.length() == 0) && super.isEmpty(); |
188 | |
} |
189 | |
|
190 | |
public boolean accept(MessageVisitor visitor, Location location) throws HL7Exception { |
191 | 1470 | return visitor.visit(this, location); |
192 | |
} |
193 | |
|
194 | |
} |