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