001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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-2013, 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 org.jfree.chart.util.ParamChecks;
060
061import org.jfree.data.category.CategoryDataset;
062import 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 */
068public 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        ParamChecks.nullNotPermitted(prefix, "prefix");
097        this.prefix = prefix;
098    }
099
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, String categoryParameterName) {
111
112        ParamChecks.nullNotPermitted(prefix, "prefix");
113        ParamChecks.nullNotPermitted(seriesParameterName, "seriesParameterName");
114        ParamChecks.nullNotPermitted(categoryParameterName, "categoryParameterName");
115        this.prefix = prefix;
116        this.seriesParameterName = seriesParameterName;
117        this.categoryParameterName = categoryParameterName;
118
119    }
120
121    /**
122     * Generates a URL for a particular item within a series.
123     *
124     * @param dataset  the dataset.
125     * @param series  the series index (zero-based).
126     * @param category  the category index (zero-based).
127     *
128     * @return The generated URL.
129     */
130    @Override
131    public String generateURL(CategoryDataset dataset, int series,
132                              int category) {
133        String url = this.prefix;
134        Comparable seriesKey = dataset.getRowKey(series);
135        Comparable categoryKey = dataset.getColumnKey(category);
136        boolean firstParameter = url.indexOf("?") == -1;
137        url += firstParameter ? "?" : "&amp;";
138        url += this.seriesParameterName + "=" + URLUtilities.encode(
139                seriesKey.toString(), "UTF-8");
140        url += "&amp;" + this.categoryParameterName + "="
141                + URLUtilities.encode(categoryKey.toString(), "UTF-8");
142        return url;
143    }
144
145    /**
146     * Returns an independent copy of the URL generator.
147     *
148     * @return A clone.
149     *
150     * @throws CloneNotSupportedException not thrown by this class, but
151     *         subclasses (if any) might.
152     */
153    @Override
154    public Object clone() throws CloneNotSupportedException {
155        // all attributes are immutable, so we can just return the super.clone()
156        // FIXME: in fact, the generator itself is immutable, so cloning is
157        // not necessary
158        return super.clone();
159    }
160
161    /**
162     * Tests the generator for equality with an arbitrary object.
163     *
164     * @param obj  the object (<code>null</code> permitted).
165     *
166     * @return A boolean.
167     */
168    @Override
169    public boolean equals(Object obj) {
170        if (obj == this) {
171            return true;
172        }
173        if (!(obj instanceof StandardCategoryURLGenerator)) {
174            return false;
175        }
176        StandardCategoryURLGenerator that = (StandardCategoryURLGenerator) obj;
177        if (!ObjectUtilities.equal(this.prefix, that.prefix)) {
178            return false;
179        }
180
181        if (!ObjectUtilities.equal(this.seriesParameterName,
182                that.seriesParameterName)) {
183            return false;
184        }
185        if (!ObjectUtilities.equal(this.categoryParameterName,
186                that.categoryParameterName)) {
187            return false;
188        }
189        return true;
190    }
191
192    /**
193     * Returns a hash code.
194     *
195     * @return A hash code.
196     */
197    @Override
198    public int hashCode() {
199        int result;
200        result = (this.prefix != null ? this.prefix.hashCode() : 0);
201        result = 29 * result
202            + (this.seriesParameterName != null
203                    ? this.seriesParameterName.hashCode() : 0);
204        result = 29 * result
205            + (this.categoryParameterName != null
206                    ? this.categoryParameterName.hashCode() : 0);
207        return result;
208    }
209
210}