View Javadoc

1   package org.codehaus.mojo.fitnesse.log;
2   
3   /*
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, version 2.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU General Public License for more details.
12   *
13   * You should have received a copy of the GNU General Public License
14   * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
15   */
16  
17  import java.io.File;
18  import java.io.FileWriter;
19  import java.io.IOException;
20  
21  public class FileConsumer
22      implements FitnesseStreamConsumer
23  {
24  
25      FileWriter mOutputWriter;
26  
27      private boolean mHasGeneratedResultFile = false;
28  
29      private String mLineSep = System.getProperty( "line.separator" );
30  
31      /** Only for test. */
32      public FileConsumer()
33      {
34      }
35  
36      public FileConsumer( File pOutputFile )
37      {
38          super();
39          try
40          {
41              mOutputWriter = new FileWriter( pOutputFile );
42          }
43          catch ( IOException e )
44          {
45              throw new RuntimeException( "Unable to write into file" );
46          }
47      }
48  
49      public synchronized void consumeLine( String pMessage )
50      {
51          try
52          {
53              mOutputWriter.write( pMessage + mLineSep );
54          }
55          catch ( IOException e )
56          {
57              throw new RuntimeException( "Unable to write into file" );
58          }
59          mHasGeneratedResultFile = mHasGeneratedResultFile || pMessage.startsWith( "Formatting as html" );
60      }
61  
62      public boolean hasGeneratedResultFile()
63      {
64          return mHasGeneratedResultFile;
65      }
66  
67      public void close()
68      {
69          try
70          {
71              mOutputWriter.flush();
72              mOutputWriter.close();
73          }
74          catch ( IOException e )
75          {
76              throw new RuntimeException( "Unable to write into file" );
77          }
78      }
79  
80  }