001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * ---------------------------
028 * StandardXYURLGenerator.java
029 * ---------------------------
030 * (C) Copyright 2002-2008, 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 *
047 */
048
049package org.jfree.chart.urls;
050
051import java.io.Serializable;
052
053import org.jfree.data.xy.XYDataset;
054import org.jfree.util.ObjectUtilities;
055
056/**
057 * A URL generator.
058 */
059public class StandardXYURLGenerator implements XYURLGenerator, Serializable {
060
061    /** For serialization. */
062    private static final long serialVersionUID = -1771624523496595382L;
063
064    /** The default prefix. */
065    public static final String DEFAULT_PREFIX = "index.html";
066
067    /** The default series parameter. */
068    public static final String DEFAULT_SERIES_PARAMETER = "series";
069
070    /** The default item parameter. */
071    public static final String DEFAULT_ITEM_PARAMETER = "item";
072
073    /** Prefix to the URL */
074    private String prefix;
075
076    /** Series parameter name to go in each URL */
077    private String seriesParameterName;
078
079    /** Item parameter name to go in each URL */
080    private String itemParameterName;
081
082    /**
083     * Creates a new default generator.  This constructor is equivalent to
084     * calling <code>StandardXYURLGenerator("index.html", "series", "item");
085     * </code>.
086     */
087    public StandardXYURLGenerator() {
088        this(DEFAULT_PREFIX, DEFAULT_SERIES_PARAMETER, DEFAULT_ITEM_PARAMETER);
089    }
090
091    /**
092     * Creates a new generator with the specified prefix.  This constructor
093     * is equivalent to calling
094     * <code>StandardXYURLGenerator(prefix, "series", "item");</code>.
095     *
096     * @param prefix  the prefix to the URL (<code>null</code> not permitted).
097     */
098    public StandardXYURLGenerator(String prefix) {
099        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        // TODO: URLEncode?
141        String url = this.prefix;
142        boolean firstParameter = url.indexOf("?") == -1;
143        url += firstParameter ? "?" : "&amp;";
144        url += this.seriesParameterName + "=" + series
145                + "&amp;" + this.itemParameterName + "=" + item;
146        return url;
147    }
148
149    /**
150     * Tests this generator for equality with an arbitrary object.
151     *
152     * @param obj  the object (<code>null</code> permitted).
153     *
154     * @return A boolean.
155     */
156    public boolean equals(Object obj) {
157        if (obj == this) {
158            return true;
159        }
160        if (!(obj instanceof StandardXYURLGenerator)) {
161            return false;
162        }
163        StandardXYURLGenerator that = (StandardXYURLGenerator) obj;
164        if (!ObjectUtilities.equal(that.prefix, this.prefix)) {
165            return false;
166        }
167        if (!ObjectUtilities.equal(that.seriesParameterName,
168                this.seriesParameterName)) {
169            return false;
170        }
171        if (!ObjectUtilities.equal(that.itemParameterName,
172                this.itemParameterName)) {
173            return false;
174        }
175        return true;
176    }
177
178}