KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > codetemplates > ParametrizedTextParser


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.lib.editor.codetemplates;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateParameter;
26 import org.netbeans.lib.editor.util.swing.PositionRegion;
27
28 /**
29  * Parser of the parametrized text.
30  *
31  * @author Miloslav Metelka
32  */

33 public final class ParametrizedTextParser {
34
35     private final CodeTemplateInsertHandler handler;
36
37     private final String JavaDoc parametrizedText;
38
39     private List JavaDoc paramImpls; // filled only when (handler == null)
40

41     /**
42      * Fragments of the parametrized text between the parameters.
43      */

44     private List JavaDoc/*<String>*/ parametrizedTextFragments;
45
46     public ParametrizedTextParser(CodeTemplateInsertHandler handler, String JavaDoc parametrizedText) {
47         this.handler = handler; // may be null for parsing for completion doc item
48
this.parametrizedText = parametrizedText;
49         if (handler == null) { // will build doc for completion item
50
paramImpls = new ArrayList JavaDoc();
51         }
52     }
53     
54     public void parse() {
55         parametrizedTextFragments = new ArrayList JavaDoc();
56
57         StringBuffer JavaDoc textFrag = new StringBuffer JavaDoc();
58         int copyStartIndex = 0;
59         int index = 0; // actual index in parametrizedText
60
boolean atEOT = false;
61         while (!atEOT) {
62             // Search for '${...}'
63
// '$$' interpreted as '$'
64
int dollarIndex = parametrizedText.indexOf('$', index);
65             if (dollarIndex != -1) { // found
66
switch (parametrizedText.charAt(dollarIndex + 1)) { // test char after '$'
67
case '{': // parameter parsing
68
// Store preceding part into fragments
69
textFrag.append(parametrizedText.substring(copyStartIndex, dollarIndex));
70                         copyStartIndex = dollarIndex;
71                         parametrizedTextFragments.add(textFrag.toString());
72                         textFrag.setLength(0);
73
74                         // Create parameter found at the dollarIndex
75
CodeTemplateParameterImpl paramImpl = new CodeTemplateParameterImpl(
76                                 handler, parametrizedText, dollarIndex);
77
78                         int afterClosingBraceIndex = paramImpl.getParametrizedTextEndOffset();
79                         if (afterClosingBraceIndex <= parametrizedText.length()) { // successfully recognized
80
if (handler != null) {
81                                 handler.notifyParameterParsed(paramImpl);
82                             } else { // store params locally
83
paramImpls.add(paramImpl);
84                             }
85                             index = afterClosingBraceIndex;
86                             copyStartIndex = index;
87
88                         } else { // parameter's parsing hit EOT
89
atEOT = true;
90                             break;
91                         }
92                         break;
93                         
94                     case '$': // shrink to single '$'
95
textFrag.append(parametrizedText.substring(copyStartIndex, dollarIndex + 1));
96                         index = dollarIndex + 2;
97                         copyStartIndex = index;
98                         break;
99                         
100                     default: // something else => '$'
101
index = dollarIndex + 1;
102                         break;
103                 }
104
105             } else { // '$' not found till the end of parametrizedText
106
textFrag.append(parametrizedText.substring(copyStartIndex));
107                 parametrizedTextFragments.add(textFrag.toString());
108                 atEOT = true;
109             }
110         }
111     }
112     
113     public String JavaDoc buildInsertText(List JavaDoc/*<CodeTemplateParameter>*/ allParameters) {
114         StringBuffer JavaDoc insertTextBuffer = new StringBuffer JavaDoc(parametrizedText.length());
115         insertTextBuffer.append(parametrizedTextFragments.get(0));
116         int fragIndex = 1;
117         for (Iterator JavaDoc it = allParameters.iterator(); it.hasNext();) {
118             CodeTemplateParameterImpl param = CodeTemplateParameterImpl.get(
119                     (CodeTemplateParameter)it.next());
120             int startOffset = insertTextBuffer.length();
121             insertTextBuffer.append(param.getValue());
122             param.resetPositions(
123                     PositionRegion.createFixedPosition(startOffset),
124                     PositionRegion.createFixedPosition(insertTextBuffer.length())
125             );
126             insertTextBuffer.append(parametrizedTextFragments.get(fragIndex));
127             fragIndex++;
128         }
129         return insertTextBuffer.toString();
130     }
131     
132     private static String JavaDoc toHtmlText(String JavaDoc text) {
133         return CodeTemplateCompletionItem.toHtmlText(text);
134     }
135     
136     public void appendHtmlText(StringBuffer JavaDoc htmlTextBuffer) {
137         htmlTextBuffer.append(toHtmlText((String JavaDoc)parametrizedTextFragments.get(0)));
138         
139         int fragIndex = 1;
140         for (Iterator JavaDoc it = paramImpls.iterator(); it.hasNext();) {
141             CodeTemplateParameterImpl paramImpl = (CodeTemplateParameterImpl)it.next();
142             htmlTextBuffer.append("<b>"); // NOI18N
143
if (CodeTemplateParameter.CURSOR_PARAMETER_NAME.equals(paramImpl.getName())) {
144                 htmlTextBuffer.append("|"); // NOI18N
145
} else {
146                 htmlTextBuffer.append(toHtmlText(paramImpl.getValue()));
147             }
148             htmlTextBuffer.append("</b>"); // NOI18N
149
htmlTextBuffer.append(toHtmlText((String JavaDoc)parametrizedTextFragments.get(fragIndex)));
150             fragIndex++;
151         }
152     }
153
154 }
155
Popular Tags