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 * StandardXYURLGenerator.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 *
035 * Changes:
036 * --------
037 * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
038 * 29-Aug-2002 : New constructor and member variables to customise series and
039 *               item parameter names (RA);
040 * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041 * 23-Mar-2003 : Implemented Serializable (DG);
042 * 01-Mar-2004 : Added equals() method (DG);
043 * 13-Jan-2005 : Modified for XHTML 1.0 compliance (DG);
044 * ------------- JFREECHART 1.0.x ---------------------------------------------
045 * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
046 * 03-Jul-2013 : Use ParamChecks (DG);
047 *
048 */
049
050package org.jfree.chart.urls;
051
052import java.io.Serializable;
053import org.jfree.chart.util.ParamChecks;
054
055import org.jfree.data.xy.XYDataset;
056import org.jfree.util.ObjectUtilities;
057
058/**
059 * A URL generator.
060 */
061public class StandardXYURLGenerator implements XYURLGenerator, Serializable {
062
063    /** For serialization. */
064    private static final long serialVersionUID = -1771624523496595382L;
065
066    /** The default prefix. */
067    public static final String DEFAULT_PREFIX = "index.html";
068
069    /** The default series parameter. */
070    public static final String DEFAULT_SERIES_PARAMETER = "series";
071
072    /** The default item parameter. */
073    public static final String DEFAULT_ITEM_PARAMETER = "item";
074
075    /** Prefix to the URL */
076    private String prefix;
077
078    /** Series parameter name to go in each URL */
079    private String seriesParameterName;
080
081    /** Item parameter name to go in each URL */
082    private String itemParameterName;
083
084    /**
085     * Creates a new default generator.  This constructor is equivalent to
086     * calling <code>StandardXYURLGenerator("index.html", "series", "item");
087     * </code>.
088     */
089    public StandardXYURLGenerator() {
090        this(DEFAULT_PREFIX, DEFAULT_SERIES_PARAMETER, DEFAULT_ITEM_PARAMETER);
091    }
092
093    /**
094     * Creates a new generator with the specified prefix.  This constructor
095     * is equivalent to calling
096     * <code>StandardXYURLGenerator(prefix, "series", "item");</code>.
097     *
098     * @param prefix  the prefix to the URL (<code>null</code> not permitted).
099     */
100    public StandardXYURLGenerator(String prefix) {
101        this(prefix, DEFAULT_SERIES_PARAMETER, DEFAULT_ITEM_PARAMETER);
102    }
103
104    /**
105     * Constructor that overrides all the defaults
106     *
107     * @param prefix  the prefix to the URL (<code>null</code> not permitted).
108     * @param seriesParameterName  the name of the series parameter to go in
109     *                             each URL (<code>null</code> not permitted).
110     * @param itemParameterName  the name of the item parameter to go in each
111     *                           URL (<code>null</code> not permitted).
112     */
113    public StandardXYURLGenerator(String prefix, String seriesParameterName,
114            String itemParameterName) {
115        ParamChecks.nullNotPermitted(prefix, "prefix");
116        ParamChecks.nullNotPermitted(seriesParameterName, "seriesParameterName");
117        ParamChecks.nullNotPermitted(itemParameterName, "itemParameterName");
118        this.prefix = prefix;
119        this.seriesParameterName = seriesParameterName;
120        this.itemParameterName = itemParameterName;
121    }
122
123    /**
124     * Generates a URL for a particular item within a series.
125     *
126     * @param dataset  the dataset.
127     * @param series  the series number (zero-based index).
128     * @param item  the item number (zero-based index).
129     *
130     * @return The generated URL.
131     */
132    @Override
133    public String generateURL(XYDataset dataset, int series, int item) {
134        // TODO: URLEncode?
135        String url = this.prefix;
136        boolean firstParameter = url.indexOf("?") == -1;
137        url += firstParameter ? "?" : "&amp;";
138        url += this.seriesParameterName + "=" + series
139                + "&amp;" + this.itemParameterName + "=" + item;
140        return url;
141    }
142
143    /**
144     * Tests this generator for equality with an arbitrary object.
145     *
146     * @param obj  the object (<code>null</code> permitted).
147     *
148     * @return A boolean.
149     */
150    @Override
151    public boolean equals(Object obj) {
152        if (obj == this) {
153            return true;
154        }
155        if (!(obj instanceof StandardXYURLGenerator)) {
156            return false;
157        }
158        StandardXYURLGenerator that = (StandardXYURLGenerator) obj;
159        if (!ObjectUtilities.equal(that.prefix, this.prefix)) {
160            return false;
161        }
162        if (!ObjectUtilities.equal(that.seriesParameterName,
163                this.seriesParameterName)) {
164            return false;
165        }
166        if (!ObjectUtilities.equal(that.itemParameterName,
167                this.itemParameterName)) {
168            return false;
169        }
170        return true;
171    }
172
173}