KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > visitorIDL3 > common > PrintStreamManager


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2005 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Mathieu Vadet.
23 Contributor(s): .
24
25 ====================================================================
26 $Id: PrintStreamManager.java,v 1.1 2005/06/17 15:58:27 merle Exp $
27 ====================================================================*/

28
29 package org.objectweb.ccm.visitorIDL3.common;
30
31 /**
32  * To manage many PrintStream objects at the same time ie :
33  * - creation and closing
34  * - directory setting
35  * - streams managment
36  *
37  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
38  * <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
39  *
40  * @version 0.3
41  */

42
43 public class PrintStreamManager
44 {
45     // ==================================================================
46
//
47
// Internal state.
48
//
49
// ==================================================================
50

51     /**
52      ** The directory in which files will be created.
53      **/

54     private String JavaDoc directory_;
55
56     /**
57      ** The list of already created PrintStream objects.
58      **/

59     private org.objectweb.ccm.util.Table streams_;
60
61     // ==================================================================
62
//
63
// Constructor.
64
//
65
// ==================================================================
66

67     /**
68      ** Default Constructor.
69      **/

70     public
71     PrintStreamManager()
72     {
73         directory_ = "";
74         streams_ = new org.objectweb.ccm.util.Table();
75     }
76
77     // ==================================================================
78
//
79
// Public methods.
80
//
81
// ==================================================================
82

83     /**
84      ** Create a new PrintStream connected to a file named name.
85      **
86      ** @param name The name that will be used for file creation.
87      ** @param tabular The atomic indentation.
88      **
89      ** @return The newly created PrintStream.
90      **/

91     public PrintStream
92     create(String JavaDoc name, String JavaDoc tabular)
93     {
94         PrintStream ps = null;
95         java.io.File JavaDoc file = new java.io.File JavaDoc(directory_);
96         try
97         {
98             file.mkdirs();
99             // check if the file name is stdout.
100
// in this case, we don't have to create a new file.
101
if (name.equals("stdout"))
102                 ps = new PrintStream(System.out,tabular, "stdout", this);
103             else
104                 ps = new PrintStream(
105                          new java.io.PrintStream JavaDoc(new java.io.FileOutputStream JavaDoc(directory_ + name)),
106                          tabular, name, this);
107         }
108         catch (java.io.FileNotFoundException JavaDoc e)
109     {
110             throw new Error JavaDoc("File " + directory_ + name + " not found.");
111         }
112         catch (java.lang.SecurityException JavaDoc e)
113         {
114             throw new Error JavaDoc("Directory " + directory_ + " can\'t be created.");
115         }
116         streams_.put(name, ps);
117         return ps;
118     }
119
120     /**
121      ** To close an already created PrintStream.
122      **
123      ** @param name The name of the underlying file.
124      **/

125     public void
126     close(String JavaDoc name)
127     {
128         PrintStream ps = (PrintStream)streams_.remove(name);
129         if (!name.equals("stdout"))
130     {
131             ps.close();
132     }
133     }
134
135     /**
136      ** Set the directory where the file should be created.
137      ** Note that you must set the directory before calling the create method.
138      **
139      ** @param directory The directory where the files will be create.
140      ** If the directory does not exist, it will be created.
141      **/

142     public void
143     setDirectory(String JavaDoc directory)
144     {
145         directory_ = directory;
146     }
147
148     /**
149      ** To obtain a already created PrintStream according to it's underlying file name.
150      **
151      ** @param name The file name.
152      **
153      ** @return The PrintStream or null if it was already closed or never created.
154      **/

155     public PrintStream
156     getPrintStream(String JavaDoc name)
157     {
158         return (PrintStream)streams_.get(name);
159     }
160 }
161
Popular Tags