1 17 package org.eclipse.emf.codegen.jet; 18 19 20 import java.io.BufferedInputStream ; 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.StringReader ; 26 import java.io.StringWriter ; 27 import java.util.ArrayList ; 28 import java.util.Collections ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.StringTokenizer ; 32 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 import javax.xml.transform.OutputKeys ; 37 import javax.xml.transform.Transformer ; 38 import javax.xml.transform.TransformerException ; 39 import javax.xml.transform.TransformerFactory ; 40 import javax.xml.transform.dom.DOMSource ; 41 import javax.xml.transform.stream.StreamResult ; 42 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.Text ; 47 import org.xml.sax.InputSource ; 48 import org.xml.sax.SAXException ; 49 50 import org.eclipse.core.resources.ICommand; 51 import org.eclipse.core.resources.IContainer; 52 import org.eclipse.core.resources.IFile; 53 import org.eclipse.core.resources.IFolder; 54 import org.eclipse.core.resources.IProject; 55 import org.eclipse.core.resources.IProjectDescription; 56 import org.eclipse.core.resources.IResource; 57 import org.eclipse.core.runtime.CoreException; 58 import org.eclipse.core.runtime.IPath; 59 import org.eclipse.core.runtime.IProgressMonitor; 60 import org.eclipse.core.runtime.NullProgressMonitor; 61 import org.eclipse.core.runtime.Path; 62 63 import org.eclipse.emf.codegen.CodeGenPlugin; 64 import org.eclipse.emf.common.util.URI; 65 66 67 public class JETNature implements IJETNature 68 { 69 74 public static JETNature getRuntime(IProject project) 75 { 76 try 77 { 78 JETNature a = (JETNature) project.getNature(IJETNature.NATURE_ID); 79 return a; 80 } 81 catch (CoreException e) 82 { 83 return null; 84 } 85 } 86 87 protected static final String BUILDER = "JETBuilder"; 88 protected static final String DEFAULT_TEMPLATE_CONTAINER_NAME = "templates"; 89 protected static final String JET_NATURE_PROPERTIES_FILE = ".jetproperties"; 90 protected static final String TEMPLATE_CONTAINER_NODE = "template-container"; 91 protected static final String SOURCE_CONTAINER_NODE = "source-container"; 92 protected static final String JET_SETTINGS_NODE = "jet-settings"; 93 94 protected IProject jetProject; 95 protected List jetTemplateContainers; 96 protected List jetTemplateSourceContainers; 97 protected IContainer jetJavaSourceContainer; 98 99 102 public JETNature() 103 { 104 super(); 105 } 106 107 public List getTemplateContainers() 108 { 109 if (jetTemplateContainers == null) 110 { 111 jetTemplateContainers = getTemplateContainersFromFile(); 112 } 113 return jetTemplateContainers; 114 } 115 116 public List getTemplateSourceContainers() 117 { 118 if (jetTemplateSourceContainers == null) 119 { 120 jetTemplateSourceContainers = getTemplateSourceContainersFromFile(); 121 } 122 return jetTemplateSourceContainers; 123 } 124 125 public IContainer getJavaSourceContainer() 126 { 127 if (jetJavaSourceContainer == null) 128 { 129 jetJavaSourceContainer = getJavaSourceContainerFromFile(); 130 } 131 return jetJavaSourceContainer; 132 } 133 134 public void setTemplateContainers(List templateContainers) 135 { 136 setTemplateContainers(templateContainers, templateContainers); 137 } 138 139 public void setTemplateContainers(List templateContainers, List templateSourceContainers) 140 { 141 jetTemplateContainers = templateContainers; 142 jetTemplateSourceContainers = templateSourceContainers; 143 try 144 { 145 setTemplateContainersToFile(templateContainers, templateSourceContainers); 146 } 147 catch (CoreException e) 148 { 149 CodeGenPlugin.write(e); 150 } 151 } 152 153 public void setJavaSourceContainer(IContainer javaSourceContainer) 154 { 155 jetJavaSourceContainer = javaSourceContainer; 156 try 157 { 158 setJavaSourceContainerToFile(javaSourceContainer); 159 } 160 catch (CoreException e) 161 { 162 CodeGenPlugin.write(e); 163 } 164 } 165 166 public void configure() throws CoreException 167 { 168 configure(new NullProgressMonitor()); 169 } 170 171 public void configure(IProgressMonitor monitor) throws CoreException 172 { 173 setDefaults(monitor); 174 175 addToFrontOfBuildSpec(CodeGenPlugin.INSTANCE.getSymbolicName() + "." + BUILDER); 178 } 179 180 183 public void setDefaults(IProgressMonitor monitor) throws CoreException 184 { 185 initTemplateContainer(monitor); 186 initJavaSourceContainer(monitor); 187 188 try 191 { 192 createDefaultJETSettingsFile(getTemplateContainers(), getJavaSourceContainer()); 193 } 194 catch (IOException e) 195 { 196 CodeGenPlugin.write(e); 197 } 198 } 199 200 public void deconfigure() throws CoreException 201 { 202 } 203 204 public IProject getProject() 205 { 206 return jetProject; 207 } 208 209 public void setProject(IProject project) 210 { 211 jetProject = project; 212 } 213 214 217 protected void initTemplateContainer(IProgressMonitor monitor) throws CoreException 218 { 219 IContainer templateFolder = getContainer(getProject(), DEFAULT_TEMPLATE_CONTAINER_NAME); 220 221 if (templateFolder instanceof IFolder && !templateFolder.exists()) 222 { 223 ((IFolder) templateFolder).create(true, true, monitor); 224 } 225 226 jetTemplateContainers = new ArrayList (); 227 jetTemplateContainers.add(templateFolder); 228 jetTemplateSourceContainers = new ArrayList (); 229 jetTemplateSourceContainers.add(templateFolder); 230 } 231 232 235 protected void initJavaSourceContainer(IProgressMonitor monitor) throws CoreException 236 { 237 IContainer sourceFolder = getContainer(getProject(), getDefaultSourcePath()); 238 if (sourceFolder instanceof IFolder && !sourceFolder.exists()) 239 { 240 ((IFolder)sourceFolder).create(true, true, monitor); 241 } 242 243 jetJavaSourceContainer = sourceFolder; 244 } 245 246 249 protected IPath getDefaultSourcePath() 250 { 251 IPath path = new Path(""); 252 return path; 253 } 254 255 258 protected void addToFrontOfBuildSpec(String builderID) throws CoreException 259 { 260 IProjectDescription description = getProject().getDescription(); 261 ICommand[] commands = description.getBuildSpec(); 262 boolean found = false; 263 for (int i = 0; i < commands.length; ++i) 264 { 265 if (commands[i].getBuilderName().equals(builderID)) 266 { 267 found = true; 268 break; 269 } 270 } 271 if (!found) 272 { 273 ICommand command = description.newCommand(); 274 command.setBuilderName(builderID); 275 ICommand [] newCommands = new ICommand [commands.length + 1]; 276 System.arraycopy(commands, 0, newCommands, 1, commands.length); 277 newCommands[0] = command; 278 description.setBuildSpec(newCommands); 279 getProject().setDescription(description, null); 280 } 281 } 282 283 286 public List getTemplateContainersFromFile() 287 { 288 List result = Collections.EMPTY_LIST; 289 290 try 291 { 292 Document document = parseJETSettings(); 293 if (document != null) 294 { 295 result = getContainerValues(document.getDocumentElement(), TEMPLATE_CONTAINER_NODE, false); 296 } 297 else 298 { 299 setDefaults(new NullProgressMonitor()); 300 result = getTemplateContainers(); 301 } 302 } 303 catch (Exception e) 304 { 305 try 306 { 307 setDefaults(new NullProgressMonitor()); 308 result = getTemplateContainers(); 309 } 310 catch (Exception ex) 311 { 312 CodeGenPlugin.write(ex); 313 } 314 } 315 316 return result; 317 } 318 319 322 protected List getTemplateSourceContainersFromFile() 323 { 324 List result = Collections.EMPTY_LIST; 325 326 try 327 { 328 Document document = parseJETSettings(); 329 if (document != null) 330 { 331 result = getContainerValues(document.getDocumentElement(), TEMPLATE_CONTAINER_NODE, true); 332 } 333 else 334 { 335 setDefaults(new NullProgressMonitor()); 336 result = getTemplateContainers(); 337 } 338 } 339 catch (Exception e) 340 { 341 try 342 { 343 setDefaults(new NullProgressMonitor()); 344 result = getTemplateContainers(); 345 } 346 catch (Exception ex) 347 { 348 CodeGenPlugin.write(ex); 349 } 350 } 351 352 return result; 353 } 354 355 358 public IContainer getJavaSourceContainerFromFile() 359 { 360 IContainer result = null; 361 362 try 363 { 364 Document document = parseJETSettings(); 365 if (document != null) 366 { 367 result = getContainerValue(document.getDocumentElement(), SOURCE_CONTAINER_NODE); 368 369 } 370 else 371 { 372 setDefaults(new NullProgressMonitor()); 373 result = getJavaSourceContainer(); 374 } 375 } 376 catch (Exception e) 377 { 378 try 379 { 380 setDefaults(new NullProgressMonitor()); 381 result = getJavaSourceContainer(); 382 } 383 catch (Exception ex) 384 { 385 CodeGenPlugin.write(ex); 386 } 387 } 388 389 return result; 390 } 391 392 395 protected Document parseJETSettings() throws ParserConfigurationException , SAXException , IOException , CoreException 396 { 397 Document document = null; 398 StringReader reader = readJETSettingFile(); 399 if (reader != null) 400 { 401 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 402 documentBuilderFactory.setNamespaceAware(true); 403 documentBuilderFactory.setValidating(false); 404 DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); 405 document = parser.parse(new InputSource (reader)); 406 if (!document.getDocumentElement().getNodeName().equalsIgnoreCase(JET_SETTINGS_NODE)) 407 { 408 CodeGenPlugin.write(new IOException (CodeGenPlugin.getPlugin().getString("_UI_MalformedJETPropertiesFile_exception"))); 409 } 410 reader.close(); 411 } 412 return document; 413 } 414 415 418 protected StringReader readJETSettingFile() throws CoreException, IOException 419 { 420 StringReader reader = null; 421 422 IFile jetSettingsFile = getProject().getFile(JET_NATURE_PROPERTIES_FILE); 423 if (jetSettingsFile.exists()) 424 { 425 InputStream input = jetSettingsFile.getContents(true); 426 427 String jetSettings = new String (readContentsAsBytes(input)); 428 reader = new StringReader (jetSettings); 429 } 430 return reader; 431 } 432 433 436 protected List getContainerValues(Element element, String localName) 437 { 438 return getContainerValues(element, localName, false); 439 } 440 441 444 protected List getContainerValues(Element element, String localName, boolean filter) 445 { 446 List result = new ArrayList (); 447 Element childElement = getChildWithLocalName(element, localName); 448 for (Node node = childElement.getFirstChild(); node != null; node = node.getNextSibling()) 449 { 450 if (node.getNodeType() == Node.TEXT_NODE) 451 { 452 result = getContainers(getProject(), node.getNodeValue(), filter); 453 break; 454 } 455 } 456 457 if (result.isEmpty()) 458 { 459 result.add(getProject()); 460 } 461 462 return result; 463 } 464 465 468 protected IContainer getContainerValue(Element element, String localName) 469 { 470 IContainer result = null; 471 Element childElement = getChildWithLocalName(element, localName); 472 for (Node node = childElement.getFirstChild(); node != null; node = node.getNextSibling()) 473 { 474 if (node.getNodeType() == Node.TEXT_NODE) 475 { 476 result = getContainer(getProject(), node.getNodeValue()); 477 break; 478 } 479 } 480 481 if (result == null) 482 { 483 result = getProject(); 484 } 485 486 return result; 487 } 488 489 protected void setContainerValues(List containers, Element element, String localName) 490 { 491 setContainerValues(containers, containers, element, localName); 492 } 493 494 497 protected void setContainerValues(List containers, List sourceContainers, Element element, String localName) 498 { 499 Element childElement = getChildWithLocalName(element, localName); 500 Text text = null; 501 for (Node node = childElement.getFirstChild(); node != null; node = node.getNextSibling()) 502 { 503 if (node.getNodeType() == Node.TEXT_NODE) 504 { 505 text = (Text )node; 506 break; 507 } 508 } 509 510 if (text == null) 511 { 512 text = element.getOwnerDocument().createTextNode(getContainers(jetProject, containers, sourceContainers)); 513 childElement.appendChild(text); 514 } 515 else 516 { 517 text.setNodeValue(getContainers(jetProject, containers, sourceContainers)); 518 } 519 } 520 521 524 protected void setContainerValue(IContainer container, Element element, String localName) 525 { 526 Element childElement = getChildWithLocalName(element, localName); 527 Text text = null; 528 for (Node node = childElement.getFirstChild(); node != null; node = node.getNextSibling()) 529 { 530 if (node.getNodeType() == Node.TEXT_NODE) 531 { 532 text = (Text )node; 533 break; 534 } 535 } 536 537 if (text == null) 538 { 539 text = element.getOwnerDocument().createTextNode(container.getProjectRelativePath().toString()); 540 childElement.appendChild(text); 541 } 542 else 543 { 544 text.setNodeValue(container.getProjectRelativePath().toString()); 545 } 546 } 547 548 551 protected Element getChildWithLocalName(Element element, String localName) 552 { 553 for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) 554 { 555 if (child.getNodeType() == Node.ELEMENT_NODE) 556 { 557 Element childElement = (Element )child; 558 if (childElement.getLocalName().equals(localName)) 559 { 560 return childElement; 561 } 562 } 563 } 564 565 return null; 566 } 567 568 571 public static byte[] readContentsAsBytes(InputStream input) throws IOException 572 { 573 BufferedInputStream bufferedInputStream = null; 574 try 575 { 576 final int BUF_SIZE = 8192; 577 byte[] buf = new byte[BUF_SIZE]; 578 int read; 579 int totalRead = 0; 580 bufferedInputStream = new BufferedInputStream (input); 581 while (totalRead < BUF_SIZE && (read = bufferedInputStream.read(buf, totalRead, BUF_SIZE - totalRead)) != -1) 582 { 583 totalRead += read; 584 } 585 if (totalRead < BUF_SIZE) 586 { 587 byte[] result = new byte[totalRead]; 588 System.arraycopy(buf, 0, result, 0, totalRead); 589 return result; 590 } 591 ByteArrayOutputStream out = new ByteArrayOutputStream (BUF_SIZE * 2); 592 out.write(buf); 593 while ((read = bufferedInputStream.read(buf, 0, BUF_SIZE)) != -1) 594 { 595 out.write(buf, 0, read); 596 } 597 return out.toByteArray(); 598 } 599 finally 600 { 601 try 602 { 603 if (bufferedInputStream != null) 604 { 605 bufferedInputStream.close(); 606 } 607 } 608 catch (IOException e) 609 { 610 CodeGenPlugin.write(e); 611 } 612 } 613 } 614 615 public void setTemplateContainersToFile(List templateContainers) throws CoreException 616 { 617 setTemplateContainersToFile(templateContainers, templateContainers); 618 } 619 620 623 public void setTemplateContainersToFile(List templateContainers, List templateSourceContainers) throws CoreException 624 { 625 Document document; 626 try 627 { 628 try 629 { 630 document = parseJETSettings(); 631 if (document != null) 632 { 633 setContainerValues(templateContainers, templateSourceContainers, document.getDocumentElement(), TEMPLATE_CONTAINER_NODE); 634 commitXML(document); 635 } 636 else 637 { 638 initJavaSourceContainer(new NullProgressMonitor()); 639 createDefaultJETSettingsFile(templateContainers, getJavaSourceContainer()); 640 } 641 } 642 catch (Exception e) 643 { 644 initJavaSourceContainer(new NullProgressMonitor()); 645 createDefaultJETSettingsFile(templateContainers, templateSourceContainers, getJavaSourceContainer()); 646 } 647 } 648 catch (Exception e) 649 { 650 CodeGenPlugin.write(e); 651 } 652 } 653 654 657 public void setJavaSourceContainerToFile(IContainer sourceContainer) throws CoreException 658 { 659 Document document; 660 try 661 { 662 try 663 { 664 document = parseJETSettings(); 665 if (document != null) 666 { 667 setContainerValue(sourceContainer, document.getDocumentElement(), SOURCE_CONTAINER_NODE); 668 commitXML(document); 669 } 670 else 671 { 672 initTemplateContainer(new NullProgressMonitor()); 673 createDefaultJETSettingsFile(getTemplateContainers(), sourceContainer); 674 } 675 } 676 catch (Exception e) 677 { 678 initTemplateContainer(new NullProgressMonitor()); 679 createDefaultJETSettingsFile(getTemplateContainers(), sourceContainer); 680 } 681 } 682 catch (Exception e) 683 { 684 CodeGenPlugin.write(e); 685 } 686 } 687 688 protected void createDefaultJETSettingsFile(List templateContainers, IContainer sourceContainer) 689 throws CoreException, IOException 690 { 691 createDefaultJETSettingsFile(templateContainers, templateContainers, sourceContainer); 692 } 693 694 697 protected void createDefaultJETSettingsFile(List templateContainers, List templateSourceContainers, IContainer sourceContainer) 698 throws CoreException, IOException 699 { 700 StringWriter writer = new StringWriter (); 701 703 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 704 writer.write("\n"); 705 writer.write("<" + JET_SETTINGS_NODE + ">"); 706 writer.write("\n"); 707 writer.write( 708 "\t<" 709 + TEMPLATE_CONTAINER_NODE 710 + ">" 711 + getContainers(jetProject, templateContainers, templateSourceContainers) 712 + "</" 713 + TEMPLATE_CONTAINER_NODE 714 + ">"); 715 writer.write( 716 "\t<" 717 + SOURCE_CONTAINER_NODE 718 + ">" 719 + sourceContainer.getProjectRelativePath().toString() 720 + "</" 721 + SOURCE_CONTAINER_NODE 722 + ">"); 723 writer.write("\n"); 724 writer.write("</" + JET_SETTINGS_NODE + ">"); 725 writer.write("\n"); 726 727 IFile jetSettingsFile = getProject().getFile(JET_NATURE_PROPERTIES_FILE); 728 InputStream sourceStream = new ByteArrayInputStream (writer.toString().getBytes()); 729 if (jetSettingsFile.exists()) 730 { 731 jetSettingsFile.setContents(sourceStream, true, true, null); 732 } 733 else 734 { 735 jetSettingsFile.create(sourceStream, true, null); 736 } 737 738 sourceStream.close(); 739 } 740 741 742 protected void commitXML(Document document) throws CoreException, ClassNotFoundException , IOException 743 { 744 IFile jetSettingsFile = getProject().getFile(JET_NATURE_PROPERTIES_FILE); 745 746 StringWriter writer = new StringWriter (); 747 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 748 try 749 { 750 Transformer transformer = transformerFactory.newTransformer(); 751 752 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 753 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 754 755 transformer.transform(new DOMSource (document), new StreamResult (writer)); 756 } 757 catch (TransformerException exception) 758 { 759 CodeGenPlugin.write(exception); 760 } 761 762 InputStream sourceStream = new ByteArrayInputStream (writer.toString().getBytes()); 763 if (jetSettingsFile.exists()) 764 { 765 jetSettingsFile.setContents(sourceStream, true, true, null); 766 } 767 else 768 { 769 jetSettingsFile.create(sourceStream, true, null); 770 } 771 772 sourceStream.close(); 773 } 774 775 public static IContainer getContainer(IProject project, IPath path) 776 { 777 IContainer result = project; 778 if (!path.isEmpty()) 779 { 780 if (path.getDevice() != null) 781 { 782 result = null; 783 } 784 else if (path.isAbsolute() && path.getDevice() == null) 785 { 786 IResource resource = project.getWorkspace().getRoot().findMember(path); 787 if (resource instanceof IContainer) 788 { 789 result = (IContainer)resource; 790 } 791 } 792 else 793 { 794 result = project.getFolder(path); 795 } 796 } 797 return result; 798 } 799 800 public static IContainer getContainer(IProject project, String path) 801 { 802 return getContainer(project, new Path(path)); 803 } 804 805 public static List getContainers(IProject project, String paths) 806 { 807 return getContainers(project, paths, false); 808 } 809 810 public static List getContainers(IProject project, String paths, boolean filter) 811 { 812 List result = new ArrayList (); 813 for (StringTokenizer stringTokenizer = new StringTokenizer (paths, " ;"); stringTokenizer.hasMoreTokens(); ) 814 { 815 String path = stringTokenizer.nextToken(); 816 if (path.startsWith("@")) 817 { 818 if (filter) continue; 819 path = path.substring(1); 820 } 821 IContainer container = getContainer(project, new Path(path)); 822 if (container == null) 823 { 824 URI uri = URI.createURI(path); 825 if (!uri.isRelative()) 826 { 827 result.add(uri); 828 } 829 } 830 else 831 { 832 result.add(container); 833 } 834 } 835 return result; 836 } 837 838 public static String getContainers(IProject project, List containers) 839 { 840 return getContainers(project, containers, containers); 841 } 842 843 public static String getContainers(IProject project, List containers, List sourceContainers) 844 { 845 StringBuffer result = new StringBuffer (); 846 for (Iterator i = containers.iterator(); i.hasNext(); ) 847 { 848 Object container = i.next(); 849 if (result.length() != 0) 850 { 851 result.append(";"); 852 } 853 if (!sourceContainers.contains(container)) 854 { 855 result.append("@"); 856 } 857 if (container instanceof IContainer) 858 { 859 result.append(getContainer(project, (IContainer)container)); 860 } 861 else if (container instanceof URI) 862 { 863 result.append(container); 864 } 865 } 866 return result.toString(); 867 } 868 869 public static String getContainer(IProject project, IContainer container) 870 { 871 return 872 (project == container.getProject() ? 873 container.getProjectRelativePath() : 874 container.getFullPath()).toString(); 875 } 876 } 877 | Popular Tags |