/*
  i-net software provides programming examples for illustration only, without warranty
  either expressed or implied, including, but not limited to, the implied warranties
  of merchantability and/or fitness for a particular purpose. This programming example
  assumes that you are familiar with the programming language being demonstrated and
  the tools used to create and debug procedures. i-net software support professionals
  can help explain the functionality of a particular procedure, but they will not modify
  these examples to provide added functionality or construct procedures to meet your
  specific needs.
  © i-net software 2011
*/
 
package samples;
 
import java.util.ArrayList;
import java.util.List;
 
import com.inet.pdfc.config.IConfiguration;
import com.inet.pdfc.config.PDFCProperty;
import com.inet.pdfc.results.IDifference;
import com.inet.pdfc.results.IPageResult;
import com.inet.pdfc.results.IReportResult;
import com.inet.pdfc.results.IResultHandler;
 
 
/**
 * This is an example of a custom result handler that provides some additional functionality:
 * <pre>
 * getResultCount()
 * getDifferenceCount( int reportIndex )
 * isFilesIdentical( int reportIndex )
 * getDifferenceDescription( int reportIndex )
 * </pre> 
 */
public class CustomResultHandler implements IResultHandler {
    private IConfiguration configuration;
    private List<IReportResult> allResults = new ArrayList<IReportResult>();
 
    /**
     * Initiates the custom handler
     * @param configuration  i-net PDFC configuration
     */
    public CustomResultHandler( IConfiguration configuration ) {
        this.configuration = configuration;
    }
 
    /**
     * {@inheritDoc}
     */
    public void addResult( IReportResult result ) {
        allResults.add(result);
    }
 
    /**
     * {@inheritDoc}
     */
    public void finish() {
 
    }
 
    /**
     * @return the number of file compare results 
     */
    public int getResultCount() {
        return allResults.size();
    }
 
    /**
     * Returns the number of differences detected by comparing files.
     * @param reportIndex   index of the files (0- based)
     * @return   number of differences discovered.
     */
    public int getDifferenceCount( int reportIndex ) {
        int reportDiffs = 0;
        IReportResult reportResult = allResults.get( reportIndex );
        List<IPageResult> pageResults = reportResult.getPageResults();
        for( IPageResult pageResult : pageResults ) {
            List<IDifference> differences = pageResult.getDifferences();
            reportDiffs += differences.size();
        }    
        return reportDiffs;
    }
 
    /**
     * Returns true if no differences found
     * @param reportIndex   index of the files (0- based)
     * @return  true if there were no differences detected.    
     */
    public boolean isFilesIdentical( int reportIndex ) {
        return getDifferenceCount(reportIndex) == 0;
    }
 
    /**
     * Returns the differences found by comparing the two files. The number of differences
     * are limited to the value PDFCProperty.MAX_ERRORS_PER_FILE.
     * @param reportIndex   index of the files (0- based)
     * @return    the description of detected differences.    
     */
    public String getDifferenceDescription( int reportIndex ) {
        IReportResult reportResult = allResults.get( reportIndex );
        List<IPageResult> pageResults = reportResult.getPageResults();
        int maxerrors = configuration.getInt( PDFCProperty.MAX_ERRORS_PER_FILE );
        int errorCount = 0;
        StringBuilder strBuilder = new StringBuilder();
        for( IPageResult pageResult : pageResults ) {
            List<IDifference> differences = pageResult.getDifferences();
            if( differences.size() > 0 ) {
                strBuilder.append( "Page differences: " + pageResult.getPageNumber() + "\n" );
                for( IDifference difference : differences ) {
                    if( maxerrors > 0 && errorCount < maxerrors ) {
                        String errorDescription = difference.getDescription();
                        strBuilder.append( errorDescription );
                        strBuilder.append( ' ' );
                    }
                    errorCount++;
                }
            }
        }
        return strBuilder.toString();
    }
 
    /**
     * Returns the name of the reference file
     * @param reportIndex   index of the file (0-based)
     * @return   the name of the file
     */
    public String getName( int reportIndex ) {
        IReportResult reportResult = allResults.get( reportIndex );
        return reportResult.getName();
    }
}
 

© Copyright 1996 - 2012, i-net software; All Rights Reserved.