KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > CPListElement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Status;
23
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IWorkspaceRoot;
26 import org.eclipse.core.resources.ResourcesPlugin;
27
28 import org.eclipse.jdt.core.ClasspathContainerInitializer;
29 import org.eclipse.jdt.core.IAccessRule;
30 import org.eclipse.jdt.core.IClasspathAttribute;
31 import org.eclipse.jdt.core.IClasspathContainer;
32 import org.eclipse.jdt.core.IClasspathEntry;
33 import org.eclipse.jdt.core.IJavaProject;
34 import org.eclipse.jdt.core.JavaCore;
35 import org.eclipse.jdt.core.JavaModelException;
36
37 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
38
39 import org.eclipse.jdt.launching.JavaRuntime;
40
41 import org.eclipse.jdt.ui.JavaUI;
42
43 import org.eclipse.jdt.internal.ui.JavaPlugin;
44
45 public class CPListElement {
46     
47     public static final String JavaDoc SOURCEATTACHMENT= "sourcepath"; //$NON-NLS-1$
48
public static final String JavaDoc OUTPUT= "output"; //$NON-NLS-1$
49
public static final String JavaDoc EXCLUSION= "exclusion"; //$NON-NLS-1$
50
public static final String JavaDoc INCLUSION= "inclusion"; //$NON-NLS-1$
51

52     public static final String JavaDoc ACCESSRULES= "accessrules"; //$NON-NLS-1$
53
public static final String JavaDoc COMBINE_ACCESSRULES= "combineaccessrules"; //$NON-NLS-1$
54

55     public static final String JavaDoc JAVADOC= IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME;
56     public static final String JavaDoc NATIVE_LIB_PATH= JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY;
57     
58     private IJavaProject fProject;
59     
60     private int fEntryKind;
61     private IPath fPath, fOrginalPath;
62     private IResource fResource;
63     private boolean fIsExported;
64     private boolean fIsMissing;
65     
66     private Object JavaDoc fParentContainer;
67         
68     private IClasspathEntry fCachedEntry;
69     private ArrayList JavaDoc fChildren;
70     private IPath fLinkTarget, fOrginalLinkTarget;
71     
72     private CPListElement() {}
73     
74     public CPListElement(IJavaProject project, int entryKind, IPath path, IResource res) {
75         this(null, project, entryKind, path, res);
76     }
77     
78     public CPListElement(Object JavaDoc parent, IJavaProject project, int entryKind, IPath path, IResource res) {
79         this(parent, project, entryKind, path, res, null);
80     }
81     
82     public CPListElement(IJavaProject project, int entryKind) {
83         this(null, project, entryKind, null, null);
84     }
85     
86     public CPListElement(Object JavaDoc parent, IJavaProject project, int entryKind, IPath path, IResource res, IPath linkTarget) {
87         fProject= project;
88
89         fEntryKind= entryKind;
90         fPath= path;
91         fOrginalPath= path;
92         fLinkTarget= linkTarget;
93         fOrginalLinkTarget= linkTarget;
94         fChildren= new ArrayList JavaDoc();
95         fResource= res;
96         fIsExported= false;
97         
98         fIsMissing= false;
99         fCachedEntry= null;
100         fParentContainer= parent;
101         
102         switch (entryKind) {
103             case IClasspathEntry.CPE_SOURCE:
104                 createAttributeElement(OUTPUT, null, true);
105                 createAttributeElement(INCLUSION, new Path[0], true);
106                 createAttributeElement(EXCLUSION, new Path[0], true);
107                 createAttributeElement(NATIVE_LIB_PATH, null, false);
108                 break;
109             case IClasspathEntry.CPE_LIBRARY:
110             case IClasspathEntry.CPE_VARIABLE:
111                 createAttributeElement(SOURCEATTACHMENT, null, true);
112                 createAttributeElement(JAVADOC, null, false);
113                 createAttributeElement(NATIVE_LIB_PATH, null, false);
114                 createAttributeElement(ACCESSRULES, new IAccessRule[0], true);
115                 break;
116             case IClasspathEntry.CPE_PROJECT:
117                 createAttributeElement(ACCESSRULES, new IAccessRule[0], true);
118                 createAttributeElement(COMBINE_ACCESSRULES, Boolean.FALSE, true); // not rendered
119
createAttributeElement(NATIVE_LIB_PATH, null, false);
120                 break;
121             case IClasspathEntry.CPE_CONTAINER:
122                 createAttributeElement(ACCESSRULES, new IAccessRule[0], true);
123                 try {
124                     IClasspathContainer container= JavaCore.getClasspathContainer(fPath, fProject);
125                     if (container != null) {
126                         IClasspathEntry[] entries= container.getClasspathEntries();
127                         if (entries != null) { // invalid container implementation
128
for (int i= 0; i < entries.length; i++) {
129                                 IClasspathEntry entry= entries[i];
130                                 if (entry != null) {
131                                     CPListElement curr= createFromExisting(this, entry, fProject);
132                                     fChildren.add(curr);
133                                 } else {
134                                     JavaPlugin.logErrorMessage("Null entry in container '" + fPath + "'"); //$NON-NLS-1$//$NON-NLS-2$
135
}
136                             }
137                         } else {
138                             JavaPlugin.logErrorMessage("container returns null as entries: '" + fPath + "'"); //$NON-NLS-1$//$NON-NLS-2$
139
}
140                     }
141                 } catch (JavaModelException e) {
142                 }
143                 createAttributeElement(NATIVE_LIB_PATH, null, false);
144                 break;
145             default:
146         }
147     }
148     
149     public IClasspathEntry getClasspathEntry() {
150         if (fCachedEntry == null) {
151             fCachedEntry= newClasspathEntry();
152         }
153         return fCachedEntry;
154     }
155     
156     
157     private IClasspathAttribute[] getClasspathAttributes() {
158         ArrayList JavaDoc res= new ArrayList JavaDoc();
159         for (int i= 0; i < fChildren.size(); i++) {
160             Object JavaDoc curr= fChildren.get(i);
161             if (curr instanceof CPListElementAttribute) {
162                 CPListElementAttribute elem= (CPListElementAttribute) curr;
163                 if (!elem.isBuiltIn() && elem.getValue() != null) {
164                     res.add(elem.getClasspathAttribute());
165                 }
166             }
167         }
168         return (IClasspathAttribute[]) res.toArray(new IClasspathAttribute[res.size()]);
169     }
170     
171
172     private IClasspathEntry newClasspathEntry() {
173
174         IClasspathAttribute[] extraAttributes= getClasspathAttributes();
175         switch (fEntryKind) {
176             case IClasspathEntry.CPE_SOURCE:
177                 IPath[] inclusionPattern= (IPath[]) getAttribute(INCLUSION);
178                 IPath[] exclusionPattern= (IPath[]) getAttribute(EXCLUSION);
179                 IPath outputLocation= (IPath) getAttribute(OUTPUT);
180                 return JavaCore.newSourceEntry(fPath, inclusionPattern, exclusionPattern, outputLocation, extraAttributes);
181             case IClasspathEntry.CPE_LIBRARY: {
182                 IPath attach= (IPath) getAttribute(SOURCEATTACHMENT);
183                 IAccessRule[] accesRules= (IAccessRule[]) getAttribute(ACCESSRULES);
184                 return JavaCore.newLibraryEntry(fPath, attach, null, accesRules, extraAttributes, isExported());
185             }
186             case IClasspathEntry.CPE_PROJECT: {
187                 IAccessRule[] accesRules= (IAccessRule[]) getAttribute(ACCESSRULES);
188                 boolean combineAccessRules= ((Boolean JavaDoc) getAttribute(COMBINE_ACCESSRULES)).booleanValue();
189                 return JavaCore.newProjectEntry(fPath, accesRules, combineAccessRules, extraAttributes, isExported());
190             }
191             case IClasspathEntry.CPE_CONTAINER: {
192                 IAccessRule[] accesRules= (IAccessRule[]) getAttribute(ACCESSRULES);
193                 return JavaCore.newContainerEntry(fPath, accesRules, extraAttributes, isExported());
194             }
195             case IClasspathEntry.CPE_VARIABLE: {
196                 IPath varAttach= (IPath) getAttribute(SOURCEATTACHMENT);
197                 IAccessRule[] accesRules= (IAccessRule[]) getAttribute(ACCESSRULES);
198                 return JavaCore.newVariableEntry(fPath, varAttach, null, accesRules, extraAttributes, isExported());
199             }
200             default:
201                 return null;
202         }
203     }
204     
205     /**
206      * Gets the class path entry path.
207      * @return returns the path
208      * @see IClasspathEntry#getPath()
209      */

210     public IPath getPath() {
211         return fPath;
212     }
213
214     /**
215      * Gets the class path entry kind.
216      * @return the entry kind
217      * @see IClasspathEntry#getEntryKind()
218      */

219     public int getEntryKind() {
220         return fEntryKind;
221     }
222
223     /**
224      * Entries without resource are either non existing or a variable entry
225      * External jars do not have a resource
226      * @return returns the resource
227      */

228     public IResource getResource() {
229         return fResource;
230     }
231     
232     public CPListElementAttribute setAttribute(String JavaDoc key, Object JavaDoc value) {
233         CPListElementAttribute attribute= findAttributeElement(key);
234         if (attribute == null) {
235             return null;
236         }
237         if (key.equals(EXCLUSION) || key.equals(INCLUSION)) {
238             Assert.isTrue(value != null || fEntryKind != IClasspathEntry.CPE_SOURCE);
239         }
240         
241         if (key.equals(ACCESSRULES)) {
242             Assert.isTrue(value != null || fEntryKind == IClasspathEntry.CPE_SOURCE);
243         }
244         if (key.equals(COMBINE_ACCESSRULES)) {
245             Assert.isTrue(value instanceof Boolean JavaDoc);
246         }
247         
248         attribute.setValue(value);
249         return attribute;
250     }
251     
252     public boolean addToExclusions(IPath path) {
253         String JavaDoc key= CPListElement.EXCLUSION;
254         return addFilter(path, key);
255     }
256     
257     public boolean addToInclusion(IPath path) {
258         String JavaDoc key= CPListElement.INCLUSION;
259         return addFilter(path, key);
260     }
261     
262     public boolean removeFromExclusions(IPath path) {
263         String JavaDoc key= CPListElement.EXCLUSION;
264         return removeFilter(path, key);
265     }
266     
267     public boolean removeFromInclusion(IPath path) {
268         String JavaDoc key= CPListElement.INCLUSION;
269         return removeFilter(path, key);
270     }
271     
272     private boolean addFilter(IPath path, String JavaDoc key) {
273         IPath[] filters= (IPath[]) getAttribute(key);
274         if (filters == null)
275             return false;
276         
277         if (!JavaModelUtil.isExcludedPath(path, filters)) {
278             IPath toAdd= path.removeFirstSegments(getPath().segmentCount()).addTrailingSeparator();
279             IPath[] newFilters= new IPath[filters.length + 1];
280             System.arraycopy(filters, 0, newFilters, 0, filters.length);
281             newFilters[filters.length]= toAdd;
282             setAttribute(key, newFilters);
283             return true;
284         }
285         return false;
286     }
287     
288     private boolean removeFilter(IPath path, String JavaDoc key) {
289         IPath[] filters= (IPath[]) getAttribute(key);
290         if (filters == null)
291             return false;
292         
293         IPath toRemove= path.removeFirstSegments(getPath().segmentCount()).addTrailingSeparator();
294         if (JavaModelUtil.isExcludedPath(toRemove, filters)) {
295             List JavaDoc l= new ArrayList JavaDoc(Arrays.asList(filters));
296             l.remove(toRemove);
297             IPath[] newFilters= (IPath[])l.toArray(new IPath[l.size()]);
298             setAttribute(key, newFilters);
299             return true;
300         }
301         return false;
302     }
303     
304     public CPListElementAttribute findAttributeElement(String JavaDoc key) {
305         for (int i= 0; i < fChildren.size(); i++) {
306             Object JavaDoc curr= fChildren.get(i);
307             if (curr instanceof CPListElementAttribute) {
308                 CPListElementAttribute elem= (CPListElementAttribute) curr;
309                 if (key.equals(elem.getKey())) {
310                     return elem;
311                 }
312             }
313         }
314         return null;
315     }
316     
317     
318     public Object JavaDoc getAttribute(String JavaDoc key) {
319         CPListElementAttribute attrib= findAttributeElement(key);
320         if (attrib != null) {
321             return attrib.getValue();
322         }
323         return null;
324     }
325     
326     public CPListElementAttribute[] getAllAttributes() {
327         ArrayList JavaDoc res= new ArrayList JavaDoc();
328         for (int i= 0; i < fChildren.size(); i++) {
329             Object JavaDoc curr= fChildren.get(i);
330             if (curr instanceof CPListElementAttribute) {
331                 res.add(curr);
332             }
333         }
334         return (CPListElementAttribute[]) res.toArray(new CPListElementAttribute[res.size()]);
335     }
336     
337     
338     private void createAttributeElement(String JavaDoc key, Object JavaDoc value, boolean builtIn) {
339         fChildren.add(new CPListElementAttribute(this, key, value, builtIn));
340     }
341     
342     private static boolean isFiltered(Object JavaDoc entry, String JavaDoc[] filteredKeys) {
343         if (entry instanceof CPListElementAttribute) {
344             CPListElementAttribute curr= (CPListElementAttribute) entry;
345             String JavaDoc key= curr.getKey();
346             for (int i= 0; i < filteredKeys.length; i++) {
347                 if (key.equals(filteredKeys[i])) {
348                     return true;
349                 }
350             }
351             if (curr.isNotSupported()) {
352                 return true;
353             }
354             if (!curr.isBuiltIn() && !key.equals(CPListElement.JAVADOC) && !key.equals(CPListElement.NATIVE_LIB_PATH)) {
355                 return !JavaPlugin.getDefault().getClasspathAttributeConfigurationDescriptors().containsKey(key);
356             }
357         }
358         return false;
359     }
360     
361     private Object JavaDoc[] getFilteredChildren(String JavaDoc[] filteredKeys) {
362         int nChildren= fChildren.size();
363         ArrayList JavaDoc res= new ArrayList JavaDoc(nChildren);
364         
365         for (int i= 0; i < nChildren; i++) {
366             Object JavaDoc curr= fChildren.get(i);
367             if (!isFiltered(curr, filteredKeys)) {
368                 res.add(curr);
369             }
370         }
371         return res.toArray();
372     }
373         
374     public Object JavaDoc[] getChildren(boolean hideOutputFolder) {
375         if (hideOutputFolder && fEntryKind == IClasspathEntry.CPE_SOURCE) {
376             return getFilteredChildren(new String JavaDoc[] { OUTPUT });
377         }
378         /*if (isInContainer(JavaRuntime.JRE_CONTAINER)) {
379             return getFilteredChildren(new String[] { COMBINE_ACCESSRULES, NATIVE_LIB_PATH });
380         }*/

381         if (fEntryKind == IClasspathEntry.CPE_PROJECT) {
382             return getFilteredChildren(new String JavaDoc[] { COMBINE_ACCESSRULES });
383         }
384         return getFilteredChildren(new String JavaDoc[0]);
385     }
386         
387     public Object JavaDoc getParentContainer() {
388         return fParentContainer;
389     }
390     
391     protected void attributeChanged(String JavaDoc key) {
392         fCachedEntry= null;
393     }
394         
395     private IStatus evaluateContainerChildStatus(CPListElementAttribute attrib) {
396         if (fProject != null) {
397             ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(fPath.segment(0));
398             if (initializer != null && initializer.canUpdateClasspathContainer(fPath, fProject)) {
399                 if (attrib.isBuiltIn()) {
400                     if (CPListElement.SOURCEATTACHMENT.equals(attrib.getKey())) {
401                         return initializer.getSourceAttachmentStatus(fPath, fProject);
402                     } else if (CPListElement.ACCESSRULES.equals(attrib.getKey())) {
403                         return initializer.getAccessRulesStatus(fPath, fProject);
404                     }
405                 } else {
406                     return initializer.getAttributeStatus(fPath, fProject, attrib.getKey());
407                 }
408             }
409             return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY, "", null); //$NON-NLS-1$
410
}
411         return null;
412     }
413         
414     public IStatus getContainerChildStatus(CPListElementAttribute attrib) {
415         if (fParentContainer instanceof CPListElement) {
416             CPListElement parent= (CPListElement) fParentContainer;
417             if (parent.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
418                 return parent.evaluateContainerChildStatus(attrib);
419             }
420             return ((CPListElement) fParentContainer).getContainerChildStatus(attrib);
421         }
422         return null;
423     }
424     
425     public boolean isInContainer(String JavaDoc containerName) {
426         if (fParentContainer instanceof CPListElement) {
427             CPListElement elem= (CPListElement) fParentContainer;
428             return new Path(containerName).isPrefixOf(elem.getPath());
429         }
430         return false;
431     }
432     
433     public boolean isDeprecated() {
434         if (fEntryKind != IClasspathEntry.CPE_VARIABLE) {
435             return false;
436         }
437         if (fPath.segmentCount() > 0) {
438             return JavaCore.getClasspathVariableDeprecationMessage(fPath.segment(0)) != null;
439         }
440         return false;
441     }
442     
443     public String JavaDoc getDeprecationMessage() {
444         if (fEntryKind != IClasspathEntry.CPE_VARIABLE) {
445             return null;
446         }
447         if (fPath.segmentCount() > 0) {
448             String JavaDoc varName= fPath.segment(0);
449             return BuildPathSupport.getDeprecationMessage(varName);
450         }
451         return null;
452     }
453     
454     /*
455      * @see Object#equals(java.lang.Object)
456      */

