KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > queries > SourceLevelQuery


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.api.java.queries;
21
22 import java.util.logging.Level JavaDoc;
23 import java.util.logging.Logger JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25 import org.netbeans.spi.java.queries.SourceLevelQueryImplementation;
26 import org.openide.filesystems.FileObject;
27 import org.openide.util.Lookup;
28
29 /**
30  * Returns source level of the given Java source file if it is known.
31  * @see org.netbeans.spi.java.queries.SourceLevelQueryImplementation
32  * @author David Konecny
33  * @since org.netbeans.api.java/1 1.5
34  */

35 public class SourceLevelQuery {
36
37     private static final Logger JavaDoc LOGGER = Logger.getLogger(SourceLevelQuery.class.getName());
38
39     private static final Pattern JavaDoc SOURCE_LEVEL = Pattern.compile("\\d+\\.\\d+");
40
41     private static final Lookup.Result<? extends SourceLevelQueryImplementation> implementations =
42         Lookup.getDefault().lookupResult (SourceLevelQueryImplementation.class);
43
44     private SourceLevelQuery() {
45     }
46
47     /**
48      * Returns source level of the given Java file, Java package or source folder. For acceptable return values
49      * see the documentation of <code>-source</code> command line switch of
50      * <code>javac</code> compiler .
51      * @param javaFile Java source file, Java package or source folder in question
52      * @return source level of the Java file, e.g. "1.3", "1.4" or "1.5", or null
53      * if it is not known
54      */

55     public static String JavaDoc getSourceLevel(FileObject javaFile) {
56         for (SourceLevelQueryImplementation sqi : implementations.allInstances()) {
57             String JavaDoc s = sqi.getSourceLevel(javaFile);
58             if (s != null) {
59                 if (!SOURCE_LEVEL.matcher(s).matches()) {
60                     LOGGER.log(Level.WARNING, "#83994: Ignoring bogus source level {0} for {1} from {2}", new Object JavaDoc[] {s, javaFile, sqi});
61                     continue;
62                 }
63                 LOGGER.log(Level.FINE, "Found source level {0} for {1} from {2}", new Object JavaDoc[] {s, javaFile, sqi});
64                 return s;
65             }
66         }
67         LOGGER.log(Level.FINE, "No source level found for {0}", javaFile);
68         return null;
69     }
70
71 }
72
Popular Tags