KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > html > HTMLPrinter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.internal.text.html;
12
13 import java.io.IOException JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.net.URL JavaDoc;
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 /**
25  * Provides a set of convenience methods for creating HTML pages.
26  * <p>
27  * Moved into this package from <code>org.eclipse.jface.internal.text.revisions</code>.</p>
28  */

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 JavaDoc() {
38                     /*
39                      * @see java.lang.Runnable#run()
40                      */

41                     public void run() {
42                         BG_COLOR_RGB= display.getSystemColor(SWT.COLOR_INFO_BACKGROUND).getRGB();
43                     }
44                 });
45             } catch (SWTError err) {
46                 // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=45294
47
if (err.code != SWT.ERROR_DEVICE_DISPOSED)
48                     throw err;
49             }
50         }
51     }
52
53     private HTMLPrinter() {
54     }
55
56     private static String JavaDoc replace(String JavaDoc text, char c, String JavaDoc s) {
57
58         int previous= 0;
59         int current= text.indexOf(c, previous);
60
61         if (current == -1)
62             return text;
63
64         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
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 JavaDoc convertToHTMLContent(String JavaDoc content) {
77         content= replace(content, '&', "&amp;"); //$NON-NLS-1$
78
content= replace(content, '"', "&quot;"); //$NON-NLS-1$
79
content= replace(content, '<', "&lt;"); //$NON-NLS-1$
80
return replace(content, '>', "&gt;"); //$NON-NLS-1$
81
}
82
83     public static String JavaDoc read(Reader JavaDoc rd) {
84
85         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
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 JavaDoc x) {
96         }
97
98         return null;
99     }
100
101     public static void insertPageProlog(StringBuffer JavaDoc buffer, int position, RGB bgRGB, URL JavaDoc styleSheetURL) {
102
103         if (bgRGB == null)
104             insertPageProlog(buffer, position, styleSheetURL);
105         else {
106             StringBuffer JavaDoc pageProlog= new StringBuffer JavaDoc(300);
107
108             pageProlog.append("<html>"); //$NON-NLS-1$
109

110             appendStyleSheetURL(pageProlog, styleSheetURL);
111
112             pageProlog.append("<body text=\"#000000\" bgcolor=\""); //$NON-NLS-1$
113
appendColor(pageProlog, bgRGB);
114             pageProlog.append("\">"); //$NON-NLS-1$
115

116             buffer.insert(position, pageProlog.toString());
117         }
118     }
119     public static void insertPageProlog(StringBuffer JavaDoc buffer, int position, RGB bgRGB, String JavaDoc styleSheet) {
120         
121         if (bgRGB == null)
122             insertPageProlog(buffer, position, styleSheet);
123         else {
124             StringBuffer JavaDoc pageProlog= new StringBuffer JavaDoc(300);
125             
126             pageProlog.append("<html>"); //$NON-NLS-1$
127

128             appendStyleSheetURL(pageProlog, styleSheet);
129             
130             pageProlog.append("<body text=\"#000000\" bgcolor=\""); //$NON-NLS-1$
131
appendColor(pageProlog, bgRGB);
132             pageProlog.append("\">"); //$NON-NLS-1$
133

134             buffer.insert(position, pageProlog.toString());
135         }
136     }
137
138     public static void insertStyles(StringBuffer JavaDoc buffer, String JavaDoc[] styles) {
139         if (styles == null || styles.length == 0)
140             return;
141
142         StringBuffer JavaDoc styleBuf= new StringBuffer JavaDoc(10 * styles.length);
143         for (int i= 0; styles != null && i < styles.length; i++) {
144             styleBuf.append(" style=\""); //$NON-NLS-1$
145
styleBuf.append(styles[i]);
146             styleBuf.append('"');
147         }
148
149         // Find insertion index
150
// a) within existing body tag with trailing space
151
int index= buffer.indexOf("<body "); //$NON-NLS-1$
152
if (index != -1) {
153             buffer.insert(index+5, styleBuf);
154             return;
155         }
156
157         // b) within existing body tag without attributes
158
index= buffer.indexOf("<body>"); //$NON-NLS-1$
159
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 JavaDoc buffer, int position, RGB bgRGB) {
167         if (bgRGB == null)
168             insertPageProlog(buffer, position);
169         else {
170             StringBuffer JavaDoc pageProlog= new StringBuffer JavaDoc(60);
171             pageProlog.append("<html><body text=\"#000000\" bgcolor=\""); //$NON-NLS-1$
172
appendColor(pageProlog, bgRGB);
173             pageProlog.append("\">"); //$NON-NLS-1$
174
buffer.insert(position, pageProlog.toString());
175         }
176     }
177
178     private static void appendStyleSheetURL(StringBuffer JavaDoc buffer, String JavaDoc styleSheet) {
179         if (styleSheet == null)
180             return;
181         
182         buffer.append("<head><style CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); //$NON-NLS-1$
183
buffer.append(styleSheet);
184         buffer.append("</style></head>"); //$NON-NLS-1$
185
}
186     
187     private static void appendStyleSheetURL(StringBuffer JavaDoc buffer, URL JavaDoc styleSheetURL) {
188         if (styleSheetURL == null)
189             return;
190
191         buffer.append("<head>"); //$NON-NLS-1$
192

193         buffer.append("<LINK REL=\"stylesheet\" HREF= \""); //$NON-NLS-1$
194
buffer.append(styleSheetURL);
195         buffer.append("\" CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); //$NON-NLS-1$
196

197         buffer.append("</head>"); //$NON-NLS-1$
198
}
199
200     private static void appendColor(StringBuffer JavaDoc 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 JavaDoc buffer, int position) {
208         insertPageProlog(buffer, position, getBgColor());
209     }
210
211     public static void insertPageProlog(StringBuffer JavaDoc buffer, int position, URL JavaDoc styleSheetURL) {
212         insertPageProlog(buffer, position, getBgColor(), styleSheetURL);
213     }
214     
215     public static void insertPageProlog(StringBuffer JavaDoc buffer, int position, String JavaDoc 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); // RGB value of info bg color on WindowsXP
223

224     }
225
226     public static void addPageProlog(StringBuffer JavaDoc buffer) {
227         insertPageProlog(buffer, buffer.length());
228     }
229
230     public static void addPageEpilog(StringBuffer JavaDoc buffer) {
231         buffer.append("</font></body></html>"); //$NON-NLS-1$
232
}
233
234     public static void startBulletList(StringBuffer JavaDoc buffer) {
235         buffer.append("<ul>"); //$NON-NLS-1$
236
}
237
238     public static void endBulletList(StringBuffer JavaDoc buffer) {
239         buffer.append("</ul>"); //$NON-NLS-1$
240
}
241
242     public static void addBullet(StringBuffer JavaDoc buffer, String JavaDoc bullet) {
243         if (bullet != null) {
244             buffer.append("<li>"); //$NON-NLS-1$
245
buffer.append(bullet);
246             buffer.append("</li>"); //$NON-NLS-1$
247
}
248     }
249
250     public static void addSmallHeader(StringBuffer JavaDoc buffer, String JavaDoc header) {
251         if (header != null) {
252             buffer.append("<h5>"); //$NON-NLS-1$
253
buffer.append(header);
254             buffer.append("</h5>"); //$NON-NLS-1$
255
}
256     }
257
258     public static void addParagraph(StringBuffer JavaDoc buffer, String JavaDoc paragraph) {
259         if (paragraph != null) {
260             buffer.append("<p>"); //$NON-NLS-1$
261
buffer.append(paragraph);
262         }
263     }
264
265     public static void addParagraph(StringBuffer JavaDoc buffer, Reader JavaDoc paragraphReader) {
266         if (paragraphReader != null)
267             addParagraph(buffer, read(paragraphReader));
268     }
269     
270     /**
271      * Replaces the following style attributes of the font definition of the <code>html</code>
272      * element:
273      * <ul>
274      * <li>font-size</li>
275      * <li>font-weight</li>
276      * <li>font-style</li>
277      * <li>font-family</li>
278      * </ul>
279      * The font's name is used as font family, a <code>sans-serif</code> default font family is
280      * appended for the case that the given font name is not available.
281      * <p>
282      * If the listed font attributes are not contained in the passed style list, nothing happens.
283      * </p>
284      *
285      * @param styles CSS style definitions
286      * @param fontData the font information to use
287      * @return the modified style definitions
288      * @since 3.3
289      */

290     public static String JavaDoc convertTopLevelFont(String JavaDoc styles, FontData fontData) {
291         boolean bold= (fontData.getStyle() & SWT.BOLD) != 0;
292         boolean italic= (fontData.getStyle() & SWT.ITALIC) != 0;
293         
294         // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=155993
295
String JavaDoc size= Integer.toString(fontData.getHeight()) + ("carbon".equals(SWT.getPlatform()) ? "px" : "pt"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
296

297         String JavaDoc family= "'" + fontData.getName() + "',sans-serif"; //$NON-NLS-1$ //$NON-NLS-2$
298
styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-size:\\s*)\\d+pt(\\;?.*\\})", "$1" + size + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
299
styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-weight:\\s*)\\w+(\\;?.*\\})", "$1" + (bold ? "bold" : "normal") + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
300
styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-style:\\s*)\\w+(\\;?.*\\})", "$1" + (italic ? "italic" : "normal") + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
301
styles= styles.replaceFirst("(html\\s*\\{.*(?:\\s|;)font-family:\\s*).+?(;.*\\})", "$1" + family + "$2"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
302
return styles;
303     }
304 }
305
Popular Tags