1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import org.apache.tools.ant.BuildException; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.Element ; 36 import org.xml.sax.InputSource ; 37 38 42 class ModuleTracking { 43 private static final String ELEMENT_MODULE_TRACKING = "tracking"; private static final String ELEMENT_MODULE = "module"; private static final String ATTR_MODULE_NAME = "name"; private static final String ELEMENT_FILE = "file"; private static final String ATTR_FILE_NAME = "name"; private static final String ATTR_CODE_NAME = "codename"; private static final String ATTR_MODULE_PATH = "path"; private static final String ATTR_NBM_HOME_PAGE = "nbmhomepage"; private static final String ATTR_NBM_FILE_NAME = "nbmfilename"; private static final String ATTR_NBM_NEEDS_RESTART = "nbmneedsrestart"; private static final String ATTR_NBM_IS_GLOBAL = "nbmisglobal"; private static final String ATTR_NBM_TARGET_CLUSTER = "nbmtargetcluster"; private static final String ATTR_NBM_RELEASE_DATE = "nbmreleasedate"; private static final String ATTR_NBM_MODULE_AUTHOR = "nbmmoduleauthor"; 58 59 private static final String FILE_SEPARATOR = System.getProperty ("file.separator"); 61 62 public static final String TRACKING_FILE = "module_tracking.xml"; 64 private boolean pError = false; 65 66 private File trackingFile = null; 67 68 private String nbPath = null; 69 private Tracking tracking = null; 70 71 public ModuleTracking(String nbPath) { 73 this.nbPath = nbPath; 74 File directory = new File ( nbPath ); 75 if (!directory.exists()) { 76 directory.mkdirs(); 77 } 78 trackingFile = new File (directory, TRACKING_FILE); 79 read(); 80 } 81 82 87 88 public void putModule(String name, String codename, String path, String nbmfilename, 89 String nbmhomepage, String nbmneedsrestart, String nbmreleasedate, String nbmmoduleauthor, String nbmisglobal, String nbmtargetcluster, String [] files) { 90 if (name == null) { 91 return; 93 } 94 Module modByName = tracking.getModule(name); 95 Module modByCodeName = tracking.getModuleByCodeName(codename); 96 Module module = null; 97 if (modByName != null) { 98 module = modByName; 99 } else if (modByCodeName != null) { 100 module = modByCodeName; 101 } 102 if (module == null) { 103 module = new Module(); 104 module.setName(name); 105 module.setCodeName(codename); 106 module.setPath(path); 107 tracking.addModule(module); 108 } 109 if (module.getNbmFileName().equals("")) module.setNbmFileName(nbmfilename); 111 if (module.getNbmHomePage().equals("")) module.setNbmHomePage(nbmhomepage); 113 if (module.getNbmModuleAuthor().equals("")) module.setNbmModuleAuthor(nbmmoduleauthor); 115 if (module.getNbmNeedsRestart().equals("")) module.setNbmNeedsRestart(nbmneedsrestart); 117 if (module.getNbmReleaseDate().equals("")) module.setNbmReleaseDate(nbmreleasedate); 119 if (module.getNbmIsGlobal().equals ("")) { module.setNbmIsGlobal(nbmisglobal); 121 } 122 if (module.getNbmTargetCluster().equals ("")) { module.setNbmTargetCluster(nbmtargetcluster); 124 } 125 137 module.putFiles( files ); 138 } 139 140 public Map <String ,Module> getModules() { 141 return tracking.getModules(); 142 } 143 144 public Hashtable getModulesByCodeName() { 145 return tracking.getModulesByCodeName(); 146 } 147 148 public Iterator getFilesForModule(String name) { 149 Module module = tracking.getModule(name); 150 if (module == null) return null; 151 String [] files = new String [module.getFiles().size()]; 152 return module.getFiles().iterator(); 153 } 154 155 public String getModulePath(String name) { 156 Module module = tracking.getModule(name); 157 if (module == null) return null; 158 return module.getPath(); 159 } 160 161 void write( ) { 162 Document document = XMLUtil.createDocument(ELEMENT_MODULE_TRACKING); 163 Element e_module_tracking = document.getDocumentElement(); 164 Iterator it2 = tracking.getModules().values().iterator(); 165 while ( it2.hasNext() ) { 166 Module mod = (Module)it2.next(); 167 Element e_module = document.createElement(ELEMENT_MODULE); 168 assert mod.getName() != null : mod.getCodeName(); 169 e_module.setAttribute(ATTR_MODULE_NAME, mod.getName()); 170 e_module.setAttribute(ATTR_CODE_NAME, mod.getCodeName()); 171 e_module.setAttribute(ATTR_MODULE_PATH, mod.getPath()); 172 e_module.setAttribute(ATTR_NBM_FILE_NAME, mod.getNbmFileName()); 173 e_module.setAttribute(ATTR_NBM_HOME_PAGE, mod.getNbmHomePage()); 174 e_module.setAttribute(ATTR_NBM_MODULE_AUTHOR, mod.getNbmModuleAuthor()); 175 e_module.setAttribute(ATTR_NBM_NEEDS_RESTART, mod.getNbmNeedsRestart()); 176 e_module.setAttribute(ATTR_NBM_IS_GLOBAL, mod.getNbmIsGlobal ()); 177 e_module.setAttribute(ATTR_NBM_TARGET_CLUSTER, mod.getNbmTargetCluster ()); 178 e_module.setAttribute(ATTR_NBM_RELEASE_DATE, mod.getNbmReleaseDate()); 179 e_module_tracking.appendChild( e_module ); 180 Iterator it3 = mod.getFiles().iterator(); 181 while ( it3.hasNext() ) { 182 String file = (String )it3.next(); 183 Element e_file = document.createElement(ELEMENT_FILE); 184 e_file.setAttribute(ATTR_FILE_NAME, file.replace(File.separatorChar,'/')); 185 e_module.appendChild( e_file ); 186 } 187 } 188 189 try { 191 OutputStream os = new FileOutputStream (trackingFile); 192 XMLUtil.write(document, os); 193 os.close(); 194 } catch (Exception e) { 195 e.printStackTrace(); 196 trackingFile.delete(); 197 throw new BuildException("Could not write update tracking file " + trackingFile.getAbsolutePath(), e); 198 } 199 } 200 201 202 private void read() { 203 204 org.w3c.dom.Document document; 205 if (trackingFile.exists()) { 206 InputStream is; 207 try { 208 is = new FileInputStream ( trackingFile ); 209 210 InputSource xmlInputSource = new InputSource ( is ); 211 document = XMLUtil.parse( xmlInputSource, false, false, new ErrorCatcher(), null ); 212 if (is != null) 213 is.close(); 214 } 215 catch ( org.xml.sax.SAXException e ) { 216 System.out.println("Module tracking file " + trackingFile.getAbsolutePath() + " is not well formatted XML document" ); 217 e.printStackTrace(); 218 return; 219 } 220 catch ( java.io.IOException e ) { 221 System.out.println("I/O error when accessing module tracking file " + trackingFile.getAbsolutePath() ); 222 e.printStackTrace(); 223 return; 224 } 225 226 org.w3c.dom.Element element = document.getDocumentElement(); 227 if ((element != null) && element.getTagName().equals(ELEMENT_MODULE_TRACKING)) { 228 scanElement_module_tracking(element); 229 } 230 } 231 if (tracking == null) 232 tracking = new Tracking(); 233 } 234 235 236 void scanElement_module_tracking(org.w3c.dom.Element element) { tracking = new Tracking(); 238 org.w3c.dom.NodeList nodes = element.getChildNodes(); 239 for (int i = 0; i < nodes.getLength(); i++) { 240 org.w3c.dom.Node node = nodes.item(i); 241 if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) { 242 org.w3c.dom.Element nodeElement = (org.w3c.dom.Element )node; 243 if (nodeElement.getTagName().equals(ELEMENT_MODULE)) { 244 scanElement_module(nodeElement, tracking); 245 } 246 } 247 } 248 } 249 250 251 void scanElement_module(org.w3c.dom.Element element, Tracking tracking) { Module module = new Module(); 253 org.w3c.dom.NamedNodeMap attrs = element.getAttributes(); 254 for (int i = 0; i < attrs.getLength(); i++) { 255 org.w3c.dom.Attr attr = (org.w3c.dom.Attr )attrs.item(i); 256 if (attr.getName().equals(ATTR_MODULE_NAME)) { module.setName( attr.getValue() ); 258 } 259 if (attr.getName().equals(ATTR_CODE_NAME)) { module.setCodeName( attr.getValue() ); 261 } 262 if (attr.getName().equals(ATTR_MODULE_PATH)) { module.setPath( attr.getValue() ); 264 } 265 if (attr.getName().equals(ATTR_NBM_HOME_PAGE)) { module.setNbmHomePage( attr.getValue() ); 267 } 268 if (attr.getName().equals(ATTR_NBM_FILE_NAME)) { module.setNbmFileName( attr.getValue() ); 270 } 271 if (attr.getName().equals(ATTR_NBM_NEEDS_RESTART)) { module.setNbmNeedsRestart( attr.getValue() ); 273 } 274 if (attr.getName().equals(ATTR_NBM_IS_GLOBAL)) { module.setNbmIsGlobal( attr.getValue() ); 276 } 277 if (attr.getName().equals(ATTR_NBM_TARGET_CLUSTER)) { module.setNbmTargetCluster ( attr.getValue() ); 279 } 280 if (attr.getName().equals(ATTR_NBM_RELEASE_DATE)) { module.setNbmReleaseDate( attr.getValue() ); 282 } 283 if (attr.getName().equals(ATTR_NBM_MODULE_AUTHOR)) { module.setNbmModuleAuthor( attr.getValue() ); 285 } 286 } 287 org.w3c.dom.NodeList nodes = element.getChildNodes(); 288 for (int i = 0; i < nodes.getLength(); i++) { 289 org.w3c.dom.Node node = nodes.item(i); 290 if ( node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE ) { 291 org.w3c.dom.Element nodeElement = (org.w3c.dom.Element )node; 292 if (nodeElement.getTagName().equals(ELEMENT_FILE)) { 293 scanElement_file(nodeElement, module); 294 } 295 } 296 } 297 tracking.addModule(module); 298 } 299 300 301 void scanElement_file(org.w3c.dom.Element element, Module module) { String file = null; 303 org.w3c.dom.NamedNodeMap attrs = element.getAttributes(); 304 for (int i = 0; i < attrs.getLength(); i++) { 305 org.w3c.dom.Attr attr = (org.w3c.dom.Attr )attrs.item(i); 306 if (attr.getName().equals(ATTR_FILE_NAME)) { file = attr.getValue(); 308 } 309 } 310 module.addFile (file.replace(File.separatorChar,'/')); 311 } 312 313 public class Tracking extends Object { 314 private Map <String ,Module> modules = new HashMap <String ,Module>(); 315 private Hashtable <String ,Module> modulesByCodeName = new Hashtable <String ,Module>(); 316 317 Map <String ,Module> getModules() { 318 return modules; 319 } 320 321 324 Hashtable <String ,Module> getModulesByCodeName() { 325 return modulesByCodeName; 326 } 327 328 331 void setModulesByCodeName(Hashtable <String ,Module> modules) { 332 this.modulesByCodeName = modules; 333 } 334 335 public void addModule( Module module ) { 336 modules.put( module.getName(), module ); 337 modulesByCodeName.put( module.getCodeName(), module ); 338 } 339 340 public Module getModule( String name ) { 341 return modules.get(name); 342 } 343 public Module getModuleByCodeName( String codename ) { 344 return modulesByCodeName.get(codename); 345 } 346 } 347 348 public class Module extends Object { 349 350 private String name; 351 352 353 private String codename; 354 355 356 private String path; 357 358 359 private String nbmhomepage = ""; 361 362 private String nbmfilename = ""; 364 365 private String nbmneedsrestart = ""; 367 368 private String nbmisglobal = ""; 370 371 private String nbmtargetcluster = ""; 373 374 private String nbmreleasedate = ""; 376 377 private String nbmmoduleauthor = ""; 379 380 private List <String > files = new ArrayList <String >(); 381 382 383 String getName() { 384 return name; 385 } 386 387 388 void setName(String name) { 389 this.name = name; 390 } 391 392 393 String getCodeName() { 394 return codename; 395 } 396 397 398 void setCodeName(String codename) { 399 this.codename = codename; 400 } 401 402 403 String getPath() { 404 return path; 405 } 406 407 408 void setPath(String path) { 409 this.path = path; 410 } 411 412 413 String getNbmHomePage () { 414 if (this.nbmhomepage == null) { 415 return ""; } else { 417 return this.nbmhomepage; 418 } 419 } 420 421 422 void setNbmHomePage (String nbmh) { 423 if (!(nbmh == null)) 424 this.nbmhomepage = nbmh; 425 } 426 427 428 String getNbmFileName () { 429 if (this.nbmfilename == null) { 430 return ""; } else { 432 return this.nbmfilename; 433 } 434 } 435 436 437 void setNbmFileName (String nbmfn) { 438 if (!(nbmfn == null)) 439 this.nbmfilename = nbmfn; 440 } 441 442 443 String getNbmNeedsRestart () { 444 if (this.nbmneedsrestart == null) { 445 return ""; } else { 447 return this.nbmneedsrestart; 448 } 449 } 450 451 452 void setNbmNeedsRestart (String nbmnr) { 453 if (!(nbmnr == null)) 454 this.nbmneedsrestart = nbmnr; 455 } 456 457 458 void setNbmIsGlobal (String isGlobal) { 459 if (!(isGlobal == null)) 460 this.nbmisglobal = isGlobal; 461 } 462 463 464 String getNbmIsGlobal () { 465 return this.nbmisglobal; 466 } 467 468 void setNbmTargetCluster (String targetcluster) { 469 if (! (targetcluster == null)) { 470 this.nbmtargetcluster = targetcluster; 471 } 472 } 473 474 475 String getNbmTargetCluster () { 476 return this.nbmtargetcluster; 477 } 478 479 480 String getNbmReleaseDate () { 481 if (this.nbmreleasedate == null) { 482 return ""; } else { 484 return this.nbmreleasedate; 485 } 486 } 487 488 489 void setNbmReleaseDate (String nbmrd) { 490 if (!(nbmrd == null )) 491 this.nbmreleasedate = nbmrd; 492 } 493 494 495 String getNbmModuleAuthor () { 496 if (this.nbmmoduleauthor == null) { 497 return ""; } else { 499 return this.nbmmoduleauthor; 500 } 501 } 502 503 504 void setNbmModuleAuthor (String nbmma) { 505 if (!(nbmma == null)) 506 this.nbmmoduleauthor = nbmma; 507 } 508 511 List getFiles() { 512 return files; 513 } 514 515 518 void setFiles(List <String > files) { 519 this.files = files; 520 } 521 522 public void addFile( String filename) { 523 files.add( filename.replace(File.separatorChar, '/') ); 524 } 525 526 public void putFiles( String [] list ) { 527 for (int i=0; i < list.length; i++) { 528 if (!files.contains(list[i].replace(File.separatorChar,'/'))) { 529 files.add(list[i].replace(File.separatorChar,'/')); 530 } 531 } 532 } 533 534 } 535 536 class ErrorCatcher implements org.xml.sax.ErrorHandler { 537 private void message (String level, org.xml.sax.SAXParseException e) { 538 pError = true; 539 } 540 541 public void error (org.xml.sax.SAXParseException e) { 542 pError = true; 544 } 545 546 public void warning (org.xml.sax.SAXParseException e) { 547 } 549 550 public void fatalError (org.xml.sax.SAXParseException e) { 551 pError = true; 552 } 553 } 554 555 } 556 | Popular Tags |