KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > util > JRubyFile


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2006 Ola Bini <ola@ologix.com>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 /**
29  * $Id$
30  */

31 package org.jruby.util;
32
33 import java.io.File JavaDoc;
34 import java.io.FileFilter JavaDoc;
35 import java.io.FilenameFilter JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 /**
39  * <p>This file acts as an alternative to NormalizedFile, due to the problems with current working
40  * directory.</p>
41  *
42  */

43 public class JRubyFile extends File JavaDoc {
44     private static final long serialVersionUID = 435364547567567L;
45
46     public static JRubyFile create(String JavaDoc cwd, String JavaDoc pathname) {
47         File JavaDoc internal = new File JavaDoc(pathname);
48         if(!internal.isAbsolute()) {
49             internal = new File JavaDoc(cwd,pathname);
50             if(!internal.isAbsolute()) {
51                 throw new IllegalArgumentException JavaDoc("Neither current working directory ("+cwd+") nor pathname ("+pathname+") led to an absolute path");
52             }
53         }
54         return new JRubyFile(internal);
55     }
56
57     public static String JavaDoc getFileProperty(String JavaDoc property) {
58         String JavaDoc value = System.getProperty(property);
59         
60         return value.replace(File.separatorChar, '/');
61     }
62
63     private JRubyFile(File JavaDoc file) {
64         this(file.getAbsolutePath());
65     }
66
67     private JRubyFile(String JavaDoc filename) {
68         super(filename);
69     }
70
71     public String JavaDoc getAbsolutePath() {
72         return super.getAbsolutePath().replace(File.separatorChar, '/');
73     }
74
75     public String JavaDoc getCanonicalPath() throws IOException JavaDoc {
76         return super.getCanonicalPath().replace(File.separatorChar, '/');
77     }
78
79     public String JavaDoc getPath() {
80         return super.getPath().replace(File.separatorChar, '/');
81     }
82
83     public String JavaDoc toString() {
84         return super.toString().replace(File.separatorChar, '/');
85     }
86
87     public File JavaDoc getAbsoluteFile() {
88         return new JRubyFile(getAbsolutePath());
89     }
90
91     public File JavaDoc getCanonicalFile() throws IOException JavaDoc {
92         return new JRubyFile(getCanonicalPath());
93     }
94
95     public String JavaDoc getParent() {
96         return super.getParent().replace(File.separatorChar, '/');
97     }
98
99     public File JavaDoc getParentFile() {
100         return new JRubyFile(getParent());
101     }
102     
103     public static File JavaDoc[] listRoots() {
104         File JavaDoc[] roots = File.listRoots();
105         JRubyFile[] smartRoots = new JRubyFile[roots.length];
106         for(int i = 0, j = roots.length; i < j; i++) {
107             smartRoots[i] = new JRubyFile(roots[i].getPath());
108         }
109         return smartRoots;
110     }
111     
112     public static File JavaDoc createTempFile(String JavaDoc prefix, String JavaDoc suffix, File JavaDoc directory) throws IOException JavaDoc {
113         return new JRubyFile(File.createTempFile(prefix, suffix,directory));
114     }
115     
116     public static File JavaDoc createTempFile(String JavaDoc prefix, String JavaDoc suffix) throws IOException JavaDoc {
117         return new JRubyFile(File.createTempFile(prefix, suffix));
118     }
119
120     public String JavaDoc[] list(FilenameFilter JavaDoc filter) {
121         String JavaDoc[] files = super.list(filter);
122         if (files == null) {
123             return null;
124         }
125         
126         String JavaDoc[] smartFiles = new String JavaDoc[files.length];
127         for (int i = 0; i < files.length; i++) {
128             smartFiles[i] = files[i].replace(File.separatorChar, '/');
129         }
130         return smartFiles;
131     }
132
133     public File JavaDoc[] listFiles() {
134         File JavaDoc[] files = super.listFiles();
135         if (files == null) {
136             return null;
137         }
138         
139         JRubyFile[] smartFiles = new JRubyFile[files.length];
140         for (int i = 0, j = files.length; i < j; i++) {
141             smartFiles[i] = create(super.getAbsolutePath(),files[i].getPath());
142         }
143         return smartFiles;
144     }
145
146     public File JavaDoc[] listFiles(final FileFilter JavaDoc filter) {
147         final File JavaDoc[] files = super.listFiles(filter);
148         if (files == null) {
149             return null;
150         }
151         
152         JRubyFile[] smartFiles = new JRubyFile[files.length];
153         for (int i = 0,j = files.length; i < j; i++) {
154             smartFiles[i] = create(super.getAbsolutePath(),files[i].getPath());
155         }
156         return smartFiles;
157     }
158
159     public File JavaDoc[] listFiles(final FilenameFilter JavaDoc filter) {
160         final File JavaDoc[] files = super.listFiles(filter);
161         if (files == null) {
162             return null;
163         }
164         
165         JRubyFile[] smartFiles = new JRubyFile[files.length];
166         for (int i = 0,j = files.length; i < j; i++) {
167             smartFiles[i] = create(super.getAbsolutePath(),files[i].getPath());
168         }
169         return smartFiles;
170     }
171 }
172
Popular Tags