1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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 License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ---------------------------------
  27. * StandardCategoryURLGenerator.java
  28. * ---------------------------------
  29. * (C) Copyright 2002-2005, by Richard Atkinson and Contributors.
  30. *
  31. * Original Author: Richard Atkinson;
  32. * Contributors: David Gilbert (for Object Refinery Limited);
  33. * Cleland Early;
  34. *
  35. * $Id: StandardCategoryURLGenerator.java,v 1.4 2005/03/09 13:45:32 mungady Exp $
  36. *
  37. * Changes:
  38. * --------
  39. * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
  40. * 29-Aug-2002 : Reversed seriesParameterName and itemParameterName in
  41. * constructor. Never should have been the other way round.
  42. * Also updated JavaDoc (RA);
  43. * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  44. * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
  45. * 23-Mar-2003 : Implemented Serializable (DG);
  46. * 13-Aug-2003 : Implemented Cloneable (DG);
  47. * 23-Dec-2003 : Added fix for bug 861282 (DG);
  48. * 21-May-2004 : Added URL encoding - see patch 947854 (DG);
  49. * 13-Jan-2004 : Fixed for compliance with XHTML 1.0 (DG);
  50. *
  51. */
  52. package org.jfree.chart.urls;
  53. import java.io.Serializable;
  54. import java.net.URLEncoder;
  55. import org.jfree.data.category.CategoryDataset;
  56. import org.jfree.util.ObjectUtilities;
  57. /**
  58. * A URL generator that can be assigned to a
  59. * {@link org.jfree.chart.renderer.category.CategoryItemRenderer}.
  60. *
  61. * @author Richard Atkinson
  62. */
  63. public class StandardCategoryURLGenerator implements CategoryURLGenerator,
  64. Cloneable, Serializable {
  65. /** Prefix to the URL */
  66. private String prefix = "index.html";
  67. /** Series parameter name to go in each URL */
  68. private String seriesParameterName = "series";
  69. /** Category parameter name to go in each URL */
  70. private String categoryParameterName = "category";
  71. /**
  72. * Creates a new generator with default settings.
  73. */
  74. public StandardCategoryURLGenerator() {
  75. super();
  76. }
  77. /**
  78. * Constructor that overrides default prefix to the URL.
  79. *
  80. * @param prefix the prefix to the URL (<code>null</code> not permitted).
  81. */
  82. public StandardCategoryURLGenerator(String prefix) {
  83. if (prefix == null) {
  84. throw new IllegalArgumentException("Null 'prefix' argument.");
  85. }
  86. this.prefix = prefix;
  87. }
  88. /**
  89. * Constructor that overrides all the defaults.
  90. *
  91. * @param prefix the prefix to the URL (<code>null</code> not permitted).
  92. * @param seriesParameterName the name of the series parameter to go in
  93. * each URL (<code>null</code> not permitted).
  94. * @param categoryParameterName the name of the category parameter to go in
  95. * each URL (<code>null</code> not permitted).
  96. */
  97. public StandardCategoryURLGenerator(String prefix,
  98. String seriesParameterName,
  99. String categoryParameterName) {
  100. if (prefix == null) {
  101. throw new IllegalArgumentException("Null 'prefix' argument.");
  102. }
  103. if (seriesParameterName == null) {
  104. throw new IllegalArgumentException(
  105. "Null 'seriesParameterName' argument."
  106. );
  107. }
  108. if (categoryParameterName == null) {
  109. throw new IllegalArgumentException(
  110. "Null 'categoryParameterName' argument."
  111. );
  112. }
  113. this.prefix = prefix;
  114. this.seriesParameterName = seriesParameterName;
  115. this.categoryParameterName = categoryParameterName;
  116. }
  117. /**
  118. * Generates a URL for a particular item within a series.
  119. *
  120. * @param dataset the dataset.
  121. * @param series the series index (zero-based).
  122. * @param category the category index (zero-based).
  123. *
  124. * @return The generated URL.
  125. */
  126. public String generateURL(CategoryDataset dataset, int series,
  127. int category) {
  128. String url = this.prefix;
  129. Comparable seriesKey = dataset.getRowKey(series);
  130. Comparable categoryKey = dataset.getColumnKey(category);
  131. boolean firstParameter = url.indexOf("?") == -1;
  132. url += firstParameter ? "?" : "&";
  133. // try {
  134. url += this.seriesParameterName + "="
  135. + URLEncoder.encode(seriesKey.toString());
  136. // + URLEncoder.encode(seriesKey.toString(), "UTF-8");
  137. // Not supported in JDK 1.2.2
  138. // }
  139. // catch (UnsupportedEncodingException uee) {
  140. // url += this.seriesParameterName + "=" + seriesKey.toString();
  141. // }
  142. // try {
  143. url += "&" + this.categoryParameterName + "="
  144. + URLEncoder.encode(categoryKey.toString());
  145. //+ URLEncoder.encode(categoryKey.toString(), "UTF-8");
  146. // not supported in JDK 1.2.2
  147. // }
  148. // catch (UnsupportedEncodingException uee) {
  149. // url += "&" + this.categoryParameterName + "="
  150. // + categoryKey.toString();
  151. // }
  152. return url;
  153. }
  154. /**
  155. * Returns an independent copy of the URL generator.
  156. *
  157. * @return A clone.
  158. *
  159. * @throws CloneNotSupportedException not thrown by this class, but
  160. * subclasses (if any) might.
  161. */
  162. public Object clone() throws CloneNotSupportedException {
  163. // all attributes are immutable, so we can just return the super.clone()
  164. return super.clone();
  165. }
  166. /**
  167. * Tests the generator for equality with an arbitrary object.
  168. *
  169. * @param obj the object (<code>null</code> permitted).
  170. *
  171. * @return A boolean.
  172. */
  173. public boolean equals(Object obj) {
  174. if (obj == this) {
  175. return true;
  176. }
  177. if (!(obj instanceof StandardCategoryURLGenerator)) {
  178. return false;
  179. }
  180. StandardCategoryURLGenerator that = (StandardCategoryURLGenerator) obj;
  181. if (!ObjectUtilities.equal(this.prefix, that.prefix)) {
  182. return false;
  183. }
  184. if (!ObjectUtilities.equal(this.seriesParameterName,
  185. that.seriesParameterName)) {
  186. return false;
  187. }
  188. if (!ObjectUtilities.equal(this.categoryParameterName,
  189. that.categoryParameterName)) {
  190. return false;
  191. }
  192. return true;
  193. }
  194. /**
  195. * Returns a hash code.
  196. *
  197. * @return A hash code.
  198. */
  199. public int hashCode() {
  200. int result;
  201. result = (this.prefix != null ? this.prefix.hashCode() : 0);
  202. result = 29 * result
  203. + (this.seriesParameterName != null
  204. ? this.seriesParameterName.hashCode() : 0);
  205. result = 29 * result
  206. + (this.categoryParameterName != null
  207. ? this.categoryParameterName.hashCode() : 0);
  208. return result;
  209. }
  210. }