1 | |
package ca.uhn.hl7v2.conf.store; |
2 | |
|
3 | |
import java.io.IOException; |
4 | |
import java.net.URL; |
5 | |
import java.util.ArrayList; |
6 | |
import java.util.HashMap; |
7 | |
import java.util.List; |
8 | |
import java.util.Map; |
9 | |
|
10 | |
import org.xml.sax.Attributes; |
11 | |
import org.xml.sax.SAXException; |
12 | |
import org.xml.sax.XMLReader; |
13 | |
import org.xml.sax.helpers.DefaultHandler; |
14 | |
import org.xml.sax.helpers.XMLReaderFactory; |
15 | |
|
16 | |
import ca.uhn.hl7v2.conf.ProfileException; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 109920 | public class ProfileCodeStore extends AbstractCodeStore { |
32 | |
|
33 | 120 | private Map<String, List<String>> codes = new HashMap<String, List<String>>(); |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 120 | public ProfileCodeStore(String uri) throws ProfileException, IOException { |
47 | |
try { |
48 | 120 | if (uri == null) { |
49 | 0 | throw new ProfileException("The input url parameter cannot be null"); |
50 | |
} |
51 | 120 | XMLReader reader = XMLReaderFactory.createXMLReader(); |
52 | 120 | reader.setContentHandler(new ProfileCodeStoreHandler()); |
53 | 120 | reader.parse(uri); |
54 | 0 | } catch (IOException e) { |
55 | 0 | throw e; |
56 | 0 | } catch (Exception e) { |
57 | 0 | throw new ProfileException(e.toString(), e); |
58 | 120 | } |
59 | 120 | } |
60 | |
|
61 | |
|
62 | |
public ProfileCodeStore(URL url) throws ProfileException, IOException { |
63 | 120 | this(url.toExternalForm()); |
64 | 120 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public String[] getValidCodes(String codeSystem) throws ProfileException { |
80 | 90 | List<String> result = getCodeTable(codeSystem); |
81 | 80 | if (result == null) |
82 | 5 | throw new ProfileException("Unknown code system: " + codeSystem); |
83 | 75 | return result.toArray(new String[result.size()]); |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public boolean knowsCodes(String codeSystem) { |
100 | |
try { |
101 | 785 | return getCodeTable(codeSystem) != null; |
102 | 20 | } catch (ProfileException e) { |
103 | 20 | return false; |
104 | |
} |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
private List<String> getCodeTable(String codeSystem) throws ProfileException { |
118 | 875 | if (codeSystem == null) { |
119 | 5 | throw new ProfileException("The input codeSystem parameter cannot be null"); |
120 | |
} |
121 | 870 | if (codeSystem.length() < 4) { |
122 | 25 | throw new ProfileException("The input codeSystem parameter cannot be less than 4 characters long"); |
123 | |
} |
124 | |
|
125 | 845 | String tableId = codeSystem.substring(codeSystem.length() - 4); |
126 | 845 | return codes.get(tableId); |
127 | |
} |
128 | |
|
129 | 240 | private class ProfileCodeStoreHandler extends DefaultHandler { |
130 | |
|
131 | |
private String currentTable; |
132 | |
private static final String HL7_TABLE_QNAME = "hl7table"; |
133 | |
private static final String TABLE_ELEMENT_QNAME = "tableElement"; |
134 | |
|
135 | |
@Override |
136 | |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
137 | 110640 | if (HL7_TABLE_QNAME.equals(qName)) { |
138 | 2620 | currentTable = attributes.getValue("id"); |
139 | 2620 | codes.put(currentTable, new ArrayList<String>()); |
140 | 108020 | } else if (TABLE_ELEMENT_QNAME.equals(qName)) { |
141 | 107300 | codes.get(currentTable).add(attributes.getValue("code")); |
142 | |
} |
143 | 110640 | } |
144 | |
|
145 | |
} |
146 | |
} |