FileDocCategorySizeDatePackage
ReportGenerator.javaAPI DocAndroid 1.5 API17058Wed May 06 22:41:16 BST 2009com.vladium.emma.report.txt

ReportGenerator

public final class ReportGenerator extends com.vladium.emma.report.AbstractReportGenerator implements com.vladium.emma.IAppErrorCodes
author
Vlad Roubtsov, (C) 2003

Fields Summary
private char
m_separator
private LinkedList
m_queue
private BufferedWriter
m_out
private static final String
TYPE
private static final String
LINE
private static final int
IO_BUF_SIZE
Constructors Summary
Methods Summary
private voidaddHeaderRow(com.vladium.emma.report.IItem item, int[] columns)

        if ($assert.ENABLED)
        {
            $assert.ASSERT (item != null, "null input: item");
            $assert.ASSERT (columns != null, "null input: columns");
        }
        
        // header row:
        final StringBuffer buf = new StringBuffer ();
        
        for (int c = 0, cLimit = columns.length; c < cLimit; ++ c)
        {
            final int attrID = columns [c];
            final IItemAttribute attr = item.getAttribute (attrID, m_settings.getUnitsType ());
            
            if (attr != null)
            {
                buf.append ('[");
                buf.append (attr.getName ());
                buf.append (']");
            }
            if (c != cLimit - 1) buf.append (m_separator);
        }
        
        row (buf);
    
private voidaddItemRow(com.vladium.emma.report.IItem item, int[] columns)

        if ($assert.ENABLED)
        {
            $assert.ASSERT (item != null, "null input: item");
            $assert.ASSERT (columns != null, "null input: columns");
        }
        
        final StringBuffer buf = new StringBuffer (11); // TODO: reuse a buffer
        
        for (int c = 0, cLimit = columns.length; c < cLimit; ++ c)
        {
            final int attrID = columns [c];
            final IItemAttribute attr = item.getAttribute (attrID, m_settings.getUnitsType ());
            
            if (attr != null)
            {
                boolean fail = (m_metrics [attrID] > 0) && ! attr.passes (item, m_metrics [attrID]);
                
                if (fail)
                {
                    //buf.append ('(');
                    //buf.append ("! ");
                    attr.format (item, buf);
                    buf.append ('!");
                    //buf.append (')');
                }
                else
                {
                    attr.format (item, buf);
                }
            }
            if (c != cLimit - 1) buf.append (m_separator);
        }
        
        row (buf);
    
private voidaddTitleRow(java.lang.String text, int hlines, int flines)

        for (int i = 0; i < hlines; ++ i) eol ();
        row (new StringBuffer (text).append (":"));
        for (int i = 0; i < flines; ++ i) eol ();
    
public voidcleanup()

        m_queue = null;
        close ();
        
        super.cleanup ();
    
private voidclose()

        if (m_out != null)
        {
            try
            {
                m_out.flush ();
                m_out.close ();
            }
            catch (IOException ioe)
            {
                throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
            }
            finally
            {
                m_out = null;
            }
        }
    
private voideol()

        try
        {
            m_out.newLine ();
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }
    
public java.lang.StringgetType()

        return TYPE;
    
private voidline()

        try
        {
            m_out.write (LINE);
            m_out.newLine ();
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }
    
private voidopenOutFile(java.io.File file, java.lang.String encoding, boolean mkdirs)

        try
        {
            if (mkdirs)
            {
                final File parent = file.getParentFile ();
                if (parent != null) parent.mkdirs ();
            }
            
            m_out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file), encoding), IO_BUF_SIZE);
        }
        catch (UnsupportedEncodingException uee)
        {
            // TODO: error code
            throw new EMMARuntimeException (uee);
        }
        // note: in J2SDK 1.3 FileOutputStream constructor's throws clause
        // was narrowed to FileNotFoundException:
        catch (IOException fnfe) // FileNotFoundException
        {
            // TODO: error code
            throw new EMMARuntimeException (fnfe);
        }
    
