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