KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > jet > JETNature


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: JETNature.java,v 1.5 2005/06/08 06:15:57 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.jet;
18
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import javax.xml.parsers.DocumentBuilder JavaDoc;
34 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36 import javax.xml.transform.OutputKeys JavaDoc;
37 import javax.xml.transform.Transformer JavaDoc;
38 import javax.xml.transform.TransformerException JavaDoc;
39 import javax.xml.transform.TransformerFactory JavaDoc;
40 import javax.xml.transform.dom.DOMSource JavaDoc;
41 import javax.xml.transform.stream.StreamResult JavaDoc;
42
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45 import org.w3c.dom.Node JavaDoc;
46 import org.w3c.dom.Text JavaDoc;
47 import org.xml.sax.InputSource JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
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   /**
70    * Get a JETNature that corresponds to the supplied project.
71    * @return JETNature
72    * @param project IProject
73    */

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 JavaDoc BUILDER = "JETBuilder";
88   protected static final String JavaDoc DEFAULT_TEMPLATE_CONTAINER_NAME = "templates";
89   protected static final String JavaDoc JET_NATURE_PROPERTIES_FILE = ".jetproperties";
90   protected static final String JavaDoc TEMPLATE_CONTAINER_NODE = "template-container";
91   protected static final String JavaDoc SOURCE_CONTAINER_NODE = "source-container";
92   protected static final String JavaDoc JET_SETTINGS_NODE = "jet-settings";
93
94   protected IProject jetProject;
95   protected List JavaDoc jetTemplateContainers;
96   protected List JavaDoc jetTemplateSourceContainers;
97   protected IContainer jetJavaSourceContainer;
98
99   /**
100    * Constructor for JETNature.
101    */

102   public JETNature()
103   {
104     super();
105   }
106
107   public List JavaDoc getTemplateContainers()
108   {
109     if (jetTemplateContainers == null)
110     {
111       jetTemplateContainers = getTemplateContainersFromFile();
112     }
113     return jetTemplateContainers;
114   }
115   
116   public List JavaDoc 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 JavaDoc templateContainers)
135   {
136     setTemplateContainers(templateContainers, templateContainers);
137   }
138   
139   public void setTemplateContainers(List JavaDoc templateContainers, List JavaDoc 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     // Add JETBuilder
176
//
177
addToFrontOfBuildSpec(CodeGenPlugin.INSTANCE.getSymbolicName() + "." + BUILDER);
178   }
179   
180   /**
181    * Sets the containers to their defaults.
182    */

