001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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-2016, by Richard Atkinson and Contributors. 031 * 032 * Original Author: Richard Atkinson; 033 * Contributors: David Gilbert (for Object Refinery Limited); 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 * @since 1.0.6 105 */ 106 public DateFormat getDateFormat() { 107 return (DateFormat) this.dateFormat.clone(); 108 } 109 110 /** 111 * Returns the prefix string. 112 * 113 * @return The prefix string (never {@code null}). 114 * 115 * @since 1.0.6 116 */ 117 public String getPrefix() { 118 return this.prefix; 119 } 120 121 /** 122 * Returns the series parameter name. 123 * 124 * @return The series parameter name (never {@code null}). 125 * 126 * @since 1.0.6 127 */ 128 public String getSeriesParameterName() { 129 return this.seriesParameterName; 130 } 131 132 /** 133 * Returns the item parameter name. 134 * 135 * @return The item parameter name (never {@code null}). 136 * 137 * @since 1.0.6 138 */ 139 public String getItemParameterName() { 140 return this.itemParameterName; 141 } 142 143 /** 144 * Generates a URL for a particular item within a series. 145 * 146 * @param dataset the dataset ({@code null} not permitted). 147 * @param series the series number (zero-based index). 148 * @param item the item number (zero-based index). 149 * 150 * @return The generated URL. 151 */ 152 @Override 153 public String generateURL(XYDataset dataset, int series, int item) { 154 String result = this.prefix; 155 boolean firstParameter = !result.contains("?"); 156 Comparable seriesKey = dataset.getSeriesKey(series); 157 if (seriesKey != null) { 158 result += firstParameter ? "?" : "&"; 159 try { 160 result += this.seriesParameterName + "=" + URLEncoder.encode( 161 seriesKey.toString(), "UTF-8"); 162 } catch (UnsupportedEncodingException ex) { 163 throw new RuntimeException(ex); 164 } 165 firstParameter = false; 166 } 167 168 long x = (long) dataset.getXValue(series, item); 169 String xValue = this.dateFormat.format(new Date(x)); 170 result += firstParameter ? "?" : "&"; 171 try { 172 result += this.itemParameterName + "=" + URLEncoder.encode(xValue, 173 "UTF-8"); 174 } catch (UnsupportedEncodingException ex) { 175 throw new RuntimeException(ex); 176 } 177 178 return result; 179 } 180 181 /** 182 * Tests this generator for equality with an arbitrary object. 183 * 184 * @param obj the object ({@code null} permitted). 185 * 186 * @return A boolean. 187 */ 188 @Override 189 public boolean equals(Object obj) { 190 if (obj == this) { 191 return true; 192 } 193 if (!(obj instanceof TimeSeriesURLGenerator)) { 194 return false; 195 } 196 TimeSeriesURLGenerator that = (TimeSeriesURLGenerator) obj; 197 if (!this.dateFormat.equals(that.dateFormat)) { 198 return false; 199 } 200 if (!this.itemParameterName.equals(that.itemParameterName)) { 201 return false; 202 } 203 if (!this.prefix.equals(that.prefix)) { 204 return false; 205 } 206 if (!this.seriesParameterName.equals(that.seriesParameterName)) { 207 return false; 208 } 209 return true; 210 } 211 212}