001/* ====================================================== 002 * JFreeChart : a chart library for the Java(tm) platform 003 * ====================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert and Contributors. 006 * 007 * Project Info: https://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 * ImageMapUtils.java 029 * ------------------ 030 * (C) Copyright 2004-present, by Richard Atkinson and Contributors. 031 * 032 * Original Author: Richard Atkinson; 033 * Contributor(s): David Gilbert; 034 * Fawad Halim - bug 2690293; 035 * 036 */ 037 038package org.jfree.chart.imagemap; 039 040import java.io.IOException; 041import java.io.PrintWriter; 042 043import org.jfree.chart.ChartRenderingInfo; 044import org.jfree.chart.entity.ChartEntity; 045import org.jfree.chart.entity.EntityCollection; 046import org.jfree.chart.util.Args; 047import org.jfree.chart.util.StringUtils; 048 049/** 050 * Collection of utility methods related to producing image maps. 051 * Functionality was originally in {@link org.jfree.chart.ChartUtils}. 052 */ 053public class ImageMapUtils { 054 055 private ImageMapUtils() { 056 // no requirement to instantiate 057 } 058 059 /** 060 * Writes an image map to an output stream. 061 * 062 * @param writer the writer ({@code null} not permitted). 063 * @param name the map name ({@code null} not permitted). 064 * @param info the chart rendering info ({@code null} not permitted). 065 * 066 * @throws java.io.IOException if there are any I/O errors. 067 */ 068 public static void writeImageMap(PrintWriter writer, String name, 069 ChartRenderingInfo info) throws IOException { 070 071 // defer argument checking... 072 writeImageMap(writer, name, info, 073 new StandardToolTipTagFragmentGenerator(), 074 new StandardURLTagFragmentGenerator()); 075 076 } 077 078 /** 079 * Writes an image map to an output stream. 080 * 081 * @param writer the writer ({@code null} not permitted). 082 * @param name the map name ({@code null} not permitted). 083 * @param info the chart rendering info ({@code null} not permitted). 084 * @param useOverLibForToolTips whether to use OverLIB for tooltips 085 * (http://www.bosrup.com/web/overlib/). 086 * 087 * @throws java.io.IOException if there are any I/O errors. 088 */ 089 public static void writeImageMap(PrintWriter writer, 090 String name, ChartRenderingInfo info, 091 boolean useOverLibForToolTips) throws IOException { 092 093 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator; 094 if (useOverLibForToolTips) { 095 toolTipTagFragmentGenerator 096 = new OverLIBToolTipTagFragmentGenerator(); 097 } 098 else { 099 toolTipTagFragmentGenerator 100 = new StandardToolTipTagFragmentGenerator(); 101 } 102 writeImageMap(writer, name, info, 103 toolTipTagFragmentGenerator, 104 new StandardURLTagFragmentGenerator()); 105 106 } 107 108 /** 109 * Writes an image map to an output stream. 110 * 111 * @param writer the writer ({@code null} not permitted). 112 * @param name the map name ({@code null} not permitted). 113 * @param info the chart rendering info ({@code null} not permitted). 114 * @param toolTipTagFragmentGenerator a generator for the HTML fragment 115 * that will contain the tooltip text ({@code null} not permitted 116 * if {@code info} contains tooltip information). 117 * @param urlTagFragmentGenerator a generator for the HTML fragment that 118 * will contain the URL reference ({@code null} not permitted if 119 * {@code info} contains URLs). 120 * 121 * @throws java.io.IOException if there are any I/O errors. 122 */ 123 public static void writeImageMap(PrintWriter writer, String name, 124 ChartRenderingInfo info, 125 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, 126 URLTagFragmentGenerator urlTagFragmentGenerator) 127 throws IOException { 128 129 writer.println(ImageMapUtils.getImageMap(name, info, 130 toolTipTagFragmentGenerator, urlTagFragmentGenerator)); 131 } 132 133 /** 134 * Creates an image map element that complies with the XHTML 1.0 135 * specification. 136 * 137 * @param name the map name ({@code null} not permitted). 138 * @param info the chart rendering info ({@code null} not permitted). 139 * 140 * @return The map element. 141 */ 142 public static String getImageMap(String name, ChartRenderingInfo info) { 143 return ImageMapUtils.getImageMap(name, info, 144 new StandardToolTipTagFragmentGenerator(), 145 new StandardURLTagFragmentGenerator()); 146 } 147 148 /** 149 * Creates an image map element that complies with the XHTML 1.0 150 * specification. 151 * 152 * @param name the map name ({@code null} not permitted). 153 * @param info the chart rendering info ({@code null} not permitted). 154 * @param toolTipTagFragmentGenerator a generator for the HTML fragment 155 * that will contain the tooltip text ({@code null} not permitted 156 * if {@code info} contains tooltip information). 157 * @param urlTagFragmentGenerator a generator for the HTML fragment that 158 * will contain the URL reference ({@code null} not permitted if 159 * {@code info} contains URLs). 160 * 161 * @return The map tag. 162 */ 163 public static String getImageMap(String name, ChartRenderingInfo info, 164 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, 165 URLTagFragmentGenerator urlTagFragmentGenerator) { 166 167 StringBuilder sb = new StringBuilder(); 168 sb.append("<map id=\"").append(htmlEscape(name)); 169 sb.append("\" name=\"").append(htmlEscape(name)).append("\">"); 170 sb.append(StringUtils.getLineSeparator()); 171 EntityCollection entities = info.getEntityCollection(); 172 if (entities != null) { 173 int count = entities.getEntityCount(); 174 for (int i = count - 1; i >= 0; i--) { 175 ChartEntity entity = entities.getEntity(i); 176 if (entity.getToolTipText() != null 177 || entity.getURLText() != null) { 178 String area = entity.getImageMapAreaTag( 179 toolTipTagFragmentGenerator, 180 urlTagFragmentGenerator); 181 if (area.length() > 0) { 182 sb.append(area); 183 sb.append(StringUtils.getLineSeparator()); 184 } 185 } 186 } 187 } 188 sb.append("</map>"); 189 return sb.toString(); 190 191 } 192 193 /** 194 * Returns a string that is equivalent to the input string, but with 195 * special characters converted to HTML escape sequences. 196 * 197 * @param input the string to escape ({@code null} not permitted). 198 * 199 * @return A string with characters escaped. 200 */ 201 public static String htmlEscape(String input) { 202 Args.nullNotPermitted(input, "input"); 203 StringBuilder result = new StringBuilder(); 204 int length = input.length(); 205 for (int i = 0; i < length; i++) { 206 char c = input.charAt(i); 207 if (c == '&') { 208 result.append("&"); 209 } 210 else if (c == '\"') { 211 result.append("""); 212 } 213 else if (c == '<') { 214 result.append("<"); 215 } 216 else if (c == '>') { 217 result.append(">"); 218 } 219 else if (c == '\'') { 220 result.append("'"); 221 } 222 else if (c == '\\') { 223 result.append("\"); 224 } 225 else { 226 result.append(c); 227 } 228 } 229 return result.toString(); 230 } 231 232 /** 233 * Returns a string that is equivalent to the input string, but with 234 * special characters converted to JavaScript escape sequences. 235 * 236 * @param input the string to escape ({@code null} not permitted). 237 * 238 * @return A string with characters escaped. 239 */ 240 public static String javascriptEscape(String input) { 241 Args.nullNotPermitted(input, "input"); 242 StringBuilder result = new StringBuilder(); 243 int length = input.length(); 244 for (int i = 0; i < length; i++) { 245 char c = input.charAt(i); 246 if (c == '\"') { 247 result.append("\\\""); 248 } 249 else if (c == '\'') { 250 result.append("\\'"); 251 } 252 else if (c == '\\') { 253 result.append("\\\\"); 254 } 255 else { 256 result.append(c); 257 } 258 } 259 return result.toString(); 260 } 261}