001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2014, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * --------------------------------- 028 * StandardCategoryURLGenerator.java 029 * --------------------------------- 030 * (C) Copyright 2002-2014, by Richard Atkinson and Contributors. 031 * 032 * Original Author: Richard Atkinson; 033 * Contributors: David Gilbert (for Object Refinery Limited); 034 * Cleland Early; 035 * 036 * Changes: 037 * -------- 038 * 05-Aug-2002 : Version 1, contributed by Richard Atkinson; 039 * 29-Aug-2002 : Reversed seriesParameterName and itemParameterName in 040 * constructor. Never should have been the other way round. 041 * Also updated JavaDoc (RA); 042 * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG); 043 * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG); 044 * 23-Mar-2003 : Implemented Serializable (DG); 045 * 13-Aug-2003 : Implemented Cloneable (DG); 046 * 23-Dec-2003 : Added fix for bug 861282 (DG); 047 * 21-May-2004 : Added URL encoding - see patch 947854 (DG); 048 * 13-Jan-2004 : Fixed for compliance with XHTML 1.0 (DG); 049 * ------------- JFREECHART 1.0.x --------------------------------------------- 050 * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG); 051 * 17-Apr-2007 : Use new URLUtilities class to encode URLs (DG); 052 * 02-Jul-2013 : Use ParamChecks (DG); 053 * 054 */ 055 056package org.jfree.chart.urls; 057 058import java.io.Serializable; 059import java.io.UnsupportedEncodingException; 060import java.net.URLEncoder; 061import org.jfree.chart.util.ParamChecks; 062 063import org.jfree.data.category.CategoryDataset; 064import org.jfree.util.ObjectUtilities; 065 066/** 067 * A URL generator that can be assigned to a 068 * {@link org.jfree.chart.renderer.category.CategoryItemRenderer}. 069 */ 070public class StandardCategoryURLGenerator implements CategoryURLGenerator, 071 Cloneable, Serializable { 072 073 /** For serialization. */ 074 private static final long serialVersionUID = 2276668053074881909L; 075 076 /** Prefix to the URL */ 077 private String prefix = "index.html"; 078 079 /** Series parameter name to go in each URL */ 080 private String seriesParameterName = "series"; 081 082 /** Category parameter name to go in each URL */ 083 private String categoryParameterName = "category"; 084 085 /** 086 * Creates a new generator with default settings. 087 */ 088 public StandardCategoryURLGenerator() { 089 super(); 090 } 091 092 /** 093 * Constructor that overrides default prefix to the URL. 094 * 095 * @param prefix the prefix to the URL (<code>null</code> not permitted). 096 */ 097 public StandardCategoryURLGenerator(String prefix) { 098 ParamChecks.nullNotPermitted(prefix, "prefix"); 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, String categoryParameterName) { 113 114 ParamChecks.nullNotPermitted(prefix, "prefix"); 115 ParamChecks.nullNotPermitted(seriesParameterName, 116 "seriesParameterName"); 117 ParamChecks.nullNotPermitted(categoryParameterName, 118 "categoryParameterName"); 119 this.prefix = prefix; 120 this.seriesParameterName = seriesParameterName; 121 this.categoryParameterName = categoryParameterName; 122 123 } 124 125 /** 126 * Generates a URL for a particular item within a series. 127 * 128 * @param dataset the dataset. 129 * @param series the series index (zero-based). 130 * @param category the category index (zero-based). 131 * 132 * @return The generated URL. 133 */ 134 @Override 135 public String generateURL(CategoryDataset dataset, int series, 136 int category) { 137 String url = this.prefix; 138 Comparable seriesKey = dataset.getRowKey(series); 139 Comparable categoryKey = dataset.getColumnKey(category); 140 boolean firstParameter = !url.contains("?"); 141 url += firstParameter ? "?" : "&"; 142 try { 143 url += this.seriesParameterName + "=" + URLEncoder.encode( 144 seriesKey.toString(), "UTF-8"); 145 url += "&" + this.categoryParameterName + "=" 146 + URLEncoder.encode(categoryKey.toString(), "UTF-8"); 147 } catch (UnsupportedEncodingException ex) { 148 throw new RuntimeException(ex); // this won't happen :) 149 } 150 return url; 151 } 152 153 /** 154 * Returns an independent copy of the URL generator. 155 * 156 * @return A clone. 157 * 158 * @throws CloneNotSupportedException not thrown by this class, but 159 * subclasses (if any) might. 160 */ 161 @Override 162 public Object clone() throws CloneNotSupportedException { 163 // all attributes are immutable, so we can just return the super.clone() 164 // FIXME: in fact, the generator itself is immutable, so cloning is 165 // not necessary 166 return super.clone(); 167 } 168 169 /** 170 * Tests the generator for equality with an arbitrary object. 171 * 172 * @param obj the object (<code>null</code> permitted). 173 * 174 * @return A boolean. 175 */ 176 @Override 177 public boolean equals(Object obj) { 178 if (obj == this) { 179 return true; 180 } 181 if (!(obj instanceof StandardCategoryURLGenerator)) { 182 return false; 183 } 184 StandardCategoryURLGenerator that = (StandardCategoryURLGenerator) obj; 185 if (!ObjectUtilities.equal(this.prefix, that.prefix)) { 186 return false; 187 } 188 189 if (!ObjectUtilities.equal(this.seriesParameterName, 190 that.seriesParameterName)) { 191 return false; 192 } 193 if (!ObjectUtilities.equal(this.categoryParameterName, 194 that.categoryParameterName)) { 195 return false; 196 } 197 return true; 198 } 199 200 /** 201 * Returns a hash code. 202 * 203 * @return A hash code. 204 */ 205 @Override 206 public int hashCode() { 207 int result; 208 result = (this.prefix != null ? this.prefix.hashCode() : 0); 209 result = 29 * result 210 + (this.seriesParameterName != null 211 ? this.seriesParameterName.hashCode() : 0); 212 result = 29 * result 213 + (this.categoryParameterName != null 214 ? this.categoryParameterName.hashCode() : 0); 215 return result; 216 } 217 218}