1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | |
---|
4 | import org.apache.commons.lang.*; |
---|
5 | import com.opensymphony.xwork2.ActionSupport; |
---|
6 | import net.sf.json.JSON; |
---|
7 | import net.sf.json.JSONArray; |
---|
8 | import net.sf.json.JSONObject; |
---|
9 | import net.sf.json.JSONSerializer; |
---|
10 | import net.sf.json.JSONException; |
---|
11 | import net.sf.json.xml.XMLSerializer; |
---|
12 | |
---|
13 | import java.io.BufferedInputStream; |
---|
14 | import java.io.File; |
---|
15 | import java.io.FileInputStream; |
---|
16 | import java.io.FileWriter; |
---|
17 | import java.io.IOException; |
---|
18 | import java.io.InputStream; |
---|
19 | import java.io.StringReader; |
---|
20 | import java.io.StringWriter; |
---|
21 | import java.net.MalformedURLException; |
---|
22 | import java.net.URL; |
---|
23 | import java.util.HashMap; |
---|
24 | import java.io.ByteArrayInputStream; |
---|
25 | |
---|
26 | import javax.xml.parsers.DocumentBuilder; |
---|
27 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
28 | import javax.xml.parsers.ParserConfigurationException; |
---|
29 | import javax.xml.transform.OutputKeys; |
---|
30 | import javax.xml.transform.Transformer; |
---|
31 | import javax.xml.transform.TransformerConfigurationException; |
---|
32 | import javax.xml.transform.TransformerException; |
---|
33 | import javax.xml.transform.TransformerFactory; |
---|
34 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
35 | import javax.xml.transform.dom.DOMSource; |
---|
36 | import javax.xml.transform.stream.StreamResult; |
---|
37 | |
---|
38 | import org.apache.commons.io.IOUtils; |
---|
39 | import org.w3c.dom.Document; |
---|
40 | import org.w3c.dom.Element; |
---|
41 | import org.w3c.dom.Node; |
---|
42 | import org.w3c.dom.NodeList; |
---|
43 | import org.xml.sax.InputSource; |
---|
44 | import org.xml.sax.SAXException; |
---|
45 | |
---|
46 | import eu.clarin.cmdi.mdservice.model.Query; |
---|
47 | |
---|
48 | /** |
---|
49 | * Query load , save from PROFILE |
---|
50 | * |
---|
51 | * @author |
---|
52 | * |
---|
53 | */ |
---|
54 | public class SaveWorkspaceAction extends GenericProxyAction { |
---|
55 | |
---|
56 | /** |
---|
57 | * |
---|
58 | */ |
---|
59 | private static final long serialVersionUID = 1L; |
---|
60 | private String q; |
---|
61 | private String id; |
---|
62 | private String base_path; |
---|
63 | |
---|
64 | |
---|
65 | /* |
---|
66 | public XMLSerializer getXMLSerializer(){ |
---|
67 | if (serializer == null){ |
---|
68 | serializer = new XMLSerializer(); |
---|
69 | } |
---|
70 | return serializer; |
---|
71 | } |
---|
72 | public JSONSerializer getJSONSerializer(){ |
---|
73 | if (json_serializer == null){ |
---|
74 | json_serializer = new JSONSerializer(); |
---|
75 | } |
---|
76 | return json_serializer; |
---|
77 | } |
---|
78 | */ |
---|
79 | public String getQ() { |
---|
80 | return q; |
---|
81 | } |
---|
82 | |
---|
83 | public void setQ(String q) { |
---|
84 | this.q = q; |
---|
85 | } |
---|
86 | |
---|
87 | public String getID() { |
---|
88 | return id; |
---|
89 | } |
---|
90 | |
---|
91 | public void setID(String _id) { |
---|
92 | this.id = _id; |
---|
93 | } |
---|
94 | public String getWorkspaceProfile() { |
---|
95 | return "WorkspaceProfile.xml"; |
---|
96 | } |
---|
97 | |
---|
98 | public String getBasePath() { |
---|
99 | if (base_path == null) { |
---|
100 | base_path = Admin.getConfig().getProperty("query.path"); |
---|
101 | } |
---|
102 | return base_path; |
---|
103 | } |
---|
104 | public String getPath() { |
---|
105 | String targetPath = getBasePath() + getWorkspaceProfile(); |
---|
106 | |
---|
107 | return targetPath; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | public String getProxyKey() { |
---|
112 | return ""; |
---|
113 | } |
---|
114 | |
---|
115 | public String getFullFormat() { |
---|
116 | return ""; |
---|
117 | } |
---|
118 | |
---|
119 | public void changeNodeName(Document doc, Node node, String new_name){ |
---|
120 | // Create an element with the new name |
---|
121 | Node node2 = (Node ) doc.createElement(new_name); |
---|
122 | // 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); } |
---|
123 | // Move all the children |
---|
124 | while (node.hasChildNodes()) { node2.appendChild(node.getFirstChild()); } |
---|
125 | // Replace the old node with the new node |
---|
126 | Admin.notifyUser("replace:" + node2.getNodeName() + "," + node.getNodeName()); |
---|
127 | node.getParentNode().replaceChild(node2, node); |
---|
128 | } |
---|
129 | public void prepare(){ |
---|
130 | |
---|
131 | String jsonData = getQ(); |
---|
132 | Admin.notifyUser("JSON data:"+jsonData); |
---|
133 | |
---|
134 | XMLSerializer serializer = new XMLSerializer(); |
---|
135 | JSON json = JSONSerializer.toJSON( jsonData ); |
---|
136 | //Admin.notifyUser("test5"); |
---|
137 | serializer.setRootName("WorkspaceProfile"); |
---|
138 | serializer.setTypeHintsEnabled(false); |
---|
139 | String xml = serializer.write( json ); |
---|
140 | Admin.notifyUser("XML:" + xml); |
---|
141 | |
---|
142 | |
---|
143 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
---|
144 | DocumentBuilder builder; |
---|
145 | try { |
---|
146 | builder = factory.newDocumentBuilder(); |
---|
147 | try { |
---|
148 | Document document = builder.parse(new InputSource(new StringReader(xml))); |
---|
149 | NodeList list = document.getElementsByTagName("e"); |
---|
150 | Admin.notifyUser("list:" + list.getLength()); |
---|
151 | while (list.getLength() > 0){ |
---|
152 | Admin.notifyUser(0 + "::" + list.item(0).getNodeName() + "::" + list.item(0).getParentNode().getNodeName()); |
---|
153 | if (list.item(0).getParentNode().getNodeName().equals("Queries")){ |
---|
154 | changeNodeName(document, list.item(0),"Query"); |
---|
155 | }else{ |
---|
156 | if (list.item(0).getParentNode().getNodeName().equals("Querysets")){ |
---|
157 | changeNodeName(document, list.item(0),"Queryset"); |
---|
158 | } else{ |
---|
159 | if (list.item(0).getParentNode().getNodeName().equals("collections")){ |
---|
160 | changeNodeName(document, list.item(0),"item"); |
---|
161 | } |
---|
162 | } |
---|
163 | } |
---|
164 | list = document.getElementsByTagName("e"); |
---|
165 | } |
---|
166 | |
---|
167 | Transformer transformer; |
---|
168 | try { |
---|
169 | transformer = TransformerFactory.newInstance().newTransformer(); |
---|
170 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
171 | StreamResult result = new StreamResult(new StringWriter()); |
---|
172 | DOMSource source = new DOMSource(document); |
---|
173 | try { |
---|
174 | transformer.transform(source, result); |
---|
175 | String xmlString = result.getWriter().toString(); |
---|
176 | |
---|
177 | Admin.notifyUser("XML:" + xmlString); |
---|
178 | |
---|
179 | InputStream is_xml = new ByteArrayInputStream( xmlString.getBytes( ) ); |
---|
180 | Admin.writeToFile(getPath(), is_xml); |
---|
181 | |
---|
182 | } catch (TransformerException e1) { |
---|
183 | // TODO Auto-generated catch block |
---|
184 | e1.printStackTrace(); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | } catch (TransformerConfigurationException e) { |
---|
190 | // TODO Auto-generated catch block |
---|
191 | e.printStackTrace(); |
---|
192 | } catch (TransformerFactoryConfigurationError e) { |
---|
193 | // TODO Auto-generated catch block |
---|
194 | e.printStackTrace(); |
---|
195 | } |
---|
196 | |
---|
197 | } catch (SAXException e) { |
---|
198 | // TODO Auto-generated catch block |
---|
199 | e.printStackTrace(); |
---|
200 | } catch (IOException e) { |
---|
201 | // TODO Auto-generated catch block |
---|
202 | e.printStackTrace(); |
---|
203 | } |
---|
204 | } catch (ParserConfigurationException e) { |
---|
205 | // TODO Auto-generated catch block |
---|
206 | e.printStackTrace(); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | } |
---|
211 | |
---|
212 | public String execute() throws Exception { |
---|
213 | |
---|
214 | prepare(); |
---|
215 | return SUCCESS; |
---|
216 | |
---|
217 | |
---|
218 | } |
---|
219 | } |
---|