001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * --------------------------------- 028 * StandardCategoryURLGenerator.java 029 * --------------------------------- 030 * (C) Copyright 2002-2007, by Richard Atkinson and Contributors. 031 * 032 * Original Author: Richard Atkinson; 033 * Contributors: David Gilbert (for Object Refinery Limited); 034 * Cleland Early; 035 * 036 * $Id: StandardCategoryURLGenerator.java,v 1.6.2.2 2007/02/02 15:51:05 mungady Exp $ 037 * 038 * Changes: 039 * -------- 040 * 05-Aug-2002 : Version 1, contributed by Richard Atkinson; 041 * 29-Aug-2002 : Reversed seriesParameterName and itemParameterName in 042 * constructor. Never should have been the other way round. 043 * Also updated JavaDoc (RA); 044 * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG); 045 * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG); 046 * 23-Mar-2003 : Implemented Serializable (DG); 047 * 13-Aug-2003 : Implemented Cloneable (DG); 048 * 23-Dec-2003 : Added fix for bug 861282 (DG); 049 * 21-May-2004 : Added URL encoding - see patch 947854 (DG); 050 * 13-Jan-2004 : Fixed for compliance with XHTML 1.0 (DG); 051 * ------------- JFREECHART 1.0.x --------------------------------------------- 052 * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG); 053 * 054 */ 055 056 package org.jfree.chart.urls; 057 058 import java.io.Serializable; 059 import java.net.URLEncoder; 060 061 import org.jfree.data.category.CategoryDataset; 062 import org.jfree.util.ObjectUtilities; 063 064 /** 065 * A URL generator that can be assigned to a 066 * {@link org.jfree.chart.renderer.category.CategoryItemRenderer}. 067 */ 068 public class StandardCategoryURLGenerator implements CategoryURLGenerator, 069 Cloneable, Serializable { 070 071 /** For serialization. */ 072 private static final long serialVersionUID = 2276668053074881909L; 073 074 /** Prefix to the URL */ 075 private String prefix = "index.html"; 076 077 /** Series parameter name to go in each URL */ 078 private String seriesParameterName = "series"; 079 080 /** Category parameter name to go in each URL */ 081 private String categoryParameterName = "category"; 082 083 /** 084 * Creates a new generator with default settings. 085 */ 086 public StandardCategoryURLGenerator() { 087 super(); 088 } 089 090 /** 091 * Constructor that overrides default prefix to the URL. 092 * 093 * @param prefix the prefix to the URL (<code>null</code> not permitted). 094 */ 095 public StandardCategoryURLGenerator(String prefix) { 096 if (prefix == null) { 097 throw new IllegalArgumentException("Null 'prefix' argument."); 098 } 099 this.prefix = prefix; 100 } 101 102 /** 103 * Constructor that overrides all the defaults. 104 * 105 * @param prefix the prefix to the URL (<code>null</code> not permitted). 106 * @param seriesParameterName the name of the series parameter to go in 107 * each URL (<code>null</code> not permitted). 108 * @param categoryParameterName the name of the category parameter to go in 109 * each URL (<code>null</code> not permitted). 110 */ 111 public StandardCategoryURLGenerator(String prefix, 112 String seriesParameterName, 113 String categoryParameterName) { 114 115 if (prefix == null) { 116 throw new IllegalArgumentException("Null 'prefix' argument."); 117 } 118 if (seriesParameterName == null) { 119 throw new IllegalArgumentException( 120 "Null 'seriesParameterName' argument." 121 ); 122 } 123 if (categoryParameterName == null) { 124 throw new IllegalArgumentException( 125 "Null 'categoryParameterName' argument." 126 ); 127 } 128 this.prefix = prefix; 129 this.seriesParameterName = seriesParameterName; 130 this.categoryParameterName = categoryParameterName; 131 132 } 133 134 /** 135 * Generates a URL for a particular item within a series. 136 * 137 * @param dataset the dataset. 138 * @param series the series index (zero-based). 139 * @param category the category index (zero-based). 140 * 141 * @return The generated URL. 142 */ 143 public String generateURL(CategoryDataset dataset, int series, 144 int category) { 145 String url = this.prefix; 146 Comparable seriesKey = dataset.getRowKey(series); 147 Comparable categoryKey = dataset.getColumnKey(category); 148 boolean firstParameter = url.indexOf("?") == -1; 149 url += firstParameter ? "?" : "&"; 150 // try { 151 url += this.seriesParameterName + "=" 152 + URLEncoder.encode(seriesKey.toString()); 153 // + URLEncoder.encode(seriesKey.toString(), "UTF-8"); 154 // Not supported in JDK 1.2.2 155 // } 156 // catch (UnsupportedEncodingException uee) { 157 // url += this.seriesParameterName + "=" + seriesKey.toString(); 158 // } 159 // try { 160 url += "&" + this.categoryParameterName + "=" 161 + URLEncoder.encode(categoryKey.toString()); 162 //+ URLEncoder.encode(categoryKey.toString(), "UTF-8"); 163 // not supported in JDK 1.2.2 164 // } 165 // catch (UnsupportedEncodingException uee) { 166 // url += "&" + this.categoryParameterName + "=" 167 // + categoryKey.toString(); 168 // } 169 return url; 170 } 171 172 /** 173 * Returns an independent copy of the URL generator. 174 * 175 * @return A clone. 176 * 177 * @throws CloneNotSupportedException not thrown by this class, but 178 * subclasses (if any) might. 179 */ 180 public Object clone() throws CloneNotSupportedException { 181 182 // all attributes are immutable, so we can just return the super.clone() 183 return super.clone(); 184 185 } 186 187 /** 188 * Tests the generator for equality with an arbitrary object. 189 * 190 * @param obj the object (<code>null</code> permitted). 191 * 192 * @return A boolean. 193 */ 194 public boolean equals(Object obj) { 195 if (obj == this) { 196 return true; 197 } 198 if (!(obj instanceof StandardCategoryURLGenerator)) { 199 return false; 200 } 201 StandardCategoryURLGenerator that = (StandardCategoryURLGenerator) obj; 202 if (!ObjectUtilities.equal(this.prefix, that.prefix)) { 203 return false; 204 } 205 206 if (!ObjectUtilities.equal(this.seriesParameterName, 207 that.seriesParameterName)) { 208 return false; 209 } 210 if (!ObjectUtilities.equal(this.categoryParameterName, 211 that.categoryParameterName)) { 212 return false; 213 } 214 return true; 215 } 216 217 /** 218 * Returns a hash code. 219 * 220 * @return A hash code. 221 */ 222 public int hashCode() { 223 int result; 224 result = (this.prefix != null ? this.prefix.hashCode() : 0); 225 result = 29 * result 226 + (this.seriesParameterName != null 227 ? this.seriesParameterName.hashCode() : 0); 228 result = 29 * result 229 + (this.categoryParameterName != null 230 ? this.categoryParameterName.hashCode() : 0); 231 return result; 232 } 233 234 }