1 | package eu.clarin.cmdi.mdservice.model; |
---|
2 | |
---|
3 | import java.util.HashMap; |
---|
4 | |
---|
5 | import javax.xml.xpath.XPath; |
---|
6 | import javax.xml.xpath.XPathConstants; |
---|
7 | import javax.xml.xpath.XPathExpression; |
---|
8 | import javax.xml.xpath.XPathExpressionException; |
---|
9 | import javax.xml.xpath.XPathFactory; |
---|
10 | |
---|
11 | import org.apache.log4j.Logger; |
---|
12 | import org.w3c.dom.Document; |
---|
13 | import org.w3c.dom.Node; |
---|
14 | import org.w3c.dom.NodeList; |
---|
15 | |
---|
16 | import eu.clarin.cmdi.mdservice.internal.Utils; |
---|
17 | |
---|
18 | public class Termset { |
---|
19 | |
---|
20 | public static Logger log = Logger.getLogger("Termset"); |
---|
21 | |
---|
22 | private final static HashMap<String,Object> termsets = new HashMap<String,Object>(); |
---|
23 | |
---|
24 | private final HashMap<String,String> attrs = new HashMap<String,String>(); |
---|
25 | |
---|
26 | public Termset () { |
---|
27 | } |
---|
28 | |
---|
29 | public Termset (Node n) { |
---|
30 | this.setAttrs(n); |
---|
31 | } |
---|
32 | |
---|
33 | public void setAttrs(Node n) { |
---|
34 | |
---|
35 | for (int i=0; i < n.getAttributes().getLength(); i++) { |
---|
36 | String key = n.getAttributes().item(i).getNodeName(); |
---|
37 | String value = n.getAttributes().item(i).getNodeValue(); |
---|
38 | attrs.put(key, value); |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | public String getAttr(String key) { |
---|
43 | return attrs.get(key); |
---|
44 | } |
---|
45 | |
---|
46 | public static HashMap<String, Object> getTermsets() { |
---|
47 | if (termsets.size() == 0) { |
---|
48 | initTermsets(); |
---|
49 | } |
---|
50 | return termsets; |
---|
51 | } |
---|
52 | |
---|
53 | public static Termset getTermset(String ts_id) { |
---|
54 | |
---|
55 | Termset res = (Termset) getTermsets().get(ts_id); |
---|
56 | if (res==null) log.debug("getTermset(): no termset found for:" + ts_id); |
---|
57 | //else |
---|
58 | //log.debug("getDCR():" + res.getAttr("id") + ":" + res.getAttr("url") ); |
---|
59 | return res; |
---|
60 | } |
---|
61 | |
---|
62 | public static String getTermsetAttr(String ts_id, String key) { |
---|
63 | Termset ts = getTermset(ts_id); |
---|
64 | String val=""; |
---|
65 | if (ts!=null) val= ts.getAttr(key); |
---|
66 | return val; |
---|
67 | } |
---|
68 | |
---|
69 | public static void initTermsets() { |
---|
70 | Document dcrs_setup = Utils.load2Document(Utils.getConfig("terms.file")); |
---|
71 | |
---|
72 | // Utils.load2(Utils.getConfig().getProperty("service.list.file")); |
---|
73 | log.debug("initTermsets().loaded."); |
---|
74 | //.getDocumentElement().getLocalName() |
---|
75 | //Document chains_doc = Utils.load2Document(Utils.getConfig().getProperty("chains.file")); |
---|
76 | |
---|
77 | //log.debug("formPath:xpath:"+xpath_expr); |
---|
78 | //creating an XPathFactory: |
---|
79 | XPathFactory factory = XPathFactory.newInstance(); |
---|
80 | //using this factory to create an XPath object: |
---|
81 | XPath xpath = factory.newXPath(); |
---|
82 | //XPath object created compiles the XPath expression: |
---|
83 | XPathExpression expr; |
---|
84 | try { |
---|
85 | expr = xpath.compile("//Termset"); |
---|
86 | //expression is evaluated with respect to a certain context node which is doc. |
---|
87 | Object result = expr.evaluate(dcrs_setup, XPathConstants.NODESET); |
---|
88 | NodeList list = (NodeList) result; |
---|
89 | |
---|
90 | // <Termset type="dcr" id="isocat" label="isoCAT" url="http://www.isocat.org/rest/" format="dcif" /> |
---|
91 | for (int i = 0; i<list.getLength();i++) { |
---|
92 | //String value = list.item(i).getTextContent(); |
---|
93 | Node n = list.item(i); |
---|
94 | Termset ts = new Termset(n); |
---|
95 | |
---|
96 | log.debug("reading in Termset: " + ts.getAttr("id")); |
---|
97 | termsets.put(ts.getAttr("id"), ts ); |
---|
98 | } |
---|
99 | } catch (XPathExpressionException e) { |
---|
100 | // TODO Auto-generated catch block |
---|
101 | e.printStackTrace(); |
---|
102 | } |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | } |
---|