Source for org.jfree.chart.urls.StandardXYURLGenerator

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ---------------------------
  28:  * StandardXYURLGenerator.java
  29:  * ---------------------------
  30:  * (C) Copyright 2002-2007, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributors:     David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: StandardXYURLGenerator.java,v 1.6.2.3 2007/02/20 15:36:23 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
  40:  * 29-Aug-2002 : New constructor and member variables to customise series and 
  41:  *               item parameter names (RA);
  42:  * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  43:  * 23-Mar-2003 : Implemented Serializable (DG);
  44:  * 01-Mar-2004 : Added equals() method (DG);
  45:  * 13-Jan-2005 : Modified for XHTML 1.0 compliance (DG);
  46:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  47:  * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
  48:  *
  49:  */
  50: 
  51: package org.jfree.chart.urls;
  52: 
  53: import java.io.Serializable;
  54: 
  55: import org.jfree.data.xy.XYDataset;
  56: import org.jfree.util.ObjectUtilities;
  57: 
  58: /**
  59:  * A URL generator.
  60:  */
  61: public class StandardXYURLGenerator implements XYURLGenerator, Serializable {
  62:     
  63:     /** For serialization. */
  64:     private static final long serialVersionUID = -1771624523496595382L;
  65:     
  66:     /** The default prefix. */
  67:     public static final String DEFAULT_PREFIX = "index.html";
  68:     
  69:     /** The default series parameter. */
  70:     public static final String DEFAULT_SERIES_PARAMETER = "series";
  71:     
  72:     /** The default item parameter. */
  73:     public static final String DEFAULT_ITEM_PARAMETER = "item";
  74:     
  75:     /** Prefix to the URL */
  76:     private String prefix;
  77: 
  78:     /** Series parameter name to go in each URL */
  79:     private String seriesParameterName;
  80: 
  81:     /** Item parameter name to go in each URL */
  82:     private String itemParameterName;
  83: 
  84:     /**
  85:      * Creates a new default generator.  This constructor is equivalent to
  86:      * calling <code>StandardXYURLGenerator("index.html", "series", "item");
  87:      * </code>.
  88:      */
  89:     public StandardXYURLGenerator() {
  90:         this(DEFAULT_PREFIX, DEFAULT_SERIES_PARAMETER, DEFAULT_ITEM_PARAMETER);
  91:     }
  92: 
  93:     /**
  94:      * Creates a new generator with the specified prefix.  This constructor 
  95:      * is equivalent to calling 
  96:      * <code>StandardXYURLGenerator(prefix, "series", "item");</code>.
  97:      *
  98:      * @param prefix  the prefix to the URL (<code>null</code> not permitted).
  99:      */
 100:     public StandardXYURLGenerator(String prefix) {
 101:         this(prefix, DEFAULT_SERIES_PARAMETER, DEFAULT_ITEM_PARAMETER);
 102:     }
 103: 
 104:     /**
 105:      * Constructor that overrides all the defaults
 106:      *
 107:      * @param prefix  the prefix to the URL (<code>null</code> not permitted).
 108:      * @param seriesParameterName  the name of the series parameter to go in 
 109:      *                             each URL (<code>null</code> not permitted).
 110:      * @param itemParameterName  the name of the item parameter to go in each
 111:      *                           URL (<code>null</code> not permitted).
 112:      */
 113:     public StandardXYURLGenerator(String prefix,
 114:                                   String seriesParameterName,
 115:                                   String itemParameterName) {
 116:         if (prefix == null) {
 117:             throw new IllegalArgumentException("Null 'prefix' argument.");
 118:         }
 119:         if (seriesParameterName == null) {
 120:             throw new IllegalArgumentException(
 121:                     "Null 'seriesParameterName' argument.");
 122:         }
 123:         if (itemParameterName == null) {
 124:             throw new IllegalArgumentException(
 125:                     "Null 'itemParameterName' argument.");
 126:         }
 127:         this.prefix = prefix;
 128:         this.seriesParameterName = seriesParameterName;
 129:         this.itemParameterName = itemParameterName;
 130:     }
 131: 
 132:     /**
 133:      * Generates a URL for a particular item within a series.
 134:      *
 135:      * @param dataset  the dataset.
 136:      * @param series  the series number (zero-based index).
 137:      * @param item  the item number (zero-based index).
 138:      *
 139:      * @return The generated URL.
 140:      */
 141:     public String generateURL(XYDataset dataset, int series, int item) {
 142:         String url = this.prefix;
 143:         boolean firstParameter = url.indexOf("?") == -1;
 144:         url += firstParameter ? "?" : "&amp;";
 145:         url += this.seriesParameterName + "=" + series
 146:                 + "&amp;" + this.itemParameterName + "=" + item;
 147:         return url;
 148:     }
 149: 
 150:     /**
 151:      * Tests this generator for equality with an arbitrary object.
 152:      * 
 153:      * @param obj  the object (<code>null</code> permitted).
 154:      * 
 155:      * @return A boolean.
 156:      */
 157:     public boolean equals(Object obj) {
 158:         if (obj == this) {
 159:             return true;   
 160:         }
 161:         if (!(obj instanceof StandardXYURLGenerator)) {
 162:             return false;
 163:         }
 164:         StandardXYURLGenerator that = (StandardXYURLGenerator) obj;
 165:         if (!ObjectUtilities.equal(that.prefix, this.prefix)) {
 166:             return false;
 167:         }
 168:         if (!ObjectUtilities.equal(that.seriesParameterName, 
 169:                 this.seriesParameterName)) {
 170:             return false;
 171:         }
 172:         if (!ObjectUtilities.equal(that.itemParameterName, 
 173:                 this.itemParameterName)) {
 174:             return false;
 175:         }
 176:         return true;
 177:     }
 178:     
 179: }