001/* =================================================== 002 * JFreeSVG : an SVG library for the Java(tm) platform 003 * =================================================== 004 * 005 * (C)opyright 2013, 2014, 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.OutputStreamWriter; 040import java.util.logging.Level; 041import java.util.logging.Logger; 042import org.jfree.graphics2d.Args; 043 044/** 045 * Utility methods related to the {@link SVGGraphics2D} implementation. 046 */ 047public class SVGUtils { 048 049 private SVGUtils() { 050 // no need to instantiate this 051 } 052 053 /** 054 * Returns a new string where any special characters in the source string 055 * have been encoded. 056 * 057 * @param source the source string (<code>null</code> not permitted). 058 * 059 * @return A new string with special characters escaped for XML. 060 * 061 * @since 1.5 062 */ 063 public static String escapeForXML(String source) { 064 Args.nullNotPermitted(source, "source"); 065 StringBuilder sb = new StringBuilder(); 066 for (int i = 0; i < source.length(); i++) { 067 char c = source.charAt(i); 068 switch (c) { 069 case '<' : { 070 sb.append("<"); 071 break; 072 } 073 case '>' : { 074 sb.append(">"); 075 break; 076 } 077 case '&' : { 078 String next = source.substring(i, Math.min(i + 6, 079 source.length())); 080 if (next.startsWith("<") || next.startsWith(">") 081 || next.startsWith("&") 082 || next.startsWith("'") 083 || next.startsWith(""")) { 084 sb.append(c); 085 } else { 086 sb.append("&"); 087 } 088 break; 089 } 090 case '\'' : { 091 sb.append("'"); 092 break; 093 } 094 case '\"' : { 095 sb.append("""); 096 break; 097 } 098 default : sb.append(c); 099 } 100 } 101 return sb.toString(); 102 } 103 104 /** 105 * Writes a file containing the SVG element. 106 * 107 * @param file the file (<code>null</code> not permitted). 108 * @param svgElement the SVG element (<code>null</code> not permitted). 109 * 110 * @throws IOException if there is an I/O problem. 111 * 112 * @since 1.2 113 */ 114 public static void writeToSVG(File file, String svgElement) 115 throws IOException { 116 BufferedWriter writer = null; 117 try { 118 FileOutputStream fos = new FileOutputStream(file); 119 OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); 120 writer = new BufferedWriter(osw); 121 writer.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"); 122 writer.write(svgElement + "\n"); 123 writer.flush(); 124 } finally { 125 try { 126 if (writer != null) { 127 writer.close(); 128 } 129 } catch (IOException ex) { 130 throw new RuntimeException(ex); 131 } 132 } 133 } 134 135 /** 136 * Writes an HTML file containing an SVG element. 137 * 138 * @param file the file. 139 * @param title the title. 140 * @param svgElement the SVG element. 141 * 142 * @throws IOException if there is an I/O problem. 143 */ 144 public static void writeToHTML(File file, String title, String svgElement) 145 throws IOException { 146 BufferedWriter writer = null; 147 try { 148 FileOutputStream fos = new FileOutputStream(file); 149 OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); 150 writer = new BufferedWriter(osw); 151 writer.write("<!DOCTYPE html>\n"); 152 writer.write("<html>\n"); 153 writer.write("<head>\n"); 154 writer.write("<title>" + title + "</title>\n"); 155 writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"); 156 writer.write("</head>\n"); 157 writer.write("<body>\n"); 158 writer.write(svgElement + "\n"); 159 writer.write("</body>\n"); 160 writer.write("</html>\n"); 161 writer.flush(); 162 } finally { 163 try { 164 if (writer != null) { 165 writer.close(); 166 } 167 } catch (IOException ex) { 168 Logger.getLogger(SVGUtils.class.getName()).log(Level.SEVERE, 169 null, ex); 170 } 171 } 172 } 173 174}