KickJava   Java API By Example, From Geeks To Geeks.

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


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 Charles O Nutter <headius@headius.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 package org.jruby.util;
29
30 import java.io.File JavaDoc;
31 import java.io.FileFilter JavaDoc;
32 import java.io.FilenameFilter JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.net.URI JavaDoc;
35
36 /**
37  * This class provides a File implementation that normalizes all path separators to forward slashes.
38  * This mimics the behavior of C Ruby, where all paths are internally made UNIX-style, even on Windows.
39  */

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