| 1 7 package org.mmbase.applications.packaging.projects; 8 9 import java.io.DataOutputStream ; 10 import java.io.File ; 11 import java.io.FileOutputStream ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 15 import org.mmbase.applications.packaging.ProjectManager; 16 import org.mmbase.applications.packaging.projects.creators.CreatorInterface; 17 import org.mmbase.applications.packaging.util.ExtendedDocumentReader; 18 import org.mmbase.util.XMLEntityResolver; 19 import org.mmbase.util.logging.Logger; 20 import org.mmbase.util.logging.Logging; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.NamedNodeMap ; 23 24 28 public class Project { 29 30 private static Logger log = Logging.getLoggerInstance(Project.class); 32 33 String path; 34 String name; 35 String basedir; 36 HashMap targets = new HashMap (); 37 HashMap packagetargets = new HashMap (); 38 HashMap bundletargets = new HashMap (); 39 40 43 public final static String DTD_PACKAGING_1_0 = "packaging_1_0.dtd"; 44 45 48 public final static String PUBLIC_ID_PACKAGING_1_0 = "-//MMBase//DTD packaging config 1.0//EN"; 49 50 51 55 public static void registerPublicIDs() { 56 XMLEntityResolver.registerPublicID(PUBLIC_ID_PACKAGING_1_0, "DTD_PACKAGING_1_0", Project.class); 57 } 58 59 60 66 public Project(String name, String path) { 67 this.name = name; 68 this.path = path; 69 basedir = expandBasedir("."); 70 readTargets(); 72 } 73 74 75 80 public void setName(String name) { 81 this.name = name; 82 } 83 84 85 90 public void setPath(String path) { 91 this.path = path; 92 } 93 94 95 100 public String getPath() { 101 return path; 102 } 103 104 public String getDir() { 105 if (path!=null) { 106 int pos=path.lastIndexOf("/"); 107 if (pos==-1) pos=path.lastIndexOf("\\"); 108 if (pos!=-1) return path.substring(0,pos+1); 109 } 110 return null; 111 } 112 113 118 public String getName() { 119 return name; 120 } 121 122 123 128 public String getBaseDir() { 129 return basedir; 130 } 131 132 133 138 public Iterator getTargets() { 139 return targets.values().iterator(); 140 } 141 142 143 149 public boolean deleteTarget(String name) { 150 targets.remove(name); 152 packagetargets.remove(name); 153 bundletargets.remove(name); 154 save(); 155 return true; 156 } 157 158 159 167 public boolean addBundleTarget(String name, String type, String path) { 168 if (name.equals("") || name.equals("[auto]")) { 170 name = type.substring(type.indexOf("/") + 1); 171 } 172 if (path.equals("") || path.equals("[auto]")) { 173 path = "packaging" + File.separator + getName() + "_" + type.replace('/','_') + ".xml"; 174 path = path.replace(' ', '_'); 175 } 176 177 String dirsp = basedir + path.substring(0, path.lastIndexOf(File.separator)); 179 File dirs = new File (dirsp); 180 if (!dirs.exists()) { 181 dirs.mkdirs(); 182 } 183 184 Target t = new Target(this,name); 185 t.setBundle(true); 186 t.setBaseDir(basedir); 187 t.setPath(path); 188 t.setType(type); 189 CreatorInterface cr = ProjectManager.getCreatorByType(type); 191 if (cr != null) { 192 t.setCreator(cr); 193 } 194 bundletargets.put(name, t); 195 save(); 196 return true; 197 } 198 199 200 208 public boolean addPackageTarget(String name, String type, String path) { 209 CreatorInterface cr = ProjectManager.getCreatorByType(type); 210 if (name.equals("") || name.equals("[auto]")) { 211 if (cr != null) { 212 name = cr.getDefaultTargetName(); 213 name = checkDubName(name); 214 } else { 215 name = type.substring(type.indexOf("/") + 1); 216 } 217 } 218 if (path.equals("") || path.equals("[auto]")) { 219 path = "packaging" + File.separator + getName() + "_" + type.replace('/','_') + ".xml"; 220 path = path.replace(' ', '_'); 221 path = checkDubFilename(path); 222 } 223 String dirsp = basedir + path.substring(0, path.lastIndexOf(File.separator)); 225 File dirs = new File (dirsp); 226 if (!dirs.exists()) { 227 dirs.mkdirs(); 228 } 229 Target t = new Target(this,name); 230 t.setPath(path); 231 t.setBaseDir(basedir); 232 t.setType(type); 233 if (cr != null) { 234 t.setCreator(cr); 235 t.setDefaults(); 236 } 237 packagetargets.put(name, t); 238 save(); 239 return true; 240 } 241 242 243 249 public Target getTarget(String name) { 250 Object o = targets.get(name); 251 if (o != null) { 252 return (Target) o; 253 } 254 o = packagetargets.get(name); 255 if (o != null) { 256 return (Target) o; 257 } 258 o = bundletargets.get(name); 259 if (o != null) { 260 return (Target) o; 261 } 262 return null; 263 } 264 265 266 272 public Target getTargetById(String id) { 273 Iterator e = packagetargets.values().iterator(); 275 while (e.hasNext()) { 276 Target t = (Target) e.next(); 277 if (t.getId().equals(id)) { 278 return t; 279 } 280 } 281 return null; 282 } 283 284 285 290 public Iterator getPackageTargets() { 291 return packagetargets.values().iterator(); 292 } 293 294 295 300 public Iterator getBundleTargets() { 301 return bundletargets.values().iterator(); 302 } 303 304 305 308 public void readTargets() { 309 File file = new File (path); 310 if (file.exists()) { 311 ExtendedDocumentReader reader = new ExtendedDocumentReader(path, Project.class); 312 if (reader != null) { 313 314 org.w3c.dom.Node n2 = reader.getElementByPath("packaging"); 315 if (n2 != null) { 316 NamedNodeMap nm = n2.getAttributes(); 317 if (nm != null) { 318 org.w3c.dom.Node n3 = nm.getNamedItem("basedir"); 319 if (n3 != null) { 320 basedir = n3.getNodeValue(); 321 if (basedir.equals(".")) { 322 basedir = expandBasedir(basedir); 323 } 324 } 325 } 326 } 327 328 for (Iterator ns = reader.getChildElements("packaging", "target"); ns.hasNext(); ) { 330 Element n = (Element ) ns.next(); 331 NamedNodeMap nm = n.getAttributes(); 332 if (nm != null) { 333 String name = null; 334 String depends = null; 335 336 org.w3c.dom.Node n3 = nm.getNamedItem("name"); 338 if (n3 != null) { 339 name = n3.getNodeValue(); 340 } 341 n3 = nm.getNamedItem("depends"); 343 if (n3 != null) { 344 depends = n3.getNodeValue(); 345 } 346 347 if (name != null) { 348 Target t = new Target(this,name); 349 if (depends != null) { 350 t.setDepends(depends); 351 } 352 targets.put(name, t); 353 } 354 } 355 } 356 357 for (Iterator ns = reader.getChildElements("packaging", "package"); ns.hasNext(); ) { 359 Element n = (Element ) ns.next(); 360 NamedNodeMap nm = n.getAttributes(); 361 if (nm != null) { 362 String name = null; 363 String type = null; 364 String path = null; 365 366 org.w3c.dom.Node n3 = nm.getNamedItem("name"); 368 if (n3 != null) { 369 name = n3.getNodeValue(); 370 } 371 n3 = nm.getNamedItem("file"); 373 if (n3 != null) { 374 path = n3.getNodeValue(); 375 } 376 377 n3 = nm.getNamedItem("type"); 379 if (n3 != null) { 380 type = n3.getNodeValue(); 381 } 382 383 if (name != null) { 384 Target t = new Target(this,name); 385 if (path != null) { 386 t.setBaseDir(basedir); 387 t.setPath(path); 388 } 389 if (type != null) { 390 t.setType(type); 391 CreatorInterface cr = ProjectManager.getCreatorByType(type); 393 if (cr != null) { 394 t.setCreator(cr); 395 } 396 } 397 packagetargets.put(name, t); 398 } 399 } 400 } 401 402 for (Iterator ns = reader.getChildElements("packaging", "bundle"); ns.hasNext(); ) { 404 Element n = (Element ) ns.next(); 405 NamedNodeMap nm = n.getAttributes(); 406 if (nm != null) { 407 String name = null; 408 String type = "bundle/basic"; 409 String depends = null; 410 String path = null; 411 412 org.w3c.dom.Node n3 = nm.getNamedItem("name"); 414 if (n3 != null) { 415 name = n3.getNodeValue(); 416 } 417 n3 = nm.getNamedItem("type"); 419 if (n3 != null) { 420 type = n3.getNodeValue(); 421 } 422 n3 = nm.getNamedItem("file"); 424 if (n3 != null) { 425 path = n3.getNodeValue(); 426 } 427 n3 = nm.getNamedItem("depends"); 429 if (n3 != null) { 430 depends = n3.getNodeValue(); 431 } 432 433 if (name != null) { 434 Target t = new Target(this,name); 435 t.setBundle(true); 436 if (depends != null) { 437 t.setDepends(depends); 438 } 439 if (path != null) { 440 t.setPath(path); 441 t.setBaseDir(basedir); 442 } 443 if (type != null) { 444 t.setType(type); 445 CreatorInterface cr = ProjectManager.getCreatorByType(type); 447 if (cr != null) { 448 t.setCreator(cr); 449 } 450 } 451 452 bundletargets.put(name, t); 453 } 454 } 455 } 456 457 } 458 } else { 459 log.error("missing projects file : " + path); 460 } 461 } 462 463 464 470 private String expandBasedir(String basedir) { 471 File basefile = new File (path); 472 if (basefile != null) { 473 return basefile.getParent() + File.separator; 474 } 475 return basedir; 476 } 477 478 479 484 public boolean save() { 485 String body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 486 body += "<!DOCTYPE packaging PUBLIC \"-//MMBase/DTD packaging config 1.0//EN\" \"http://www.mmbase.org/dtd/packaging_1_0.dtd\">\n"; 487 body += "<packaging basedir=\".\">\n"; 488 Iterator e = packagetargets.values().iterator(); 489 while (e.hasNext()) { 490 Target t = (Target) e.next(); 491 body += "\t<package name=\"" + t.getName() + "\" type=\"" + t.getType() + "\" file=\"" + t.getPath() + "\" />\n"; 492 } 493 e = bundletargets.values().iterator(); 494 while (e.hasNext()) { 495 Target t = (Target) e.next(); 496 body += "\t<bundle name=\"" + t.getName() + "\" type=\"" + t.getType() + "\" file=\"" + t.getPath() + "\" />\n"; 497 } 498 body += "</packaging>\n"; 499 File sfile = new File (path); 500 501 String dirsp = path.substring(0, path.lastIndexOf(File.separator)); 503 File dirs = new File (dirsp); 504 if (!dirs.exists()) { 505 dirs.mkdirs(); 506 } 507 try { 508 DataOutputStream scan = new DataOutputStream (new FileOutputStream (sfile)); 509 scan.writeBytes(body); 510 scan.flush(); 511 scan.close(); 512 } catch (Exception f) { 513 log.error("Can't save packaging file : " + path); 514 return false; 515 } 516 return true; 517 } 518 519 520 525 public boolean hasSyntaxErrors() { 526 Iterator e = packagetargets.values().iterator(); 529 while (e.hasNext()) { 530 Target t = (Target) e.next(); 531 if (t.hasSyntaxErrors()) { 532 return true; 533 } 534 } 535 e = bundletargets.values().iterator(); 536 while (e.hasNext()) { 537 Target t = (Target) e.next(); 538 if (t.hasSyntaxErrors()) { 539 return true; 540 } 541 } 542 return false; 543 } 544 545 private String checkDubName(String name) { 546 boolean dub = true; 547 int counter = 2; 548 String newname = name; 549 while (dub) { 550 Target t = getTarget(newname); 551 if (t != null) { 552 newname = name + (counter++); 553 } else { 554 dub = false; 555 } 556 } 557 return newname; 558 } 559 560 561 private String checkDubFilename(String filename) { 562 boolean dub = true; 563 int counter = 2; 564 String newfilename = filename; 565 while (dub) { 566 File t=new File (basedir+File.separator+newfilename); 567 if (t.exists()) { 568 newfilename = filename.substring(0,filename.length()-4) + (counter++)+".xml"; 569 } else { 570 dub = false; 571 } 572 } 573 return newfilename; 574 } 575 576 } 577 578 | Popular Tags |