KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > junit > FileHandling


1 package xdoclet.junit;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.File JavaDoc;
19 import java.io.FileWriter JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 /**
23  * Performs file i/o, generation and removal of temporary files etc.
24  *
25  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
26  */

27 public class FileHandling
28 {
29     private File JavaDoc _tmpDir = null;
30
31     public File JavaDoc getTmpDir()
32     {
33         return _tmpDir;
34     }
35
36     public File JavaDoc write(String JavaDoc name, String JavaDoc content) throws IOException JavaDoc
37     {
38         // first we determine the temp directory
39
if (_tmpDir == null)
40         {
41             File JavaDoc dummy = File.createTempFile("dummy", ".java");
42             String JavaDoc tmpDir = dummy.getPath().substring(0, dummy.getPath().lastIndexOf(File.separatorChar));
43
44             if ((tmpDir == null) || (tmpDir.length() == 0))
45             {
46                 tmpDir = ".";
47             }
48             dummy.delete();
49
50             int nr = 0;
51
52             while (_tmpDir == null)
53             {
54                 _tmpDir = new File JavaDoc(tmpDir, "test"+nr);
55                 if (_tmpDir.exists())
56                 {
57                     nr++;
58                     _tmpDir = null;
59                 }
60             }
61             _tmpDir.mkdir();
62         }
63
64         // next we generate a file for the srcClass
65
String JavaDoc fileName = name.replace('.', File.separatorChar) + ".java";
66         File JavaDoc srcFile = ensurePath(fileName);
67         FileWriter JavaDoc output = new FileWriter JavaDoc(srcFile.getAbsolutePath());
68
69         output.write(content);
70         output.close();
71         return srcFile;
72     }
73
74     public File JavaDoc ensurePath(String JavaDoc fileName)
75     {
76         String JavaDoc shortFileName = fileName;
77         File JavaDoc dir = _tmpDir;
78         int pos = shortFileName.indexOf(File.separatorChar);
79
80         while (pos >= 0)
81         {
82             dir = new File JavaDoc(dir, shortFileName.substring(0, pos));
83             dir.mkdir();
84             shortFileName = shortFileName.substring(pos + 1);
85             pos = shortFileName.indexOf(File.separatorChar);
86         }
87
88         return new File JavaDoc(dir, shortFileName);
89     }
90
91     public void removeTmpDir()
92     {
93         if (_tmpDir != null)
94         {
95             removeDir(_tmpDir);
96         }
97         _tmpDir = null;
98     }
99
100     private void removeDir(File JavaDoc dir)
101     {
102         if (dir.exists())
103         {
104             String JavaDoc[] files = dir.list();
105             File JavaDoc sub;
106
107             if (files != null)
108             {
109                 for (int idx = 0; idx < files.length; idx++)
110                 {
111                     sub = new File JavaDoc(dir, files[idx]);
112                     if (sub.isDirectory())
113                     {
114                         removeDir(sub);
115                     }
116                     else
117                     {
118                         sub.delete();
119                     }
120                 }
121             }
122             dir.delete();
123         }
124     }
125 }
126
Popular Tags