1 /* ===========================================================
2 * JFreeChart : a free chart library for the Java(tm) platform
3 * ===========================================================
4 *
5 * (C) Copyright 2000-2007, 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 * StandardXYURLGenerator.java
29 * ---------------------------
30 * (C) Copyright 2002-2007, by Richard Atkinson and Contributors.
31 *
32 * Original Author: Richard Atkinson;
33 * Contributors: David Gilbert (for Object Refinery Limited);
34 *
35 * Changes:
36 * --------
37 * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
38 * 29-Aug-2002 : New constructor and member variables to customise series and
39 * item parameter names (RA);
40 * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41 * 23-Mar-2003 : Implemented Serializable (DG);
42 * 01-Mar-2004 : Added equals() method (DG);
43 * 13-Jan-2005 : Modified for XHTML 1.0 compliance (DG);
44 * ------------- JFREECHART 1.0.x ---------------------------------------------
45 * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
46 *
47 */
48
49 package org.jfree.chart.urls;
50
51 import java.io.Serializable;
52
53 import org.jfree.data.xy.XYDataset;
54 import org.jfree.util.ObjectUtilities;
55
56 /**
57 * A URL generator.
58 */
59 public class StandardXYURLGenerator implements XYURLGenerator, Serializable {
60
61 /** For serialization. */
62 private static final long serialVersionUID = -1771624523496595382L;
63
64 /** The default prefix. */
65 public static final String DEFAULT_PREFIX = "index.html";
66
67 /** The default series parameter. */
68 public static final String DEFAULT_SERIES_PARAMETER = "series";
69
70 /** The default item parameter. */
71 public static final String DEFAULT_ITEM_PARAMETER = "item";
72
73 /** Prefix to the URL */
74 private String prefix;
75
76 /** Series parameter name to go in each URL */
77 private String seriesParameterName;
78
79 /** Item parameter name to go in each URL */
80 private String itemParameterName;
81
82 /**
83 * Creates a new default generator. This constructor is equivalent to
84 * calling <code>StandardXYURLGenerator("index.html", "series", "item");
85 * </code>.
86 */
87 public StandardXYURLGenerator() {
88 this(DEFAULT_PREFIX, DEFAULT_SERIES_PARAMETER, DEFAULT_ITEM_PARAMETER);
89 }
90
91 /**
92 * Creates a new generator with the specified prefix. This constructor
93 * is equivalent to calling
94 * <code>StandardXYURLGenerator(prefix, "series", "item");</code>.
95 *
96 * @param prefix the prefix to the URL (<code>null</code> not permitted).
97 */
98 public StandardXYURLGenerator(String prefix) {
99 this(prefix, DEFAULT_SERIES_PARAMETER, DEFAULT_ITEM_PARAMETER);
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 itemParameterName the name of the item parameter to go in each
109 * URL (<code>null</code> not permitted).
110 */
111 public StandardXYURLGenerator(String prefix,
112 String seriesParameterName,
113 String itemParameterName) {
114 if (prefix == null) {
115 throw new IllegalArgumentException("Null 'prefix' argument.");
116 }
117 if (seriesParameterName == null) {
118 throw new IllegalArgumentException(
119 "Null 'seriesParameterName' argument.");
120 }
121 if (itemParameterName == null) {
122 throw new IllegalArgumentException(
123 "Null 'itemParameterName' argument.");
124 }
125 this.prefix = prefix;
126 this.seriesParameterName = seriesParameterName;
127 this.itemParameterName = itemParameterName;
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 number (zero-based index).
135 * @param item the item number (zero-based index).
136 *
137 * @return The generated URL.
138 */
139 public String generateURL(XYDataset dataset, int series, int item) {
140 String url = this.prefix;
141 boolean firstParameter = url.indexOf("?") == -1;
142 url += firstParameter ? "?" : "&";
143 url += this.seriesParameterName + "=" + series
144 + "&" + this.itemParameterName + "=" + item;
145 return url;
146 }
147
148 /**
149 * Tests this generator for equality with an arbitrary object.
150 *
151 * @param obj the object (<code>null</code> permitted).
152 *
153 * @return A boolean.
154 */
155 public boolean equals(Object obj) {
156 if (obj == this) {
157 return true;
158 }
159 if (!(obj instanceof StandardXYURLGenerator)) {
160 return false;
161 }
162 StandardXYURLGenerator that = (StandardXYURLGenerator) obj;
163 if (!ObjectUtilities.equal(that.prefix, this.prefix)) {
164 return false;
165 }
166 if (!ObjectUtilities.equal(that.seriesParameterName,
167 this.seriesParameterName)) {
168 return false;
169 }
170 if (!ObjectUtilities.equal(that.itemParameterName,
171 this.itemParameterName)) {
172 return false;
173 }
174 return true;
175 }
176
177 }