KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > util > io > lib > JavaFileHolder


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.util.io.lib;
25
26 import java.io.File JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import org.objectweb.jorm.util.io.api.TargetHolder;
33
34 /**
35  * A TargetHolder ...
36  */

37 public class JavaFileHolder implements TargetHolder {
38
39     private Hashtable JavaDoc name2fw = null;
40
41     private File JavaDoc targetDirectory = null;
42
43     public JavaFileHolder(String JavaDoc _targetDirectory) throws Exception JavaDoc {
44         name2fw = new Hashtable JavaDoc();
45         if (_targetDirectory == null) {
46             String JavaDoc currentDir = System.getProperties().getProperty("user.dir");
47             targetDirectory = new File JavaDoc(currentDir);
48         } else {
49             targetDirectory = new File JavaDoc(_targetDirectory);
50         }
51         if (!targetDirectory.isDirectory())
52             throw new Exception JavaDoc("The parameter must be a directory.");
53     }
54
55     // IMPLEMENTATION OF THE TargetHolder INTERFACE //
56

57     /**
58      * This method permits to obtain a FileWriter either the filename specified
59      * in parameter.
60      */

61     public synchronized FileWriter JavaDoc getFileWriter(String JavaDoc fileName) throws Exception JavaDoc {
62         File JavaDoc child = new File JavaDoc(targetDirectory, fileName);
63         FileWriter JavaDoc result = (FileWriter JavaDoc) name2fw.get(child);
64         if (result == null) {
65             // The file is not referenced into the hashtable
66

67             File JavaDoc parentDir = child.getParentFile();
68             if (!parentDir.exists()) {
69                 //The directory does'nt exist => create it
70
parentDir.mkdirs();
71             }
72
73             // Create the file if necessary see java.io.file.createNewFile()
74
child.createNewFile();
75             result = new FileWriter JavaDoc(child);
76             name2fw.put(child, result);
77         }
78         return result;
79     }
80
81     /**
82      * Return an iterator on the absolute file managed by this holder.
83      * Element type of the returned iterator is java.util.File
84      */

85     public synchronized Iterator JavaDoc iterateFile() {
86         return name2fw.keySet().iterator();
87     }
88
89     /**
90      * Return an iterator on the absolute file writer managed by this holder.
91      * Element type of the returned iterator is java.util.FileWriter
92      */

93     public synchronized Iterator JavaDoc iterateFileWriter() {
94         return name2fw.entrySet().iterator();
95     }
96
97     /**
98      * This method compiles all files managed by this holder
99      */

100     public synchronized void javacAll() throws Exception JavaDoc {
101         //TODO
102
}
103 }
104
Popular Tags