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 * TimeSeriesURLGenerator.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 * 29-Aug-2002 : Initial version (RA);
038 * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
039 * 23-Mar-2003 : Implemented Serializable (DG);
040 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
041 *               getYValue() (DG);
042 * 13-Jan-2005 : Modified for XHTML 1.0 compliance (DG);
043 * ------------- JFREECHART 1.0.x ---------------------------------------------
044 * 06-Jul-2006 : Swap call to dataset's getX() --> getXValue() (DG);
045 * 17-Apr-2007 : Added null argument checks to constructor, new accessor
046 *               methods, added equals() override and used new URLUtilities
047 *               class to encode series key and date (DG);
048 * 03-Jul-2013 : Use ParamChecks (DG);
049 *
050 */
051
052package org.jfree.chart.urls;
053
054import java.io.Serializable;
055import java.io.UnsupportedEncodingException;
056import java.net.URLEncoder;
057import java.text.DateFormat;
058import java.util.Date;
059
060import org.jfree.chart.util.ParamChecks;
061import org.jfree.data.xy.XYDataset;
062
063/**
064 * A URL generator for time series charts.
065 */
066public class TimeSeriesURLGenerator implements XYURLGenerator, Serializable {
067
068    /** For serialization. */
069    private static final long serialVersionUID = -9122773175671182445L;
070
071    /** A formatter for the date. */
072    private DateFormat dateFormat = DateFormat.getInstance();
073
074    /** Prefix to the URL */
075    private String prefix = "index.html";
076
077    /** Name to use to identify the series */
078    private String seriesParameterName = "series";
079
080    /** Name to use to identify the item */
081    private String itemParameterName = "item";
082
083    /**
084     * Default constructor.
085     */
086    public TimeSeriesURLGenerator() {
087        super();
088    }
089
090    /**
091     * Construct TimeSeriesURLGenerator overriding defaults.
092     *
093     * @param dateFormat  a formatter for the date (<code>null</code> not
094     *         permitted).
095     * @param prefix  the prefix of the URL (<code>null</code> not permitted).
096     * @param seriesParameterName  the name of the series parameter in the URL
097     *         (<code>null</code> not permitted).
098     * @param itemParameterName  the name of the item parameter in the URL
099     *         (<code>null</code> not permitted).
100     */
101    public TimeSeriesURLGenerator(DateFormat dateFormat, String prefix,
102            String seriesParameterName, String itemParameterName) {
103
104        ParamChecks.nullNotPermitted(dateFormat, "dateFormat");
105        ParamChecks.nullNotPermitted(prefix, "prefix");
106        ParamChecks.nullNotPermitted(seriesParameterName, "seriesParameterName");
107        ParamChecks.nullNotPermitted(itemParameterName, "itemParameterName");
108        this.dateFormat = (DateFormat) dateFormat.clone();
109        this.prefix = prefix;
110        this.seriesParameterName = seriesParameterName;
111        this.itemParameterName = itemParameterName;
112    }
113
114    /**
115     * Returns a clone of the date format assigned to this URL generator.
116     *
117     * @return The date format (never <code>null</code>).
118     *
119     * @since 1.0.6
120     */
121    public DateFormat getDateFormat() {
122        return (DateFormat) this.dateFormat.clone();
123    }
124
125    /**
126     * Returns the prefix string.
127     *
128     * @return The prefix string (never <code>null</code>).
129     *
130     * @since 1.0.6
131     */
132    public String getPrefix() {
133        return this.prefix;
134    }
135
136    /**
137     * Returns the series parameter name.
138     *
139     * @return The series parameter name (never <code>null</code>).
140     *
141     * @since 1.0.6
142     */
143    public String getSeriesParameterName() {
144        return this.seriesParameterName;
145    }
146
147    /**
148     * Returns the item parameter name.
149     *
150     * @return The item parameter name (never <code>null</code>).
151     *
152     * @since 1.0.6
153     */
154    public String getItemParameterName() {
155        return this.itemParameterName;
156    }
157
158    /**
159     * Generates a URL for a particular item within a series.
160     *
161     * @param dataset  the dataset (<code>null</code> not permitted).
162     * @param series  the series number (zero-based index).
163     * @param item  the item number (zero-based index).
164     *
165     * @return The generated URL.
166     */
167    @Override
168    public String generateURL(XYDataset dataset, int series, int item) {
169        String result = this.prefix;
170        boolean firstParameter = !result.contains("?");
171        Comparable seriesKey = dataset.getSeriesKey(series);
172        if (seriesKey != null) {
173            result += firstParameter ? "?" : "&amp;";
174            try {
175                result += this.seriesParameterName + "=" + URLEncoder.encode(
176                        seriesKey.toString(), "UTF-8");
177            } catch (UnsupportedEncodingException ex) {
178                throw new RuntimeException(ex);
179            }
180            firstParameter = false;
181        }
182
183        long x = (long) dataset.getXValue(series, item);
184        String xValue = this.dateFormat.format(new Date(x));
185        result += firstParameter ? "?" : "&amp;";
186        try {
187            result += this.itemParameterName + "=" + URLEncoder.encode(xValue,
188                    "UTF-8");
189        } catch (UnsupportedEncodingException ex) {
190            throw new RuntimeException(ex);
191        }
192
193        return result;
194    }
195
196    /**
197     * Tests this generator for equality with an arbitrary object.
198     *
199     * @param obj  the object (<code>null</code> permitted).
200     *
201     * @return A boolean.
202     */
203    @Override
204    public boolean equals(Object obj) {
205        if (obj == this) {
206            return true;
207        }
208        if (!(obj instanceof TimeSeriesURLGenerator)) {
209            return false;
210        }
211        TimeSeriesURLGenerator that = (TimeSeriesURLGenerator) obj;
212        if (!this.dateFormat.equals(that.dateFormat)) {
213            return false;
214        }
215        if (!this.itemParameterName.equals(that.itemParameterName)) {
216            return false;
217        }
218        if (!this.prefix.equals(that.prefix)) {
219            return false;
220        }
221        if (!this.seriesParameterName.equals(that.seriesParameterName)) {
222            return false;
223        }
224        return true;
225    }
226
227}