KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > javancss > FileUtil


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the reusable ccl java library
15  * (http://www.kclee.com/clemens/java/ccl/).
16  *
17  * The Initial Developer of the Original Code is
18  * Chr. Clemens Lee.
19  * Portions created by Chr. Clemens Lee are Copyright (C) 2002
20  * Chr. Clemens Lee. All Rights Reserved.
21  *
22  * Contributor(s): Chr. Clemens Lee <clemens@kclee.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */

37
38 package net.sourceforge.cobertura.javancss;
39
40 import java.io.BufferedReader JavaDoc;
41 import java.io.File JavaDoc;
42 import java.io.FileNotFoundException JavaDoc;
43 import java.io.FileReader JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 /**
47  * Utility class for file operations.<p>
48  *
49  * Simple but most commonly used methods of this class are:<br>
50  * - {@link #readFile(java.lang.String) readFile}<br>
51  * - {@link #concatPath(java.lang.String, java.lang.String) concatPath}<br>
52  *
53  * Other less frequently used but still handy methods are:<br>
54  * - {@link #normalizeFileName(java.lang.String) normalizeFileName} to take the current user directory into account via the 'user.dir' system property<br>
55  *
56  * @version $Id: FileUtil.java 384 2006-03-17 20:10:49Z thekingant $
57  * @author <a HREF="http://www.kclee.com/clemens/">
58  * Chr. Clemens Lee</a>
59  * &lt;<a HREF="mailto:clemens@kclee.com">
60  * clemens@kclee.com
61  * </a>>
62  */

63 public class FileUtil
64 {
65
66     /**
67      * Utility class which should never instanciate itself.
68      */

69     private FileUtil()
70     {
71         super();
72     }
73
74     /**
75      * Concatenates a file path with the file name. If
76      * necessary it adds a File.separator between the path
77      * and file name. For example "/home" or "/home/" and "clemens" both
78      * become "/home/clemens".<p>
79      *
80      * This method is inspired from the FrIJDE project out
81      * of the gCollins.File.FileTools class.<p>
82      *
83      * FrIJDE Homepage:
84      * http://amber.wpi.edu/~thethe/Document/Besiex/Java/FrIJDE/
85      *
86      * @param sPath_ a directory path. Is not allowed to be null.
87      * @param sFile_ the base name of a file.
88      *
89      * @return sPath_ if sFile_ is empty.
90      */

91     private static String JavaDoc concatPath(String JavaDoc sPath_, String JavaDoc sFile_)
92     {
93         Util.panicIf(sPath_ == null);
94         //System.out.println("ccl.util.FileUtil.concatPath(..).sPath_: --->" + sPath_ + "<---");
95
//System.out.println("ccl.util.FileUtil.concatPath(..).sFile_: " + sFile_);
96

97         String JavaDoc sRetVal = sPath_;
98
99         if (!Util.isEmpty(sFile_))
100         {
101             if (sPath_.length() > 0 && !sPath_.endsWith(File.separator))
102             {
103                 sRetVal += File.separator;
104             }
105
106             sRetVal += sFile_;
107         }
108
109         return sRetVal;
110     }
111
112     /**
113      * Reads a File and returns the content in a String.
114      * CRLF -> LF conversion takes place. This is a convenience method so you don't
115      * need to bother creating a file reader object and closing it after it has
116      * been used.
117      *
118      * @param sFileName_ the name of the file to read.
119      *
120      * @return a string with the content of the file but without
121      * any CR characters.
122      *
123      * @throws FileNotFoundException if file does not exist.
124      * @throws IOException if any file operation fails.
125      */

126     public static String JavaDoc readFile(String JavaDoc sFileName_) throws IOException JavaDoc, FileNotFoundException JavaDoc
127     {
128         StringBuffer JavaDoc sFileContent = new StringBuffer JavaDoc(100000);
129
130         try
131         {
132             FileReader JavaDoc frIni = new FileReader JavaDoc(sFileName_);
133             if (frIni != null)
134             {
135                 BufferedReader JavaDoc brIni = new BufferedReader JavaDoc(frIni);
136                 if (brIni != null)
137                 {
138                     while (brIni.ready())
139                     {
140                         String JavaDoc sLine = brIni.readLine();
141                         if (sLine == null)
142                         {
143                             break;
144                         }
145                         sFileContent.append(sLine).append('\n');
146                     }
147                     brIni.close();
148                 }
149                 frIni.close();
150             }
151         }
152         catch (FileNotFoundException JavaDoc fileNotFoundException)
153         {
154             throw new FileNotFoundException JavaDoc("No such file: '" + sFileName_ + "'");
155         }
156
157         return sFileContent.toString();
158     }
159
160     /**
161      * @return It's the canonical path of sFileName_.
162      */

163     private static String JavaDoc getAbsoluteFileName(String JavaDoc sFileName_)
164     {
165         String JavaDoc sRetVal = null;
166
167         try
168         {
169             File JavaDoc pFile = new File JavaDoc(sFileName_);
170             sRetVal = pFile.getCanonicalPath();
171         }
172         catch (Exception JavaDoc e)
173         {
174             return null;
175         }
176
177         return sRetVal;
178     }
179
180     /**
181      * This method returns an absolute (canonical)
182      * file name. The difference to getAbsoluteFileName
183      * is that this method uses the system property
184      * "user.dir" instead of the native system's current
185      * directory. This way you get a chance of changing
186      * the current directory inside Java and let your
187      * program reflect that change.
188      */

189     public static String JavaDoc normalizeFileName(String JavaDoc sFile)
190     {
191         return normalizeFileName(sFile, (String JavaDoc)System.getProperties().get("user.dir"));
192     }
193
194     /**
195      * This method returns an absolute (canonical)
196      * file name. The difference to getAbsoluteFileName
197      * is that this method uses the system property
198      * sUserDir instead of the native system's current
199      * directory. This way you get a chance of changing
200      * the current directory inside Java and let your
201      * program reflect that change.
202      */

203     private static String JavaDoc normalizeFileName(String JavaDoc sFile, String JavaDoc sUserDir)
204     {
205         sFile = sFile.trim();
206         if (Util.isEmpty(sFile) || sFile.equals("."))
207         {
208             sFile = sUserDir;
209         }
210         else if (!FileUtil.isAbsolute(sFile))
211         {
212             sFile = FileUtil.concatPath(sUserDir, sFile);
213         }
214         sFile = FileUtil.getAbsoluteFileName(sFile);
215
216         return sFile;
217     }
218
219     /**
220      * Tests if the file represented by this File object is an absolute
221      * pathname. The definition of an absolute pathname is system
222      * dependent. For example, on UNIX, a pathname is absolute if its first
223      * character is the separator character. On Windows
224      * platforms, a pathname is absolute if its first character is an
225      * ASCII '\' or '/', or if it begins with a letter followed by a colon.
226      */

227     private static boolean isAbsolute(String JavaDoc sFileName_)
228     {
229         return new File JavaDoc(sFileName_).isAbsolute();
230     }
231
232 }
233
Popular Tags