1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.BufferedInputStream; |
---|
4 | import java.io.ByteArrayInputStream; |
---|
5 | import java.io.File; |
---|
6 | import java.io.FileInputStream; |
---|
7 | import java.io.IOException; |
---|
8 | import java.io.InputStream; |
---|
9 | import java.net.MalformedURLException; |
---|
10 | import java.net.URL; |
---|
11 | |
---|
12 | import java.io.StringReader; |
---|
13 | import java.io.StringWriter; |
---|
14 | import javax.xml.parsers.DocumentBuilder; |
---|
15 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
16 | import javax.xml.parsers.ParserConfigurationException; |
---|
17 | import javax.xml.transform.OutputKeys; |
---|
18 | import javax.xml.transform.Transformer; |
---|
19 | import javax.xml.transform.TransformerConfigurationException; |
---|
20 | import javax.xml.transform.TransformerException; |
---|
21 | import javax.xml.transform.TransformerFactory; |
---|
22 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
23 | import javax.xml.transform.dom.DOMSource; |
---|
24 | import javax.xml.transform.stream.StreamResult; |
---|
25 | |
---|
26 | |
---|
27 | import net.sf.json.JSON; |
---|
28 | import net.sf.json.JSONSerializer; |
---|
29 | import net.sf.json.xml.XMLSerializer; |
---|
30 | |
---|
31 | import org.w3c.dom.Document; |
---|
32 | import org.w3c.dom.Node; |
---|
33 | import org.w3c.dom.NodeList; |
---|
34 | import org.xml.sax.InputSource; |
---|
35 | import org.xml.sax.SAXException; |
---|
36 | |
---|
37 | import com.opensymphony.xwork2.ActionSupport; |
---|
38 | |
---|
39 | |
---|
40 | /** |
---|
41 | * |
---|
42 | * @author |
---|
43 | * |
---|
44 | */ |
---|
45 | public class WorkspaceAction extends ActionSupport |
---|
46 | { |
---|
47 | |
---|
48 | private static final long serialVersionUID = 1L; |
---|
49 | |
---|
50 | /** |
---|
51 | * Properties to be filled by Struts with according request-parameters |
---|
52 | */ |
---|
53 | private String actionkey; |
---|
54 | private String data; |
---|
55 | private String id; |
---|
56 | private String format; |
---|
57 | private String userMsg; |
---|
58 | |
---|
59 | private String base_path; |
---|
60 | |
---|
61 | public String getData() { |
---|
62 | return data; |
---|
63 | } |
---|
64 | |
---|
65 | public void setData(String data) { |
---|
66 | this.data = data; |
---|
67 | } |
---|
68 | |
---|
69 | public String getID() { |
---|
70 | return id; |
---|
71 | } |
---|
72 | |
---|
73 | public void setID(String _id) { |
---|
74 | this.id = _id; |
---|
75 | } |
---|
76 | |
---|
77 | public String getFormat() { |
---|
78 | return format; |
---|
79 | } |
---|
80 | |
---|
81 | public void setFormat(String format) { |
---|
82 | this.format = format; |
---|
83 | } |
---|
84 | |
---|
85 | public String getActionkey() { |
---|
86 | return actionkey; |
---|
87 | } |
---|
88 | |
---|
89 | public void setActionkey(String actionKey) { |
---|
90 | actionkey = actionKey; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * primitive identification of the target-proxy |
---|
95 | * base for finding the right base_url in the props |
---|
96 | * subclass has to override with its specific proxykey |
---|
97 | * @return |
---|
98 | */ |
---|
99 | public String getProxyKey() { |
---|
100 | return "workspaceprofile"; |
---|
101 | } |
---|
102 | |
---|
103 | public String getUserMsg() { |
---|
104 | return userMsg; |
---|
105 | } |
---|
106 | |
---|
107 | public void setUserMsg(String userMsg) { |
---|
108 | this.userMsg = this.userMsg + "\n" + userMsg; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | private InputStream resultStream; |
---|
113 | private InputStream sourceStream; |
---|
114 | |
---|
115 | /** |
---|
116 | * The stream holding the resulting data to be sent back to the user as response |
---|
117 | * @return |
---|
118 | */ |
---|
119 | public InputStream getResultStream() { |
---|
120 | return resultStream; |
---|
121 | } |
---|
122 | public void setResultStream(InputStream _resultStream){ |
---|
123 | resultStream = _resultStream; |
---|
124 | } |
---|
125 | public void setSourceStream(InputStream _sourceStream){ |
---|
126 | sourceStream = _sourceStream; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | public URL getBaseURL() { |
---|
131 | File file=new File(getPath()); |
---|
132 | URL url=null; |
---|
133 | try{ |
---|
134 | url=file.toURL(); |
---|
135 | Admin.notifyUser(" JSON TEST: url " + url); |
---|
136 | } catch (MalformedURLException e){ |
---|
137 | Admin.notifyUser("JSON exception has been caught" + e); |
---|
138 | } |
---|
139 | |
---|
140 | return url; |
---|
141 | } |
---|
142 | |
---|
143 | public String getBasePath() { |
---|
144 | if (base_path == null) { |
---|
145 | base_path = Admin.getConfig().getProperty(getProxyKey() + ".path"); |
---|
146 | } |
---|
147 | return base_path; |
---|
148 | } |
---|
149 | //TODO identification of WOrkspaceProfile (user, name, id) |
---|
150 | public String getWorkspaceProfile() { |
---|
151 | return "WorkspaceProfile.xml"; |
---|
152 | } |
---|
153 | |
---|
154 | public String getFullFormat() { |
---|
155 | return "xml2json"; |
---|
156 | } |
---|
157 | |
---|
158 | public URL getURL() { |
---|
159 | File file=new File(getPath()); |
---|
160 | URL url=null; |
---|
161 | try{ |
---|
162 | url=file.toURL(); |
---|
163 | //Admin.notifyUser("url " + url); |
---|
164 | } catch (MalformedURLException e){ |
---|
165 | //Admin.notifyUser("" + e); |
---|
166 | } |
---|
167 | return url; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | public String getPath() { |
---|
172 | String targetPath = getBasePath() + getWorkspaceProfile(); |
---|
173 | Admin.notifyUser(targetPath); |
---|
174 | return targetPath; |
---|
175 | } |
---|
176 | |
---|
177 | public InputStream getSourceStream() throws IOException { |
---|
178 | InputStream stream = new BufferedInputStream( new FileInputStream(getPath())); |
---|
179 | |
---|
180 | |
---|
181 | return stream; |
---|
182 | } |
---|
183 | |
---|
184 | public void prepare() throws Exception { |
---|
185 | |
---|
186 | //Admin.notifyUser("execute:"); |
---|
187 | sourceStream = getSourceStream(); |
---|
188 | if (format.equals("xml")) { |
---|
189 | resultStream = sourceStream; |
---|
190 | }else { //JSON |
---|
191 | // set srcFile (for MDTransformer to pass to xsl-scripts) |
---|
192 | MDTransformer.getMDTransformer().setSrcFile(getURL()); |
---|
193 | resultStream = MDTransformer.getMDTransformer().transformXML(sourceStream, getFullFormat()); |
---|
194 | } |
---|
195 | |
---|
196 | Admin.notifyUser(getProxyKey() + " success:" + (resultStream!=null)); |
---|
197 | } |
---|
198 | |
---|
199 | public void changeNodeName(Document doc, Node node, String new_name){ |
---|
200 | // Create an element with the new name |
---|
201 | Node node2 = (Node ) doc.createElement(new_name); |
---|
202 | // 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); } |
---|
203 | // Move all the children |
---|
204 | while (node.hasChildNodes()) { node2.appendChild(node.getFirstChild()); } |
---|
205 | // Replace the old node with the new node |
---|
206 | Admin.notifyUser("replace:" + node2.getNodeName() + "," + node.getNodeName()); |
---|
207 | node.getParentNode().replaceChild(node2, node); |
---|
208 | } |
---|
209 | |
---|
210 | /* |
---|
211 | public String save(){ |
---|
212 | |
---|
213 | String jsonData = getData(); |
---|
214 | Admin.notifyUser("JSON data:"+jsonData); |
---|
215 | |
---|
216 | resultStream = null; |
---|
217 | |
---|
218 | XMLSerializer serializer = new XMLSerializer(); |
---|
219 | JSON json = JSONSerializer.toJSON( jsonData ); |
---|
220 | //Admin.notifyUser("test5"); |
---|
221 | serializer.setRootName("WorkspaceProfile"); |
---|
222 | serializer.setTypeHintsEnabled(false); |
---|
223 | |
---|
224 | String xml = serializer.write( json ); |
---|
225 | Admin.notifyUser("XML:" + xml); |
---|
226 | |
---|
227 | |
---|
228 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
---|
229 | DocumentBuilder builder; |
---|
230 | try { |
---|
231 | builder = factory.newDocumentBuilder(); |
---|
232 | try { |
---|
233 | Document document = builder.parse(new InputSource(new StringReader(xml))); |
---|
234 | NodeList list = document.getElementsByTagName("e"); |
---|
235 | Admin.notifyUser("list:" + list.getLength()); |
---|
236 | while (list.getLength() > 0){ |
---|
237 | Admin.notifyUser(0 + "::" + list.item(0).getNodeName() + "::" + list.item(0).getParentNode().getNodeName()); |
---|
238 | if (list.item(0).getParentNode().getNodeName().equals("Queries")){ |
---|
239 | changeNodeName(document, list.item(0),"Query"); |
---|
240 | }else{ |
---|
241 | if (list.item(0).getParentNode().getNodeName().equals("Querysets")){ |
---|
242 | changeNodeName(document, list.item(0),"Queryset"); |
---|
243 | } else{ |
---|
244 | if (list.item(0).getParentNode().getNodeName().equals("collections")){ |
---|
245 | changeNodeName(document, list.item(0),"item"); |
---|
246 | } else { |
---|
247 | if (list.item(0).getParentNode().getNodeName().equals("columns")){ |
---|
248 | changeNodeName(document, list.item(0),"col"); |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | } |
---|
253 | list = document.getElementsByTagName("e"); |
---|
254 | } |
---|
255 | |
---|
256 | Transformer transformer; |
---|
257 | try { |
---|
258 | transformer = TransformerFactory.newInstance().newTransformer(); |
---|
259 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
260 | StreamResult result = new StreamResult(new StringWriter()); |
---|
261 | DOMSource source = new DOMSource(document); |
---|
262 | try { |
---|
263 | transformer.transform(source, result); |
---|
264 | String xmlString = result.getWriter().toString(); |
---|
265 | |
---|
266 | Admin.notifyUser("XML:" + xmlString); |
---|
267 | |
---|
268 | InputStream is_xml = new ByteArrayInputStream( xmlString.getBytes( ) ); |
---|
269 | Admin.writeToFile(getPath(), is_xml); |
---|
270 | |
---|
271 | } catch (TransformerException e1) { |
---|
272 | // TODO Auto-generated catch block |
---|
273 | e1.printStackTrace(); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | } catch (TransformerConfigurationException e) { |
---|
279 | // TODO Auto-generated catch block |
---|
280 | e.printStackTrace(); |
---|
281 | } catch (TransformerFactoryConfigurationError e) { |
---|
282 | // TODO Auto-generated catch block |
---|
283 | e.printStackTrace(); |
---|
284 | } |
---|
285 | |
---|
286 | } catch (SAXException e) { |
---|
287 | // TODO Auto-generated catch block |
---|
288 | e.printStackTrace(); |
---|
289 | } catch (IOException e) { |
---|
290 | // TODO Auto-generated catch block |
---|
291 | e.printStackTrace(); |
---|
292 | } |
---|
293 | } catch (ParserConfigurationException e) { |
---|
294 | // TODO Auto-generated catch block |
---|
295 | e.printStackTrace(); |
---|
296 | } |
---|
297 | |
---|
298 | String ret = "1"; |
---|
299 | resultStream = new ByteArrayInputStream(ret.getBytes()); |
---|
300 | |
---|
301 | |
---|
302 | return SUCCESS; |
---|
303 | } |
---|
304 | |
---|
305 | */ |
---|
306 | public String save(){ |
---|
307 | |
---|
308 | String jsonData = getData(); |
---|
309 | Admin.notifyUser("JSON data:"+jsonData); |
---|
310 | |
---|
311 | XMLSerializer serializer = new XMLSerializer(); |
---|
312 | JSON json = JSONSerializer.toJSON( jsonData ); |
---|
313 | serializer.setRootName("WorkspaceProfile"); |
---|
314 | serializer.setElementName("item"); |
---|
315 | serializer.setTypeHintsEnabled(false); |
---|
316 | String xml = serializer.write( json ); |
---|
317 | Admin.notifyUser("XML:" + xml); |
---|
318 | |
---|
319 | InputStream is_xml = new ByteArrayInputStream( xml.getBytes( ) ); |
---|
320 | Admin.writeToFile(getPath(), is_xml); |
---|
321 | |
---|
322 | String ret = "1"; |
---|
323 | resultStream = new ByteArrayInputStream(ret.getBytes()); |
---|
324 | |
---|
325 | |
---|
326 | return SUCCESS; |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | * default Action method |
---|
331 | */ |
---|
332 | public String execute() throws Exception { |
---|
333 | |
---|
334 | prepare(); |
---|
335 | if (resultStream == null) { |
---|
336 | return ERROR; |
---|
337 | } else { |
---|
338 | return SUCCESS; |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | |
---|
343 | |
---|
344 | } |
---|