001: package xdoclet.junit;
002:
003: /* Copyright 2003-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.File;
019: import java.io.FileWriter;
020: import java.io.IOException;
021:
022: /**
023: * Performs file i/o, generation and removal of temporary files etc.
024: *
025: * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
026: */
027: public class FileHandling {
028: private File _tmpDir = null;
029:
030: public File getTmpDir() {
031: return _tmpDir;
032: }
033:
034: public File write(String name, String content) throws IOException {
035: // first we determine the temp directory
036: if (_tmpDir == null) {
037: File dummy = File.createTempFile("dummy", ".java");
038: String tmpDir = dummy.getPath().substring(0,
039: dummy.getPath().lastIndexOf(File.separatorChar));
040:
041: if ((tmpDir == null) || (tmpDir.length() == 0)) {
042: tmpDir = ".";
043: }
044: dummy.delete();
045:
046: int nr = 0;
047:
048: while (_tmpDir == null) {
049: _tmpDir = new File(tmpDir, "test" + nr);
050: if (_tmpDir.exists()) {
051: nr++;
052: _tmpDir = null;
053: }
054: }
055: _tmpDir.mkdir();
056: }
057:
058: // next we generate a file for the srcClass
059: String fileName = name.replace('.', File.separatorChar)
060: + ".java";
061: File srcFile = ensurePath(fileName);
062: FileWriter output = new FileWriter(srcFile.getAbsolutePath());
063:
064: output.write(content);
065: output.close();
066: return srcFile;
067: }
068:
069: public File ensurePath(String fileName) {
070: String shortFileName = fileName;
071: File dir = _tmpDir;
072: int pos = shortFileName.indexOf(File.separatorChar);
073:
074: while (pos >= 0) {
075: dir = new File(dir, shortFileName.substring(0, pos));
076: dir.mkdir();
077: shortFileName = shortFileName.substring(pos + 1);
078: pos = shortFileName.indexOf(File.separatorChar);
079: }
080:
081: return new File(dir, shortFileName);
082: }
083:
084: public void removeTmpDir() {
085: if (_tmpDir != null) {
086: removeDir(_tmpDir);
087: }
088: _tmpDir = null;
089: }
090:
091: private void removeDir(File dir) {
092: if (dir.exists()) {
093: String[] files = dir.list();
094: File sub;
095:
096: if (files != null) {
097: for (int idx = 0; idx < files.length; idx++) {
098: sub = new File(dir, files[idx]);
099: if (sub.isDirectory()) {
100: removeDir(sub);
101: } else {
102: sub.delete();
103: }
104: }
105: }
106: dir.delete();
107: }
108: }
109: }
|