1 /* ===========================================================
2 * JFreeChart : a free chart library for the Java(tm) platform
3 * ===========================================================
4 *
5 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
6 *
7 * Project Info: http://www.jfree.org/jfreechart/index.html
8 *
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 * USA.
23 *
24 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25 * in the United States and other countries.]
26 *
27 * ---------------------------------
28 * StandardCategoryURLGenerator.java
29 * ---------------------------------
30 * (C) Copyright 2002-2008, by Richard Atkinson and Contributors.
31 *
32 * Original Author: Richard Atkinson;
33 * Contributors: David Gilbert (for Object Refinery Limited);
34 * Cleland Early;
35 *
36 * Changes:
37 * --------
38 * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
39 * 29-Aug-2002 : Reversed seriesParameterName and itemParameterName in
40 * constructor. Never should have been the other way round.
41 * Also updated JavaDoc (RA);
42 * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
43 * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
44 * 23-Mar-2003 : Implemented Serializable (DG);
45 * 13-Aug-2003 : Implemented Cloneable (DG);
46 * 23-Dec-2003 : Added fix for bug 861282 (DG);
47 * 21-May-2004 : Added URL encoding - see patch 947854 (DG);
48 * 13-Jan-2004 : Fixed for compliance with XHTML 1.0 (DG);
49 * ------------- JFREECHART 1.0.x ---------------------------------------------
50 * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
51 * 17-Apr-2007 : Use new URLUtilities class to encode URLs (DG);
52 *
53 */
54
55 package org.jfree.chart.urls;
56
57 import java.io.Serializable;
58
59 import org.jfree.data.category.CategoryDataset;
60 import org.jfree.util.ObjectUtilities;
61
62 /**
63 * A URL generator that can be assigned to a
64 * {@link org.jfree.chart.renderer.category.CategoryItemRenderer}.
65 */
66 public class StandardCategoryURLGenerator implements CategoryURLGenerator,
67 Cloneable, Serializable {
68
69 /** For serialization. */
70 private static final long serialVersionUID = 2276668053074881909L;
71
72 /** Prefix to the URL */
73 private String prefix = "index.html";
74
75 /** Series parameter name to go in each URL */
76 private String seriesParameterName = "series";
77
78 /** Category parameter name to go in each URL */
79 private String categoryParameterName = "category";
80
81 /**
82 * Creates a new generator with default settings.
83 */
84 public StandardCategoryURLGenerator() {
85 super();
86 }
87
88 /**
89 * Constructor that overrides default prefix to the URL.
90 *
91 * @param prefix the prefix to the URL (<code>null</code> not permitted).
92 */
93 public StandardCategoryURLGenerator(String prefix) {
94 if (prefix == null) {
95 throw new IllegalArgumentException("Null 'prefix' argument.");
96 }
97 this.prefix = prefix;
98 }
99
100 /**
101 * Constructor that overrides all the defaults.
102 *
103 * @param prefix the prefix to the URL (<code>null</code> not permitted).
104 * @param seriesParameterName the name of the series parameter to go in
105 * each URL (<code>null</code> not permitted).
106 * @param categoryParameterName the name of the category parameter to go in
107 * each URL (<code>null</code> not permitted).
108 */
109 public StandardCategoryURLGenerator(String prefix,
110 String seriesParameterName,
111 String categoryParameterName) {
112
113 if (prefix == null) {
114 throw new IllegalArgumentException("Null 'prefix' argument.");
115 }
116 if (seriesParameterName == null) {
117 throw new IllegalArgumentException(
118 "Null 'seriesParameterName' argument.");
119 }
120 if (categoryParameterName == null) {
121 throw new IllegalArgumentException(
122 "Null 'categoryParameterName' argument.");
123 }
124 this.prefix = prefix;
125 this.seriesParameterName = seriesParameterName;
126 this.categoryParameterName = categoryParameterName;
127
128 }
129
130 /**
131 * Generates a URL for a particular item within a series.
132 *
133 * @param dataset the dataset.
134 * @param series the series index (zero-based).
135 * @param category the category index (zero-based).
136 *
137 * @return The generated URL.
138 */
139 public String generateURL(CategoryDataset dataset, int series,
140 int category) {
141 String url = this.prefix;
142 Comparable seriesKey = dataset.getRowKey(series);
143 Comparable categoryKey = dataset.getColumnKey(category);
144 boolean firstParameter = url.indexOf("?") == -1;
145 url += firstParameter ? "?" : "&";
146 url += this.seriesParameterName + "=" + URLUtilities.encode(
147 seriesKey.toString(), "UTF-8");
148 url += "&" + this.categoryParameterName + "="
149 + URLUtilities.encode(categoryKey.toString(), "UTF-8");
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 public Object clone() throws CloneNotSupportedException {
162 // all attributes are immutable, so we can just return the super.clone()
163 // FIXME: in fact, the generator itself is immutable, so cloning is
164 // not necessary
165 return super.clone();
166 }
167
168 /**
169 * Tests the generator for equality with an arbitrary object.
170 *
171 * @param obj the object (<code>null</code> permitted).
172 *
173 * @return A boolean.
174 */
175 public boolean equals(Object obj) {
176 if (obj == this) {
177 return true;
178 }
179 if (!(obj instanceof StandardCategoryURLGenerator)) {
180 return false;
181 }
182 StandardCategoryURLGenerator that = (StandardCategoryURLGenerator) obj;
183 if (!ObjectUtilities.equal(this.prefix, that.prefix)) {
184 return false;
185 }
186
187 if (!ObjectUtilities.equal(this.seriesParameterName,
188 that.seriesParameterName)) {
189 return false;
190 }
191 if (!ObjectUtilities.equal(this.categoryParameterName,
192 that.categoryParameterName)) {
193 return false;
194 }
195 return true;
196 }
197
198 /**
199 * Returns a hash code.
200 *
201 * @return A hash code.
202 */
203 public int hashCode() {
204 int result;
205 result = (this.prefix != null ? this.prefix.hashCode() : 0);
206 result = 29 * result
207 + (this.seriesParameterName != null
208 ? this.seriesParameterName.hashCode() : 0);
209 result = 29 * result
210 + (this.categoryParameterName != null
211 ? this.categoryParameterName.hashCode() : 0);
212 return result;
213 }
214
215 }