KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > SourceLevelQueryImpl


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.freeform;
21
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.WeakHashMap JavaDoc;
25 import org.netbeans.api.project.ProjectManager;
26 import org.netbeans.modules.ant.freeform.spi.support.Util;
27 import org.netbeans.spi.java.queries.SourceLevelQueryImplementation;
28 import org.netbeans.spi.project.AuxiliaryConfiguration;
29 import org.netbeans.spi.project.support.ant.AntProjectHelper;
30 import org.netbeans.spi.project.support.ant.AntProjectListener;
31 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.Mutex;
35 import org.w3c.dom.Element JavaDoc;
36
37 /**
38  * Specifies the Java source level (for example 1.4) to use for freeform sources.
39  * @author Jesse Glick
40  */

41 final class SourceLevelQueryImpl implements SourceLevelQueryImplementation, AntProjectListener {
42     
43     private AntProjectHelper helper;
44     private PropertyEvaluator evaluator;
45     private AuxiliaryConfiguration aux;
46     
47     /**
48      * Map from package roots to source levels.
49      */

50     private final Map JavaDoc<FileObject,String JavaDoc> sourceLevels = new WeakHashMap JavaDoc<FileObject,String JavaDoc>();
51     
52     public SourceLevelQueryImpl(AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux) {
53         this.helper = helper;
54         this.evaluator = evaluator;
55         this.aux = aux;
56         this.helper.addAntProjectListener(this);
57     }
58     
59     public String JavaDoc getSourceLevel(final FileObject file) {
60         //#60638: the getSourceLevelImpl method takes read access on ProjectManager.mutex
61
//taking the read access before the private lock to prevent deadlocks.
62
return ProjectManager.mutex().readAccess(new Mutex.Action<String JavaDoc>() {
63             public String JavaDoc run() {
64                 return getSourceLevelImpl(file);
65             }
66         });
67     }
68     
69     private synchronized String JavaDoc getSourceLevelImpl(FileObject file) {
70         // Check for cached value.
71
for (Map.Entry JavaDoc<FileObject,String JavaDoc> entry : sourceLevels.entrySet()) {
72             FileObject root = entry.getKey();
73             if (root == file || FileUtil.isParentOf(root, file)) {
74                 // Already have it.
75
return entry.getValue();
76             }
77         }
78         // Need to compute it.
79
Element JavaDoc java = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
80         if (java == null) {
81             return null;
82         }
83         for (Element JavaDoc compilationUnitEl : Util.findSubElements(java)) {
84             assert compilationUnitEl.getLocalName().equals("compilation-unit") : compilationUnitEl;
85             List JavaDoc<FileObject> packageRoots = Classpaths.findPackageRoots(helper, evaluator, compilationUnitEl);
86             for (FileObject root : packageRoots) {
87                 if (root == file || FileUtil.isParentOf(root, file)) {
88                     // Got it. Retrieve source level and cache it (for each root).
89
String JavaDoc lvl = getLevel(compilationUnitEl);
90                     for (FileObject root2 : packageRoots) {
91                         sourceLevels.put(root2, lvl);
92                     }
93                     return lvl;
94                 }
95             }
96         }
97         // Didn't find anything.
98
return null;
99     }
100     
101     public void propertiesChanged(org.netbeans.spi.project.support.ant.AntProjectEvent ev) {
102     }
103
104     public void configurationXmlChanged(org.netbeans.spi.project.support.ant.AntProjectEvent ev) {
105         synchronized (this) {
106             this.sourceLevels.clear();
107         }
108     }
109     
110     /**
111      * Get the source level indicated in a compilation unit (or null if none is indicated).
112      */

113     private String JavaDoc getLevel(Element JavaDoc compilationUnitEl) {
114         Element JavaDoc sourceLevelEl = Util.findElement(compilationUnitEl, "source-level", JavaProjectNature.NS_JAVA_2);
115         if (sourceLevelEl != null) {
116             return Util.findText(sourceLevelEl);
117         } else {
118             return null;
119         }
120     }
121     
122 }
123
Popular Tags