public voidprocess(com.vladium.emma.data.IMetaData mdata, com.vladium.emma.data.ICoverageData cdata, com.vladium.emma.report.SourcePathCache cache, com.vladium.util.IProperties properties)

        initialize (mdata, cdata, cache, properties);
        
        long start = 0, end;
        final boolean trace1 = m_log.atTRACE1 ();
        
        if (trace1) start = System.currentTimeMillis ();
        
        {
            m_queue = new LinkedList ();
            for (m_queue.add (m_view.getRoot ()); ! m_queue.isEmpty (); )
            {
                final IItem head = (IItem) m_queue.removeFirst ();
                
                head.accept (this, null);
            }
            line ();
            
            close ();
        }
                
        if (trace1)
        {
            end = System.currentTimeMillis ();
            
            m_log.trace1 ("process", "[" + getType () + "] report generated in " + (end - start) + " ms");
        }
    
private voidrow(java.lang.StringBuffer str)

        if ($assert.ENABLED) $assert.ASSERT (str != null, "str = null");
        
        try
        {
            m_out.write (str.toString ());
            m_out.newLine ();
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }
    
private voidrow(java.lang.String str)

        if ($assert.ENABLED) $assert.ASSERT (str != null, "str = null");
        
        try
        {
            m_out.write (str);
            m_out.newLine ();
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }
    
public java.lang.Objectvisit(com.vladium.emma.report.AllItem item, java.lang.Object ctx)

        File outFile = m_settings.getOutFile ();
        if (outFile == null)
        {
            outFile = new File ("coverage.txt");
            m_settings.setOutFile (outFile);
        }
        
        final File fullOutFile = Files.newFile (m_settings.getOutDir (), outFile);
        
        m_log.info ("writing [" + getType () + "] report to [" + fullOutFile.getAbsolutePath () + "] ...");
        
        openOutFile (fullOutFile, m_settings.getOutEncoding (), true);
        
        // build ID stamp:
        try
        {
            final StringBuffer label = new StringBuffer (101);
            
            label.append ("[");
            label.append (IAppConstants.APP_NAME);
            label.append (" v"); label.append (IAppConstants.APP_VERSION_WITH_BUILD_ID_AND_TAG);
            label.append (" report, generated ");
            label.append (new Date (EMMAProperties.getTimeStamp ()));
            label.append ("]");
            
            m_out.write (label.toString ());
            m_out.newLine ();
            
            m_out.flush ();
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }
        
        final int [] columns = m_settings.getColumnOrder ();
         
        line ();
                
        // [all] coverage summary row:
        addTitleRow ("OVERALL COVERAGE SUMMARY", 0, 1);            
        {
            // header row:
            addHeaderRow (item, columns);
            
            // coverage row:
            addItemRow (item, columns);
        }
                
        // [all] stats summary table ([all] only):
        addTitleRow ("OVERALL STATS SUMMARY", 1, 1);
        {
            row ("total packages:" + m_separator + item.getChildCount ());
            row ("total classes:" + m_separator + item.getAggregate (IItem.TOTAL_CLASS_COUNT)); 
            row ("total methods:" + m_separator + item.getAggregate (IItem.TOTAL_METHOD_COUNT));
            
            if (m_srcView && m_hasSrcFileInfo)
            {
                row ("total executable files:" + m_separator + item.getAggregate (IItem.TOTAL_SRCFILE_COUNT));
                
                if (m_hasLineNumberInfo)
                    row ("total executable lines:" + m_separator + item.getAggregate (IItem.TOTAL_LINE_COUNT));
            }
        }
        
        final boolean deeper = (m_settings.getDepth () > item.getMetadata ().getTypeID ());
        
        // render package summary rows:
        addTitleRow ("COVERAGE BREAKDOWN BY PACKAGE", 1, 1);
        {
            boolean headerDone = false;
            final ItemComparator order = m_typeSortComparators [PackageItem.getTypeMetadata ().getTypeID ()];                
            for (Iterator packages = item.getChildren (order); packages.hasNext (); )
            {
                final IItem pkg = (IItem) packages.next ();
                
                if (! headerDone)
                {
                    // header row:
                    addHeaderRow (pkg, columns);                        
                    headerDone = true;
                }
                
                // coverage row:
                addItemRow (pkg, columns);
                
                if (deeper) m_queue.addLast (pkg);
            }
        }

        return ctx;
    
