KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > derived > HTMLPrinter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ant.internal.ui.editor.derived;
12
13
14 import java.io.IOException JavaDoc;
15 import java.io.Reader JavaDoc;
16
17
18 /**
19  * Provides a set of convenience methods for creating HTML pages.
20  *
21  * @see org.eclipse.jdt.internal.ui.text.HTMLPrinter
22  */

23 public class HTMLPrinter {
24             
25     private HTMLPrinter() {
26     }
27     
28     private static String JavaDoc replace(String JavaDoc text, char c, String JavaDoc s) {
29                 
30         int previous= 0;
31         int current= text.indexOf(c, previous);
32         
33         if (current == -1)
34             return text;
35         
36         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
37         while (current > -1) {
38             buffer.append(text.substring(previous, current));
39             buffer.append(s);
40             previous= current + 1;
41             current= text.indexOf(c, previous);
42         }
43         buffer.append(text.substring(previous));
44         
45         return buffer.toString();
46     }
47     
48     public static String JavaDoc convertToHTMLContent(String JavaDoc content) {
49         content= replace(content, '<', "&lt;"); //$NON-NLS-1$
50
return replace(content, '>', "&gt;"); //$NON-NLS-1$
51
}
52     
53     public static String JavaDoc read(Reader JavaDoc rd) {
54         
55         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
56         char[] readBuffer= new char[2048];
57         
58         try {
59             int n= rd.read(readBuffer);
60             while (n > 0) {
61                 buffer.append(readBuffer, 0, n);
62                 n= rd.read(readBuffer);
63             }
64             return buffer.toString();
65         } catch (IOException JavaDoc x) {
66         }
67         
68         return null;
69     }
70
71     public static void insertPageProlog(StringBuffer JavaDoc buffer, int position) {
72         buffer.insert(position, "<html><body text=\"#000000\" bgcolor=\"#FFFF88\"><font size=-1>"); //$NON-NLS-1$
73
}
74     
75     public static void addPageProlog(StringBuffer JavaDoc buffer) {
76         insertPageProlog(buffer, buffer.length());
77     }
78     
79     public static void addPageEpilog(StringBuffer JavaDoc buffer) {
80         buffer.append("</font></body></html>"); //$NON-NLS-1$
81
}
82     
83     public static void startBulletList(StringBuffer JavaDoc buffer) {
84         buffer.append("<ul>"); //$NON-NLS-1$
85
}
86     
87     public static void endBulletList(StringBuffer JavaDoc buffer) {
88         buffer.append("</ul>"); //$NON-NLS-1$
89
}
90     
91     public static void addBullet(StringBuffer JavaDoc buffer, String JavaDoc bullet) {
92         if (bullet != null) {
93             buffer.append("<li>"); //$NON-NLS-1$
94
buffer.append(bullet);
95             buffer.append("</li>"); //$NON-NLS-1$
96
}
97     }
98     
99     public static void addSmallHeader(StringBuffer JavaDoc buffer, String JavaDoc header) {
100         if (header != null) {
101             buffer.append("<h5>"); //$NON-NLS-1$
102
buffer.append(header);
103             buffer.append("</h5>"); //$NON-NLS-1$
104
}
105     }
106     
107     public static void addParagraph(StringBuffer JavaDoc buffer, String JavaDoc paragraph) {
108         if (paragraph != null) {
109             buffer.append("<p>"); //$NON-NLS-1$
110
buffer.append(paragraph);
111         }
112     }
113     
114     public static void addParagraph(StringBuffer JavaDoc buffer, Reader JavaDoc paragraphReader) {
115         if (paragraphReader != null)
116             addParagraph(buffer, read(paragraphReader));
117     }
118 }
119
Popular Tags