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 | |
public XmlResponse(CommandHandler command, ResourceTypeHandler resourceType, |
86 | 0 | String currentFolder, String constructedUrl) { |
87 | |
|
88 | |
try { |
89 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory |
90 | |
.newInstance(); |
91 | 0 | DocumentBuilder builder = factory.newDocumentBuilder(); |
92 | 0 | document = builder.newDocument(); |
93 | 0 | } catch (ParserConfigurationException e) { |
94 | 0 | throw new RuntimeException(e); |
95 | 0 | } |
96 | |
|
97 | 0 | Element root = document.createElement("Connector"); |
98 | 0 | document.appendChild(root); |
99 | 0 | root.setAttribute("command", command.toString()); |
100 | 0 | root.setAttribute("resourceType", resourceType.toString()); |
101 | |
|
102 | 0 | Element currentFolderElement = document.createElement("CurrentFolder"); |
103 | 0 | currentFolderElement.setAttribute("path", currentFolder); |
104 | |
|
105 | 0 | currentFolderElement.setAttribute("url", constructedUrl); |
106 | 0 | root.appendChild(currentFolderElement); |
107 | |
|
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | 0 | public XmlResponse(int number, String text) { |
116 | |
try { |
117 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory |
118 | |
.newInstance(); |
119 | 0 | DocumentBuilder builder = factory.newDocumentBuilder(); |
120 | 0 | document = builder.newDocument(); |
121 | 0 | } catch (ParserConfigurationException e) { |
122 | 0 | throw new RuntimeException(e); |
123 | 0 | } |
124 | |
|
125 | 0 | Element root = document.createElement("Connector"); |
126 | 0 | document.appendChild(root); |
127 | 0 | setError(number, text); |
128 | 0 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public XmlResponse(int number) { |
135 | 0 | this(number, null); |
136 | 0 | } |
137 | |
|
138 | |
public void setError(int number, String text) { |
139 | |
|
140 | 0 | if (errorElement == null) { |
141 | 0 | errorElement = document.createElement("Error"); |
142 | 0 | document.getDocumentElement().appendChild(errorElement); |
143 | |
} |
144 | |
|
145 | 0 | errorElement.setAttribute("number", String.valueOf(number)); |
146 | 0 | if (Utils.isNotEmpty(text)) |
147 | 0 | errorElement.setAttribute("text", text); |
148 | |
|
149 | 0 | } |
150 | |
|
151 | |
public void setError(int number) { |
152 | 0 | setError(number, null); |
153 | 0 | } |
154 | |
|
155 | |
public void setFolders(File dir) { |
156 | |
|
157 | 0 | if (foldersElement != null) { |
158 | 0 | Element parent = (Element) foldersElement.getParentNode(); |
159 | 0 | parent.removeChild(foldersElement); |
160 | |
} |
161 | |
|
162 | 0 | foldersElement = document.createElement("Folders"); |
163 | 0 | document.getDocumentElement().appendChild(foldersElement); |
164 | |
|
165 | 0 | String[] fileList = dir.list(DirectoryFileFilter.DIRECTORY); |
166 | 0 | for (String file : fileList) { |
167 | 0 | Element folderElement = document.createElement("Folder"); |
168 | 0 | folderElement.setAttribute("name", file); |
169 | 0 | foldersElement.appendChild(folderElement); |
170 | |
} |
171 | 0 | } |
172 | |
|
173 | |
public void setFiles(File dir) { |
174 | |
|
175 | 0 | if (filesElement != null) { |
176 | 0 | Element parent = (Element) filesElement.getParentNode(); |
177 | 0 | parent.removeChild(filesElement); |
178 | |
} |
179 | |
|
180 | 0 | filesElement = document.createElement("Files"); |
181 | 0 | document.getDocumentElement().appendChild(filesElement); |
182 | |
|
183 | 0 | File[] fileList = dir.listFiles((FileFilter) FileFileFilter.FILE); |
184 | |
long length; |
185 | 0 | for (File file : fileList) { |
186 | 0 | Element fileElement = document.createElement("File"); |
187 | 0 | fileElement.setAttribute("name", file.getName()); |
188 | 0 | if (file.length() < 1024) |
189 | 0 | length = 1L; |
190 | |
else |
191 | 0 | length = file.length()/1024; |
192 | 0 | fileElement.setAttribute("size", String.valueOf(length)); |
193 | 0 | filesElement.appendChild(fileElement); |
194 | |
} |
195 | 0 | } |
196 | |
|
197 | |
public void setFoldersAndFiles(File dir) { |
198 | 0 | setFolders(dir); |
199 | 0 | setFiles(dir); |
200 | 0 | } |
201 | |
|
202 | |
@Override |
203 | |
public String toString() { |
204 | 0 | document.getDocumentElement().normalize(); |
205 | 0 | TransformerFactory factory = TransformerFactory.newInstance(); |
206 | |
|
207 | 0 | StringWriter sw = new StringWriter(); |
208 | |
|
209 | |
try { |
210 | 0 | Transformer transformer = factory.newTransformer(); |
211 | |
|
212 | 0 | DOMSource source = new DOMSource(document); |
213 | 0 | StreamResult result = new StreamResult(sw); |
214 | |
|
215 | 0 | transformer.transform(source, result); |
216 | 0 | } catch (TransformerException e) { |
217 | 0 | throw new RuntimeException(e); |
218 | 0 | } |
219 | |
|
220 | 0 | return sw.toString(); |
221 | |
} |
222 | |
|
223 | |
} |