KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
23 import org.apache.tools.ant.types.FileSet;
24 import org.apache.tools.ant.*;
25 import java.util.jar.*;
26 import java.io.*;
27 import java.util.zip.ZipEntry JavaDoc;
28
29 /** Create XML files corresponding to the set of known modules
30  * without actually running the IDE.
31  * @author Jesse Glick
32  */

33 public class CreateModuleXML extends Task {
34
35     private final List<FileSet> enabled = new ArrayList<FileSet>(1);
36     private final List<FileSet> disabled = new ArrayList<FileSet>(1);
37     private final List<FileSet> autoload = new ArrayList<FileSet>(1);
38     private final List<FileSet> eager = new ArrayList<FileSet>(1);
39     private final List<FileSet> hidden = new ArrayList<FileSet>(1);
40     
41     /** Add a set of module JARs that should be enabled.
42      */

43     public void addEnabled(FileSet fs) {
44         enabled.add(fs);
45     }
46     
47     /** Add a set of module JARs that should be disabled.
48      */

49     public void addDisabled(FileSet fs) {
50         disabled.add(fs);
51     }
52     
53     /** Add a set of module JARs that should be autoloads.
54      */

55     public void addAutoload(FileSet fs) {
56         autoload.add(fs);
57     }
58     
59     /** Add a set of module JARs that should be eager modules.
60      */

61     public void addEager(FileSet fs) {
62         eager.add(fs);
63     }
64     
65     /** Add a set of module JARs that should be hidden.
66      */

67     public void addHidden(FileSet fs) {
68         hidden.add(fs);
69     }
70     
71     private File xmldir = null;
72     
73     /** Set the modules directory where XML will be stored.
74      * Normally the system/Modules/ directory in an installation.
75      */

76     public void setXmldir(File f) {
77         xmldir = f;
78     }
79     
80     private List<String JavaDoc> enabledNames = new ArrayList<String JavaDoc>(50);
81     private List<String JavaDoc> disabledNames = new ArrayList<String JavaDoc>(10);
82     private List<String JavaDoc> autoloadNames = new ArrayList<String JavaDoc>(10);
83     private List<String JavaDoc> eagerNames = new ArrayList<String JavaDoc>(10);
84     private List<String JavaDoc> hiddenNames = new ArrayList<String JavaDoc>(10);
85     
86     public void execute() throws BuildException {
87         if (xmldir == null) throw new BuildException("Must set xmldir attribute", getLocation());
88         if (!xmldir.exists ()) {
89             if (!xmldir.mkdirs()) throw new BuildException("Cannot create directory " + xmldir, getLocation());
90         }
91         if (enabled.isEmpty() && disabled.isEmpty() && autoload.isEmpty() && eager.isEmpty() && hidden.isEmpty()) {
92             log("Warning: <createmodulexml> with no modules listed", Project.MSG_WARN);
93         }
94         for (FileSet fs : enabled) {
95             scanModules(fs, true, false, false, false, enabledNames);
96         }
97         for (FileSet fs : disabled) {
98             scanModules(fs, false, false, false, false, disabledNames);
99         }
100         for (FileSet fs : autoload) {
101             scanModules(fs, false, true, false, false, autoloadNames);
102         }
103         for (FileSet fs : eager) {
104             scanModules(fs, false, false, true, false, eagerNames);
105         }
106         for (FileSet fs : hidden) {
107             scanModules(fs, false, false, false, true, hiddenNames);
108         }
109         Collections.sort(enabledNames);
110         Collections.sort(disabledNames);
111         Collections.sort(autoloadNames);
112         Collections.sort(eagerNames);
113         Collections.sort(hiddenNames);
114         if (!enabledNames.isEmpty()) {
115             log("Enabled modules: " + enabledNames);
116         }
117         if (!disabledNames.isEmpty()) {
118             log("Disabled modules: " + disabledNames);
119         }
120         if (!autoloadNames.isEmpty()) {
121             log("Autoload modules: " + autoloadNames);
122         }
123         if (!eagerNames.isEmpty()) {
124             log("Eager modules: " + eagerNames);
125         }
126         if (!hiddenNames.isEmpty()) {
127             log("Hidden modules: " + hiddenNames);
128         }
129     }
130     
131     private void scanModules(FileSet fs, boolean isEnabled, boolean isAutoload, boolean isEager, boolean isHidden, List<String JavaDoc> names) throws BuildException {
132         FileScanner scan = fs.getDirectoryScanner(getProject());
133         File dir = scan.getBasedir();
134         for (String JavaDoc kid : scan.getIncludedFiles()) {
135             File module = new File(dir, kid);
136             if (!module.exists()) throw new BuildException("Module file does not exist: " + module, getLocation());
137             if (!module.getName().endsWith(".jar")) throw new BuildException("Only *.jar may be listed, check the fileset: " + module, getLocation());
138             try {
139                 JarFile jar = new JarFile(module);
140                 try {
141                     Manifest m = jar.getManifest();
142                     Attributes attr = m.getMainAttributes();
143                     String JavaDoc codename = attr.getValue("OpenIDE-Module");
144                     if (codename == null) {
145                         throw new BuildException("Missing manifest tag OpenIDE-Module; " + module + " is not a module", getLocation());
146                     }
147                     if (codename.endsWith(" ") || codename.endsWith("\t")) { // #62887
148
throw new BuildException("Illegal trailing space in OpenIDE-Module value from " + module, getLocation());
149                     }
150                     int idx = codename.lastIndexOf('/');
151                     String JavaDoc codenamebase;
152                     int rel;
153                     if (idx == -1) {
154                         codenamebase = codename;
155                         rel = -1;
156                     } else {
157                         codenamebase = codename.substring(0, idx);
158                         try {
159                             rel = Integer.parseInt(codename.substring(idx + 1));
160                         } catch (NumberFormatException JavaDoc e) {
161                             throw new BuildException("Invalid OpenIDE-Module '" + codename + "' in " + module, getLocation());
162                         }
163                     }
164                     File xml = new File(xmldir, codenamebase.replace('.', '-') + ".xml");
165                     if (xml.exists()) {
166                         // XXX should check that the old file actually matches what we would have written
167
log("Will not overwrite " + xml + "; skipping...", Project.MSG_VERBOSE);
168                         continue;
169                     }
170                     String JavaDoc displayname = attr.getValue("OpenIDE-Module-Name");
171                     if (displayname == null) {
172                         String JavaDoc bundle = attr.getValue("OpenIDE-Module-Localizing-Bundle");
173                         if (bundle != null) {
174                             // Display name actually found in a bundle, not manifest.
175
ZipEntry JavaDoc entry = jar.getEntry(bundle);
176                             InputStream is;
177                             if (entry != null) {
178                                 is = jar.getInputStream(entry);
179                             } else {
180                                 File moduleloc = new File(new File(module.getParentFile(), "locale"), module.getName());
181                                 if (! moduleloc.isFile()) {
182                                     throw new BuildException("Expecting localizing bundle: " + bundle + " in: " + module);
183                                 }
184                                 JarFile jarloc = new JarFile(moduleloc);
185                                 try {
186                                     ZipEntry JavaDoc entry2 = jarloc.getEntry(bundle);
187                                     if (entry2 == null) {
188                                         throw new BuildException("Expecting localizing bundle: " + bundle + " in: " + module);
189                                     }
190                                     is = jarloc.getInputStream(entry2);
191                                 } finally {
192                                     jarloc.close();
193                                 }
194                             }
195                             try {
196                                 Properties p = new Properties();
197                                 p.load(is);
198                                 displayname = p.getProperty("OpenIDE-Module-Name");
199                             } finally {
200                                 is.close();
201                             }
202                         }
203                     }
204                     if (displayname == null) displayname = codename;
205                     names.add(displayname);
206                     String JavaDoc spec = attr.getValue("OpenIDE-Module-Specification-Version");
207                     
208                     if (isHidden) {
209                         File h = new File(xml.getParentFile(), xml.getName() + "_hidden");
210                         h.createNewFile();
211                     }
212                     
213                     if (isEager || isAutoload || isEnabled) {
214                         OutputStream os = new FileOutputStream(xml);
215                         try {
216                             PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
217                             // Please make sure formatting matches what the IDE actually spits
218
// out; it could matter.
219
pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
220                             pw.println("<!DOCTYPE module PUBLIC \"-//NetBeans//DTD Module Status 1.0//EN\"");
221                             pw.println(" \"http://www.netbeans.org/dtds/module-status-1_0.dtd\">");
222                             pw.println("<module name=\"" + codenamebase + "\">");
223                             pw.println(" <param name=\"autoload\">" + isAutoload + "</param>");
224                             pw.println(" <param name=\"eager\">" + isEager + "</param>");
225                             if (!isAutoload && !isEager) {
226                                 pw.println(" <param name=\"enabled\">" + isEnabled + "</param>");
227                             }
228                             pw.println(" <param name=\"jar\">" + kid.replace(File.separatorChar, '/') + "</param>");
229                             if (rel != -1) {
230                                 pw.println(" <param name=\"release\">" + rel + "</param>");
231                             }
232                             pw.println(" <param name=\"reloadable\">false</param>");
233                             if (spec != null) {
234                                 pw.println(" <param name=\"specversion\">" + spec + "</param>");
235                             }
236                             pw.println("</module>");
237                             pw.flush();
238                             pw.close();
239                         } finally {
240                             os.close();
241                         }
242                     }
243                 } finally {
244                     jar.close();
245                 }
246             } catch (IOException ioe) {
247                 throw new BuildException("Caught while processing " + module + ": " + ioe, ioe, getLocation());
248             }
249         }
250     }
251     
252 }
253
Popular Tags