001    package railo.commons.io.res.util;
002    
003    import java.io.File;
004    import java.io.FileFilter;
005    import java.io.FilenameFilter;
006    import java.io.IOException;
007    import java.io.InputStream;
008    import java.io.OutputStream;
009    import java.net.MalformedURLException;
010    import java.net.URI;
011    import java.net.URL;
012    
013    import railo.commons.io.res.Resource;
014    import railo.commons.io.res.ResourceProvider;
015    import railo.commons.io.res.filter.ResourceFilter;
016    import railo.commons.io.res.filter.ResourceNameFilter;
017    
018    public final class FileWrapper extends File implements Resource {
019    
020            private final Resource res;
021    
022            /**
023             * Constructor of the class
024             * @param res
025             */
026            private FileWrapper(Resource res) {
027                    super(res.getPath());
028                    this.res=res;
029            }
030            
031            /**
032             *
033             * @see java.io.File#canRead()
034             */
035            public boolean canRead() {
036                    return res.canRead();
037            }
038    
039    
040            /**
041             *
042             * @see java.io.File#canWrite()
043             */
044            public boolean canWrite() {
045                    return res.canWrite();
046            }
047    
048    
049            /**
050             *
051             * @see java.io.File#compareTo(java.io.File)
052             */
053            public int compareTo(File pathname) {
054                    if(res instanceof File) ((File)res).compareTo(pathname);
055                    return res.getPath().compareTo(pathname.getPath());
056            }
057    
058    
059    
060            /**
061             *
062             * @see java.io.File#createNewFile()
063             */
064            public boolean createNewFile() {
065                    return res.createNewFile();
066            }
067    
068    
069            /**
070             *
071             * @see java.io.File#delete()
072             */
073            public boolean delete() {
074                    return res.delete();
075            }
076    
077    
078            /**
079             *
080             * @see java.io.File#deleteOnExit()
081             */
082            public void deleteOnExit() {
083                    if(res instanceof File) ((File)res).deleteOnExit();
084            }
085    
086    
087            /**
088             *
089             * @see java.io.File#equals(java.lang.Object)
090             */
091            public boolean equals(Object obj) {
092                    return res.equals(obj);
093            }
094    
095    
096            /**
097             *
098             * @see java.io.File#exists()
099             */
100            public boolean exists() {
101                    return res.exists();
102            }
103    
104    
105            /**
106             *
107             * @see java.io.File#getAbsoluteFile()
108             */
109            public File getAbsoluteFile() {
110                    if(res.isAbsolute()) return this;
111                    return new FileWrapper(res.getAbsoluteResource());
112            }
113    
114    
115            /**
116             *
117             * @see java.io.File#getAbsolutePath()
118             */
119            public String getAbsolutePath() {
120                    return res.getAbsolutePath();
121            }
122    
123    
124            /**
125             *
126             * @see java.io.File#getCanonicalFile()
127             */
128            public File getCanonicalFile() throws IOException {
129                    return new FileWrapper(res.getCanonicalResource());
130            }
131    
132    
133            /**
134             *
135             * @see java.io.File#getCanonicalPath()
136             */
137            public String getCanonicalPath() throws IOException {
138                    return res.getCanonicalPath();
139            }
140    
141    
142            /**
143             *
144             * @see java.io.File#getName()
145             */
146            public String getName() {
147                    return res.getName();
148            }
149    
150    
151            /**
152             *
153             * @see java.io.File#getParent()
154             */
155            public String getParent() {
156                    return res.getParent();
157            }
158    
159    
160            /**
161             *
162             * @see java.io.File#getParentFile()
163             */
164            public File getParentFile() {
165                    return new FileWrapper(this.getParentResource());
166            }
167    
168    
169            /**
170             *
171             * @see java.io.File#getPath()
172             */
173            public String getPath() {
174                    return res.getPath();
175            }
176    
177    
178            /**
179             *
180             * @see java.io.File#hashCode()
181             */
182            public int hashCode() {
183                    return res.hashCode();
184            }
185    
186    
187            /**
188             *
189             * @see java.io.File#isAbsolute()
190             */
191            public boolean isAbsolute() {
192                    return res.isAbsolute();
193            }
194    
195    
196            /**
197             *
198             * @see java.io.File#isDirectory()
199             */
200            public boolean isDirectory() {
201                    return res.isDirectory();
202            }
203    
204    
205            /**
206             *
207             * @see java.io.File#isFile()
208             */
209            public boolean isFile() {
210                    return res.isFile();
211            }
212    
213    
214            /**
215             *
216             * @see java.io.File#isHidden()
217             */
218            public boolean isHidden() {
219                    return res.isHidden();
220            }
221    
222    
223            /**
224             *
225             * @see java.io.File#lastModified()
226             */
227            public long lastModified() {
228                    return res.lastModified();
229            }
230    
231    
232            /**
233             *
234             * @see java.io.File#length()
235             */
236            public long length() {
237                    return res.length();
238            }
239    
240    
241            /**
242             *
243             * @see java.io.File#list()
244             */
245            public String[] list() {
246                    return res.list();
247            }
248    
249    
250            /**
251             *
252             * @see java.io.File#list(java.io.FilenameFilter)
253             */
254            public String[] list(FilenameFilter filter) {
255                    if(res instanceof File) ((File)res).list(filter);
256                    return list((ResourceNameFilter)new FileNameFilterWrapper(filter));
257            }
258    
259    
260            /**
261             *
262             * @see java.io.File#listFiles()
263             */
264            public File[] listFiles() {
265                    //if(res instanceof File) return ((File)res).listFiles();
266                    return toFiles(listResources());
267            }
268            
269            private File[] toFiles(Resource[] resources) {
270                    File[] files = new File[resources.length];
271                    for(int i=0;i<resources.length;i++) {
272                            files[i]=new FileWrapper(resources[i]);
273                    }
274                    return files;
275            }
276    
277    
278            /**
279             *
280             * @see java.io.File#listFiles(java.io.FileFilter)
281             */
282            public File[] listFiles(FileFilter filter) {
283                    //if(res instanceof File) return ((File)res).listFiles(filter);
284                    return toFiles(listResources(new FileFilterWrapper(filter)));
285            }
286    
287    
288            /**
289             *
290             * @see java.io.File#listFiles(java.io.FilenameFilter)
291             */
292            public File[] listFiles(FilenameFilter filter) {
293                    //if(res instanceof File) return ((File)res).listFiles(filter);
294                    return toFiles(listResources(new FileNameFilterWrapper(filter)));
295            }
296    
297    
298            /**
299             *
300             * @see java.io.File#mkdir()
301             */
302            public boolean mkdir() {
303                    return res.mkdir();
304            }
305    
306    
307            /**
308             *
309             * @see java.io.File#mkdirs()
310             */
311            public boolean mkdirs() {
312                    return res.mkdirs();
313            }
314    
315    
316            /**
317             *
318             * @throws IOException 
319             * @see java.io.File#renameTo(java.io.File)
320             */
321            public boolean renameTo(File dest) {
322                    try {
323                            if(res instanceof File) return ((File)res).renameTo(dest);
324                            if(dest instanceof Resource) return res.renameTo((Resource)dest);
325                            ResourceUtil.moveTo(this, ResourceUtil.toResource(dest));
326                            return true;
327                    }
328                    catch(IOException ioe) {
329                            return false;
330                    }
331            }
332    
333    
334            /**
335             *
336             * @see java.io.File#setLastModified(long)
337             */
338            public boolean setLastModified(long time) {
339                    return res.setLastModified(time);
340            }
341    
342    
343            /**
344             *
345             * @see java.io.File#setReadOnly()
346             */
347            public boolean setReadOnly() {
348                    return res.setReadOnly();
349            }
350    
351    
352            /**
353             *
354             * @see java.io.File#toString()
355             */
356            public String toString() {
357                    return res.toString();
358            }
359    
360    
361            /**
362             *
363             * @see java.io.File#toURI()
364             */
365            public URI toURI() {
366                    if(res instanceof File) return ((File)res).toURI();
367                    return null;
368            }
369    
370    
371            /**
372             *
373             * @see java.io.File#toURL()
374             */
375            public URL toURL() throws MalformedURLException {
376                    if(res instanceof File) return ((File)res).toURL();
377                    return null;
378            }
379    
380            /**
381             * @see railo.commons.io.res.Resource#createDirectory(boolean)
382             */
383            public void createDirectory(boolean createParentWhenNotExists) throws IOException {
384                    res.createDirectory(createParentWhenNotExists);
385            }
386    
387            /**
388             * @see railo.commons.io.res.Resource#createFile(boolean)
389             */
390            public void createFile(boolean createParentWhenNotExists) throws IOException {
391                    res.createFile(createParentWhenNotExists);
392            }
393    
394            /**
395             * @see railo.commons.io.res.Resource#getAbsoluteResource()
396             */
397            public Resource getAbsoluteResource() {
398                    return res.getAbsoluteResource();
399            }
400    
401            /**
402             * @see railo.commons.io.res.Resource#getCanonicalResource()
403             */
404            public Resource getCanonicalResource() throws IOException {
405                    return res.getCanonicalResource();
406            }
407    
408            /**
409             * @see railo.commons.io.res.Resource#getInputStream()
410             */
411            public InputStream getInputStream() throws IOException {
412                    return res.getInputStream();
413            }
414    
415            /**
416             * @see railo.commons.io.res.Resource#getMode()
417             */
418            public int getMode() {
419                    return res.getMode();
420            }
421    
422            /**
423             * @see railo.commons.io.res.Resource#getOutputStream()
424             */
425            public OutputStream getOutputStream() throws IOException {
426                    return res.getOutputStream();
427            }
428    
429            /**
430             * @see railo.commons.io.res.Resource#getOutputStream(boolean)
431             */
432            public OutputStream getOutputStream(boolean append) throws IOException {
433                    return res.getOutputStream(append);
434            }
435    
436            /**
437             * @see railo.commons.io.res.Resource#getParentResource()
438             */
439            public Resource getParentResource() {
440                    return res.getParentResource();
441            }
442    
443            /**
444             * @see railo.commons.io.res.Resource#getReal(java.lang.String)
445             */
446            public String getReal(String realpath) {
447                    return res.getReal(realpath);
448            }
449    
450            /**
451             * @see railo.commons.io.res.Resource#getRealResource(java.lang.String)
452             */
453            public Resource getRealResource(String realpath) {
454                    return res.getRealResource(realpath);
455            }
456    
457            /**
458             * @see railo.commons.io.res.Resource#getResourceProvider()
459             */
460            public ResourceProvider getResourceProvider() {
461                    return res.getResourceProvider();
462            }
463    
464            /**
465             * @see railo.commons.io.res.Resource#isArchive()
466             */
467            public boolean isArchive() {
468                    return res.isArchive();
469            }
470    
471            /**
472             * @see railo.commons.io.res.Resource#isReadable()
473             */
474            public boolean isReadable() {
475                    return res.isReadable();
476            }
477    
478            /**
479             * @see railo.commons.io.res.Resource#isSystem()
480             */
481            public boolean isSystem() {
482                    return res.isSystem();
483            }
484    
485            /**
486             * @see railo.commons.io.res.Resource#isWriteable()
487             */
488            public boolean isWriteable() {
489                    return res.isWriteable();
490            }
491    
492            /**
493             * @see railo.commons.io.res.Resource#list(railo.commons.io.res.filter.ResourceNameFilter)
494             */
495            public String[] list(ResourceNameFilter filter) {
496                    return res.list(filter);
497            }
498    
499            /**
500             * @see railo.commons.io.res.Resource#list(railo.commons.io.res.filter.ResourceFilter)
501             */
502            public String[] list(ResourceFilter filter) {
503                    return res.list(filter);
504            }
505    
506            /**
507             * @see railo.commons.io.res.Resource#listResources()
508             */
509            public Resource[] listResources() {
510                    return res.listResources();
511            }
512    
513            /**
514             * @see railo.commons.io.res.Resource#listResources(railo.commons.io.res.filter.ResourceFilter)
515             */
516            public Resource[] listResources(ResourceFilter filter) {
517                    return res.listResources(filter);
518            }
519    
520            /**
521             * @see railo.commons.io.res.Resource#listResources(railo.commons.io.res.filter.ResourceNameFilter)
522             */
523            public Resource[] listResources(ResourceNameFilter filter) {
524                    return res.listResources(filter);
525            }
526    
527            /**
528             * @see railo.commons.io.res.Resource#moveTo(railo.commons.io.res.Resource)
529             */
530            public void moveTo(Resource dest) throws IOException {
531                    res.moveTo(dest);
532            }
533    
534            /**
535             * @see railo.commons.io.res.Resource#remove(boolean)
536             */
537            public void remove(boolean force) throws IOException {
538                    res.remove(force);
539            }
540    
541            /**
542             * @see railo.commons.io.res.Resource#renameTo(railo.commons.io.res.Resource)
543             */
544            public boolean renameTo(Resource dest) {
545                    return res.renameTo(dest);
546            }
547    
548            /**
549             *
550             * @see railo.commons.io.res.Resource#setMode(int)
551             */
552            public void setMode(int mode) throws IOException {
553                    res.setMode(mode);
554            }
555    
556    
557            /**
558             * @param res
559             * @return
560             */
561            public static File toFile(Resource res) {
562                    if(res instanceof File) return (File)res;
563                    return new FileWrapper(res);
564            }
565    
566    
567            /**
568             *
569             * @see railo.commons.io.res.Resource#setArchive(boolean)
570             */
571            public void setArchive(boolean value) throws IOException {
572                    res.setArchive(value);
573            }
574    
575            /**
576             *
577             * @see railo.commons.io.res.Resource#setHidden(boolean)
578             */
579            public void setHidden(boolean value) throws IOException {
580                    res.setHidden(value);
581            }
582    
583            /**
584             *
585             * @see railo.commons.io.res.Resource#setSystem(boolean)
586             */
587            public void setSystem(boolean value) throws IOException {
588                    res.setSystem(value);
589            }
590    
591    
592            /**
593             * @see railo.commons.io.res.Resource#getAttribute(short)
594             */
595            public boolean getAttribute(short attribute) {
596                    return res.getAttribute(attribute);
597            }
598    
599    
600            /**
601             * @see railo.commons.io.res.Resource#setAttribute(short, boolean)
602             */
603            public void setAttribute(short attribute, boolean value) throws IOException {
604                    res.setAttribute(attribute, value);
605            }
606    
607    
608            /**
609             *
610             * @see railo.commons.io.res.Resource#setReadable(boolean)
611             */
612            public boolean setReadable(boolean value) {
613                    return res.setReadable(value);
614            }
615    
616    
617            /**
618             *
619             * @see railo.commons.io.res.Resource#setWritable(boolean)
620             */
621            public boolean setWritable(boolean value) {
622                    return res.setWritable(value);
623            }
624    
625            public void copyFrom(Resource res, boolean append) throws IOException {
626                    res.copyFrom(res, append);
627            }
628    
629            public void copyTo(Resource res, boolean append) throws IOException {
630                    res.copyTo(res, append);
631            }
632    
633    }