001    /**
002     * www.jcoverage.com
003     * Copyright (C)2003 jcoverage ltd.
004     *
005     * This file is part of jcoverage.
006     *
007     * jcoverage is free software; you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published
009     * by the Free Software Foundation; either version 2 of the License,
010     * or (at your option) any later version.
011     *
012     * jcoverage is distributed in the hope that it will be useful, but
013     * WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with jcoverage; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020     * USA
021     *
022     */
023    package com.jcoverage.coverage.reporting.collation;
024    
025    import java.io.*;
026    import org.apache.log4j.Logger;
027    
028    import com.jcoverage.coverage.reporting.collation.JavaFilePage;
029    import com.jcoverage.coverage.reporting.collation.PackageSummaryPage;
030    import com.jcoverage.coverage.reporting.collation.ReportSummaryPage;
031    import com.jcoverage.coverage.reporting.html.MultiViewStaticHtmlFormat;
032    import com.jcoverage.reporting.AbstractCollator;
033    import com.jcoverage.reporting.FormattingContext;
034    import com.jcoverage.reporting.MultiViewCollator;
035    import com.jcoverage.reporting.Page;
036    import com.jcoverage.reporting.ReportingException;
037    import com.jcoverage.reporting.View;
038    import com.jcoverage.reporting.ViewFormattingContext;
039    import com.jcoverage.reporting.staticgen.StaticView;
040    
041    /**
042     * This class provides an implementation of a collator which
043     * constructs filename paths suitable for a statically generated set
044     * of files.
045     *
046     * <p>
047     *
048     * As it deals with static files, views must be of type {@link
049     * com.jcoverage.reporting.staticgen.StaticView}, since the view's
050     * filename modifier is used to construct a unique filename.
051     */
052    public class StaticFileCollator extends AbstractCollator implements MultiViewCollator {
053      
054      static Logger logger=Logger.getLogger(StaticFileCollator.class);
055    
056      String suffix;
057    
058      public StaticFileCollator(String suffix) {
059        if (suffix.startsWith(".")) {
060          this.suffix=suffix;
061        } else {
062          this.suffix="."+suffix;
063        }
064      }
065    
066      String getFilenameModifier(View view,Page page) {
067        String result="";
068        if (view!=null) {
069          if (view instanceof StaticView) {
070            if (!view.equals(MultiViewStaticHtmlFormat.ALPHABETICAL_VIEW)) {
071              String modifier=((StaticView)view).getFilenameModifier(page);
072              if (modifier!=null) {
073                result=modifier+"/"; // put the view in it's own subdirectory
074              }
075            }
076          } else {
077            throw new IllegalStateException("Views given to this StaticFileCollator must be of type StaticView, this one is "+view.getClass());
078          }
079        }
080        return result;
081      }
082    
083      String getUniquePathFromComponents(View view,Page page,String name,String suffix) {
084        return getFilenameModifier(view,page)+name+suffix;
085      }
086    
087      String getPathToPageView(View view,Page page) {
088    
089        if (page instanceof ReportSummaryPage) {
090          return getUniquePathFromComponents(view,page,"index",suffix);
091    
092        } else if (page instanceof PackageSummaryPage) {
093          String packageName=(String)page.getMasterLine().getField(ReportSummaryPackageLine.COLUMN_PACKAGE_NAME);
094          return getUniquePathFromComponents(view,page,packageName,suffix);
095    
096        } else if (page instanceof JavaFilePage) {
097          String clzName=(String)page.getMasterLine().getField(JavaFileLine.COLUMN_FILE_NAME);
098          return getUniquePathFromComponents(view,page,clzName,suffix);
099    
100        }
101        return null;
102      }
103    
104      public String getPathToPage(FormattingContext ctx,Page page) {
105        if (ctx instanceof ViewFormattingContext) {
106          return getPathToPageView(((ViewFormattingContext)ctx).getCurrentView(),page);
107        } else {
108          return getPathToPageView(null,page);
109        }
110      }
111    
112      /**
113       *
114       */
115      public String getPathToPage(FormattingContext ctx,Page page,Page from) {
116        return getRelativePath(getPathToPage(ctx,from),getPathToPage(ctx,page));
117      }
118    
119      public String getPathToResource(FormattingContext ctx,String resource,Page from) {
120        return getRelativePath(getPathToPage(ctx,from),resource);
121      }
122    
123      public String getPathToPage(ViewFormattingContext ctx,Page page,Page from,View toView) {
124        return getRelativePath(getPathToPage(ctx,from),getPathToPageView(toView,page));
125      }
126    
127    }