KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > ModuleSelector


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.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.jar.JarFile JavaDoc;
30 import java.util.jar.Manifest JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.parsers.SAXParser JavaDoc;
33 import javax.xml.parsers.SAXParserFactory JavaDoc;
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.types.Parameter;
37 import org.apache.tools.ant.types.selectors.BaseExtendSelector;
38 import org.xml.sax.Attributes JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.helpers.DefaultHandler JavaDoc;
41
42 /** Selector that accepts modules based on their code base name.
43  *
44  * @author Jaroslav Tulach
45  */

46 public final class ModuleSelector extends BaseExtendSelector {
47     private Set JavaDoc<String JavaDoc> excludeModules;
48     private Set JavaDoc<String JavaDoc> includeClusters;
49     private Set JavaDoc<String JavaDoc> excludeClusters;
50     private Map JavaDoc<String JavaDoc,String JavaDoc> fileToOwningModule;
51     private boolean acceptExcluded;
52     
53     /** Creates a new instance of ModuleSelector */
54     public ModuleSelector() {
55     }
56
57     public boolean isSelected(File JavaDoc dir, String JavaDoc filename, File JavaDoc file) throws BuildException {
58         validate();
59      
60         Boolean JavaDoc check = checkSelected(dir, filename, file);
61         if (check == null) {
62             return false;
63         }
64         
65         if (acceptExcluded) {
66             log("Reverting the accepted state", Project.MSG_VERBOSE);
67             return !check.booleanValue();
68         } else {
69             return check.booleanValue();
70         }
71     }
72     
73     private Boolean JavaDoc checkSelected(File JavaDoc dir, String JavaDoc filename, File JavaDoc file) throws BuildException {
74         if (file.isDirectory()) {
75             log("Skipping directory: " + file, Project.MSG_VERBOSE);
76             return null;
77         }
78         
79         String JavaDoc module = null;
80         if (file.getName().endsWith(".jar")) {
81             try {
82                 JarFile JavaDoc jar = new JarFile JavaDoc(file);
83                 Manifest JavaDoc m = jar.getManifest();
84                 if (m != null) {
85                     module = m.getMainAttributes().getValue("OpenIDE-Module");
86                 }
87                 jar.close();
88             } catch (IOException JavaDoc ex) {
89                 throw new BuildException("Problem with file: " + file, ex);
90             }
91         }
92
93         String JavaDoc name = file.getName();
94         File JavaDoc p = file.getParentFile();
95         for(;;) {
96
97             if (new File JavaDoc(p, "update_tracking").isDirectory()) { // else includeClusters does not work
98
String JavaDoc cluster = p.getName();
99                 
100                 if (!includeClusters.isEmpty() && !includeClusters.contains(cluster)) {
101                     log("Not included cluster: " + cluster + " for " + file, Project.MSG_VERBOSE);
102                     return null;
103                 }
104
105                 if (includeClusters.isEmpty() && excludeClusters.contains(cluster)) {
106                     log("Excluded cluster: " + cluster + " for " + file, Project.MSG_VERBOSE);
107                     return null;
108                 }
109             }
110             
111             if (module == null && fileToOwningModule != null) {
112                 module = fileToOwningModule.get(name);
113             }
114             
115             if (dir.equals(p)) {
116                 break;
117             }
118             name = p.getName() + '/' + name;
119             p = p.getParentFile();
120         }
121         
122         if (module == null) {
123             log("No module in: " + file, Project.MSG_VERBOSE);
124             return null;
125         }
126         int slash = module.indexOf('/');
127         if (slash >= 0) {
128             module = module.substring(0, slash);
129         }
130         
131         if (excludeModules.contains(module)) {
132             log("Excluded module: " + file, Project.MSG_VERBOSE);
133             return Boolean.FALSE;
134         }
135
136         log("Accepted file: " + file, Project.MSG_VERBOSE);
137         return Boolean.TRUE;
138     }
139
140     public void verifySettings() {
141         if (includeClusters != null) {
142             return;
143         }
144         
145         includeClusters = new HashSet JavaDoc<String JavaDoc>();
146         excludeClusters = new HashSet JavaDoc<String JavaDoc>();
147         excludeModules = new HashSet JavaDoc<String JavaDoc>();
148         
149         Parameter[] arr = getParameters();
150         if (arr == null) {
151             return;
152         }
153         
154         for (Parameter p : arr) {
155             if ("excludeModules".equals(p.getName())) {
156                 parse(p.getValue(), excludeModules);
157                 log("Will excludeModules: " + excludeModules, Project.MSG_VERBOSE);
158                 continue;
159             }
160             if ("includeClusters".equals(p.getName())) {
161                 parse(p.getValue(), includeClusters);
162                 log("Will includeClusters: " + includeClusters, Project.MSG_VERBOSE);
163                 continue;
164             }
165             if ("excludeClusters".equals(p.getName())) {
166                 parse(p.getValue(), excludeClusters);
167                 log("Will excludeClusters: " + excludeClusters, Project.MSG_VERBOSE);
168                 continue;
169             }
170             if ("excluded".equals(p.getName())) {
171                 acceptExcluded = Boolean.parseBoolean(p.getValue());
172                 log("Will acceptExcluded: " + acceptExcluded, Project.MSG_VERBOSE);
173                 continue;
174             }
175             if ("updateTrackingFiles".equals(p.getName())) {
176                 fileToOwningModule = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
177                 try {
178                     readUpdateTracking(getProject(), p.getValue(), fileToOwningModule);
179                 } catch (IOException JavaDoc ex) {
180                     throw new BuildException(ex);
181                 } catch (ParserConfigurationException JavaDoc ex) {
182                     throw new BuildException(ex);
183                 } catch (SAXException JavaDoc ex) {
184                     throw new BuildException(ex);
185                 }
186                 log("Will accept these files: " + fileToOwningModule.keySet(), Project.MSG_VERBOSE);
187                 continue;
188             }
189             setError("Unknown parameter: " + p.getName());
190         }
191     }
192     
193     private static void parse(String JavaDoc tokens, Set JavaDoc<String JavaDoc> to) {
194         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(tokens, ", \n");
195         
196         while(tok.hasMoreElements()) {
197             to.add(tok.nextToken());
198         }
199     }
200
201     static void readUpdateTracking(final Project p, String JavaDoc tokens, final Map JavaDoc<String JavaDoc,String JavaDoc> files) throws SAXException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc {
202         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(tokens, File.pathSeparator);
203         
204         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
205         factory.setValidating(false);
206         final SAXParser JavaDoc parser = factory.newSAXParser();
207
208         class MyHandler extends DefaultHandler JavaDoc {
209             public File JavaDoc where;
210             public String JavaDoc module;
211             
212             public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
213                 if (qName.equals("file")) {
214                     String JavaDoc file = attributes.getValue("name");
215                     if (file == null) {
216                         throw new BuildException("<file/> without name attribute in " + where);
217                     }
218                     
219                     files.put(file.replace(File.separatorChar, '/'), module);
220                 }
221             }
222             
223             public void iterate(StringTokenizer JavaDoc tok) throws SAXException JavaDoc, IOException JavaDoc {
224                 while(tok.hasMoreElements()) {
225                     where = new File JavaDoc(tok.nextToken());
226                     
227                     module = where.getName();
228                     if (module.endsWith(".xml")) {
229                         module = module.substring(0, module.length() - 4);
230                     }
231                     module = module.replace('-', '.');
232
233                     try {
234                         if (p != null) {
235                             p.log("Parsing " + where, Project.MSG_VERBOSE);
236                         }
237                         parser.parse(where, this);
238                     } catch (SAXException JavaDoc ex) {
239                         throw new BuildException("Wrong file " + where, ex);
240                     }
241                     
242                     // the update tracking file belongs to the moduel as well
243
files.put(where.getParentFile().getName() + '/' + where.getName(), module);
244                 }
245             }
246         }
247         MyHandler handler = new MyHandler();
248         handler.iterate (tok);
249         
250         
251     }
252 }
253
Popular Tags