001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2006 Charles O Nutter <headius@headius.com>
015: *
016: * Alternatively, the contents of this file may be used under the terms of
017: * either of the GNU General Public License Version 2 or later (the "GPL"),
018: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
019: * in which case the provisions of the GPL or the LGPL are applicable instead
020: * of those above. If you wish to allow use of your version of this file only
021: * under the terms of either the GPL or the LGPL, and not to allow others to
022: * use your version of this file under the terms of the CPL, indicate your
023: * decision by deleting the provisions above and replace them with the notice
024: * and other provisions required by the GPL or the LGPL. If you do not delete
025: * the provisions above, a recipient may use your version of this file under
026: * the terms of any one of the CPL, the GPL or the LGPL.
027: ***** END LICENSE BLOCK *****/package org.jruby.util;
028:
029: import java.io.File;
030: import java.io.FileFilter;
031: import java.io.FilenameFilter;
032: import java.io.IOException;
033: import java.net.URI;
034:
035: /**
036: * This class provides a File implementation that normalizes all path separators to forward slashes.
037: * This mimics the behavior of C Ruby, where all paths are internally made UNIX-style, even on Windows.
038: */
039: public class NormalizedFile extends File {
040: private static final long serialVersionUID = 7630618150344842227L;
041:
042: public NormalizedFile(String pathname) {
043: super (pathname);
044: }
045:
046: public NormalizedFile(URI uri) {
047: super (uri);
048: }
049:
050: public NormalizedFile(File parent, String child) {
051: super (parent, child);
052: }
053:
054: public NormalizedFile(String parent, String child) {
055: super (parent, child);
056: }
057:
058: public String getAbsolutePath() {
059: return new File(super .getPath()).getAbsolutePath().replace(
060: File.separatorChar, '/');
061: }
062:
063: public String getCanonicalPath() throws IOException {
064: return super .getCanonicalPath()
065: .replace(File.separatorChar, '/');
066: }
067:
068: public String getPath() {
069: return super .getPath().replace(File.separatorChar, '/');
070: }
071:
072: public String toString() {
073: return super .toString().replace(File.separatorChar, '/');
074: }
075:
076: public File getAbsoluteFile() {
077: return new NormalizedFile(getAbsolutePath());
078: }
079:
080: public File getCanonicalFile() throws IOException {
081: return new NormalizedFile(getCanonicalPath());
082: }
083:
084: public String getParent() {
085: return super .getParent().replace(File.separatorChar, '/');
086: }
087:
088: public File getParentFile() {
089: return new NormalizedFile(getParent());
090: }
091:
092: public static File[] listRoots() {
093: File[] roots = File.listRoots();
094: NormalizedFile[] smartRoots = new NormalizedFile[roots.length];
095: for (int i = 0; i < roots.length; i++) {
096: smartRoots[i] = new NormalizedFile(roots[i].getPath());
097: }
098:
099: return smartRoots;
100: }
101:
102: public static File createTempFile(String prefix, String suffix,
103: File directory) throws IOException {
104: File file = File.createTempFile(prefix, suffix, directory);
105: return new NormalizedFile(file.getPath());
106: }
107:
108: public static File createTempFile(String prefix, String suffix)
109: throws IOException {
110: File file = File.createTempFile(prefix, suffix);
111: return new NormalizedFile(file.getPath());
112: }
113:
114: public String[] list() {
115: return super .list();
116: }
117:
118: public String[] list(FilenameFilter filter) {
119: String[] files = super .list(filter);
120:
121: if (files == null) {
122: return null;
123: } else {
124: String[] smartFiles = new String[files.length];
125: for (int i = 0; i < files.length; i++) {
126: smartFiles[i] = files[i].replace(File.separatorChar,
127: '/');
128: }
129: return smartFiles;
130: }
131: }
132:
133: public File[] listFiles() {
134: File[] files = super .listFiles();
135:
136: if (files == null) {
137: return null;
138: } else {
139: NormalizedFile[] smartFiles = new NormalizedFile[files.length];
140: for (int i = 0; i < files.length; i++) {
141: smartFiles[i] = new NormalizedFile(files[i].getPath());
142: }
143: return smartFiles;
144: }
145: }
146:
147: public File[] listFiles(FileFilter filter) {
148: File[] files = super .listFiles(filter);
149:
150: if (files == null) {
151: return null;
152: } else {
153: NormalizedFile[] smartFiles = new NormalizedFile[files.length];
154: for (int i = 0; i < files.length; i++) {
155: smartFiles[i] = new NormalizedFile(files[i].getPath());
156: }
157: return smartFiles;
158: }
159: }
160:
161: public File[] listFiles(FilenameFilter filter) {
162: File[] files = super .listFiles(filter);
163:
164: if (files == null) {
165: return null;
166: } else {
167: NormalizedFile[] smartFiles = new NormalizedFile[files.length];
168: for (int i = 0; i < files.length; i++) {
169: smartFiles[i] = new NormalizedFile(files[i].getPath());
170: }
171: return smartFiles;
172: }
173: }
174:
175: public static String getFileProperty(String property) {
176: String value = System.getProperty(property);
177:
178: return value.replace(File.separatorChar, '/');
179: }
180: }
|