1 11 package org.eclipse.jface.internal.text.revisions; 12 13 14 import java.io.IOException ; 15 import java.io.Reader ; 16 import java.net.URL ; 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.SWTError; 20 import org.eclipse.swt.graphics.RGB; 21 import org.eclipse.swt.widgets.Display; 22 23 24 27 public class HTMLPrinter { 28 29 private static RGB BG_COLOR_RGB= null; 30 31 static { 32 final Display display= Display.getDefault(); 33 if (display != null && !display.isDisposed()) { 34 try { 35 display.asyncExec(new Runnable () { 36 39 public void run() { 40 BG_COLOR_RGB= display.getSystemColor(SWT.COLOR_INFO_BACKGROUND).getRGB(); 41 } 42 }); 43 } catch (SWTError err) { 44 if (err.code != SWT.ERROR_DEVICE_DISPOSED) 46 throw err; 47 } 48 } 49 } 50 51 private HTMLPrinter() { 52 } 53 54 private static String replace(String text, char c, String s) { 55 56 int previous= 0; 57 int current= text.indexOf(c, previous); 58 59 if (current == -1) 60 return text; 61 62 StringBuffer buffer= new StringBuffer (); 63 while (current > -1) { 64 buffer.append(text.substring(previous, current)); 65 buffer.append(s); 66 previous= current + 1; 67 current= text.indexOf(c, previous); 68 } 69 buffer.append(text.substring(previous)); 70 71 return buffer.toString(); 72 } 73 74 public static String convertToHTMLContent(String content) { 75 content= replace(content, '&', "&"); content= replace(content, '"', """); content= replace(content, '<', "<"); return replace(content, '>', ">"); } 80 81 public static String read(Reader rd) { 82 83 StringBuffer buffer= new StringBuffer (); 84 char[] readBuffer= new char[2048]; 85 86 try { 87 int n= rd.read(readBuffer); 88 while (n > 0) { 89 buffer.append(readBuffer, 0, n); 90 n= rd.read(readBuffer); 91 } 92 return buffer.toString(); 93 } catch (IOException x) { 94 } 95 96 return null; 97 } 98 99 public static void insertPageProlog(StringBuffer buffer, int position, RGB bgRGB, URL styleSheetURL) { 100 101 if (bgRGB == null) 102 insertPageProlog(buffer, position, styleSheetURL); 103 else { 104 StringBuffer pageProlog= new StringBuffer (300); 105 106 pageProlog.append("<html>"); 108 appendStyleSheetURL(pageProlog, styleSheetURL); 109 110 pageProlog.append("<body text=\"#000000\" bgcolor=\""); appendColor(pageProlog, bgRGB); 112 pageProlog.append("\">"); 114 buffer.insert(position, pageProlog.toString()); 115 } 116 } 117 public static void insertPageProlog(StringBuffer buffer, int position, RGB bgRGB, String styleSheet) { 118 119 if (bgRGB == null) 120 insertPageProlog(buffer, position, styleSheet); 121 else { 122 StringBuffer pageProlog= new StringBuffer (300); 123 124 pageProlog.append("<html>"); 126 appendStyleSheetURL(pageProlog, styleSheet); 127 128 pageProlog.append("<body text=\"#000000\" bgcolor=\""); appendColor(pageProlog, bgRGB); 130 pageProlog.append("\">"); 132 buffer.insert(position, pageProlog.toString()); 133 } 134 } 135 136 public static void insertStyles(StringBuffer buffer, String [] styles) { 137 if (styles == null || styles.length == 0) 138 return; 139 140 StringBuffer styleBuf= new StringBuffer (10 * styles.length); 141 for (int i= 0; styles != null && i < styles.length; i++) { 142 styleBuf.append(" style=\""); styleBuf.append(styles[i]); 144 styleBuf.append('"'); 145 } 146 147 int index= buffer.indexOf("<body "); if (index == -1) 150 return; 151 152 buffer.insert(index+5, styleBuf); 153 } 154 155 public static void insertPageProlog(StringBuffer buffer, int position, RGB bgRGB) { 156 if (bgRGB == null) 157 insertPageProlog(buffer, position); 158 else { 159 StringBuffer pageProlog= new StringBuffer (60); 160 pageProlog.append("<html><body text=\"#000000\" bgcolor=\""); appendColor(pageProlog, bgRGB); 162 pageProlog.append("\">"); buffer.insert(position, pageProlog.toString()); 164 } 165 } 166 167 private static void appendStyleSheetURL(StringBuffer buffer, String styleSheet) { 168 if (styleSheet == null) 169 return; 170 171 buffer.append("<head><style CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); buffer.append(styleSheet); 173 buffer.append("</style></head>"); } 175 176 private static void appendStyleSheetURL(StringBuffer buffer, URL styleSheetURL) { 177 if (styleSheetURL == null) 178 return; 179 180 buffer.append("<head>"); 182 buffer.append("<LINK REL=\"stylesheet\" HREF= \""); buffer.append(styleSheetURL); 184 buffer.append("\" CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); 186 buffer.append("</head>"); } 188 189 private static void appendColor(StringBuffer buffer, RGB rgb) { 190 buffer.append('#'); 191 buffer.append(Integer.toHexString(rgb.red)); 192 buffer.append(Integer.toHexString(rgb.green)); 193 buffer.append(Integer.toHexString(rgb.blue)); 194 } 195 196 public static void insertPageProlog(StringBuffer buffer, int position) { 197 insertPageProlog(buffer, position, getBgColor()); 198 } 199 200 public static void insertPageProlog(StringBuffer buffer, int position, URL styleSheetURL) { 201 insertPageProlog(buffer, position, getBgColor(), styleSheetURL); 202 } 203 204 public static void insertPageProlog(StringBuffer buffer, int position, String styleSheet) { 205 insertPageProlog(buffer, position, getBgColor(), styleSheet); 206 } 207 208 private static RGB getBgColor() { 209 if (BG_COLOR_RGB != null) 210 return BG_COLOR_RGB; 211 return new RGB(255,255, 225); 213 } 214 215 public static void addPageProlog(StringBuffer buffer) { 216 insertPageProlog(buffer, buffer.length()); 217 } 218 219 public static void addPageEpilog(StringBuffer buffer) { 220 buffer.append("</font></body></html>"); } 222 223 public static void startBulletList(StringBuffer buffer) { 224 buffer.append("<ul>"); } 226 227 public static void endBulletList(StringBuffer buffer) { 228 buffer.append("</ul>"); } 230 231 public static void addBullet(StringBuffer buffer, String bullet) { 232 if (bullet != null) { 233 buffer.append("<li>"); buffer.append(bullet); 235 buffer.append("</li>"); } 237 } 238 239 public static void addSmallHeader(StringBuffer buffer, String header) { 240 if (header != null) { 241 buffer.append("<h5>"); buffer.append(header); 243 buffer.append("</h5>"); } 245 } 246 247 public static void addParagraph(StringBuffer buffer, String paragraph) { 248 if (paragraph != null) { 249 buffer.append("<p>"); buffer.append(paragraph); 251 } 252 } 253 254 public static void addParagraph(StringBuffer buffer, Reader paragraphReader) { 255 if (paragraphReader != null) 256 addParagraph(buffer, read(paragraphReader)); 257 } 258 } 259 | Popular Tags |