001/* ===================================================
002 * JFreeSVG : an SVG library for the Java(tm) platform
003 * ===================================================
004 * 
005 * (C)opyright 2013-2021, by Object Refinery Limited.  All rights reserved.
006 *
007 * Project Info:  http://www.jfree.org/jfreesvg/index.html
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see visit the
027 * JFreeSVG home page:
028 * 
029 * http://www.jfree.org/jfreesvg
030 * 
031 */
032
033package org.jfree.graphics2d.svg;
034
035import java.io.BufferedWriter;
036import java.io.File;
037import java.io.FileOutputStream;
038import java.io.IOException;
039import java.io.OutputStream;
040import java.io.OutputStreamWriter;
041import java.util.logging.Level;
042import java.util.logging.Logger;
043import java.util.zip.GZIPOutputStream;
044import org.jfree.graphics2d.Args;
045
046/**
047 * Utility methods related to the {@link SVGGraphics2D} implementation.
048 */
049public class SVGUtils {
050    
051    private SVGUtils() {
052        // no need to instantiate this
053    }
054
055    /**
056     * Returns a new string where any special characters in the source string
057     * have been encoded.
058     * 
059     * @param source  the source string ({@code null} not permitted).
060     * 
061     * @return A new string with special characters escaped for XML.
062     * 
063     * @since 1.5
064     */
065    public static String escapeForXML(String source) {
066        Args.nullNotPermitted(source, "source");
067        StringBuilder sb = new StringBuilder();
068        for (int i = 0; i < source.length(); i++) {
069            char c = source.charAt(i);
070            switch (c) {
071                case '<' : {
072                    sb.append("&lt;");
073                    break;
074                } 
075                case '>' : {
076                    sb.append("&gt;");
077                    break;
078                } 
079                case '&' : {
080                    String next = source.substring(i, Math.min(i + 6, 
081                            source.length()));
082                    if (next.startsWith("&lt;") || next.startsWith("&gt;") 
083                            || next.startsWith("&amp;") 
084                            || next.startsWith("&apos;")
085                            || next.startsWith("&quot;")) {
086                        sb.append(c); 
087                    } else {
088                        sb.append("&amp;");
089                    }
090                    break;
091                } 
092                case '\'' : {
093                    sb.append("&apos;");
094                    break;
095                } 
096                case '\"' : {
097                    sb.append("&quot;");
098                    break;
099                } 
100                default : sb.append(c);
101            }
102        }
103        return sb.toString();
104    }
105    
106    /**
107     * Writes a file containing the SVG element.
108     * 
109     * @param file  the file ({@code null} not permitted).
110     * @param svgElement  the SVG element ({@code null} not permitted).
111     * 
112     * @throws IOException if there is an I/O problem.
113     * 
114     * @since 1.2
115     */
116    public static void writeToSVG(File file, String svgElement) 
117            throws IOException {
118        writeToSVG(file, svgElement, false);
119    }
120    
121    /**
122     * Writes a file containing the SVG element.
123     * 
124     * @param file  the file ({@code null} not permitted).
125     * @param svgElement  the SVG element ({@code null} not permitted).
126     * @param zip  compress the output.
127     * 
128     * @throws IOException if there is an I/O problem.
129     * 
130     * @since 3.0
131     */
132    public static void writeToSVG(File file, String svgElement, boolean zip) 
133            throws IOException {    
134        BufferedWriter writer = null;
135        try {
136            OutputStream os = new FileOutputStream(file);
137            if (zip) {
138                os = new GZIPOutputStream(os);
139            }
140            OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
141            writer = new BufferedWriter(osw);
142            writer.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
143            writer.write(svgElement + "\n");
144            writer.flush();
145        } finally {
146            try {
147                if (writer != null) {
148                    writer.close();
149                }
150            } catch (IOException ex) {
151                throw new RuntimeException(ex);
152            }
153        } 
154    }
155    
156    /**
157     * Writes an HTML file containing an SVG element.
158     * 
159     * @param file  the file.
160     * @param title  the title.
161     * @param svgElement  the SVG element.
162     * 
163     * @throws IOException if there is an I/O problem.
164     */
165    public static void writeToHTML(File file, String title, String svgElement) 
166            throws IOException {
167        BufferedWriter writer = null;
168        try {
169            FileOutputStream fos = new FileOutputStream(file);
170            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
171            writer = new BufferedWriter(osw);
172            writer.write("<!DOCTYPE html>\n");
173            writer.write("<html>\n");
174            writer.write("<head>\n");
175            writer.write("<title>" + title + "</title>\n");
176            writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"); 
177            writer.write("</head>\n");
178            writer.write("<body>\n");
179            writer.write(svgElement + "\n");
180            writer.write("</body>\n");
181            writer.write("</html>\n");
182            writer.flush();
183        } finally {
184            try {
185                if (writer != null) {
186                    writer.close();
187                }
188            } catch (IOException ex) {
189                Logger.getLogger(SVGUtils.class.getName()).log(Level.SEVERE,
190                        null, ex);
191            }
192        } 
193    }
194    
195}