KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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.j2ee.persistence.editor.completion;
20
//
21
//import java.util.ArrayList;
22
//import java.util.HashMap;
23
//import java.util.Iterator;
24
//import java.util.List;
25
//import java.util.Map;
26
//import javax.swing.text.BadLocationException;
27
//import org.netbeans.editor.BaseDocument;
28
//import org.netbeans.editor.SyntaxSupport;
29
//import org.netbeans.editor.TokenItem;
30
//import org.netbeans.editor.ext.java.JavaSyntaxSupport;
31
//import org.netbeans.editor.ext.java.JavaTokenContext;
32
//import org.openide.ErrorManager;
33
//
34
///**
35
// * Builds an annotations tree containg NN name and attribs map. Supports nested annotations.
36
// *
37
// * @author Marek Fukala
38
// */
39
// TODO: RETOUCHE
40
//public class NNParser {
41
//
42
// //parser states
43
// private static final int INIT = 0;
44
// private static final int NN = 1; //@
45
// private static final int ERROR = 2;
46
// private static final int NNNAME = 3; //@Table
47
// private static final int INNN = 4; //@Table(
48
// private static final int ATTRNAME = 5; //@Table(name
49
// private static final int EQ = 6; //@Table(name=
50
// private static final int ATTRVALUE = 7; //@Table(name="hello" || @Table(name=@
51
//
52
// private JavaSyntaxSupport sup;
53
//
54
// public NNParser(BaseDocument bdoc) {
55
// SyntaxSupport ssup = bdoc.getSyntaxSupport();
56
// if(!(ssup instanceof JavaSyntaxSupport)) throw new IllegalArgumentException("Only java files are supported!");
57
// sup = (JavaSyntaxSupport)ssup;
58
// }
59
//
60
// public NN parseAnnotation(int offset) {
61
// int nnStart = findAnnotationStart(offset);
62
// if(nnStart == -1) {
63
// return null;
64
// } else {
65
// return parseAnnotationOnOffset(nnStart);
66
// }
67
// }
68
//
69
// /** very simple annotations parser */
70
// private NN parseAnnotationOnOffset(int offset) {
71
// try {
72
// int parentCount = -1;
73
// int state = INIT;
74
// TokenItem ti = sup.getTokenChain(offset, offset+1);
75
//
76
// assert ti.getTokenID() == JavaTokenContext.ANNOTATION;
77
//
78
// int nnstart = offset;
79
// int nnend = -1;
80
// String nnName = null;
81
// String currAttrName = null;
82
// String currAttrValue = null;
83
// List<NNAttr> attrs = new ArrayList<NNAttr>(5);
84
// //helper var
85
// int eqOffset = -1;
86
//
87
// do {
88
// int tid = ti.getTokenID().getNumericID();
89
// //ignore whitespaces
90
// if(tid == JavaTokenContext.WHITESPACE_ID) {
91
// ti = ti.getNext();
92
// continue;
93
// }
94
//
95
// switch(state) {
96
// case INIT:
97
// switch(tid) {
98
// case JavaTokenContext.ANNOTATION_ID:
99
// state = NN;
100
// break;
101
// default:
102
// state = ERROR;
103
// }
104
// break;
105
// case NN:
106
// switch(tid) {
107
// case JavaTokenContext.IDENTIFIER_ID:
108
// state = NNNAME;
109
// nnName = ti.getImage();
110
//// debug("parsing annotation " + nnName);
111
// break;
112
// default:
113
// state = ERROR;
114
// }
115
// break;
116
// case NNNAME:
117
// switch(tid) {
118
// case JavaTokenContext.LPAREN_ID:
119
// state = INNN;
120
// break;
121
// case JavaTokenContext.DOT_ID:
122
// case JavaTokenContext.IDENTIFIER_ID:
123
// //add the token image to the NN name
124
// nnName += ti.getImage();
125
// break;
126
// default:
127
// //we are in NN name, but no parenthesis came
128
// //this mean either error or annotation without parenthesis like @Id
129
// nnend = nnstart + "@".length() + nnName.length();
130
// NN newNN = new NN(nnName, attrs, nnstart, nnend);
131
// return newNN;
132
// }
133
// break;
134
// case INNN:
135
// switch(tid) {
136
// case JavaTokenContext.IDENTIFIER_ID:
137
// currAttrName = ti.getImage();
138
//// debug("parsing attribute " + currAttrName);
139
// state = ATTRNAME;
140
// break;
141
// //case JavaTokenContext.RPAREN_ID:
142
// case JavaTokenContext.COMMA_ID:
143
// //just consume, still in INNN
144
// break;
145
// default:
146
// //we reached end of the annotation, or error
147
// state = ERROR;
148
// break;
149
// }
150
// break;
151
// case ATTRNAME:
152
// switch(tid) {
153
// case JavaTokenContext.EQ_ID:
154
// state = EQ;
155
// eqOffset = ti.getOffset();
156
// break;
157
// default:
158
// state = ERROR;
159
// }
160
// break;
161
// case EQ:
162
// switch(tid) {
163
// case JavaTokenContext.STRING_LITERAL_ID:
164
// state = INNN;
165
// currAttrValue = Utils.unquote(ti.getImage());
166
// attrs.add(new NNAttr(currAttrName, currAttrValue, ti.getOffset(), true));
167
// break;
168
// case JavaTokenContext.IDENTIFIER_ID:
169
// state = INNN;
170
// currAttrValue = ti.getImage();
171
// attrs.add(new NNAttr(currAttrName, currAttrValue, ti.getOffset(), false));
172
// break;
173
// case JavaTokenContext.ANNOTATION_ID:
174
// //nested annotation
175
// NN nestedNN = parseAnnotationOnOffset(ti.getOffset());
176
// attrs.add(new NNAttr(currAttrName, nestedNN, ti.getOffset(), false));
177
// state = INNN;
178
// //I need to skip what was parsed in the nested annotation in this parser
179
// ti = sup.getTokenChain(nestedNN.getEndOffset(), nestedNN.getEndOffset()+1);
180
// continue; //next loop
181
// default:
182
// //ERROR => recover
183
//// debug("found uncompleted attribute " + currAttrName);
184
// //set the start offset of the value to the offset of the equator + 1
185
// attrs.add(new NNAttr(currAttrName, "", eqOffset + 1, false));
186
// state = INNN;
187
// break;
188
// }
189
// }
190
//
191
// //if(state == ERROR) return null;
192
// if(state == ERROR) {
193
// //return what we parser so far to be error recovery as much as possible
194
// nnend = ti.getOffset() + ti.getImage().length();
195
// NN newNN = new NN(nnName, attrs, nnstart, nnend);
196
// return newNN;
197
// }
198
// ti = ti.getNext();//get next token
199
//
200
// } while(ti != null);
201
//
202
// }catch(BadLocationException e) {
203
// ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
204
// }
205
//
206
// return null;
207
// }
208
//
209
//
210
// private int findAnnotationStart(int offset) {
211
// int parentCount = -100;
212
// try {
213
// TokenItem ti = sup.getTokenChain(offset - 1, offset);
214
// while(ti != null) {
215
//// debug(ti);
216
// if(ti.getTokenID() == JavaTokenContext.RPAREN) {
217
// if(parentCount == -100) parentCount = 0;
218
// parentCount++;
219
// } else if(ti.getTokenID() == JavaTokenContext.LPAREN) {
220
// if(parentCount == -100) parentCount = 0;
221
// parentCount--;
222
// } else if(ti.getTokenID() == JavaTokenContext.ANNOTATION) {
223
// if(parentCount == -1 || parentCount == -100) { //needed if offset is not within annotation content
224
//// debug("found outer annotation: " + ti.getImage());
225
// return ti.getOffset();
226
// }
227
// }
228
// ti = ti.getPrevious();
229
// }
230
//
231
// }catch(BadLocationException e) {
232
// ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
233
// }
234
//
235
// return -1;
236
// }
237
//
238
//// private static void debug(Object message) {
239
//// System.out.println(message.toString());
240
//// }
241
//
242
// public class NNAttr {
243
// private String name;
244
// private Object value;
245
// private int valueOffset;
246
// private boolean quoted;
247
//
248
// NNAttr(String name, Object value, int valueOffset, boolean quoted) {
249
// this.name = name;
250
// this.value = value;
251
// this.valueOffset = valueOffset;
252
// this.quoted = quoted;
253
// }
254
//
255
// public String getName() {
256
// return name;
257
// }
258
//
259
// public Object getValue() {
260
// return value;
261
// }
262
//
263
// public int getValueOffset() {
264
// return valueOffset;
265
// }
266
//
267
// public boolean isValueQuoted() {
268
// return quoted;
269
// }
270
//
271
// }
272
//
273
// public class NN {
274
//
275
// private String name;
276
// private List<NNAttr> attributes;
277
// private int startOffset, endOffset;
278
//
279
// public NN(String name, List<NNAttr> attributes, int startOffset, int endOffset) {
280
// this.name = name;
281
// this.attributes = attributes;
282
// this.startOffset = startOffset;
283
// this.endOffset = endOffset;
284
// }
285
//
286
// public String getName() {
287
// return name;
288
// }
289
//
290
// public List<NNAttr> getAttributesList() {
291
// return attributes;
292
// }
293
//
294
// public Map<String,Object> getAttributes() {
295
// HashMap<String,Object> map = new HashMap<String,Object>();
296
// for(NNAttr nnattr : getAttributesList()) {
297
// map.put(nnattr.getName(), nnattr.getValue());
298
// }
299
// return map;
300
// }
301
//
302
// public NNAttr getAttributeForOffset(int offset) {
303
// NNAttr prevnn = null;
304
// for(NNAttr nnattr : getAttributesList()) {
305
// if(nnattr.getValueOffset() >= offset) {
306
// prevnn = nnattr;
307
// break;
308
// }
309
// prevnn = nnattr;
310
// }
311
//
312
// if(prevnn == null) return null;
313
//
314
// int nnEndOffset = prevnn.getValueOffset() + prevnn.getValue().toString().length() + (prevnn.isValueQuoted() ? 2 : 0);
315
// if(nnEndOffset >= offset && prevnn.getValueOffset() <= offset) {
316
// return prevnn;
317
// } else {
318
// return null;
319
// }
320
// }
321
//
322
// public int getStartOffset() {
323
// return startOffset;
324
// }
325
//
326
// public int getEndOffset() {
327
// return endOffset;
328
// }
329
//
330
// public String toString() {
331
// //just debug purposes -> no need for superb performance
332
// String text = "@" + getName() + " [" + getStartOffset() + " - " + getEndOffset() + "](";
333
// for(NNAttr nnattr : getAttributesList()) {
334
// String key = nnattr.getName();
335
// String value = nnattr.getValue().toString();
336
// text+=key+"="+value+ " (" + nnattr.getValueOffset() + ") ,";
337
// }
338
// text = text.substring(0, text.length() -1);
339
// text+=")";
340
// return text;
341
// }
342
//
343
// }
344
//
345
//}
346
Popular Tags