457     public boolean equals(Object JavaDoc other) {
458         if (other != null && other.getClass().equals(getClass())) {
459             CPListElement elem= (CPListElement) other;
460             return getClasspathEntry().equals(elem.getClasspathEntry());
461         }
462         return false;
463     }
464         
465     /*
466      * @see Object#hashCode()
467      */

468     public int hashCode() {
469         return fPath.hashCode() + fEntryKind;
470     }
471     
472     /* (non-Javadoc)
473      * @see java.lang.Object#toString()
474      */

475     public String JavaDoc toString() {
476         return getClasspathEntry().toString();
477     }
478
479     /**
480      * Returns if a entry is missing.
481      * @return Returns a boolean
482      */

483     public boolean isMissing() {
484         return fIsMissing;
485     }
486
487     /**
488      * Sets the 'missing' state of the entry.
489      * @param isMissing the new state
490      */

491     public void setIsMissing(boolean isMissing) {
492         fIsMissing= isMissing;
493     }
494
495     /**
496      * Returns if a entry is exported (only applies to libraries)
497      * @return Returns a boolean
498      */

499     public boolean isExported() {
500         return fIsExported;
501     }
502
503     /**
504      * Sets the export state of the entry.
505      * @param isExported the new state
506      */

507     public void setExported(boolean isExported) {
508         if (isExported != fIsExported) {
509             fIsExported = isExported;
510             
511             attributeChanged(null);
512         }
513     }
514
515     /**
516      * Gets the project.
517      * @return Returns a IJavaProject
518      */

