1 19 20 package org.netbeans.modules.editor; 21 22 import java.awt.Color ; 23 import java.awt.Font ; 24 import java.text.MessageFormat ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import org.netbeans.editor.Coloring; 29 import org.netbeans.editor.PrintContainer; 30 import org.netbeans.editor.SettingsUtil; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 34 public class HtmlPrintContainer implements PrintContainer { 35 36 private static final String DOCTYPE = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">"; private static final String T_HTML_S = "<html>"; private static final String T_HTML_E = "</html>"; private static final String T_HEAD_S = "<head>"; private static final String T_HEAD_E = "</head>"; private static final String T_BODY_S = "<body>"; private static final String T_BODY_E = "</body>"; private static final String T_TITLE = "<title>{0}</title>"; private static final String T_PRE_S = "<pre>"; private static final String T_PRE_E = "</pre>"; private static final String T_BLOCK_S = "<span class=\"{0}\">"; private static final String T_BLOCK_E = "</span>"; private static final String T_NAME_TABLE = "<table width=\"100%\"><tr><td align=\"center\">{0}</td></tr></table>"; private static final String T_CHARSET = "<meta http-equiv=\"content-type\" content=\"text/html; charset={0}\">"; private static final String T_STYLE_S = "<style type=\"text/css\">"; private static final String T_STYLE_E = "</style>"; private static final String T_COMMENT_S = "<!--"; private static final String T_COMMENT_E = "-->"; private static final String ST_BODY = "body"; private static final String ST_TABLE = "table"; private static final String ST_BEGIN = "{"; private static final String ST_COLOR = "color: "; private static final String ST_BGCOLOR = "background-color: "; private static final String ST_BOLD = "font-weight: bold"; private static final String ST_ITALIC = "font-style: italic"; private static final String ST_SIZE = "font-size: "; private static final String ST_FONT_FAMILY = "font-family: "; private static final String ST_SEPARATOR = "; "; private static final String ST_END = "}"; private static final String EOL = "\n"; private static final String WS = " "; private static final String ESC_LT = "<"; private static final String ESC_GT = ">"; private static final String ESC_AMP = "&"; private static final String ESC_QUOT = """; private static final String ESC_APOS = "'"; private static final char ZERO = '0'; private static final char DOT = '.'; private static final String STYLE_PREFIX = "ST"; 76 private Color defaultBackgroundColor; 77 private Color defaultForegroundColor; 78 private Color headerBackgroundColor; 79 private Color headerForegroundColor; 80 private Font defaultFont; 81 private StringBuffer buffer; 82 private String fileName; 83 private String shortFileName; 84 private Styles styles; 85 private boolean[] boolHolder; 86 private Map syntaxColoring; 87 private String charset; 88 89 public HtmlPrintContainer () { 90 } 91 92 public final void begin (FileObject fo, Font font, Color fgColor, Color bgColor, Color hfgColor, Color hbgColor, Class kitClass, String charset) { 93 styles = new Styles (); 94 buffer = new StringBuffer (); 95 fileName = FileUtil.getFileDisplayName(fo); 96 shortFileName = fo.getNameExt(); 97 boolHolder = new boolean [1]; 98 this.defaultForegroundColor = fgColor; 99 this.defaultBackgroundColor = bgColor; 100 this.defaultFont = font; 101 this.headerForegroundColor = hfgColor; 102 this.headerBackgroundColor = hbgColor; 103 this.syntaxColoring = SettingsUtil.getColoringMap(kitClass, false, true); 104 this.charset = charset; 105 } 106 107 public final void add(char[] chars, Font font, Color foreColor, Color backColor) { 108 String text = escape(chars, boolHolder); 109 String styleId = this.styles.getStyleId (font, foreColor, backColor); 110 boolHolder[0]&= (styleId!=null); 111 if (boolHolder[0]) { 112 buffer.append(MessageFormat.format(T_BLOCK_S,new Object []{styleId})); 113 } 114 buffer.append (text); 115 if (boolHolder[0]) { 116 buffer.append(T_BLOCK_E); 117 } 118 } 119 120 public final void eol() { 121 buffer.append (EOL); 122 } 123 124 public final String end () { 125 StringBuffer result = new StringBuffer (); 126 result.append (DOCTYPE); 127 result.append (EOL); 128 result.append (T_HTML_S); 129 result.append (EOL); 130 result.append (T_HEAD_S); 131 result.append (EOL); 132 result.append (MessageFormat.format (T_TITLE, new Object [] {this.shortFileName})); 133 result.append (EOL); 134 result.append (MessageFormat.format (T_CHARSET, new Object [] {this.charset})); 135 result.append (EOL); 136 result.append (T_STYLE_S); 137 result.append (EOL); 138 result.append (T_COMMENT_S); 139 result.append (EOL); 140 result.append (createStyle(ST_BODY,null,getDefaultFont(),getDefaultColor(),getDefaultBackgroundColor(),false)); 141 result.append (EOL); 142 result.append (createStyle(ST_TABLE,null,getDefaultFont(),headerForegroundColor,headerBackgroundColor,false)); 143 result.append (EOL); 144 result.append (styles.toExternalForm()); 145 result.append (T_COMMENT_E); 146 result.append (EOL); 147 result.append (T_STYLE_E); 148 result.append (EOL); 149 result.append (T_HEAD_E); 150 result.append (EOL); 151 result.append (T_BODY_S); result.append (EOL); 153 result.append (MessageFormat.format (T_NAME_TABLE, new Object [] {this.fileName})); 154 result.append (EOL); 155 result.append (T_PRE_S); 156 result.append (EOL); 157 result.append (this.buffer); 158 result.append (T_PRE_E); 159 result.append (T_BODY_E); 160 result.append (EOL); 161 result.append (T_HTML_E); 162 result.append (EOL); 163 this.styles = null; 164 this.buffer = null; 165 this.fileName = null; 166 this.shortFileName = null; 167 this.defaultBackgroundColor = null; 168 this.defaultForegroundColor = null; 169 this.defaultFont = null; 170 return result.toString(); 171 } 172 173 public final boolean initEmptyLines() { 174 return false; 175 } 176 177 private String escape (char[] buffer, boolean[] boolHolder) { 178 StringBuffer result = new StringBuffer (); 179 boolHolder[0] = false; 180 for (int i = 0; i < buffer.length; i++) { 181 if (buffer[i] == '<') { result.append(ESC_LT); 183 boolHolder[0]|=true; 184 } 185 else if (buffer[i] == '>') { result.append(ESC_GT); 187 boolHolder[0]|=true; 188 } 189 else if (buffer[i] =='&') { result.append(ESC_AMP); 191 boolHolder[0]|=true; 192 } 193 else if (buffer[i] =='\'') { result.append(ESC_APOS); 195 boolHolder[0]|=true; 196 } 197 else if (buffer[i] =='\"') { result.append(ESC_QUOT); 199 boolHolder[0]|=true; 200 } 201 else if (Character.isWhitespace(buffer[i])) { 202 result.append (buffer[i]); 203 } 204 else { 205 result.append (buffer[i]); 206 boolHolder[0]|=true; 207 } 208 } 209 return result.toString(); 210 } 211 212 private Color getDefaultColor () { 213 return this.defaultForegroundColor; 214 } 215 216 private Color getDefaultBackgroundColor () { 217 return this.defaultBackgroundColor; 218 } 219 220 private Font getDefaultFont () { 221 return this.defaultFont; 222 } 223 224 private String createStyle (String element, String selector, Font font, Color fg, Color bg, boolean useDefaults) { 225 StringBuffer sb = new StringBuffer (); 226 if (element != null) { 227 sb.append (element); 228 sb.append (WS); 229 } 230 231 if (selector != null) { 232 sb.append (DOT); 233 sb.append (selector); 234 sb.append (WS); 235 } 236 237 sb.append (ST_BEGIN); 238 boolean first = true; 239 if ((!useDefaults || !fg.equals(getDefaultColor())) && fg != null) { 240 sb.append (ST_COLOR); 241 sb.append (getHtmlColor(fg)); 242 first = false; 243 } 244 245 if ((!useDefaults || !bg.equals (getDefaultBackgroundColor())) && bg != null) { 246 if (!first) { 247 sb.append (ST_SEPARATOR); 248 } 249 sb.append (ST_BGCOLOR); 250 sb.append (getHtmlColor(bg)); 251 first = false; 252 } 253 254 if ((!useDefaults || !font.equals (getDefaultFont())) && font != null) { 255 if (!first) { 256 sb.append (ST_SEPARATOR); 257 } 258 sb.append (ST_FONT_FAMILY); 259 sb.append (font.getFamily()); if (font.isBold()) { 261 sb.append (ST_SEPARATOR); 262 sb.append (ST_BOLD); 263 } 264 if (font.isItalic()) { 265 sb.append (ST_SEPARATOR); 266 sb.append (ST_ITALIC); 267 } 268 Font defaultFont = getDefaultFont(); 269 if (defaultFont!=null && defaultFont.getSize() != font.getSize()) { 270 sb.append (ST_SEPARATOR); 271 sb.append (ST_SIZE); 272 sb.append (String.valueOf(font.getSize())); 273 } 274 275 } 276 sb.append (ST_END); 277 return sb.toString(); 278 } 279 280 private static String getHtmlColor (Color c) { 281 int r = c.getRed(); 282 int g = c.getGreen(); 283 int b = c.getBlue(); 284 StringBuffer result = new StringBuffer (); 285 result.append ("#"); String rs = Integer.toHexString (r); 287 String gs = Integer.toHexString (g); 288 String bs = Integer.toHexString (b); 289 if (r < 0x10) 290 result.append(ZERO); 291 result.append(rs); 292 if (g < 0x10) 293 result.append (ZERO); 294 result.append(gs); 295 if (b < 0x10) 296 result.append (ZERO); 297 result.append(bs); 298 return result.toString(); 299 } 300 301 private class Styles { 302 private Map descs; 303 private int sequence; 304 305 public Styles () { 306 this.descs = new HashMap (); 307 } 308 309 private boolean coloringEquals(Coloring coloring, Font f, Color fc, Color bc){ 310 if (coloring == null) return false; 311 Font coloringFont = coloring.getFont(); 312 if (coloringFont == null) coloringFont = getDefaultFont(); 313 Color coloringForeColor = coloring.getForeColor(); 314 if (coloringForeColor == null) coloringForeColor = getDefaultColor(); 315 Color coloringBackColor = coloring.getBackColor(); 316 if (coloringBackColor == null) coloringBackColor = getDefaultBackgroundColor(); 317 318 return f.equals(coloringFont) && fc.equals(coloringForeColor) && bc.equals(coloringBackColor); 319 } 320 321 public final String getStyleId (Font f, Color fc, Color bc) { 322 if (!fc.equals(getDefaultColor()) || !bc.equals(getDefaultBackgroundColor()) || !f.equals(getDefaultFont())) { 323 StyleDescriptor sd = new StyleDescriptor (f, fc, bc); 324 String id = (String ) this.descs.get (sd); 325 if (id == null) { 326 java.util.Set keySet = syntaxColoring.keySet(); 327 Iterator iter = keySet.iterator(); 328 while(iter.hasNext()){ 329 Object key = iter.next(); 330 if (coloringEquals((Coloring)syntaxColoring.get(key), f, fc, bc)){ 331 id = (String ) key; 332 break; 333 } 334 } 335 336 if (id == null){ 337 id = STYLE_PREFIX + this.sequence++; 338 } 339 sd.name = id; 340 this.descs.put (sd, id); 341 } 342 return id; 343 } 344 else { 345 return null; } 347 } 348 349 public final String toExternalForm () { 350 StringBuffer result = new StringBuffer (); 351 for (Iterator it = descs.keySet().iterator(); it.hasNext();) { 352 result.append (((StyleDescriptor)it.next()).toExternalForm()); 353 result.append (EOL); 354 } 355 return result.toString(); 356 } 357 358 public final String toString () { 359 return this.toExternalForm(); 360 } 361 362 private class StyleDescriptor { 363 364 String name; 365 private Font font; 366 private Color fgColor; 367 private Color bgColor; 368 369 public StyleDescriptor (Font font, Color fgColor, Color bgColor) { 370 this.font = font; 371 this.fgColor = fgColor; 372 this.bgColor = bgColor; 373 } 374 375 public final String getName () { 376 return this.name; 377 } 378 379 public final String toExternalForm () { 380 return createStyle (null,name,font,fgColor,bgColor,true); 381 } 382 383 public final String toString () { 384 return this.toExternalForm(); 385 } 386 387 public final boolean equals (Object object) { 388 if (!(object instanceof StyleDescriptor)) 389 return false; 390 StyleDescriptor od = (StyleDescriptor) object; 391 return coloringEquals(new Coloring(font, fgColor, bgColor), od.font, od.fgColor, od.bgColor); 392 } 393 394 public final int hashCode () { 395 return this.font.hashCode() ^ this.fgColor.hashCode() ^ this.bgColor.hashCode(); 396 } 397 } 398 } 399 } 400 | Popular Tags |