1 package ca.uhn.hl7v2.conf.store;
2
3 import java.util.StringTokenizer;
4
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7
8
9
10
11
12
13
14 public abstract class AbstractCodeStore implements CodeStore {
15
16 private static final Logger log = LoggerFactory.getLogger(AbstractCodeStore.class);
17
18 private static final RegisteredPattern[] WILDCARDS = {
19 new RegisteredPattern("ISOnnnn", "ISO\\d\\d\\d\\d"),
20 new RegisteredPattern("HL7nnnn", "HL7\\d\\d\\d\\d"),
21 new RegisteredPattern("99zzz", "99[\\w]*"),
22 new RegisteredPattern("NNxxx", "99[\\w]*")
23 };
24
25
26
27
28 public boolean isValidCode(String codeSystem, String code) {
29 try {
30 for (String validCode : getValidCodes(codeSystem)) {
31 if (checkCode(code, validCode)) return true;
32 }
33 return false;
34 }
35 catch (Exception e) {
36 log.error("Error checking code " + code + " in code system " + codeSystem, e);
37 return false;
38 }
39 }
40
41
42
43
44
45
46 private boolean checkCode(String code, String pattern) {
47
48
49
50 if (code.equals(pattern)) return true;
51
52 if (pattern.indexOf(' ') >= 0 || pattern.indexOf(',') >= 0) {
53 StringTokenizer tok = new StringTokenizer(pattern, ", ", false);
54 while (tok.hasMoreTokens()) {
55 String t = tok.nextToken();
56 if (!t.equalsIgnoreCase("or") && checkCode(code, t))
57 return true;
58 }
59 } else {
60 for (RegisteredPattern wildcard : WILDCARDS) {
61 if (wildcard.matches(pattern, code))
62 return true;
63 }
64 }
65
66 return false;
67 }
68
69 }