KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > revisions > 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.revisions;
12
13
14 import java.io.IOException JavaDoc;
15 import java.io.Reader JavaDoc;
16 import java.net.URL JavaDoc;
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 /**
25  * Provides a set of convenience methods for creating HTML pages.
26  */

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

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

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

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

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

132             buffer.insert(position, pageProlog.toString());
133         }
134     }
135
136     public static void insertStyles(StringBuffer JavaDoc buffer, String JavaDoc[] styles) {
137         if (styles == null || styles.length == 0)
138             return;
139
140         StringBuffer JavaDoc styleBuf= new StringBuffer JavaDoc(10 * styles.length);
141         for (int i= 0; styles != null && i < styles.length; i++) {
142             styleBuf.append(" style=\""); //$NON-NLS-1$
143
styleBuf.append(styles[i]);
144             styleBuf.append('"');
145         }
146
147         // Find insertion index
148
int index= buffer.indexOf("<body "); //$NON-NLS-1$
149
if (index == -1)
150             return;
151
152         buffer.insert(index+5, styleBuf);
153     }
154
155     public static void insertPageProlog(StringBuffer JavaDoc buffer, int position, RGB bgRGB) {
156         if (bgRGB == null)
157             insertPageProlog(buffer, position);
158         else {
159             StringBuffer JavaDoc pageProlog= new StringBuffer JavaDoc(60);
160             pageProlog.append("<html><body text=\"#000000\" bgcolor=\""); //$NON-NLS-1$
161
appendColor(pageProlog, bgRGB);
162             pageProlog.append("\">"); //$NON-NLS-1$
163
buffer.insert(position, pageProlog.toString());
164         }
165     }
166
167     private static void appendStyleSheetURL(StringBuffer JavaDoc buffer, String JavaDoc styleSheet) {
168         if (styleSheet == null)
169             return;
170         
171         buffer.append("<head><style CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); //$NON-NLS-1$
172
buffer.append(styleSheet);
173         buffer.append("</style></head>"); //$NON-NLS-1$
174
}
175     
176     private static void appendStyleSheetURL(StringBuffer JavaDoc buffer, URL JavaDoc styleSheetURL) {
177         if (styleSheetURL == null)
178             return;
179
180         buffer.append("<head>"); //$NON-NLS-1$
181

182         buffer.append("<LINK REL=\"stylesheet\" HREF= \""); //$NON-NLS-1$
183
buffer.append(styleSheetURL);
184         buffer.append("\" CHARSET=\"ISO-8859-1\" TYPE=\"text/css\">"); //$NON-NLS-1$
185

186         buffer.append("</head>"); //$NON-NLS-1$
187
}
188
189     private static void appendColor(StringBuffer JavaDoc 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 JavaDoc buffer, int position) {
197         insertPageProlog(buffer, position, getBgColor());
198     }
199
200     public static void insertPageProlog(StringBuffer JavaDoc buffer, int position, URL JavaDoc styleSheetURL) {
201         insertPageProlog(buffer, position, getBgColor(), styleSheetURL);
202     }
203     
204     public static void insertPageProlog(StringBuffer JavaDoc buffer, int position, String JavaDoc 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); // RGB value of info bg color on WindowsXP
212

213     }
214
215     public static void addPageProlog(StringBuffer JavaDoc buffer) {
216         insertPageProlog(buffer, buffer.length());
217     }
218
219     public static void addPageEpilog(StringBuffer JavaDoc buffer) {
220         buffer.append("</font></body></html>"); //$NON-NLS-1$
221
}
222
223     public static void startBulletList(StringBuffer JavaDoc buffer) {
224         buffer.append("<ul>"); //$NON-NLS-1$
225
}
226
227     public static void endBulletList(StringBuffer JavaDoc buffer) {
228         buffer.append("</ul>"); //$NON-NLS-1$
229
}
230
231     public static void addBullet(StringBuffer JavaDoc buffer, String JavaDoc bullet) {
232         if (bullet != null) {
233             buffer.append("<li>"); //$NON-NLS-1$
234
buffer.append(bullet);
235             buffer.append("</li>"); //$NON-NLS-1$
236
}
237     }
238
239     public static void addSmallHeader(StringBuffer JavaDoc buffer, String JavaDoc header) {
240         if (header != null) {
241             buffer.append("<h5>"); //$NON-NLS-1$
242
buffer.append(header);
243             buffer.append("</h5>"); //$NON-NLS-1$
244
}
245     }
246
247     public static void addParagraph(StringBuffer JavaDoc buffer, String JavaDoc paragraph) {
248         if (paragraph != null) {
249             buffer.append("<p>"); //$NON-NLS-1$
250
buffer.append(paragraph);
251         }
252     }
253
254     public static void addParagraph(StringBuffer JavaDoc buffer, Reader JavaDoc paragraphReader) {
255         if (paragraphReader != null)
256             addParagraph(buffer, read(paragraphReader));
257     }
258 }
259
Popular Tags