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.reporting; 024 025 import java.util.*; 026 027 import org.apache.log4j.Logger; 028 029 /** 030 * This class implements a simple {@link Collator}. 031 */ 032 public abstract class AbstractCollator implements Collator { 033 034 static Logger logger=Logger.getLogger(AbstractCollator.class); 035 036 Map contextsByFormat=new HashMap(); 037 038 public void addOutputter(Format format,Serializer serializer) { 039 contextsByFormat.put(format,new FormattingContext(this,serializer)); 040 } 041 042 public void pageClosed(Page page) throws ReportingException { 043 for(Iterator it=contextsByFormat.keySet().iterator();it.hasNext();) { 044 Format format=(Format)it.next(); 045 format.formatPage((FormattingContext)contextsByFormat.get(format),page); 046 } 047 } 048 049 public String getPathToResource(FormattingContext ctx,String resource,Page from) { 050 return getRelativePath(getPathToPage(ctx,from),resource); 051 } 052 053 protected static String getRelativePath(String from,String to) { 054 StringBuffer result=new StringBuffer(to); 055 // TODO: This is a simple implementation - a better one would 056 // recognize a common prefix between the two paths. 057 int it=0; 058 while (true) { 059 it=from.indexOf('/',it); 060 if (it!=-1) { 061 result.insert(0,"../"); 062 } else { 063 break; 064 } 065 it+=1; 066 } 067 return result.toString(); 068 } 069 070 public abstract String getPathToPage(FormattingContext ctx,Page page); 071 072 }