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 WorkspaceProfile{ |
---|
61 | |
---|
62 | private static WorkspaceProfile singleton; |
---|
63 | private static Document workspace_doc; |
---|
64 | |
---|
65 | private String workspacefilename; |
---|
66 | private String username; |
---|
67 | private String elementtype; |
---|
68 | private String qid; |
---|
69 | private String qsid; |
---|
70 | //TODO counter not working |
---|
71 | private static final Integer START_QUERY = 1; |
---|
72 | |
---|
73 | // save element type |
---|
74 | public static String SE_WORKSPACE = "workspace"; |
---|
75 | public static String SE_QUERYSET = "queryset"; |
---|
76 | public static String SE_QUERY = "query"; |
---|
77 | public static String SE_BOOKMARKSET = "bookmarkset"; |
---|
78 | public static String SE_BOOKMARK = "bookmark"; |
---|
79 | |
---|
80 | public static String USER = "user"; |
---|
81 | public static String SERVER = "server"; |
---|
82 | |
---|
83 | public static String XPATH_EXPR_WORKSPACE = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='dummy']"; |
---|
84 | public static String XPATH_EXPR_REPOSITORIES = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='server']/Repositories"; |
---|
85 | public static String XPATH_EXPR_OPTIONS = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='server']/Options"; |
---|
86 | public static String XPATH_EXPR_TERMSETS = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='server']/Termsets"; |
---|
87 | public static String XPATH_EXPR_REPOPATH = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='server']/Repositories/item[@name='dummy']/uri"; |
---|
88 | public static String XPATH_EXPR_QUERY_PARENT = "//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[id='dummy']/Queries"; |
---|
89 | public static String XPATH_EXPR_QUERYSET = "//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[id='dummy']"; |
---|
90 | public static String XPATH_EXPR_WORKSPACE_QUERY = "//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='dummy']"; |
---|
91 | public static String XPATH_EXPR_BOOKMARKSET = "//Profiles/WorkspaceProfiles/WorkspaceProfile/Bookmarksets/item[id='dummy']"; |
---|
92 | public static String XPATH_EXPR_BOOKMARK_PARENT = "//Profiles/WorkspaceProfiles/WorkspaceProfile/Bookmarksets/item[id='dummy']/Bookmarks"; |
---|
93 | ///// |
---|
94 | private static HashMap<String,String> fcs_mapping = null; |
---|
95 | |
---|
96 | |
---|
97 | public void setQsid(String qsid) { |
---|
98 | this.qsid = qsid; |
---|
99 | } |
---|
100 | |
---|
101 | public String getQsid() { |
---|
102 | return qsid; |
---|
103 | } |
---|
104 | |
---|
105 | public void setQid(String qid) { |
---|
106 | this.qid = qid; |
---|
107 | } |
---|
108 | |
---|
109 | public String getQid() { |
---|
110 | return qid; |
---|
111 | } |
---|
112 | |
---|
113 | public void setElementtype(String elementtype) { |
---|
114 | this.elementtype = elementtype; |
---|
115 | } |
---|
116 | |
---|
117 | public String getElementtype() { |
---|
118 | return elementtype; |
---|
119 | } |
---|
120 | |
---|
121 | public void setUsername(String username) { |
---|
122 | this.username = username; |
---|
123 | } |
---|
124 | |
---|
125 | public String getUsername() { |
---|
126 | return username; |
---|
127 | } |
---|
128 | |
---|
129 | public WorkspaceProfile() { |
---|
130 | Init(); |
---|
131 | } |
---|
132 | |
---|
133 | public static WorkspaceProfile getWorkspaceProfile() { |
---|
134 | if (singleton == null) { |
---|
135 | singleton = new WorkspaceProfile(); |
---|
136 | } |
---|
137 | return singleton; |
---|
138 | } |
---|
139 | |
---|
140 | public void Init(){ |
---|
141 | //configure |
---|
142 | Utils.loadConfig("mdservice", "mdservice.properties", this.getClass().getClassLoader()); |
---|
143 | workspacefilename = Utils.getConfig("workspaceprofile.path") + Utils.getConfig("workspace.file"); |
---|
144 | username = SERVER; |
---|
145 | |
---|
146 | if (workspace_doc == null){ |
---|
147 | initDocument(); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | public void initDocument() { |
---|
152 | |
---|
153 | Integer counter = START_QUERY; |
---|
154 | File f = new File (workspacefilename); |
---|
155 | |
---|
156 | if (!f.exists()) { |
---|
157 | // create new counter document |
---|
158 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
159 | DocumentBuilder docBuilder; |
---|
160 | try { |
---|
161 | docBuilder = docFactory.newDocumentBuilder(); |
---|
162 | workspace_doc = docBuilder.newDocument(); |
---|
163 | // append root tag <index > |
---|
164 | Element root = (Element) workspace_doc.createElement("Profiles"); |
---|
165 | //root.setAttribute("idcounter", "1"); |
---|
166 | workspace_doc.appendChild(root); |
---|
167 | Element e = workspace_doc.createElement("Querycounter"); |
---|
168 | root.appendChild(e); |
---|
169 | e = workspace_doc.createElement("maxqid"); |
---|
170 | e.setNodeValue(counter.toString()); |
---|
171 | e = workspace_doc.createElement("WorkspaceProfiles"); |
---|
172 | root.appendChild(e); |
---|
173 | |
---|
174 | //updateQuerycounter(); |
---|
175 | this.saveDocument(); |
---|
176 | |
---|
177 | //log.debug("new document"); |
---|
178 | } catch (ParserConfigurationException e) { |
---|
179 | // TODO Auto-generated catch block |
---|
180 | e.printStackTrace(); |
---|
181 | } |
---|
182 | } |
---|
183 | else { |
---|
184 | try { |
---|
185 | |
---|
186 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
187 | DocumentBuilder docBuilder; |
---|
188 | try { |
---|
189 | docBuilder = docFactory.newDocumentBuilder(); |
---|
190 | try { |
---|
191 | workspace_doc = docBuilder.parse(f);//workspacefilename); |
---|
192 | } catch (SAXException e) { |
---|
193 | // TODO Auto-generated catch block |
---|
194 | |
---|
195 | e.printStackTrace(); |
---|
196 | } |
---|
197 | } catch (ParserConfigurationException e) { |
---|
198 | // TODO Auto-generated catch block |
---|
199 | e.printStackTrace(); |
---|
200 | } |
---|
201 | |
---|
202 | //read counter |
---|
203 | //counter = new Integer(workspace_doc.getFirstChild().getFirstChild().getFirstChild().getNodeValue()); |
---|
204 | //init = true; |
---|
205 | } catch (IOException ex){ |
---|
206 | //log.debug("initCacheCounter:" + ex.toString()); |
---|
207 | ex.printStackTrace(); |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | //return counter; |
---|
212 | } |
---|
213 | |
---|
214 | public void saveDocument() { |
---|
215 | //public void writeQuerycounter (Integer i) { |
---|
216 | |
---|
217 | // first update <index idcounter> |
---|
218 | //workspace_doc.getFirstChild().getAttributes().getNamedItem("idcounter").setNodeValue(i.toString()); |
---|
219 | |
---|
220 | //log.debug("writeCacheCounter:" + i.toString()); |
---|
221 | // write xml |
---|
222 | Transformer transformer; |
---|
223 | try { |
---|
224 | transformer = TransformerFactory.newInstance().newTransformer(); |
---|
225 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
226 | |
---|
227 | //initialize StreamResult with File object to save to file |
---|
228 | StreamResult result = new StreamResult(new StringWriter()); |
---|
229 | DOMSource source = new DOMSource(workspace_doc); |
---|
230 | try { |
---|
231 | transformer.transform(source, result); |
---|
232 | } catch (TransformerException e1) { |
---|
233 | // TODO Auto-generated catch block |
---|
234 | e1.printStackTrace(); |
---|
235 | } |
---|
236 | |
---|
237 | String xmlString = result.getWriter().toString(); |
---|
238 | |
---|
239 | File f = new File (workspacefilename); |
---|
240 | FileWriter fw; |
---|
241 | try { |
---|
242 | fw = new FileWriter(f); |
---|
243 | try { |
---|
244 | fw.write(xmlString); |
---|
245 | fw.close(); |
---|
246 | } catch (IOException e) { |
---|
247 | // TODO Auto-generated catch block |
---|
248 | e.printStackTrace(); |
---|
249 | } |
---|
250 | } catch (IOException e) { |
---|
251 | // TODO Auto-generated catch block |
---|
252 | e.printStackTrace(); |
---|
253 | } |
---|
254 | } catch (TransformerConfigurationException e2) { |
---|
255 | // TODO Auto-generated catch block |
---|
256 | e2.printStackTrace(); |
---|
257 | } catch (TransformerFactoryConfigurationError e2) { |
---|
258 | // TODO Auto-generated catch block |
---|
259 | e2.printStackTrace(); |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | public String getXPathExpressionDummy(String xpath_expr, String new_dummy){ |
---|
264 | |
---|
265 | return xpath_expr.replace("dummy", new_dummy); |
---|
266 | } |
---|
267 | |
---|
268 | public Object getWorkspaceData(String xpath_str, QName type) throws XPathExpressionException{ |
---|
269 | XPathFactory factory = XPathFactory.newInstance(); |
---|
270 | XPath xpath = factory.newXPath(); |
---|
271 | XPathExpression expr; |
---|
272 | expr = xpath.compile(xpath_str); |
---|
273 | //expression is evaluated with respect to a certain context node which is doc. |
---|
274 | Object result = null; |
---|
275 | try{ |
---|
276 | result = expr.evaluate(workspace_doc, type);//XPathConstants.NODESET); |
---|
277 | } catch(Exception e){ |
---|
278 | |
---|
279 | } |
---|
280 | |
---|
281 | return result; |
---|
282 | } |
---|
283 | |
---|
284 | public void addWorkspaceToDocument(){ |
---|
285 | //{'WorkspaceProfile':{'CustomTermsets':'null', |
---|
286 | // 'Termsets':'null', |
---|
287 | // 'Repositories':'null', |
---|
288 | // 'Querysets':'null','created':'null','lastchanged':'null','profilename':'null'}}; |
---|
289 | Element we = workspace_doc.createElement("WorkspaceProfile"); |
---|
290 | we.setAttribute("user", getUsername().toLowerCase()); |
---|
291 | workspace_doc.getFirstChild().appendChild(we); |
---|
292 | |
---|
293 | Element e = workspace_doc.createElement("CustomTermsets"); |
---|
294 | e.setNodeValue("null"); |
---|
295 | we.appendChild(e); |
---|
296 | |
---|
297 | e = workspace_doc.createElement("Termsets"); |
---|
298 | e.setNodeValue("null"); |
---|
299 | we.appendChild(e); |
---|
300 | |
---|
301 | e = workspace_doc.createElement("Repositories"); |
---|
302 | e.setNodeValue("null"); |
---|
303 | we.appendChild(e); |
---|
304 | |
---|
305 | e = workspace_doc.createElement("Querysets"); |
---|
306 | e.setNodeValue("null"); |
---|
307 | we.appendChild(e); |
---|
308 | |
---|
309 | DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); |
---|
310 | String strdate = dateFormat.format(new Date()).toString(); |
---|
311 | e = workspace_doc.createElement("created"); |
---|
312 | e.setNodeValue(strdate); |
---|
313 | we.appendChild(e); |
---|
314 | |
---|
315 | e = workspace_doc.createElement("lastchanged"); |
---|
316 | e.setNodeValue(strdate); |
---|
317 | we.appendChild(e); |
---|
318 | |
---|
319 | e = workspace_doc.createElement("profilename"); |
---|
320 | e.setNodeValue(getUsername().toLowerCase()); |
---|
321 | we.appendChild(e); |
---|
322 | } |
---|
323 | |
---|
324 | public InputStream getXMLStream(String xpath_expr) throws XPathExpressionException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError{ |
---|
325 | InputStream is; |
---|
326 | NodeList list = (NodeList) getWorkspaceData(xpath_expr, XPathConstants.NODESET);//result; |
---|
327 | if (list.getLength() < 1) { |
---|
328 | addWorkspaceToDocument(); |
---|
329 | list = (NodeList) getWorkspaceData(xpath_expr, XPathConstants.NODESET); |
---|
330 | } |
---|
331 | if (list.getLength() > 1){ |
---|
332 | // error |
---|
333 | } |
---|
334 | // nodelist2stream |
---|
335 | is = Utils.document2Stream(list.item(0)); |
---|
336 | return is; |
---|
337 | |
---|
338 | } |
---|
339 | |
---|
340 | //todo .has("Queries") |
---|
341 | public void parseDataInit(JSONObject json){ |
---|
342 | // workspaceelement |
---|
343 | if (json.has("Querysets")) { |
---|
344 | this.setQid("0"); |
---|
345 | this.setElementtype(WorkspaceProfile.SE_WORKSPACE); |
---|
346 | } else { |
---|
347 | // Querysetelement |
---|
348 | if (json.has("Queries")) { |
---|
349 | this.setElementtype(WorkspaceProfile.SE_QUERYSET); |
---|
350 | this.setQsid(json.getString("id")); |
---|
351 | } else { |
---|
352 | if (json.has("Bookmarks")) { |
---|
353 | this.setElementtype(WorkspaceProfile.SE_BOOKMARKSET); |
---|
354 | this.setQsid(json.getString("id")); |
---|
355 | } else { |
---|
356 | // Query-Bookmark-element |
---|
357 | if (json.getString("bookmark").equals("1")) { |
---|
358 | this.setElementtype(WorkspaceProfile.SE_BOOKMARK); |
---|
359 | } else { |
---|
360 | // Queryelement |
---|
361 | this.setElementtype(WorkspaceProfile.SE_QUERY); |
---|
362 | } |
---|
363 | this.setQid(json.getString("id")); |
---|
364 | } |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | } |
---|
369 | |
---|
370 | public String getXMLRootName(){ |
---|
371 | String rootname = "WorkspaceProfile"; |
---|
372 | if (this.elementtype.equals(WorkspaceProfile.SE_QUERY)){ |
---|
373 | rootname = "item"; |
---|
374 | } |
---|
375 | if (this.elementtype.equals(WorkspaceProfile.SE_QUERYSET)){ |
---|
376 | rootname = "item"; |
---|
377 | } |
---|
378 | return rootname; |
---|
379 | } |
---|
380 | |
---|
381 | public String save(String jsonData) throws ParserConfigurationException, SAXException, IOException, DOMException, XPathExpressionException, TransformerException, TransformerFactoryConfigurationError{ |
---|
382 | |
---|
383 | XMLSerializer serializer = new XMLSerializer(); |
---|
384 | JSON json = JSONSerializer.toJSON( jsonData ); |
---|
385 | // init qid, elementtype |
---|
386 | parseDataInit((JSONObject)json); |
---|
387 | serializer.setRootName(getXMLRootName()); //"WorkspaceProfile"); |
---|
388 | serializer.setElementName("item"); |
---|
389 | serializer.setTypeHintsEnabled(false); |
---|
390 | String xml = serializer.write( json ); |
---|
391 | //log.debug("XML:" + xml); |
---|
392 | InputStream is_xml = new ByteArrayInputStream( xml.getBytes( ) ); |
---|
393 | // add to document |
---|
394 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
---|
395 | DocumentBuilder builder = factory.newDocumentBuilder(); |
---|
396 | Document new_doc = builder.parse( is_xml ); |
---|
397 | String newid = DocumentReplaceNewElement(new_doc); |
---|
398 | //TODO |
---|
399 | saveDocument(); |
---|
400 | |
---|
401 | return newid.toString(); |
---|
402 | |
---|
403 | } |
---|
404 | |
---|
405 | public String createNewId(){//String name){ |
---|
406 | String name = "maxid"; |
---|
407 | String id = workspace_doc.getElementsByTagName(name).item(0).getTextContent(); |
---|
408 | Integer i = Integer.parseInt(id) + 1; |
---|
409 | id = i.toString(); |
---|
410 | workspace_doc.getElementsByTagName(name).item(0).setTextContent(id); |
---|
411 | return id.toString(); |
---|
412 | } |
---|
413 | |
---|
414 | public String DocumentReplaceNewElement(Document new_doc) throws XPathExpressionException{ |
---|
415 | String newid = ""; |
---|
416 | if (this.elementtype.equals(WorkspaceProfile.SE_WORKSPACE)){ |
---|
417 | NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_QUERY_PARENT, this.qsid),XPathConstants.NODESET); |
---|
418 | // remove |
---|
419 | //childeNode |
---|
420 | if (wsnodelist.getLength() > 0){ |
---|
421 | ((Node)wsnodelist.item(0)).getParentNode().removeChild(wsnodelist.item(0)); |
---|
422 | } |
---|
423 | |
---|
424 | Node node = new_doc.getFirstChild(); |
---|
425 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
426 | ((Element)node2).setAttribute("user", this.getUsername()); |
---|
427 | workspace_doc.getElementsByTagName("WorkspaceProfiles").item(0).appendChild(node2); |
---|
428 | } |
---|
429 | if (this.elementtype.equals(WorkspaceProfile.SE_QUERY)){ |
---|
430 | // new query |
---|
431 | Node node = new_doc.getFirstChild(); |
---|
432 | if (this.qid.equals("0")){ |
---|
433 | newid = createNewId();//"maxqid"); |
---|
434 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
435 | Element root = workspace_doc.getDocumentElement(); |
---|
436 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
437 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
438 | // apend to specific queryset |
---|
439 | NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_QUERY_PARENT, this.qsid),XPathConstants.NODESET);; |
---|
440 | Node anode = wsnodelist.item(0); |
---|
441 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
442 | anode.setTextContent(""); |
---|
443 | } |
---|
444 | anode.appendChild(node2); |
---|
445 | } |
---|
446 | |
---|
447 | else { |
---|
448 | // edit existing query |
---|
449 | NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_WORKSPACE_QUERY, this.qsid),XPathConstants.NODESET);;; |
---|
450 | // remove |
---|
451 | //childeNode |
---|
452 | if (wsnodelist.getLength() > 0){ |
---|
453 | //Element root = workspace_doc.getDocumentElement(); |
---|
454 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
455 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
456 | Node qnode = (Node)wsnodelist.item(0); |
---|
457 | //root.replaceChild(node2,(Node)wsnodelist.item(0)); |
---|
458 | |
---|
459 | Element parentElement = (Element)qnode.getParentNode(); |
---|
460 | parentElement.insertBefore(node2, qnode); |
---|
461 | qnode.getParentNode().removeChild(qnode); |
---|
462 | parentElement.normalize(); |
---|
463 | } |
---|
464 | } |
---|
465 | } |
---|
466 | if (this.elementtype.equals(WorkspaceProfile.SE_BOOKMARK)){ |
---|
467 | // new bookmark |
---|
468 | Node node = new_doc.getFirstChild(); |
---|
469 | if (this.qid.equals("0")){ |
---|
470 | newid = createNewId();//"maxbid"); |
---|
471 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
472 | Element root = workspace_doc.getDocumentElement(); |
---|
473 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
474 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
475 | // apend to specific bookmarkset |
---|
476 | NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_BOOKMARK_PARENT, this.qsid),XPathConstants.NODESET); |
---|
477 | Node anode = wsnodelist.item(0); |
---|
478 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
479 | anode.setTextContent(""); |
---|
480 | } |
---|
481 | anode.appendChild(node2); |
---|
482 | } |
---|
483 | |
---|
484 | else { |
---|
485 | // edit existing query |
---|
486 | NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_WORKSPACE_QUERY, this.qsid),XPathConstants.NODESET); |
---|
487 | // remove |
---|
488 | //childeNode |
---|
489 | if (wsnodelist.getLength() > 0){ |
---|
490 | //Element root = workspace_doc.getDocumentElement(); |
---|
491 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
492 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
493 | Node qnode = (Node)wsnodelist.item(0); |
---|
494 | //root.replaceChild(node2,(Node)wsnodelist.item(0)); |
---|
495 | |
---|
496 | Element parentElement = (Element)qnode.getParentNode(); |
---|
497 | parentElement.insertBefore(node2, qnode); |
---|
498 | qnode.getParentNode().removeChild(qnode); |
---|
499 | parentElement.normalize(); |
---|
500 | } |
---|
501 | } |
---|
502 | } |
---|
503 | //new queryset |
---|
504 | if (this.elementtype.equals(WorkspaceProfile.SE_QUERYSET)){ |
---|
505 | // new queryset |
---|
506 | Node node = new_doc.getFirstChild(); |
---|
507 | if (this.qsid.equals("0")){ |
---|
508 | newid = createNewId();//"maxqsid"); |
---|
509 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
510 | Element root = workspace_doc.getDocumentElement(); |
---|
511 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
512 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
513 | // apend to specific querysets |
---|
514 | NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_WORKSPACE, this.qsid),XPathConstants.NODESET); |
---|
515 | Element welem = (Element)wsnodelist.item(0); |
---|
516 | Node anode = welem.getElementsByTagName("Querysets").item(0); |
---|
517 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
518 | anode.setTextContent(""); |
---|
519 | } |
---|
520 | anode.appendChild(node2); |
---|
521 | } |
---|
522 | |
---|
523 | else { |
---|
524 | // edit existing queryset |
---|
525 | NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_QUERYSET, this.qsid),XPathConstants.NODESET); |
---|
526 | // remove |
---|
527 | //childeNode |
---|
528 | if (wsnodelist.getLength() > 0){ |
---|
529 | //Element root = workspace_doc.getDocumentElement(); |
---|
530 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
531 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
532 | Node qset = (Node)wsnodelist.item(0); |
---|
533 | |
---|
534 | Element parentElement = (Element)qset.getParentNode(); |
---|
535 | parentElement.insertBefore(node2, qset); |
---|
536 | qset.getParentNode().removeChild(qset); |
---|
537 | parentElement.normalize(); |
---|
538 | } |
---|
539 | } |
---|
540 | } |
---|
541 | |
---|
542 | //new bookmarkset |
---|
543 | if (this.elementtype.equals(WorkspaceProfile.SE_BOOKMARKSET)){ |
---|
544 | // new queryset |
---|
545 | Node node = new_doc.getFirstChild(); |
---|
546 | if (this.qsid.equals("0")){ |
---|
547 | newid = createNewId();//"maxbsid"); |
---|
548 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
549 | Element root = workspace_doc.getDocumentElement(); |
---|
550 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
551 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
552 | // apend to specific querysets |
---|
553 | NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_WORKSPACE, this.qsid),XPathConstants.NODESET); |
---|
554 | Element welem = (Element)wsnodelist.item(0); |
---|
555 | Node anode = welem.getElementsByTagName("Bookmarksets").item(0); |
---|
556 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
557 | anode.setTextContent(""); |
---|
558 | } |
---|
559 | anode.appendChild(node2); |
---|
560 | } |
---|
561 | |
---|
562 | else { |
---|
563 | // edit existing bookmarkset |
---|
564 | NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_BOOKMARKSET, this.qsid),XPathConstants.NODESET); |
---|
565 | // remove |
---|
566 | //childeNode |
---|
567 | if (wsnodelist.getLength() > 0){ |
---|
568 | //Element root = workspace_doc.getDocumentElement(); |
---|
569 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
570 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
571 | Node qset = (Node)wsnodelist.item(0); |
---|
572 | |
---|
573 | Element parentElement = (Element)qset.getParentNode(); |
---|
574 | parentElement.insertBefore(node2, qset); |
---|
575 | qset.getParentNode().removeChild(qset); |
---|
576 | parentElement.normalize(); |
---|
577 | } |
---|
578 | } |
---|
579 | } |
---|
580 | return newid; |
---|
581 | } |
---|
582 | |
---|
583 | //TODO Utils |
---|
584 | public static InputStream createStream(String value) throws TransformerException, TransformerFactoryConfigurationError, ParserConfigurationException, UnsupportedEncodingException{ |
---|
585 | DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); |
---|
586 | DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); |
---|
587 | Document doc = docBuilder.newDocument(); |
---|
588 | |
---|
589 | //////////////////////// |
---|
590 | //Creating the XML tree |
---|
591 | Element root = doc.createElement("root"); |
---|
592 | root.setTextContent(value); |
---|
593 | doc.appendChild(root); |
---|
594 | |
---|
595 | DOMSource source = new DOMSource(doc); |
---|
596 | StringWriter xmlAsWriter = new StringWriter(); |
---|
597 | StreamResult result = new StreamResult(xmlAsWriter); |
---|
598 | TransformerFactory.newInstance().newTransformer().transform(source, result); |
---|
599 | |
---|
600 | // write changes |
---|
601 | ByteArrayInputStream is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8")); |
---|
602 | |
---|
603 | |
---|
604 | return is; |
---|
605 | |
---|
606 | |
---|
607 | } |
---|
608 | |
---|
609 | } |
---|
610 | |
---|
611 | |
---|