1 | package eu.clarin.cmdi.mdservice.model; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.util.HashMap; |
---|
6 | import javax.xml.parsers.DocumentBuilder; |
---|
7 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
8 | import javax.xml.parsers.ParserConfigurationException; |
---|
9 | import javax.xml.transform.TransformerConfigurationException; |
---|
10 | import javax.xml.transform.TransformerException; |
---|
11 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
12 | import org.w3c.dom.Document; |
---|
13 | import org.w3c.dom.Element; |
---|
14 | import org.w3c.dom.Node; |
---|
15 | import org.w3c.dom.NodeList; |
---|
16 | import org.xml.sax.SAXException; |
---|
17 | |
---|
18 | import eu.clarin.cmdi.mdservice.internal.MDTransformer; |
---|
19 | import eu.clarin.cmdi.mdservice.internal.Utils; |
---|
20 | import eu.clarin.cmdi.mdservice.action.FCSAction; |
---|
21 | import eu.clarin.cmdi.mdservice.internal.NoStylesheetException; |
---|
22 | |
---|
23 | /** |
---|
24 | |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | public class FCSRepositories{ |
---|
30 | |
---|
31 | private static FCSRepositories singleton; |
---|
32 | private static Document fcsrepositories_document; |
---|
33 | |
---|
34 | public static HashMap<String,String> fcs_mapping; |
---|
35 | |
---|
36 | public FCSRepositories() { |
---|
37 | if (fcsrepositories_document == null){ |
---|
38 | Init(); |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | public static FCSRepositories getFCSRepositories() { |
---|
43 | if (singleton == null) { |
---|
44 | singleton = new FCSRepositories(); |
---|
45 | } |
---|
46 | return singleton; |
---|
47 | } |
---|
48 | |
---|
49 | public void Init(){ |
---|
50 | NodeList ilist, nlist; |
---|
51 | String type = null, name = null, uri = null; |
---|
52 | NodeList fcsnodes, fcsrepositories_nodelist; |
---|
53 | Document fcsdoc; |
---|
54 | Node fcsnode; |
---|
55 | |
---|
56 | Repositories rep = Repositories.getRepositories(); |
---|
57 | fcsrepositories_nodelist = (NodeList ) rep.getRepositoriesNodeList(); |
---|
58 | nlist = fcsrepositories_nodelist.item(0).getChildNodes(); |
---|
59 | |
---|
60 | fcs_mapping = new HashMap<String,String>(); |
---|
61 | fcsrepositories_document = Utils.createDocument("Repositories"); |
---|
62 | |
---|
63 | Element keycontainer = fcsrepositories_document.getDocumentElement(); |
---|
64 | //keycontainer.setAttributeNS(null, "Version", "1.0"); |
---|
65 | //xmlns:sru="http://www.loc.gov/zing/srw/ |
---|
66 | keycontainer.setAttributeNS("http://www.w3.org/2000/xmlns/", |
---|
67 | "xmlns:srw", |
---|
68 | "http://www.loc.gov/zing/srw/"); |
---|
69 | keycontainer.setAttributeNS("http://www.w3.org/2000/xmlns/", |
---|
70 | "xmlns:sru", |
---|
71 | "http://www.loc.gov/zing/srw/"); |
---|
72 | |
---|
73 | nlist = fcsrepositories_nodelist.item(0).getChildNodes(); |
---|
74 | for(int i=0;i<nlist.getLength();i++){ |
---|
75 | if (nlist.item(i).getNodeName().equals("item")){ |
---|
76 | fcsnode = fcsrepositories_document.adoptNode(nlist.item(i).cloneNode(true)); |
---|
77 | fcsrepositories_document.getChildNodes().item(0).appendChild(fcsnode); |
---|
78 | |
---|
79 | ilist = fcsnode.getChildNodes(); |
---|
80 | for(int j=0;j<ilist.getLength();j++){ |
---|
81 | if (ilist.item(j).getNodeName().equals("type")) |
---|
82 | type = ilist.item(j).getFirstChild().getNodeValue(); |
---|
83 | if (ilist.item(j).getNodeName().equals("name")) |
---|
84 | name = ilist.item(j).getFirstChild().getNodeValue(); |
---|
85 | if (ilist.item(j).getNodeName().equals("uri")) |
---|
86 | uri = ilist.item(j).getFirstChild().getNodeValue(); |
---|
87 | } |
---|
88 | if (type.equals("fcs.resource")){ |
---|
89 | fcsdoc = FCSRepositories.getFCSData(name); |
---|
90 | addFCS2Repositories(fcsnode, fcsdoc, name); |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | public String getFCSMapping(String context){ |
---|
97 | return this.fcs_mapping.get(context); |
---|
98 | } |
---|
99 | |
---|
100 | public void addFCS2Repositories(Node node, Document fcs_doc, String name){ |
---|
101 | Node extraTermNode, extraTermNodeAdopted, eNode; |
---|
102 | String ename = null; |
---|
103 | NodeList termsnodelist, termlist; |
---|
104 | |
---|
105 | NodeList elist = fcs_doc.getElementsByTagName("sru:extraTermData"); |
---|
106 | for(int i=0;i<elist.getLength();i++){ |
---|
107 | extraTermNode = elist.item(i); |
---|
108 | eNode = extraTermNode; |
---|
109 | while (!eNode.getNodeName().equals("sru:value")){ |
---|
110 | eNode = eNode.getPreviousSibling(); |
---|
111 | } |
---|
112 | if (eNode.getTextContent().equals(name)){ |
---|
113 | extraTermNodeAdopted = fcsrepositories_document.adoptNode(extraTermNode.cloneNode(true)); |
---|
114 | node.appendChild(extraTermNodeAdopted); |
---|
115 | termsnodelist = extraTermNodeAdopted.getChildNodes().item(1).getChildNodes(); |
---|
116 | for(int k=0;k<termsnodelist.getLength();k++){ |
---|
117 | termlist = termsnodelist.item(k).getChildNodes(); |
---|
118 | for(int l=0;l<termlist.getLength();l++){ |
---|
119 | if (termlist.item(l).getNodeName().equals("sru:value")){ |
---|
120 | ename = termlist.item(l).getTextContent(); |
---|
121 | this.fcs_mapping.put(ename, name); |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | public static Document getFCSData(String name){ |
---|
130 | |
---|
131 | Document doc; |
---|
132 | |
---|
133 | FCSAction fcsa = new FCSAction(); |
---|
134 | fcsa.setActionkey("fcs"); |
---|
135 | fcsa.setParam("operation", "scan"); |
---|
136 | fcsa.setParam("scanClause", "fcs.resource"); |
---|
137 | fcsa.setParam("x-context", name); |
---|
138 | |
---|
139 | try { |
---|
140 | fcsa.execute(); |
---|
141 | |
---|
142 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
143 | DocumentBuilder docBuilder; |
---|
144 | try { |
---|
145 | docBuilder = docFactory.newDocumentBuilder(); |
---|
146 | try { |
---|
147 | doc = docBuilder.parse(fcsa.getResultStream()); |
---|
148 | return doc; |
---|
149 | } catch (SAXException e) { |
---|
150 | // TODO Auto-generated catch block |
---|
151 | |
---|
152 | e.printStackTrace(); |
---|
153 | } |
---|
154 | } catch (ParserConfigurationException e) { |
---|
155 | // TODO Auto-generated catch block |
---|
156 | e.printStackTrace(); |
---|
157 | } |
---|
158 | } catch (Exception e) { |
---|
159 | // TODO Auto-generated catch block |
---|
160 | e.printStackTrace(); |
---|
161 | } |
---|
162 | return null; |
---|
163 | |
---|
164 | } |
---|
165 | |
---|
166 | public InputStream getXMLStream() throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError{ |
---|
167 | |
---|
168 | InputStream srustream = null; |
---|
169 | InputStream xmlstream = Utils.document2Stream(fcsrepositories_document.getFirstChild()); |
---|
170 | // convert to sru |
---|
171 | MDTransformer transformer = new MDTransformer(); |
---|
172 | transformer.configure(Utils.getAppConfig("mdservice"), this.getClass().getClassLoader()); |
---|
173 | //transformer.setSrcFile(getTargetRequest()); |
---|
174 | transformer.setParams(MDTransformer.createParamsMap("xml2sruscan")); |
---|
175 | try { |
---|
176 | srustream = transformer.transformXML(xmlstream); |
---|
177 | } catch (IOException e) { |
---|
178 | // TODO Auto-generated catch block |
---|
179 | e.printStackTrace(); |
---|
180 | } catch (InterruptedException e) { |
---|
181 | // TODO Auto-generated catch block |
---|
182 | e.printStackTrace(); |
---|
183 | } catch (NoStylesheetException e) { |
---|
184 | // TODO Auto-generated catch block |
---|
185 | e.printStackTrace(); |
---|
186 | } |
---|
187 | return srustream; |
---|
188 | |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | } |
---|
194 | |
---|
195 | |
---|