KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > guards > JavaGuardedReader


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.java.guards;
21
22 import java.nio.CharBuffer JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.util.regex.Matcher JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32 import org.netbeans.api.editor.guards.GuardedSection;
33 import org.netbeans.modules.java.guards.GuardTag;
34 import org.netbeans.modules.java.guards.SectionDescriptor;
35
36 /**
37  *
38  * @author Jan Pokorsky
39  */

40 final class JavaGuardedReader {
41
42     /** The prefix of all magic strings */
43     final static String JavaDoc MAGIC_PREFIX = "//GEN-"; // NOI18N
44

45     Pattern JavaDoc magicsAsRE;
46     
47     private static final int LONGEST_ITEM = 10;
48     
49     /** The list of the SectionsDesc. */
50     private final LinkedList JavaDoc<SectionDescriptor> list;
51
52     private final JavaGuardedSectionsProvider provider;
53     
54     /** Creates a new instance of JavaGuardedReader */
55     public JavaGuardedReader(JavaGuardedSectionsProvider provider) {
56         list = new LinkedList JavaDoc<SectionDescriptor>();
57         this.provider = provider;
58     }
59     
60     public List JavaDoc<GuardedSection> getGuardedSections() {
61         return fillSections(list);
62     }
63     
64     public char[] translateToCharBuff(char[] readBuff) {
65         char[] charBuff = new char[readBuff.length];
66         // points to first unused cell in charBuff
67
int charBuffPtr = 0;
68         int stop = readBuff.length - 1;
69
70         // read char
71
int c;
72         // ptr to first not processed char in readBuff
73
int i = 0;
74         // points to a character right after a newline
75
int lastNewLine = 0;
76
77         // final automata
78
int fatpos = 0;
79         final int MAGICLEN = MAGIC_PREFIX.length();
80
81
82         //process newlines so only '\n' appears in the charBuff
83
//count all kinds of newlines - most used will be used on save
84
while (i < stop) {
85             c = readBuff[i];
86             if (c == '\n') {
87                 lastNewLine = charBuffPtr;
88             }
89             charBuff[charBuffPtr++] = readBuff[i++];
90
91             switch (fatpos) {
92             case 0:
93                 if (c == '/') {
94                     fatpos++;
95                 } else {
96                     fatpos = 0;
97                 }
98                 break;
99
100             case 1:
101                 if (c == '/') {
102                     fatpos++;
103                 } else {
104                     fatpos = 0;
105                 }
106                 break;
107
108             case 2:
109                 if (c == 'G') {
110                     fatpos++;
111             } else if (c == '/') {
112             fatpos = 2; // what if /////GEN-xxx?
113
} else {
114                     fatpos = 0;
115                 }
116                 break;
117
118             case 3:
119                 if (c == 'E') {
120                     fatpos++;
121                 } else {
122                     fatpos = 0;
123                 }
124                 break;
125
126             case 4:
127                 if (c == 'N') {
128                     fatpos++;
129                 } else {
130                     fatpos = 0;
131                 }
132                 break;
133
134             case 5:
135                 if (c == '-') {
136                     fatpos++;
137                 } else {
138                     fatpos = 0;
139                 }
140                 break;
141
142             default:
143                 fatpos = 0;
144             }
145
146             // "//GEN-" was reached at this time
147
if (fatpos == MAGICLEN) {
148                 fatpos = 0;
149                 Pattern JavaDoc magics = getMagicsAsRE();
150                 int searchLen = Math.min(LONGEST_ITEM, readBuff.length - i);
151                 CharBuffer JavaDoc chi = CharBuffer.wrap(readBuff, i, searchLen);
152                 Matcher JavaDoc matcher = magics.matcher(chi);
153                 if (matcher.find()) {
154                     String JavaDoc match = matcher.group();
155
156                     charBuffPtr -= MAGICLEN;
157                     i += match.length();
158                     int toNl = toNewLine(i, readBuff);
159                     int sectionSize = MAGICLEN+match.length()+toNl;
160                     
161 // if (!justFilter) {
162
// System.out.println("## MATCH: '" + match.substring(0, match.length() - 1) + "'");
163
SectionDescriptor desc = new SectionDescriptor(
164                                 GuardTag.valueOf(match.substring(0, match.length() - 1)), //XXX catch IAE
165
String.valueOf(readBuff, i, toNl),
166                                 lastNewLine + 1,
167                                 charBuffPtr + sectionSize
168                                 );
169 // new SectionDescriptor(GuardTag.valueOf(match.substring(0, match.length() - 1))); //XXX catch IAE
170
// desc.begin = lastNewLine;
171
// desc.end = charBuffPtr + sectionSize + 1;
172
// desc.name = new String(readBuff, i, toNl);
173
list.add(desc);
174 // }
175
i += toNl;
176                     Arrays.fill(charBuff,charBuffPtr,charBuffPtr+sectionSize,' ');
177                     charBuffPtr+=sectionSize;
178                 }
179             }
180         }
181
182         if (i == stop) {
183 // c = readBuff[i];
184
// switch(c) {
185
// case (int) '\n':
186
//
187
// newLineTypes[NewLine.N.ordinal()]++;
188
//
189
// charBuff[charBuffPtr++] = '\n';
190
//
191
// break;
192
// case (int) '\r':
193
//
194
// newLineTypes[NewLine.R.ordinal()]++;
195
//
196
// charBuff[charBuffPtr++] = '\n';
197
//
198
// break;
199
// default:
200
//
201
charBuff[charBuffPtr++] = readBuff[i++];
202 // }
203
}
204
205         // repair last SectionDesc
206
if (/*!justFilter && */(list.size() > 0)) {
207             SectionDescriptor desc = (SectionDescriptor) list.getLast();
208             if (desc.getEnd() > charBuffPtr) {
209                 desc.setEnd(charBuffPtr);
210             }
211         }
212         
213         char[] res;
214         if (charBuffPtr != charBuff.length) {
215             res = new char[charBuffPtr];
216             System.arraycopy(charBuff, 0, res, 0, charBuffPtr);
217         } else {
218             res = charBuff;
219         }
220         return charBuff;
221     }
222
223     /** @return searching engine for magics */
224     final Pattern JavaDoc getMagicsAsRE() {
225         if (magicsAsRE == null) {
226             magicsAsRE = Pattern.compile(makeOrRegexp());
227         }
228         return magicsAsRE;
229     }
230
231     /** Makes or regular expression for magics */
232     final String JavaDoc makeOrRegexp() {
233         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(100);
234 // final int len = MAGIC_PREFIX.length();
235
for (GuardTag t: GuardTag.values()) {
236             sb.append(t.name() + ':');
237             sb.append('|');
238         }
239
240         return sb.substring(0, sb.length() - 1);
241     }
242
243     /** Searches for newline from i */
244     static int toNewLine(int i, char[] readBuff) {
245         int c;
246         int counter = i;
247         final int len = readBuff.length;
248         while (counter < len) {
249             c = readBuff[counter++];
250             if (c == '\r' || c == '\n') {
251                 counter--;
252                 break;
253             }
254         }
255
256         return counter - i;
257     }
258
259     /** Takes the section descriptors from the GuardedReader and
260     * fills the table 'sections', also marks as guarded all sections
261     * in the given document.
262     * @param is Where to take the guarded section descriptions.
263     * @param doc Where to mark guarded.
264     */

265     List JavaDoc<GuardedSection> fillSections(List JavaDoc<SectionDescriptor> descs) {
266         SectionDescriptor descBegin = null;
267         List JavaDoc<GuardedSection> sections = new ArrayList JavaDoc<GuardedSection>(descs.size());
268         
269         for (SectionDescriptor descCurrent: descs) {
270             try {
271                 GuardedSection sect = null;
272                 switch (descCurrent.getType()) {
273                 case LINE:
274                     sect = provider.createSimpleSection(
275                             descCurrent.getName(),
276                             descCurrent.getBegin(),
277                             descCurrent.getEnd()
278                             );
279                     break;
280
281                 case BEGIN:
282                 case HEADER:
283                 case FIRST:
284                     descBegin = descCurrent;
285                     break;
286
287                 case HEADEREND:
288                     if ((descBegin != null) &&
289                             ((descBegin.getType() == GuardTag.HEADER) || (descBegin.getType() == GuardTag.FIRST)) &&
290                             (descCurrent.getName().equals(descBegin.getName()))
291                        ) {
292                         descBegin.setEnd(descCurrent.getEnd());
293                     }
294                     else {
295                         //SYNTAX ERROR - ignore it.
296
descBegin = null;
297                     }
298                     break;
299
300                 case END:
301                 case LAST:
302                     if ((descBegin != null) && (descBegin.getName().equals(descCurrent.getName()))) {
303                         if ((descBegin.getType() == GuardTag.BEGIN) && (descCurrent.getType() == GuardTag.END)) {
304                             // simple section
305
sect = provider.createSimpleSection(
306                                     descCurrent.getName(),
307                                     descBegin.getBegin(), descCurrent.getEnd()
308                                     );
309                             break;
310                         }
311                         if (((descBegin.getType() == GuardTag.FIRST) && (descCurrent.getType() == GuardTag.LAST)) ||
312                                 ((descBegin.getType() == GuardTag.HEADER) && (descCurrent.getType() == GuardTag.END))) {
313                                 // interior section
314
sect = provider.createInteriorSection(
315                                         descCurrent.getName(),
316                                         descBegin.getBegin(), descBegin.getEnd(),
317                                         descCurrent.getBegin(), descCurrent.getEnd()
318                                         );
319                             break;
320                         }
321                     }
322                     //SYNTAX ERROR - ignore it.
323
descBegin = null;
324                     break;
325                 }
326
327                 if (sect != null) {
328                     sections.add(sect);
329                 }
330             } catch (BadLocationException JavaDoc ex) {
331                 Logger.getLogger(JavaGuardedReader.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
332             }
333         }
334         
335         return sections;
336     }
337     
338 // static String magic(GuardTag tag) {
339
// return MAGIC_PREFIX + tag.name() + ':';
340
// }
341
}
342
Popular Tags