KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > HtmlPrintContainer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
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 JavaDoc DOCTYPE = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">"; // NOI18N
37
private static final String JavaDoc T_HTML_S = "<html>"; //NOI18N
38
private static final String JavaDoc T_HTML_E = "</html>"; //NOI18N
39
private static final String JavaDoc T_HEAD_S = "<head>"; //NOI18N
40
private static final String JavaDoc T_HEAD_E = "</head>"; //NOI18N
41
private static final String JavaDoc T_BODY_S = "<body>"; //NOI18N
42
private static final String JavaDoc T_BODY_E = "</body>"; //NOI18N
43
private static final String JavaDoc T_TITLE = "<title>{0}</title>"; //NOI18N
44
private static final String JavaDoc T_PRE_S = "<pre>"; //NOI18N
45
private static final String JavaDoc T_PRE_E = "</pre>"; //NOI18N
46
private static final String JavaDoc T_BLOCK_S = "<span class=\"{0}\">"; //NOI18N
47
private static final String JavaDoc T_BLOCK_E = "</span>"; //NOI18N
48
private static final String JavaDoc T_NAME_TABLE = "<table width=\"100%\"><tr><td align=\"center\">{0}</td></tr></table>"; //NOI18N
49
private static final String JavaDoc T_CHARSET = "<meta http-equiv=\"content-type\" content=\"text/html; charset={0}\">"; //NOI18N
50
private static final String JavaDoc T_STYLE_S = "<style type=\"text/css\">"; //NOI18N
51
private static final String JavaDoc T_STYLE_E = "</style>"; //NOI18N
52
private static final String JavaDoc T_COMMENT_S = "<!--"; //NOI18N
53
private static final String JavaDoc T_COMMENT_E = "-->"; //NOI18N
54
private static final String JavaDoc ST_BODY = "body"; //NOI18N
55
private static final String JavaDoc ST_TABLE = "table"; //NOI18N
56
private static final String JavaDoc ST_BEGIN = "{"; //NOI18N
57
private static final String JavaDoc ST_COLOR = "color: "; //NOI18N
58
private static final String JavaDoc ST_BGCOLOR = "background-color: "; //NOI18N
59
private static final String JavaDoc ST_BOLD = "font-weight: bold"; //NOI18N
60
private static final String JavaDoc ST_ITALIC = "font-style: italic"; //NOI18N
61
private static final String JavaDoc ST_SIZE = "font-size: "; //NOI18N
62
private static final String JavaDoc ST_FONT_FAMILY = "font-family: "; //NOI18N
63
private static final String JavaDoc ST_SEPARATOR = "; "; //NOI18N
64
private static final String JavaDoc ST_END = "}"; //NOI18N
65
private static final String JavaDoc EOL = "\n"; //NOI18N
66
private static final String JavaDoc WS = " "; //NOI18N
67
private static final String JavaDoc ESC_LT = "&lt;"; //NOI18N
68
private static final String JavaDoc ESC_GT = "&gt;"; //NOI18N
69
private static final String JavaDoc ESC_AMP = "&amp;"; //NOI18N
70
private static final String JavaDoc ESC_QUOT = "&quot;"; //NOI18N
71
private static final String JavaDoc ESC_APOS = "&apos;"; //NOI18N
72
private static final char ZERO = '0'; //NOI18N
73
private static final char DOT = '.'; //NOI18N
74
private static final String JavaDoc STYLE_PREFIX = "ST"; //NOI18N
75

