1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 /*
24  * Creates logfile and file for debug information
25  *
26  */
27 package com.sun.star.tooling.converter;
28 
29 import java.io.BufferedWriter;
30 import java.io.IOException;
31 
32 /**
33  * Handle the whole output during converting process except the converted files.
34  * Write output to logfiles and to screen depending on switches given
35  * at command line.
36  * @author Christian Schmidt 2005
37  *
38  */
39 public class OutputHandler {
40 
41     private static final String  EMPTY   = "";
42 
43     /**
44      * The logfile to write
45      */
46     public static BufferedWriter logFile;
47 
48     /**
49      * Indicates whether excisting files should be overwritten
50      */
51     public static boolean        doLog   = false;
52 
53     /**
54      * Indicate whether the debug information should be written
55      */
56     public static boolean        doDebug = false;
57 
58     /**
59      * The File to write the debug information to
60      */
61     public static BufferedWriter dbgFile;
62 
63     /**
64      * Write the data to screen and if the switches were set to
65      * logfile and debugfile
66      *
67      * @param out the String to write
68      */
out(String out)69     final static void out(String out) {
70         try {
71             if (doDebug)
72                 OutputHandler.dbg(out);
73 
74             if (!doLog || logFile == null) {
75                 System.out.println(out);
76             } else {
77                 System.out.println(out);
78                 if (EMPTY.equals(out)) {
79                     OutputHandler.log(EMPTY);
80                 } else {
81                     OutputHandler.log(out);
82                 }
83             }
84         } catch (IOException e) {
85 
86             OutputHandler.log(e.toString());
87         }
88     }
89 
90     /**
91      * make a dot on the screen to show the user that it is going on
92      */
printDot()93     final static void printDot() {
94         System.out.print(".");
95 
96     }
97 
98     /**
99      * write the data to log file and if the switch is set
100      * to the debug file
101      *
102      * @param out the String to write
103      */
log(String out)104     final static void log(String out) {
105         try {
106             if(out==null) return;
107             if (doDebug)
108 
109                 OutputHandler.dbg(out);
110 
111             if (doLog) {
112                 if (!EMPTY.equals(out)) {
113                     logFile.write(out);
114                 }
115                 logFile.newLine();
116                 logFile.flush();
117             }
118         } catch (IOException e) {
119 
120             System.out.println(e.getMessage());
121         }
122     }
123 
124     /**
125      * write the string to the debug file
126      *
127      * @param out the String to write
128      * @throws IOException
129      */
dbg(String out)130     final static void dbg(String out) throws IOException {
131         if(out==null) return;
132         if (doDebug) {
133             if (!EMPTY.equals(out)) {
134                 dbgFile.write(out);
135 
136             }
137             dbgFile.newLine();
138             dbgFile.flush();
139         }
140     }
141 
142     /**
143      * Set a new value to indicate whether a debug file should be written or not
144      *
145      * @param doDebug
146      *            The debug value to set.
147      */
setDebug(boolean doDebug)148     private static void setDebug(boolean doDebug) {
149         OutputHandler.doDebug = doDebug;
150     }
151 
152     //	/**
153     //	 * @return Returns the doDebug.
154     //	 */
155     //	private static boolean isDebug() {
156     //		return doDebug;
157     //	}
158 
159     /**
160      * Set the debug file to write to.
161      *
162      * @param dbgFile
163      *            The debug File to use.
164      */
setDbgFile(BufferedWriter dbgFile)165     private static void setDbgFile(BufferedWriter dbgFile) {
166         OutputHandler.dbgFile = dbgFile;
167     }
168 
169     /**
170      * @return Returns the debug File.
171      */
getDbgFile()172     private static BufferedWriter getDbgFile() {
173         return dbgFile;
174     }
175 
176     /**
177      * @param doLog
178      *            The log value to set.
179      */
setLog(boolean doLog)180     private static void setLog(boolean doLog) {
181         OutputHandler.doLog = doLog;
182     }
183 
184     /**
185      * @return Returns the log value.
186      */
isLog()187     private static boolean isLog() {
188         return doLog;
189     }
190 
191     /**
192      * @param logFile
193      *            The logFile to set.
194      */
setLogFile(BufferedWriter logFile)195     private static void setLogFile(BufferedWriter logFile) {
196         OutputHandler.logFile = logFile;
197     }
198 
199     /**
200      * @return Returns the logFile.
201      */
getLogFile()202     private static BufferedWriter getLogFile() {
203         return logFile;
204     }
205 
206     /**
207      * @throws IOException
208      *             close log and dbg
209      */
closeAll()210     public static void closeAll() throws IOException {
211         if (OutputHandler.logFile != null) {
212             OutputHandler.logFile.flush();
213             OutputHandler.logFile.close();
214         }
215         if (OutputHandler.dbgFile != null) {
216             OutputHandler.dbgFile.flush();
217             OutputHandler.dbgFile.close();
218         }
219     }
220 
221 }