1 | package eu.clarin.cmdi.mdservice.model; |
---|
2 | |
---|
3 | import java.io.BufferedInputStream; |
---|
4 | import java.io.ByteArrayInputStream; |
---|
5 | import java.io.File; |
---|
6 | import java.io.FileInputStream; |
---|
7 | import java.io.FileWriter; |
---|
8 | import java.io.IOException; |
---|
9 | import java.io.InputStream; |
---|
10 | import java.io.StringWriter; |
---|
11 | import java.io.UnsupportedEncodingException; |
---|
12 | import java.net.URL; |
---|
13 | import java.text.DateFormat; |
---|
14 | import java.text.SimpleDateFormat; |
---|
15 | import java.util.Date; |
---|
16 | import java.util.HashMap; |
---|
17 | |
---|
18 | import javax.xml.namespace.QName; |
---|
19 | import javax.xml.parsers.DocumentBuilder; |
---|
20 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
21 | import javax.xml.parsers.ParserConfigurationException; |
---|
22 | import javax.xml.transform.OutputKeys; |
---|
23 | import javax.xml.transform.Transformer; |
---|
24 | import javax.xml.transform.TransformerConfigurationException; |
---|
25 | import javax.xml.transform.TransformerException; |
---|
26 | import javax.xml.transform.TransformerFactory; |
---|
27 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
28 | import javax.xml.transform.dom.DOMSource; |
---|
29 | import javax.xml.transform.stream.StreamResult; |
---|
30 | import javax.xml.xpath.XPath; |
---|
31 | import javax.xml.xpath.XPathConstants; |
---|
32 | import javax.xml.xpath.XPathExpression; |
---|
33 | import javax.xml.xpath.XPathExpressionException; |
---|
34 | import javax.xml.xpath.XPathFactory; |
---|
35 | |
---|
36 | import org.w3c.dom.DOMException; |
---|
37 | import org.w3c.dom.Document; |
---|
38 | import org.w3c.dom.Element; |
---|
39 | import org.w3c.dom.Node; |
---|
40 | import org.w3c.dom.NodeList; |
---|
41 | import org.xml.sax.SAXException; |
---|
42 | |
---|
43 | import net.sf.json.JSON; |
---|
44 | import net.sf.json.JSONArray; |
---|
45 | import net.sf.json.JSONObject; |
---|
46 | import net.sf.json.JSONSerializer; |
---|
47 | import net.sf.json.xml.XMLSerializer; |
---|
48 | import eu.clarin.cmdi.mdservice.internal.MDTransformer; |
---|
49 | import eu.clarin.cmdi.mdservice.internal.Utils; |
---|
50 | import eu.clarin.cmdi.mdservice.action.FCSAction; |
---|
51 | import eu.clarin.cmdi.mdservice.action.WorkspaceAction; |
---|
52 | import eu.clarin.cmdi.mdservice.internal.NoStylesheetException; |
---|
53 | |
---|
54 | /** |
---|
55 | |
---|
56 | */ |
---|
57 | |
---|
58 | |
---|
59 | |
---|
60 | public class Repositories{ |
---|
61 | |
---|
62 | private static Repositories singleton; |
---|
63 | private static NodeList repositories_nodelist; |
---|
64 | |
---|
65 | |
---|
66 | public static enum RepositoryType |
---|
67 | { |
---|
68 | MD, SRU, PAZPAR, FCS, FCSRESOURCE, NOVALUE; |
---|
69 | |
---|
70 | public static RepositoryType toRepositoryType(String str) |
---|
71 | { |
---|
72 | try { |
---|
73 | return valueOf(str.replace(".", "").toUpperCase()); |
---|
74 | } |
---|
75 | catch (Exception ex) { |
---|
76 | return NOVALUE; |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | public NodeList getRepositoriesNodeList(){ |
---|
82 | return repositories_nodelist; |
---|
83 | } |
---|
84 | |
---|
85 | public Repositories() { |
---|
86 | if (repositories_nodelist == null){ |
---|
87 | Init(); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | public static Repositories getRepositories() { |
---|
93 | if (singleton == null) { |
---|
94 | singleton = new Repositories(); |
---|
95 | } |
---|
96 | return singleton; |
---|
97 | } |
---|
98 | |
---|
99 | public void Init(){ |
---|
100 | WorkspaceProfile wsp = WorkspaceProfile.getWorkspaceProfile(); |
---|
101 | try { |
---|
102 | repositories_nodelist = (NodeList ) wsp.getWorkspaceData( |
---|
103 | wsp.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_REPOSITORIES, WorkspaceProfile.SERVER), |
---|
104 | XPathConstants.NODESET); |
---|
105 | } catch (XPathExpressionException e) { |
---|
106 | // TODO Auto-generated catch block |
---|
107 | e.printStackTrace(); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | public Object getDocumentData(Document doc, String xpath_str, QName type) throws XPathExpressionException{ |
---|
112 | XPathFactory factory = XPathFactory.newInstance(); |
---|
113 | XPath xpath = factory.newXPath(); |
---|
114 | XPathExpression expr; |
---|
115 | expr = xpath.compile(xpath_str); |
---|
116 | //expression is evaluated with respect to a certain context node which is doc. |
---|
117 | Object result = null; |
---|
118 | try{ |
---|
119 | result = expr.evaluate(doc, type);//XPathConstants.NODESET); |
---|
120 | } catch(Exception e){ |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | return result; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | public String getRepositoryByIndex(int ind){ |
---|
130 | String repository_name = null; |
---|
131 | Integer icount = 0; |
---|
132 | |
---|
133 | NodeList chlist = repositories_nodelist.item(0).getChildNodes(); |
---|
134 | if (chlist.getLength() > ind){ |
---|
135 | for(int i=0;i<chlist.getLength();i++){ |
---|
136 | Node node = chlist.item(i); |
---|
137 | if (node.getNodeName().equals("item") && icount == ind){ |
---|
138 | NodeList list = node.getChildNodes(); |
---|
139 | for(int j=0;j<list.getLength();j++){ |
---|
140 | if (list.item(j).getNodeName().equals("name")){ |
---|
141 | return list.item(j).getTextContent(); |
---|
142 | |
---|
143 | } |
---|
144 | } |
---|
145 | icount++; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | return repository_name; |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | public String getRepositoryType(String repository_name){ |
---|
157 | String typestr = ""; |
---|
158 | boolean selected = false; |
---|
159 | |
---|
160 | NodeList chlist = repositories_nodelist.item(0).getChildNodes(); |
---|
161 | for(int i=0;i<chlist.getLength();i++){ |
---|
162 | Node node = chlist.item(i); |
---|
163 | if (node.getNodeName().equals("item")){ |
---|
164 | NodeList list = node.getChildNodes(); |
---|
165 | for(int j=0;j<list.getLength();j++){ |
---|
166 | if (list.item(j).getNodeName().equals("name") && list.item(j).getTextContent().equals(repository_name)){ |
---|
167 | selected = true; |
---|
168 | } |
---|
169 | } |
---|
170 | if (selected){ |
---|
171 | for(int j=0;j<list.getLength();j++){ |
---|
172 | if (list.item(j).getNodeName().equals("type")){ |
---|
173 | typestr = list.item(j).getTextContent(); |
---|
174 | } |
---|
175 | } |
---|
176 | return typestr; |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | return (typestr); |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | public String getRepositoryPath(String repository_name){ |
---|
187 | String uri = ""; |
---|
188 | boolean selected = false; |
---|
189 | |
---|
190 | NodeList chlist = repositories_nodelist.item(0).getChildNodes(); |
---|
191 | for(int i=0;i<chlist.getLength();i++){ |
---|
192 | Node node = chlist.item(i); |
---|
193 | if (node.getNodeName().equals("item")){ |
---|
194 | for(int j=0;j<node.getChildNodes().getLength();j++){ |
---|
195 | if (node.getChildNodes().item(j).getNodeName().equals("name") && node.getChildNodes().item(j).getTextContent().equals(repository_name)){ |
---|
196 | selected = true; |
---|
197 | } |
---|
198 | } |
---|
199 | if (selected){ |
---|
200 | for(int j=0;j<node.getChildNodes().getLength();j++){ |
---|
201 | if (node.getChildNodes().item(j).getNodeName().equals("uri")){ |
---|
202 | uri = node.getChildNodes().item(j).getTextContent(); |
---|
203 | } |
---|
204 | } |
---|
205 | return uri; |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | return (uri); |
---|
211 | |
---|
212 | } |
---|
213 | |
---|
214 | public Boolean repositoryExists(String repository_name){ |
---|
215 | |
---|
216 | NodeList chlist = repositories_nodelist.item(0).getChildNodes(); |
---|
217 | for(int i=0;i<chlist.getLength();i++){ |
---|
218 | Node node = chlist.item(i); |
---|
219 | if (node.getNodeName().equals("item")){ |
---|
220 | for(int j=0;j<node.getChildNodes().getLength();j++){ |
---|
221 | if (node.getChildNodes().item(j).getNodeName().equals("name") && node.getChildNodes().item(j).getTextContent().equals(repository_name)){ |
---|
222 | return true; |
---|
223 | } |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | return false; |
---|
230 | |
---|
231 | } |
---|
232 | |
---|
233 | public InputStream getXMLStream(String extradata) throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError{ |
---|
234 | if (extradata.equals("fcs.resource")){ |
---|
235 | return FCSRepositories.getFCSRepositories().getXMLStream(); |
---|
236 | } |
---|
237 | return Utils.document2Stream(repositories_nodelist.item(0)); |
---|
238 | |
---|
239 | } |
---|
240 | |
---|
241 | public InputStream getSRUStream(){ |
---|
242 | //if () addFCSRepositories |
---|
243 | |
---|
244 | // convert to SRU |
---|
245 | // SRU nodelist |
---|
246 | // add FCS to nodelist |
---|
247 | // convert to xml |
---|
248 | return null; |
---|
249 | |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | } |
---|
255 | |
---|
256 | |
---|