519     public IJavaProject getJavaProject() {
520         return fProject;
521     }
522     
523     public static CPListElement createFromExisting(IClasspathEntry curr, IJavaProject project) {
524         return createFromExisting(null, curr, project);
525     }
526         
527     public static CPListElement createFromExisting(Object JavaDoc parent, IClasspathEntry curr, IJavaProject project) {
528         IPath path= curr.getPath();
529         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
530
531         // get the resource
532
IResource res= null;
533         boolean isMissing= false;
534         IPath linkTarget= null;
535         
536         switch (curr.getEntryKind()) {
537             case IClasspathEntry.CPE_CONTAINER:
538                 try {
539                     isMissing= project != null && (JavaCore.getClasspathContainer(path, project) == null);
540                 } catch (JavaModelException e) {
541                     isMissing= true;
542                 }
543                 break;
544             case IClasspathEntry.CPE_VARIABLE:
545                 IPath resolvedPath= JavaCore.getResolvedVariablePath(path);
546                 isMissing= root.findMember(resolvedPath) == null && !resolvedPath.toFile().isFile();
547                 break;
548             case IClasspathEntry.CPE_LIBRARY:
549                 res= root.findMember(path);
550                 if (res == null) {
551                     if (!ArchiveFileFilter.isArchivePath(path)) {
552                         if (root.getWorkspace().validatePath(path.toString(), IResource.FOLDER).isOK()
553                                 && root.getProject(path.segment(0)).exists()) {
554                             res= root.getFolder(path);
555                         }
556                     }
557                     isMissing= !path.toFile().isFile(); // look for external JARs
558
} else if (res.isLinked()) {
559                     linkTarget= res.getLocation();
560                 }
561                 break;
562             case IClasspathEntry.CPE_SOURCE:
563                 path= path.removeTrailingSeparator();
564                 res= root.findMember(path);
565                 if (res == null) {
566                     if (root.getWorkspace().validatePath(path.toString(), IResource.FOLDER).isOK()) {
567                         res= root.getFolder(path);
568                     }
569                     isMissing= true;
570                 } else if (res.isLinked()) {
571                     linkTarget= res.getLocation();
572                 }
573                 break;
574             case IClasspathEntry.CPE_PROJECT:
575                 res= root.findMember(path);
576                 isMissing= (res == null);
577                 break;
578         }
579         CPListElement elem= new CPListElement(parent, project, curr.getEntryKind(), path, res, linkTarget);
580         elem.setExported(curr.isExported());
581         elem.setAttribute(SOURCEATTACHMENT, curr.getSourceAttachmentPath());
582         elem.setAttribute(OUTPUT, curr.getOutputLocation());
583         elem.setAttribute(EXCLUSION, curr.getExclusionPatterns());
584         elem.setAttribute(INCLUSION, curr.getInclusionPatterns());
585         elem.setAttribute(ACCESSRULES, curr.getAccessRules());
586         elem.setAttribute(COMBINE_ACCESSRULES, new Boolean JavaDoc(curr.combineAccessRules()));
587         
588         IClasspathAttribute[] extraAttributes= curr.getExtraAttributes();
589         for (int i= 0; i < extraAttributes.length; i++) {
590             IClasspathAttribute attrib= extraAttributes[i];
591             CPListElementAttribute attribElem= elem.findAttributeElement(attrib.getName());
592             if (attribElem == null) {
593                 elem.createAttributeElement(attrib.getName(), attrib.getValue(), false);
594             } else {
595                 attribElem.setValue(attrib.getValue());
596             }
597         }
598         
599         if (project != null && project.exists()) {
600             elem.setIsMissing(isMissing);
601         }
602         return elem;
603     }
604
605     public static StringBuffer JavaDoc appendEncodePath(IPath path, StringBuffer JavaDoc buf) {
606         if (path != null) {
607             String JavaDoc str= path.toString();
608             buf.append('[').append(str.length()).append(']').append(str);
609         } else {
610             buf.append('[').append(']');
611         }
612         return buf;
613     }
614     
615     public static StringBuffer JavaDoc appendEncodedString(String JavaDoc str, StringBuffer JavaDoc buf) {
616         if (str != null) {
617             buf.append('[').append(str.length()).append(']').append(str);
618         } else {
619             buf.append('[').append(']');
620         }
621         return buf;
622     }
623     
624     public static StringBuffer JavaDoc appendEncodedFilter(IPath[] filters, StringBuffer JavaDoc buf) {
625         if (filters != null) {
626             buf.append('[').append(filters.length).append(']');
627             for (int i= 0; i < filters.length; i++) {
628                 appendEncodePath(filters[i], buf).append(';');
629             }
630         } else {
631             buf.append('[').append(']');
632         }
633         return buf;
634     }
635     
636     public static StringBuffer JavaDoc appendEncodedAccessRules(IAccessRule[] rules, StringBuffer JavaDoc buf) {
637         if (rules != null) {
638             buf.append('[').append(rules.length).append(']');
639             for (int i= 0; i < rules.length; i++) {
640                 appendEncodePath(rules[i].getPattern(), buf).append(';');
641                 buf.append(rules[i].getKind()).append(';');
642             }
643         } else {
644             buf.append('[').append(']');
645         }
646         return buf;
647     }
648     
649
650     public StringBuffer JavaDoc appendEncodedSettings(StringBuffer JavaDoc buf) {
651         buf.append(fEntryKind).append(';');
652         if (getLinkTarget() == null) {
653             appendEncodePath(fPath, buf).append(';');
654         } else {
655             appendEncodePath(fPath, buf).append('-').append('>');
656             appendEncodePath(getLinkTarget(), buf).append(';');
657         }
658         buf.append(Boolean.valueOf(fIsExported)).append(';');
659         for (int i= 0; i < fChildren.size(); i++) {
660             Object JavaDoc curr= fChildren.get(i);
661             if (curr instanceof CPListElementAttribute) {
662                 CPListElementAttribute elem= (CPListElementAttribute) curr;
663                 if (elem.isBuiltIn()) {
664                     String JavaDoc key= elem.getKey();
665                     if (OUTPUT.equals(key) || SOURCEATTACHMENT.equals(key)) {
666                         appendEncodePath((IPath) elem.getValue(), buf).append(';');
667                     } else if (EXCLUSION.equals(key) || INCLUSION.equals(key)) {
668                         appendEncodedFilter((IPath[]) elem.getValue(), buf).append(';');
669                     } else if (ACCESSRULES.equals(key)) {
670                         appendEncodedAccessRules((IAccessRule[]) elem.getValue(), buf).append(';');
671                     } else if (COMBINE_ACCESSRULES.equals(key)) {
672                         buf.append(((Boolean JavaDoc) elem.getValue()).booleanValue()).append(';');
673                     }
674                 } else {
675                     appendEncodedString((String JavaDoc) elem.getValue(), buf);
676                 }
677             }
678         }
679         return buf;
680     }
681
682     public IPath getLinkTarget() {
683         return fLinkTarget;
684     }
685
686     public void setPath(IPath path) {
687         fCachedEntry= null;
688         fPath= path;
689     }
690     
691     public void setLinkTarget(IPath linkTarget) {
692         fCachedEntry= null;
693         fLinkTarget= linkTarget;
694     }
695
696     public static void insert(CPListElement element, List JavaDoc cpList) {
697         int length= cpList.size();
698         CPListElement[] elements= (CPListElement[])cpList.toArray(new CPListElement[length]);
699         int i= 0;
700         while (i < length && elements[i].getEntryKind() != element.getEntryKind()) {
701             i++;
702         }
703         if (i < length) {
704             i++;
705             while (i < length && elements[i].getEntryKind() == element.getEntryKind()) {
706                 i++;
707             }
708             cpList.add(i, element);
709             return;
710         }
711         
712         switch (element.getEntryKind()) {
713         case IClasspathEntry.CPE_SOURCE:
714             cpList.add(0, element);
715             break;
716         case IClasspathEntry.CPE_CONTAINER:
717         case IClasspathEntry.CPE_LIBRARY:
718         case IClasspathEntry.CPE_PROJECT:
719         case IClasspathEntry.CPE_VARIABLE:
720         default:
721             cpList.add(element);
722             break;
723         }
724     }
725
726     public static IClasspathEntry[] convertToClasspathEntries(List JavaDoc/*<CPListElement>*/ cpList) {
727         IClasspathEntry[] result= new IClasspathEntry[cpList.size()];
728         int i= 0;
729         for (Iterator JavaDoc iter= cpList.iterator(); iter.hasNext();) {
730             CPListElement cur= (CPListElement)iter.next();
731             result[i]= cur.getClasspathEntry();
732             i++;
733         }
734         return result;
735     }
736     
737     public static CPListElement[] createFromExisting(IJavaProject project) throws JavaModelException {
738         IClasspathEntry[] rawClasspath= project.getRawClasspath();
739         CPListElement[] result= new CPListElement[rawClasspath.length];
740         for (int i= 0; i < rawClasspath.length; i++) {
741             result[i]= CPListElement.createFromExisting(rawClasspath[i], project);
742         }
743         return result;
744     }
745     
746     public static boolean isProjectSourceFolder(CPListElement[] existing, IJavaProject project) {
747         IPath projPath= project.getProject().getFullPath();
748         for (int i= 0; i < existing.length; i++) {
749             IClasspathEntry curr= existing[i].getClasspathEntry();
750             if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
751                 if (projPath.equals(curr.getPath())) {
752                     return true;
753                 }
754             }
755         }
756         return false;
757     }
758
759     public IPath getOrginalPath() {
760         return fOrginalPath;
761     }
762
763     public IPath getOrginalLinkTarget() {
764         return fOrginalLinkTarget;
765     }
766
767
768     public CPListElement copy() {
769         CPListElement result= new CPListElement();
770         result.fProject= fProject;
771         result.fEntryKind= fEntryKind;
772         result.fPath= fPath;
773         result.fOrginalPath= fOrginalPath;
774         result.fResource= fResource;
775         result.fIsExported= fIsExported;
776         result.fIsMissing= fIsMissing;
777         result.fParentContainer= fParentContainer;
778         result.fCachedEntry= null;
779         result.fChildren= new ArrayList JavaDoc(fChildren.size());
780         for (Iterator JavaDoc iterator= fChildren.iterator(); iterator.hasNext();) {
781             Object JavaDoc child= iterator.next();
782             if (child instanceof CPListElement) {
783                 result.fChildren.add(((CPListElement)child).copy());
784             } else {
785                 result.fChildren.add(((CPListElementAttribute)child).copy());
786             }
787         }
788         result.fLinkTarget= fLinkTarget;
789         result.fOrginalLinkTarget= fOrginalLinkTarget;
790         return result;
791     }
792     
793     public void setAttributesFromExisting(CPListElement existing) {
794         Assert.isTrue(existing.getEntryKind() == getEntryKind());
795         CPListElementAttribute[] attributes= existing.getAllAttributes();
796         for (int i= 0; i < attributes.length; i++) {
797             CPListElementAttribute curr= attributes[i];
798             CPListElementAttribute elem= findAttributeElement(curr.getKey());
799             if (elem == null) {
800                 createAttributeElement(curr.getKey(), curr.getValue(), false);
801             } else {
802                 elem.setValue(curr.getValue());
803             }
804         }
805     }
806
807 }
808
Popular Tags