Initial import of the Guide contributed to the project byRobert Kraske. Was originall...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / HTMLPrinter.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13 import java.io.IOException;
14 import java.io.Reader;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.RGB;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PlatformUI;
21
22 /**
23  * Provides a set of convenience methods for creating HTML pages.
24  */
25 public class HTMLPrinter {
26
27         private HTMLPrinter() {
28         }
29
30         private static String replace(String text, char c, String s) {
31
32                 int previous = 0;
33                 int current = text.indexOf(c, previous);
34
35                 if (current == -1)
36                         return text;
37
38                 StringBuffer buffer = new StringBuffer();
39                 while (current > -1) {
40                         buffer.append(text.substring(previous, current));
41                         buffer.append(s);
42                         previous = current + 1;
43                         current = text.indexOf(c, previous);
44                 }
45                 buffer.append(text.substring(previous));
46
47                 return buffer.toString();
48         }
49
50         public static String convertToHTMLContent(String content) {
51                 content = replace(content, '<', "&lt;"); //$NON-NLS-1$
52                 return replace(content, '>', "&gt;"); //$NON-NLS-1$
53         }
54
55         public static String read(Reader rd) {
56
57                 StringBuffer buffer = new StringBuffer();
58                 char[] readBuffer = new char[2048];
59
60                 try {
61                         int n = rd.read(readBuffer);
62                         while (n > 0) {
63                                 buffer.append(readBuffer, 0, n);
64                                 n = rd.read(readBuffer);
65                         }
66                         return buffer.toString();
67                 } catch (IOException x) {
68                 }
69
70                 return null;
71         }
72
73         public static void insertPageProlog(StringBuffer buffer, int position,
74                         RGB bgRGB) {
75                 if (bgRGB == null)
76                         insertPageProlog(buffer, position);
77                 else {
78                         StringBuffer pageProlog = new StringBuffer(60);
79                         pageProlog.append("<html><body text=\"#000000\" bgcolor=\""); //$NON-NLS-1$
80                         appendColor(pageProlog, bgRGB);
81                         pageProlog.append("\"><font size=-1>"); //$NON-NLS-1$
82                         buffer.insert(position, pageProlog.toString());
83                 }
84         }
85
86         private static void appendColor(StringBuffer buffer, RGB rgb) {
87                 buffer.append('#');
88                 buffer.append(Integer.toHexString(rgb.red));
89                 buffer.append(Integer.toHexString(rgb.green));
90                 buffer.append(Integer.toHexString(rgb.blue));
91         }
92
93         public static void insertPageProlog(StringBuffer buffer, int position) {
94                 RGB bgColor = null;
95                 IWorkbenchWindow window = PlatformUI.getWorkbench()
96                                 .getActiveWorkbenchWindow();
97                 if (window != null) {
98                         Display display = window.getShell().getDisplay();
99                         if (display != null && !display.isDisposed())
100                                 bgColor = display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)
101                                                 .getRGB();
102                 }
103                 if (bgColor == null)
104                         bgColor = new RGB(255, 255, 225); // RGB value of info bg color on
105                                                                                                 // WindowsXP
106
107                 insertPageProlog(buffer, position, bgColor); //$NON-NLS-1$
108         }
109
110         public static void addPageProlog(StringBuffer buffer) {
111                 insertPageProlog(buffer, buffer.length());
112         }
113
114         public static void addPageEpilog(StringBuffer buffer) {
115                 buffer.append("</font></body></html>"); //$NON-NLS-1$
116         }
117
118         public static void startBulletList(StringBuffer buffer) {
119                 buffer.append("<ul>"); //$NON-NLS-1$
120         }
121
122         public static void endBulletList(StringBuffer buffer) {
123                 buffer.append("</ul>"); //$NON-NLS-1$
124         }
125
126         public static void addBullet(StringBuffer buffer, String bullet) {
127                 if (bullet != null) {
128                         buffer.append("<li>"); //$NON-NLS-1$
129                         buffer.append(bullet);
130                         buffer.append("</li>"); //$NON-NLS-1$
131                 }
132         }
133
134         public static void addSmallHeader(StringBuffer buffer, String header) {
135                 if (header != null) {
136                         buffer.append("<h5>"); //$NON-NLS-1$
137                         buffer.append(header);
138                         buffer.append("</h5>"); //$NON-NLS-1$
139                 }
140         }
141
142         public static void addParagraph(StringBuffer buffer, String paragraph) {
143                 if (paragraph != null) {
144                         buffer.append("<p>"); //$NON-NLS-1$
145                         buffer.append(paragraph);
146                 }
147         }
148
149         public static void addParagraph(StringBuffer buffer, Reader paragraphReader) {
150                 if (paragraphReader != null)
151                         addParagraph(buffer, read(paragraphReader));
152         }
153 }