1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.BufferedInputStream; |
---|
4 | import java.io.ByteArrayInputStream; |
---|
5 | import java.io.ByteArrayOutputStream; |
---|
6 | import java.io.File; |
---|
7 | import java.io.FileInputStream; |
---|
8 | import java.io.FileWriter; |
---|
9 | import java.io.IOException; |
---|
10 | import java.io.InputStream; |
---|
11 | import java.io.StringWriter; |
---|
12 | import java.io.UnsupportedEncodingException; |
---|
13 | import java.net.MalformedURLException; |
---|
14 | import java.net.URL; |
---|
15 | import java.text.DateFormat; |
---|
16 | import java.text.SimpleDateFormat; |
---|
17 | import java.util.Date; |
---|
18 | |
---|
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.Result; |
---|
24 | import javax.xml.transform.Source; |
---|
25 | import javax.xml.transform.Transformer; |
---|
26 | import javax.xml.transform.TransformerConfigurationException; |
---|
27 | import javax.xml.transform.TransformerException; |
---|
28 | import javax.xml.transform.TransformerFactory; |
---|
29 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
30 | import javax.xml.transform.dom.DOMSource; |
---|
31 | import javax.xml.transform.stream.StreamResult; |
---|
32 | import javax.xml.xpath.XPath; |
---|
33 | import javax.xml.xpath.XPathConstants; |
---|
34 | import javax.xml.xpath.XPathExpression; |
---|
35 | import javax.xml.xpath.XPathExpressionException; |
---|
36 | import javax.xml.xpath.XPathFactory; |
---|
37 | |
---|
38 | import net.sf.json.JSON; |
---|
39 | import net.sf.json.JSONObject; |
---|
40 | import net.sf.json.JSONSerializer; |
---|
41 | import net.sf.json.xml.XMLSerializer; |
---|
42 | |
---|
43 | import org.apache.log4j.Logger; |
---|
44 | import org.w3c.dom.DOMException; |
---|
45 | import org.w3c.dom.Document; |
---|
46 | import org.w3c.dom.Element; |
---|
47 | import org.w3c.dom.Node; |
---|
48 | import org.w3c.dom.NodeList; |
---|
49 | import org.xml.sax.SAXException; |
---|
50 | |
---|
51 | import eu.clarin.cmdi.mdservice.internal.Admin; |
---|
52 | import eu.clarin.cmdi.mdservice.internal.MDTransformer; |
---|
53 | import eu.clarin.cmdi.mdservice.model.Diagnostic; |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | * |
---|
58 | * @author |
---|
59 | * |
---|
60 | */ |
---|
61 | public class WorkspaceAction extends GenericAction |
---|
62 | //implements ServletRequestAware |
---|
63 | { |
---|
64 | |
---|
65 | private static final long serialVersionUID = 1L; |
---|
66 | private static Logger log = Logger.getLogger("WorkspaceAction"); |
---|
67 | |
---|
68 | // workspaceprofiles |
---|
69 | public static String USER = "user"; |
---|
70 | public static String SERVER = "server"; |
---|
71 | |
---|
72 | // save element type |
---|
73 | public static String SE_WORKSPACE = "workspace"; |
---|
74 | public static String SE_QUERYSET = "queryset"; |
---|
75 | public static String SE_QUERY = "query"; |
---|
76 | public static String SE_BOOKMARKSET = "bookmarkset"; |
---|
77 | public static String SE_BOOKMARK = "bookmark"; |
---|
78 | |
---|
79 | // administration query identification |
---|
80 | public static String ADMIN = "admin"; |
---|
81 | public static String FILENAME = "filename"; |
---|
82 | |
---|
83 | public static String PROFILENAME_BASE = "WorkspaceProfile_"; |
---|
84 | public static String PROFILENAME_SERVER = "WorkspaceProfileServer.xml"; |
---|
85 | public static String PROFILENAME_DEFAULT = PROFILENAME_BASE + "default.xml"; |
---|
86 | public static String ADMINISTRATION_FILENAME = "Administration.xml"; |
---|
87 | |
---|
88 | public static String WORKSPACE_FILENAME = "WorkspaceProfile.xml"; |
---|
89 | /** |
---|
90 | * Properties to be filled by Struts with according request-parameters |
---|
91 | */ |
---|
92 | //private String actionkey; |
---|
93 | private String data; |
---|
94 | private String type; //workspacetype |
---|
95 | private String elementtype; |
---|
96 | private String qid; |
---|
97 | private String qsid; |
---|
98 | //private String format; |
---|
99 | //private String userMsg; |
---|
100 | private String base_path; |
---|
101 | |
---|
102 | |
---|
103 | private static WorkspaceAction singleton; |
---|
104 | private static String workspacefilename; |
---|
105 | private static Document workspace_doc; |
---|
106 | |
---|
107 | //private String workspaceindex_path; |
---|
108 | //private static Integer querycounter; |
---|
109 | private static final Integer start_query = 1; |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | //private HttpServletRequest request; |
---|
114 | |
---|
115 | public String Workspace_filename(){ |
---|
116 | return WORKSPACE_FILENAME; |
---|
117 | } |
---|
118 | |
---|
119 | public void setWorkspace_filename(String workspace_filename) { |
---|
120 | this.WORKSPACE_FILENAME = workspace_filename; |
---|
121 | } |
---|
122 | |
---|
123 | public String getData(){ |
---|
124 | return data; |
---|
125 | } |
---|
126 | |
---|
127 | public void setData(String data) { |
---|
128 | this.data = data; |
---|
129 | } |
---|
130 | |
---|
131 | public String getEelemnttype(){ |
---|
132 | return elementtype; |
---|
133 | } |
---|
134 | |
---|
135 | public void setElementtype(String elementtype) { |
---|
136 | this.elementtype = elementtype; |
---|
137 | } |
---|
138 | |
---|
139 | public String getType() { |
---|
140 | return type; |
---|
141 | } |
---|
142 | |
---|
143 | public void setType(String type) { |
---|
144 | this.type = type; |
---|
145 | } |
---|
146 | |
---|
147 | public String getQid() { |
---|
148 | return qid; |
---|
149 | } |
---|
150 | |
---|
151 | public void setQid(String qid) { |
---|
152 | this.qid = qid; |
---|
153 | } |
---|
154 | |
---|
155 | public void setQsid(String qsid) { |
---|
156 | this.qsid = qsid; |
---|
157 | } |
---|
158 | |
---|
159 | public String getQsid() { |
---|
160 | return qsid; |
---|
161 | } |
---|
162 | |
---|
163 | public WorkspaceAction () { |
---|
164 | this.elementtype = WorkspaceAction.SE_WORKSPACE; |
---|
165 | workspacefilename = Admin.getConfig().getProperty("workspaceprofile.path") + Admin.getConfig().getProperty("workspace.file"); |
---|
166 | if (workspace_doc == null){ |
---|
167 | //workspacefilename = Admin.getConfig().getProperty("workspace.path") + Admin.getConfig().getProperty("workspace.file"); |
---|
168 | initDocument(); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | public static WorkspaceAction getWorkspaceAction() { |
---|
173 | if (singleton == null) { |
---|
174 | singleton = new WorkspaceAction(); |
---|
175 | } |
---|
176 | return singleton; |
---|
177 | } |
---|
178 | |
---|
179 | protected void finalize() throws Throwable { |
---|
180 | try { |
---|
181 | //updateQuerycounter(); |
---|
182 | //saveDocument(); |
---|
183 | } catch(Exception e) { |
---|
184 | } |
---|
185 | finally { |
---|
186 | super.finalize(); |
---|
187 | //more code can be written here as per need of application |
---|
188 | } |
---|
189 | } |
---|
190 | /** |
---|
191 | * primitive identification of the target-proxy |
---|
192 | * base for finding the right base_url in the props |
---|
193 | * subclass has to override with its specific proxykey |
---|
194 | * @return |
---|
195 | */ |
---|
196 | public String getProxyKey() { |
---|
197 | return "workspaceprofile"; |
---|
198 | } |
---|
199 | |
---|
200 | @Override |
---|
201 | public String getActionContentType() { |
---|
202 | if (this.getFormat().toLowerCase().startsWith("json")) { |
---|
203 | this.setActionContentType("application/json"); |
---|
204 | //this.actionContentType = "application/xhtml+xml"; |
---|
205 | } else { |
---|
206 | this.setActionContentType("text/xml"); |
---|
207 | } |
---|
208 | |
---|
209 | return getActionContentType(); |
---|
210 | } |
---|
211 | |
---|
212 | //@Override |
---|
213 | public URL getBaseURL() { |
---|
214 | File file=new File(getPath()); |
---|
215 | URL url=null; |
---|
216 | try{ |
---|
217 | url=file.toURI().toURL(); |
---|
218 | log.debug("DEBUG: WorkspaceAction.getBaseURL(): " + url); |
---|
219 | } catch (MalformedURLException e){ |
---|
220 | log.error("getBaseURL: JSON exception has been caught;\n getPath=" + getPath() ); |
---|
221 | getDiagnostics().Add(Diagnostic.SYSTEM_ERROR, "WorkspaceAction.getBaseURL()", "JSON exception has been caught;\n getPath=" + getPath() ); |
---|
222 | } |
---|
223 | |
---|
224 | return url; |
---|
225 | } |
---|
226 | |
---|
227 | public String getBasePath() { |
---|
228 | if (base_path == null) { |
---|
229 | base_path = Admin.getConfig().getProperty(getProxyKey() + ".path"); |
---|
230 | } |
---|
231 | return base_path; |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | public String getWorkspaceProfile() { |
---|
236 | String profilename = null; |
---|
237 | |
---|
238 | if (type.toLowerCase().equals(USER)) { |
---|
239 | if (getServletRequest().getRemoteUser() != null) { |
---|
240 | profilename = PROFILENAME_BASE + getServletRequest().getRemoteUser() + ".xml"; |
---|
241 | } else { |
---|
242 | profilename = PROFILENAME_DEFAULT; |
---|
243 | } |
---|
244 | } else if (type.toLowerCase().equals(SERVER)) { |
---|
245 | profilename = PROFILENAME_SERVER; |
---|
246 | /* |
---|
247 | File f = new File(getBasePath() + profilename); |
---|
248 | if (! f.exists()){ |
---|
249 | //Admin.notifyUser(" not exists"); |
---|
250 | profilename = "WorkspaceProfile_default.xml"; |
---|
251 | } |
---|
252 | */ |
---|
253 | } else if (type.toLowerCase().equals(ADMIN)){ |
---|
254 | profilename = ADMINISTRATION_FILENAME; |
---|
255 | } else if (type.toLowerCase().equals(FILENAME)){ |
---|
256 | profilename = WORKSPACE_FILENAME; |
---|
257 | } |
---|
258 | |
---|
259 | if (profilename == null){ |
---|
260 | profilename = PROFILENAME_DEFAULT; |
---|
261 | Admin.notifyUser("WorkspaceProfilePath not specified (USER, SERVER)!"); |
---|
262 | } |
---|
263 | |
---|
264 | Admin.notifyUser("WorkspaceProfilename : " + profilename); |
---|
265 | return profilename; |
---|
266 | } |
---|
267 | |
---|
268 | @Override |
---|
269 | public String getFullFormat() { |
---|
270 | return "xml2json"; |
---|
271 | } |
---|
272 | |
---|
273 | public URL getURL() { |
---|
274 | File file=new File(getPath()); |
---|
275 | URL url=null; |
---|
276 | try{ |
---|
277 | url=file.toURI().toURL(); |
---|
278 | //Admin.notifyUser("url " + url); |
---|
279 | } catch (MalformedURLException e){ |
---|
280 | //Admin.notifyUser("" + e); |
---|
281 | } |
---|
282 | return url; |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | public String getPath() { |
---|
287 | String targetPath = getBasePath() + this.WORKSPACE_FILENAME;//getWorkspaceProfile(); |
---|
288 | log.debug("WorkspaceAction TARGETPATH: " + targetPath); |
---|
289 | return targetPath; |
---|
290 | } |
---|
291 | |
---|
292 | @Override |
---|
293 | public InputStream getSourceStream() throws IOException { |
---|
294 | InputStream stream = new BufferedInputStream( new FileInputStream(getPath())); |
---|
295 | |
---|
296 | |
---|
297 | return stream; |
---|
298 | } |
---|
299 | |
---|
300 | // set the server - session data = repositories, termsets |
---|
301 | public void setSessionData(InputStream source) throws IOException { |
---|
302 | if (source == null) return; |
---|
303 | if (this.getWorkspaceProfile() != PROFILENAME_SERVER) return; |
---|
304 | |
---|
305 | |
---|
306 | |
---|
307 | } |
---|
308 | |
---|
309 | /* |
---|
310 | @Override |
---|
311 | public void prepare() throws Exception { |
---|
312 | |
---|
313 | //Admin.notifyUser("execute:"); |
---|
314 | InputStream in = getSourceStream(); |
---|
315 | this.setSourceStream(in); |
---|
316 | if (getFormat().equals("xml")) { |
---|
317 | setResultStream(in); |
---|
318 | }else { //JSON |
---|
319 | // set srcFile (for MDTransformer to pass to xsl-scripts) |
---|
320 | MDTransformer transformer = new MDTransformer(); |
---|
321 | transformer.setSrcFile(getURL()); |
---|
322 | transformer.setParams(MDTransformer.createParamsMap(getFullFormat())); |
---|
323 | setResultStream(transformer.transformXML(in));//, getFullFormat())); |
---|
324 | //setSessionData(getResultStream()); |
---|
325 | } |
---|
326 | |
---|
327 | Admin.notifyUser(getProxyKey() + " success:" + (getResultStream()!=null)); |
---|
328 | } |
---|
329 | |
---|
330 | public void changeNodeName(Document doc, Node node, String new_name){ |
---|
331 | // Create an element with the new name |
---|
332 | Node node2 = (Node ) doc.createElement(new_name); |
---|
333 | // Copy the attributes to the new element NamedNodeMap attrs = element.getAttributes(); for (int i=0; i<attrs.getLength(); i++) { Attr attr2 = (Attr)doc.importNode(attrs.item(i), true); element2.getAttributes().setNamedItem(attr2); } |
---|
334 | // Move all the children |
---|
335 | while (node.hasChildNodes()) { node2.appendChild(node.getFirstChild()); } |
---|
336 | // Replace the old node with the new node |
---|
337 | Admin.notifyUser("replace:" + node2.getNodeName() + "," + node.getNodeName()); |
---|
338 | node.getParentNode().replaceChild(node2, node); |
---|
339 | } |
---|
340 | |
---|
341 | public void administrateQid() throws IOException, SAXException, ParserConfigurationException, TransformerException{ |
---|
342 | WorkspaceAction wa = new WorkspaceAction(); |
---|
343 | wa.setType("Admin"); |
---|
344 | |
---|
345 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
---|
346 | DocumentBuilder builder; |
---|
347 | |
---|
348 | builder = factory.newDocumentBuilder(); |
---|
349 | Document document = builder.parse(wa.getSourceStream()); |
---|
350 | // find user-filename-id or create one |
---|
351 | //add qid |
---|
352 | //NodeList list = document.getElementsByTagName("e"); |
---|
353 | //Admin.notifyUser("list:" + list.getLength()); |
---|
354 | |
---|
355 | // save the changes |
---|
356 | Transformer transformer; |
---|
357 | transformer = TransformerFactory.newInstance().newTransformer(); |
---|
358 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
359 | StreamResult result = new StreamResult(new StringWriter()); |
---|
360 | DOMSource source = new DOMSource(document); |
---|
361 | transformer.transform(source, result); |
---|
362 | String xmlString = result.getWriter().toString(); |
---|
363 | InputStream is_xml = new ByteArrayInputStream( xmlString.getBytes( ) ); |
---|
364 | Admin.writeToFile(wa.getPath(), is_xml); |
---|
365 | //wa.save(); |
---|
366 | } |
---|
367 | |
---|
368 | public String save() throws IOException, SAXException, ParserConfigurationException, TransformerException { |
---|
369 | |
---|
370 | String jsonData = getData(); |
---|
371 | |
---|
372 | Admin.notifyUser("JSON data:"+jsonData); |
---|
373 | if ((!this.getType().equals(ADMIN)) && this.getQid() != null) { |
---|
374 | administrateQid(); |
---|
375 | } |
---|
376 | |
---|
377 | XMLSerializer serializer = new XMLSerializer(); |
---|
378 | JSON json = JSONSerializer.toJSON( jsonData ); |
---|
379 | if (this.getType().equals(ADMIN)){ |
---|
380 | serializer.setRootName("Admin"); |
---|
381 | } else { |
---|
382 | serializer.setRootName("WorkspaceProfile"); |
---|
383 | } |
---|
384 | serializer.setElementName("item"); |
---|
385 | serializer.setTypeHintsEnabled(false); |
---|
386 | String xml = serializer.write( json ); |
---|
387 | Admin.notifyUser("XML:" + xml); |
---|
388 | |
---|
389 | InputStream is_xml = new ByteArrayInputStream( xml.getBytes( ) ); |
---|
390 | |
---|
391 | Admin.writeToFile(getPath(), is_xml); |
---|
392 | |
---|
393 | String ret = "1"; |
---|
394 | this.setResultStream( new ByteArrayInputStream(ret.getBytes())); |
---|
395 | |
---|
396 | |
---|
397 | return SUCCESS; |
---|
398 | } |
---|
399 | */ |
---|
400 | public void changeQueryId(Document doc, String from_id, String to_id) throws XPathExpressionException{ |
---|
401 | InputStream is = null; |
---|
402 | XPathFactory factory = XPathFactory.newInstance(); |
---|
403 | XPath xpath = factory.newXPath(); |
---|
404 | XPathExpression expr; |
---|
405 | String xpath_expr = ""; |
---|
406 | //xpath_expr = "//WorkspaceProfile/Querysets/item/Queries/item/id[.='" + from_id + "']"; |
---|
407 | xpath_expr = "//item/id[.='" + from_id + "']"; |
---|
408 | expr = xpath.compile(xpath_expr); |
---|
409 | Object result = null; |
---|
410 | result = expr.evaluate(doc, XPathConstants.NODESET); |
---|
411 | |
---|
412 | NodeList nodelist = (NodeList) result; |
---|
413 | nodelist.item(0).setTextContent(to_id); |
---|
414 | } |
---|
415 | public String createNewId(){//String name){ |
---|
416 | String name = "maxid"; |
---|
417 | String id = workspace_doc.getElementsByTagName(name).item(0).getTextContent(); |
---|
418 | Integer i = Integer.parseInt(id) + 1; |
---|
419 | id = i.toString(); |
---|
420 | workspace_doc.getElementsByTagName(name).item(0).setTextContent(id); |
---|
421 | return id.toString(); |
---|
422 | } |
---|
423 | /* |
---|
424 | public String createNewQsid(){ |
---|
425 | String id = workspace_doc.getElementsByTagName("maxqsid").item(0).getTextContent(); |
---|
426 | Integer i = Integer.parseInt(id) + 1; |
---|
427 | id = i.toString(); |
---|
428 | workspace_doc.getElementsByTagName("maxqsid").item(0).setTextContent(id); |
---|
429 | return id.toString(); |
---|
430 | } |
---|
431 | */ |
---|
432 | public InputStream createStream(String value) throws TransformerException, TransformerFactoryConfigurationError, ParserConfigurationException, UnsupportedEncodingException{ |
---|
433 | DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); |
---|
434 | DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); |
---|
435 | Document doc = docBuilder.newDocument(); |
---|
436 | |
---|
437 | //////////////////////// |
---|
438 | //Creating the XML tree |
---|
439 | Element root = doc.createElement("root"); |
---|
440 | root.setTextContent(value); |
---|
441 | doc.appendChild(root); |
---|
442 | //set up a transformer |
---|
443 | TransformerFactory transfac = TransformerFactory.newInstance(); |
---|
444 | Transformer trans = transfac.newTransformer(); |
---|
445 | //trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
---|
446 | //trans.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
447 | |
---|
448 | //create string from xml tree |
---|
449 | //StringWriter sw = new StringWriter(); |
---|
450 | //StreamResult result = new StreamResult(sw); |
---|
451 | //DOMSource source = new DOMSource(doc); |
---|
452 | //trans.transform(source, result); |
---|
453 | |
---|
454 | |
---|
455 | DOMSource source = new DOMSource(doc); |
---|
456 | StringWriter xmlAsWriter = new StringWriter(); |
---|
457 | StreamResult result = new StreamResult(xmlAsWriter); |
---|
458 | TransformerFactory.newInstance().newTransformer().transform(source, result); |
---|
459 | |
---|
460 | // write changes |
---|
461 | ByteArrayInputStream is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8")); |
---|
462 | |
---|
463 | |
---|
464 | return is; |
---|
465 | |
---|
466 | |
---|
467 | } |
---|
468 | public String getXMLRootName(){ |
---|
469 | String rootname = "WorkspaceProfile"; |
---|
470 | if (this.elementtype.equals(WorkspaceAction.SE_QUERY)){ |
---|
471 | rootname = "item"; |
---|
472 | } |
---|
473 | if (this.elementtype.equals(WorkspaceAction.SE_QUERYSET)){ |
---|
474 | rootname = "item"; |
---|
475 | } |
---|
476 | return rootname; |
---|
477 | } |
---|
478 | public String DocumentReplaceNewElement(Document new_doc) throws XPathExpressionException{ |
---|
479 | String newid = ""; |
---|
480 | if (this.elementtype.equals(WorkspaceAction.SE_WORKSPACE)){ |
---|
481 | NodeList wsnodelist = (NodeList) getWorkspace(); |
---|
482 | // remove |
---|
483 | //childeNode |
---|
484 | if (wsnodelist.getLength() > 0){ |
---|
485 | ((Node)wsnodelist.item(0)).getParentNode().removeChild(wsnodelist.item(0)); |
---|
486 | } |
---|
487 | //String qidstring = ""; |
---|
488 | //if (qid.equals("0")) { |
---|
489 | // newid = createNewQid(); |
---|
490 | // changeQueryId(new_doc,qid,newid); |
---|
491 | //} |
---|
492 | |
---|
493 | Node node = new_doc.getFirstChild(); |
---|
494 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
495 | ((Element)node2).setAttribute("user", getUserName()); |
---|
496 | workspace_doc.getElementsByTagName("WorkspaceProfiles").item(0).appendChild(node2); |
---|
497 | } |
---|
498 | if (this.elementtype.equals(WorkspaceAction.SE_QUERY)){ |
---|
499 | // new query |
---|
500 | Node node = new_doc.getFirstChild(); |
---|
501 | if (this.qid.equals("0")){ |
---|
502 | newid = createNewId();//"maxqid"); |
---|
503 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
504 | Element root = workspace_doc.getDocumentElement(); |
---|
505 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
506 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
507 | // apend to specific queryset |
---|
508 | NodeList wsnodelist = (NodeList) getQueryParent(); |
---|
509 | Node anode = wsnodelist.item(0); |
---|
510 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
511 | anode.setTextContent(""); |
---|
512 | } |
---|
513 | anode.appendChild(node2); |
---|
514 | } |
---|
515 | |
---|
516 | else { |
---|
517 | // edit existing query |
---|
518 | NodeList wsnodelist = (NodeList) getWorkspaceQuery(); |
---|
519 | // remove |
---|
520 | //childeNode |
---|
521 | if (wsnodelist.getLength() > 0){ |
---|
522 | //Element root = workspace_doc.getDocumentElement(); |
---|
523 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
524 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
525 | Node qnode = (Node)wsnodelist.item(0); |
---|
526 | //root.replaceChild(node2,(Node)wsnodelist.item(0)); |
---|
527 | |
---|
528 | Element parentElement = (Element)qnode.getParentNode(); |
---|
529 | parentElement.insertBefore(node2, qnode); |
---|
530 | qnode.getParentNode().removeChild(qnode); |
---|
531 | parentElement.normalize(); |
---|
532 | } |
---|
533 | } |
---|
534 | } |
---|
535 | if (this.elementtype.equals(WorkspaceAction.SE_BOOKMARK)){ |
---|
536 | // new bookmark |
---|
537 | Node node = new_doc.getFirstChild(); |
---|
538 | if (this.qid.equals("0")){ |
---|
539 | newid = createNewId();//"maxbid"); |
---|
540 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
541 | Element root = workspace_doc.getDocumentElement(); |
---|
542 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
543 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
544 | // apend to specific bookmarkset |
---|
545 | NodeList wsnodelist = (NodeList) getBookmarkParent(); |
---|
546 | Node anode = wsnodelist.item(0); |
---|
547 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
548 | anode.setTextContent(""); |
---|
549 | } |
---|
550 | anode.appendChild(node2); |
---|
551 | } |
---|
552 | |
---|
553 | else { |
---|
554 | // edit existing query |
---|
555 | NodeList wsnodelist = (NodeList) getWorkspaceQuery(); |
---|
556 | // remove |
---|
557 | //childeNode |
---|
558 | if (wsnodelist.getLength() > 0){ |
---|
559 | //Element root = workspace_doc.getDocumentElement(); |
---|
560 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
561 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
562 | Node qnode = (Node)wsnodelist.item(0); |
---|
563 | //root.replaceChild(node2,(Node)wsnodelist.item(0)); |
---|
564 | |
---|
565 | Element parentElement = (Element)qnode.getParentNode(); |
---|
566 | parentElement.insertBefore(node2, qnode); |
---|
567 | qnode.getParentNode().removeChild(qnode); |
---|
568 | parentElement.normalize(); |
---|
569 | } |
---|
570 | } |
---|
571 | } |
---|
572 | //new queryset |
---|
573 | if (this.elementtype.equals(WorkspaceAction.SE_QUERYSET)){ |
---|
574 | // new queryset |
---|
575 | Node node = new_doc.getFirstChild(); |
---|
576 | if (this.qsid.equals("0")){ |
---|
577 | newid = createNewId();//"maxqsid"); |
---|
578 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
579 | Element root = workspace_doc.getDocumentElement(); |
---|
580 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
581 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
582 | // apend to specific querysets |
---|
583 | NodeList wsnodelist = (NodeList) this.getWorkspace(); |
---|
584 | Element welem = (Element)wsnodelist.item(0); |
---|
585 | Node anode = welem.getElementsByTagName("Querysets").item(0); |
---|
586 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
587 | anode.setTextContent(""); |
---|
588 | } |
---|
589 | anode.appendChild(node2); |
---|
590 | } |
---|
591 | |
---|
592 | else { |
---|
593 | // edit existing queryset |
---|
594 | NodeList wsnodelist = (NodeList) getQueryset(); |
---|
595 | // remove |
---|
596 | //childeNode |
---|
597 | if (wsnodelist.getLength() > 0){ |
---|
598 | //Element root = workspace_doc.getDocumentElement(); |
---|
599 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
600 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
601 | Node qset = (Node)wsnodelist.item(0); |
---|
602 | |
---|
603 | Element parentElement = (Element)qset.getParentNode(); |
---|
604 | parentElement.insertBefore(node2, qset); |
---|
605 | qset.getParentNode().removeChild(qset); |
---|
606 | parentElement.normalize(); |
---|
607 | } |
---|
608 | } |
---|
609 | } |
---|
610 | |
---|
611 | //new bookmarkset |
---|
612 | if (this.elementtype.equals(WorkspaceAction.SE_BOOKMARKSET)){ |
---|
613 | // new queryset |
---|
614 | Node node = new_doc.getFirstChild(); |
---|
615 | if (this.qsid.equals("0")){ |
---|
616 | newid = createNewId();//"maxbsid"); |
---|
617 | new_doc.getElementsByTagName("id").item(0).setTextContent(newid); |
---|
618 | Element root = workspace_doc.getDocumentElement(); |
---|
619 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
620 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
621 | // apend to specific querysets |
---|
622 | NodeList wsnodelist = (NodeList) this.getWorkspace(); |
---|
623 | Element welem = (Element)wsnodelist.item(0); |
---|
624 | Node anode = welem.getElementsByTagName("Bookmarksets").item(0); |
---|
625 | if (anode.getTextContent().trim().toLowerCase().equals("null")){ |
---|
626 | anode.setTextContent(""); |
---|
627 | } |
---|
628 | anode.appendChild(node2); |
---|
629 | } |
---|
630 | |
---|
631 | else { |
---|
632 | // edit existing bookmarkset |
---|
633 | NodeList wsnodelist = (NodeList) getBookmarkset(); |
---|
634 | // remove |
---|
635 | //childeNode |
---|
636 | if (wsnodelist.getLength() > 0){ |
---|
637 | //Element root = workspace_doc.getDocumentElement(); |
---|
638 | Node node2 = workspace_doc.adoptNode(node.cloneNode(true)); |
---|
639 | node2 = workspace_doc.renameNode(node2, "", "item"); |
---|
640 | Node qset = (Node)wsnodelist.item(0); |
---|
641 | |
---|
642 | Element parentElement = (Element)qset.getParentNode(); |
---|
643 | parentElement.insertBefore(node2, qset); |
---|
644 | qset.getParentNode().removeChild(qset); |
---|
645 | parentElement.normalize(); |
---|
646 | } |
---|
647 | } |
---|
648 | } |
---|
649 | return newid; |
---|
650 | } |
---|
651 | |
---|
652 | //todo .has("Queries") |
---|
653 | public void parseDataInit(JSONObject json){ |
---|
654 | // workspaceelement |
---|
655 | if (json.has("Querysets")) { |
---|
656 | this.setQid("0"); |
---|
657 | this.setElementtype(WorkspaceAction.SE_WORKSPACE); |
---|
658 | } else { |
---|
659 | // Querysetelement |
---|
660 | if (json.has("Queries")) { |
---|
661 | this.setElementtype(WorkspaceAction.SE_QUERYSET); |
---|
662 | this.setQsid(json.getString("id")); |
---|
663 | } else { |
---|
664 | if (json.has("Bookmarks")) { |
---|
665 | this.setElementtype(WorkspaceAction.SE_BOOKMARKSET); |
---|
666 | this.setQsid(json.getString("id")); |
---|
667 | } else { |
---|
668 | // Query-Bookmark-element |
---|
669 | if (json.getString("bookmark").equals("1")) { |
---|
670 | this.setElementtype(WorkspaceAction.SE_BOOKMARK); |
---|
671 | } else { |
---|
672 | // Queryelement |
---|
673 | this.setElementtype(WorkspaceAction.SE_QUERY); |
---|
674 | } |
---|
675 | this.setQid(json.getString("id")); |
---|
676 | } |
---|
677 | } |
---|
678 | } |
---|
679 | |
---|
680 | } |
---|
681 | public String save() throws ParserConfigurationException, SAXException, IOException, DOMException, XPathExpressionException, TransformerException, TransformerFactoryConfigurationError{ |
---|
682 | |
---|
683 | // user data |
---|
684 | String jsonData = getData(); |
---|
685 | XMLSerializer serializer = new XMLSerializer(); |
---|
686 | JSON json = JSONSerializer.toJSON( jsonData ); |
---|
687 | // init qid, elementtype |
---|
688 | parseDataInit((JSONObject)json); |
---|
689 | serializer.setRootName(getXMLRootName()); //"WorkspaceProfile"); |
---|
690 | serializer.setElementName("item"); |
---|
691 | serializer.setTypeHintsEnabled(false); |
---|
692 | String xml = serializer.write( json ); |
---|
693 | //Admin.notifyUser("XML:" + xml); |
---|
694 | InputStream is_xml = new ByteArrayInputStream( xml.getBytes( ) ); |
---|
695 | // add to document |
---|
696 | //Admin.writeToFile(getPath(), is_xml); |
---|
697 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
---|
698 | DocumentBuilder builder = factory.newDocumentBuilder(); |
---|
699 | Document new_doc = builder.parse( is_xml ); |
---|
700 | // set user |
---|
701 | //((Element)d.getFirstChild()).setAttribute("user", getServletRequest().getRemoteUser()); |
---|
702 | String newid = DocumentReplaceNewElement(new_doc); |
---|
703 | |
---|
704 | |
---|
705 | //TODO |
---|
706 | saveDocument(); |
---|
707 | // return new qid |
---|
708 | //String src = "<body>" + qidstring + "</body>"; |
---|
709 | //javax.xml.transform.stream.StreamSource s = new StreamSource(src); |
---|
710 | //InputStream in = s.getInputStream(); |
---|
711 | this.setResultStream(createStream(newid));//(InputStream)(new ByteArrayInputStream(qidstring.getBytes()))); |
---|
712 | |
---|
713 | |
---|
714 | return SUCCESS; |
---|
715 | |
---|
716 | } |
---|
717 | public InputStream DocumentToStream(Node node) throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError{ |
---|
718 | |
---|
719 | InputStream is = null; |
---|
720 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
---|
721 | Source xmlSource; |
---|
722 | |
---|
723 | if (node == null) { |
---|
724 | xmlSource = new DOMSource(workspace_doc); |
---|
725 | } else { |
---|
726 | xmlSource = new DOMSource(node); |
---|
727 | } |
---|
728 | Result outputTarget = new StreamResult(outputStream); |
---|
729 | TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); |
---|
730 | is = new ByteArrayInputStream(outputStream.toByteArray()); |
---|
731 | return is; |
---|
732 | } |
---|
733 | |
---|
734 | public String DocumentToXMLString(){ |
---|
735 | Transformer transformer; |
---|
736 | String xmlString = ""; |
---|
737 | try { |
---|
738 | transformer = TransformerFactory.newInstance().newTransformer(); |
---|
739 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
740 | |
---|
741 | //initialize StreamResult with File object to save to file |
---|
742 | StreamResult result = new StreamResult(new StringWriter()); |
---|
743 | DOMSource source = new DOMSource(workspace_doc); |
---|
744 | try { |
---|
745 | transformer.transform(source, result); |
---|
746 | } catch (TransformerException e1) { |
---|
747 | // TODO Auto-generated catch block |
---|
748 | e1.printStackTrace(); |
---|
749 | } |
---|
750 | |
---|
751 | xmlString = result.getWriter().toString(); |
---|
752 | } catch (TransformerConfigurationException e) { |
---|
753 | // TODO Auto-generated catch block |
---|
754 | e.printStackTrace(); |
---|
755 | } catch (TransformerFactoryConfigurationError e) { |
---|
756 | // TODO Auto-generated catch block |
---|
757 | e.printStackTrace(); |
---|
758 | } |
---|
759 | |
---|
760 | return xmlString; |
---|
761 | } |
---|
762 | |
---|
763 | public void addWorkspaceToDocument(){ |
---|
764 | //{'WorkspaceProfile':{'CustomTermsets':'null', |
---|
765 | // 'Termsets':'null', |
---|
766 | // 'Repositories':'null', |
---|
767 | // 'Querysets':'null','created':'null','lastchanged':'null','profilename':'null'}}; |
---|
768 | Element we = workspace_doc.createElement("WorkspaceProfile"); |
---|
769 | we.setAttribute("user", getType().toLowerCase()); |
---|
770 | workspace_doc.getFirstChild().appendChild(we); |
---|
771 | |
---|
772 | Element e = workspace_doc.createElement("CustomTermsets"); |
---|
773 | e.setNodeValue("null"); |
---|
774 | we.appendChild(e); |
---|
775 | |
---|
776 | e = workspace_doc.createElement("Termsets"); |
---|
777 | e.setNodeValue("null"); |
---|
778 | we.appendChild(e); |
---|
779 | |
---|
780 | e = workspace_doc.createElement("Repositories"); |
---|
781 | e.setNodeValue("null"); |
---|
782 | we.appendChild(e); |
---|
783 | |
---|
784 | e = workspace_doc.createElement("Querysets"); |
---|
785 | e.setNodeValue("null"); |
---|
786 | we.appendChild(e); |
---|
787 | |
---|
788 | DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); |
---|
789 | String strdate = dateFormat.format(new Date()).toString(); |
---|
790 | e = workspace_doc.createElement("created"); |
---|
791 | e.setNodeValue(strdate); |
---|
792 | we.appendChild(e); |
---|
793 | |
---|
794 | e = workspace_doc.createElement("lastchanged"); |
---|
795 | e.setNodeValue(strdate); |
---|
796 | we.appendChild(e); |
---|
797 | |
---|
798 | e = workspace_doc.createElement("profilename"); |
---|
799 | e.setNodeValue(getType().toLowerCase()); |
---|
800 | we.appendChild(e); |
---|
801 | } |
---|
802 | |
---|
803 | public String getUserName(){ |
---|
804 | String userstring; |
---|
805 | if (getType().toLowerCase().equals(SERVER)){ |
---|
806 | userstring = SERVER; |
---|
807 | }else { |
---|
808 | userstring = getServletRequest().getRemoteUser(); |
---|
809 | if (userstring == null){ |
---|
810 | userstring = "default"; |
---|
811 | } |
---|
812 | } |
---|
813 | return userstring; |
---|
814 | } |
---|
815 | public String getWorkspaceXPathExpression(){ |
---|
816 | String xpath_expr = ""; |
---|
817 | |
---|
818 | xpath_expr = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='" + getUserName() + "']"; |
---|
819 | |
---|
820 | return xpath_expr; |
---|
821 | } |
---|
822 | |
---|
823 | public Object getWorkspace() throws XPathExpressionException{ |
---|
824 | InputStream is = null; |
---|
825 | XPathFactory factory = XPathFactory.newInstance(); |
---|
826 | XPath xpath = factory.newXPath(); |
---|
827 | XPathExpression expr; |
---|
828 | expr = xpath.compile(getWorkspaceXPathExpression()); |
---|
829 | //expression is evaluated with respect to a certain context node which is doc. |
---|
830 | Object result = null; |
---|
831 | try{ |
---|
832 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
833 | } catch(Exception e){ |
---|
834 | |
---|
835 | } |
---|
836 | |
---|
837 | return result; |
---|
838 | } |
---|
839 | |
---|
840 | public Object getWorkspaceQuery() throws XPathExpressionException{ |
---|
841 | |
---|
842 | XPathFactory factory = XPathFactory.newInstance(); |
---|
843 | XPath xpath = factory.newXPath(); |
---|
844 | XPathExpression expr; |
---|
845 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='" + this.qid + "']"); |
---|
846 | //expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='" +this.qid.toString()+"']"); |
---|
847 | //expr = xpath.compile("//item[@id='" + this.qid + "']"); |
---|
848 | //expression is evaluated with respect to a certain context node which is doc. |
---|
849 | Object result = null; |
---|
850 | try{ |
---|
851 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
852 | } catch(Exception e){ |
---|
853 | |
---|
854 | } |
---|
855 | |
---|
856 | return result; |
---|
857 | } |
---|
858 | |
---|
859 | public Object getQueryParent() throws XPathExpressionException{ |
---|
860 | |
---|
861 | XPathFactory factory = XPathFactory.newInstance(); |
---|
862 | XPath xpath = factory.newXPath(); |
---|
863 | XPathExpression expr; |
---|
864 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[id='" + this.qsid + "']/Queries"); |
---|
865 | //expression is evaluated with respect to a certain context node which is doc. |
---|
866 | Object result = null; |
---|
867 | try{ |
---|
868 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
869 | } catch(Exception e){ |
---|
870 | |
---|
871 | } |
---|
872 | |
---|
873 | return result; |
---|
874 | } |
---|
875 | |
---|
876 | public Object getQueryVcrid() throws XPathExpressionException{ |
---|
877 | |
---|
878 | XPathFactory factory = XPathFactory.newInstance(); |
---|
879 | XPath xpath = factory.newXPath(); |
---|
880 | XPathExpression expr; |
---|
881 | // expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[id='" + this.qsid + "']/vcrid"); |
---|
882 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='" + this.qid + "']/vcrid"); |
---|
883 | |
---|
884 | //expression is evaluated with respect to a certain context node which is doc. |
---|
885 | Object result = null; |
---|
886 | try{ |
---|
887 | result = expr.evaluate(workspace_doc, XPathConstants.STRING); |
---|
888 | } catch(Exception e){ |
---|
889 | |
---|
890 | } |
---|
891 | |
---|
892 | return result; |
---|
893 | } |
---|
894 | |
---|
895 | public Object getVcrid() throws XPathExpressionException{ |
---|
896 | |
---|
897 | XPathFactory factory = XPathFactory.newInstance(); |
---|
898 | XPath xpath = factory.newXPath(); |
---|
899 | XPathExpression expr1, expr2; |
---|
900 | //expr = xpath.compile("item[id='" + this.qid + "']/vcrid"); |
---|
901 | expr1 = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='" + this.qid + "']/vcrid"); |
---|
902 | expr2 = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Bookmarksets/item[id='" + this.qid + "']/vcrid"); |
---|
903 | |
---|
904 | //expression is evaluated with respect to a certain context node which is doc. |
---|
905 | Object result = null; |
---|
906 | try{ |
---|
907 | result = expr1.evaluate(workspace_doc, XPathConstants.STRING); |
---|
908 | } catch(Exception e){ |
---|
909 | |
---|
910 | } |
---|
911 | if (result.equals("")){ |
---|
912 | try{ |
---|
913 | result = expr2.evaluate(workspace_doc, XPathConstants.STRING); |
---|
914 | } catch(Exception e){ |
---|
915 | |
---|
916 | } |
---|
917 | } |
---|
918 | return result; |
---|
919 | } |
---|
920 | public Object getQueryset() throws XPathExpressionException{ |
---|
921 | |
---|
922 | XPathFactory factory = XPathFactory.newInstance(); |
---|
923 | XPath xpath = factory.newXPath(); |
---|
924 | XPathExpression expr; |
---|
925 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[id='" + this.qsid + "']"); |
---|
926 | //expression is evaluated with respect to a certain context node which is doc. |
---|
927 | Object result = null; |
---|
928 | try{ |
---|
929 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
930 | } catch(Exception e){ |
---|
931 | |
---|
932 | } |
---|
933 | |
---|
934 | return result; |
---|
935 | } |
---|
936 | |
---|
937 | public Object getBookmarkParent() throws XPathExpressionException{ |
---|
938 | |
---|
939 | XPathFactory factory = XPathFactory.newInstance(); |
---|
940 | XPath xpath = factory.newXPath(); |
---|
941 | XPathExpression expr; |
---|
942 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Bookmarksets/item[id='" + this.qsid + "']/Bookmarks"); |
---|
943 | //expression is evaluated with respect to a certain context node which is doc. |
---|
944 | Object result = null; |
---|
945 | try{ |
---|
946 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
947 | } catch(Exception e){ |
---|
948 | |
---|
949 | } |
---|
950 | |
---|
951 | return result; |
---|
952 | } |
---|
953 | |
---|
954 | public Object getBookmarkset() throws XPathExpressionException{ |
---|
955 | |
---|
956 | XPathFactory factory = XPathFactory.newInstance(); |
---|
957 | XPath xpath = factory.newXPath(); |
---|
958 | XPathExpression expr; |
---|
959 | expr = xpath.compile("//Profiles/WorkspaceProfiles/WorkspaceProfile/Bookmarksets/item[id='" + this.qsid + "']"); |
---|
960 | //expression is evaluated with respect to a certain context node which is doc. |
---|
961 | Object result = null; |
---|
962 | try{ |
---|
963 | result = expr.evaluate(workspace_doc, XPathConstants.NODESET); |
---|
964 | } catch(Exception e){ |
---|
965 | |
---|
966 | } |
---|
967 | |
---|
968 | return result; |
---|
969 | } |
---|
970 | |
---|
971 | |
---|
972 | @Override |
---|
973 | public void prepare() throws Exception { |
---|
974 | |
---|
975 | loadParams(); |
---|
976 | |
---|
977 | NodeList list = (NodeList) getWorkspace();//result; |
---|
978 | if (list.getLength() < 1) { |
---|
979 | addWorkspaceToDocument(); |
---|
980 | list = (NodeList) getWorkspace(); |
---|
981 | } |
---|
982 | if (list.getLength() > 1){ |
---|
983 | // error |
---|
984 | } |
---|
985 | // nodelistto stream |
---|
986 | InputStream is = this.DocumentToStream(list.item(0)); |
---|
987 | this.setSourceStream(is); |
---|
988 | |
---|
989 | if (getFormat().equals("xml")) { |
---|
990 | setResultStream(is); |
---|
991 | }else { //JSON |
---|
992 | // set srcFile (for MDTransformer to pass to xsl-scripts) |
---|
993 | MDTransformer transformer = new MDTransformer(); |
---|
994 | transformer.setSrcFile(getURL()); |
---|
995 | transformer.setParams(getParams()); |
---|
996 | //transformer.setParams(MDTransformer.createParamsMap(getFullFormat())); |
---|
997 | setResultStream(transformer.transformXML(is));//, getFullFormat())); |
---|
998 | //setSessionData(getResultStream()); |
---|
999 | } |
---|
1000 | |
---|
1001 | log.debug(getProxyKey() + " success:" + (getResultStream()!=null)); |
---|
1002 | } |
---|
1003 | |
---|
1004 | |
---|
1005 | |
---|
1006 | public void initDocument() { |
---|
1007 | |
---|
1008 | boolean init=false; |
---|
1009 | Integer counter = start_query; |
---|
1010 | //String fname = getPath();//workspacefile; |
---|
1011 | File f = new File (workspacefilename); |
---|
1012 | |
---|
1013 | if (!f.exists()) { |
---|
1014 | // create new counter document |
---|
1015 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
1016 | DocumentBuilder docBuilder; |
---|
1017 | try { |
---|
1018 | docBuilder = docFactory.newDocumentBuilder(); |
---|
1019 | workspace_doc = docBuilder.newDocument(); |
---|
1020 | // append root tag <index > |
---|
1021 | Element root = (Element) workspace_doc.createElement("Profiles"); |
---|
1022 | //root.setAttribute("idcounter", "1"); |
---|
1023 | workspace_doc.appendChild(root); |
---|
1024 | Element e = workspace_doc.createElement("Querycounter"); |
---|
1025 | root.appendChild(e); |
---|
1026 | e = workspace_doc.createElement("maxqid"); |
---|
1027 | e.setNodeValue(counter.toString()); |
---|
1028 | e = workspace_doc.createElement("WorkspaceProfiles"); |
---|
1029 | root.appendChild(e); |
---|
1030 | |
---|
1031 | //updateQuerycounter(); |
---|
1032 | this.saveDocument(); |
---|
1033 | |
---|
1034 | //Admin.notifyUser("new document"); |
---|
1035 | } catch (ParserConfigurationException e) { |
---|
1036 | // TODO Auto-generated catch block |
---|
1037 | e.printStackTrace(); |
---|
1038 | } |
---|
1039 | } |
---|
1040 | else { |
---|
1041 | try { |
---|
1042 | |
---|
1043 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
1044 | DocumentBuilder docBuilder; |
---|
1045 | try { |
---|
1046 | docBuilder = docFactory.newDocumentBuilder(); |
---|
1047 | try { |
---|
1048 | workspace_doc = docBuilder.parse(workspacefilename); |
---|
1049 | } catch (SAXException e) { |
---|
1050 | // TODO Auto-generated catch block |
---|
1051 | |
---|
1052 | e.printStackTrace(); |
---|
1053 | } |
---|
1054 | } catch (ParserConfigurationException e) { |
---|
1055 | // TODO Auto-generated catch block |
---|
1056 | e.printStackTrace(); |
---|
1057 | } |
---|
1058 | |
---|
1059 | //read counter |
---|
1060 | //counter = new Integer(workspace_doc.getFirstChild().getFirstChild().getFirstChild().getNodeValue()); |
---|
1061 | init = true; |
---|
1062 | } catch (IOException ex){ |
---|
1063 | //Admin.notifyUser("initCacheCounter:" + ex.toString()); |
---|
1064 | ex.printStackTrace(); |
---|
1065 | } |
---|
1066 | } |
---|
1067 | |
---|
1068 | //return counter; |
---|
1069 | } |
---|
1070 | /* |
---|
1071 | public void updateQuerycounter () { |
---|
1072 | workspace_doc.getFirstChild().getAttributes().getNamedItem("idcounter").setNodeValue(querycounter.toString()); |
---|
1073 | saveDocument(); |
---|
1074 | } |
---|
1075 | */ |
---|
1076 | public void saveDocument() { |
---|
1077 | //public void writeQuerycounter (Integer i) { |
---|
1078 | |
---|
1079 | // first update <index idcounter> |
---|
1080 | //workspace_doc.getFirstChild().getAttributes().getNamedItem("idcounter").setNodeValue(i.toString()); |
---|
1081 | |
---|
1082 | //Admin.notifyUser("writeCacheCounter:" + i.toString()); |
---|
1083 | // write xml |
---|
1084 | Transformer transformer; |
---|
1085 | try { |
---|
1086 | transformer = TransformerFactory.newInstance().newTransformer(); |
---|
1087 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
1088 | |
---|
1089 | //initialize StreamResult with File object to save to file |
---|
1090 | StreamResult result = new StreamResult(new StringWriter()); |
---|
1091 | DOMSource source = new DOMSource(workspace_doc); |
---|
1092 | try { |
---|
1093 | transformer.transform(source, result); |
---|
1094 | } catch (TransformerException e1) { |
---|
1095 | // TODO Auto-generated catch block |
---|
1096 | e1.printStackTrace(); |
---|
1097 | } |
---|
1098 | |
---|
1099 | String xmlString = result.getWriter().toString(); |
---|
1100 | |
---|
1101 | File f = new File (workspacefilename); |
---|
1102 | FileWriter fw; |
---|
1103 | try { |
---|
1104 | fw = new FileWriter(f); |
---|
1105 | try { |
---|
1106 | fw.write(xmlString); |
---|
1107 | fw.close(); |
---|
1108 | } catch (IOException e) { |
---|
1109 | // TODO Auto-generated catch block |
---|
1110 | e.printStackTrace(); |
---|
1111 | } |
---|
1112 | } catch (IOException e) { |
---|
1113 | // TODO Auto-generated catch block |
---|
1114 | e.printStackTrace(); |
---|
1115 | } |
---|
1116 | } catch (TransformerConfigurationException e2) { |
---|
1117 | // TODO Auto-generated catch block |
---|
1118 | e2.printStackTrace(); |
---|
1119 | } catch (TransformerFactoryConfigurationError e2) { |
---|
1120 | // TODO Auto-generated catch block |
---|
1121 | e2.printStackTrace(); |
---|
1122 | } |
---|
1123 | } |
---|
1124 | |
---|
1125 | |
---|
1126 | } |
---|