1 package com.melloware.jukes.file;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.Map;
8
9 import org.apache.commons.lang.StringUtils;
10 import org.apache.commons.lang.SystemUtils;
11 import org.apache.commons.lang.WordUtils;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import com.melloware.jukes.exception.InfrastructureException;
16
17
18
19
20
21
22
23
24
25 public final class FileUtil {
26
27 private static final Log LOG = LogFactory.getLog(FileUtil.class);
28 public static final String WINDOWS_COMMAND = "attrib -R ";
29 public static final String NIX_COMMAND = "chmod u+w ";
30 private static final String INVALID_OUTPUT_NAME_REGEX = "[\\\\\\/\\:\\*\\\"\\<\\>\\|\\?]";
31 private static final char[] CAPS_CHARS = new char[] { ' ', '.', '"', '(', '[', ',', ':', '-', ']', ')', '/', '\\'};
32
33
34
35
36 private FileUtil() {
37
38 }
39
40
41
42
43
44
45
46
47 public static void setReadOnly(final File aFile, final boolean flag) {
48 if (flag) {
49 aFile.setReadOnly();
50 } else {
51 String path = aFile.getAbsolutePath();
52 if (path.indexOf(' ') >= 0) {
53 path = String.valueOf(String.valueOf((new StringBuffer(String.valueOf(String.valueOf('"')))).append(path).append('"')));
54 }
55
56 final String command = getCommand() + path;
57 try {
58 LOG.debug(command);
59 Runtime.getRuntime().exec(command).waitFor();
60 } catch (IOException ioexception) {
61 LOG.error("Error clearing the read-only attribute of ".concat(String.valueOf(String.valueOf(path))),
62 ioexception);
63 } catch (InterruptedException interruptedexception) {
64 LOG.error("Error clearing the read-only attribute of ".concat(String.valueOf(String.valueOf(path))),
65 interruptedexception);
66 }
67 }
68 }
69
70
71
72
73
74
75
76
77 public static String capitalize(final String aStringToCapitalize) {
78 String word = WordUtils.capitalizeFully(aStringToCapitalize, CAPS_CHARS);
79 word = StringUtils.replace(word, "Ii", "II");
80 word = StringUtils.replace(word, "IIi", "III");
81 word = StringUtils.replace(word, "Iv", "IV");
82 return word;
83 }
84
85
86
87
88
89
90 public static void checkValidFileName(final String aFilename, final String message) {
91 if (aFilename.matches(INVALID_OUTPUT_NAME_REGEX)) {
92 throw new InfrastructureException(message);
93 }
94 }
95
96
97
98
99
100
101
102
103 public static String correctFileName(final String aFilename) {
104 return aFilename.replaceAll(INVALID_OUTPUT_NAME_REGEX, "_");
105 }
106
107
108
109
110
111
112
113 @SuppressWarnings("unchecked")
114 public static Map deepCopy(final Map src) {
115 final HashMap map = new HashMap();
116 if (src != null) {
117 final Iterator it = src.keySet().iterator();
118 while (it.hasNext()) {
119 final Object key = it.next();
120 final Object value = src.get(key);
121 map.put(key, value);
122 }
123 }
124 return map;
125 }
126
127
128
129
130
131
132
133
134 public static boolean rename(final String sOldname, final String sNewname) {
135 if (LOG.isDebugEnabled()) {
136 LOG.debug("Renaming: '" + sOldname + "' to '" + sNewname + "'");
137 }
138
139 final File oldFile = new File(sOldname);
140 final File newFile = new File(sNewname);
141
142 final boolean success = oldFile.renameTo(newFile);
143
144 if (LOG.isDebugEnabled()) {
145 LOG.debug("Completed reanaming: " + success);
146 }
147
148 return success;
149 }
150
151
152
153
154
155
156
157 private static String getCommand() {
158 if (SystemUtils.IS_OS_WINDOWS) {
159 return WINDOWS_COMMAND;
160 } else {
161 return NIX_COMMAND;
162 }
163 }
164
165 }