1 | package org.greenstone.gsdl3.service;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileWriter;
|
---|
5 | import java.io.Serializable;
|
---|
6 | import java.util.HashMap;
|
---|
7 |
|
---|
8 | import javax.xml.transform.OutputKeys;
|
---|
9 | import javax.xml.transform.Transformer;
|
---|
10 | import javax.xml.transform.TransformerFactory;
|
---|
11 | import javax.xml.transform.dom.DOMSource;
|
---|
12 | import javax.xml.transform.stream.StreamResult;
|
---|
13 |
|
---|
14 | import org.apache.log4j.Logger;
|
---|
15 | import org.greenstone.gsdl3.util.GSXML;
|
---|
16 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
17 | import org.w3c.dom.Document;
|
---|
18 | import org.w3c.dom.Element;
|
---|
19 | import org.w3c.dom.NodeList;
|
---|
20 |
|
---|
21 | public class DebugService extends ServiceRack
|
---|
22 | {
|
---|
23 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.DebugService.class.getName());
|
---|
24 |
|
---|
25 | /**********************************************************
|
---|
26 | * The list of services the utility service rack supports *
|
---|
27 | *********************************************************/
|
---|
28 | protected static final String RETRIEVE_TEMPLATE_FROM_XML_FILE = "RetrieveXMLTemplateFromFile";
|
---|
29 | protected static final String SAVE_TEMPLATE_TO_XML_FILE = "SaveXMLTemplateToFile";
|
---|
30 | /*********************************************************/
|
---|
31 |
|
---|
32 | String[] services = { RETRIEVE_TEMPLATE_FROM_XML_FILE, SAVE_TEMPLATE_TO_XML_FILE };
|
---|
33 |
|
---|
34 | public boolean configure(Element info, Element extra_info)
|
---|
35 | {
|
---|
36 | if (!super.configure(info, extra_info))
|
---|
37 | {
|
---|
38 | return false;
|
---|
39 | }
|
---|
40 |
|
---|
41 | logger.info("Configuring DebugServices...");
|
---|
42 | this.config_info = info;
|
---|
43 |
|
---|
44 | for (int i = 0; i < services.length; i++)
|
---|
45 | {
|
---|
46 | Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
47 | service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
48 | service.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
49 | this.short_service_info.appendChild(service);
|
---|
50 | }
|
---|
51 |
|
---|
52 | return true;
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected Element getServiceDescription(String service_id, String lang, String subset)
|
---|
56 | {
|
---|
57 | for (int i = 0; i < services.length; i++)
|
---|
58 | {
|
---|
59 | if (service_id.equals(services[i]))
|
---|
60 | {
|
---|
61 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
62 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
63 | service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
64 | return service_elem;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | return null;
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected Element processRetrieveXMLTemplateFromFile(Element request)
|
---|
72 | {
|
---|
73 | Element result = GSXML.createBasicResponse(this.doc, RETRIEVE_TEMPLATE_FROM_XML_FILE);
|
---|
74 |
|
---|
75 | if (request == null)
|
---|
76 | {
|
---|
77 | GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
78 | return result;
|
---|
79 | }
|
---|
80 |
|
---|
81 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
82 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
83 |
|
---|
84 | // Get the parameters of the request
|
---|
85 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
86 |
|
---|
87 | if (param_list == null)
|
---|
88 | {
|
---|
89 | GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
90 | return result;
|
---|
91 | }
|
---|
92 |
|
---|
93 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
94 |
|
---|
95 | String filepath = (String) params.get("filePath");
|
---|
96 | String namespace = (String) params.get("namespace");
|
---|
97 | String nodeName = (String) params.get("nodename");
|
---|
98 | String nameToGet = (String) params.get("name");
|
---|
99 | String matchToGet = (String) params.get("match");
|
---|
100 |
|
---|
101 | String fullNamespace;
|
---|
102 | if (namespace.toLowerCase().equals("gsf"))
|
---|
103 | {
|
---|
104 | fullNamespace = GSXML.GSF_NAMESPACE;
|
---|
105 | }
|
---|
106 | else if (namespace.toLowerCase().equals("xsl"))
|
---|
107 | {
|
---|
108 | fullNamespace = GSXML.XSL_NAMESPACE;
|
---|
109 | }
|
---|
110 | else
|
---|
111 | {
|
---|
112 | return result;
|
---|
113 | }
|
---|
114 |
|
---|
115 | File xslFile = new File(filepath);
|
---|
116 | if (xslFile.exists())
|
---|
117 | {
|
---|
118 | XMLConverter converter = new XMLConverter();
|
---|
119 | Document xslDoc = converter.getDOM(xslFile, "UTF-8");
|
---|
120 |
|
---|
121 | NodeList templateElems = xslDoc.getElementsByTagNameNS(fullNamespace, nodeName);
|
---|
122 |
|
---|
123 | if (nameToGet != null && nameToGet.length() != 0)
|
---|
124 | {
|
---|
125 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
126 | {
|
---|
127 | Element template = (Element) templateElems.item(i);
|
---|
128 | if (template.getAttribute("name").equals(nameToGet))
|
---|
129 | {
|
---|
130 | Element requestedTemplate = this.doc.createElement("requestedTemplate");
|
---|
131 | requestedTemplate.appendChild(this.doc.importNode(template, true));
|
---|
132 | result.appendChild(requestedTemplate);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | //Maybe should look for highest priority
|
---|
137 | if (matchToGet != null && matchToGet.length() != 0)
|
---|
138 | {
|
---|
139 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
140 | {
|
---|
141 | Element template = (Element) templateElems.item(i);
|
---|
142 | if (template.getAttribute("match").equals(matchToGet))
|
---|
143 | {
|
---|
144 | Element requestedTemplate = this.doc.createElement("requestedTemplate");
|
---|
145 | requestedTemplate.appendChild(this.doc.importNode(template, true));
|
---|
146 | result.appendChild(requestedTemplate);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | return result;
|
---|
153 | }
|
---|
154 |
|
---|
155 | protected Element processSaveXMLTemplateToFile(Element request)
|
---|
156 | {
|
---|
157 | Element result = GSXML.createBasicResponse(this.doc, SAVE_TEMPLATE_TO_XML_FILE);
|
---|
158 |
|
---|
159 | if (request == null)
|
---|
160 | {
|
---|
161 | GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
162 | return result;
|
---|
163 | }
|
---|
164 |
|
---|
165 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
166 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
167 |
|
---|
168 | // Get the parameters of the request
|
---|
169 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
170 |
|
---|
171 | if (param_list == null)
|
---|
172 | {
|
---|
173 | GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
174 | return result;
|
---|
175 | }
|
---|
176 |
|
---|
177 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
178 |
|
---|
179 | String filepath = (String) params.get("filePath");
|
---|
180 | String namespace = (String) params.get("namespace");
|
---|
181 | String nodeName = (String) params.get("nodename");
|
---|
182 | String nameToSave = (String) params.get("name");
|
---|
183 | String matchToSave = (String) params.get("match");
|
---|
184 | String xml = (String) params.get("xml");
|
---|
185 |
|
---|
186 | String fullNamespace;
|
---|
187 | if (namespace.toLowerCase().equals("gsf"))
|
---|
188 | {
|
---|
189 | fullNamespace = GSXML.GSF_NAMESPACE;
|
---|
190 | }
|
---|
191 | else if (namespace.toLowerCase().equals("xsl"))
|
---|
192 | {
|
---|
193 | fullNamespace = GSXML.XSL_NAMESPACE;
|
---|
194 | }
|
---|
195 | else
|
---|
196 | {
|
---|
197 | GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": The specified namespace was not valid", GSXML.ERROR_TYPE_SYNTAX);
|
---|
198 | return result;
|
---|
199 | }
|
---|
200 |
|
---|
201 | File xslFile = new File(filepath);
|
---|
202 | if (xslFile.exists())
|
---|
203 | {
|
---|
204 | XMLConverter converter = new XMLConverter();
|
---|
205 | Document xslDoc = converter.getDOM(xslFile, "UTF-8");
|
---|
206 |
|
---|
207 | NodeList templateElems = xslDoc.getElementsByTagNameNS(fullNamespace, nodeName);
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * NodeList textElems =
|
---|
211 | * xslDoc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "text"); for
|
---|
212 | * (int i = 0; i < textElems.getLength(); i++) {
|
---|
213 | * System.err.println("\n\n" + i + "\n\n" +
|
---|
214 | * GSXML.xmlNodeToString(textElems.item(i))); }
|
---|
215 | */
|
---|
216 |
|
---|
217 | boolean found = false;
|
---|
218 | if (nameToSave != null && nameToSave.length() != 0)
|
---|
219 | {
|
---|
220 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
221 | {
|
---|
222 | Element template = (Element) templateElems.item(i);
|
---|
223 | if (template.getAttribute("name").equals(nameToSave))
|
---|
224 | {
|
---|
225 | try
|
---|
226 | {
|
---|
227 | System.err.println(xml);
|
---|
228 | Element newTemplate = (Element) converter.getDOM("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"" + GSXML.XSL_NAMESPACE + "\" xmlns:java=\"" + GSXML.JAVA_NAMESPACE + "\" xmlns:util=\"" + GSXML.UTIL_NAMESPACE + "\" xmlns:gsf=\"" + GSXML.GSF_NAMESPACE + "\">" + xml + "</xsl:stylesheet>", "UTF-8").getDocumentElement().getElementsByTagNameNS(fullNamespace, nodeName).item(0);
|
---|
229 | template.getParentNode().replaceChild(xslDoc.importNode(newTemplate, true), template);
|
---|
230 | found = true;
|
---|
231 | }
|
---|
232 | catch (Exception ex)
|
---|
233 | {
|
---|
234 | ex.printStackTrace();
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | //Maybe should look for highest priority match
|
---|
240 | if (matchToSave != null && matchToSave.length() != 0)
|
---|
241 | {
|
---|
242 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
243 | {
|
---|
244 | Element template = (Element) templateElems.item(i);
|
---|
245 | if (template.getAttribute("match").equals(matchToSave))
|
---|
246 | {
|
---|
247 |
|
---|
248 | Element newTemplate = (Element) converter.getDOM("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"" + GSXML.XSL_NAMESPACE + "\" xmlns:java=\"" + GSXML.JAVA_NAMESPACE + "\" xmlns:util=\"" + GSXML.UTIL_NAMESPACE + "\" xmlns:gsf=\"" + GSXML.GSF_NAMESPACE + "\">" + xml + "</xsl:stylesheet>", "UTF-8").getDocumentElement().getFirstChild();
|
---|
249 | template.getParentNode().replaceChild(xslDoc.importNode(newTemplate, true), template);
|
---|
250 | found = true;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (!found)
|
---|
256 | {
|
---|
257 | GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": Could not save as the specified template could not be found", GSXML.ERROR_TYPE_SYNTAX);
|
---|
258 | }
|
---|
259 | else
|
---|
260 | {
|
---|
261 |
|
---|
262 | try
|
---|
263 | {
|
---|
264 | Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
---|
265 |
|
---|
266 | //initialize StreamResult with File object to save to file
|
---|
267 | StreamResult sresult = new StreamResult(new FileWriter(xslFile));
|
---|
268 | DOMSource source = new DOMSource(xslDoc);
|
---|
269 | transformer.transform(source, sresult);
|
---|
270 | }
|
---|
271 | catch (Exception ex)
|
---|
272 | {
|
---|
273 | GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": There was an error writing out the XML file", GSXML.ERROR_TYPE_SYNTAX);
|
---|
274 | }
|
---|
275 |
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | return result;
|
---|
280 | }
|
---|
281 | } |
---|