1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.StringTokenizer ; 29 import java.util.jar.JarFile ; 30 import java.util.jar.Manifest ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.parsers.SAXParser ; 33 import javax.xml.parsers.SAXParserFactory ; 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 ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.helpers.DefaultHandler ; 41 42 46 public final class ModuleSelector extends BaseExtendSelector { 47 private Set <String > excludeModules; 48 private Set <String > includeClusters; 49 private Set <String > excludeClusters; 50 private Map <String ,String > fileToOwningModule; 51 private boolean acceptExcluded; 52 53 54 public ModuleSelector() { 55 } 56 57 public boolean isSelected(File dir, String filename, File file) throws BuildException { 58 validate(); 59 60 Boolean 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 checkSelected(File dir, String filename, File file) throws BuildException { 74 if (file.isDirectory()) { 75 log("Skipping directory: " + file, Project.MSG_VERBOSE); 76 return null; 77 } 78 79 String module = null; 80 if (file.getName().endsWith(".jar")) { 81 try { 82 JarFile jar = new JarFile (file); 83 Manifest m = jar.getManifest(); 84 if (m != null) { 85 module = m.getMainAttributes().getValue("OpenIDE-Module"); 86 } 87 jar.close(); 88 } catch (IOException ex) { 89 throw new BuildException("Problem with file: " + file, ex); 90 } 91 } 92 93 String name = file.getName(); 94 File p = file.getParentFile(); 95 for(;;) { 96 97 if (new File (p, "update_tracking").isDirectory()) { String 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 <String >(); 146 excludeClusters = new HashSet <String >(); 147 excludeModules = new HashSet <String >(); 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 <String ,String >(); 177 try { 178 readUpdateTracking(getProject(), p.getValue(), fileToOwningModule); 179 } catch (IOException ex) { 180 throw new BuildException(ex); 181 } catch (ParserConfigurationException ex) { 182 throw new BuildException(ex); 183 } catch (SAXException 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 tokens, Set <String > to) { 194 StringTokenizer tok = new StringTokenizer (tokens, ", \n"); 195 196 while(tok.hasMoreElements()) { 197 to.add(tok.nextToken()); 198 } 199 } 200 201 static void readUpdateTracking(final Project p, String tokens, final Map <String ,String > files) throws SAXException , IOException , ParserConfigurationException { 202 StringTokenizer tok = new StringTokenizer (tokens, File.pathSeparator); 203 204 SAXParserFactory factory = SAXParserFactory.newInstance(); 205 factory.setValidating(false); 206 final SAXParser parser = factory.newSAXParser(); 207 208 class MyHandler extends DefaultHandler { 209 public File where; 210 public String module; 211 212 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 213 if (qName.equals("file")) { 214 String 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 tok) throws SAXException , IOException { 224 while(tok.hasMoreElements()) { 225 where = new File (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 ex) { 239 throw new BuildException("Wrong file " + where, ex); 240 } 241 242 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 |