1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
package net.fckeditor.response; |
22 | |
|
23 | |
import java.io.File; |
24 | |
import java.io.FileFilter; |
25 | |
import java.io.StringWriter; |
26 | |
|
27 | |
import javax.xml.parsers.DocumentBuilder; |
28 | |
import javax.xml.parsers.DocumentBuilderFactory; |
29 | |
import javax.xml.parsers.ParserConfigurationException; |
30 | |
import javax.xml.transform.Transformer; |
31 | |
import javax.xml.transform.TransformerException; |
32 | |
import javax.xml.transform.TransformerFactory; |
33 | |
import javax.xml.transform.dom.DOMSource; |
34 | |
import javax.xml.transform.stream.StreamResult; |
35 | |
|
36 | |
import net.fckeditor.handlers.CommandHandler; |
37 | |
import net.fckeditor.handlers.ResourceTypeHandler; |
38 | |
import net.fckeditor.tool.Utils; |
39 | |
|
40 | |
import org.apache.commons.io.filefilter.DirectoryFileFilter; |
41 | |
import org.apache.commons.io.filefilter.FileFileFilter; |
42 | |
import org.w3c.dom.Document; |
43 | |
import org.w3c.dom.Element; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class XmlResponse { |
54 | |
|
55 | |
private Document document; |
56 | |
private Element errorElement; |
57 | |
private Element foldersElement; |
58 | |
private Element filesElement; |
59 | |
|
60 | |
|
61 | |
public static final int EN_OK = 0; |
62 | |
|
63 | |
|
64 | |
public static final int EN_ERROR = 1; |
65 | |
|
66 | |
|
67 | |
public static final int EN_ALREADY_EXISTS = 101; |
68 | |
|
69 | |
|
70 | |
public static final int EN_INVALID_FOLDER_NAME = 102; |
71 | |
|
72 | |
|
73 | |
public static final int EN_SECURITY_ERROR = 103; |
74 | |
|
75 | |
|
76 | |
public static final int EN_UKNOWN = 110; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public XmlResponse(CommandHandler command, ResourceTypeHandler resourceType, |
87 | 0 | String currentFolder, String constructedUrl) { |
88 | |
|
89 | |
try { |
90 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory |
91 | |
.newInstance(); |
92 | 0 | DocumentBuilder builder = factory.newDocumentBuilder(); |
93 | 0 | document = builder.newDocument(); |
94 | 0 | } catch (ParserConfigurationException e) { |
95 | 0 | throw new RuntimeException(e); |
96 | 0 | } |
97 | |
|
98 | 0 | Element root = document.createElement("Connector"); |
99 | 0 | document.appendChild(root); |
100 | 0 | root.setAttribute("command", command.toString()); |
101 | 0 | root.setAttribute("resourceType", resourceType.toString()); |
102 | |
|
103 | 0 | Element currentFolderElement = document.createElement("CurrentFolder"); |
104 | 0 | currentFolderElement.setAttribute("path", currentFolder); |
105 | |
|
106 | 0 | currentFolderElement.setAttribute("url", constructedUrl); |
107 | 0 | root.appendChild(currentFolderElement); |
108 | |
|
109 | 0 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | 0 | public XmlResponse(int number, String text) { |
120 | |
try { |
121 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory |
122 | |
.newInstance(); |
123 | 0 | DocumentBuilder builder = factory.newDocumentBuilder(); |
124 | 0 | document = builder.newDocument(); |
125 | 0 | } catch (ParserConfigurationException e) { |
126 | 0 | throw new RuntimeException(e); |
127 | 0 | } |
128 | |
|
129 | 0 | Element root = document.createElement("Connector"); |
130 | 0 | document.appendChild(root); |
131 | 0 | setError(number, text); |
132 | 0 | } |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public XmlResponse(int number) { |
140 | 0 | this(number, null); |
141 | 0 | } |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public void setError(int number, String text) { |
150 | |
|
151 | 0 | if (errorElement == null) { |
152 | 0 | errorElement = document.createElement("Error"); |
153 | 0 | document.getDocumentElement().appendChild(errorElement); |
154 | |
} |
155 | |
|
156 | 0 | errorElement.setAttribute("number", String.valueOf(number)); |
157 | 0 | if (Utils.isNotEmpty(text)) |
158 | 0 | errorElement.setAttribute("text", text); |
159 | |
|
160 | 0 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public void setError(int number) { |
168 | 0 | setError(number, null); |
169 | 0 | } |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public void setFolders(File dir) { |
176 | |
|
177 | 0 | if (foldersElement != null) { |
178 | 0 | Element parent = (Element) foldersElement.getParentNode(); |
179 | 0 | parent.removeChild(foldersElement); |
180 | |
} |
181 | |
|
182 | 0 | foldersElement = document.createElement("Folders"); |
183 | 0 | document.getDocumentElement().appendChild(foldersElement); |
184 | |
|
185 | 0 | String[] fileList = dir.list(DirectoryFileFilter.DIRECTORY); |
186 | 0 | for (String file : fileList) { |
187 | 0 | Element folderElement = document.createElement("Folder"); |
188 | 0 | folderElement.setAttribute("name", file); |
189 | 0 | foldersElement.appendChild(folderElement); |
190 | |
} |
191 | 0 | } |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public void setFiles(File dir) { |
199 | |
|
200 | 0 | if (filesElement != null) { |
201 | 0 | Element parent = (Element) filesElement.getParentNode(); |
202 | 0 | parent.removeChild(filesElement); |
203 | |
} |
204 | |
|
205 | 0 | filesElement = document.createElement("Files"); |
206 | 0 | document.getDocumentElement().appendChild(filesElement); |
207 | |
|
208 | 0 | File[] fileList = dir.listFiles((FileFilter) FileFileFilter.FILE); |
209 | |
long length; |
210 | 0 | for (File file : fileList) { |
211 | 0 | Element fileElement = document.createElement("File"); |
212 | 0 | fileElement.setAttribute("name", file.getName()); |
213 | 0 | if (file.length() < 1024) |
214 | 0 | length = 1L; |
215 | |
else |
216 | 0 | length = file.length()/1024; |
217 | 0 | fileElement.setAttribute("size", String.valueOf(length)); |
218 | 0 | filesElement.appendChild(fileElement); |
219 | |
} |
220 | 0 | } |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public void setFoldersAndFiles(File dir) { |
228 | 0 | setFolders(dir); |
229 | 0 | setFiles(dir); |
230 | 0 | } |
231 | |
|
232 | |
@Override |
233 | |
public String toString() { |
234 | 0 | document.getDocumentElement().normalize(); |
235 | 0 | TransformerFactory factory = TransformerFactory.newInstance(); |
236 | |
|
237 | 0 | StringWriter sw = new StringWriter(); |
238 | |
|
239 | |
try { |
240 | 0 | Transformer transformer = factory.newTransformer(); |
241 | |
|
242 | 0 | DOMSource source = new DOMSource(document); |
243 | 0 | StreamResult result = new StreamResult(sw); |
244 | |
|
245 | 0 | transformer.transform(source, result); |
246 | 0 | } catch (TransformerException e) { |
247 | 0 | throw new RuntimeException(e); |
248 | 0 | } |
249 | |
|
250 | 0 | return sw.toString(); |
251 | |
} |
252 | |
|
253 | |
} |