1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 package net.sourceforge.xuse.utils;
111
112 import java.io.File;
113 import java.io.FileFilter;
114 import java.io.FileInputStream;
115 import java.io.FileNotFoundException;
116 import java.io.FileOutputStream;
117 import java.io.FileWriter;
118 import java.io.IOException;
119 import java.io.InputStream;
120 import java.io.OutputStream;
121 import java.io.UnsupportedEncodingException;
122 import java.net.URL;
123 import java.net.URLDecoder;
124 import java.nio.channels.FileChannel;
125 import java.util.ArrayList;
126 import java.util.Arrays;
127 import java.util.Enumeration;
128 import java.util.Iterator;
129 import java.util.List;
130 import java.util.jar.JarEntry;
131 import java.util.jar.JarFile;
132
133 import net.sourceforge.xuse.build.BuildException;
134 import net.sourceforge.xuse.build.XusePropertiesReader;
135 import net.sourceforge.xuse.log.Logger;
136
137 import com.xmlsolutions.annotation.Requirement;
138
139
140 public class FileUtils {
141
142 private static final FileUtils LOG_INSTANCE = new FileUtils();
143
144 public static final File WORKING_DIR = new File(XusePropertiesReader.getXuseWorkingDir());
145
146 public static void copy(FileInputStream in, File destination) throws IOException {
147 FileChannel srcChannel = null;
148 FileChannel dstChannel = null;
149 try {
150 srcChannel = in.getChannel();
151 dstChannel = new FileOutputStream(destination).getChannel();
152 dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
153 } finally {
154 if (srcChannel != null) {
155 srcChannel.close();
156 }
157 if (dstChannel != null) {
158 dstChannel.close();
159 }
160 }
161 }
162
163 public static final boolean fileExists(String path) {
164 if (path != null) {
165 String safePath = path;
166 try {
167 safePath = URLDecoder.decode(path, "UTF-8");
168 } catch (UnsupportedEncodingException uee) {
169 Logger.warn(LOG_INSTANCE, "Could not safely decode file path", uee);
170 }
171 File file = new File(safePath);
172 return file.exists();
173 }
174 return false;
175 }
176
177 public static void copyFileToDir(String file, File destDir) throws IOException {
178 if (file != null) {
179 if (destDir != null) {
180 if (!destDir.exists()) {
181 destDir.mkdirs();
182 } else if (!destDir.isDirectory()) {
183 Logger.warn(LOG_INSTANCE, "Cannot copy file " + file + " to " + destDir.getAbsolutePath() + "; destination is not a directory");
184 }
185 Logger.debug(LOG_INSTANCE, "Copying " + file + " to " + destDir.getAbsolutePath());
186
187 File input = new File(file);
188 String fileName = input.getName();
189 File destFile = new File(destDir, fileName);
190 copyFile(input, destFile);
191 } else {
192 Logger.warn(LOG_INSTANCE, "Cannot copy file " + file + "; destination was null");
193 }
194 } else {
195 Logger.warn(LOG_INSTANCE, "Cannot copy file as file was null");
196 }
197 }
198
199 public static void copyFile(File input, File destination) throws IOException {
200 if (input != null) {
201 if (input.exists()) {
202 Logger.debug(LOG_INSTANCE, "Copying " + input.getAbsolutePath() + " to " + destination.getAbsolutePath());
203 copy(new FileInputStream(input), destination);
204 } else {
205 Logger.warn(LOG_INSTANCE, "Cannot copy " + input.getAbsolutePath() + " to " + destination.getAbsolutePath() + ", file does not exist");
206 }
207 } else {
208 Logger.warn(LOG_INSTANCE, "Cannot copy file to source or destination was null");
209 }
210 }
211
212 public static void copy(InputStream in, File destination) throws IOException {
213 Logger.debug(LOG_INSTANCE, "Writing: " + destination.getAbsolutePath());
214 OutputStream out = new FileOutputStream(destination);
215
216 byte[] buf = new byte[1024];
217 int len;
218 int totalSize = 0;
219 while ((len = in.read(buf)) > 0) {
220 out.write(buf, 0, len);
221 totalSize += len;
222 }
223 in.close();
224 out.flush();
225 out.close();
226 Logger.debug(LOG_INSTANCE, "Wrote: " + totalSize + " bytes");
227 }
228
229 public static void extractStyleSheets() throws IOException {
230
231 File xslDir = new File(WORKING_DIR, XusePropertiesReader.getXslDirectory());
232 xslDir.mkdirs();
233 copyDirectory(XusePropertiesReader.getXslDirectory(), WORKING_DIR);
234
235
236 }
237
238 public static void extractResources() throws IOException {
239 File resourcesDir = new File(WORKING_DIR, XusePropertiesReader.getXuseResourcesDirectory());
240 resourcesDir.mkdirs();
241 copyDirectory(XusePropertiesReader.getXuseResourcesDirectory(), WORKING_DIR);
242 }
243
244 public static void extractSchemas() throws IOException {
245 File dest = new File(System.getProperty("user.dir") + "/" + XusePropertiesReader.getXuseSchemaDir());
246 copyDirectory("xsd", dest, false);
247 }
248
249 @Requirement(traceTo={"RQ40"})
250 public static void extractTemplateProject() throws IOException {
251 File dest = new File(System.getProperty("user.dir"));
252 Logger.debug(LOG_INSTANCE, "Extracting template project to " + dest.getCanonicalPath());
253 copyDirectory(XusePropertiesReader.getTemplateProjectPath(), dest, false);
254 }
255
256 public static void extractResourceFromJar(String directoryInJar, String filename, File destination) throws IOException {
257 if (directoryInJar != null && destination != null) {
258 JarFile jarFile = getJarFile();
259 Enumeration<JarEntry> enumeration = jarFile.entries();
260 destination.getParentFile().mkdirs();
261 while (enumeration.hasMoreElements()) {
262 JarEntry entry = (JarEntry)enumeration.nextElement();
263 String entryName = entry.getName();
264 if (entryName.startsWith(directoryInJar + "/" + filename) && !entryName.endsWith("/")) {
265 InputStream in = getInputStreamFromSystemOrClasspath(entry.getName());
266 copy(in, destination);
267 }
268 }
269 }
270 }
271
272 public static boolean hasBeenExtracted() throws IOException {
273 File xslDir = new File(WORKING_DIR, XusePropertiesReader.getXslDirectory());
274 return xslDir.exists() && xslDir.isDirectory() && xslDir.list() != null && xslDir.list().length > 0;
275
276 }
277
278 public static JarFile getJarFile() throws BuildException {
279 String path = "unknown";
280 try {
281 URL jarPath = FileUtils.class.getResource("FileUtils.class");
282 Logger.debug(LOG_INSTANCE, "Getting Jar file URL: " + jarPath.toString());
283 path = jarPath.toString().replaceAll("%20", " ");
284 String os = System.getProperty("os.name");
285 Logger.debug(LOG_INSTANCE, "OS: " + os);
286 if (os != null && os.contains("windows")) {
287 path = path.replaceAll("jar:file:/", "");
288 } else {
289 path = path.replaceAll("jar:file:", "");
290 }
291 path = path.substring(0, path.indexOf("!"));
292 Logger.debug(LOG_INSTANCE, "Jar file location: " + path);
293 return new JarFile(path);
294 } catch (Throwable t) {
295 throw new BuildException("Could not locate Xuse jar file: found path " + path, t);
296 }
297 }
298
299 @Requirement(traceTo={"RQ19", "RQ60"})
300 public static void copyHTMLResources(String outputDir) throws IOException {
301 if (outputDir != null) {
302 copyHTMLResources(new File(outputDir));
303 }
304 }
305
306 public static void copyHTMLResources(File outputDir) throws IOException {
307 if (outputDir != null) {
308 copyDirectory("css", outputDir);
309 copyDirectory("images", outputDir);
310 copyDirectory("themes/" + XusePropertiesReader.getHTMLTheme(), outputDir);
311 copyDirectory("js", outputDir);
312
313 copyCustomResources(outputDir);
314 }
315 }
316
317 public static void copyCustomResources(File outputDir) throws IOException {
318
319 Logger.debug(LOG_INSTANCE, "in copyCustomResources()");
320 if (XusePropertiesReader.isCustomTheme()) {
321 String css = XusePropertiesReader.getHTMLTheme();
322 String fullCss = XusePropertiesReader.getResourcesDirectory() + "/" + css + ".css";
323 Logger.info(LOG_INSTANCE, "Got custom theme: " + css);
324 Logger.debug(LOG_INSTANCE, "CSS is: " + fullCss);
325 Logger.debug(LOG_INSTANCE, "before...");
326 copyFileToDir(fullCss, new File(outputDir, "css"));
327 Logger.debug(LOG_INSTANCE, "after...");
328 }
329 Logger.debug(LOG_INSTANCE, "here...");
330
331 File imageDir = new File(outputDir, "images");
332 String customerLogo = XusePropertiesReader.getCustomerLogo();
333 Logger.debug(LOG_INSTANCE, "Customer logo: " + customerLogo);
334 copyFileToDir(customerLogo, imageDir);
335 String companyLogo = XusePropertiesReader.getCompanyLogo();
336 Logger.debug(LOG_INSTANCE, "Company logo: " + companyLogo);
337 copyFileToDir(companyLogo, imageDir);
338 String projectLogo = XusePropertiesReader.getProjectLogo();
339 Logger.debug(LOG_INSTANCE, "Project logo: " + projectLogo);
340 copyFileToDir(projectLogo, imageDir);
341 }
342
343 public static void copyDirectory(File sourceDir, File destDir) throws IOException {
344 if (sourceDir != null && sourceDir.isDirectory() && destDir != null) {
345 if (destDir.exists() && destDir.isFile()) {
346 throw new IOException("Cannot write files to location " + destDir.getCanonicalPath());
347 } else if (!destDir.exists()) {
348 destDir.mkdir();
349 }
350 List<File> srcFiles = Arrays.asList(sourceDir.listFiles());
351 Iterator<File> iter = srcFiles.iterator();
352 File currentFile = null;
353 while (iter.hasNext()) {
354 currentFile = (File)iter.next();
355 if (currentFile.isDirectory()) {
356 copyDirectory(currentFile, new File(destDir, currentFile.getName()));
357 } else {
358 copy(new FileInputStream(currentFile), new File(destDir, currentFile.getName()));
359 }
360 }
361 }
362 }
363
364 public static void copyDirectory(String directoryInJar, File destination, boolean mirrorDir) throws IOException {
365 Logger.debug(LOG_INSTANCE, "Copying directory from " + directoryInJar + " in jar file to " + destination.getCanonicalPath());
366 if (directoryInJar != null && destination != null) {
367 JarFile jarFile = getJarFile();
368 Enumeration<JarEntry> enumeration = jarFile.entries();
369 File destDir = mirrorDir ? new File(destination, directoryInJar) : destination;
370 destDir.mkdirs();
371 while (enumeration.hasMoreElements()) {
372 JarEntry entry = (JarEntry)enumeration.nextElement();
373 String entryName = entry.getName();
374 if (entryName.startsWith(directoryInJar + "/") && !entryName.endsWith("/")) {
375 InputStream in = getInputStreamFromSystemOrClasspath(entry.getName());
376 String outputFileName = null;
377 if (mirrorDir) {
378 outputFileName = entry.getName();
379 } else {
380 int index = entry.getName().indexOf(directoryInJar) + directoryInJar.length();
381 outputFileName = entry.getName().substring(index);
382 }
383
384 File dest = new File(destination, outputFileName);
385 File folder = dest.getParentFile();
386 if (!folder.exists()) {
387 if (!folder.mkdirs()) {
388 throw new BuildException("Could not create folder: " + folder.getAbsolutePath());
389 }
390 }
391 if (dest.exists() && dest.isFile()) {
392 Logger.debug(LOG_INSTANCE, "Destination " + dest.getAbsolutePath() + " already exists");
393 dest.delete();
394 }
395 copy(in, dest);
396 }
397 }
398 }
399 }
400
401 public static void copyDirectory(String directoryInJar, File destination) throws IOException {
402 if (directoryInJar != null && destination != null) {
403 copyDirectory(directoryInJar, destination, true);
404 }
405 }
406
407 public static void copyResource(String file, String directory, File destDir) throws IOException {
408
409 File destDirF = new File(destDir, directory);
410 if (!destDirF.exists()) {
411 destDirF.mkdirs();
412 }
413 File outfile = new File(destDirF, file);
414 InputStream in = getInputStreamFromSystemOrClasspath(directory + "/" + file);
415 copy(in, outfile);
416 }
417
418
419 public static void archive(File file) throws Exception {
420 if (file != null && file.isFile() && file.canWrite() && file.exists()) {
421 File newFile = new File(file.getParent(), file.getName() + "." + System.currentTimeMillis() + ".old");
422 Logger.info(LOG_INSTANCE, "Archiving old file: " + file.getAbsolutePath() + " to " + newFile.getAbsolutePath());
423 if (!(file.renameTo(newFile))) {
424 throw new IOException("Could not rename: " + file.getAbsolutePath());
425 }
426 }
427 }
428
429 public static void renameUpgraded(File file) throws Exception {
430 if (file != null && file.isFile() && file.canWrite()) {
431 Logger.info(LOG_INSTANCE, "Renaming upgraded file: " + file.getCanonicalPath());
432 String newName = Utils.substringBefore(file.getName(), "-upgrade.xml");
433 file.renameTo(new File(file.getParent(), newName + ".xml"));
434 }
435 }
436
437 public static List<String> getFilesInDir(File dir, FileFilter filter) {
438 if (dir != null && dir.isDirectory()) {
439 Logger.debug(LOG_INSTANCE, "Looking for files in directory: " + dir.getAbsolutePath());
440 List<String> allFiles = new ArrayList<String>();
441 File[] children = dir.listFiles(filter);
442 Logger.debug(LOG_INSTANCE, "Found " + children.length + " children");
443 for (int i = 0; i < children.length; i++) {
444 if (children[i].isDirectory()) {
445 allFiles.addAll(getFilesInDir(children[i], filter));
446 } else {
447 Logger.debug(LOG_INSTANCE, "Adding child: " + children[i].getAbsolutePath());
448 allFiles.add(children[i].getAbsolutePath());
449 }
450 }
451 return allFiles;
452 } else {
453 Logger.warn(LOG_INSTANCE, "Could not find any files at " + dir);
454 }
455 return null;
456 }
457
458 public static void writeFileContents(File output, String contents) throws BuildException {
459 if (output != null) {
460 try {
461 if (!output.exists()) {
462 output.createNewFile();
463 }
464 FileWriter fileWriter = new FileWriter(output);
465 fileWriter.append(contents);
466 fileWriter.close();
467 } catch (IOException ioe) {
468 throw new BuildException("COuld not write file contents", ioe);
469 }
470 }
471 }
472
473 private static InputStream getInputStreamFromSystemOrClasspath(String name) throws BuildException {
474 File file = new File(name);
475 try {
476 return new FileInputStream(file);
477 } catch (FileNotFoundException fnfe) {
478 try {
479 URL url = FileUtils.class.getClassLoader().getResource(name);
480 File in = new File(url.getPath());
481 if (!in.exists()) {
482 InputStream inStream = FileUtils.class.getClassLoader().getResourceAsStream(name);
483 if (inStream == null) {
484 throw new BuildException("Cannot find file " + name);
485 }
486 return inStream;
487 }
488 return new FileInputStream(in.getAbsolutePath());
489 } catch (Throwable t) {
490 throw new BuildException("Error finding the file - " + name + " - in the classpath.", t);
491 }
492 } catch (Throwable t) {
493 throw new BuildException("Error finding the file - " + name + " - in the classpath.", t);
494 }
495 }
496
497 }