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

ReportGenerator

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

Fields Summary
private IndentingWriter
m_out
private static final String
TYPE
private static final int
IO_BUF_SIZE
Constructors Summary
Methods Summary
public voidcleanup()

        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 voidcloseElementTag(boolean simple)

        if (simple)
            m_out.write ("/>");
        else
            m_out.write ('>");
    
private voidemitItem(com.vladium.emma.report.IItem item, com.vladium.emma.report.ItemComparator childrenOrder)

        
        final IItemMetadata metadata = item.getMetadata (); 
        final int [] columns = m_settings.getColumnOrder ();            
        final String tag = metadata.getTypeName ();
 
        eol ();
        
        // emit opening tag with name attribute:
        {
            openElementTag (tag);
            
            m_out.write (" name=\"");
            m_out.write (Strings.HTMLEscape (item.getName ()));
            m_out.write ('"");
            
            closeElementTag (false);
        }
        
        eol ();
            
        m_out.incIndent ();       

        emitItemCoverage (item, columns);
        
        final boolean deeper = (childrenOrder != null) && (m_settings.getDepth () > metadata.getTypeID ()) && (item.getChildCount () > 0);
        
        if (deeper)
        {
            for (Iterator packages = item.getChildren (childrenOrder); packages.hasNext (); )
            {
                ((IItem) packages.next ()).accept (this, null);
            }
            
            eol ();
        }

        m_out.decIndent ();
        
        // emit closing tag:
        {
            endElement (tag);
        }
    
private voidemitItemCoverage(com.vladium.emma.report.IItem item, int[] columns)

        final StringBuffer buf = new StringBuffer (64);
        
        for (int c = 0, cLimit = columns.length; c < cLimit; ++ c)
        {
            final int attrID = columns [c];
            
            if (attrID != IItemAttribute.ATTRIBUTE_NAME_ID)
            {
                final IItemAttribute attr = item.getAttribute (attrID, m_settings.getUnitsType ());
                
                if (attr != null)
                {
                    openElementTag ("coverage");

                    m_out.write (" type=\"");
                    m_out.write (Strings.HTMLEscape (attr.getName ()));
                    m_out.write ("\" value=\"");
                    attr.format (item, buf);
                    m_out.write (Strings.HTMLEscape (buf.toString ()));
                    m_out.write ('"");
                    buf.setLength (0);
                    
                    closeElementTag (true);
                    
                    eol ();
                }
            }
        }
        
    
private voidemitStatsCount(java.lang.String name, int value)

        
     // end of nested class
    
    
            
         
    
        eol ();
        openElementTag (name);
        m_out.write (" value=\"" + value);
        m_out.write ('"");
        closeElementTag (true);
    
private voidendElement(java.lang.String tag)

        m_out.write ("</");
        m_out.write (tag);
        m_out.write ('>");
    
private voideol()

        m_out.newLine ();
    
public java.lang.StringgetType()

        return TYPE;
    
private voidopenElementTag(java.lang.String tag)

        m_out.write ('<");
        m_out.write (tag);
    
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 IndentingWriter (new OutputStreamWriter (new FileOutputStream (file), encoding), IO_BUF_SIZE, 0);
        }
        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_view.getRoot ().accept (this, null);
            close ();
        }
                
        if (trace1)
        {
            end = System.currentTimeMillis ();
            
            m_log.trace1 ("process", "[" + getType () + "] report generated in " + (end - start) + " ms");
        }
    
public java.lang.Objectvisit(com.vladium.emma.report.AllItem item, java.lang.Object ctx)

        try
        {
            File outFile = m_settings.getOutFile ();
            if (outFile == null)
            {
                outFile = new File ("coverage.xml");
                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);
            
            // XML header:
            m_out.write ("<?xml version=\"1.0\" encoding=\"" + m_settings.getOutEncoding () + "\"?>");
            
            // 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);
            }
            
            eol ();
            openElementTag ("report");
            closeElementTag (false);
            m_out.incIndent ();
            
            // stats summary section:
            eol ();
            openElementTag ("stats");
            closeElementTag (false);
            m_out.incIndent ();
            {
                emitStatsCount ("packages", item.getChildCount ());
                emitStatsCount ("classes", item.getAggregate (IItem.TOTAL_CLASS_COUNT));
                emitStatsCount ("methods", item.getAggregate (IItem.TOTAL_METHOD_COUNT));
                
                if (m_srcView && m_hasSrcFileInfo)
                {
                    emitStatsCount ("srcfiles", item.getAggregate (IItem.TOTAL_SRCFILE_COUNT));
                    
                    if (m_hasLineNumberInfo)
                        emitStatsCount ("srclines", item.getAggregate (IItem.TOTAL_LINE_COUNT));
                }
            }
            m_out.decIndent ();
            eol ();
            endElement ("stats");
            
            // actual coverage data:
            eol ();
            openElementTag ("data");
            closeElementTag (false);
            m_out.incIndent ();
            {
                final ItemComparator childrenOrder = m_typeSortComparators [PackageItem.getTypeMetadata ().getTypeID ()];
                emitItem (item, childrenOrder);
            }
            m_out.decIndent ();
            eol ();
            endElement ("data");
            
            m_out.decIndent ();
            eol ();
            endElement ("report");            
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }

        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 () + "] ...");
        
        try
        {
            final ItemComparator childrenOrder = m_typeSortComparators [m_srcView ? SrcFileItem.getTypeMetadata ().getTypeID () : ClassItem.getTypeMetadata ().getTypeID ()];
            emitItem (item, childrenOrder);
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }

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

        try
        {
            final ItemComparator childrenOrder = m_typeSortComparators [ClassItem.getTypeMetadata ().getTypeID ()];
            emitItem (item, childrenOrder);
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }

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

        try
        {
            final ItemComparator childrenOrder = m_typeSortComparators [MethodItem.getTypeMetadata ().getTypeID ()];
            emitItem (item, childrenOrder);
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }

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

        try
        {
            emitItem (item, null);
        }
        catch (IOException ioe)
        {
            throw new EMMARuntimeException (IAppErrorCodes.REPORT_IO_FAILURE, ioe);
        }

        return ctx;