76     private Color JavaDoc defaultBackgroundColor;
77     private Color JavaDoc defaultForegroundColor;
78     private Color JavaDoc headerBackgroundColor;
79     private Color JavaDoc headerForegroundColor;
80     private Font JavaDoc defaultFont;
81     private StringBuffer JavaDoc buffer;
82     private String JavaDoc fileName;
83     private String JavaDoc shortFileName;
84     private Styles styles;
85     private boolean[] boolHolder;
86     private Map JavaDoc syntaxColoring;
87     private String JavaDoc charset;
88
89     public HtmlPrintContainer () {
90     }
91
92     public final void begin (FileObject fo, Font JavaDoc font, Color JavaDoc fgColor, Color JavaDoc bgColor, Color JavaDoc hfgColor, Color JavaDoc hbgColor, Class JavaDoc kitClass, String JavaDoc charset) {
93         styles = new Styles ();
94         buffer = new StringBuffer JavaDoc();
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 JavaDoc font, Color JavaDoc foreColor, Color JavaDoc backColor) {
108         String JavaDoc text = escape(chars, boolHolder);
109         String JavaDoc 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 JavaDoc[]{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 JavaDoc end () {
125         StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
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 JavaDoc[] {this.shortFileName}));
133         result.append (EOL);
134         result.append (MessageFormat.format (T_CHARSET, new Object JavaDoc[] {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); //NOI18N
152
result.append (EOL);
153         result.append (MessageFormat.format (T_NAME_TABLE, new Object JavaDoc[] {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 JavaDoc escape (char[] buffer, boolean[] boolHolder) {
178         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
179         boolHolder[0] = false;
180         for (int i = 0; i < buffer.length; i++) {
181             if (buffer[i] == '<') { //NOI18N
182
result.append(ESC_LT);
183                 boolHolder[0]|=true;
184             }
185             else if (buffer[i] == '>') { //NOI18N
186
result.append(ESC_GT);
187                 boolHolder[0]|=true;
188             }
189             else if (buffer[i] =='&') { //NOI18N
190
result.append(ESC_AMP);
191                 boolHolder[0]|=true;
192             }
193             else if (buffer[i] =='\'') { //NOI18N
194
result.append(ESC_APOS);
195                 boolHolder[0]|=true;
196             }
197             else if (buffer[i] =='\"') { //NOI18N
198
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 JavaDoc getDefaultColor () {
213         return this.defaultForegroundColor;
214     }
215
216     private Color JavaDoc getDefaultBackgroundColor () {
217         return this.defaultBackgroundColor;
218     }
219
220     private Font JavaDoc getDefaultFont () {
221         return this.defaultFont;
222     }
223
224     private String JavaDoc createStyle (String JavaDoc element, String JavaDoc selector, Font JavaDoc font, Color JavaDoc fg, Color JavaDoc bg, boolean useDefaults) {
225         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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()); //TODO: Locale should go here
260
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 JavaDoc 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 JavaDoc getHtmlColor (Color JavaDoc c) {
281         int r = c.getRed();
282         int g = c.getGreen();
283         int b = c.getBlue();
284         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
285         result.append ("#"); //NOI18N
286
String JavaDoc rs = Integer.toHexString (r);
287         String JavaDoc gs = Integer.toHexString (g);
288         String JavaDoc 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 JavaDoc descs;
303         private int sequence;
304
305         public Styles () {
306             this.descs = new HashMap JavaDoc();
307         }
308
309         private boolean coloringEquals(Coloring coloring, Font JavaDoc f, Color JavaDoc fc, Color JavaDoc bc){
310             if (coloring == null) return false;
311             Font JavaDoc coloringFont = coloring.getFont();
312             if (coloringFont == null) coloringFont = getDefaultFont();
313             Color JavaDoc coloringForeColor = coloring.getForeColor();
314             if (coloringForeColor == null) coloringForeColor = getDefaultColor();
315             Color JavaDoc 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 JavaDoc getStyleId (Font JavaDoc f, Color JavaDoc fc, Color JavaDoc bc) {
322             if (!fc.equals(getDefaultColor()) || !bc.equals(getDefaultBackgroundColor()) || !f.equals(getDefaultFont())) {
323                 StyleDescriptor sd = new StyleDescriptor (f, fc, bc);
324                 String JavaDoc id = (String JavaDoc) this.descs.get (sd);
325                 if (id == null) {
326                     java.util.Set JavaDoc keySet = syntaxColoring.keySet();
327                     Iterator JavaDoc iter = keySet.iterator();
328                     while(iter.hasNext()){
329                         Object JavaDoc key = iter.next();
330                         if (coloringEquals((Coloring)syntaxColoring.get(key), f, fc, bc)){
331                             id = (String JavaDoc) 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; //No style needed
346
}
347         }
348
349         public final String JavaDoc toExternalForm () {
350             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
351             for (Iterator JavaDoc 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 JavaDoc toString () {
359             return this.toExternalForm();
360         }
361
362         private class StyleDescriptor {
363
364             String JavaDoc name;
365             private Font JavaDoc font;
366             private Color JavaDoc fgColor;
367             private Color JavaDoc bgColor;
368
369             public StyleDescriptor (Font JavaDoc font, Color JavaDoc fgColor, Color JavaDoc bgColor) {
370                 this.font = font;
371                 this.fgColor = fgColor;
372                 this.bgColor = bgColor;
373             }
374
375             public final String JavaDoc getName () {
376                 return this.name;
377             }
378
379             public final String JavaDoc toExternalForm () {
380                 return createStyle (null,name,font,fgColor,bgColor,true);
381             }
382
383             public final String JavaDoc toString () {
384                 return this.toExternalForm();
385             }
386
387             public final boolean equals (Object JavaDoc 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