KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > gsf > GsfHtmlFormatter


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 package org.netbeans.modules.gsf;
20
21 import org.netbeans.api.gsf.ElementKind;
22 import org.netbeans.api.gsf.HtmlFormatter;
23
24
25 /**
26  *
27  * @author Tor Norbye
28  */

29 public class GsfHtmlFormatter extends HtmlFormatter {
30     protected boolean isDeprecated;
31     protected boolean isParameter;
32     protected boolean isType;
33     protected boolean isName;
34
35     protected StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
36
37     public GsfHtmlFormatter() {
38     }
39
40     public void reset() {
41         sb.setLength(0);
42     }
43
44     public void appendHtml(String JavaDoc html) {
45         sb.append(html);
46     }
47
48     public void appendText(String JavaDoc text) {
49         for (int i = 0, n = text.length(); i < n; i++) {
50             char c = text.charAt(i);
51
52             switch (c) {
53             case '<':
54                 sb.append("&lt;"); // NOI18N
55

56                 break;
57
58             case '>':
59                 sb.append("&gt;"); // NOI18N
60

61                 break;
62
63             case '&':
64                 sb.append("&amp;"); // NOI18N
65

66                 break;
67
68             case '"':
69                 sb.append("&quot;"); // NOI18N
70

71                 break;
72
73             case '\'':
74                 sb.append("&apos;"); // NOI18N
75

76                 break;
77
78             default:
79                 sb.append(c);
80             }
81         }
82     }
83
84     public void name(ElementKind kind, boolean start) {
85         assert start != isName;
86         isName = start;
87
88         if (isName) {
89             sb.append("<b>");
90         } else {
91             sb.append("</b>");
92         }
93     }
94
95     public void parameters(boolean start) {
96         assert start != isParameter;
97         isParameter = start;
98
99         if (isParameter) {
100             sb.append("<font color=\"#808080\">");
101         } else {
102             sb.append("</font>");
103         }
104     }
105
106     public void type(boolean start) {
107         assert start != isType;
108         isType = start;
109
110         if (isType) {
111             sb.append("<font color=\"#808080\">");
112         } else {
113             sb.append("</font>");
114         }
115     }
116
117     public void deprecated(boolean start) {
118         assert start != isDeprecated;
119         isDeprecated = start;
120
121         if (isDeprecated) {
122             sb.append("<s>");
123         } else {
124             sb.append("</s>");
125         }
126     }
127
128     public String JavaDoc getText() {
129         assert !isParameter && !isDeprecated && !isName && !isType;
130
131         return sb.toString();
132     }
133 }
134
Popular Tags