KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > util > AnnotationScanner


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * jgarms@bea.com - initial API and implementation
10  *
11  *******************************************************************************/

12 package org.eclipse.jdt.apt.core.internal.util;
13
14 import java.io.IOException JavaDoc;
15
16 import static org.eclipse.jdt.apt.core.internal.util.AnnotationScanner.State.*;
17
18 /**
19  * Utility scanner for quickly determining if a file contains annotations
20  */

21 public abstract class AnnotationScanner {
22
23     enum State {
24         NORMAL,
25         SEEN_SLASH,
26         IN_COMMENT,
27         IN_COMMENT_SEEN_STAR,
28         IN_SINGLE_LINE_COMMENT,
29         IN_SINGLE_QUOTE,
30         IN_DOUBLE_QUOTE
31     }
32     
33     public AnnotationScanner() {}
34     
35     public boolean containsAnnotations() throws IOException JavaDoc {
36         State state = NORMAL;
37         
38         // for escaping quotes -- need to ignore the next single character
39
// Since this applies to all states it's handled separately
40
boolean seenBackslash = false;
41         
42         int c = getNext();
43         while (c != -1) {
44             
45             if (seenBackslash) {
46                 // Skip one character
47
seenBackslash = false;
48             }
49             else if (c == '\\') {
50                 // Skip the next character
51
seenBackslash = true;
52             }
53             else {
54                 // Handle the character based on state
55
switch (state) {
56                 
57                 case NORMAL :
58                     if (c == '@')
59                         return true;
60                     if (c == '/') {
61                         state = SEEN_SLASH;
62                     }
63                     else if (c == '\'') {
64                         state = IN_SINGLE_QUOTE;
65                     }
66                     else if (c == '\"') {
67                         state = IN_DOUBLE_QUOTE;
68                     }
69                     break;
70                     
71                 case SEEN_SLASH :
72                     if (c == '*') {
73                         state = IN_COMMENT;
74                     }
75                     else if (c == '/') {
76                         state = IN_SINGLE_LINE_COMMENT;
77                     }
78                     else {
79                         state = NORMAL;
80                     }
81                     break;
82                 
83                 case IN_COMMENT :
84                     if (c == '*') {
85                         state = IN_COMMENT_SEEN_STAR;
86                     }
87                     break;
88                 
89                 case IN_COMMENT_SEEN_STAR :
90                     if (c == '/') {
91                         state = NORMAL;
92                     }
93                     else {
94                         state = IN_COMMENT;
95                     }
96                     break;
97                     
98                 case IN_SINGLE_LINE_COMMENT :
99                     if (c == '\n' || c == '\r') {
100                         state = NORMAL;
101                     }
102                     break;
103                     
104                 case IN_SINGLE_QUOTE :
105                     if (c == '\'') {
106                         state = NORMAL;
107                     }
108                     break;
109                     
110                 case IN_DOUBLE_QUOTE :
111                     if (c == '\"') {
112                         state = NORMAL;
113                     }
114                     break;
115                     
116                 default :
117                     throw new IllegalStateException JavaDoc("Unhandled state: " + state); //$NON-NLS-1$
118
}
119             }
120             c = getNext();
121         }
122         return false;
123     }
124     
125     /**
126      * Returns -1 at the end of the input
127      */

128     protected abstract int getNext() throws IOException JavaDoc;
129 }
130
Popular Tags