f803855fd0f0d9b80841d0f266dbf587a48cfb0e
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / JavaProject.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.io.BufferedInputStream;
14 import java.io.BufferedOutputStream;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.OutputStreamWriter;
24 import java.io.StringReader;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Hashtable;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
36 import javax.xml.parsers.SAXParserFactory;
37
38 import net.sourceforge.phpdt.core.IClasspathEntry;
39 import net.sourceforge.phpdt.core.ICompilationUnit;
40 import net.sourceforge.phpdt.core.IJavaElement;
41 import net.sourceforge.phpdt.core.IJavaModelMarker;
42 import net.sourceforge.phpdt.core.IJavaModelStatus;
43 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
44 import net.sourceforge.phpdt.core.IJavaProject;
45 import net.sourceforge.phpdt.core.IPackageFragment;
46 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
47 import net.sourceforge.phpdt.core.JavaCore;
48 import net.sourceforge.phpdt.core.JavaModelException;
49 import net.sourceforge.phpdt.core.WorkingCopyOwner;
50 import net.sourceforge.phpdt.internal.codeassist.ISearchableNameEnvironment;
51 import net.sourceforge.phpdt.internal.compiler.util.ObjectVector;
52 import net.sourceforge.phpdt.internal.core.util.Util;
53 import net.sourceforge.phpdt.internal.corext.Assert;
54 import net.sourceforge.phpeclipse.LoadPathEntry;
55 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
56
57 import org.eclipse.core.resources.ICommand;
58 import org.eclipse.core.resources.IFile;
59 import org.eclipse.core.resources.IFolder;
60 import org.eclipse.core.resources.IMarker;
61 import org.eclipse.core.resources.IProject;
62 import org.eclipse.core.resources.IProjectDescription;
63 import org.eclipse.core.resources.IProjectNature;
64 import org.eclipse.core.resources.IResource;
65 import org.eclipse.core.resources.IWorkspace;
66 import org.eclipse.core.resources.IWorkspaceRoot;
67 import org.eclipse.core.resources.ResourcesPlugin;
68 import org.eclipse.core.runtime.CoreException;
69 import org.eclipse.core.runtime.IPath;
70 import org.eclipse.core.runtime.IProgressMonitor;
71 import org.eclipse.core.runtime.Path;
72 import org.eclipse.core.runtime.Preferences;
73 import org.eclipse.core.runtime.QualifiedName;
74 import org.w3c.dom.Element;
75 import org.w3c.dom.Node;
76 import org.w3c.dom.NodeList;
77 import org.xml.sax.Attributes;
78 import org.xml.sax.ContentHandler;
79 import org.xml.sax.InputSource;
80 import org.xml.sax.Locator;
81 import org.xml.sax.SAXException;
82 import org.xml.sax.XMLReader;
83
84 /**
85  * Handle for a Java Project.
86  *
87  * <p>A Java Project internally maintains a devpath that corresponds
88  * to the project's classpath. The classpath may include source folders
89  * from the current project; jars in the current project, other projects,
90  * and the local file system; and binary folders (output location) of other
91  * projects. The Java Model presents source elements corresponding to output
92  * .class files in other projects, and thus uses the devpath rather than
93  * the classpath (which is really a compilation path). The devpath mimics
94  * the classpath, except has source folder entries in place of output
95  * locations in external projects.
96  *
97  * <p>Each JavaProject has a NameLookup facility that locates elements
98  * on by name, based on the devpath.
99  *
100  * @see IJavaProject
101  */
102 public class JavaProject 
103         extends Openable 
104         implements IJavaProject , IProjectNature {
105
106         /**
107          * Whether the underlying file system is case sensitive.
108          */
109         protected static final boolean IS_CASE_SENSITIVE = !new File("Temp").equals(new File("temp")); //$NON-NLS-1$ //$NON-NLS-2$
110
111         /**
112          * An empty array of strings indicating that a project doesn't have any prerequesite projects.
113          */
114         protected static final String[] NO_PREREQUISITES = new String[0];
115
116         /**
117          * The platform project this <code>IJavaProject</code> is based on
118          */
119         protected IProject project;
120         protected List fLoadPathEntries;
121         protected boolean fScratched;
122         
123         /**
124          * Name of file containing project classpath
125          */
126         public static final String CLASSPATH_FILENAME = ".classpath";  //$NON-NLS-1$
127
128         /**
129          * Name of file containing custom project preferences
130          */
131         public static final String PREF_FILENAME = ".jprefs";  //$NON-NLS-1$
132         
133         /**
134          * Value of the project's raw classpath if the .classpath file contains invalid entries.
135          */
136         public static final IClasspathEntry[] INVALID_CLASSPATH = new IClasspathEntry[0];
137
138         private static final String CUSTOM_DEFAULT_OPTION_VALUE = "#\r\n\r#custom-non-empty-default-value#\r\n\r#"; //$NON-NLS-1$
139         
140         /**
141          * Returns a canonicalized path from the given external path.
142          * Note that the return path contains the same number of segments
143          * and it contains a device only if the given path contained one.
144          * @see java.io.File for the definition of a canonicalized path
145          */
146         public static IPath canonicalizedPath(IPath externalPath) {
147                 
148                 if (externalPath == null)
149                         return null;
150
151                 if (JavaModelManager.VERBOSE) {
152                         System.out.println("JAVA MODEL - Canonicalizing " + externalPath.toString()); //$NON-NLS-1$
153                 }
154
155                 if (IS_CASE_SENSITIVE) {
156                         if (JavaModelManager.VERBOSE) {
157                                 System.out.println("JAVA MODEL - Canonical path is original path (file system is case sensitive)"); //$NON-NLS-1$
158                         }
159                         return externalPath;
160                 }
161
162                 // if not external path, return original path
163                 IWorkspace workspace = ResourcesPlugin.getWorkspace();
164                 if (workspace == null) return externalPath; // protection during shutdown (30487)
165                 if (workspace.getRoot().findMember(externalPath) != null) {
166                         if (JavaModelManager.VERBOSE) {
167                                 System.out.println("JAVA MODEL - Canonical path is original path (member of workspace)"); //$NON-NLS-1$
168                         }
169                         return externalPath;
170                 }
171
172                 IPath canonicalPath = null;
173                 try {
174                         canonicalPath =
175                                 new Path(new File(externalPath.toOSString()).getCanonicalPath());
176                 } catch (IOException e) {
177                         // default to original path
178                         if (JavaModelManager.VERBOSE) {
179                                 System.out.println("JAVA MODEL - Canonical path is original path (IOException)"); //$NON-NLS-1$
180                         }
181                         return externalPath;
182                 }
183                 
184                 IPath result;
185                 int canonicalLength = canonicalPath.segmentCount();
186                 if (canonicalLength == 0) {
187                         // the java.io.File canonicalization failed
188                         if (JavaModelManager.VERBOSE) {
189                                 System.out.println("JAVA MODEL - Canonical path is original path (canonical path is empty)"); //$NON-NLS-1$
190                         }
191                         return externalPath;
192                 } else if (externalPath.isAbsolute()) {
193                         result = canonicalPath;
194                 } else {
195                         // if path is relative, remove the first segments that were added by the java.io.File canonicalization
196                         // e.g. 'lib/classes.zip' was converted to 'd:/myfolder/lib/classes.zip'
197                         int externalLength = externalPath.segmentCount();
198                         if (canonicalLength >= externalLength) {
199                                 result = canonicalPath.removeFirstSegments(canonicalLength - externalLength);
200                         } else {
201                                 if (JavaModelManager.VERBOSE) {
202                                         System.out.println("JAVA MODEL - Canonical path is original path (canonical path is " + canonicalPath.toString() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
203                                 }
204                                 return externalPath;
205                         }
206                 }
207                 
208                 // keep device only if it was specified (this is because File.getCanonicalPath() converts '/lib/classed.zip' to 'd:/lib/classes/zip')
209                 if (externalPath.getDevice() == null) {
210                         result = result.setDevice(null);
211                 } 
212                 if (JavaModelManager.VERBOSE) {
213                         System.out.println("JAVA MODEL - Canonical path is " + result.toString()); //$NON-NLS-1$
214                 }
215                 return result;
216         }
217
218         /**
219          * Constructor needed for <code>IProject.getNature()</code> and <code>IProject.addNature()</code>.
220          *
221          * @see #setProject(IProject)
222          */
223         public JavaProject() {
224                 super(null, null);
225         }
226
227         public JavaProject(IProject project, JavaElement parent) {
228                 super(parent, project.getName());
229                 this.project = project;
230         }
231         public void addLoadPathEntry(IProject anotherPHPProject) {
232                 fScratched = true;
233
234                 LoadPathEntry newEntry = new LoadPathEntry(anotherPHPProject);
235                 getLoadPathEntries().add(newEntry);
236         }
237
238         public void configure() throws CoreException {
239                 //      get project description and then the associated build commands 
240                 IProjectDescription desc = project.getDescription();
241                 ICommand[] commands = desc.getBuildSpec();
242
243                 // determine if builder already associated
244                 boolean found = false;
245                 for (int i = 0; i < commands.length; ++i) {
246                         if (commands[i].getBuilderName().equals(PHPeclipsePlugin.BUILDER_PARSER_ID)) {
247                                 found = true;
248                                 break;
249                         }
250                 }
251
252                 // add builder if not already in project
253                 if (!found) {
254                         ICommand command = desc.newCommand();
255                         command.setBuilderName(PHPeclipsePlugin.BUILDER_PARSER_ID);
256                         ICommand[] newCommands = new ICommand[commands.length + 1];
257
258                         // Add it before other builders. 
259                         System.arraycopy(commands, 0, newCommands, 1, commands.length);
260                         newCommands[0] = command;
261                         desc.setBuildSpec(newCommands);
262                         project.setDescription(desc, null);
263                 }
264         }
265
266         protected void loadLoadPathEntries() {
267                         fLoadPathEntries = new ArrayList();
268
269                         IFile loadPathsFile = getLoadPathEntriesFile();
270
271                         XMLReader reader = null;
272                         try {
273                                 reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
274                                 reader.setContentHandler(getLoadPathEntriesContentHandler());
275                                 reader.parse(new InputSource(loadPathsFile.getContents()));
276                         } catch (Exception e) {
277                                 //the file is nonextant or unreadable
278                         }
279                 }
280                 
281         public List getLoadPathEntries() {
282                 if (fLoadPathEntries == null) {
283                         loadLoadPathEntries();
284                 }
285
286                 return fLoadPathEntries;
287         }
288
289         protected ContentHandler getLoadPathEntriesContentHandler() {
290                 return new ContentHandler() {
291                         public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
292                         }
293
294                         public void endDocument() throws SAXException {
295                         }
296
297                         public void endElement(String arg0, String arg1, String arg2) throws SAXException {
298                         }
299
300                         public void endPrefixMapping(String arg0) throws SAXException {
301                         }
302
303                         public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
304                         }
305
306                         public void processingInstruction(String arg0, String arg1) throws SAXException {
307                         }
308
309                         public void setDocumentLocator(Locator arg0) {
310                         }
311
312                         public void skippedEntity(String arg0) throws SAXException {
313                         }
314
315                         public void startDocument() throws SAXException {
316                         }
317
318                         public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
319                                 if ("pathentry".equals(qName))
320                                         if ("project".equals(atts.getValue("type"))) {
321                                                 IPath referencedProjectPath = new Path(atts.getValue("path"));
322                                                 IProject referencedProject = getProject(referencedProjectPath.lastSegment());
323                                                 fLoadPathEntries.add(new LoadPathEntry(referencedProject));
324                                         }
325                         }
326
327                         public void startPrefixMapping(String arg0, String arg1) throws SAXException {
328                         }
329                 };
330         }
331
332         protected IFile getLoadPathEntriesFile() {
333                 return project.getFile(".loadpath");
334         }
335
336         protected String getLoadPathXML() {
337                 StringBuffer buffer = new StringBuffer();
338                 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><loadpath>");
339
340                 Iterator pathEntriesIterator = fLoadPathEntries.iterator();
341
342                 while (pathEntriesIterator.hasNext()) {
343                         LoadPathEntry entry = (LoadPathEntry) pathEntriesIterator.next();
344                         buffer.append(entry.toXML());
345                 }
346
347                 buffer.append("</loadpath>");
348                 return buffer.toString();
349         }
350         /**
351          * Adds a builder to the build spec for the given project.
352          */
353         protected void addToBuildSpec(String builderID) throws CoreException {
354
355                 IProjectDescription description = getProject().getDescription();
356                 ICommand javaCommand = getJavaCommand(description);
357
358                 if (javaCommand == null) {
359
360                         // Add a Java command to the build spec
361                         ICommand command = description.newCommand();
362                         command.setBuilderName(builderID);
363                         setJavaCommand(description, command);
364                 }
365         }
366         /**
367          * @see Openable
368          */
369         protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
370         
371                 // check whether the java project can be opened
372                 if (!underlyingResource.isAccessible()) {
373                         throw newNotPresentException();
374                 }
375                 
376                 IWorkspace workspace = ResourcesPlugin.getWorkspace();
377                 IWorkspaceRoot wRoot = workspace.getRoot();
378                 // cannot refresh cp markers on opening (emulate cp check on startup) since can create deadlocks (see bug 37274)
379 //              IClasspathEntry[] resolvedClasspath = getResolvedClasspath(true/*ignoreUnresolvedEntry*/, false/*don't generateMarkerOnError*/, false/*don't returnResolutionInProgress*/);
380
381 //              // compute the pkg fragment roots
382 //              info.setChildren(computePackageFragmentRoots(resolvedClasspath, false));        
383 //              
384 //              // remember the timestamps of external libraries the first time they are looked up
385 //              for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
386 //                      IClasspathEntry entry = resolvedClasspath[i];
387 //                      if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
388 //                              IPath path = entry.getPath();
389 //                              Object target = JavaModel.getTarget(wRoot, path, true);
390 //                              if (target instanceof java.io.File) {
391 //                                      Map externalTimeStamps = JavaModelManager.getJavaModelManager().deltaState.externalTimeStamps;
392 //                                      if (externalTimeStamps.get(path) == null) {
393 //                                              long timestamp = DeltaProcessor.getTimeStamp((java.io.File)target);
394 //                                              externalTimeStamps.put(path, new Long(timestamp));                                                      
395 //                                      }
396 //                              }
397 //                      }
398 //              }                       
399
400                 return true;
401         }
402         protected void closing(Object info) {
403                 
404 //              // forget source attachment recommendations
405 //              Object[] children = ((JavaElementInfo)info).children;
406 //              for (int i = 0, length = children.length; i < length; i++) {
407 //                      Object child = children[i];
408 //                      if (child instanceof JarPackageFragmentRoot){
409 //                              ((JarPackageFragmentRoot)child).setSourceAttachmentProperty(null); 
410 //                      }
411 //              }
412                 
413                 super.closing(info);
414         }
415 //      protected void closing(Object info) throws JavaModelException {
416 //              
417 //              // forget source attachment recommendations
418 //              IPackageFragmentRoot[] roots = this.getPackageFragmentRoots();
419 ////            for (int i = 0; i < roots.length; i++) {
420 ////                    if (roots[i] instanceof JarPackageFragmentRoot){
421 ////                            ((JarPackageFragmentRoot) roots[i]).setSourceAttachmentProperty(null); 
422 ////                    }
423 ////            }
424 //              
425 //              super.closing(info);
426 //      }
427         
428
429
430         /**
431          * Internal computation of an expanded classpath. It will eliminate duplicates, and produce copies
432          * of exported classpath entries to avoid possible side-effects ever after.
433          */                     
434         private void computeExpandedClasspath(
435                 JavaProject initialProject, 
436                 boolean ignoreUnresolvedVariable,
437                 boolean generateMarkerOnError,
438                 HashSet visitedProjects, 
439                 ObjectVector accumulatedEntries) throws JavaModelException {
440                 
441                 if (visitedProjects.contains(this)){
442                         return; // break cycles if any
443                 }
444                 visitedProjects.add(this);
445
446                 if (generateMarkerOnError && !this.equals(initialProject)){
447                         generateMarkerOnError = false;
448                 }
449                 IClasspathEntry[] immediateClasspath = 
450                         getResolvedClasspath(ignoreUnresolvedVariable, generateMarkerOnError);
451                         
452                 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
453                 for (int i = 0, length = immediateClasspath.length; i < length; i++){
454                         IClasspathEntry entry = immediateClasspath[i];
455
456                         boolean isInitialProject = this.equals(initialProject);
457                         if (isInitialProject || entry.isExported()){
458                                 
459                                 accumulatedEntries.add(entry);
460                                 
461                                 // recurse in project to get all its indirect exports (only consider exported entries from there on)                            
462                                 if (entry.getEntryKind() == ClasspathEntry.CPE_PROJECT) {
463                                         IResource member = workspaceRoot.findMember(entry.getPath()); 
464                                         if (member != null && member.getType() == IResource.PROJECT){ // double check if bound to project (23977)
465                                                 IProject projRsc = (IProject) member;
466                                                 if (JavaProject.hasJavaNature(projRsc)) {                               
467                                                         JavaProject project = (JavaProject) JavaCore.create(projRsc);
468                                                         project.computeExpandedClasspath(
469                                                                 initialProject, 
470                                                                 ignoreUnresolvedVariable, 
471                                                                 generateMarkerOnError,
472                                                                 visitedProjects, 
473                                                                 accumulatedEntries);
474                                                 }
475                                         }
476                                 }
477                         }                       
478                 }
479         }
480         
481         /**
482          * Returns (local/all) the package fragment roots identified by the given project's classpath.
483          * Note: this follows project classpath references to find required project contributions,
484          * eliminating duplicates silently.
485          * Only works with resolved entries
486          */
487         public IPackageFragmentRoot[] computePackageFragmentRoots(IClasspathEntry[] resolvedClasspath, boolean retrieveExportedRoots) throws JavaModelException {
488
489                 ObjectVector accumulatedRoots = new ObjectVector();
490                 computePackageFragmentRoots(
491                         resolvedClasspath, 
492                         accumulatedRoots, 
493                         new HashSet(5), // rootIDs
494                         true, // inside original project
495                         true, // check existency
496                         retrieveExportedRoots);
497                 IPackageFragmentRoot[] rootArray = new IPackageFragmentRoot[accumulatedRoots.size()];
498                 accumulatedRoots.copyInto(rootArray);
499                 return rootArray;
500         }
501
502         /**
503          * Computes the package fragment roots identified by the given entry.
504          * Only works with resolved entry
505          */
506         public IPackageFragmentRoot[] computePackageFragmentRoots(IClasspathEntry resolvedEntry) {
507                 try {
508                         return 
509                                 computePackageFragmentRoots(
510                                         new IClasspathEntry[]{ resolvedEntry }, 
511                                         false // don't retrieve exported roots
512                                 );
513                 } catch (JavaModelException e) {
514                         return new IPackageFragmentRoot[] {};
515                 }
516         }
517         
518         /**
519          * Returns the package fragment roots identified by the given entry. In case it refers to
520          * a project, it will follow its classpath so as to find exported roots as well.
521          * Only works with resolved entry
522          */
523         public void computePackageFragmentRoots(
524                 IClasspathEntry resolvedEntry,
525                 ObjectVector accumulatedRoots, 
526                 HashSet rootIDs, 
527                 boolean insideOriginalProject,
528                 boolean checkExistency,
529                 boolean retrieveExportedRoots) throws JavaModelException {
530                         
531                 String rootID = ((ClasspathEntry)resolvedEntry).rootID();
532                 if (rootIDs.contains(rootID)) return;
533
534                 IPath projectPath = getProject().getFullPath();
535                 IPath entryPath = resolvedEntry.getPath();
536                 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
537                 
538                 switch(resolvedEntry.getEntryKind()){
539                         
540                         // source folder
541                         case IClasspathEntry.CPE_SOURCE :
542
543                                 if (projectPath.isPrefixOf(entryPath)){
544                                         if (checkExistency) {
545                                                 Object target = JavaModel.getTarget(workspaceRoot, entryPath, checkExistency);
546                                                 if (target == null) return;
547         
548                                                 if (target instanceof IFolder || target instanceof IProject){
549                                                         accumulatedRoots.add(
550                                                                 getPackageFragmentRoot((IResource)target));
551                                                         rootIDs.add(rootID);
552                                                 }
553                                         } else {
554                                                 IPackageFragmentRoot root = getFolderPackageFragmentRoot(entryPath);
555                                                 if (root != null) {
556                                                         accumulatedRoots.add(root);
557                                                         rootIDs.add(rootID);
558                                                 }
559                                         }
560                                 }
561                                 break;
562
563                         // internal/external JAR or folder
564                         case IClasspathEntry.CPE_LIBRARY :
565                         
566                                 if (!insideOriginalProject && !resolvedEntry.isExported()) return;
567                                 
568                                 if (checkExistency) {
569                                         Object target = JavaModel.getTarget(workspaceRoot, entryPath, checkExistency);
570                                         if (target == null) return;
571         
572                                         if (target instanceof IResource){
573                                                 // internal target
574                                                 IResource resource = (IResource) target;
575                                                 IPackageFragmentRoot root = getPackageFragmentRoot(resource);
576                                                 if (root != null) {
577                                                         accumulatedRoots.add(root);
578                                                         rootIDs.add(rootID);
579                                                 }
580                                         } else {
581                                                 // external target - only JARs allowed
582 //                                              if (((java.io.File)target).isFile() && (ProjectPrefUtil.isArchiveFileName(entryPath.lastSegment()))) {
583 //                                                      accumulatedRoots.add(
584 //                                                              new JarPackageFragmentRoot(entryPath, this));
585 //                                                      rootIDs.add(rootID);
586 //                                              }
587                                         }
588                                 } else {
589                                         IPackageFragmentRoot root = getPackageFragmentRoot(entryPath);
590                                         if (root != null) {
591                                                 accumulatedRoots.add(root);
592                                                 rootIDs.add(rootID);
593                                         }
594                                 }
595                                 break;
596
597                         // recurse into required project
598                         case IClasspathEntry.CPE_PROJECT :
599
600                                 if (!retrieveExportedRoots) return;
601                                 if (!insideOriginalProject && !resolvedEntry.isExported()) return;
602
603                                 IResource member = workspaceRoot.findMember(entryPath);
604                                 if (member != null && member.getType() == IResource.PROJECT){// double check if bound to project (23977)
605                                         IProject requiredProjectRsc = (IProject) member;
606                                         if (JavaProject.hasJavaNature(requiredProjectRsc)){ // special builder binary output
607                                                 rootIDs.add(rootID);
608                                                 JavaProject requiredProject = (JavaProject)JavaCore.create(requiredProjectRsc);
609                                                 requiredProject.computePackageFragmentRoots(
610                                                         requiredProject.getResolvedClasspath(true), 
611                                                         accumulatedRoots, 
612                                                         rootIDs, 
613                                                         false, 
614                                                         checkExistency, 
615                                                         retrieveExportedRoots);
616                                         }
617                                 break;
618                         }
619                 }
620         }
621
622         /**
623          * Returns (local/all) the package fragment roots identified by the given project's classpath.
624          * Note: this follows project classpath references to find required project contributions,
625          * eliminating duplicates silently.
626          * Only works with resolved entries
627          */
628         public void computePackageFragmentRoots(
629                 IClasspathEntry[] resolvedClasspath,
630                 ObjectVector accumulatedRoots, 
631                 HashSet rootIDs, 
632                 boolean insideOriginalProject,
633                 boolean checkExistency,
634                 boolean retrieveExportedRoots) throws JavaModelException {
635
636                 if (insideOriginalProject){
637                         rootIDs.add(rootID());
638                 }       
639                 for (int i = 0, length = resolvedClasspath.length; i < length; i++){
640                         computePackageFragmentRoots(
641                                 resolvedClasspath[i],
642                                 accumulatedRoots,
643                                 rootIDs,
644                                 insideOriginalProject,
645                                 checkExistency,
646                                 retrieveExportedRoots);
647                 }
648         }
649
650         /**
651          * Compute the file name to use for a given shared property
652          */
653         public String computeSharedPropertyFileName(QualifiedName qName) {
654
655                 return '.' + qName.getLocalName();
656         }
657         
658         /*
659          * Returns whether the given resource is accessible through the children or the non-Java resources of this project.
660          * Returns true if the resource is not in the project.
661          * Assumes that the resource is a folder or a file.
662          */
663         public boolean contains(IResource resource) {
664                         
665                 IClasspathEntry[] classpath;
666                 IPath output;
667                 try {
668                         classpath = getResolvedClasspath(true);
669                         output = getOutputLocation();
670                 } catch (JavaModelException e) {
671                         return false;
672                 }
673                 
674                 IPath fullPath = resource.getFullPath();
675                 IPath innerMostOutput = output.isPrefixOf(fullPath) ? output : null;
676                 IClasspathEntry innerMostEntry = null;
677                 for (int j = 0, cpLength = classpath.length; j < cpLength; j++) {
678                         IClasspathEntry entry = classpath[j];
679                 
680                         IPath entryPath = entry.getPath();
681                         if ((innerMostEntry == null || innerMostEntry.getPath().isPrefixOf(entryPath))
682                                         && entryPath.isPrefixOf(fullPath)) {
683                                 innerMostEntry = entry;
684                         }
685                         IPath entryOutput = classpath[j].getOutputLocation();
686                         if (entryOutput != null && entryOutput.isPrefixOf(fullPath)) {
687                                 innerMostOutput = entryOutput;
688                         }
689                 }
690                 if (innerMostEntry != null) {
691                         // special case prj==src and nested output location
692                         if (innerMostOutput != null && innerMostOutput.segmentCount() > 1 // output isn't project
693                                         && innerMostEntry.getPath().segmentCount() == 1) { // 1 segment must be project name
694                                 return false;
695                         }
696                         if  (resource instanceof IFolder) {
697                                  // folders are always included in src/lib entries
698                                  return true;
699                         }
700                         switch (innerMostEntry.getEntryKind()) {
701                                 case IClasspathEntry.CPE_SOURCE:
702                                         // .class files are not visible in source folders 
703                                         return true; //!net.sourceforge.phpdt.internal.compiler.util.ProjectPrefUtil.isClassFileName(fullPath.lastSegment());
704                                 case IClasspathEntry.CPE_LIBRARY:
705                                         // .java files are not visible in library folders
706                                         return !net.sourceforge.phpdt.internal.compiler.util.Util.isJavaFileName(fullPath.lastSegment());
707                         }
708                 }
709                 if (innerMostOutput != null) {
710                         return false;
711                 }
712                 return true;
713         }
714
715         /**
716          * Record a new marker denoting a classpath problem
717          */
718         IMarker createClasspathProblemMarker(IJavaModelStatus status) {
719                         
720                 IMarker marker = null;
721                 int severity;
722                 String[] arguments = new String[0];
723                 boolean isCycleProblem = false, isClasspathFileFormatProblem = false;
724                 switch (status.getCode()) {
725
726                         case  IJavaModelStatusConstants.CLASSPATH_CYCLE :
727                                 isCycleProblem = true;
728                                 if (JavaCore.ERROR.equals(getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true))) {
729                                         severity = IMarker.SEVERITY_ERROR;
730                                 } else {
731                                         severity = IMarker.SEVERITY_WARNING;
732                                 }
733                                 break;
734
735                         case  IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT :
736                                 isClasspathFileFormatProblem = true;
737                                 severity = IMarker.SEVERITY_ERROR;
738                                 break;
739
740                         default:
741                                 IPath path = status.getPath();
742                                 if (path != null) arguments = new String[] { path.toString() };
743                                 if (JavaCore.ERROR.equals(getOption(JavaCore.CORE_INCOMPLETE_CLASSPATH, true))) {
744                                         severity = IMarker.SEVERITY_ERROR;
745                                 } else {
746                                         severity = IMarker.SEVERITY_WARNING;
747                                 }
748                                 break;
749                 }
750                 
751                 try {
752                         marker = getProject().createMarker(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER);
753                         marker.setAttributes(
754                                 new String[] { 
755                                         IMarker.MESSAGE, 
756                                         IMarker.SEVERITY, 
757                                         IMarker.LOCATION, 
758                                         IJavaModelMarker.CYCLE_DETECTED,
759                                         IJavaModelMarker.CLASSPATH_FILE_FORMAT,
760                                         IJavaModelMarker.ID,
761                                         IJavaModelMarker.ARGUMENTS ,
762                                 },
763                                 new Object[] {
764                                         status.getMessage(),
765                                         new Integer(severity), 
766                                         Util.bind("classpath.buildPath"),//$NON-NLS-1$
767                                         isCycleProblem ? "true" : "false",//$NON-NLS-1$ //$NON-NLS-2$
768                                         isClasspathFileFormatProblem ? "true" : "false",//$NON-NLS-1$ //$NON-NLS-2$
769                                         new Integer(status.getCode()),
770                                         Util.getProblemArgumentsForMarker(arguments) ,
771                                 }
772                         );
773                 } catch (CoreException e) {
774                 }
775                 return marker;
776         }
777         
778         /**
779          * Returns a new element info for this element.
780          */
781         protected Object createElementInfo() {
782                 return new JavaProjectElementInfo();
783         }
784         
785
786         /*
787          * Returns a new search name environment for this project. This name environment first looks in the given working copies.
788          */
789 //      public ISearchableNameEnvironment newSearchableNameEnvironment(ICompilationUnit[] workingCopies) throws JavaModelException {
790 //              return new SearchableEnvironment(this, workingCopies);
791 //      }
792
793         /*
794          * Returns a new search name environment for this project. This name environment first looks in the working copies
795          * of the given owner.
796          */
797         public ISearchableNameEnvironment newSearchableNameEnvironment(WorkingCopyOwner owner) throws JavaModelException {
798                 return new SearchableEnvironment(this, owner);
799         }
800         /**
801          * Reads and decode an XML classpath string
802          */
803         protected IClasspathEntry[] decodeClasspath(String xmlClasspath, boolean createMarker, boolean logProblems) {
804
805                 ArrayList paths = new ArrayList();
806                 IClasspathEntry defaultOutput = null;
807                 try {
808                         if (xmlClasspath == null) return null;
809                         StringReader reader = new StringReader(xmlClasspath);
810                         Element cpElement;
811         
812                         try {
813                                 DocumentBuilder parser =
814                                         DocumentBuilderFactory.newInstance().newDocumentBuilder();
815                                 cpElement = parser.parse(new InputSource(reader)).getDocumentElement();
816                         } catch (SAXException e) {
817                                 throw new IOException(Util.bind("file.badFormat")); //$NON-NLS-1$
818                         } catch (ParserConfigurationException e) {
819                                 throw new IOException(Util.bind("file.badFormat")); //$NON-NLS-1$
820                         } finally {
821                                 reader.close();
822                         }
823         
824                         if (!cpElement.getNodeName().equalsIgnoreCase("classpath")) { //$NON-NLS-1$
825                                 throw new IOException(Util.bind("file.badFormat")); //$NON-NLS-1$
826                         }
827                         NodeList list = cpElement.getElementsByTagName("classpathentry"); //$NON-NLS-1$
828                         int length = list.getLength();
829         
830                         for (int i = 0; i < length; ++i) {
831                                 Node node = list.item(i);
832                                 if (node.getNodeType() == Node.ELEMENT_NODE) {
833                                         IClasspathEntry entry = ClasspathEntry.elementDecode((Element)node, this);
834                                         if (entry != null){
835                                                 if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) { 
836                                                         defaultOutput = entry; // separate output
837                                                 } else {
838                                                         paths.add(entry);
839                                 }
840                         }
841                                 }
842                         }
843                 } catch (IOException e) {
844                         // bad format
845                         if (createMarker && this.getProject().isAccessible()) {
846                                         this.createClasspathProblemMarker(new JavaModelStatus(
847                                                         IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
848                                                         Util.bind("classpath.xmlFormatError", this.getElementName(), e.getMessage()))); //$NON-NLS-1$
849                         }
850                         if (logProblems) {
851                                 Util.log(e, 
852                                         "Exception while retrieving "+ this.getPath() //$NON-NLS-1$
853                                         +"/.classpath, will mark classpath as invalid"); //$NON-NLS-1$
854                         }
855                         return INVALID_CLASSPATH;
856                 } catch (Assert.AssertionFailedException e) { 
857                         // failed creating CP entries from file
858                         if (createMarker && this.getProject().isAccessible()) {
859                                 this.createClasspathProblemMarker(new JavaModelStatus(
860                                                 IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
861                                                 Util.bind("classpath.illegalEntryInClasspathFile", this.getElementName(), e.getMessage()))); //$NON-NLS-1$
862                         }
863                         if (logProblems) {
864                                 Util.log(e, 
865                                         "Exception while retrieving "+ this.getPath() //$NON-NLS-1$
866                                         +"/.classpath, will mark classpath as invalid"); //$NON-NLS-1$
867                         }
868                         return INVALID_CLASSPATH;
869                 }
870                 int pathSize = paths.size();
871                 if (pathSize > 0 || defaultOutput != null) {
872                         IClasspathEntry[] entries = new IClasspathEntry[pathSize + (defaultOutput == null ? 0 : 1)];
873                         paths.toArray(entries);
874                         if (defaultOutput != null) entries[pathSize] = defaultOutput; // ensure output is last item
875                         return entries;
876                 } else {
877                         return null;
878                 }
879         }
880
881         /**
882         /**
883          * Removes the Java nature from the project.
884          */
885         public void deconfigure() throws CoreException {
886
887                 // deregister Java builder
888                 removeFromBuildSpec(PHPeclipsePlugin.BUILDER_PARSER_ID);
889         }
890
891         /**
892          * Returns a default class path.
893          * This is the root of the project
894          */
895         protected IClasspathEntry[] defaultClasspath() throws JavaModelException {
896
897                 return new IClasspathEntry[] {
898                          JavaCore.newSourceEntry(getProject().getFullPath())};
899         }
900
901         /**
902          * Returns a default output location.
903          * This is the project bin folder
904          */
905         protected IPath defaultOutputLocation() throws JavaModelException {
906                 return null; //getProject().getFullPath().append("bin"); //$NON-NLS-1$
907         }
908
909         /**
910          * Returns the XML String encoding of the class path.
911          */
912         protected String encodeClasspath(IClasspathEntry[] classpath, IPath outputLocation, boolean indent) throws JavaModelException {
913                 try {
914                         ByteArrayOutputStream s = new ByteArrayOutputStream();
915                         OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8"); //$NON-NLS-1$
916                         XMLWriter xmlWriter = new XMLWriter(writer);
917                         
918                         xmlWriter.startTag("classpath", indent); //$NON-NLS-1$
919                         for (int i = 0; i < classpath.length; ++i) {
920                                 ((ClasspathEntry)classpath[i]).elementEncode(xmlWriter, this.project.getFullPath(), indent, true);
921                         }
922         
923                         if (outputLocation != null) {
924                                 outputLocation = outputLocation.removeFirstSegments(1);
925                                 outputLocation = outputLocation.makeRelative();
926                                 HashMap parameters = new HashMap();
927                                 parameters.put("kind", ClasspathEntry.kindToString(ClasspathEntry.K_OUTPUT));//$NON-NLS-1$
928                                 parameters.put("path", String.valueOf(outputLocation));//$NON-NLS-1$
929                                 xmlWriter.printTag("classpathentry", parameters, indent, true, true);//$NON-NLS-1$
930                         }
931         
932                         xmlWriter.endTag("classpath", indent);//$NON-NLS-1$
933                         writer.flush();
934                         writer.close();
935                         return s.toString("UTF8");//$NON-NLS-1$
936                 } catch (IOException e) {
937                         throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
938                 }
939         }
940         /**
941          * Returns the XML String encoding of the class path.
942          */
943 //      protected String encodeClasspath(IClasspathEntry[] classpath, IPath outputLocation, boolean useLineSeparator) throws JavaModelException {
944 //
945 //              Document document = new DocumentImpl();
946 //              Element cpElement = document.createElement("classpath"); //$NON-NLS-1$
947 //              document.appendChild(cpElement);
948 //
949 //              for (int i = 0; i < classpath.length; ++i) {
950 //                      cpElement.appendChild(((ClasspathEntry)classpath[i]).elementEncode(document, getProject().getFullPath()));
951 //              }
952 //
953 //              if (outputLocation != null) {
954 //                      outputLocation = outputLocation.removeFirstSegments(1);
955 //                      outputLocation = outputLocation.makeRelative();
956 //                      Element oElement = document.createElement("classpathentry"); //$NON-NLS-1$
957 //                      oElement.setAttribute("kind", ClasspathEntry.kindToString(ClasspathEntry.K_OUTPUT));    //$NON-NLS-1$
958 //                      oElement.setAttribute("path", outputLocation.toString()); //$NON-NLS-1$
959 //                      cpElement.appendChild(oElement);
960 //              }
961 //
962 //              // produce a String output
963 //              try {
964 //                      ByteArrayOutputStream s = new ByteArrayOutputStream();
965 //                      OutputFormat format = new OutputFormat();
966 //                      if (useLineSeparator) {
967 //                              format.setIndenting(true);
968 //                              format.setLineSeparator(System.getProperty("line.separator"));  //$NON-NLS-1$
969 //                      } else {
970 //                              format.setPreserveSpace(true);
971 //                      }                       
972 //                      Serializer serializer =
973 //                              SerializerFactory.getSerializerFactory(Method.XML).makeSerializer(
974 //                                      new OutputStreamWriter(s, "UTF8"), //$NON-NLS-1$
975 //                                      format);
976 //                      serializer.asDOMSerializer().serialize(document);
977 //                      return s.toString("UTF8"); //$NON-NLS-1$
978 //              } catch (IOException e) {
979 //                      throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
980 //              }
981 //      }
982         
983         /**
984          * Returns true if this handle represents the same Java project
985          * as the given handle. Two handles represent the same
986          * project if they are identical or if they represent a project with 
987          * the same underlying resource and occurrence counts.
988          *
989          * @see JavaElement#equals
990          */
991         public boolean equals(Object o) {
992
993                 if (this == o)
994                         return true;
995
996                 if (!(o instanceof JavaProject))
997                         return false;
998
999                 JavaProject other = (JavaProject) o;
1000                 return getProject().equals(other.getProject())
1001                         && occurrenceCount == other.occurrenceCount;
1002         }
1003
1004         public boolean exists() {
1005                 if (!hasJavaNature(project)) return false;
1006                 return super.exists();
1007         }       
1008
1009         /**
1010          * @see IJavaProject
1011          */
1012         public IJavaElement findElement(IPath path) throws JavaModelException {
1013
1014                 if (path == null || path.isAbsolute()) {
1015                         throw new JavaModelException(
1016                                 new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, path));
1017                 }
1018 //              try {
1019
1020                         String extension = path.getFileExtension();
1021                         if (extension == null) {
1022                                 String packageName = path.toString().replace(IPath.SEPARATOR, '.');
1023
1024 //                              IPackageFragment[] pkgFragments =
1025 //                                      getNameLookup().findPackageFragments(packageName, false);
1026 //                              if (pkgFragments == null) {
1027                                         return null;
1028
1029 //                              } else {
1030 //                                      // try to return one that is a child of this project
1031 //                                      for (int i = 0, length = pkgFragments.length; i < length; i++) {
1032 //
1033 //                                              IPackageFragment pkgFragment = pkgFragments[i];
1034 //                                              if (this.equals(pkgFragment.getParent().getParent())) {
1035 //                                                      return pkgFragment;
1036 //                                              }
1037 //                                      }
1038 //                                      // default to the first one
1039 //                                      return pkgFragments[0];
1040 //                              }
1041                         } else if (
1042                                 extension.equalsIgnoreCase("java") //$NON-NLS-1$
1043                                         || extension.equalsIgnoreCase("class")) {  //$NON-NLS-1$
1044                                 IPath packagePath = path.removeLastSegments(1);
1045                                 String packageName = packagePath.toString().replace(IPath.SEPARATOR, '.');
1046                                 String typeName = path.lastSegment();
1047                                 typeName = typeName.substring(0, typeName.length() - extension.length() - 1);
1048                                 String qualifiedName = null;
1049                                 if (packageName.length() > 0) {
1050                                         qualifiedName = packageName + "." + typeName; //$NON-NLS-1$
1051                                 } else {
1052                                         qualifiedName = typeName;
1053                                 }
1054 //                              IType type =
1055 //                                      getNameLookup().findType(
1056 //                                              qualifiedName,
1057 //                                              false,
1058 //                                              NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
1059 //                              if (type != null) {
1060 //                                      return type.getParent();
1061 //                              } else {
1062                                         return null;
1063 //                              }
1064                         } else {
1065                                 // unsupported extension
1066                                 return null;
1067                         }
1068 //              } catch (JavaModelException e) {
1069 //                      if (e.getStatus().getCode()
1070 //                              == IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST) {
1071 //                              return null;
1072 //                      } else {
1073 //                              throw e;
1074 //                      }
1075 //              }
1076         }
1077
1078         /**
1079          * @see IJavaProject
1080          */
1081 //      public IPackageFragment findPackageFragment(IPath path)
1082 //              throws JavaModelException {
1083 //
1084 //              return findPackageFragment0(JavaProject.canonicalizedPath(path));
1085 //      }
1086 //
1087 //      /**
1088 //       * non path canonicalizing version
1089 //       */
1090 //      public IPackageFragment findPackageFragment0(IPath path) 
1091 //              throws JavaModelException {
1092 //
1093 //              return getNameLookup().findPackageFragment(path);
1094 //      }
1095
1096         /**
1097          * @see IJavaProject
1098          */
1099         public IPackageFragmentRoot findPackageFragmentRoot(IPath path)
1100                 throws JavaModelException {
1101
1102                 return findPackageFragmentRoot0(JavaProject.canonicalizedPath(path));
1103         }
1104
1105         /**
1106          * no path canonicalization 
1107          */
1108         public IPackageFragmentRoot findPackageFragmentRoot0(IPath path)
1109                 throws JavaModelException {
1110
1111                 IPackageFragmentRoot[] allRoots = this.getAllPackageFragmentRoots();
1112                 if (!path.isAbsolute()) {
1113                         throw new IllegalArgumentException(Util.bind("path.mustBeAbsolute")); //$NON-NLS-1$
1114                 }
1115                 for (int i= 0; i < allRoots.length; i++) {
1116                         IPackageFragmentRoot classpathRoot= allRoots[i];
1117                         if (classpathRoot.getPath().equals(path)) {
1118                                 return classpathRoot;
1119                         }
1120                 }
1121                 return null;
1122         }
1123         /**
1124          * @see IJavaProject
1125          */
1126         public IPackageFragmentRoot[] findPackageFragmentRoots(IClasspathEntry entry) {
1127                 try {
1128                         IClasspathEntry[] classpath = this.getRawClasspath();
1129                         for (int i = 0, length = classpath.length; i < length; i++) {
1130                                 if (classpath[i].equals(entry)) { // entry may need to be resolved
1131                                         return 
1132                                                 computePackageFragmentRoots(
1133                                                         getResolvedClasspath(new IClasspathEntry[] {entry}, null, true, false, null/*no reverse map*/), 
1134                                                         false); // don't retrieve exported roots
1135                                 }
1136                         }
1137                 } catch (JavaModelException e) {
1138                 }
1139                 return new IPackageFragmentRoot[] {};
1140         }
1141         
1142         /**
1143          * @see IJavaProject#findType(String)
1144          */
1145 //      public IType findType(String fullyQualifiedName) throws JavaModelException {
1146 //              IType type = 
1147 //                      this.getNameLookup().findType(
1148 //                              fullyQualifiedName, 
1149 //                              false,
1150 //                              NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
1151 //              if (type == null) {
1152 //                      // try to find enclosing type
1153 //                      int lastDot = fullyQualifiedName.lastIndexOf('.');
1154 //                      if (lastDot == -1) return null;
1155 //                      type = this.findType(fullyQualifiedName.substring(0, lastDot));
1156 //                      if (type != null) {
1157 //                              type = type.getType(fullyQualifiedName.substring(lastDot+1));
1158 //                              if (!type.exists()) {
1159 //                                      return null;
1160 //                              }
1161 //                      }
1162 //              }
1163 //              return type;
1164 //      }
1165         
1166         /**
1167          * @see IJavaProject#findType(String, String)
1168          */
1169 //      public IType findType(String packageName, String typeQualifiedName) throws JavaModelException {
1170 //              return 
1171 //                      this.getNameLookup().findType(
1172 //                              typeQualifiedName, 
1173 //                              packageName,
1174 //                              false,
1175 //                              NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
1176 //      }       
1177 //      
1178         /**
1179          * Remove all markers denoting classpath problems
1180          */
1181         protected void flushClasspathProblemMarkers(boolean flushCycleMarkers, boolean flushClasspathFormatMarkers) {
1182                 try {
1183                         IProject project = getProject();
1184                         if (project.exists()) {
1185                                 IMarker[] markers = project.findMarkers(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER, false, IResource.DEPTH_ZERO);
1186                                 for (int i = 0, length = markers.length; i < length; i++) {
1187                                         IMarker marker = markers[i];
1188                                         if (flushCycleMarkers && flushClasspathFormatMarkers) {
1189                                                 marker.delete();
1190                                         } else {
1191                                                 String cycleAttr = (String)marker.getAttribute(IJavaModelMarker.CYCLE_DETECTED);
1192                                                 String classpathFileFormatAttr =  (String)marker.getAttribute(IJavaModelMarker.CLASSPATH_FILE_FORMAT);
1193                                                 if ((flushCycleMarkers == (cycleAttr != null && cycleAttr.equals("true"))) //$NON-NLS-1$
1194                                                         && (flushClasspathFormatMarkers == (classpathFileFormatAttr != null && classpathFileFormatAttr.equals("true")))){ //$NON-NLS-1$
1195                                                         marker.delete();
1196                                                 }
1197                                         }
1198                                 }
1199                         }
1200                 } catch (CoreException e) {
1201                 }
1202         }
1203
1204 //      /**
1205 //       * @see Openable
1206 //       */
1207 //      protected boolean generateInfos(
1208 //              OpenableElementInfo info,
1209 //              IProgressMonitor pm,
1210 //              Map newElements,
1211 //              IResource underlyingResource) throws JavaModelException {
1212 //
1213 //              boolean validInfo = false;
1214 //              try {
1215 //                      if (getProject().isOpen()) {
1216 //                              // put the info now, because computing the roots requires it
1217 //                              JavaModelManager.getJavaModelManager().putInfo(this, info);
1218 //
1219 //                              // compute the pkg fragment roots
1220 //                              updatePackageFragmentRoots();                           
1221 //      
1222 //                              // remember the timestamps of external libraries the first time they are looked up
1223 //                              IClasspathEntry[] resolvedClasspath = getResolvedClasspath(true/*ignore unresolved variable*/);
1224 //                              for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
1225 //                                      IClasspathEntry entry = resolvedClasspath[i];
1226 //                                      if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
1227 //                                              IPath path = entry.getPath();
1228 //                                              Object target = JavaModel.getTarget(ResourcesPlugin.getWorkspace().getRoot(), path, true);
1229 //                                              if (target instanceof java.io.File) {
1230 //                                                      Map externalTimeStamps = JavaModelManager.getJavaModelManager().deltaProcessor.externalTimeStamps;
1231 //                                                      if (externalTimeStamps.get(path) == null) {
1232 //                                                              long timestamp = DeltaProcessor.getTimeStamp((java.io.File)target);
1233 //                                                              externalTimeStamps.put(path, new Long(timestamp));                                                      
1234 //                                                      }
1235 //                                              }
1236 //                                      }
1237 //                              }                       
1238 //
1239 //                              // only valid if reaches here                           
1240 //                              validInfo = true;
1241 //                      }
1242 //              } finally {
1243 //                      if (!validInfo)
1244 //                              JavaModelManager.getJavaModelManager().removeInfo(this);
1245 //              }
1246 //              return validInfo;
1247 //      }
1248
1249         /**
1250          * @see IJavaProject
1251          */
1252         public IPackageFragmentRoot[] getAllPackageFragmentRoots()
1253                 throws JavaModelException {
1254
1255                 return computePackageFragmentRoots(getResolvedClasspath(true), true);
1256         }
1257
1258         /**
1259          * Returns the classpath entry that refers to the given path
1260          * or <code>null</code> if there is no reference to the path.
1261          */
1262         public IClasspathEntry getClasspathEntryFor(IPath path)
1263                 throws JavaModelException {
1264
1265                 IClasspathEntry[] entries = getExpandedClasspath(true);
1266                 for (int i = 0; i < entries.length; i++) {
1267                         if (entries[i].getPath().equals(path)) {
1268                                 return entries[i];
1269                         }
1270                 }
1271                 return null;
1272         }
1273
1274         /*
1275          * Returns the cycle marker associated with this project or null if none.
1276          */
1277         public IMarker getCycleMarker(){
1278                 try {
1279                         IProject project = getProject();
1280                         if (project.exists()) {
1281                                 IMarker[] markers = project.findMarkers(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER, false, IResource.DEPTH_ZERO);
1282                                 for (int i = 0, length = markers.length; i < length; i++) {
1283                                         IMarker marker = markers[i];
1284                                         String cycleAttr = (String)marker.getAttribute(IJavaModelMarker.CYCLE_DETECTED);
1285                                         if (cycleAttr != null && cycleAttr.equals("true")){ //$NON-NLS-1$
1286                                                 return marker;
1287                                         }
1288                                 }
1289                         }
1290                 } catch (CoreException e) {
1291                 }
1292                 return null;
1293         }
1294         /**
1295          * @see IJavaElement
1296          */
1297         public int getElementType() {
1298                 return JAVA_PROJECT;
1299         }
1300         /**
1301          * This is a helper method returning the expanded classpath for the project, as a list of classpath entries, 
1302          * where all classpath variable entries have been resolved and substituted with their final target entries.
1303          * All project exports have been appended to project entries.
1304          */
1305         public IClasspathEntry[] getExpandedClasspath(boolean ignoreUnresolvedVariable) throws JavaModelException {
1306                         
1307                         return getExpandedClasspath(ignoreUnresolvedVariable, false);
1308         }
1309                 
1310         /**
1311          * Internal variant which can create marker on project for invalid entries,
1312          * it will also perform classpath expansion in presence of project prerequisites
1313          * exporting their entries.
1314          */
1315         public IClasspathEntry[] getExpandedClasspath(
1316                 boolean ignoreUnresolvedVariable,
1317                 boolean generateMarkerOnError) throws JavaModelException {
1318         
1319                 ObjectVector accumulatedEntries = new ObjectVector();           
1320                 computeExpandedClasspath(this, ignoreUnresolvedVariable, generateMarkerOnError, new HashSet(5), accumulatedEntries);
1321                 
1322                 IClasspathEntry[] expandedPath = new IClasspathEntry[accumulatedEntries.size()];
1323                 accumulatedEntries.copyInto(expandedPath);
1324
1325                 return expandedPath;
1326         }
1327
1328         /**
1329          * Returns the <code>char</code> that marks the start of this handles
1330          * contribution to a memento.
1331          */
1332         protected char getHandleMementoDelimiter() {
1333
1334                 return JEM_JAVAPROJECT;
1335         }
1336
1337         /**
1338          * Find the specific Java command amongst the build spec of a given description
1339          */
1340         private ICommand getJavaCommand(IProjectDescription description)
1341                 throws CoreException {
1342
1343                 ICommand[] commands = description.getBuildSpec();
1344                 for (int i = 0; i < commands.length; ++i) {
1345                         if (commands[i].getBuilderName().equals(PHPeclipsePlugin.BUILDER_PARSER_ID)) {
1346                                 return commands[i];
1347                         }
1348                 }
1349                 return null;
1350         }
1351
1352         /**
1353          * Convenience method that returns the specific type of info for a Java project.
1354          */
1355         protected JavaProjectElementInfo getJavaProjectElementInfo()
1356                 throws JavaModelException {
1357
1358                 return (JavaProjectElementInfo) getElementInfo();
1359         }
1360
1361         /**
1362          * @see IJavaProject
1363          */
1364         public NameLookup getNameLookup() throws JavaModelException {
1365
1366                 JavaProjectElementInfo info = getJavaProjectElementInfo();
1367                 // lock on the project info to avoid race condition
1368                 synchronized(info){
1369                         NameLookup nameLookup;
1370                         if ((nameLookup = info.getNameLookup()) == null){
1371                                 info.setNameLookup(nameLookup = new NameLookup(this));
1372                         }
1373                         return nameLookup;
1374                 }
1375         }
1376         /*
1377          * Returns a new name lookup. This name lookup first looks in the given working copies.
1378          */
1379         public NameLookup newNameLookup(ICompilationUnit[] workingCopies) throws JavaModelException {
1380
1381                 JavaProjectElementInfo info = getJavaProjectElementInfo();
1382                 // lock on the project info to avoid race condition while computing the pkg fragment roots and package fragment caches
1383 //              synchronized(info){
1384 //                      return new NameLookup(info.getAllPackageFragmentRoots(this), info.getAllPackageFragments(this), workingCopies);
1385 //              }
1386                 return null;
1387         }
1388
1389         /*
1390          * Returns a new name lookup. This name lookup first looks in the working copies of the given owner.
1391          */
1392         public NameLookup newNameLookup(WorkingCopyOwner owner) throws JavaModelException {
1393                 
1394                 JavaModelManager manager = JavaModelManager.getJavaModelManager();
1395                 ICompilationUnit[] workingCopies = owner == null ? null : manager.getWorkingCopies(owner, true/*add primary WCs*/);
1396                 return newNameLookup(workingCopies);
1397         }
1398 //
1399 //      /**
1400 //       * Returns an array of non-java resources contained in the receiver.
1401 //       */
1402 //      public Object[] getNonJavaResources() throws JavaModelException {
1403 //
1404 //              return ((JavaProjectElementInfo) getElementInfo()).getNonJavaResources(this);
1405 //      }
1406
1407         /**
1408          * @see org.eclipse.jdt.core.IJavaProject#getOption(String, boolean)
1409          */     
1410         public String getOption(String optionName, boolean inheritJavaCoreOptions) {
1411                 
1412                 if (JavaModelManager.OptionNames.contains(optionName)){
1413                         
1414                         Preferences preferences = getPreferences();
1415                         if (preferences == null || preferences.isDefault(optionName)) {
1416                                 return inheritJavaCoreOptions ? JavaCore.getOption(optionName) : null;
1417                         }
1418                         return preferences.getString(optionName).trim();
1419                 }
1420                 return null;
1421         }
1422         
1423         /**
1424          * @see org.eclipse.jdt.core.IJavaProject#getOptions(boolean)
1425          */
1426         public Map getOptions(boolean inheritJavaCoreOptions) {
1427                 
1428                 // initialize to the defaults from JavaCore options pool
1429                 Map options = inheritJavaCoreOptions ? JavaCore.getOptions() : new Hashtable(5);
1430
1431                 Preferences preferences = getPreferences();
1432                 if (preferences == null) return options; // cannot do better (non-Java project)
1433                 HashSet optionNames = JavaModelManager.OptionNames;
1434                 
1435                 // get preferences set to their default
1436                 if (inheritJavaCoreOptions){
1437                         String[] defaultPropertyNames = preferences.defaultPropertyNames();
1438                         for (int i = 0; i < defaultPropertyNames.length; i++){
1439                                 String propertyName = defaultPropertyNames[i];
1440                                 if (optionNames.contains(propertyName)){
1441                                         options.put(propertyName, preferences.getDefaultString(propertyName).trim());
1442                                 }
1443                         }               
1444                 }
1445                 // get custom preferences not set to their default
1446                 String[] propertyNames = preferences.propertyNames();
1447                 for (int i = 0; i < propertyNames.length; i++){
1448                         String propertyName = propertyNames[i];
1449                         if (optionNames.contains(propertyName)){
1450                                 options.put(propertyName, preferences.getString(propertyName).trim());
1451                         }
1452                 }               
1453                 return options;
1454         }
1455         
1456         /**
1457          * @see IJavaProject
1458          */
1459         public IPath getOutputLocation() throws JavaModelException {
1460
1461                 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project);
1462                 IPath outputLocation = perProjectInfo.outputLocation;
1463                 if (outputLocation != null) return outputLocation;
1464
1465                 // force to read classpath - will position output location as well
1466                 this.getRawClasspath();
1467                 outputLocation = perProjectInfo.outputLocation;
1468                 if (outputLocation == null) {
1469                         return defaultOutputLocation();
1470                 }
1471                 return outputLocation;
1472         }
1473
1474         /**
1475          * @return A handle to the package fragment root identified by the given path.
1476          * This method is handle-only and the element may or may not exist. Returns
1477          * <code>null</code> if unable to generate a handle from the path (for example,
1478          * an absolute path that has less than 1 segment. The path may be relative or
1479          * absolute.
1480          */
1481         public IPackageFragmentRoot getPackageFragmentRoot(IPath path) {
1482                 if (!path.isAbsolute()) {
1483                         path = getPath().append(path);
1484                 }
1485                 int segmentCount = path.segmentCount();
1486                 switch (segmentCount) {
1487                         case 0:
1488                                 return null;
1489                         case 1:
1490                                 // default root
1491                                 return getPackageFragmentRoot(getProject());
1492                         default:
1493                                 // a path ending with .jar/.zip is still ambiguous and could still resolve to a source/lib folder 
1494                                 // thus will try to guess based on existing resource
1495 //                              if (ProjectPrefUtil.isArchiveFileName(path.lastSegment())) {
1496 //                                      IResource resource = getProject().getWorkspace().getRoot().findMember(path); 
1497 //                                      if (resource != null && resource.getType() == IResource.FOLDER){
1498 //                                              return getPackageFragmentRoot(resource);
1499 //                                      }
1500 //                                      return getPackageFragmentRoot0(path);
1501 //                              } else {
1502                                         return getPackageFragmentRoot(getProject().getWorkspace().getRoot().getFolder(path));
1503 //                              }
1504                 }
1505         }
1506
1507         /**
1508          * The path is known to match a source/library folder entry.
1509          */
1510         public IPackageFragmentRoot getFolderPackageFragmentRoot(IPath path) {
1511                 if (path.segmentCount() == 1) { // default project root
1512                         return getPackageFragmentRoot(getProject());
1513                 }
1514                 return getPackageFragmentRoot(getProject().getWorkspace().getRoot().getFolder(path));
1515         }
1516         
1517         /**
1518          * @see IJavaProject
1519          */
1520         public IPackageFragmentRoot getPackageFragmentRoot(IResource resource) {
1521
1522                 switch (resource.getType()) {
1523                         case IResource.FILE:
1524 //                              if (ProjectPrefUtil.isArchiveFileName(resource.getName())) {
1525 //                                      return new JarPackageFragmentRoot(resource, this);
1526 //                              } else {
1527                                         return null;
1528 //                              }
1529                         case IResource.FOLDER:
1530                                 return new PackageFragmentRoot(resource, this, resource.getName());
1531                         case IResource.PROJECT:
1532                                 return new PackageFragmentRoot(resource, this, ""); //$NON-NLS-1$
1533                         default:
1534                                 return null;
1535                 }
1536         }
1537
1538         /**
1539          * @see IJavaProject
1540          */
1541 //      public IPackageFragmentRoot getPackageFragmentRoot(String jarPath) {
1542 //
1543 //              return getPackageFragmentRoot0(JavaProject.canonicalizedPath(new Path(jarPath)));
1544 //      }
1545 //      
1546 //      /**
1547 //       * no path canonicalization
1548 //       */
1549 //      public IPackageFragmentRoot getPackageFragmentRoot0(IPath jarPath) {
1550 //
1551 //              return new JarPackageFragmentRoot(jarPath, this);
1552 //      }
1553
1554         /**
1555          * @see IJavaProject
1556          */
1557         public IPackageFragmentRoot[] getPackageFragmentRoots()
1558                 throws JavaModelException {
1559
1560                 Object[] children;
1561                 int length;
1562                 IPackageFragmentRoot[] roots;
1563
1564                 System.arraycopy(
1565                         children = getChildren(), 
1566                         0, 
1567                         roots = new IPackageFragmentRoot[length = children.length], 
1568                         0, 
1569                         length);
1570                         
1571                 return roots;
1572         }
1573
1574         /**
1575          * @see IJavaProject
1576          * @deprecated
1577          */
1578         public IPackageFragmentRoot[] getPackageFragmentRoots(IClasspathEntry entry) {
1579                 return findPackageFragmentRoots(entry);
1580         }
1581
1582         /**
1583          * Returns the package fragment root prefixed by the given path, or
1584          * an empty collection if there are no such elements in the model.
1585          */
1586         protected IPackageFragmentRoot[] getPackageFragmentRoots(IPath path)
1587
1588                 throws JavaModelException {
1589                 IPackageFragmentRoot[] roots = getAllPackageFragmentRoots();
1590                 ArrayList matches = new ArrayList();
1591
1592                 for (int i = 0; i < roots.length; ++i) {
1593                         if (path.isPrefixOf(roots[i].getPath())) {
1594                                 matches.add(roots[i]);
1595                         }
1596                 }
1597                 IPackageFragmentRoot[] copy = new IPackageFragmentRoot[matches.size()];
1598                 matches.toArray(copy);
1599                 return copy;
1600         }
1601
1602         /**
1603          * @see IJavaProject
1604          */
1605         public IPackageFragment[] getPackageFragments() throws JavaModelException {
1606
1607                 IPackageFragmentRoot[] roots = getPackageFragmentRoots();
1608                 return getPackageFragmentsInRoots(roots);
1609         }
1610
1611         /**
1612          * Returns all the package fragments found in the specified
1613          * package fragment roots.
1614          */
1615         public IPackageFragment[] getPackageFragmentsInRoots(IPackageFragmentRoot[] roots) {
1616
1617                 ArrayList frags = new ArrayList();
1618                 for (int i = 0; i < roots.length; i++) {
1619                         IPackageFragmentRoot root = roots[i];
1620                         try {
1621                                 IJavaElement[] rootFragments = root.getChildren();
1622                                 for (int j = 0; j < rootFragments.length; j++) {
1623                                         frags.add(rootFragments[j]);
1624                                 }
1625                         } catch (JavaModelException e) {
1626                                 // do nothing
1627                         }
1628                 }
1629                 IPackageFragment[] fragments = new IPackageFragment[frags.size()];
1630                 frags.toArray(fragments);
1631                 return fragments;
1632         }
1633         
1634         /*
1635          * @see IJavaElement
1636          */
1637         public IPath getPath() {
1638                 return this.getProject().getFullPath();
1639         }
1640         
1641         /**
1642          * @see IJavaProject
1643          */
1644         public IProject getProject() {
1645
1646                 return project;
1647         }
1648         /**
1649          * Sets the underlying kernel project of this Java project,
1650          * and fills in its parent and name.
1651          * Called by IProject.getNature().
1652          *
1653          * @see IProjectNature#setProject(IProject)
1654          */
1655         public void setProject(IProject project) {
1656
1657                 this.project = project;
1658                 this.parent = JavaModelManager.getJavaModelManager().getJavaModel();
1659                 this.name = project.getName();
1660         }
1661         protected IProject getProject(String name) {
1662                 return PHPeclipsePlugin.getWorkspace().getRoot().getProject(name);
1663         }
1664         
1665         public List getReferencedProjects() {
1666                         List referencedProjects = new ArrayList();
1667
1668                         Iterator iterator = getLoadPathEntries().iterator();
1669                         while (iterator.hasNext()) {
1670                                 LoadPathEntry pathEntry = (LoadPathEntry) iterator.next();
1671                                 if (pathEntry.getType() == LoadPathEntry.TYPE_PROJECT)
1672                                         referencedProjects.add(pathEntry.getProject());
1673                         }
1674
1675                         return referencedProjects;
1676                 }
1677                 
1678         /**
1679          * Returns the project custom preference pool.
1680          * Project preferences may include custom encoding.
1681          */     
1682         public Preferences getPreferences(){
1683                 IProject project = getProject();
1684                 if (!JavaProject.hasJavaNature(project)) return null;
1685                 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfo(project, true);
1686                 Preferences preferences =  perProjectInfo.preferences;
1687                 if (preferences != null) return preferences;
1688                 preferences = loadPreferences();
1689                 if (preferences == null) preferences = new Preferences();
1690                 perProjectInfo.preferences = preferences;
1691                 return preferences;
1692         }
1693
1694         /**
1695          * @see IJavaProject
1696          */
1697         public IClasspathEntry[] getRawClasspath() throws JavaModelException {
1698
1699                 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project);
1700                 IClasspathEntry[] classpath = perProjectInfo.classpath;
1701                 if (classpath != null) return classpath;
1702                 classpath = this.readClasspathFile(false/*don't create markers*/, true/*log problems*/);
1703                 
1704                 // extract out the output location
1705                 IPath outputLocation = null;
1706                 if (classpath != null && classpath.length > 0) {
1707                         IClasspathEntry entry = classpath[classpath.length - 1];
1708                         if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) {
1709                                 outputLocation = entry.getPath();
1710                                 IClasspathEntry[] copy = new IClasspathEntry[classpath.length - 1];
1711                                 System.arraycopy(classpath, 0, copy, 0, copy.length);
1712                                 classpath = copy;
1713                         }
1714                 }
1715                 if (classpath == null) {
1716                         return defaultClasspath();
1717                 }
1718                 /* Disable validate: classpath can contain CP variables and container that need to be resolved 
1719                 if (classpath != INVALID_CLASSPATH
1720                                 && !JavaConventions.validateClasspath(this, classpath, outputLocation).isOK()) {
1721                         classpath = INVALID_CLASSPATH;
1722                 }
1723                 */
1724                 perProjectInfo.classpath = classpath;
1725                 perProjectInfo.outputLocation = outputLocation;
1726                 return classpath;
1727         }
1728
1729         /**
1730          * @see IJavaProject#getRequiredProjectNames
1731          */
1732         public String[] getRequiredProjectNames() throws JavaModelException {
1733
1734                 return this.projectPrerequisites(getResolvedClasspath(true));
1735         }
1736
1737         /**
1738          * @see IJavaProject
1739          */
1740         public IClasspathEntry[] getResolvedClasspath(boolean ignoreUnresolvedEntry)
1741                 throws JavaModelException {
1742
1743                 return 
1744                         this.getResolvedClasspath(
1745                                 ignoreUnresolvedEntry, 
1746                                 false); // generateMarkerOnError
1747         }
1748
1749         /**
1750          * Internal variant which can create marker on project for invalid entries
1751          * and caches the resolved classpath on perProjectInfo
1752          */
1753         public IClasspathEntry[] getResolvedClasspath(
1754                 boolean ignoreUnresolvedEntry,
1755                 boolean generateMarkerOnError)
1756                 throws JavaModelException {
1757
1758                 JavaModelManager manager = JavaModelManager.getJavaModelManager();
1759                 JavaModelManager.PerProjectInfo perProjectInfo = manager.getPerProjectInfoCheckExistence(project);
1760                 
1761                 // reuse cache if not needing to refresh markers or checking bound variables
1762                 if (ignoreUnresolvedEntry && !generateMarkerOnError && perProjectInfo != null){
1763                         // resolved path is cached on its info
1764                         IClasspathEntry[] infoPath = perProjectInfo.lastResolvedClasspath;
1765                         if (infoPath != null) return infoPath;
1766                 }
1767                 Map reverseMap = perProjectInfo == null ? null : new HashMap(5);
1768                 IClasspathEntry[] resolvedPath = getResolvedClasspath(
1769                         getRawClasspath(), 
1770                         generateMarkerOnError ? getOutputLocation() : null, 
1771                         ignoreUnresolvedEntry, 
1772                         generateMarkerOnError,
1773                         reverseMap);
1774
1775                 if (perProjectInfo != null){
1776                         if (perProjectInfo.classpath == null // .classpath file could not be read
1777                                 && generateMarkerOnError 
1778                                 && JavaProject.hasJavaNature(project)) {
1779                                         this.createClasspathProblemMarker(new JavaModelStatus(
1780                                                 IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
1781                                                 Util.bind("classpath.cannotReadClasspathFile", this.getElementName()))); //$NON-NLS-1$
1782                                 }
1783
1784                         perProjectInfo.lastResolvedClasspath = resolvedPath;
1785                         perProjectInfo.resolvedPathToRawEntries = reverseMap;
1786                 }
1787                 return resolvedPath;
1788         }
1789         
1790         /**
1791          * Internal variant which can process any arbitrary classpath
1792          */
1793         public IClasspathEntry[] getResolvedClasspath(
1794                 IClasspathEntry[] classpathEntries,
1795                 IPath projectOutputLocation, // only set if needing full classpath validation (and markers)
1796                 boolean ignoreUnresolvedEntry, // if unresolved entries are met, should it trigger initializations
1797                 boolean generateMarkerOnError,
1798                 Map reverseMap) // can be null if not interested in reverse mapping
1799                 throws JavaModelException {
1800
1801                 IJavaModelStatus status;
1802                 if (generateMarkerOnError){
1803                         flushClasspathProblemMarkers(false, false);
1804                 }
1805
1806                 int length = classpathEntries.length;
1807                 ArrayList resolvedEntries = new ArrayList();
1808                 
1809                 for (int i = 0; i < length; i++) {
1810
1811                         IClasspathEntry rawEntry = classpathEntries[i];
1812                         IPath resolvedPath;
1813                         status = null;
1814                         
1815                         /* validation if needed */
1816 //                      if (generateMarkerOnError || !ignoreUnresolvedEntry) {
1817 //                              status = JavaConventions.validateClasspathEntry(this, rawEntry, false);
1818 //                              if (generateMarkerOnError && !status.isOK()) createClasspathProblemMarker(status);
1819 //                      }
1820
1821                         switch (rawEntry.getEntryKind()){
1822                                 
1823                                 case IClasspathEntry.CPE_VARIABLE :
1824                                 
1825                                         IClasspathEntry resolvedEntry = JavaCore.getResolvedClasspathEntry(rawEntry);
1826                                         if (resolvedEntry == null) {
1827                                                 if (!ignoreUnresolvedEntry) throw new JavaModelException(status);
1828                                         } else {
1829                                                 if (reverseMap != null && reverseMap.get(resolvedPath = resolvedEntry.getPath()) == null) reverseMap.put(resolvedPath , rawEntry);
1830                                                 resolvedEntries.add(resolvedEntry);
1831                                         }
1832                                         break; 
1833
1834 //                              case IClasspathEntry.CPE_CONTAINER :
1835 //                              
1836 //                                      IClasspathContainer container = PHPCore.getClasspathContainer(rawEntry.getPath(), this);
1837 //                                      if (container == null){
1838 //                                              if (!ignoreUnresolvedEntry) throw new JavaModelException(status);
1839 //                                              break;
1840 //                                      }
1841 //
1842 //                                      IClasspathEntry[] containerEntries = container.getClasspathEntries();
1843 //                                      if (containerEntries == null) break;
1844 //
1845 //                                      // container was bound
1846 //                                      for (int j = 0, containerLength = containerEntries.length; j < containerLength; j++){
1847 //                                              IClasspathEntry cEntry = containerEntries[j];
1848 //                                              
1849 //                                              if (generateMarkerOnError) {
1850 //                                                      IJavaModelStatus containerStatus = JavaConventions.validateClasspathEntry(this, cEntry, false);
1851 //                                                      if (!containerStatus.isOK()) createClasspathProblemMarker(containerStatus);
1852 //                                              }
1853 //                                              // if container is exported, then its nested entries must in turn be exported  (21749)
1854 //                                              if (rawEntry.isExported()){
1855 //                                                      cEntry = new ClasspathEntry(cEntry.getContentKind(),
1856 //                                                              cEntry.getEntryKind(), cEntry.getPath(),
1857 //                                                              cEntry.getExclusionPatterns(), cEntry.getSourceAttachmentPath(),
1858 //                                                              cEntry.getSourceAttachmentRootPath(), cEntry.getOutputLocation(), 
1859 //                                                              true); // duplicate container entry for tagging it as exported
1860 //                                              }
1861 //                                              if (reverseMap != null && reverseMap.get(resolvedPath = cEntry.getPath()) == null) reverseMap.put(resolvedPath, rawEntry);
1862 //                                              resolvedEntries.add(cEntry);
1863 //                                      }
1864 //                                      break;
1865                                                                                 
1866                                 default :
1867
1868                                         if (reverseMap != null && reverseMap.get(resolvedPath = rawEntry.getPath()) == null) reverseMap.put(resolvedPath, rawEntry);
1869                                         resolvedEntries.add(rawEntry);
1870                                 
1871                         }                                       
1872                 }
1873
1874                 IClasspathEntry[] resolvedPath = new IClasspathEntry[resolvedEntries.size()];
1875                 resolvedEntries.toArray(resolvedPath);
1876
1877 //              if (generateMarkerOnError && projectOutputLocation != null) {
1878 //                      status = JavaConventions.validateClasspath(this, resolvedPath, projectOutputLocation);
1879 //                      if (!status.isOK()) createClasspathProblemMarker(status);
1880 //              }
1881                 return resolvedPath;
1882         }
1883
1884         /*
1885          * @see IJavaElement
1886          */
1887         public IResource getResource() {
1888                 return this.getProject();
1889         }
1890
1891         /**
1892          * @see IJavaProject
1893          */
1894         public ISearchableNameEnvironment getSearchableNameEnvironment()
1895                 throws JavaModelException {
1896
1897                 JavaProjectElementInfo info = getJavaProjectElementInfo();
1898                 if (info.getSearchableEnvironment() == null) {
1899                         info.setSearchableEnvironment(new SearchableEnvironment(this));
1900                 }
1901                 return info.getSearchableEnvironment();
1902         }
1903
1904         /**
1905          * Retrieve a shared property on a project. If the property is not defined, answers null.
1906          * Note that it is orthogonal to IResource persistent properties, and client code has to decide
1907          * which form of storage to use appropriately. Shared properties produce real resource files which
1908          * can be shared through a VCM onto a server. Persistent properties are not shareable.
1909          *
1910          * @see JavaProject#setSharedProperty(String, String)
1911          */
1912         public String getSharedProperty(String key) throws CoreException {
1913
1914                 String property = null;
1915                 IFile rscFile = getProject().getFile(key);
1916                 if (rscFile.exists()) {
1917                         property = new String(Util.getResourceContentsAsByteArray(rscFile));
1918                 }
1919                 return property;
1920         }
1921
1922         /**
1923          * @see JavaElement
1924          */
1925 //      public SourceMapper getSourceMapper() {
1926 //
1927 //              return null;
1928 //      }
1929
1930         /**
1931          * @see IJavaElement
1932          */
1933         public IResource getUnderlyingResource() throws JavaModelException {
1934                 if (!exists()) throw newNotPresentException();
1935                 return getProject();
1936         }
1937
1938         /**
1939          * @see IJavaProject
1940          */
1941         public boolean hasBuildState() {
1942
1943                 return JavaModelManager.getJavaModelManager().getLastBuiltState(this.getProject(), null) != null;
1944         }
1945
1946         /**
1947          * @see IJavaProject
1948          */
1949         public boolean hasClasspathCycle(IClasspathEntry[] preferredClasspath) {
1950                 HashSet cycleParticipants = new HashSet();
1951                 updateCycleParticipants(preferredClasspath, new ArrayList(2), cycleParticipants, ResourcesPlugin.getWorkspace().getRoot(), new HashSet(2));
1952                 return !cycleParticipants.isEmpty();
1953         }
1954         
1955         public boolean hasCycleMarker(){
1956                 return this.getCycleMarker() != null;
1957         }
1958
1959         public int hashCode() {
1960                 return project.hashCode();
1961         }
1962
1963         /**
1964          * Returns true if the given project is accessible and it has
1965          * a java nature, otherwise false.
1966          */
1967         public static boolean hasJavaNature(IProject project) { 
1968                 try {
1969                         return project.hasNature(PHPeclipsePlugin.PHP_NATURE_ID);
1970                 } catch (CoreException e) {
1971                         // project does not exist or is not open
1972                 }
1973                 return false;
1974         }
1975         
1976         /**
1977          * Answers true if the project potentially contains any source. A project which has no source is immutable.
1978          */
1979         public boolean hasSource() {
1980
1981                 // look if any source folder on the classpath
1982                 // no need for resolved path given source folder cannot be abstracted
1983                 IClasspathEntry[] entries;
1984                 try {
1985                         entries = this.getRawClasspath();
1986                 } catch (JavaModelException e) {
1987                         return true; // unsure
1988                 }
1989                 for (int i = 0, max = entries.length; i < max; i++) {
1990                         if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
1991                                 return true;
1992                         }
1993                 }
1994                 return false;
1995         }
1996
1997         /**
1998          * Compare current classpath with given one to see if any different.
1999          * Note that the argument classpath contains its binary output.
2000          */
2001         public boolean isClasspathEqualsTo(IClasspathEntry[] newClasspath, IPath newOutputLocation, IClasspathEntry[] otherClasspathWithOutput)
2002                 throws JavaModelException {
2003
2004                 if (otherClasspathWithOutput != null && otherClasspathWithOutput.length > 0) {
2005
2006                         int length = otherClasspathWithOutput.length;
2007                         if (length == newClasspath.length + 1) {
2008                                 // output is amongst file entries (last one)
2009
2010                                 // compare classpath entries
2011                                 for (int i = 0; i < length - 1; i++) {
2012                                         if (!otherClasspathWithOutput[i].equals(newClasspath[i]))
2013                                                 return false;
2014                                 }
2015                                 // compare binary outputs
2016                                 IClasspathEntry output = otherClasspathWithOutput[length - 1];
2017                                 if (output.getContentKind() == ClasspathEntry.K_OUTPUT
2018                                                 && output.getPath().equals(newOutputLocation))
2019                                         return true;
2020                         }
2021                 }
2022                 return false;
2023         }
2024         
2025
2026         
2027         /*
2028          * @see IJavaProject
2029          */
2030         public boolean isOnClasspath(IJavaElement element) {
2031                 IPath path = element.getPath();
2032                 switch (element.getElementType()) {
2033                         case IJavaElement.PACKAGE_FRAGMENT_ROOT:
2034                                 if (!((IPackageFragmentRoot)element).isArchive()) {
2035                                         // ensure that folders are only excluded if all of their children are excluded
2036                                         path = path.append("*"); //$NON-NLS-1$
2037                                 }
2038                                 break;
2039                         case IJavaElement.PACKAGE_FRAGMENT:
2040                                 if (!((IPackageFragmentRoot)element.getParent()).isArchive()) {
2041                                         // ensure that folders are only excluded if all of their children are excluded
2042                                         path = path.append("*"); //$NON-NLS-1$
2043                                 }
2044                                 break;
2045                 }
2046                 return this.isOnClasspath(path);
2047         }
2048         private boolean isOnClasspath(IPath path) {
2049                 IClasspathEntry[] classpath;
2050                 try {
2051                         classpath = this.getResolvedClasspath(true/*ignore unresolved variable*/);
2052                 } catch(JavaModelException e){
2053                         return false; // not a Java project
2054                 }
2055                 for (int i = 0; i < classpath.length; i++) {
2056                         IClasspathEntry entry = classpath[i];
2057                         if (entry.getPath().isPrefixOf(path) 
2058                                         && !Util.isExcluded(path, ((ClasspathEntry)entry).fullExclusionPatternChars())) {
2059                                 return true;
2060                         }
2061                 }
2062                 return false;
2063         }
2064         /*
2065          * @see IJavaProject
2066          */
2067         public boolean isOnClasspath(IResource resource) {
2068                 IPath path = resource.getFullPath();
2069                 
2070                 // ensure that folders are only excluded if all of their children are excluded
2071                 if (resource.getType() == IResource.FOLDER) {
2072                         path = path.append("*"); //$NON-NLS-1$
2073                 }
2074                 
2075                 return this.isOnClasspath(path);
2076         }
2077
2078         private IPath getPluginWorkingLocation() {
2079                 return this.project.getWorkingLocation(JavaCore.PLUGIN_ID);
2080         }       
2081         /*
2082          * load preferences from a shareable format (VCM-wise)
2083          */
2084          public Preferences loadPreferences() {
2085                 
2086                 Preferences preferences = new Preferences();
2087                 
2088 //              File prefFile = getProject().getLocation().append(PREF_FILENAME).toFile();
2089                 IPath projectMetaLocation = getPluginWorkingLocation();
2090                 if (projectMetaLocation != null) {
2091                         File prefFile = projectMetaLocation.append(PREF_FILENAME).toFile();
2092                         if (prefFile.exists()) { // load preferences from file
2093                                 InputStream in = null;
2094                                 try {
2095                                         in = new BufferedInputStream(new FileInputStream(prefFile));
2096                                         preferences.load(in);
2097                                         return preferences;
2098                                 } catch (IOException e) { // problems loading preference store - quietly ignore
2099                                 } finally {
2100                                         if (in != null) {
2101                                                 try {
2102                                                         in.close();
2103                                                 } catch (IOException e) { // ignore problems with close
2104                                                 }
2105                                         }
2106                                 }
2107                         }
2108                 }
2109                 return null;
2110          }
2111          
2112         /**
2113          * @see IJavaProject#newEvaluationContext
2114          */
2115 //      public IEvaluationContext newEvaluationContext() {
2116 //
2117 //              return new EvaluationContextWrapper(new EvaluationContext(), this);
2118 //      }
2119
2120         /**
2121          * @see IJavaProject
2122          */
2123 //      public ITypeHierarchy newTypeHierarchy(
2124 //              IRegion region,
2125 //              IProgressMonitor monitor)
2126 //              throws JavaModelException {
2127 //
2128 //              if (region == null) {
2129 //                      throw new IllegalArgumentException(ProjectPrefUtil.bind("hierarchy.nullRegion"));//$NON-NLS-1$
2130 //              }
2131 //              CreateTypeHierarchyOperation op =
2132 //                      new CreateTypeHierarchyOperation(null, region, this, true);
2133 //              runOperation(op, monitor);
2134 //              return op.getResult();
2135 //      }
2136
2137         /**
2138          * @see IJavaProject
2139          */
2140 //      public ITypeHierarchy newTypeHierarchy(
2141 //              IType type,
2142 //              IRegion region,
2143 //              IProgressMonitor monitor)
2144 //              throws JavaModelException {
2145 //
2146 //              if (type == null) {
2147 //                      throw new IllegalArgumentException(ProjectPrefUtil.bind("hierarchy.nullFocusType"));//$NON-NLS-1$
2148 //              }
2149 //              if (region == null) {
2150 //                      throw new IllegalArgumentException(ProjectPrefUtil.bind("hierarchy.nullRegion"));//$NON-NLS-1$
2151 //              }
2152 //              CreateTypeHierarchyOperation op =
2153 //                      new CreateTypeHierarchyOperation(type, region, this, true);
2154 //              runOperation(op, monitor);
2155 //              return op.getResult();
2156 //      }
2157
2158 //      /**
2159 //       * Open project if resource isn't closed
2160 //       */
2161 //      protected void openWhenClosed(IProgressMonitor pm) throws JavaModelException {
2162 //
2163 //              if (!this.fProject.isOpen()) {
2164 //                      throw newNotPresentException();
2165 //              } else {
2166 //                      super.openWhenClosed(pm);
2167 //              }
2168 //      }
2169
2170         public String[] projectPrerequisites(IClasspathEntry[] entries)
2171                 throws JavaModelException {
2172                         
2173                 ArrayList prerequisites = new ArrayList();
2174                 // need resolution
2175                 entries = getResolvedClasspath(entries, null, true, false, null/*no reverse map*/);
2176                 for (int i = 0, length = entries.length; i < length; i++) {
2177                         IClasspathEntry entry = entries[i];
2178                         if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
2179                                 prerequisites.add(entry.getPath().lastSegment());
2180                         }
2181                 }
2182                 int size = prerequisites.size();
2183                 if (size == 0) {
2184                         return NO_PREREQUISITES;
2185                 } else {
2186                         String[] result = new String[size];
2187                         prerequisites.toArray(result);
2188                         return result;
2189                 }
2190         }
2191
2192
2193         /**
2194          * Reads the .classpath file from disk and returns the list of entries it contains (including output location entry)
2195          * Returns null if .classfile is not present.
2196          * Returns INVALID_CLASSPATH if it has a format problem.
2197          */
2198         protected IClasspathEntry[] readClasspathFile(boolean createMarker, boolean logProblems) {
2199
2200                 try {
2201                         String xmlClasspath = getSharedProperty(CLASSPATH_FILENAME);
2202                         if (xmlClasspath == null) return null;
2203                         return decodeClasspath(xmlClasspath, createMarker, logProblems);
2204                 } catch(CoreException e) {
2205                         // file does not exist (or not accessible)
2206                         if (createMarker && this.getProject().isAccessible()) {
2207                                         this.createClasspathProblemMarker(new JavaModelStatus(
2208                                                 IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
2209                                                 Util.bind("classpath.cannotReadClasspathFile", this.getElementName()))); //$NON-NLS-1$
2210                         }
2211                         if (logProblems) {
2212                                 Util.log(e, 
2213                                         "Exception while retrieving "+ this.getPath() //$NON-NLS-1$
2214                                         +"/.classpath, will revert to default classpath"); //$NON-NLS-1$
2215                         }
2216                 }
2217                 return null;
2218         }
2219
2220         /**
2221          * Removes the given builder from the build spec for the given project.
2222          */
2223         protected void removeFromBuildSpec(String builderID) throws CoreException {
2224
2225                 IProjectDescription description = getProject().getDescription();
2226                 ICommand[] commands = description.getBuildSpec();
2227                 for (int i = 0; i < commands.length; ++i) {
2228                         if (commands[i].getBuilderName().equals(builderID)) {
2229                                 ICommand[] newCommands = new ICommand[commands.length - 1];
2230                                 System.arraycopy(commands, 0, newCommands, 0, i);
2231                                 System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1);
2232                                 description.setBuildSpec(newCommands);
2233                                 getProject().setDescription(description, null);
2234                                 return;
2235                         }
2236                 }
2237         }
2238
2239
2240         /**
2241          * @see JavaElement#rootedAt(IJavaProject)
2242          */
2243         public IJavaElement rootedAt(IJavaProject project) {
2244                 return project;
2245         
2246         }
2247         
2248         /**
2249          * Answers an ID which is used to distinguish project/entries during package
2250          * fragment root computations
2251          */
2252         public String rootID(){
2253                 return "[PRJ]"+this.getProject().getFullPath(); //$NON-NLS-1$
2254         }
2255         
2256         /**
2257          * Saves the classpath in a shareable format (VCM-wise) only when necessary, that is, if  it is semantically different
2258          * from the existing one in file. Will never write an identical one.
2259          * 
2260          * @return Return whether the .classpath file was modified.
2261          */
2262         public boolean saveClasspath(IClasspathEntry[] newClasspath, IPath newOutputLocation) throws JavaModelException {
2263
2264                 if (!getProject().exists()) return false;
2265
2266                 IClasspathEntry[] fileEntries = readClasspathFile(false /*don't create markers*/, false/*don't log problems*/);
2267                 if (fileEntries != null && isClasspathEqualsTo(newClasspath, newOutputLocation, fileEntries)) {
2268                         // no need to save it, it is the same
2269                         return false;
2270                 }
2271
2272                 // actual file saving
2273                 try {
2274                         setSharedProperty(CLASSPATH_FILENAME, encodeClasspath(newClasspath, newOutputLocation, true));
2275                         return true;
2276                 } catch (CoreException e) {
2277                         throw new JavaModelException(e);
2278                 }
2279         }
2280
2281
2282         /**
2283          * Save project custom preferences to shareable file (.jprefs)
2284          */
2285         private void savePreferences(Preferences preferences) {
2286                 
2287                 if (!JavaProject.hasJavaNature(this.project)) return; // ignore
2288                 
2289                 if (preferences == null || (!preferences.needsSaving() && preferences.propertyNames().length != 0)) {
2290                         // nothing to save
2291                         return;
2292                 }
2293         
2294                 // preferences need to be saved
2295                 // the preferences file is located in the plug-in's state area
2296                 // at a well-known name (.jprefs)
2297 //              File prefFile = this.project.getLocation().append(PREF_FILENAME).toFile();
2298                 File prefFile = getPluginWorkingLocation().append(PREF_FILENAME).toFile();
2299                 if (preferences.propertyNames().length == 0) {
2300                         // there are no preference settings
2301                         // rather than write an empty file, just delete any existing file
2302                         if (prefFile.exists()) {
2303                                 prefFile.delete(); // don't worry if delete unsuccessful
2304                         }
2305                         return;
2306                 }
2307                 
2308                 // write file, overwriting an existing one
2309                 OutputStream out = null;
2310                 try {
2311                         // do it as carefully as we know how so that we don't lose/mangle
2312                         // the setting in times of stress
2313                         out = new BufferedOutputStream(new FileOutputStream(prefFile));
2314                         preferences.store(out, null);
2315                 } catch (IOException e) { // problems saving preference store - quietly ignore
2316                 } finally {
2317                         if (out != null) {
2318                                 try {
2319                                         out.close();
2320                                 } catch (IOException e) { // ignore problems with close
2321                                 }
2322                         }
2323                 }
2324         }
2325         /**
2326          * Update the Java command in the build spec (replace existing one if present,
2327          * add one first if none).
2328          */
2329         private void setJavaCommand(
2330                 IProjectDescription description,
2331                 ICommand newCommand)
2332                 throws CoreException {
2333
2334                 ICommand[] oldCommands = description.getBuildSpec();
2335                 ICommand oldJavaCommand = getJavaCommand(description);
2336                 ICommand[] newCommands;
2337
2338                 if (oldJavaCommand == null) {
2339                         // Add a Java build spec before other builders (1FWJK7I)
2340                         newCommands = new ICommand[oldCommands.length + 1];
2341                         System.arraycopy(oldCommands, 0, newCommands, 1, oldCommands.length);
2342                         newCommands[0] = newCommand;
2343                 } else {
2344                         for (int i = 0, max = oldCommands.length; i < max; i++) {
2345                                 if (oldCommands[i] == oldJavaCommand) {
2346                                         oldCommands[i] = newCommand;
2347                                         break;
2348                                 }
2349                         }
2350                         newCommands = oldCommands;
2351                 }
2352
2353                 // Commit the spec change into the project
2354                 description.setBuildSpec(newCommands);
2355                 getProject().setDescription(description, null);
2356         }
2357
2358         /**
2359          * @see org.eclipse.jdt.core.IJavaProject#setOptions(Map)
2360          */
2361         public void setOptions(Map newOptions) {
2362
2363                 Preferences preferences;
2364                 setPreferences(preferences = new Preferences()); // always reset (26255)
2365                 if (newOptions != null){
2366                         Iterator keys = newOptions.keySet().iterator();
2367                         while (keys.hasNext()){
2368                                 String key = (String)keys.next();
2369                                 if (!JavaModelManager.OptionNames.contains(key)) continue; // unrecognized option
2370                                 // no filtering for encoding (custom encoding for project is allowed)
2371                                 String value = (String)newOptions.get(key);
2372                                 preferences.setDefault(key, CUSTOM_DEFAULT_OPTION_VALUE); // empty string isn't the default (26251)
2373                                 preferences.setValue(key, value);
2374                         }
2375                 }
2376                 
2377                 // persist options
2378                 savePreferences(preferences);   
2379         }
2380
2381         /**
2382          * @see IJavaProject
2383          */
2384         public void setOutputLocation(IPath path, IProgressMonitor monitor)
2385                 throws JavaModelException {
2386
2387                 if (path == null) {
2388                         throw new IllegalArgumentException(Util.bind("path.nullpath")); //$NON-NLS-1$
2389                 }
2390                 if (path.equals(getOutputLocation())) {
2391                         return;
2392                 }
2393                 this.setRawClasspath(SetClasspathOperation.ReuseClasspath, path, monitor);
2394         }
2395
2396         /*
2397          * Set cached preferences, no preference file is saved, only info is updated
2398          */
2399         public void setPreferences(Preferences preferences) {
2400                 IProject project = getProject();
2401                 if (!JavaProject.hasJavaNature(project)) return; // ignore
2402                 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfo(project, true);
2403                 perProjectInfo.preferences = preferences;
2404         }
2405
2406         /**
2407          * @see IJavaProject
2408          */
2409         public void setRawClasspath(
2410                 IClasspathEntry[] entries,
2411                 IPath outputLocation,
2412                 IProgressMonitor monitor)
2413                 throws JavaModelException {
2414
2415                 setRawClasspath(
2416                         entries, 
2417                         outputLocation, 
2418                         monitor, 
2419                         true, // canChangeResource (as per API contract)
2420                         getResolvedClasspath(true), // ignoreUnresolvedVariable
2421                         true, // needValidation
2422                         true); // need to save
2423         }
2424
2425         public void setRawClasspath(
2426                 IClasspathEntry[] newEntries,
2427                 IPath newOutputLocation,
2428                 IProgressMonitor monitor,
2429                 boolean canChangeResource,
2430                 IClasspathEntry[] oldResolvedPath,
2431                 boolean needValidation,
2432                 boolean needSave)
2433                 throws JavaModelException {
2434
2435                 JavaModelManager manager =
2436                         (JavaModelManager) JavaModelManager.getJavaModelManager();
2437                 try {
2438                         IClasspathEntry[] newRawPath = newEntries;
2439                         if (newRawPath == null) { //are we already with the default classpath
2440                                 newRawPath = defaultClasspath();
2441                         }
2442                         SetClasspathOperation op =
2443                                 new SetClasspathOperation(
2444                                         this, 
2445                                         oldResolvedPath, 
2446                                         newRawPath, 
2447                                         newOutputLocation,
2448                                         canChangeResource, 
2449                                         needValidation,
2450                                         needSave);
2451                         runOperation(op, monitor);
2452                         
2453                 } catch (JavaModelException e) {
2454                         manager.flush();
2455                         throw e;
2456                 }
2457         }
2458
2459         /**
2460          * @see IJavaProject
2461          */
2462         public void setRawClasspath(
2463                 IClasspathEntry[] entries,
2464                 IProgressMonitor monitor)
2465                 throws JavaModelException {
2466
2467                 setRawClasspath(
2468                         entries, 
2469                         SetClasspathOperation.ReuseOutputLocation, 
2470                         monitor, 
2471                         true, // canChangeResource (as per API contract)
2472                         getResolvedClasspath(true), // ignoreUnresolvedVariable
2473                         true, // needValidation
2474                         true); // need to save
2475         }
2476
2477         /**
2478          * NOTE: <code>null</code> specifies default classpath, and an empty
2479          * array specifies an empty classpath.
2480          *
2481          * @exception NotPresentException if this project does not exist.
2482          */
2483         protected void setRawClasspath0(IClasspathEntry[] rawEntries)
2484                 throws JavaModelException {
2485
2486                 JavaModelManager.PerProjectInfo info = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project);
2487         
2488                 synchronized (info) {
2489                         if (rawEntries != null) {
2490                                 info.classpath = rawEntries;
2491                         }
2492                         
2493                         // clear cache of resolved classpath
2494                         info.lastResolvedClasspath = null;
2495                         info.resolvedPathToRawEntries = null;
2496                 }
2497         }
2498
2499         /**
2500          * Record a shared persistent property onto a project.
2501          * Note that it is orthogonal to IResource persistent properties, and client code has to decide
2502          * which form of storage to use appropriately. Shared properties produce real resource files which
2503          * can be shared through a VCM onto a server. Persistent properties are not shareable.
2504          * 
2505          * shared properties end up in resource files, and thus cannot be modified during
2506          * delta notifications (a CoreException would then be thrown).
2507          * 
2508          * @see JavaProject#getSharedProperty(String key)
2509          */
2510         public void setSharedProperty(String key, String value) throws CoreException {
2511
2512                 IFile rscFile = getProject().getFile(key);
2513                 InputStream inputStream = new ByteArrayInputStream(value.getBytes());
2514                 // update the resource content
2515                 if (rscFile.exists()) {
2516                         if (rscFile.isReadOnly()) {
2517                                 // provide opportunity to checkout read-only .classpath file (23984)
2518                                 ResourcesPlugin.getWorkspace().validateEdit(new IFile[]{rscFile}, null);
2519                         }
2520                         rscFile.setContents(inputStream, IResource.FORCE, null);
2521                 } else {
2522                         rscFile.create(inputStream, IResource.FORCE, null);
2523                 }
2524         }
2525
2526         /**
2527          * Update cycle markers for all java projects
2528          */
2529         public static void updateAllCycleMarkers() throws JavaModelException {
2530
2531                 //long start = System.currentTimeMillis();
2532
2533                 JavaModelManager manager = JavaModelManager.getJavaModelManager();
2534                 IJavaProject[] projects = manager.getJavaModel().getJavaProjects();
2535                 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
2536
2537                 HashSet cycleParticipants = new HashSet();
2538                 HashSet traversed = new HashSet();
2539                 int length = projects.length;
2540                 
2541                 // compute cycle participants
2542                 ArrayList prereqChain = new ArrayList();
2543                 for (int i = 0; i < length; i++){
2544                         JavaProject project = (JavaProject)projects[i];
2545                         if (!traversed.contains(project.getPath())){
2546                                 prereqChain.clear();
2547                                 project.updateCycleParticipants(null, prereqChain, cycleParticipants, workspaceRoot, traversed);
2548                         }
2549                 }
2550                 //System.out.println("updateAllCycleMarkers: " + (System.currentTimeMillis() - start) + " ms");
2551
2552                 for (int i = 0; i < length; i++){
2553                         JavaProject project = (JavaProject)projects[i];
2554                         
2555                         if (cycleParticipants.contains(project.getPath())){
2556                                 IMarker cycleMarker = project.getCycleMarker();
2557                                 String circularCPOption = project.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true);
2558                                 int circularCPSeverity = JavaCore.ERROR.equals(circularCPOption) ? IMarker.SEVERITY_ERROR : IMarker.SEVERITY_WARNING;
2559                                 if (cycleMarker != null) {
2560                                         // update existing cycle marker if needed
2561                                         try {
2562                                                 int existingSeverity = ((Integer)cycleMarker.getAttribute(IMarker.SEVERITY)).intValue();
2563                                                 if (existingSeverity != circularCPSeverity) {
2564                                                         cycleMarker.setAttribute(IMarker.SEVERITY, circularCPSeverity);
2565                                                 }
2566                                         } catch (CoreException e) {
2567                                                 throw new JavaModelException(e);
2568                                         }
2569                                 } else {
2570                                         // create new marker
2571                                         project.createClasspathProblemMarker(
2572                                                 new JavaModelStatus(IJavaModelStatusConstants.CLASSPATH_CYCLE, project)); 
2573                                 }
2574                         } else {
2575                                 project.flushClasspathProblemMarkers(true, false);
2576                         }                       
2577                 }
2578         }
2579
2580         /**
2581          * If a cycle is detected, then cycleParticipants contains all the paths of projects involved in this cycle (directly and indirectly),
2582          * no cycle if the set is empty (and started empty)
2583          */
2584         public void updateCycleParticipants(
2585                         IClasspathEntry[] preferredClasspath, 
2586                         ArrayList prereqChain, 
2587                         HashSet cycleParticipants, 
2588                         IWorkspaceRoot workspaceRoot,
2589                         HashSet traversed){
2590
2591                 IPath path = this.getPath();
2592                 prereqChain.add(path);
2593                 traversed.add(path);
2594                 try {
2595                         IClasspathEntry[] classpath = preferredClasspath == null ? getResolvedClasspath(true) : preferredClasspath;
2596                         for (int i = 0, length = classpath.length; i < length; i++) {
2597                                 IClasspathEntry entry = classpath[i];
2598                                 
2599                                 if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT){
2600                                         IPath prereqProjectPath = entry.getPath();
2601                                         int index = cycleParticipants.contains(prereqProjectPath) ? 0 : prereqChain.indexOf(prereqProjectPath);
2602                                         if (index >= 0) { // refer to cycle, or in cycle itself
2603                                                 for (int size = prereqChain.size(); index < size; index++) {
2604                                                         cycleParticipants.add(prereqChain.get(index)); 
2605                                                 }
2606                                         } else {
2607                                                 if (!traversed.contains(prereqProjectPath)) {
2608                                                         IResource member = workspaceRoot.findMember(prereqProjectPath);
2609                                                         if (member != null && member.getType() == IResource.PROJECT){
2610                                                                 JavaProject project = (JavaProject)JavaCore.create((IProject)member);
2611                                                                 project.updateCycleParticipants(null, prereqChain, cycleParticipants, workspaceRoot, traversed);
2612                                                         }
2613                                                 }
2614                                         }
2615                                 }
2616                         }
2617                 } catch(JavaModelException e){
2618                 }
2619                 prereqChain.remove(path);
2620         }
2621         /**
2622          * Reset the collection of package fragment roots (local ones) - only if opened.
2623          * Need to check *all* package fragment roots in order to reset NameLookup
2624          */
2625         public void updatePackageFragmentRoots(){
2626                 
2627                         if (this.isOpen()) {
2628                                 try {
2629                                         JavaProjectElementInfo info = getJavaProjectElementInfo();
2630
2631                                         IClasspathEntry[] classpath = getResolvedClasspath(true);
2632 //                                      NameLookup lookup = info.getNameLookup();
2633 //                                      if (lookup != null){
2634 //                                              IPackageFragmentRoot[] oldRoots = lookup.fPackageFragmentRoots;
2635 //                                              IPackageFragmentRoot[] newRoots = computePackageFragmentRoots(classpath, true);
2636 //                                              checkIdentical: { // compare all pkg fragment root lists
2637 //                                                      if (oldRoots.length == newRoots.length){
2638 //                                                              for (int i = 0, length = oldRoots.length; i < length; i++){
2639 //                                                                      if (!oldRoots[i].equals(newRoots[i])){
2640 //                                                                              break checkIdentical;
2641 //                                                                      }
2642 //                                                              }
2643 //                                                              return; // no need to update
2644 //                                                      }       
2645 //                                              }
2646 //                                              info.setNameLookup(null); // discard name lookup (hold onto roots)
2647 //                                      }                               
2648                                         info.setNonJavaResources(null);
2649                                         info.setChildren(
2650                                                 computePackageFragmentRoots(classpath, false));         
2651
2652                                 } catch(JavaModelException e){
2653                                         try {
2654                                                 close(); // could not do better
2655                                         } catch(JavaModelException ex){
2656                                         }
2657                                 }
2658                         }
2659         }
2660
2661                 public void removeLoadPathEntry(IProject anotherPHPProject) {
2662                         Iterator entries = getLoadPathEntries().iterator();
2663                         while (entries.hasNext()) {
2664                                 LoadPathEntry entry = (LoadPathEntry) entries.next();
2665                                 if (entry.getType() == LoadPathEntry.TYPE_PROJECT && entry.getProject().getName().equals(anotherPHPProject.getName())) {
2666                                         getLoadPathEntries().remove(entry);
2667                                         fScratched = true;
2668                                         break;
2669                                 }
2670                         }
2671                 }
2672
2673                 public void save() throws CoreException {
2674                         if (fScratched) {
2675                                 InputStream xmlPath = new ByteArrayInputStream(getLoadPathXML().getBytes());
2676                                 IFile loadPathsFile = getLoadPathEntriesFile();
2677                                 if (!loadPathsFile.exists())
2678                                         loadPathsFile.create(xmlPath, true, null);
2679                                 else
2680                                         loadPathsFile.setContents(xmlPath, true, false, null);
2681
2682                                 fScratched = false;
2683                         }
2684                 }
2685
2686 }