View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.struts.flow.sugar;
17  
18  import org.mozilla.javascript.*;
19  
20  import java.io.*;
21  import java.util.*;
22  
23  /***
24   * Adds various functions to java.io.File
25   * @targetClass java.io.File
26   */
27  public class FileExtensions {
28  
29  
30       /***
31       *  Appends text to the file.
32       *
33       *  @funcParams String text
34       *  @funcReturn java.io.File
35       *  @example file.append("added text")
36       */
37      public static ExtensionFunction append(final File file) {
38          return new ExtensionFunction() {    
39              public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) 
40                      throws IOException {
41                  String text = args[0].toString();
42                  BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
43                  writer.write(text);
44                  writer.close();
45                  return file;
46              }
47          };
48      }
49      
50       /***
51       *  Gets the contents of the file as a String.
52       *
53       *  @funcParams 
54       *  @funcReturn String
55       *  @example text = file.getText()
56       */
57      public static ExtensionFunction getText(final File file) {
58          return new ExtensionFunction() {    
59              public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) 
60                      throws IOException {
61                  
62                  BufferedReader reader = new BufferedReader(new FileReader(file));
63                  StringBuffer answer = new StringBuffer();
64                  // reading the content of the file within a char buffer allow to keep the correct line endings
65                  char[] charBuffer = new char[4096];
66                  int nbCharRead = 0;
67                  while ((nbCharRead = reader.read(charBuffer)) != -1) {
68                      // appends buffer
69                      answer.append(charBuffer, 0, nbCharRead);
70                  }
71                  reader.close();
72                  return answer.toString();
73              }
74          };
75      }
76      
77       /***
78       *  Passes each line to the provided function.  The file is opened, and 
79       *  interpreted as a text file using the default encoding.  Each line
80       *  is read and passed to the provided function.
81       *
82       *  @funcParams Function func
83       *  @funcReturn void
84       *  @example file.eachLine(function(line) { print(line) })
85       */
86      public static ExtensionFunction eachLine(final File file) {
87          return new ExtensionFunction() {    
88              public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) 
89                      throws IOException {
90                  
91                  BufferedReader reader = new BufferedReader(new FileReader(file));
92                  Function func = (Function)args[0];
93                  Object[] params = new Object[1];
94                  String line = null;
95                  while ((line = reader.readLine()) != null) {
96                      params[0] = line;
97                      func.call(cx, scope, thisObj, params);
98                  }
99                  reader.close();
100                 return null;
101             }
102         };
103     }
104     
105      /***
106      *  Collects the contents of the file as an array of lines.  The file is opened, and 
107      *  interpreted as a text file using the default encoding.  
108      *
109      *  @funcParams 
110      *  @funcReturn String[]
111      *  @example linesArray = file.getLines()
112      */
113     public static ExtensionFunction getLines(final File file) {
114         return new ExtensionFunction() {    
115             public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) 
116                     throws IOException {
117                 
118                 BufferedReader reader = new BufferedReader(new FileReader(file));
119                 ArrayList list = new ArrayList();
120                 String line = null;
121                 
122                 while ((line = reader.readLine()) != null) {
123                     list.add(line);
124                 }
125                 reader.close();
126                 
127                 Object[] lines = new Object[list.size()];
128                 for (int x=0; x<lines.length; x++) {
129                     lines[x] = cx.javaToJS(list.get(x), scope);
130                 }
131                 return cx.newArray(scope, lines);
132             }
133         };
134     }
135     
136     /***
137      *  Removes a file.  Used to get around the problem of the reserved word 'delete'.
138      *
139      *  @funcParams 
140      *  @funcReturn boolean
141      *  @example isRemoved = file.remove()
142      */
143     public static ExtensionFunction remove(final File file) {
144         return new ExtensionFunction() {    
145             public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) 
146                     throws IOException {
147                 
148                 return new Boolean(file.delete());
149             }
150         };
151     }
152     
153 }