View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-Alert: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2006 Open World Ltd, Kevin Rogers
4   //
5   // This file is part of MillScript-Alert.
6   //
7   // MillScript-Alert is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript-Alert is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript-Alert; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.commons.alert.reporters;
22  
23  import org.millscript.commons.alert.Alert;
24  import org.millscript.commons.alert.AlertReporter;
25  import org.millscript.commons.alert.Culprit;
26  
27  import java.util.Iterator;
28  
29  /**
30   * This class provides a skeletal AlertReporter implementation, to make reduce
31   * duplication of code between concrete implementations.
32   */
33  public abstract class AbstractAlertReporter implements AlertReporter {
34  
35      /**
36       * @see org.millscript.commons.alert.AlertReporter#report(org.millscript.commons.alert.Alert)
37       */
38      public void report( final Alert alert ) {
39          // Flust standard output, so the error doesn't get mixed up with other
40          // normal output. We don't need to flush standard error at this point. 
41          System.out.flush();
42          // Report the complaint
43          this.reportComplaint( alert.getComplaint() );
44          // Report the reason
45          if ( alert.getReason() != null ) {
46              this.reportCulprit(
47                  new Culprit( "because", alert.getReason() )
48              );
49          }
50          // Report the alerts culprits
51          final Iterator< Culprit > it = alert.getCulprits().iterator();
52          while ( it.hasNext() ) {
53              this.reportCulprit( it.next() );
54          }
55          // Report any parent Alert
56          if ( alert.getParentAlert() != null ) {
57              this.reportCulprit(
58                  new Culprit( "parent alert", alert.getParentAlert().getClass() )
59              );
60          }
61          // Report any parent Throwable
62          if ( alert.getParentThrowable() != null ) {
63              this.reportCulprit(
64                  new Culprit( "parent throwable", alert.getParentThrowable().getClass() )
65              );
66          }
67          // Report any Phase
68          if ( alert.getPhase() != null ) {
69              this.reportCulprit( alert.getPhase() );
70          }
71          // Now flush the standard error stream to get the report out
72          System.err.flush();
73      }
74  
75      /**
76       * Reports the specified Culprit.
77       *
78       * @param culprit   the Culprit to report
79       */
80      public abstract void reportComplaint( final String complaint );
81  
82      /**
83       * Reports the specified Culprit.
84       *
85       * @param culprit   the Culprit to report
86       */
87      public void reportCulprit( final Culprit culprit ) {
88          System.err.print( culprit.getType().toUpperCase() );
89          System.err.print( " : " );
90          // TODO - This needs to be kept short depending on the output target.
91          // This used to be done in MillScript using it's Output class.
92          System.err.println( culprit.getValue() );
93      }
94  
95  }