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