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