001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.tools.file;
031:
032: import java.io.*;
033: import java.util.*;
034:
035: import de.intarsys.tools.deprecated.string.StringTools;
036:
037: /**
038: * A replacement/addition for file objects.
039: *
040: */
041: public class Filename implements Serializable {
042: /**
043: *
044: */
045: private Filename parent;
046:
047: private String cacheFilenameAbsolute;
048:
049: private String cacheFilenameExpanded;
050:
051: private String filename;
052:
053: private boolean caching = false;
054:
055: /**
056: * Filename constructor comment.
057: *
058: * @param filename
059: * docme
060: */
061: public Filename(String filename) {
062: this (filename, false);
063: }
064:
065: /**
066: * Filename constructor comment.
067: *
068: * @param filename
069: * docme
070: * @param parent
071: * docme
072: */
073: public Filename(String filename, Filename parent) {
074: super ();
075: setFilename(filename);
076: setParent(parent);
077: }
078:
079: /**
080: * Filename constructor comment.
081: *
082: * @param filename
083: * docme
084: * @param parent
085: * docme
086: * @param caching
087: * docme
088: */
089: public Filename(String filename, Filename parent, boolean caching) {
090: super ();
091: setFilename(filename);
092: setParent(parent);
093: setCaching(caching);
094: }
095:
096: /**
097: * Filename constructor comment.
098: *
099: * @param filename
100: * docme
101: * @param caching
102: * docme
103: */
104: public Filename(String filename, boolean caching) {
105: super ();
106: setFilename(filename);
107: setCaching(caching);
108: }
109:
110: /**
111: * Insert the method's description here. Creation date: (23.05.2001
112: * 09:48:22)
113: *
114: * @param newCaching
115: * boolean
116: */
117: public void setCaching(boolean newCaching) {
118: caching = newCaching;
119: refresh();
120: }
121:
122: /**
123: * Insert the method's description here. Creation date: (23.05.2001
124: * 09:48:22)
125: *
126: * @return boolean
127: */
128: public boolean isCaching() {
129: return caching;
130: }
131:
132: /**
133: * Insert the method's description here. Creation date: (23.05.2001
134: * 09:21:22)
135: *
136: * @param path
137: * docme
138: *
139: * @return java.lang.String
140: */
141: public Filename getDescendent(String path) {
142: if (isNull()) {
143: return new Filename(null, this , isCaching());
144: }
145: if (path == null) {
146: return new Filename("", this , isCaching());
147: }
148: return new Filename(path, this , isCaching());
149: }
150:
151: /**
152: * docme
153: *
154: * @return docme
155: */
156: public boolean isExpandable() {
157: return (cacheFilenameExpanded == null)
158: || ((parent != null) && (parent.isExpandable()));
159: }
160:
161: /**
162: */
163: public Filename getExpandedClone(Map variables) {
164: Filename clone = new Filename(getFilename());
165: clone.cacheFilenameExpanded = getFilename(variables);
166: clone.cacheFilenameAbsolute = null;
167: clone.setCaching(isCaching());
168: if (getParent() != null) {
169: clone.setParent(getParent().getExpandedClone(variables));
170: }
171: return clone;
172: }
173:
174: /**
175: * Insert the method's description here. Creation date: (23.05.2001
176: * 09:21:22)
177: *
178: * @return java.lang.String
179: */
180: public File getFile() {
181: return new File(getFilenameAbsolute());
182: }
183:
184: /**
185: * Insert the method's description here. Creation date: (23.05.2001
186: * 09:21:22)
187: *
188: * @param newFilename
189: * java.lang.String
190: */
191: public void setFilename(java.lang.String newFilename) {
192: filename = newFilename;
193: refresh();
194: }
195:
196: /**
197: * Insert the method's description here. Creation date: (23.05.2001
198: * 09:21:22)
199: *
200: * @return java.lang.String
201: */
202: public java.lang.String getFilename() {
203: return filename;
204: }
205:
206: /**
207: * Insert the method's description here. Creation date: (23.05.2001
208: * 09:21:22)
209: *
210: * @param variables
211: * docme
212: *
213: * @return java.lang.String
214: */
215: public java.lang.String getFilename(Map variables) {
216: if (isNull()) {
217: return "";
218: }
219:
220: String expanded = cacheFilenameExpanded;
221: if ((expanded == null) && (getFilename() != null)) {
222: expanded = StringTools.replaceVariables(getFilename(),
223: variables);
224: }
225: return expanded;
226: }
227:
228: /**
229: * Insert the method's description here. Creation date: (23.05.2001
230: * 09:21:22)
231: *
232: * @return java.lang.String
233: */
234: public java.lang.String getFilenameAbsolute() {
235: if (isNull()) {
236: return "";
237: }
238: if (cacheFilenameAbsolute != null) {
239: return cacheFilenameAbsolute;
240: }
241:
242: String result = null;
243: if (parent == null) {
244: result = (new File(getFilename())).getAbsolutePath();
245: } else {
246: result = FileTools.getPathRelative(parent
247: .getFilenameAbsolute(), getFilename());
248: }
249: if (isCaching()) {
250: cacheFilenameAbsolute = result;
251: }
252: return result;
253: }
254:
255: /**
256: * Insert the method's description here. Creation date: (23.05.2001
257: * 09:21:22)
258: *
259: * @param variables
260: * docme
261: *
262: * @return java.lang.String
263: */
264: public java.lang.String getFilenameAbsolute(Map variables) {
265: // todo MAYBE cache expanded names, too. (very sensible)
266: if (isNull()) {
267: return "";
268: }
269:
270: String expanded = cacheFilenameExpanded;
271: if ((expanded == null) && (getFilename() != null)) {
272: expanded = StringTools.replaceVariables(getFilename(),
273: variables);
274: }
275:
276: String result = null;
277: if (parent == null) {
278: result = (new File(expanded)).getAbsolutePath();
279: } else {
280: result = FileTools.getPathRelative(parent
281: .getFilenameAbsolute(variables), expanded);
282: }
283: return result;
284: }
285:
286: /**
287: * docme
288: *
289: * @return docme
290: */
291: public boolean isNull() {
292: return getFilename() == null;
293: }
294:
295: /**
296: * Insert the method's description here. Creation date: (23.05.2001
297: * 09:21:22)
298: *
299: * @param newParent
300: * de.intarsys.tools.file.Filename
301: */
302: public void setParent(Filename newParent) {
303: parent = newParent;
304: refresh();
305: }
306:
307: /**
308: * Insert the method's description here. Creation date: (23.05.2001
309: * 09:21:22)
310: *
311: * @return de.intarsys.tools.file.Filename
312: */
313: public Filename getParent() {
314: return parent;
315: }
316:
317: /**
318: * Insert the method's description here. Creation date: (23.05.2001
319: * 09:21:22)
320: *
321: * @return java.lang.String
322: */
323: public String getTrimFilenameAbsolute() {
324: if (isNull()) {
325: return "";
326: }
327: return FileTools.trimToFileConform(getFilenameAbsolute());
328: }
329:
330: /**
331: */
332: public String getTrimFilenameAbsolute(Map variables) {
333: if (isNull()) {
334: return "";
335: }
336: return FileTools
337: .trimToFileConform(getFilenameAbsolute(variables));
338: }
339:
340: /**
341: * docme
342: *
343: * @param defaultDirectoryName
344: * docme
345: * @param create
346: * docme
347: * @param canRead
348: * docme
349: * @param canWrite
350: * docme
351: *
352: * @throws IOException
353: * docme
354: */
355: public void checkDirectory(String defaultDirectoryName,
356: boolean create, boolean canRead, boolean canWrite)
357: throws IOException {
358: if (isNull()) {
359: return;
360: }
361: try {
362: checkDirectory(create, canRead, canWrite);
363: } catch (IOException e) {
364: setFilename(defaultDirectoryName);
365: checkDirectory(create, canRead, canWrite);
366: }
367: }
368:
369: /**
370: * docme
371: *
372: * @param create
373: * docme
374: * @param canRead
375: * docme
376: * @param canWrite
377: * docme
378: *
379: * @throws IOException
380: * docme
381: */
382: public void checkDirectory(boolean create, boolean canRead,
383: boolean canWrite) throws IOException {
384: if (isNull()) {
385: return;
386: }
387:
388: String directoryName = getFilenameAbsolute();
389: File f = new File(directoryName);
390: if (!f.exists()) {
391: if (create) {
392: if (!f.mkdirs()) {
393: throw new IOException("Directory " + directoryName
394: + " can't be created");
395: }
396: if (!f.exists()) {
397: throw new IOException("Directory " + directoryName
398: + " can't be created");
399: }
400: } else {
401: throw new IOException("Directory " + directoryName
402: + " not existent");
403: }
404: }
405: if (!f.isDirectory()) {
406: throw new IOException(directoryName + " is not a directory");
407: }
408: if (canRead && !f.canRead()) {
409: throw new IOException("No read access for directory "
410: + directoryName);
411: }
412: if (canWrite && !f.canWrite()) {
413: throw new IOException("No write access for directory "
414: + directoryName);
415: }
416: }
417:
418: /**
419: * docme
420: */
421: public void refresh() {
422: if (isNull()) {
423: return;
424: }
425: cacheFilenameAbsolute = null;
426: cacheFilenameExpanded = null;
427: if (filename.indexOf(StringTools.DELIMITER) == -1) {
428: // this is constant, dont need to expand every time
429: cacheFilenameExpanded = filename;
430: }
431: }
432:
433: /**
434: * docme
435: *
436: * @return docme
437: */
438: public String toString() {
439: return getFilenameAbsolute();
440: }
441: }
|