1 11 package org.eclipse.pde.ui.templates; 12 import java.io.ByteArrayInputStream ; 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.InputStreamReader ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.MissingResourceException ; 25 import java.util.ResourceBundle ; 26 import java.util.zip.ZipEntry ; 27 import java.util.zip.ZipException ; 28 import java.util.zip.ZipFile ; 29 30 import org.eclipse.core.resources.IContainer; 31 import org.eclipse.core.resources.IFile; 32 import org.eclipse.core.resources.IFolder; 33 import org.eclipse.core.resources.IProject; 34 import org.eclipse.core.runtime.CoreException; 35 import org.eclipse.core.runtime.FileLocator; 36 import org.eclipse.core.runtime.IPath; 37 import org.eclipse.core.runtime.IProgressMonitor; 38 import org.eclipse.core.runtime.Path; 39 import org.eclipse.jdt.core.IClasspathEntry; 40 import org.eclipse.jdt.core.IJavaProject; 41 import org.eclipse.jdt.core.JavaCore; 42 import org.eclipse.jdt.core.JavaModelException; 43 import org.eclipse.jface.wizard.Wizard; 44 import org.eclipse.pde.core.plugin.IPlugin; 45 import org.eclipse.pde.core.plugin.IPluginBase; 46 import org.eclipse.pde.core.plugin.IPluginExtension; 47 import org.eclipse.pde.core.plugin.IPluginModel; 48 import org.eclipse.pde.core.plugin.IPluginModelBase; 49 import org.eclipse.pde.core.plugin.IPluginReference; 50 import org.eclipse.pde.internal.core.TargetPlatformHelper; 51 import org.eclipse.pde.internal.core.ibundle.IBundlePluginBase; 52 import org.eclipse.pde.internal.ui.PDEUIMessages; 53 import org.eclipse.pde.internal.ui.wizards.templates.ControlStack; 54 import org.eclipse.pde.internal.ui.wizards.templates.PluginReference; 55 56 63 64 public abstract class AbstractTemplateSection 65 implements 66 ITemplateSection, 67 IVariableProvider { 68 69 72 protected IProject project; 73 76 protected IPluginModelBase model; 77 81 public static final String KEY_PLUGIN_CLASS = "pluginClass"; 83 88 public static final String KEY_ACTIVATOR_SIMPLE = "activator"; 93 public static final String KEY_PLUGIN_ID = "pluginId"; 98 public static final String KEY_PLUGIN_NAME = "pluginName"; 103 public static final String KEY_PACKAGE_NAME = "packageName"; 105 private boolean pagesAdded = false; 106 113 public String getReplacementString(String fileName, String key) { 114 String result = getKeyValue(key); 115 return result != null ? result : key; 116 } 117 118 121 122 public Object getValue(String key) { 123 return getKeyValue(key); 124 } 125 126 private String getKeyValue(String key) { 127 if (model == null) 128 return null; 129 130 if (key.equals(KEY_PLUGIN_CLASS) && model instanceof IPluginModel) { 131 IPlugin plugin = (IPlugin) model.getPluginBase(); 132 return plugin.getClassName(); 133 } 134 135 if (key.equals(KEY_ACTIVATOR_SIMPLE) && model instanceof IPluginModel) { 136 IPlugin plugin = (IPlugin) model.getPluginBase(); 137 String qualified = plugin.getClassName(); 138 if (qualified != null) { 139 int lastDot = qualified.lastIndexOf('.'); 140 return (lastDot != -1 && lastDot < qualified.length() - 1) ? qualified.substring(lastDot + 1) : qualified; 141 } 142 } 143 if (key.equals(KEY_PLUGIN_ID)) { 144 IPluginBase plugin = model.getPluginBase(); 145 return plugin.getId(); 146 } 147 if (key.equals(KEY_PLUGIN_NAME)) { 148 IPluginBase plugin = model.getPluginBase(); 149 return plugin.getTranslatedName(); 150 } 151 152 if (key.equals(KEY_PACKAGE_NAME) && model instanceof IPluginModel) { 153 IPlugin plugin = (IPlugin) model.getPluginBase(); 154 String qualified = plugin.getClassName(); 155 if (qualified != null) { 156 int lastDot = qualified.lastIndexOf('.'); 157 return (lastDot != -1) ? qualified.substring(0, lastDot) : qualified; 158 } 159 } 160 return null; 161 } 162 165 public URL getTemplateLocation() { 166 return null; 167 } 168 171 public String getDescription() { 172 return ""; } 174 183 public String getPluginResourceString(String key) { 184 ResourceBundle bundle = getPluginResourceBundle(); 185 if (bundle == null) 186 return key; 187 try { 188 return bundle.getString(key); 189 } catch (MissingResourceException e) { 190 return key; 191 } 192 } 193 202 protected abstract ResourceBundle getPluginResourceBundle(); 203 204 207 public void addPages(Wizard wizard) { 208 } 209 210 216 public boolean getPagesAdded() { 217 return pagesAdded; 218 } 219 220 226 protected void markPagesAdded() { 227 pagesAdded = true; 228 } 229 230 236 public int getNumberOfWorkUnits() { 237 return 1; 238 } 239 240 243 public IPluginReference[] getDependencies(String schemaVersion) { 244 return new IPluginReference[]{new PluginReference("org.eclipse.ui", null, 0)}; 246 } 247 248 259 260 protected IFolder getSourceFolder(IProgressMonitor monitor) 261 throws CoreException { 262 IFolder sourceFolder = null; 263 264 try { 265 IJavaProject javaProject = JavaCore.create(project); 266 IClasspathEntry[] classpath = javaProject.getRawClasspath(); 267 for (int i = 0; i < classpath.length; i++) { 268 IClasspathEntry entry = classpath[i]; 269 if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { 270 IPath path = entry.getPath().removeFirstSegments(1); 271 if (path.segmentCount() > 0) 272 sourceFolder = project.getFolder(path); 273 break; 274 } 275 } 276 } catch (JavaModelException e) { 277 } 278 return sourceFolder; 279 } 280 281 290 protected void generateFiles(IProgressMonitor monitor) throws CoreException { 291 generateFiles(monitor, getTemplateLocation()); 292 } 293 294 314 protected void generateFiles(IProgressMonitor monitor, URL locationUrl) throws CoreException { 315 monitor.setTaskName(PDEUIMessages.AbstractTemplateSection_generating); 316 317 if (locationUrl == null) { 318 return; 319 } 320 try { 321 locationUrl = FileLocator.resolve(locationUrl); 322 locationUrl = FileLocator.toFileURL(locationUrl); 323 } catch (IOException e) { 324 return; 325 } 326 if ("file".equals(locationUrl.getProtocol())) { File templateDirectory = new File (locationUrl.getFile()); 328 if (!templateDirectory.exists()) 329 return; 330 generateFiles(templateDirectory, project, true, false, monitor); 331 } else if ("jar".equals(locationUrl.getProtocol())) { String file = locationUrl.getFile(); 333 int exclamation = file.indexOf('!'); 334 if (exclamation < 0) 335 return; 336 URL fileUrl = null; 337 try { 338 fileUrl = new URL (file.substring(0, exclamation)); 339 } catch (MalformedURLException mue) { 340 return; 341 } 342 File pluginJar = new File (fileUrl.getFile()); 343 if (!pluginJar.exists()) 344 return; 345 String templateDirectory = file.substring(exclamation + 1); IPath path = new Path(templateDirectory); 347 ZipFile zipFile = null; 348 try { 349 zipFile = new ZipFile (pluginJar); 350 generateFiles(zipFile, path, project, true, false, monitor); 351 } catch (ZipException ze) { 352 } catch (IOException ioe) { 353 } finally { 354 if (zipFile != null) { 355 try { 356 zipFile.close(); 357 } catch (IOException e) { 358 } 359 } 360 } 361 362 } 363 monitor.subTask(""); monitor.worked(1); 365 } 366 367 378 protected boolean isOkToCreateFolder(File sourceFolder) { 379 return true; 380 } 381 382 394 protected boolean isOkToCreateFile(File sourceFile) { 395 return true; 396 } 397 398 405 protected abstract void updateModel(IProgressMonitor monitor) 406 throws CoreException; 407 408 416 public void execute(IProject project, IPluginModelBase model, 417 IProgressMonitor monitor) throws CoreException { 418 this.project = project; 419 this.model = model; 420 generateFiles(monitor); 421 updateModel(monitor); 422 } 423 435 protected IPluginExtension createExtension(String pointId, boolean reuse) 436 throws CoreException { 437 if (reuse) { 438 IPluginExtension[] extensions = model.getPluginBase() 439 .getExtensions(); 440 for (int i = 0; i < extensions.length; i++) { 441 IPluginExtension extension = extensions[i]; 442 if (extension.getPoint().equalsIgnoreCase(pointId)) { 443 return extension; 444 } 445 } 446 } 447 IPluginExtension extension = model.getFactory().createExtension(); 448 extension.setPoint(pointId); 449 return extension; 450 } 451 452 private void generateFiles(File src, IContainer dst, boolean firstLevel, 453 boolean binary, IProgressMonitor monitor) throws CoreException { 454 File [] members = src.listFiles(); 455 456 for (int i = 0; i < members.length; i++) { 457 File member = members[i]; 458 if (member.isDirectory()) { 459 IContainer dstContainer = null; 460 461 if (firstLevel) { 462 binary = false; 463 if (!isOkToCreateFolder(member)) 464 continue; 465 466 if (member.getName().equals("java")) { IFolder sourceFolder = getSourceFolder(monitor); 468 dstContainer = generateJavaSourceFolder(sourceFolder, monitor); 469 } else if (member.getName().equals("bin")) { binary = true; 471 dstContainer = dst; 472 } 473 } 474 if (dstContainer == null) { 475 if (isOkToCreateFolder(member) == false) 476 continue; 477 String folderName = getProcessedString(member.getName(), 478 member.getName()); 479 dstContainer = dst.getFolder(new Path(folderName)); 480 } 481 if (dstContainer instanceof IFolder && !dstContainer.exists()) 482 ((IFolder) dstContainer).create(true, true, monitor); 483 generateFiles(member, dstContainer, false, binary, monitor); 484 } else { 485 if (isOkToCreateFile(member)) { 486 if (firstLevel) 487 binary = false; 488 InputStream in = null; 489 try { 490 in = new FileInputStream (member); 491 copyFile(member.getName(), in, dst, binary, monitor); 492 } catch (IOException ioe) { 493 } finally { 494 if (in != null) 495 try { 496 in.close(); 497 } catch (IOException ioe2) { 498 } 499 } 500 } 501 } 502 } 503 } 504 505 private void generateFiles(ZipFile zipFile, IPath path, IContainer dst, 506 boolean firstLevel, boolean binary, IProgressMonitor monitor) 507 throws CoreException { 508 int pathLength = path.segmentCount(); 509 Map childZipEntries = new HashMap (); 512 for (Enumeration zipEntries = zipFile.entries(); zipEntries 513 .hasMoreElements();) { 514 ZipEntry zipEntry = (ZipEntry ) zipEntries.nextElement(); 515 IPath entryPath = new Path(zipEntry.getName()); 516 if (entryPath.segmentCount() <= pathLength) { 517 continue; 519 } 520 if (!path.isPrefixOf(entryPath)) { 521 continue; 523 } 524 if (entryPath.segmentCount() == pathLength + 1) { 525 childZipEntries.put(zipEntry.getName(), zipEntry); 526 } else { 527 String name = entryPath.uptoSegment( 528 pathLength + 1).addTrailingSeparator().toString(); 529 if(!childZipEntries.containsKey(name)){ 530 ZipEntry dirEntry = new ZipEntry (name); 531 childZipEntries.put(name, dirEntry); 532 } 533 } 534 } 535 536 for (Iterator it = childZipEntries.values().iterator(); it.hasNext();) { 537 ZipEntry zipEnry = (ZipEntry ) it.next(); 538 String name = new Path(zipEnry.getName()).lastSegment().toString(); 539 if (zipEnry.isDirectory()) { 540 IContainer dstContainer = null; 541 542 if (firstLevel) { 543 binary = false; 544 if (name.equals("java")) { IFolder sourceFolder = getSourceFolder(monitor); 546 dstContainer = generateJavaSourceFolder(sourceFolder, 547 monitor); 548 } else if (name.equals("bin")) { binary = true; 550 dstContainer = dst; 551 } 552 } 553 if (dstContainer == null) { 554 if (isOkToCreateFolder(new File (path.toFile(), name)) == false) 555 continue; 556 String folderName = getProcessedString(name, name); 557 dstContainer = dst.getFolder(new Path(folderName)); 558 } 559 if (dstContainer instanceof IFolder && !dstContainer.exists()) 560 ((IFolder) dstContainer).create(true, true, monitor); 561 generateFiles(zipFile, path.append(name), dstContainer, false, 562 binary, monitor); 563 } else { 564 if (isOkToCreateFile(new File (path.toFile(), name))) { 565 if (firstLevel) 566 binary = false; 567 InputStream in = null; 568 try { 569 in = zipFile.getInputStream(zipEnry); 570 copyFile(name, in, dst, binary, monitor); 571 } catch (IOException ioe) { 572 } finally { 573 if (in != null) 574 try { 575 in.close(); 576 } catch (IOException ioe2) { 577 } 578 } 579 } 580 } 581 } 582 } 583 584 private IFolder generateJavaSourceFolder(IFolder sourceFolder, 585 IProgressMonitor monitor) throws CoreException { 586 Object packageValue = getValue(KEY_PACKAGE_NAME); 587 String packageName = packageValue != null 588 ? packageValue.toString() 589 : null; 590 if (packageName == null) 591 packageName = model.getPluginBase().getId(); 592 IPath path = new Path(packageName.replace('.', File.separatorChar)); 593 if (sourceFolder != null) 594 path = sourceFolder.getProjectRelativePath().append(path); 595 596 for (int i = 1; i <= path.segmentCount(); i++) { 597 IPath subpath = path.uptoSegment(i); 598 IFolder subfolder = project.getFolder(subpath); 599 if (subfolder.exists() == false) 600 subfolder.create(true, true, monitor); 601 } 602 return project.getFolder(path); 603 } 604 605 private void copyFile(String fileName, InputStream input, IContainer dst, boolean binary, 606 IProgressMonitor monitor) throws CoreException { 607 String targetFileName = getProcessedString(fileName, fileName); 608 609 monitor.subTask(targetFileName); 610 IFile dstFile = dst.getFile(new Path(targetFileName)); 611 612 try { 613 InputStream stream = getProcessedStream(fileName, input, binary); 614 if (dstFile.exists()) { 615 dstFile.setContents(stream, true, true, monitor); 616 } else { 617 dstFile.create(stream, true, monitor); 618 } 619 stream.close(); 620 621 } catch (IOException e) { 622 } 623 } 624 625 private String getProcessedString(String fileName, String source) { 626 if (source.indexOf('$') == -1) 627 return source; 628 int loc = -1; 629 StringBuffer buffer = new StringBuffer (); 630 boolean replacementMode = false; 631 for (int i = 0; i < source.length(); i++) { 632 char c = source.charAt(i); 633 if (c == '$') { 634 if (replacementMode) { 635 String key = source.substring(loc, i); 636 String value = key.length() == 0 ? "$" : getReplacementString(fileName, key); 638 buffer.append(value); 639 replacementMode = false; 640 } else { 641 replacementMode = true; 642 loc = i + 1; 643 continue; 644 } 645 } else if (!replacementMode) 646 buffer.append(c); 647 } 648 return buffer.toString(); 649 } 650 651 private InputStream getProcessedStream(String fileName, InputStream stream, boolean binary) 652 throws IOException , CoreException { 653 if (binary) 654 return stream; 655 656 InputStreamReader reader = new InputStreamReader (stream); 657 int bufsize = 1024; 658 char[] cbuffer = new char[bufsize]; 659 int read = 0; 660 StringBuffer keyBuffer = new StringBuffer (); 661 StringBuffer outBuffer = new StringBuffer (); 662 StringBuffer preBuffer = new StringBuffer (); 663 boolean newLine = true; 664 ControlStack preStack = new ControlStack(); 665 preStack.setValueProvider(this); 666 667 boolean replacementMode = false; 668 boolean preprocessorMode = false; 669 boolean escape = false; 670 while (read != -1) { 671 read = reader.read(cbuffer); 672 for (int i = 0; i < read; i++) { 673 char c = cbuffer[i]; 674 675 if (escape) { 676 StringBuffer buf = preprocessorMode ? preBuffer : outBuffer; 677 buf.append(c); 678 escape = false; 679 continue; 680 } 681 682 if (newLine && c == '%') { 683 preprocessorMode = true; 685 preBuffer.delete(0, preBuffer.length()); 686 continue; 687 } 688 if (preprocessorMode) { 689 if (c == '\\') { 690 escape = true; 691 continue; 692 } 693 if (c == '\n') { 694 preprocessorMode = false; 696 newLine = true; 697 String line = preBuffer.toString().trim(); 698 preStack.processLine(line); 699 continue; 700 } 701 preBuffer.append(c); 702 703 continue; 704 } 705 706 if (preStack.getCurrentState() == false) { 707 continue; 708 } 709 710 if (c == '$') { 711 if (replacementMode) { 712 replacementMode = false; 713 String key = keyBuffer.toString(); 714 String value = key.length() == 0 ? "$" : getReplacementString(fileName, key); 716 outBuffer.append(value); 717 keyBuffer.delete(0, keyBuffer.length()); 718 } else { 719 replacementMode = true; 720 } 721 } else { 722 if (replacementMode) 723 keyBuffer.append(c); 724 else { 725 outBuffer.append(c); 726 if (c == '\n') { 727 newLine = true; 728 } else 729 newLine = false; 730 } 731 } 732 } 733 } 734 return new ByteArrayInputStream (outBuffer.toString().getBytes(project.getDefaultCharset())); 735 } 736 737 protected double getTargetVersion() { 738 try { 739 IPluginBase plugin = model.getPluginBase(); 740 if (plugin instanceof IBundlePluginBase) 741 return Double.parseDouble(((IBundlePluginBase)plugin).getTargetVersion()); 742 } catch (NumberFormatException e) { 743 } 744 return TargetPlatformHelper.getTargetVersion(); 745 } 746 747 } 748 | Popular Tags |