KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > javaparser > JPUtils


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
20 package org.netbeans.modules.tasklist.javaparser;
21
22 import org.netbeans.modules.tasklist.core.TLUtils;
23
24 /**
25  * Utility methods extracted from TLUtil because
26  * non-javaparser usages exists.
27  */

28 public class JPUtils {
29     /** Replace the given symbol on the line with the new symbol - starting
30         roughly at the given column (symbol should be at col or col+1)
31         @param sb Buffer to write into
32         @param text The text to be copied into the buffer, except for
33             the substitution of symbol into newSymbol.
34         @param pos Earliest possible starting position of the symbol
35         @param symbol The symbol which may occur multiple times; we want
36             each reference replaced (provided it's a java identifier - not
37             a prefix or suffix of a larger identifier
38         @param newSymbol The string to replace the old symbol
39         @param bold If true, make the new symbol bold
40         @param underlineBegin If -1, underline the newSymbol starting at
41             this position, ending at underlineEnd.
42         @param underlineEnd Only considererdd if underlineBegin != -1;
43             ending position for underlining started at underlineBegin.
44     */

45     public static void replaceSymbol(StringBuffer JavaDoc sb, String JavaDoc text, int pos, String JavaDoc symbol,
46                                      String JavaDoc newSymbol, boolean bold,
47                                      int underlineBegin, int underlineEnd) {
48         //System.out.println("replace('" + text + "', " + pos + ", '" + symbol + "', '" + newSymbol + "')");
49
if (pos > 0) {
50             // For some compilers, the position is off by 1 so make sure
51
// we catch the earliest possible match
52
pos--;
53         }
54         int from = 0;
55         int symLen = symbol.length();
56         int texLen = text.length();
57         while (true) {
58             int n = text.indexOf(symbol, pos);
59             if (n == -1) {
60                 break;
61             }
62             if ((n+symLen < texLen-1) &&
63                 Character.isJavaIdentifierPart(text.charAt(n+symLen))) {
64                 pos = n+symLen;
65                 continue;
66             }
67
68             for (int i = from; i < n; i++) {
69                 TLUtils.appendHTMLChar(sb, text.charAt(i));
70             }
71             if (bold) {
72                 sb.append("<b>"); // NOI18N
73
}
74             if (underlineBegin != -1) {
75                 for (int i = 0; i < underlineBegin; i++) {
76                     TLUtils.appendHTMLChar(sb, newSymbol.charAt(i));
77                 }
78                 sb.append("<u>"); // NOI18N
79
for (int i = underlineBegin; i < underlineEnd; i++) {
80                     TLUtils.appendHTMLChar(sb, newSymbol.charAt(i));
81                 }
82                 sb.append("</u>"); // NOI18N
83
int nl = newSymbol.length();
84                 for (int i = underlineEnd; i < nl; i++) {
85                     TLUtils.appendHTMLChar(sb, newSymbol.charAt(i));
86                 }
87             } else {
88                 TLUtils.appendHTMLString(sb, newSymbol);
89             }
90             if (bold) {
91                 sb.append("</b>"); // NOI18N
92
}
93             pos = n+symLen;
94             from = pos;
95         }
96         for (int i = from; i < texLen; i++) {
97             TLUtils.appendHTMLChar(sb, text.charAt(i));
98         }
99     }
100 }
101
Popular Tags