183   public void setDefaults(IProgressMonitor monitor) throws CoreException
184   {
185     initTemplateContainer(monitor);
186     initJavaSourceContainer(monitor);
187
188     // Create .jetproperties file
189
//
190
try
191     {
192       createDefaultJETSettingsFile(getTemplateContainers(), getJavaSourceContainer());
193     }
194     catch (IOException JavaDoc 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   /**
215    * Initializes the template container.
216    */

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 JavaDoc();
227     jetTemplateContainers.add(templateFolder);
228     jetTemplateSourceContainers = new ArrayList JavaDoc();
229     jetTemplateSourceContainers.add(templateFolder);
230   }
231
232   /**
233    * Initializes the Java Source Container
234    */

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   /**
247    * Returns the project's root directory
248    */

249   protected IPath getDefaultSourcePath()
250   {
251     IPath path = new Path("");
252     return path;
253   }
254
255   /**
256    * Adds a builder to the build spec for the given project.
257    */

258   protected void addToFrontOfBuildSpec(String JavaDoc 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   /**
284    * Returns the template path from the .jetproperties file.
285    */

286   public List JavaDoc getTemplateContainersFromFile()
287   {
288     List JavaDoc result = Collections.EMPTY_LIST;
289
290     try
291     {
292       Document JavaDoc 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 JavaDoc e)
304     {
305       try
306       {
307         setDefaults(new NullProgressMonitor());
308         result = getTemplateContainers();
309       }
310       catch (Exception JavaDoc ex)
311       {
312         CodeGenPlugin.write(ex);
313       }
314     }
315
316     return result;
317   }
318   
319   /**
320    * Returns the template source path from the .jetproperties file.
321    */

322   protected List JavaDoc getTemplateSourceContainersFromFile()
323   {
324     List JavaDoc result = Collections.EMPTY_LIST;
325
326     try
327     {
328       Document JavaDoc 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 JavaDoc e)
340     {
341       try
342       {
343         setDefaults(new NullProgressMonitor());
344         result = getTemplateContainers();
345       }
346       catch (Exception JavaDoc ex)
347       {
348         CodeGenPlugin.write(ex);
349       }
350     }
351
352     return result;
353   }
354
355   /**
356    * Returns the template file from the .jetproperties file
357    */

358   public IContainer getJavaSourceContainerFromFile()
359   {
360     IContainer result = null;
361
362     try
363     {
364       Document JavaDoc 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 JavaDoc e)
377     {
378       try
379       {
380         setDefaults(new NullProgressMonitor());
381         result = getJavaSourceContainer();
382       }
383       catch (Exception JavaDoc ex)
384       {
385         CodeGenPlugin.write(ex);
386       }
387     }
388
389     return result;
390   }
391
392   /**
393    * Parse the JET settings file for the root element.
394    */

395   protected Document JavaDoc parseJETSettings() throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc, CoreException
396   {
397     Document JavaDoc document = null;
398     StringReader JavaDoc reader = readJETSettingFile();
399     if (reader != null)
400     {
401       DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
402       documentBuilderFactory.setNamespaceAware(true);
403       documentBuilderFactory.setValidating(false);
404       DocumentBuilder JavaDoc parser = documentBuilderFactory.newDocumentBuilder();
405       document = parser.parse(new InputSource JavaDoc(reader));
406       if (!document.getDocumentElement().getNodeName().equalsIgnoreCase(JET_SETTINGS_NODE))
407       {
408         CodeGenPlugin.write(new IOException JavaDoc(CodeGenPlugin.getPlugin().getString("_UI_MalformedJETPropertiesFile_exception")));
409       }
410       reader.close();
411     }
412     return document;
413   }
414
415   /**
416    * Open the JET Settings file and return a StringReader on the contents
417    */

418   protected StringReader JavaDoc readJETSettingFile() throws CoreException, IOException JavaDoc
419   {
420     StringReader JavaDoc reader = null;
421
422     IFile jetSettingsFile = getProject().getFile(JET_NATURE_PROPERTIES_FILE);
423     if (jetSettingsFile.exists())
424     {
425       InputStream JavaDoc input = jetSettingsFile.getContents(true);
426
427       String JavaDoc jetSettings = new String JavaDoc(readContentsAsBytes(input));
428       reader = new StringReader JavaDoc(jetSettings);
429     }
430     return reader;
431   }
432
433   /**
434    * Returns the containers defined from the base Element passed in.
435    */

436   protected List JavaDoc getContainerValues(Element JavaDoc element, String JavaDoc localName)
437   {
438     return getContainerValues(element, localName, false);
439   }
440   
441   /**
442    * Returns the containers defined from the base Element passed in with entries starting with @ filtered out.
443    */

444   protected List JavaDoc getContainerValues(Element JavaDoc element, String JavaDoc localName, boolean filter)
445   {
446     List JavaDoc result = new ArrayList JavaDoc();
447     Element JavaDoc childElement = getChildWithLocalName(element, localName);
448     for (Node JavaDoc 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   /**
466    * Returns the container defined from the base Element passed in.
467    */

468   protected IContainer getContainerValue(Element JavaDoc element, String JavaDoc localName)
469   {
470     IContainer result = null;
471     Element JavaDoc childElement = getChildWithLocalName(element, localName);
472     for (Node JavaDoc 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 JavaDoc containers, Element JavaDoc element, String JavaDoc localName)
490   {
491     setContainerValues(containers, containers, element, localName);
492   }
493   
494   /**
495    * Sets the template container locations in the settings file
496    */

497   protected void setContainerValues(List JavaDoc containers, List JavaDoc sourceContainers, Element JavaDoc element, String JavaDoc localName)
498   {
499     Element JavaDoc childElement = getChildWithLocalName(element, localName);
500     Text JavaDoc text = null;
501     for (Node JavaDoc node = childElement.getFirstChild(); node != null; node = node.getNextSibling())
502     {
503       if (node.getNodeType() == Node.TEXT_NODE)
504       {
505         text = (Text JavaDoc)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   /**
522    * Sets the template container location in the settings file
523    */

524   protected void setContainerValue(IContainer container, Element JavaDoc element, String JavaDoc localName)
525   {
526     Element JavaDoc childElement = getChildWithLocalName(element, localName);
527     Text JavaDoc text = null;
528     for (Node JavaDoc node = childElement.getFirstChild(); node != null; node = node.getNextSibling())
529     {
530       if (node.getNodeType() == Node.TEXT_NODE)
531       {
532         text = (Text JavaDoc)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   /**
549    * Returns the child with the given local name.
550    */

551   protected Element JavaDoc getChildWithLocalName(Element JavaDoc element, String JavaDoc localName)
552   {
553     for (Node JavaDoc child = element.getFirstChild(); child != null; child = child.getNextSibling())
554     {
555       if (child.getNodeType() == Node.ELEMENT_NODE)
556       {
557         Element JavaDoc childElement = (Element JavaDoc)child;
558         if (childElement.getLocalName().equals(localName))
559         {
560           return childElement;
561         }
562       }
563     }
564
565     return null;
566   }
567
568   /**
569    * Reads an input stream and converts it to bytes
570    */

571   public static byte[] readContentsAsBytes(InputStream JavaDoc input) throws IOException JavaDoc
572   {
573     BufferedInputStream JavaDoc 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 JavaDoc(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 JavaDoc out = new ByteArrayOutputStream JavaDoc(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 JavaDoc e)
609       {
610         CodeGenPlugin.write(e);
611       }
612     }
613   }
614
615   public void setTemplateContainersToFile(List JavaDoc templateContainers) throws CoreException
616   {
617     setTemplateContainersToFile(templateContainers, templateContainers);
618   }
619   
620   /**
621    * Writes the Template Container Location to a file
622    */

623   public void setTemplateContainersToFile(List JavaDoc templateContainers, List JavaDoc templateSourceContainers) throws CoreException
624   {
625     Document JavaDoc 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 JavaDoc e)
643       {
644         initJavaSourceContainer(new NullProgressMonitor());
645         createDefaultJETSettingsFile(templateContainers, templateSourceContainers, getJavaSourceContainer());
646       }
647     }
648     catch (Exception JavaDoc e)
649     {
650       CodeGenPlugin.write(e);
651     }
652   }
653
654   /**
655    * Writes the Java Source Container Location to a file
656    */

657   public void setJavaSourceContainerToFile(IContainer sourceContainer) throws CoreException
658   {
659     Document JavaDoc 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 JavaDoc e)
677       {
678         initTemplateContainer(new NullProgressMonitor());
679         createDefaultJETSettingsFile(getTemplateContainers(), sourceContainer);
680       }
681     }
682     catch (Exception JavaDoc e)
683     {
684       CodeGenPlugin.write(e);
685     }
686   }
687
688   protected void createDefaultJETSettingsFile(List JavaDoc templateContainers, IContainer sourceContainer)
689     throws CoreException, IOException JavaDoc
690   {
691     createDefaultJETSettingsFile(templateContainers, templateContainers, sourceContainer);
692   }
693   
694   /**
695    * Writes the default file
696    */

697   protected void createDefaultJETSettingsFile(List JavaDoc templateContainers, List JavaDoc templateSourceContainers, IContainer sourceContainer)
698     throws CoreException, IOException JavaDoc
699   {
700     StringWriter JavaDoc writer = new StringWriter JavaDoc();
701     // Create a default .jetsettings file file
702

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 JavaDoc sourceStream = new ByteArrayInputStream JavaDoc(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 JavaDoc document) throws CoreException, ClassNotFoundException JavaDoc, IOException JavaDoc
743   {
744     IFile jetSettingsFile = getProject().getFile(JET_NATURE_PROPERTIES_FILE);
745
746     StringWriter JavaDoc writer = new StringWriter JavaDoc();
747     TransformerFactory JavaDoc transformerFactory = TransformerFactory.newInstance();
748     try
749     {
750       Transformer JavaDoc transformer = transformerFactory.newTransformer();
751
752       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
753       transformer.setOutputProperty(OutputKeys.METHOD, "xml");
754
755       transformer.transform(new DOMSource JavaDoc(document), new StreamResult JavaDoc(writer));
756     }
757     catch (TransformerException JavaDoc exception)
758     {
759       CodeGenPlugin.write(exception);
760     }
761
762     InputStream JavaDoc sourceStream = new ByteArrayInputStream JavaDoc(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 JavaDoc path)
801   {
802     return getContainer(project, new Path(path));
803   }
804
805   public static List JavaDoc getContainers(IProject project, String JavaDoc paths)
806   {
807     return getContainers(project, paths, false);
808   }
809   
810   public static List JavaDoc getContainers(IProject project, String JavaDoc paths, boolean filter)
811   {
812     List JavaDoc result = new ArrayList JavaDoc();
813     for (StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(paths, " ;"); stringTokenizer.hasMoreTokens(); )
814     {
815       String JavaDoc 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 JavaDoc getContainers(IProject project, List JavaDoc containers)
839   {
840     return getContainers(project, containers, containers);
841   }
842   
843   public static String JavaDoc getContainers(IProject project, List JavaDoc containers, List JavaDoc sourceContainers)
844   {
845     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
846     for (Iterator JavaDoc i = containers.iterator(); i.hasNext(); )
847     {
848       Object JavaDoc 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 JavaDoc getContainer(IProject project, IContainer container)
870   {
871     return
872       (project == container.getProject() ?
873         container.getProjectRelativePath() :
874         container.getFullPath()).toString();
875   }
876 }
877
Popular Tags