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