KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.api.project.Project;
26 import org.netbeans.api.queries.FileBuiltQuery.Status;
27 import org.netbeans.modules.ant.freeform.spi.support.Util;
28 import org.netbeans.spi.project.AuxiliaryConfiguration;
29 import org.netbeans.spi.project.support.ant.AntProjectEvent;
30 import org.netbeans.spi.project.support.ant.AntProjectHelper;
31 import org.netbeans.spi.project.support.ant.AntProjectListener;
32 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
33 import org.netbeans.spi.queries.FileBuiltQueryImplementation;
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * Provides a FileBuiltQueryImplementation for the Java Freeform projects.
41  * Currently, for each compilation unit, it looks to built-to element, finds the first
42  * directory and supposes it is the target of compilation for this compilation unit.
43  *
44  * @author Jan Lahoda
45  */

46 final class JavaFreeformFileBuiltQuery implements FileBuiltQueryImplementation, AntProjectListener {
47     
48     private static final ErrorManager ERR = ErrorManager.getDefault().getInstance(JavaFreeformFileBuiltQuery.class.getName());
49     
50     private Project project;
51     private AntProjectHelper projectHelper;
52     private PropertyEvaluator projectEvaluator;
53     private AuxiliaryConfiguration aux;
54     
55     public JavaFreeformFileBuiltQuery(Project project, AntProjectHelper projectHelper, PropertyEvaluator projectEvaluator, AuxiliaryConfiguration aux) {
56         this.project = project;
57         this.projectHelper = projectHelper;
58         this.projectEvaluator = projectEvaluator;
59         this.aux = aux;
60         
61         this.delegateTo = null;
62         
63         projectHelper.addAntProjectListener(this);
64     }
65     
66     private FileBuiltQueryImplementation delegateTo;
67     
68     private FileBuiltQueryImplementation createDelegateTo() {
69         if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) {
70             ERR.log(ErrorManager.INFORMATIONAL, "JavaFreeformFileBuiltQuery.createDelegateTo start"); // NOI18N
71
}
72         
73         Element JavaDoc java = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
74         List JavaDoc<String JavaDoc> from = new ArrayList JavaDoc<String JavaDoc>();
75         List JavaDoc<String JavaDoc> to = new ArrayList JavaDoc<String JavaDoc>();
76         
77         if (java != null) {
78             List JavaDoc<Element JavaDoc> compilationUnits = Util.findSubElements(java);
79             Iterator JavaDoc it = compilationUnits.iterator();
80             while (it.hasNext()) {
81                 Element JavaDoc compilationUnitEl = (Element JavaDoc)it.next();
82                 assert compilationUnitEl.getLocalName().equals("compilation-unit") : compilationUnitEl;
83                 List JavaDoc<String JavaDoc> rootNames = Classpaths.findPackageRootNames(compilationUnitEl);
84                 List JavaDoc<String JavaDoc> builtToNames = findBuiltToNames(compilationUnitEl);
85                 
86                 List JavaDoc<String JavaDoc> rootPatterns = new ArrayList JavaDoc<String JavaDoc>();
87                 String JavaDoc builtToPattern = null;
88                 
89                 for (String JavaDoc n : rootNames) {
90                     rootPatterns.add(projectEvaluator.evaluate(n) + "/*.java"); // NOI18N
91
}
92                 
93                 if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) {
94                     ERR.log(ErrorManager.INFORMATIONAL, "Looking for a suitable built-to:"); // NOI18N
95
}
96                 
97                 for (String JavaDoc n : builtToNames) {
98                     String JavaDoc builtTo = projectEvaluator.evaluate(n);
99                     if (builtTo == null) {
100                         continue;
101                     }
102                     boolean isFolder = JavaProjectGenerator.isFolder(projectEvaluator, FileUtil.toFile(project.getProjectDirectory()), builtTo);
103                     
104                     if (isFolder && builtToPattern == null) {
105                         builtToPattern = builtTo + "/*.class"; // NOI18N
106
break;
107                     }
108                 }
109                 
110                 if (builtToPattern != null) {
111                     if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) {
112                         ERR.log(ErrorManager.INFORMATIONAL, "Found built to pattern=" + builtToPattern + ", rootPatterns=" + rootPatterns); // NOI18N
113
}
114                     for (String JavaDoc p : rootPatterns) {
115                         from.add(p);
116                         to.add(builtToPattern);
117                     }
118                 } else {
119                     if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) {
120                         ERR.log(ErrorManager.INFORMATIONAL, "No built to pattern found, rootPatterns=" + rootPatterns); // NOI18N
121
}
122                 }
123             }
124         }
125         
126         if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) {
127             ERR.log(ErrorManager.INFORMATIONAL, "JavaFreeformFileBuiltQuery from=" + from + " to=" + to); // NOI18N
128
}
129         
130         String JavaDoc[] fromStrings = from.toArray(new String JavaDoc[from.size()]);
131         String JavaDoc[] toStrings = to.toArray(new String JavaDoc[to.size()]);
132         
133         FileBuiltQueryImplementation fbqi = projectHelper.createGlobFileBuiltQuery(projectEvaluator, fromStrings, toStrings);
134         
135         if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) {
136             ERR.log(ErrorManager.INFORMATIONAL, "JavaFreeformFileBuiltQuery.createDelegateTo end"); // NOI18N
137
}
138         
139         return fbqi;
140     }
141     
142     public void propertiesChanged(AntProjectEvent evt) {
143         //ignore
144
}
145     
146     public void configurationXmlChanged(AntProjectEvent evt) {
147         synchronized (this) {
148             delegateTo = null;
149         }
150     }
151     
152     public synchronized Status getStatus(FileObject fo) {
153         if (delegateTo == null)
154             delegateTo = createDelegateTo();
155         
156         return delegateTo.getStatus(fo);
157     }
158     
159     static List JavaDoc<String JavaDoc> findBuiltToNames(Element JavaDoc compilationUnitEl) {
160         List JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>();
161         for (Element JavaDoc e : Util.findSubElements(compilationUnitEl)) {
162             if (!e.getLocalName().equals("built-to")) { // NOI18N
163
continue;
164             }
165             String JavaDoc location = Util.findText(e);
166             names.add(location);
167         }
168         return names;
169     }
170     
171 }
172
Popular Tags