1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.PrintWriter ; 27 import java.io.StringReader ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.SortedMap ; 32 import java.util.TreeMap ; 33 import java.util.jar.JarFile ; 34 import java.util.jar.Manifest ; 35 import javax.xml.parsers.SAXParserFactory ; 36 import org.apache.tools.ant.BuildException; 37 import org.apache.tools.ant.DirectoryScanner; 38 import org.apache.tools.ant.Task; 39 import org.apache.tools.ant.types.FileSet; 40 import org.xml.sax.Attributes ; 41 import org.xml.sax.InputSource ; 42 import org.xml.sax.SAXException ; 43 import org.xml.sax.helpers.DefaultHandler ; 44 45 50 public class LayerIndex extends Task { 51 52 public LayerIndex() {} 53 54 List <FileSet> filesets = new ArrayList <FileSet>(); 55 public void addConfiguredModules(FileSet fs) { 56 filesets.add(fs); 57 } 58 59 private File output; 60 public void setOutput(File f) { 61 output = f; 62 } 63 64 @Override 65 public void execute() throws BuildException { 66 if (filesets.isEmpty()) { 67 throw new BuildException(); 68 } 69 SortedMap <String ,String > files = new TreeMap <String ,String >(); for (FileSet fs : filesets) { 71 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 72 File basedir = ds.getBasedir(); 73 for (String path : ds.getIncludedFiles()) { 74 File jar = new File (basedir, path); 75 try { 76 JarFile jf = new JarFile (jar); 77 try { 78 Manifest mf = jf.getManifest(); 79 if (mf == null) { 80 continue; 81 } 82 String modname = mf.getMainAttributes().getValue("OpenIDE-Module"); 83 if (modname == null) { 84 continue; 85 } 86 String cnb = modname.replaceFirst("/\\d+$", ""); 87 String layer = mf.getMainAttributes().getValue("OpenIDE-Module-Layer"); 88 if (layer == null) { 89 continue; 90 } 91 parse(jf.getInputStream(jf.getEntry(layer)), files, cnb); 92 } finally { 93 jf.close(); 94 } 95 } catch (Exception x) { 96 throw new BuildException("Reading " + jar + ": " + x, x, getLocation()); 97 } 98 } 99 } 100 int maxlength = 0; 101 for (String cnb : files.values()) { 102 if (cnb != null) { 103 maxlength = Math.max(maxlength, cnb.length()); 104 } 105 } 106 try { 107 PrintWriter pw = output != null ? new PrintWriter (output) : null; 108 for (Map.Entry <String ,String > entry : files.entrySet()) { 109 String path = entry.getKey(); 110 String cnb = entry.getValue(); 111 if (cnb == null) { 112 cnb = ""; 113 } 114 String line = String.format("%-" + maxlength + "s %s", cnb, path); 115 if (pw != null) { 116 pw.println(line); 117 } else { 118 log(line); 119 } 120 } 121 if (pw != null) { 122 pw.close(); 123 } 124 } catch (FileNotFoundException x) { 125 throw new BuildException(x, getLocation()); 126 } 127 if (output != null) { 128 log(output + ": layer index written"); 129 } 130 } 131 132 private void parse(InputStream is, final Map <String ,String > files, final String cnb) throws Exception { 133 SAXParserFactory f = SAXParserFactory.newInstance(); 134 f.setValidating(false); 135 f.setNamespaceAware(false); 136 f.newSAXParser().parse(is, new DefaultHandler () { 137 String prefix = ""; 138 void register(String path) { 139 if (files.containsKey(path)) { 140 files.put(path, null); } else { 142 files.put(path, cnb); 143 } 144 } 145 @Override 146 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 147 if (qName.equals("folder")) { 148 String n = attributes.getValue("name"); 149 prefix += n + "/"; 150 register(prefix); 151 } else if (qName.equals("file")) { 152 String n = attributes.getValue("name"); 153 register(prefix + n); 154 } 155 } 156 @Override 157 public void endElement(String uri, String localName, String qName) throws SAXException { 158 if (qName.equals("folder")) { 159 prefix = prefix.replaceFirst("[^/]+/$", ""); 160 } 161 } 162 @Override 163 public InputSource resolveEntity(String pub, String sys) throws IOException , SAXException { 164 return new InputSource (new StringReader ("")); 165 } 166 }); 167 } 168 169 } 170 | Popular Tags |