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 * 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    /**
056     * Writes an image map to an output stream.
057     *
058     * @param writer  the writer ({@code null} not permitted).
059     * @param name  the map name ({@code null} not permitted).
060     * @param info  the chart rendering info ({@code null} not permitted).
061     *
062     * @throws java.io.IOException if there are any I/O errors.
063     */
064    public static void writeImageMap(PrintWriter writer, String name,
065            ChartRenderingInfo info) throws IOException {
066
067        // defer argument checking...
068        writeImageMap(writer, name, info,
069                new StandardToolTipTagFragmentGenerator(),
070                new StandardURLTagFragmentGenerator());
071
072    }
073
074    /**
075     * Writes an image map to an output stream.
076     *
077     * @param writer  the writer ({@code null} not permitted).
078     * @param name  the map name ({@code null} not permitted).
079     * @param info  the chart rendering info ({@code null} not permitted).
080     * @param useOverLibForToolTips  whether to use OverLIB for tooltips
081     *                               (http://www.bosrup.com/web/overlib/).
082     *
083     * @throws java.io.IOException if there are any I/O errors.
084     */
085    public static void writeImageMap(PrintWriter writer,
086            String name, ChartRenderingInfo info,
087            boolean useOverLibForToolTips) throws IOException {
088
089        ToolTipTagFragmentGenerator toolTipTagFragmentGenerator;
090        if (useOverLibForToolTips) {
091            toolTipTagFragmentGenerator
092                    = new OverLIBToolTipTagFragmentGenerator();
093        }
094        else {
095            toolTipTagFragmentGenerator
096                    = new StandardToolTipTagFragmentGenerator();
097        }
098        writeImageMap(writer, name, info,
099                toolTipTagFragmentGenerator,
100                new StandardURLTagFragmentGenerator());
101
102    }
103
104    /**
105     * Writes an image map to an output stream.
106     *
107     * @param writer  the writer ({@code null} not permitted).
108     * @param name  the map name ({@code null} not permitted).
109     * @param info  the chart rendering info ({@code null} not permitted).
110     * @param toolTipTagFragmentGenerator  a generator for the HTML fragment
111     *     that will contain the tooltip text ({@code null} not permitted
112     *     if {@code info} contains tooltip information).
113     * @param urlTagFragmentGenerator  a generator for the HTML fragment that
114     *     will contain the URL reference ({@code null} not permitted if
115     *     {@code info} contains URLs).
116     *
117     * @throws java.io.IOException if there are any I/O errors.
118     */
119    public static void writeImageMap(PrintWriter writer, String name,
120            ChartRenderingInfo info,
121            ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
122            URLTagFragmentGenerator urlTagFragmentGenerator)
123        throws IOException {
124
125        writer.println(ImageMapUtils.getImageMap(name, info,
126                toolTipTagFragmentGenerator, urlTagFragmentGenerator));
127    }
128
129    /**
130     * Creates an image map element that complies with the XHTML 1.0
131     * specification.
132     *
133     * @param name  the map name ({@code null} not permitted).
134     * @param info  the chart rendering info ({@code null} not permitted).
135     *
136     * @return The map element.
137     */
138    public static String getImageMap(String name, ChartRenderingInfo info) {
139        return ImageMapUtils.getImageMap(name, info,
140                new StandardToolTipTagFragmentGenerator(),
141                new StandardURLTagFragmentGenerator());
142    }
143
144    /**
145     * Creates an image map element that complies with the XHTML 1.0
146     * specification.
147     *
148     * @param name  the map name ({@code null} not permitted).
149     * @param info  the chart rendering info ({@code null} not permitted).
150     * @param toolTipTagFragmentGenerator  a generator for the HTML fragment
151     *     that will contain the tooltip text ({@code null} not permitted
152     *     if {@code info} contains tooltip information).
153     * @param urlTagFragmentGenerator  a generator for the HTML fragment that
154     *     will contain the URL reference ({@code null} not permitted if
155     *     {@code info} contains URLs).
156     *
157     * @return The map tag.
158     */
159    public static String getImageMap(String name, ChartRenderingInfo info,
160            ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
161            URLTagFragmentGenerator urlTagFragmentGenerator) {
162
163        StringBuilder sb = new StringBuilder();
164        sb.append("<map id=\"").append(htmlEscape(name));
165        sb.append("\" name=\"").append(htmlEscape(name)).append("\">");
166        sb.append(StringUtils.getLineSeparator());
167        EntityCollection entities = info.getEntityCollection();
168        if (entities != null) {
169            int count = entities.getEntityCount();
170            for (int i = count - 1; i >= 0; i--) {
171                ChartEntity entity = entities.getEntity(i);
172                if (entity.getToolTipText() != null
173                        || entity.getURLText() != null) {
174                    String area = entity.getImageMapAreaTag(
175                            toolTipTagFragmentGenerator,
176                            urlTagFragmentGenerator);
177                    if (area.length() > 0) {
178                        sb.append(area);
179                        sb.append(StringUtils.getLineSeparator());
180                    }
181                }
182            }
183        }
184        sb.append("</map>");
185        return sb.toString();
186
187    }
188
189    /**
190     * Returns a string that is equivalent to the input string, but with
191     * special characters converted to HTML escape sequences.
192     *
193     * @param input  the string to escape ({@code null} not permitted).
194     *
195     * @return A string with characters escaped.
196     */
197    public static String htmlEscape(String input) {
198        Args.nullNotPermitted(input, "input");
199        StringBuilder result = new StringBuilder();
200        int length = input.length();
201        for (int i = 0; i < length; i++) {
202            char c = input.charAt(i);
203            if (c == '&') {
204                result.append("&amp;");
205            }
206            else if (c == '\"') {
207                result.append("&quot;");
208            }
209            else if (c == '<') {
210                result.append("&lt;");
211            }
212            else if (c == '>') {
213                result.append("&gt;");
214            }
215            else if (c == '\'') {
216                result.append("&#39;");
217            }
218            else if (c == '\\') {
219                result.append("&#092;");
220            }
221            else {
222                result.append(c);
223            }
224        }
225        return result.toString();
226    }
227
228    /**
229     * Returns a string that is equivalent to the input string, but with
230     * special characters converted to JavaScript escape sequences.
231     *
232     * @param input  the string to escape ({@code null} not permitted).
233     *
234     * @return A string with characters escaped.
235     */
236    public static String javascriptEscape(String input) {
237        Args.nullNotPermitted(input, "input");
238        StringBuilder result = new StringBuilder();
239        int length = input.length();
240        for (int i = 0; i < length; i++) {
241            char c = input.charAt(i);
242            if (c == '\"') {
243                result.append("\\\"");
244            }
245            else if (c == '\'') {
246                result.append("\\'");
247            }
248            else if (c == '\\') {
249                result.append("\\\\");
250            }
251            else {
252                result.append(c);
253            }
254        }
255        return result.toString();
256    }
257}