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.html; 024 025 import java.io.*; 026 027 import org.apache.log4j.Logger; 028 029 /** 030 * 031 */ 032 public class HtmlFormatHelper { 033 034 static Logger logger=Logger.getLogger(HtmlFormatHelper.class); 035 036 /** 037 * If developers have tabs in their source code, we cannot emit them 038 * in HTML, so will convert them to N non-breaking spaces per tab, 039 * where N is this constant. 040 * <p> 041 * It is recommended that developers avoid using tabs for reasons of 042 * code portability. 043 */ 044 public static final int TAB_WIDTH=4; 045 046 public static String untabify(String original,int tabwidth) { 047 StringBuffer replacement=new StringBuffer(); 048 049 for(int i=0;i<tabwidth;i++) { 050 replacement.append(" "); 051 } 052 053 return replace(original,"\t",replacement.toString()); 054 } 055 056 public static String replaceCharacterEntities(String original) { 057 return replace(replace(replace(replace(original,"&","&"),"<","<"),">",">"),"\"","""); 058 } 059 060 static String replace(String original,String toFind,String replaceWith) { 061 StringBuffer result=new StringBuffer(); 062 int position=0; 063 int p; 064 065 if(original.indexOf(toFind)==-1) { 066 return original; 067 } 068 069 while((p=original.indexOf(toFind,position))!=-1) { 070 result.append(original.substring(position,p)); 071 result.append(replaceWith); 072 position=p+toFind.length(); 073 } 074 075 if(position<original.length()) { 076 result.append(original.substring(position)); 077 } 078 079 if(logger.isDebugEnabled()) { 080 logger.debug("result: "+result); 081 } 082 083 return result.toString(); 084 } 085 086 /** 087 * Since this is HTML, we'll want to encode the incoming file 088 * according to the HTML encoding we're using, so this 089 * implementation just delegates to {@link #drainReader(java.io.Reader,java.io.PrintWriter)}. 090 * <p> 091 * For now, use the platform default encoding, but this can change 092 * later. 093 */ 094 public static void drainInputStream(InputStream in,PrintWriter writer) throws IOException { 095 if (in==null) { 096 throw new IOException("InputStream given to drainInputStream() is null"); 097 } 098 drainReader(new InputStreamReader(in),writer); 099 } 100 101 public static void drainReader(Reader in,PrintWriter writer) throws IOException { 102 if (in==null) { 103 throw new IOException("InputStream given to drainReader() is null"); 104 } 105 BufferedReader reader=new BufferedReader(in); 106 while(true) { 107 String line=reader.readLine(); 108 if (line==null) { 109 break; 110 } 111 writer.println(line); 112 } 113 } 114 115 }