KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > RubyFileTest


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) 2004-2005 Charles O Nutter <headius@headius.com>
15  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
16  * Copyright (C) 2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
17  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
18  *
19  * Alternatively, the contents of this file may be used under the terms of
20  * either of the GNU General Public License Version 2 or later (the "GPL"),
21  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
22  * in which case the provisions of the GPL or the LGPL are applicable instead
23  * of those above. If you wish to allow use of your version of this file only
24  * under the terms of either the GPL or the LGPL, and not to allow others to
25  * use your version of this file under the terms of the CPL, indicate your
26  * decision by deleting the provisions above and replace them with the notice
27  * and other provisions required by the GPL or the LGPL. If you do not delete
28  * the provisions above, a recipient may use your version of this file under
29  * the terms of any one of the CPL, the GPL or the LGPL.
30  ***** END LICENSE BLOCK *****/

31 package org.jruby;
32
33 import org.jruby.runtime.CallbackFactory;
34 import org.jruby.runtime.builtin.IRubyObject;
35 import org.jruby.util.JRubyFile;
36
37 public class RubyFileTest {
38     public static RubyModule createFileTestModule(Ruby runtime) {
39         RubyModule fileTestModule = runtime.defineModule("FileTest");
40         CallbackFactory callbackFactory = runtime.callbackFactory(RubyFileTest.class);
41
42         fileTestModule.defineFastModuleFunction("file?", callbackFactory.getFastSingletonMethod("file_p", RubyKernel.IRUBY_OBJECT));
43         fileTestModule.defineFastModuleFunction("directory?", callbackFactory.getFastSingletonMethod("directory_p", RubyKernel.IRUBY_OBJECT));
44         fileTestModule.defineFastModuleFunction("exist?", callbackFactory.getFastSingletonMethod("exist_p", RubyKernel.IRUBY_OBJECT));
45         fileTestModule.defineFastModuleFunction("exists?", callbackFactory.getFastSingletonMethod("exist_p", RubyKernel.IRUBY_OBJECT));
46         fileTestModule.defineFastModuleFunction("readable?", callbackFactory.getFastSingletonMethod("readable_p", RubyKernel.IRUBY_OBJECT));
47         fileTestModule.defineFastModuleFunction("readable_real?", callbackFactory.getFastSingletonMethod("readable_p", RubyKernel.IRUBY_OBJECT));
48         fileTestModule.defineFastModuleFunction("size", callbackFactory.getFastSingletonMethod("size", RubyKernel.IRUBY_OBJECT));
49         fileTestModule.defineFastModuleFunction("writable?", callbackFactory.getFastSingletonMethod("writable_p", RubyKernel.IRUBY_OBJECT));
50         fileTestModule.defineFastModuleFunction("writable_real?", callbackFactory.getFastSingletonMethod("writable_p", RubyKernel.IRUBY_OBJECT));
51         fileTestModule.defineFastModuleFunction("zero?", callbackFactory.getFastSingletonMethod("zero_p", RubyKernel.IRUBY_OBJECT));
52         
53         fileTestModule.defineFastMethod("file?", callbackFactory.getFastSingletonMethod("file_p", RubyKernel.IRUBY_OBJECT));
54         fileTestModule.defineFastMethod("directory?", callbackFactory.getFastSingletonMethod("directory_p", RubyKernel.IRUBY_OBJECT));
55         fileTestModule.defineFastMethod("exist?", callbackFactory.getFastSingletonMethod("exist_p", RubyKernel.IRUBY_OBJECT));
56         fileTestModule.defineFastMethod("exists?", callbackFactory.getFastSingletonMethod("exist_p", RubyKernel.IRUBY_OBJECT));
57         fileTestModule.defineFastMethod("readable?", callbackFactory.getFastSingletonMethod("readable_p", RubyKernel.IRUBY_OBJECT));
58         fileTestModule.defineFastMethod("readable_real?", callbackFactory.getFastSingletonMethod("readable_p", RubyKernel.IRUBY_OBJECT));
59         fileTestModule.defineFastMethod("size", callbackFactory.getFastSingletonMethod("size", RubyKernel.IRUBY_OBJECT));
60         fileTestModule.defineFastMethod("writable?", callbackFactory.getFastSingletonMethod("writable_p", RubyKernel.IRUBY_OBJECT));
61         fileTestModule.defineFastMethod("writable_real?", callbackFactory.getFastSingletonMethod("writable_p", RubyKernel.IRUBY_OBJECT));
62         fileTestModule.defineFastMethod("zero?", callbackFactory.getFastSingletonMethod("zero_p", RubyKernel.IRUBY_OBJECT));
63         
64         return fileTestModule;
65     }
66     
67     public static RubyBoolean directory_p(IRubyObject recv, IRubyObject filename) {
68         return recv.getRuntime().newBoolean(newFile(filename).isDirectory());
69     }
70     
71     public static IRubyObject exist_p(IRubyObject recv, IRubyObject filename) {
72         return recv.getRuntime().newBoolean(newFile(filename).exists());
73     }
74
75     // We do both readable and readable_real through the same method because
76
// in our java process effective and real userid will always be the same.
77
public static RubyBoolean readable_p(IRubyObject recv, IRubyObject filename) {
78         return filename.getRuntime().newBoolean(newFile(filename).canRead());
79     }
80     
81     public static IRubyObject size(IRubyObject recv, IRubyObject filename) {
82         JRubyFile file = newFile(filename);
83         
84         if (!file.exists()) {
85             throw recv.getRuntime().newErrnoENOENTError("No such file: " + filename);
86         }
87         return filename.getRuntime().newFixnum(file.length());
88     }
89     
90     // We do both writable and writable_real through the same method because
91
// in our java process effective and real userid will always be the same.
92
public static RubyBoolean writable_p(IRubyObject recv, IRubyObject filename) {
93         return filename.getRuntime().newBoolean(newFile(filename).canWrite());
94     }
95     
96     public static RubyBoolean zero_p(IRubyObject recv, IRubyObject filename) {
97         JRubyFile file = newFile(filename);
98         
99         return filename.getRuntime().newBoolean(file.exists() && file.length() == 0L);
100     }
101
102     public static RubyBoolean file_p(IRubyObject recv, IRubyObject filename) {
103         JRubyFile file = newFile(filename);
104         
105         return filename.getRuntime().newBoolean(file.isFile());
106     }
107     
108     private static JRubyFile newFile(IRubyObject path) {
109         return JRubyFile.create(path.getRuntime().getCurrentDirectory(), path.convertToString().toString());
110     }
111 }
112
Popular Tags