001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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-present, by Richard Atkinson and Contributors.
031 *
032 * Original Author:  Richard Atkinson;
033 * Contributors:     David Gilbert;
034 *
035 */
036
037package org.jfree.chart.urls;
038
039import java.io.Serializable;
040import java.io.UnsupportedEncodingException;
041import java.net.URLEncoder;
042import java.text.DateFormat;
043import java.util.Date;
044
045import org.jfree.chart.util.Args;
046import org.jfree.data.xy.XYDataset;
047
048/**
049 * A URL generator for time series charts.
050 */
051public class TimeSeriesURLGenerator implements XYURLGenerator, Serializable {
052
053    /** For serialization. */
054    private static final long serialVersionUID = -9122773175671182445L;
055
056    /** A formatter for the date. */
057    private DateFormat dateFormat = DateFormat.getInstance();
058
059    /** Prefix to the URL */
060    private String prefix = "index.html";
061
062    /** Name to use to identify the series */
063    private String seriesParameterName = "series";
064
065    /** Name to use to identify the item */
066    private String itemParameterName = "item";
067
068    /**
069     * Default constructor.
070     */
071    public TimeSeriesURLGenerator() {
072        super();
073    }
074
075    /**
076     * Construct TimeSeriesURLGenerator overriding defaults.
077     *
078     * @param dateFormat  a formatter for the date ({@code null} not
079     *         permitted).
080     * @param prefix  the prefix of the URL ({@code null} not permitted).
081     * @param seriesParameterName  the name of the series parameter in the URL
082     *         ({@code null} not permitted).
083     * @param itemParameterName  the name of the item parameter in the URL
084     *         ({@code null} not permitted).
085     */
086    public TimeSeriesURLGenerator(DateFormat dateFormat, String prefix,
087            String seriesParameterName, String itemParameterName) {
088
089        Args.nullNotPermitted(dateFormat, "dateFormat");
090        Args.nullNotPermitted(prefix, "prefix");
091        Args.nullNotPermitted(seriesParameterName, "seriesParameterName");
092        Args.nullNotPermitted(itemParameterName, "itemParameterName");
093        this.dateFormat = (DateFormat) dateFormat.clone();
094        this.prefix = prefix;
095        this.seriesParameterName = seriesParameterName;
096        this.itemParameterName = itemParameterName;
097    }
098
099    /**
100     * Returns a clone of the date format assigned to this URL generator.
101     *
102     * @return The date format (never {@code null}).
103     */
104    public DateFormat getDateFormat() {
105        return (DateFormat) this.dateFormat.clone();
106    }
107
108    /**
109     * Returns the prefix string.
110     *
111     * @return The prefix string (never {@code null}).
112     */
113    public String getPrefix() {
114        return this.prefix;
115    }
116
117    /**
118     * Returns the series parameter name.
119     *
120     * @return The series parameter name (never {@code null}).
121     */
122    public String getSeriesParameterName() {
123        return this.seriesParameterName;
124    }
125
126    /**
127     * Returns the item parameter name.
128     *
129     * @return The item parameter name (never {@code null}).
130     */
131    public String getItemParameterName() {
132        return this.itemParameterName;
133    }
134
135    /**
136     * Generates a URL for a particular item within a series.
137     *
138     * @param dataset  the dataset ({@code null} not permitted).
139     * @param series  the series number (zero-based index).
140     * @param item  the item number (zero-based index).
141     *
142     * @return The generated URL.
143     */
144    @Override
145    public String generateURL(XYDataset dataset, int series, int item) {
146        String result = this.prefix;
147        boolean firstParameter = !result.contains("?");
148        Comparable seriesKey = dataset.getSeriesKey(series);
149        if (seriesKey != null) {
150            result += firstParameter ? "?" : "&";
151            try {
152                result += this.seriesParameterName + "=" + URLEncoder.encode(
153                        seriesKey.toString(), "UTF-8");
154            } catch (UnsupportedEncodingException ex) {
155                throw new RuntimeException(ex);
156            }
157            firstParameter = false;
158        }
159
160        long x = (long) dataset.getXValue(series, item);
161        String xValue = this.dateFormat.format(new Date(x));
162        result += firstParameter ? "?" : "&";
163        try {
164            result += this.itemParameterName + "=" + URLEncoder.encode(xValue,
165                    "UTF-8");
166        } catch (UnsupportedEncodingException ex) {
167            throw new RuntimeException(ex);
168        }
169
170        return result;
171    }
172
173    /**
174     * Tests this generator for equality with an arbitrary object.
175     *
176     * @param obj  the object ({@code null} permitted).
177     *
178     * @return A boolean.
179     */
180    @Override
181    public boolean equals(Object obj) {
182        if (obj == this) {
183            return true;
184        }
185        if (!(obj instanceof TimeSeriesURLGenerator)) {
186            return false;
187        }
188        TimeSeriesURLGenerator that = (TimeSeriesURLGenerator) obj;
189        if (!this.dateFormat.equals(that.dateFormat)) {
190            return false;
191        }
192        if (!this.itemParameterName.equals(that.itemParameterName)) {
193            return false;
194        }
195        if (!this.prefix.equals(that.prefix)) {
196            return false;
197        }
198        if (!this.seriesParameterName.equals(that.seriesParameterName)) {
199            return false;
200        }
201        return true;
202    }
203
204}