public java.lang.Objectvisit(com.vladium.emma.report.PackageItem item, java.lang.Object ctx)

        if (m_verbose) m_log.verbose ("  report: processing package [" + item.getName () + "] ...");
        
        final int [] columns = m_settings.getColumnOrder ();
        
        line ();
        
        // coverage summary row:
        addTitleRow ("COVERAGE SUMMARY FOR PACKAGE [".concat (item.getName ()).concat ("]"), 0, 1);            
        {
            // header row:
            addHeaderRow (item, columns);
            
            // coverage row:
            addItemRow (item, columns);
        }
        
        
        final boolean deeper = (m_settings.getDepth () > item.getMetadata ().getTypeID ());
                
        // render child summary rows:
        
        final String summaryTitle = m_srcView ? "COVERAGE BREAKDOWN BY SOURCE FILE" : "COVERAGE BREAKDOWN BY CLASS";
        addTitleRow (summaryTitle, 1, 1);
        {
            boolean headerDone = false;
            final ItemComparator order = m_typeSortComparators [m_srcView ? SrcFileItem.getTypeMetadata ().getTypeID () : ClassItem.getTypeMetadata ().getTypeID ()];                
            for (Iterator srcORclsFiles = item.getChildren (order); srcORclsFiles.hasNext (); )
            {
                final IItem srcORcls = (IItem) srcORclsFiles.next ();
                
                if (! headerDone)
                {
                    // header row:
                    addHeaderRow (srcORcls, columns);                        
                    headerDone = true;
                }
                
                // coverage row:
                addItemRow (srcORcls, columns);
                
                if (deeper) m_queue.addLast (srcORcls);
            }
        }

        return ctx;
    
public java.lang.Objectvisit(com.vladium.emma.report.SrcFileItem item, java.lang.Object ctx)

        final int [] columns = m_settings.getColumnOrder ();
        
        line ();
        
        // coverage summary row:
        addTitleRow ("COVERAGE SUMMARY FOR SOURCE FILE [".concat (item.getName ()).concat ("]"), 0, 1);            
        {
            // header row:
            addHeaderRow (item, columns);
            
            // coverage row:
            addItemRow (item, columns);
        }
        
        // render child summary rows:
        addTitleRow ("COVERAGE BREAKDOWN BY CLASS AND METHOD", 1, 1);
        {
            boolean headerDone = false;
            final ItemComparator order = m_typeSortComparators [ClassItem.getTypeMetadata ().getTypeID ()];                
            for (Iterator classes = item.getChildren (order); classes.hasNext (); )
            {
                final IItem cls = (IItem) classes.next ();
                
                if (! headerDone)
                {
                    // header row:
                    addHeaderRow (cls, columns);                        
                    headerDone = true;
                }
                
                // coverage row:
                //addItemRow (child, columns);
                
                addTitleRow ("class [" + cls.getName () + "] methods", 0, 0);
                
                // TODO: select the right comparator here
                final ItemComparator order2 = m_typeSortComparators [MethodItem.getTypeMetadata ().getTypeID ()];                
                for (Iterator methods = cls.getChildren (order2); methods.hasNext (); )
                {
                    final MethodItem method = (MethodItem) methods.next ();
                    
                    addItemRow (method, columns);
                }
            }
        }

        return ctx;
    
public java.lang.Objectvisit(com.vladium.emma.report.ClassItem item, java.lang.Object ctx)

        final int [] columns = m_settings.getColumnOrder ();
        
        line ();
        
        // coverage summary row:
        addTitleRow ("COVERAGE SUMMARY FOR CLASS [".concat (item.getName ()).concat ("]"), 0, 1);            
        {
            // header row:
            addHeaderRow (item, columns);
            
            // coverage row:
            addItemRow (item, columns);
        }
        
        // render child summary rows:
        addTitleRow ("COVERAGE BREAKDOWN BY METHOD", 1, 1);
        {
            final ItemComparator order = m_typeSortComparators [MethodItem.getTypeMetadata ().getTypeID ()];                
            for (Iterator methods = item.getChildren (order); methods.hasNext (); )
            {
                final IItem method = (IItem) methods.next ();
                
                // coverage row:
                addItemRow (method, columns);                
            }
        }

        return ctx;