View Javadoc
1   package org.codehaus.mojo.clirr;
2   
3   import java.io.FileNotFoundException;
4   import java.io.FileOutputStream;
5   import java.io.PrintStream;
6   
7   import net.sf.clirr.core.ApiDifference;
8   import net.sf.clirr.core.DiffListener;
9   import net.sf.clirr.core.MessageTranslator;
10  
11  /**
12   * A copy of Clirr's XmlDiffListener (which is not subclassable nor extendable in some way)
13   * that includes the type of the difference in the XML. This feature is useful for people working
14   * with the ignored differences so that they can easily come up with the rules without consulting
15   * the documentation for the correct difference type number.
16   *
17   * @author Lukas Krejci
18   * @since 2.6
19   */
20  public class TypeRevealingXmlDiffListener
21      implements DiffListener
22  {
23      private static final String DIFFREPORT = "diffreport";
24  
25      private static final String DIFFERENCE = "difference";
26  
27      private final MessageTranslator translator = new MessageTranslator();
28  
29      private final PrintStream out;
30  
31      public TypeRevealingXmlDiffListener( String outFile )
32          throws FileNotFoundException
33      {
34          out = new PrintStream( new FileOutputStream( outFile ) );
35      }
36  
37      public void reportDiff( ApiDifference difference )
38      {
39          out.print( "  <" + DIFFERENCE );
40          out.print( " type=\"" + difference.getMessage().getId() + "\"" );
41          out.print( " binseverity=\"" + difference.getBinaryCompatibilitySeverity() + "\"" );
42          out.print( " srcseverity=\"" + difference.getSourceCompatibilitySeverity() + "\"" );
43          out.print( " class=\"" + difference.getAffectedClass() + "\"" );
44          if ( difference.getAffectedMethod() != null )
45          {
46              out.print( " method=\"" + difference.getAffectedMethod() + "\"" );
47          }
48          if ( difference.getAffectedField() != null )
49          {
50              out.print( " field=\"" + difference.getAffectedField() + "\"" );
51          }
52          out.print( ">" );
53          out.print( difference.getReport( translator ) ); // TODO: XML escapes??
54          out.println( "</" + DIFFERENCE + '>' );
55      }
56  
57      public void start()
58      {
59          out.println( "<?xml version=\"1.0\"?>" );
60          out.println( '<' + DIFFREPORT + '>' );
61      }
62  
63  
64      public void stop()
65      {
66          out.println( "</" + DIFFREPORT + '>' );
67          out.close();
68      }
69  }