1 11 package org.eclipse.jface.internal.text.html; 12 13 import java.io.IOException ; 14 import java.io.Reader ; 15 import java.net.URL ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.SWTError; 19 import org.eclipse.swt.graphics.FontData; 20 import org.eclipse.swt.graphics.RGB; 21 import org.eclipse.swt.widgets.Display; 22 23 24 29 public class HTMLPrinter { 30 31 private static RGB BG_COLOR_RGB= null; 32 33 static { 34 final Display display= Display.getDefault(); 35 if (display != null && !display.isDisposed()) { 36 try { 37 display.asyncExec(new Runnable () { 38 41 public void run() { 42 BG_COLOR_RGB= display.getSystemColor(SWT.COLOR_INFO_BACKGROUND).getRGB(); 43 } 44 }); 45 } catch (SWTError err) { 46 if (err.code != SWT.ERROR_DEVICE_DISPOSED) 48 throw err; 49 } 50 } 51 } 52 53 private HTMLPrinter() { 54 } 55 56 private static String replace(String text, char c, String s) { 57 58 int previous= 0; 59 int current= text.indexOf(c, previous); 60 61 if (current == -1) 62 return text; 63 64 StringBuffer buffer= new StringBuffer (); 65 while (current > -1) { 66 buffer.append(text.substring(previous, current)); 67 buffer.append(s); 68 previous= current + 1; 69 current= text.indexOf(c, previous); 70 } 71 buffer.append(text.substring(previous)); 72 73 return buffer.toString(); 74 } 75 76 public static String convertToHTMLContent(String content) { 77 content= replace(content, '&', "&"); content= replace(content, '"', """); content= replace(content, '<', "<"); return replace(content, '>', ">"); } 82 83 public static String read(Reader rd) { 84 85 StringBuffer buffer= new StringBuffer (); 86 char[] readBuffer= new char[2048]; 87 88 try { 89 int n= rd.read(readBuffer); 90 while (n > 0) { 91 buffer.append(readBuffer, 0, n); 92 n= rd.read(readBuffer); 93 } 94 return buffer.toString(); 95 } catch (IOException x) { 96 } 97 98 return null; 99 } 100 101 public static void insertPageProlog(StringBuffer buffer, int position, RGB bgRGB, URL styleSheetURL) { 102 103 if (bgRGB == null) 104 insertPageProlog(buffer, position, styleSheetURL); 105 else { 106 StringBuffer pageProlog= new StringBuffer (300); 107 108 pageProlog.append("<html>"); 110 appendStyleSheetURL(pageProlog, styleSheetURL); 111 112 pageProlog.append("<body text=\"#000000\" bgcolor=\""); appendColor(pageProlog, bgRGB); 114 pageProlog.append("\">"); 116 buffer.insert(position, pageProlog.toString()); 117 } 118 } 119 public static void insertPageProlog(StringBuffer buffer, int position, RGB bgRGB, String styleSheet) { 120 121 if (bgRGB == null) 122 insertPageProlog(buffer, position, styleSheet); 123 else { 124 StringBuffer pageProlog= new StringBuffer (300); 125 126 pageProlog.append("<html>"); 128 appendStyleSheetURL(pageProlog, styleSheet); 129 130 pageProlog.append("<body text=\"#000000\" bgcolor=\""); appendColor(pageProlog, bgRGB); 132 pageProlog.append("\">"); 134 buffer.insert(position, pageProlog.toString()); 135 } 136 } 137 138 public static void insertStyles(StringBuffer buffer, String [] styles) { 139 if (styles == null || styles.length == 0) 140 return; 141 142 StringBuffer styleBuf= new StringBuffer (10 * styles.length); 143 for (int i= 0; styles != null && i < styles.length; i++) { 144 styleBuf.append(" style=\""); styleBuf.append(styles[i]); 146 styleBuf.append('"'); 147 } 148 149 int index= buffer.indexOf("<body "); if (index != -1) { 153 buffer.insert(index+5, styleBuf); 154 return; 155 } 156 157 index= buffer.indexOf("<body>"); if (index != -1) { 160 buffer.insert(index+5, ' '); 161 buffer.insert(index+6, styleBuf); 162 return; 163 } 164 } 165 166 public static void insertPageProlog(StringBuffer buffer, int position, RGB bgRGB) { 167 if (bgRGB == null) 168 insertPageProlog(buffer, position); 169 else { 170 StringBuffer pageProlog= new StringBuffer (60); 171 pageProlog.append("<html><body text=\"#000000\" bgcolor=\""); appendColor(pageProlog, bgRGB); 173 pageProlog.append("\">"); buffer.insert(position, pageProlog.toString()); 175 } 176 } 177 178 private static void appendStyleSheetURL(StringBuffer buffer, String styleSheet) { 179 if (styleSheet == null) 180 return; 181 182 buffer.append("<head><style CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); buffer.append(styleSheet); 184 buffer.append("</style></head>"); } 186 187 private static void appendStyleSheetURL(StringBuffer buffer, URL styleSheetURL) { 188 if (styleSheetURL == null) 189 return; 190 191 buffer.append("<head>"); 193 buffer.append("<LINK REL=\"stylesheet\" HREF= \""); buffer.append(styleSheetURL); 195 buffer.append("\" CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); 197 buffer.append("</head>"); } 199 200 private static void appendColor(StringBuffer buffer, RGB rgb) { 201 buffer.append('#'); 202 buffer.append(Integer.toHexString(rgb.red)); 203 buffer.append(Integer.toHexString(rgb.green)); 204 buffer.append(Integer.toHexString(rgb.blue)); 205 } 206 207 public static void insertPageProlog(StringBuffer buffer, int position) { 208 insertPageProlog(buffer, position, getBgColor()); 209 } 210 211 public static void insertPageProlog(StringBuffer buffer, int position, URL styleSheetURL) { 212 insertPageProlog(buffer, position, getBgColor(), styleSheetURL); 213 } 214 215 public static void insertPageProlog(StringBuffer buffer, int position, String styleSheet) { 216 insertPageProlog(buffer, position, getBgColor(), styleSheet); 217 } 218 219 private static RGB getBgColor() { 220 if (BG_COLOR_RGB != null) 221 return BG_COLOR_RGB; 222 return new RGB(255,255, 225); 224 } 225 226 public static void addPageProlog(StringBuffer buffer) { 227 insertPageProlog(buffer, buffer.length()); 228 } 229 230 public static void addPageEpilog(StringBuffer buffer) { 231 buffer.append("</font></body></html>"); } 233 234 public static void startBulletList(StringBuffer buffer) { 235 buffer.append("<ul>"); } 237 238 public static void endBulletList(StringBuffer buffer) { 239 buffer.append("</ul>"); } 241 242 public static void addBullet(StringBuffer buffer, String bullet) { 243 if (bullet != null) { 244 buffer.append("<li>"); buffer.append(bullet); 246 buffer.append("</li>"); } 248 } 249 250 public static void addSmallHeader(StringBuffer buffer, String header) { 251 if (header != null) { 252 buffer.append("<h5>"); buffer.append(header); 254 buffer.append("</h5>"); } 256 } 257 258 public static void addParagraph(StringBuffer buffer, String paragraph) { 259 if (paragraph != null) { 260 buffer.append("<p>"); buffer.append(paragraph); 262 } 263 } 264 265 public static void addParagraph(StringBuffer buffer, Reader paragraphReader) { 266 if (paragraphReader != null) 267 addParagraph(buffer, read(paragraphReader)); 268 } 269 270 290 public static String convertTopLevelFont(String styles, FontData fontData) { 291 boolean bold= (fontData.getStyle() & SWT.BOLD) != 0; 292 boolean italic= (fontData.getStyle() & SWT.ITALIC) != 0; 293 294 String size= Integer.toString(fontData.getHeight()) + ("carbon".equals(SWT.getPlatform()) ? "px" : "pt"); 297 String family= "'" + fontData.getName() + "',sans-serif"; styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-size:\\s*)\\d+pt(\\;?.*\\})", "$1" + size + "$2"); styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-weight:\\s*)\\w+(\\;?.*\\})", "$1" + (bold ? "bold" : "normal") + "$2"); styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-style:\\s*)\\w+(\\;?.*\\})", "$1" + (italic ? "italic" : "normal") + "$2"); styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-family:\\s*).+?(;.*\\})", "$1" + family + "$2"); return styles; 303 } 304 } 305 | Popular Tags |