1 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 ; 28 29 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 43 public void addEnabled(FileSet fs) { 44 enabled.add(fs); 45 } 46 47 49 public void addDisabled(FileSet fs) { 50 disabled.add(fs); 51 } 52 53 55 public void addAutoload(FileSet fs) { 56 autoload.add(fs); 57 } 58 59 61 public void addEager(FileSet fs) { 62 eager.add(fs); 63 } 64 65 67 public void addHidden(FileSet fs) { 68 hidden.add(fs); 69 } 70 71 private File xmldir = null; 72 73 76 public void setXmldir(File f) { 77 xmldir = f; 78 } 79 80 private List<String > enabledNames = new ArrayList<String >(50); 81 private List<String > disabledNames = new ArrayList<String >(10); 82 private List<String > autoloadNames = new ArrayList<String >(10); 83 private List<String > eagerNames = new ArrayList<String >(10); 84 private List<String > hiddenNames = new ArrayList<String >(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 > names) throws BuildException { 132 FileScanner scan = fs.getDirectoryScanner(getProject()); 133 File dir = scan.getBasedir(); 134 for (String 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 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")) { throw new BuildException("Illegal trailing space in OpenIDE-Module value from " + module, getLocation()); 149 } 150 int idx = codename.lastIndexOf('/'); 151 String 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 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 log("Will not overwrite " + xml + "; skipping...", Project.MSG_VERBOSE); 168 continue; 169 } 170 String displayname = attr.getValue("OpenIDE-Module-Name"); 171 if (displayname == null) { 172 String bundle = attr.getValue("OpenIDE-Module-Localizing-Bundle"); 173 if (bundle != null) { 174 ZipEntry 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 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 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 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 |