KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > editor > completion > NNParser


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.websvc.editor.completion;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import org.netbeans.editor.BaseDocument;
26 import org.netbeans.editor.SyntaxSupport;
27 import org.netbeans.editor.TokenItem;
28 //import org.netbeans.editor.ext.java.JavaSyntaxSupport;
29
import org.netbeans.editor.ext.java.JavaTokenContext;
30 import org.openide.ErrorManager;
31
32 /**
33  * Builds an annotations tree containg NN name and attribs map. Supports nested annotations.
34  *
35  * @author Marek Fukala
36  */

37 public class NNParser {
38     
39     //parser states
40
private static final int INIT = 0;
41     private static final int NN = 1; //@
42
private static final int ERROR = 2;
43     private static final int NNNAME = 3; //@Table
44
private static final int INNN = 4; //@Table(
45
private static final int ATTRNAME = 5; //@Table(name
46
private static final int EQ = 6; //@Table(name=
47
private static final int ATTRVALUE = 7; //@Table(name="hello" || @Table(name=@
48
// Retouche
49
//private JavaSyntaxSupport sup;
50

51     public NNParser(BaseDocument bdoc) {
52         SyntaxSupport ssup = bdoc.getSyntaxSupport();
53 // Retouche
54
// if(!(ssup instanceof JavaSyntaxSupport)) throw new IllegalArgumentException("Only java files are supported!");
55
// sup = (JavaSyntaxSupport)ssup;
56
}
57     
58     public NN parseAnnotation(int offset) {
59         int nnStart = findAnnotationStart(offset);
60         if(nnStart == -1) {
61             return null;
62         } else {
63             return parseAnnotationOnOffset(nnStart);
64         }
65     }
66     
67     /** very simple annotations parser */
68     private NN parseAnnotationOnOffset(int offset) {
69 // Retouche
70
// try {
71
// int parentCount = -1;
72
// int state = INIT;
73
// TokenItem ti = sup.getTokenChain(offset, offset+1);
74
//
75
// assert ti.getTokenID() == JavaTokenContext.ANNOTATION;
76
//
77
// int nnstart = offset;
78
// int nnend = -1;
79
// String nnName = null;
80
// String currAttrName = null;
81
// Object currAttrValue = null;
82
// Map attrs = new HashMap();
83
//
84
// do {
85
// int tid = ti.getTokenID().getNumericID();
86
// //ignore whitespaces
87
// if(tid == JavaTokenContext.WHITESPACE_ID) {
88
// ti = ti.getNext();
89
// continue;
90
// }
91
//
92
// switch(state) {
93
// case INIT:
94
// switch(tid) {
95
// case JavaTokenContext.ANNOTATION_ID:
96
// state = NN;
97
// break;
98
// default:
99
// state = ERROR;
100
// }
101
// break;
102
// case NN:
103
// switch(tid) {
104
// case JavaTokenContext.IDENTIFIER_ID:
105
// state = NNNAME;
106
// nnName = ti.getImage();
107
//// debug("parsing annotation " + nnName);
108
// break;
109
// default:
110
// state = ERROR;
111
// }
112
// break;
113
// case NNNAME:
114
// switch(tid) {
115
// case JavaTokenContext.LPAREN_ID:
116
// state = INNN;
117
// break;
118
// default:
119
// //we need to handle situations when the annotation is not completely valid
120
// //->the cases when the CC is invoked like:
121
// //@Table(type="something", name=|
122
// state = ERROR;
123
// }
124
// break;
125
// case INNN:
126
// switch(tid) {
127
// case JavaTokenContext.IDENTIFIER_ID:
128
// currAttrName = ti.getImage();
129
//// debug("parsing attribute " + currAttrName);
130
// state = ATTRNAME;
131
// break;
132
// default:
133
// state = ERROR;
134
// }
135
// break;
136
// case ATTRNAME:
137
// switch(tid) {
138
// case JavaTokenContext.EQ_ID:
139
// state = EQ;
140
// break;
141
// default:
142
// state = ERROR;
143
// }
144
// break;
145
// case EQ:
146
// switch(tid) {
147
// case JavaTokenContext.STRING_LITERAL_ID:
148
// state = ATTRVALUE;
149
// currAttrValue = Utils.unquote(ti.getImage());
150
//// debug("found atribute value " + currAttrValue);
151
// attrs.put(currAttrName, currAttrValue);
152
// break;
153
// case JavaTokenContext.ANNOTATION_ID:
154
// //nested annotation
155
// NN nestedNN = parseAnnotationOnOffset(ti.getOffset());
156
// attrs.put(currAttrName, nestedNN);
157
// state = ATTRVALUE;
158
// //I need to skip what was parsed in the nested annotation in this parser
159
// ti = sup.getTokenChain(nestedNN.getEndOffset(), nestedNN.getEndOffset()+1);
160
// continue; //next loop
161
// default:
162
//// debug("currently we support just annotation and string attribute value types!");
163
// state = ERROR;
164
// }
165
// case ATTRVALUE:
166
// switch(tid) {
167
// case JavaTokenContext.COMMA_ID:
168
// state = INNN;
169
// break;
170
// case JavaTokenContext.RPAREN_ID:
171
// //we reached end of the annotation
172
// nnend = ti.getOffset() + ti.getImage().length();
173
// NN newNN = new NN(nnName, attrs, nnstart, nnend);
174
// return newNN;
175
// }
176
// break;
177
// }
178
//
179
// if(state == ERROR) break;
180
//
181
// ti = ti.getNext();//get next token
182
//
183
// } while(ti != null);
184
//
185
// //we get to the end of the file without finding NN end - this may happen when the source is broken
186
// if(nnName != null) {
187
// NN newNN = new NN(nnName, attrs, nnstart, offset);
188
// return newNN;
189
// }
190
//
191
// }catch(BadLocationException e) {
192
// ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
193
// }
194
//
195
return null;
196     }
197    
198     
199     private int findAnnotationStart(int offset) {
200 // Retouche
201
// int parentCount = 0;
202
// try {
203
// TokenItem ti = sup.getTokenChain(offset - 1, offset);
204
// while(ti != null) {
205
//// debug(ti);
206
// if(ti.getTokenID() == JavaTokenContext.RPAREN) {
207
// parentCount++;
208
// } else if(ti.getTokenID() == JavaTokenContext.LPAREN) {
209
// parentCount--;
210
// } else if(ti.getTokenID() == JavaTokenContext.ANNOTATION) {
211
// if(parentCount == -1) {
212
//// debug("found outer annotation: " + ti.getImage());
213
// return ti.getOffset();
214
// }
215
// }
216
// ti = ti.getPrevious();
217
// }
218
//
219
// }catch(BadLocationException e) {
220
// ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
221
// }
222
//
223
return -1;
224     }
225        
226 // private static void debug(Object message) {
227
// System.out.println(message.toString());
228
// }
229

230    public class NN {
231         
232         private String JavaDoc name;
233         private Map JavaDoc attributes;
234         private int startOffset, endOffset;
235         
236         public NN(String JavaDoc name, Map JavaDoc attributes, int startOffset, int endOffset) {
237             this.name = name;
238             this.attributes = attributes;
239             this.startOffset = startOffset;
240             this.endOffset = endOffset;
241         }
242         
243         public String JavaDoc getName() {
244             return name;
245         }
246         
247         public Map JavaDoc getAttributes() {
248             return attributes;
249         }
250         
251         public int getStartOffset() {
252             return startOffset;
253         }
254         
255         public int getEndOffset() {
256             return endOffset;
257         }
258         
259         public String JavaDoc toString() {
260             //just debug purposes -> no need for superb performance
261
String JavaDoc text = "@" + getName() + " [" + getStartOffset() + " - " + getEndOffset() + "](";
262             Iterator JavaDoc i = getAttributes().keySet().iterator();
263             while(i.hasNext()) {
264                 String JavaDoc key = (String JavaDoc)i.next();
265                 Object JavaDoc value = getAttributes().get(key);
266                 text+=key+"="+value.toString()+(i.hasNext() ? "," : "");
267             }
268             text+=")";
269             return text;
270         }
271         
272     }
273     
274 }
275
Popular Tags