|
Lines 110-154
Link Here
|
| 110 |
char fileSeparatorChar = File.separatorChar; |
110 |
char fileSeparatorChar = File.separatorChar; |
| 111 |
String fileSeparator = File.separator; |
111 |
String fileSeparator = File.separator; |
| 112 |
File f; |
112 |
File f; |
| 113 |
// First we ensure that the outputPath exists |
|
|
| 114 |
outputPath = outputPath.replace('/', fileSeparatorChar); |
113 |
outputPath = outputPath.replace('/', fileSeparatorChar); |
| 115 |
// To be able to pass the mkdirs() method we need to remove the extra file separator at the end of the outDir name |
114 |
// these could be optimized out if we normalized paths once and for |
| 116 |
if (outputPath.endsWith(fileSeparator)) { |
115 |
// all |
| 117 |
outputPath = outputPath.substring(0, outputPath.length() - 1); |
116 |
relativeFileName = relativeFileName.replace('/', fileSeparatorChar); |
| 118 |
} |
117 |
String outputDirPath, fileName; |
| 119 |
f = new File(outputPath); |
118 |
int separatorIndex = relativeFileName.lastIndexOf(fileSeparatorChar); |
| 120 |
if (f.exists()) { |
119 |
if (separatorIndex == -1) { |
| 121 |
if (!f.isDirectory()) { |
120 |
if (outputPath.endsWith(fileSeparator)) { |
| 122 |
final String message = Messages.bind(Messages.output_isFile, f.getAbsolutePath()); |
121 |
outputDirPath = outputPath.substring(0, outputPath.length() - 1); |
| 123 |
throw new IOException(message); |
122 |
fileName = outputPath + relativeFileName; |
|
|
123 |
} else { |
| 124 |
outputDirPath = outputPath; |
| 125 |
fileName = outputPath + fileSeparator + relativeFileName; |
| 124 |
} |
126 |
} |
| 125 |
} else { |
127 |
} else { |
| 126 |
// we have to create that directory |
128 |
if (outputPath.endsWith(fileSeparator)) { |
| 127 |
if (!f.mkdirs()) { |
129 |
outputDirPath = outputPath + |
| 128 |
final String message = Messages.bind(Messages.output_notValidAll, f.getAbsolutePath()); |
130 |
relativeFileName.substring(0, separatorIndex); |
| 129 |
throw new IOException(message); |
131 |
fileName = outputPath + relativeFileName; |
|
|
132 |
} else { |
| 133 |
outputDirPath = outputPath + fileSeparator + |
| 134 |
relativeFileName.substring(0, separatorIndex); |
| 135 |
fileName = outputPath + fileSeparator + relativeFileName; |
| 130 |
} |
136 |
} |
| 131 |
} |
137 |
} |
| 132 |
StringBuffer outDir = new StringBuffer(outputPath); |
138 |
f = new File(outputDirPath); |
| 133 |
outDir.append(fileSeparator); |
139 |
f.mkdirs(); |
| 134 |
StringTokenizer tokenizer = |
140 |
if (f.isDirectory()) { |
| 135 |
new StringTokenizer(relativeFileName, fileSeparator); |
141 |
return fileName; |
| 136 |
String token = tokenizer.nextToken(); |
142 |
} else { |
| 137 |
while (tokenizer.hasMoreTokens()) { |
143 |
// the directory creation failed for some reason - retry using |
| 138 |
f = new File(outDir.append(token).append(fileSeparator).toString()); |
144 |
// a slower algorithm so as to refine the diagnostic |
|
|
145 |
if (outputPath.endsWith(fileSeparator)) { |
| 146 |
outputPath = outputPath.substring(0, outputPath.length() - 1); |
| 147 |
} |
| 148 |
f = new File(outputPath); |
| 149 |
boolean checkFileType = false; |
| 139 |
if (f.exists()) { |
150 |
if (f.exists()) { |
| 140 |
// The outDir already exists, so we proceed the next entry |
151 |
checkFileType = true; // pre-existed |
| 141 |
// System.out.println("outDir: " + outDir + " already exists."); |
|
|
| 142 |
} else { |
152 |
} else { |
| 143 |
// Need to add the outDir |
153 |
// we have to create that directory |
| 144 |
if (!f.mkdir()) { |
154 |
if (!f.mkdirs()) { |
| 145 |
throw new IOException(Messages.bind(Messages.output_notValid, f.getName())); |
155 |
if (f.exists()) { |
| 146 |
} |
156 |
// someone else created f -- need to check its type |
|
|
157 |
checkFileType = true; |
| 158 |
} else { |
| 159 |
// no one could create f -- complain |
| 160 |
throw new IOException(Messages.bind( |
| 161 |
Messages.output_notValidAll, f.getAbsolutePath())); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
if (checkFileType) { |
| 166 |
if (!f.isDirectory()) { |
| 167 |
throw new IOException(Messages.bind( |
| 168 |
Messages.output_isFile, f.getAbsolutePath())); |
| 169 |
} |
| 170 |
} |
| 171 |
StringBuffer outDir = new StringBuffer(outputPath); |
| 172 |
outDir.append(fileSeparator); |
| 173 |
StringTokenizer tokenizer = |
| 174 |
new StringTokenizer(relativeFileName, fileSeparator); |
| 175 |
String token = tokenizer.nextToken(); |
| 176 |
while (tokenizer.hasMoreTokens()) { |
| 177 |
f = new File(outDir.append(token).append(fileSeparator).toString()); |
| 178 |
checkFileType = false; // reset |
| 179 |
if (f.exists()) { |
| 180 |
checkFileType = true; // this is suboptimal, but it catches corner cases |
| 181 |
// in which a regular file pre-exists |
| 182 |
} else { |
| 183 |
// we have to create that directory |
| 184 |
if (!f.mkdir()) { |
| 185 |
if (f.exists()) { |
| 186 |
// someone else created f -- need to check its type |
| 187 |
checkFileType = true; |
| 188 |
} else { |
| 189 |
// no one could create f -- complain |
| 190 |
throw new IOException(Messages.bind( |
| 191 |
Messages.output_notValid, |
| 192 |
outDir.substring(outputPath.length() + 1, |
| 193 |
outDir.length() - 1), |
| 194 |
outputPath)); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
if (checkFileType) { |
| 199 |
if (!f.isDirectory()) { |
| 200 |
throw new IOException(Messages.bind( |
| 201 |
Messages.output_isFile, f.getAbsolutePath())); |
| 202 |
} |
| 203 |
} |
| 204 |
token = tokenizer.nextToken(); |
| 147 |
} |
205 |
} |
| 148 |
token = tokenizer.nextToken(); |
206 |
// token contains the last one |
|
|
207 |
return outDir.append(token).toString(); |
| 149 |
} |
208 |
} |
| 150 |
// token contains the last one |
|
|
| 151 |
return outDir.append(token).toString(); |
| 152 |
} |
209 |
} |
| 153 |
|
210 |
|
| 154 |
/** |
211 |
/** |