KickJava   Java API By Example, From Geeks To Geeks.

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


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  * mkaufman@bea.com - initial API and implementation
10  *
11  *******************************************************************************/

12
13 package org.eclipse.jdt.apt.core.internal.util;
14
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.InputStreamReader JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.jdt.apt.core.internal.AptPlugin;
23 import org.eclipse.jdt.apt.core.internal.env.BuildEnv;
24 import org.eclipse.jdt.core.IBuffer;
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.JavaCore;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.core.ToolFactory;
29 import org.eclipse.jdt.core.compiler.IScanner;
30 import org.eclipse.jdt.core.compiler.ITerminalSymbols;
31 import org.eclipse.jdt.core.compiler.InvalidInputException;
32
33 public class ScannerUtil {
34
35     /**
36      * scan the source code to see if there are any annotation tokens
37      */

38     public static boolean hasAnnotationInstance( IFile f ) {
39         
40         InputStreamReader JavaDoc reader = null;
41         InputStream JavaDoc input = null;
42         try {
43             AnnotationScanner scanner;
44             // If this is a single byte encoding, we can deal directly
45
// with the bytes, which is *much* faster
46
if (SINGLE_BYTE_ENCODINGS.contains(f.getCharset())) {
47                 input = BuildEnv.getInputStream(f);
48                 scanner = new InputStreamAnnotationScanner(input);
49             }
50             else {
51                 reader = BuildEnv.getFileReader( f );
52                 scanner = new ReaderAnnotationScanner(reader);
53             }
54             return scanner.containsAnnotations();
55         }
56         catch( Exception JavaDoc ioe ) {
57             return false;
58         }
59         finally {
60             if (reader != null) { try {reader.close();} catch (IOException JavaDoc ioe) {} }
61             if (input != null) { try {input.close();} catch (IOException JavaDoc ioe) {} }
62         }
63     }
64     
65     
66     public static boolean hasAnnotationInstance( ICompilationUnit cu ) {
67         try {
68             IBuffer b = cu.getBuffer();
69             if ( b == null )
70                 return false;
71             char[] source = b.getCharacters();
72             return hasAnnotationInstance( source );
73         }
74         catch( JavaModelException jme ) {
75             return false;
76         }
77     }
78
79     
80     public static boolean hasAnnotationInstance( char[] source ) {
81         try {
82             if ( source == null )
83                 return false;
84             IScanner scanner = ToolFactory.createScanner(
85                 false, false, false, JavaCore.VERSION_1_5 );
86             scanner.setSource( source );
87             int token = scanner.getNextToken();
88             while ( token != ITerminalSymbols.TokenNameEOF ) {
89                 token = scanner.getNextToken();
90                 if ( token == ITerminalSymbols.TokenNameAT )
91                 {
92                     //
93
// found an @ sign, see if next token is "interface"
94
// @interface is an annotation decl and not an annotation
95
// instance.
96
//
97
token = scanner.getNextToken();
98                     if ( token != ITerminalSymbols.TokenNameinterface )
99                         return true;
100                 }
101             }
102             return false;
103         }
104         catch( InvalidInputException iie )
105         {
106             // lex error, so report false
107
return false;
108         }
109         catch( Exception JavaDoc e )
110         {
111             AptPlugin.log(e, "Failure scanning source: \n" + new String JavaDoc(source)); //$NON-NLS-1$
112
// TODO: deal with this exception
113
return false;
114         }
115     }
116     
117     private static final String JavaDoc[] SINGLE_BYTE_ENCODING_ARRAY = {
118         "ASCII", //$NON-NLS-1$
119
"Cp1250", //$NON-NLS-1$
120
"Cp1251", //$NON-NLS-1$
121
"Cp1252", //$NON-NLS-1$
122
"Cp1253", //$NON-NLS-1$
123
"Cp1254", //$NON-NLS-1$
124
"Cp1257", //$NON-NLS-1$
125
"ISO8859_1", //$NON-NLS-1$
126
"ISO8859_2", //$NON-NLS-1$
127
"ISO8859_4", //$NON-NLS-1$
128
"ISO8859_5", //$NON-NLS-1$
129
"ISO8859_7", //$NON-NLS-1$
130
"ISO8859_9", //$NON-NLS-1$
131
"ISO8859_13", //$NON-NLS-1$
132
"ISO8859_15", //$NON-NLS-1$
133
"UTF8" //$NON-NLS-1$
134
};
135     
136     private static final Set JavaDoc<String JavaDoc> SINGLE_BYTE_ENCODINGS =
137         new HashSet JavaDoc<String JavaDoc>(SINGLE_BYTE_ENCODING_ARRAY.length);
138         
139     static {
140         for (String JavaDoc encoding : SINGLE_BYTE_ENCODING_ARRAY) {
141             SINGLE_BYTE_ENCODINGS.add(encoding);
142         }
143     }
144     
145 }
146
Popular Tags