001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.gsf;
042:
043: import org.netbeans.modules.gsf.api.ElementKind;
044: import org.netbeans.modules.gsf.api.HtmlFormatter;
045:
046: /**
047: *
048: * @author Tor Norbye
049: */
050: public class GsfHtmlFormatter extends HtmlFormatter {
051: protected boolean isDeprecated;
052: protected boolean isParameter;
053: protected boolean isType;
054: protected boolean isName;
055: protected boolean isEmphasis;
056:
057: protected StringBuilder sb = new StringBuilder();
058:
059: public GsfHtmlFormatter() {
060: }
061:
062: public void reset() {
063: textLength = 0;
064: sb.setLength(0);
065: }
066:
067: public void appendHtml(String html) {
068: sb.append(html);
069: // Not sure what to do about maxLength here... but presumably
070: }
071:
072: public void appendText(String text) {
073: for (int i = 0, n = text.length(); i < n; i++) {
074: if (textLength >= maxLength) {
075: if (textLength == maxLength) {
076: sb.append("...");
077: textLength += 3;
078: }
079: break;
080: }
081: char c = text.charAt(i);
082:
083: switch (c) {
084: case '<':
085: sb.append("<"); // NOI18N
086:
087: break;
088:
089: case '>': // Only ]]> is dangerous
090: if ((i > 1) && (text.charAt(i - 2) == ']')
091: && (text.charAt(i - 1) == ']')) {
092: sb.append(">"); // NOI18N
093: } else {
094: sb.append(c);
095: }
096: break;
097:
098: case '&':
099: sb.append("&"); // NOI18N
100:
101: break;
102:
103: default:
104: sb.append(c);
105: }
106:
107: textLength++;
108: }
109: }
110:
111: public void name(ElementKind kind, boolean start) {
112: assert start != isName;
113: isName = start;
114:
115: if (isName) {
116: sb.append("<b>");
117: } else {
118: sb.append("</b>");
119: }
120: }
121:
122: public void parameters(boolean start) {
123: assert start != isParameter;
124: isParameter = start;
125:
126: if (isParameter) {
127: sb.append("<font color=\"#808080\">");
128: } else {
129: sb.append("</font>");
130: }
131: }
132:
133: @Override
134: public void active(boolean start) {
135: emphasis(start);
136: }
137:
138: public void type(boolean start) {
139: assert start != isType;
140: isType = start;
141:
142: if (isType) {
143: sb.append("<font color=\"#808080\">");
144: } else {
145: sb.append("</font>");
146: }
147: }
148:
149: public void deprecated(boolean start) {
150: assert start != isDeprecated;
151: isDeprecated = start;
152:
153: if (isDeprecated) {
154: sb.append("<s>");
155: } else {
156: sb.append("</s>");
157: }
158: }
159:
160: public String getText() {
161: assert !isParameter && !isDeprecated && !isName && !isType;
162:
163: return sb.toString();
164: }
165:
166: public void emphasis(boolean start) {
167: assert start != isEmphasis;
168: isEmphasis = start;
169:
170: if (isEmphasis) {
171: sb.append("<b>");
172: } else {
173: sb.append("</b>");
174: }
175: }
176:
177: }
|