001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.gsfpath.api.queries;
043:
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046: import java.util.regex.Pattern;
047: import org.netbeans.modules.gsfpath.spi.queries.SourceLevelQueryImplementation;
048: import org.openide.filesystems.FileObject;
049: import org.openide.util.Lookup;
050:
051: /**
052: * Returns source level of the given Java source file if it is known.
053: * @see org.netbeans.modules.gsfpath.spi.queries.SourceLevelQueryImplementation
054: * @author David Konecny
055: * @since org.netbeans.modules.gsfpath.api/1 1.5
056: */
057: public class SourceLevelQuery {
058:
059: private static final Logger LOGGER = Logger
060: .getLogger(SourceLevelQuery.class.getName());
061:
062: private static final Pattern SOURCE_LEVEL = Pattern
063: .compile("\\d+\\.\\d+");
064:
065: private static final Lookup.Result<? extends SourceLevelQueryImplementation> implementations = Lookup
066: .getDefault().lookupResult(
067: SourceLevelQueryImplementation.class);
068:
069: private SourceLevelQuery() {
070: }
071:
072: /**
073: * Returns source level of the given Java file, Java package or source folder. For acceptable return values
074: * see the documentation of <code>-source</code> command line switch of
075: * <code>javac</code> compiler .
076: * @param javaFile Java source file, Java package or source folder in question
077: * @return source level of the Java file, e.g. "1.3", "1.4" or "1.5", or null
078: * if it is not known
079: */
080: public static String getSourceLevel(FileObject javaFile) {
081: for (SourceLevelQueryImplementation sqi : implementations
082: .allInstances()) {
083: String s = sqi.getSourceLevel(javaFile);
084: if (s != null) {
085: if (!SOURCE_LEVEL.matcher(s).matches()) {
086: LOGGER
087: .log(
088: Level.WARNING,
089: "#83994: Ignoring bogus source level {0} for {1} from {2}",
090: new Object[] { s, javaFile, sqi });
091: continue;
092: }
093: LOGGER.log(Level.FINE,
094: "Found source level {0} for {1} from {2}",
095: new Object[] { s, javaFile, sqi });
096: return s;
097: }
098: }
099: LOGGER.log(Level.FINE, "No source level found for {0}",
100: javaFile);
101: return null;
102: }
103:
104: }
|