PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.ui.source_3.7.1.r371_v20110824-0800/org/eclipse/jdt/internal/ui/wizards/buildpaths/CPListElement.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 862 lines | 682 code | 102 blank | 78 comment | 165 complexity | 96a79c6e2e636f449b105d13a6426db1 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2011 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. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import org.eclipse.core.runtime.Assert;
  17. import org.eclipse.core.runtime.IPath;
  18. import org.eclipse.core.runtime.IStatus;
  19. import org.eclipse.core.runtime.Path;
  20. import org.eclipse.core.runtime.Status;
  21. import org.eclipse.core.resources.IResource;
  22. import org.eclipse.core.resources.IWorkspaceRoot;
  23. import org.eclipse.core.resources.ResourcesPlugin;
  24. import org.eclipse.jdt.core.ClasspathContainerInitializer;
  25. import org.eclipse.jdt.core.IAccessRule;
  26. import org.eclipse.jdt.core.IClasspathAttribute;
  27. import org.eclipse.jdt.core.IClasspathContainer;
  28. import org.eclipse.jdt.core.IClasspathEntry;
  29. import org.eclipse.jdt.core.IJavaProject;
  30. import org.eclipse.jdt.core.IPackageFragmentRoot;
  31. import org.eclipse.jdt.core.JavaCore;
  32. import org.eclipse.jdt.core.JavaModelException;
  33. import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
  34. import org.eclipse.jdt.launching.JavaRuntime;
  35. import org.eclipse.jdt.ui.JavaUI;
  36. import org.eclipse.jdt.internal.ui.JavaPlugin;
  37. public class CPListElement {
  38. public static final String SOURCEATTACHMENT= "sourcepath"; //$NON-NLS-1$
  39. public static final String OUTPUT= "output"; //$NON-NLS-1$
  40. public static final String EXCLUSION= "exclusion"; //$NON-NLS-1$
  41. public static final String INCLUSION= "inclusion"; //$NON-NLS-1$
  42. public static final String ACCESSRULES= "accessrules"; //$NON-NLS-1$
  43. public static final String COMBINE_ACCESSRULES= "combineaccessrules"; //$NON-NLS-1$
  44. public static final String JAVADOC= IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME;
  45. public static final String NATIVE_LIB_PATH= JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY;
  46. private IJavaProject fProject;
  47. private int fEntryKind;
  48. private IPath fPath, fOrginalPath;
  49. private IResource fResource;
  50. private boolean fIsExported;
  51. private boolean fIsMissing;
  52. private Object fParentContainer;
  53. private IClasspathEntry fCachedEntry;
  54. /**
  55. * List of {@link CPListElement} and {@link CPListElementAttribute}.
  56. */
  57. private ArrayList<Object> fChildren;
  58. private IPath fLinkTarget, fOrginalLinkTarget;
  59. private CPListElement() {}
  60. public CPListElement(IJavaProject project, int entryKind, IPath path, IResource res) {
  61. this(null, project, entryKind, path, res);
  62. }
  63. public CPListElement(Object parent, IJavaProject project, int entryKind, IPath path, IResource res) {
  64. this(parent, project, entryKind, path, res, null);
  65. }
  66. public CPListElement(IJavaProject project, int entryKind) {
  67. this(null, project, entryKind, null, null);
  68. }
  69. public CPListElement(Object parent, IJavaProject project, int entryKind, IPath path, IResource res, IPath linkTarget) {
  70. this(parent, project, entryKind, path, false, res, linkTarget);
  71. }
  72. public CPListElement(Object parent, IJavaProject project, int entryKind, IPath path, boolean newElement, IResource res, IPath linkTarget) {
  73. fProject= project;
  74. fEntryKind= entryKind;
  75. fPath= path;
  76. fOrginalPath= newElement ? null : path;
  77. fLinkTarget= linkTarget;
  78. fOrginalLinkTarget= linkTarget;
  79. fChildren= new ArrayList<Object>();
  80. fResource= res;
  81. fIsExported= false;
  82. fIsMissing= false;
  83. fCachedEntry= null;
  84. fParentContainer= parent;
  85. switch (entryKind) {
  86. case IClasspathEntry.CPE_SOURCE:
  87. createAttributeElement(OUTPUT, null, true);
  88. createAttributeElement(INCLUSION, new Path[0], true);
  89. createAttributeElement(EXCLUSION, new Path[0], true);
  90. createAttributeElement(NATIVE_LIB_PATH, null, false);
  91. break;
  92. case IClasspathEntry.CPE_LIBRARY:
  93. case IClasspathEntry.CPE_VARIABLE:
  94. createAttributeElement(SOURCEATTACHMENT, null, true);
  95. createAttributeElement(JAVADOC, null, false);
  96. createAttributeElement(NATIVE_LIB_PATH, null, false);
  97. createAttributeElement(ACCESSRULES, new IAccessRule[0], true);
  98. break;
  99. case IClasspathEntry.CPE_PROJECT:
  100. createAttributeElement(ACCESSRULES, new IAccessRule[0], true);
  101. createAttributeElement(COMBINE_ACCESSRULES, Boolean.FALSE, true); // not rendered
  102. createAttributeElement(NATIVE_LIB_PATH, null, false);
  103. break;
  104. case IClasspathEntry.CPE_CONTAINER:
  105. createAttributeElement(ACCESSRULES, new IAccessRule[0], true);
  106. try {
  107. IClasspathContainer container= JavaCore.getClasspathContainer(fPath, fProject);
  108. if (container != null) {
  109. IClasspathEntry[] entries= container.getClasspathEntries();
  110. if (entries != null) { // catch invalid container implementation
  111. for (int i= 0; i < entries.length; i++) {
  112. IClasspathEntry entry= entries[i];
  113. if (entry != null) {
  114. CPListElement curr= createFromExisting(this, entry, fProject);
  115. fChildren.add(curr);
  116. } else {
  117. JavaPlugin.logErrorMessage("Null entry in container '" + fPath + "'"); //$NON-NLS-1$//$NON-NLS-2$
  118. }
  119. }
  120. } else {
  121. JavaPlugin.logErrorMessage("container returns null as entries: '" + fPath + "'"); //$NON-NLS-1$//$NON-NLS-2$
  122. }
  123. }
  124. } catch (JavaModelException e) {
  125. }
  126. createAttributeElement(NATIVE_LIB_PATH, null, false);
  127. break;
  128. default:
  129. }
  130. }
  131. public IClasspathEntry getClasspathEntry() {
  132. if (fCachedEntry == null) {
  133. fCachedEntry= newClasspathEntry();
  134. }
  135. return fCachedEntry;
  136. }
  137. private IClasspathAttribute[] getClasspathAttributes() {
  138. ArrayList<IClasspathAttribute> res= new ArrayList<IClasspathAttribute>();
  139. for (int i= 0; i < fChildren.size(); i++) {
  140. Object curr= fChildren.get(i);
  141. if (curr instanceof CPListElementAttribute) {
  142. CPListElementAttribute elem= (CPListElementAttribute) curr;
  143. if (!elem.isBuiltIn() && elem.getValue() != null) {
  144. res.add(elem.getClasspathAttribute());
  145. }
  146. }
  147. }
  148. return res.toArray(new IClasspathAttribute[res.size()]);
  149. }
  150. private IClasspathEntry newClasspathEntry() {
  151. IClasspathAttribute[] extraAttributes= getClasspathAttributes();
  152. switch (fEntryKind) {
  153. case IClasspathEntry.CPE_SOURCE:
  154. IPath[] inclusionPattern= (IPath[]) getAttribute(INCLUSION);
  155. IPath[] exclusionPattern= (IPath[]) getAttribute(EXCLUSION);
  156. IPath outputLocation= (IPath) getAttribute(OUTPUT);
  157. return JavaCore.newSourceEntry(fPath, inclusionPattern, exclusionPattern, outputLocation, extraAttributes);
  158. case IClasspathEntry.CPE_LIBRARY: {
  159. IPath attach= (IPath) getAttribute(SOURCEATTACHMENT);
  160. IAccessRule[] accesRules= (IAccessRule[]) getAttribute(ACCESSRULES);
  161. return JavaCore.newLibraryEntry(fPath, attach, null, accesRules, extraAttributes, isExported());
  162. }
  163. case IClasspathEntry.CPE_PROJECT: {
  164. IAccessRule[] accesRules= (IAccessRule[]) getAttribute(ACCESSRULES);
  165. boolean combineAccessRules= ((Boolean) getAttribute(COMBINE_ACCESSRULES)).booleanValue();
  166. return JavaCore.newProjectEntry(fPath, accesRules, combineAccessRules, extraAttributes, isExported());
  167. }
  168. case IClasspathEntry.CPE_CONTAINER: {
  169. IAccessRule[] accesRules= (IAccessRule[]) getAttribute(ACCESSRULES);
  170. return JavaCore.newContainerEntry(fPath, accesRules, extraAttributes, isExported());
  171. }
  172. case IClasspathEntry.CPE_VARIABLE: {
  173. IPath varAttach= (IPath) getAttribute(SOURCEATTACHMENT);
  174. IAccessRule[] accesRules= (IAccessRule[]) getAttribute(ACCESSRULES);
  175. return JavaCore.newVariableEntry(fPath, varAttach, null, accesRules, extraAttributes, isExported());
  176. }
  177. default:
  178. return null;
  179. }
  180. }
  181. /**
  182. * Gets the class path entry path.
  183. * @return returns the path
  184. * @see IClasspathEntry#getPath()
  185. */
  186. public IPath getPath() {
  187. return fPath;
  188. }
  189. /**
  190. * Gets the class path entry kind.
  191. * @return the entry kind
  192. * @see IClasspathEntry#getEntryKind()
  193. */
  194. public int getEntryKind() {
  195. return fEntryKind;
  196. }
  197. /**
  198. * Entries without resource are either non existing or a variable entry
  199. * External jars do not have a resource
  200. * @return returns the resource
  201. */
  202. public IResource getResource() {
  203. return fResource;
  204. }
  205. public CPListElementAttribute setAttribute(String key, Object value) {
  206. CPListElementAttribute attribute= findAttributeElement(key);
  207. if (attribute == null) {
  208. return null;
  209. }
  210. if (key.equals(EXCLUSION) || key.equals(INCLUSION)) {
  211. Assert.isTrue(value != null || fEntryKind != IClasspathEntry.CPE_SOURCE);
  212. }
  213. if (key.equals(ACCESSRULES)) {
  214. Assert.isTrue(value != null || fEntryKind == IClasspathEntry.CPE_SOURCE);
  215. }
  216. if (key.equals(COMBINE_ACCESSRULES)) {
  217. Assert.isTrue(value instanceof Boolean);
  218. }
  219. attribute.setValue(value);
  220. return attribute;
  221. }
  222. public boolean addToExclusions(IPath path) {
  223. String key= CPListElement.EXCLUSION;
  224. return addFilter(path, key);
  225. }
  226. public boolean addToInclusion(IPath path) {
  227. String key= CPListElement.INCLUSION;
  228. return addFilter(path, key);
  229. }
  230. public boolean removeFromExclusions(IPath path) {
  231. String key= CPListElement.EXCLUSION;
  232. return removeFilter(path, key);
  233. }
  234. public boolean removeFromInclusion(IPath path) {
  235. String key= CPListElement.INCLUSION;
  236. return removeFilter(path, key);
  237. }
  238. private boolean addFilter(IPath path, String key) {
  239. IPath[] filters= (IPath[]) getAttribute(key);
  240. if (filters == null)
  241. return false;
  242. if (!JavaModelUtil.isExcludedPath(path, filters)) {
  243. IPath toAdd= path.removeFirstSegments(getPath().segmentCount()).addTrailingSeparator();
  244. IPath[] newFilters= new IPath[filters.length + 1];
  245. System.arraycopy(filters, 0, newFilters, 0, filters.length);
  246. newFilters[filters.length]= toAdd;
  247. setAttribute(key, newFilters);
  248. return true;
  249. }
  250. return false;
  251. }
  252. private boolean removeFilter(IPath path, String key) {
  253. IPath[] filters= (IPath[]) getAttribute(key);
  254. if (filters == null)
  255. return false;
  256. IPath toRemove= path.removeFirstSegments(getPath().segmentCount()).addTrailingSeparator();
  257. if (JavaModelUtil.isExcludedPath(toRemove, filters)) {
  258. List<IPath> l= new ArrayList<IPath>(Arrays.asList(filters));
  259. l.remove(toRemove);
  260. IPath[] newFilters= l.toArray(new IPath[l.size()]);
  261. setAttribute(key, newFilters);
  262. return true;
  263. }
  264. return false;
  265. }
  266. public CPListElementAttribute findAttributeElement(String key) {
  267. for (int i= 0; i < fChildren.size(); i++) {
  268. Object curr= fChildren.get(i);
  269. if (curr instanceof CPListElementAttribute) {
  270. CPListElementAttribute elem= (CPListElementAttribute) curr;
  271. if (key.equals(elem.getKey())) {
  272. return elem;
  273. }
  274. }
  275. }
  276. return null;
  277. }
  278. public Object getAttribute(String key) {
  279. CPListElementAttribute attrib= findAttributeElement(key);
  280. if (attrib != null) {
  281. return attrib.getValue();
  282. }
  283. return null;
  284. }
  285. public CPListElementAttribute[] getAllAttributes() {
  286. ArrayList<Object> res= new ArrayList<Object>();
  287. for (int i= 0; i < fChildren.size(); i++) {
  288. Object curr= fChildren.get(i);
  289. if (curr instanceof CPListElementAttribute) {
  290. res.add(curr);
  291. }
  292. }
  293. return res.toArray(new CPListElementAttribute[res.size()]);
  294. }
  295. private void createAttributeElement(String key, Object value, boolean builtIn) {
  296. fChildren.add(new CPListElementAttribute(this, key, value, builtIn));
  297. }
  298. private static boolean isFiltered(Object entry, String[] filteredKeys) {
  299. if (entry instanceof CPListElementAttribute) {
  300. CPListElementAttribute curr= (CPListElementAttribute) entry;
  301. String key= curr.getKey();
  302. for (int i= 0; i < filteredKeys.length; i++) {
  303. if (key.equals(filteredKeys[i])) {
  304. return true;
  305. }
  306. }
  307. if (curr.isNotSupported()) {
  308. return true;
  309. }
  310. if (!curr.isBuiltIn() && !key.equals(CPListElement.JAVADOC) && !key.equals(CPListElement.NATIVE_LIB_PATH)) {
  311. return !JavaPlugin.getDefault().getClasspathAttributeConfigurationDescriptors().containsKey(key);
  312. }
  313. }
  314. return false;
  315. }
  316. private Object[] getFilteredChildren(String[] filteredKeys) {
  317. int nChildren= fChildren.size();
  318. ArrayList<Object> res= new ArrayList<Object>(nChildren);
  319. for (int i= 0; i < nChildren; i++) {
  320. Object curr= fChildren.get(i);
  321. if (!isFiltered(curr, filteredKeys)) {
  322. res.add(curr);
  323. }
  324. }
  325. return res.toArray();
  326. }
  327. public Object[] getChildren(boolean hideOutputFolder) {
  328. if (hideOutputFolder && fEntryKind == IClasspathEntry.CPE_SOURCE) {
  329. return getFilteredChildren(new String[] { OUTPUT });
  330. }
  331. /*if (isInContainer(JavaRuntime.JRE_CONTAINER)) {
  332. return getFilteredChildren(new String[] { COMBINE_ACCESSRULES, NATIVE_LIB_PATH });
  333. }*/
  334. if (fEntryKind == IClasspathEntry.CPE_PROJECT) {
  335. return getFilteredChildren(new String[] { COMBINE_ACCESSRULES });
  336. }
  337. return getFilteredChildren(new String[0]);
  338. }
  339. public Object getParentContainer() {
  340. return fParentContainer;
  341. }
  342. /**
  343. * Sets the parent container.
  344. *
  345. * @param parent the parent container
  346. * @since 3.7
  347. */
  348. void setParentContainer(CPUserLibraryElement parent) {
  349. fParentContainer= parent;
  350. }
  351. /**
  352. * Notifies that an attribute has changed
  353. *
  354. * @param key the changed key
  355. */
  356. protected void attributeChanged(String key) {
  357. fCachedEntry= null;
  358. }
  359. private IStatus evaluateContainerChildStatus(CPListElementAttribute attrib) {
  360. if (fProject != null) {
  361. ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(fPath.segment(0));
  362. if (initializer != null && initializer.canUpdateClasspathContainer(fPath, fProject)) {
  363. if (attrib.isBuiltIn()) {
  364. if (CPListElement.SOURCEATTACHMENT.equals(attrib.getKey())) {
  365. return initializer.getSourceAttachmentStatus(fPath, fProject);
  366. } else if (CPListElement.ACCESSRULES.equals(attrib.getKey())) {
  367. return initializer.getAccessRulesStatus(fPath, fProject);
  368. }
  369. } else {
  370. return initializer.getAttributeStatus(fPath, fProject, attrib.getKey());
  371. }
  372. }
  373. return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY, "", null); //$NON-NLS-1$
  374. }
  375. return null;
  376. }
  377. public IStatus getContainerChildStatus(CPListElementAttribute attrib) {
  378. if (fParentContainer instanceof CPListElement) {
  379. CPListElement parent= (CPListElement) fParentContainer;
  380. if (parent.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
  381. return parent.evaluateContainerChildStatus(attrib);
  382. }
  383. return ((CPListElement) fParentContainer).getContainerChildStatus(attrib);
  384. }
  385. return null;
  386. }
  387. public boolean isInContainer(String containerName) {
  388. if (fParentContainer instanceof CPListElement) {
  389. CPListElement elem= (CPListElement) fParentContainer;
  390. return new Path(containerName).isPrefixOf(elem.getPath());
  391. }
  392. return false;
  393. }
  394. public boolean isDeprecated() {
  395. if (fEntryKind != IClasspathEntry.CPE_VARIABLE) {
  396. return false;
  397. }
  398. if (fPath.segmentCount() > 0) {
  399. return JavaCore.getClasspathVariableDeprecationMessage(fPath.segment(0)) != null;
  400. }
  401. return false;
  402. }
  403. public String getDeprecationMessage() {
  404. if (fEntryKind != IClasspathEntry.CPE_VARIABLE) {
  405. return null;
  406. }
  407. if (fPath.segmentCount() > 0) {
  408. String varName= fPath.segment(0);
  409. return BuildPathSupport.getDeprecationMessage(varName);
  410. }
  411. return null;
  412. }
  413. /*
  414. * @see Object#equals(java.lang.Object)
  415. */
  416. @Override
  417. public boolean equals(Object other) {
  418. if (other != null && other.getClass().equals(getClass())) {
  419. CPListElement elem= (CPListElement) other;
  420. return getClasspathEntry().equals(elem.getClasspathEntry());
  421. }
  422. return false;
  423. }
  424. /*
  425. * @see Object#hashCode()
  426. */
  427. @Override
  428. public int hashCode() {
  429. return fPath.hashCode() + fEntryKind;
  430. }
  431. /* (non-Javadoc)
  432. * @see java.lang.Object#toString()
  433. */
  434. @Override
  435. public String toString() {
  436. return getClasspathEntry().toString();
  437. }
  438. /**
  439. * Returns if a entry is missing.
  440. * @return Returns a boolean
  441. */
  442. public boolean isMissing() {
  443. return fIsMissing;
  444. }
  445. /**
  446. * Returns if a entry has children that are missing
  447. * @return Returns a boolean
  448. */
  449. public boolean hasMissingChildren() {
  450. for (int i= 0; i < fChildren.size(); i++) {
  451. Object curr= fChildren.get(i);
  452. if (curr instanceof CPListElement && ((CPListElement) curr).isMissing()) {
  453. return true;
  454. }
  455. }
  456. return false;
  457. }
  458. /**
  459. * Sets the 'missing' state of the entry.
  460. * @param isMissing the new state
  461. */
  462. public void setIsMissing(boolean isMissing) {
  463. fIsMissing= isMissing;
  464. }
  465. /**
  466. * Returns if a entry is exported (only applies to libraries)
  467. * @return Returns a boolean
  468. */
  469. public boolean isExported() {
  470. return fIsExported;
  471. }
  472. /**
  473. * Sets the export state of the entry.
  474. * @param isExported the new state
  475. */
  476. public void setExported(boolean isExported) {
  477. if (isExported != fIsExported) {
  478. fIsExported = isExported;
  479. attributeChanged(null);
  480. }
  481. }
  482. /**
  483. * Gets the project.
  484. * @return Returns a IJavaProject
  485. */
  486. public IJavaProject getJavaProject() {
  487. return fProject;
  488. }
  489. public static CPListElement createFromExisting(IClasspathEntry curr, IJavaProject project) {
  490. //Note: Some old clients of this method could actually mean create(curr, true, project)
  491. return create(curr, false, project);
  492. }
  493. public static CPListElement createFromExisting(Object parent, IClasspathEntry curr, IJavaProject project) {
  494. //Note: Some old clients of this method could actually mean create(parent, curr, true, project)
  495. return create(parent, curr, false, project);
  496. }
  497. public static CPListElement create(IClasspathEntry curr, boolean newElement, IJavaProject project) {
  498. return create(null, curr, newElement, project);
  499. }
  500. public static CPListElement create(Object parent, IClasspathEntry curr, boolean newElement, IJavaProject project) {
  501. IPath path= curr.getPath();
  502. IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
  503. // get the resource
  504. IResource res= null;
  505. boolean isMissing= false;
  506. IPath linkTarget= null;
  507. switch (curr.getEntryKind()) {
  508. case IClasspathEntry.CPE_CONTAINER:
  509. try {
  510. isMissing= project != null && (JavaCore.getClasspathContainer(path, project) == null);
  511. } catch (JavaModelException e) {
  512. isMissing= true;
  513. }
  514. break;
  515. case IClasspathEntry.CPE_VARIABLE:
  516. IPath resolvedPath= JavaCore.getResolvedVariablePath(path);
  517. isMissing= root.findMember(resolvedPath) == null && !resolvedPath.toFile().exists();
  518. break;
  519. case IClasspathEntry.CPE_LIBRARY:
  520. res= root.findMember(path);
  521. if (res == null) {
  522. if (!ArchiveFileFilter.isArchivePath(path, true)) {
  523. if (root.getWorkspace().validatePath(path.toString(), IResource.FOLDER).isOK()
  524. && root.getProject(path.segment(0)).exists()) {
  525. res= root.getFolder(path);
  526. }
  527. }
  528. IPath rawPath= path;
  529. if (project != null) {
  530. IPackageFragmentRoot[] roots= project.findPackageFragmentRoots(curr);
  531. if (roots.length == 1)
  532. rawPath= roots[0].getPath();
  533. }
  534. isMissing= !rawPath.toFile().exists(); // look for external JARs and folders
  535. } else if (res.isLinked()) {
  536. linkTarget= res.getLocation();
  537. }
  538. break;
  539. case IClasspathEntry.CPE_SOURCE:
  540. path= path.removeTrailingSeparator();
  541. res= root.findMember(path);
  542. if (res == null) {
  543. if (root.getWorkspace().validatePath(path.toString(), IResource.FOLDER).isOK()) {
  544. res= root.getFolder(path);
  545. }
  546. isMissing= true;
  547. } else if (res.isLinked()) {
  548. linkTarget= res.getLocation();
  549. }
  550. break;
  551. case IClasspathEntry.CPE_PROJECT:
  552. res= root.findMember(path);
  553. isMissing= (res == null);
  554. break;
  555. }
  556. CPListElement elem= new CPListElement(parent, project, curr.getEntryKind(), path, newElement, res, linkTarget);
  557. elem.setExported(curr.isExported());
  558. elem.setAttribute(SOURCEATTACHMENT, curr.getSourceAttachmentPath());
  559. elem.setAttribute(OUTPUT, curr.getOutputLocation());
  560. elem.setAttribute(EXCLUSION, curr.getExclusionPatterns());
  561. elem.setAttribute(INCLUSION, curr.getInclusionPatterns());
  562. elem.setAttribute(ACCESSRULES, curr.getAccessRules());
  563. elem.setAttribute(COMBINE_ACCESSRULES, new Boolean(curr.combineAccessRules()));
  564. IClasspathAttribute[] extraAttributes= curr.getExtraAttributes();
  565. for (int i= 0; i < extraAttributes.length; i++) {
  566. IClasspathAttribute attrib= extraAttributes[i];
  567. CPListElementAttribute attribElem= elem.findAttributeElement(attrib.getName());
  568. if (attribElem == null) {
  569. elem.createAttributeElement(attrib.getName(), attrib.getValue(), false);
  570. } else {
  571. attribElem.setValue(attrib.getValue());
  572. }
  573. }
  574. elem.setIsMissing(isMissing);
  575. return elem;
  576. }
  577. public static StringBuffer appendEncodePath(IPath path, StringBuffer buf) {
  578. if (path != null) {
  579. String str= path.toString();
  580. buf.append('[').append(str.length()).append(']').append(str);
  581. } else {
  582. buf.append('[').append(']');
  583. }
  584. return buf;
  585. }
  586. public static StringBuffer appendEncodedString(String str, StringBuffer buf) {
  587. if (str != null) {
  588. buf.append('[').append(str.length()).append(']').append(str);
  589. } else {
  590. buf.append('[').append(']');
  591. }
  592. return buf;
  593. }
  594. public static StringBuffer appendEncodedFilter(IPath[] filters, StringBuffer buf) {
  595. if (filters != null) {
  596. buf.append('[').append(filters.length).append(']');
  597. for (int i= 0; i < filters.length; i++) {
  598. appendEncodePath(filters[i], buf).append(';');
  599. }
  600. } else {
  601. buf.append('[').append(']');
  602. }
  603. return buf;
  604. }
  605. public static StringBuffer appendEncodedAccessRules(IAccessRule[] rules, StringBuffer buf) {
  606. if (rules != null) {
  607. buf.append('[').append(rules.length).append(']');
  608. for (int i= 0; i < rules.length; i++) {
  609. appendEncodePath(rules[i].getPattern(), buf).append(';');
  610. buf.append(rules[i].getKind()).append(';');
  611. }
  612. } else {
  613. buf.append('[').append(']');
  614. }
  615. return buf;
  616. }
  617. public StringBuffer appendEncodedSettings(StringBuffer buf) {
  618. buf.append(fEntryKind).append(';');
  619. if (getLinkTarget() == null) {
  620. appendEncodePath(fPath, buf).append(';');
  621. } else {
  622. appendEncodePath(fPath, buf).append('-').append('>');
  623. appendEncodePath(getLinkTarget(), buf).append(';');
  624. }
  625. buf.append(Boolean.valueOf(fIsExported)).append(';');
  626. for (int i= 0; i < fChildren.size(); i++) {
  627. Object curr= fChildren.get(i);
  628. if (curr instanceof CPListElementAttribute) {
  629. CPListElementAttribute elem= (CPListElementAttribute) curr;
  630. if (elem.isBuiltIn()) {
  631. String key= elem.getKey();
  632. if (OUTPUT.equals(key) || SOURCEATTACHMENT.equals(key)) {
  633. appendEncodePath((IPath) elem.getValue(), buf).append(';');
  634. } else if (EXCLUSION.equals(key) || INCLUSION.equals(key)) {
  635. appendEncodedFilter((IPath[]) elem.getValue(), buf).append(';');
  636. } else if (ACCESSRULES.equals(key)) {
  637. appendEncodedAccessRules((IAccessRule[]) elem.getValue(), buf).append(';');
  638. } else if (COMBINE_ACCESSRULES.equals(key)) {
  639. buf.append(((Boolean) elem.getValue()).booleanValue()).append(';');
  640. }
  641. } else {
  642. appendEncodedString((String) elem.getValue(), buf);
  643. }
  644. }
  645. }
  646. return buf;
  647. }
  648. public IPath getLinkTarget() {
  649. return fLinkTarget;
  650. }
  651. public void setPath(IPath path) {
  652. fCachedEntry= null;
  653. fPath= path;
  654. }
  655. public void setLinkTarget(IPath linkTarget) {
  656. fCachedEntry= null;
  657. fLinkTarget= linkTarget;
  658. }
  659. public static void insert(CPListElement element, List<CPListElement> cpList) {
  660. int length= cpList.size();
  661. CPListElement[] elements= cpList.toArray(new CPListElement[length]);
  662. int i= 0;
  663. while (i < length && elements[i].getEntryKind() != element.getEntryKind()) {
  664. i++;
  665. }
  666. if (i < length) {
  667. i++;
  668. while (i < length && elements[i].getEntryKind() == element.getEntryKind()) {
  669. i++;
  670. }
  671. cpList.add(i, element);
  672. return;
  673. }
  674. switch (element.getEntryKind()) {
  675. case IClasspathEntry.CPE_SOURCE:
  676. cpList.add(0, element);
  677. break;
  678. case IClasspathEntry.CPE_CONTAINER:
  679. case IClasspathEntry.CPE_LIBRARY:
  680. case IClasspathEntry.CPE_PROJECT:
  681. case IClasspathEntry.CPE_VARIABLE:
  682. default:
  683. cpList.add(element);
  684. break;
  685. }
  686. }
  687. public static IClasspathEntry[] convertToClasspathEntries(List<CPListElement> cpList) {
  688. IClasspathEntry[] result= new IClasspathEntry[cpList.size()];
  689. int i= 0;
  690. for (Iterator<CPListElement> iter= cpList.iterator(); iter.hasNext();) {
  691. CPListElement cur= iter.next();
  692. result[i]= cur.getClasspathEntry();
  693. i++;
  694. }
  695. return result;
  696. }
  697. public static CPListElement[] createFromExisting(IJavaProject project) throws JavaModelException {
  698. IClasspathEntry[] rawClasspath= project.getRawClasspath();
  699. CPListElement[] result= new CPListElement[rawClasspath.length];
  700. for (int i= 0; i < rawClasspath.length; i++) {
  701. result[i]= CPListElement.createFromExisting(rawClasspath[i], project);
  702. }
  703. return result;
  704. }
  705. public static boolean isProjectSourceFolder(CPListElement[] existing, IJavaProject project) {
  706. IPath projPath= project.getProject().getFullPath();
  707. for (int i= 0; i < existing.length; i++) {
  708. IClasspathEntry curr= existing[i].getClasspathEntry();
  709. if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
  710. if (projPath.equals(curr.getPath())) {
  711. return true;
  712. }
  713. }
  714. }
  715. return false;
  716. }
  717. public IPath getOrginalPath() {
  718. return fOrginalPath;
  719. }
  720. public IPath getOrginalLinkTarget() {
  721. return fOrginalLinkTarget;
  722. }
  723. public CPListElement copy() {
  724. CPListElement result= new CPListElement();
  725. result.fProject= fProject;
  726. result.fEntryKind= fEntryKind;
  727. result.fPath= fPath;
  728. result.fOrginalPath= fOrginalPath;
  729. result.fResource= fResource;
  730. result.fIsExported= fIsExported;
  731. result.fIsMissing= fIsMissing;
  732. result.fParentContainer= fParentContainer;
  733. result.fCachedEntry= null;
  734. result.fChildren= new ArrayList<Object>(fChildren.size());
  735. for (Iterator<Object> iterator= fChildren.iterator(); iterator.hasNext();) {
  736. Object child= iterator.next();
  737. if (child instanceof CPListElement) {
  738. result.fChildren.add(((CPListElement)child).copy());
  739. } else {
  740. result.fChildren.add(((CPListElementAttribute)child).copy());
  741. }
  742. }
  743. result.fLinkTarget= fLinkTarget;
  744. result.fOrginalLinkTarget= fOrginalLinkTarget;
  745. return result;
  746. }
  747. public void setAttributesFromExisting(CPListElement existing) {
  748. Assert.isTrue(existing.getEntryKind() == getEntryKind());
  749. CPListElementAttribute[] attributes= existing.getAllAttributes();
  750. for (int i= 0; i < attributes.length; i++) {
  751. CPListElementAttribute curr= attributes[i];
  752. CPListElementAttribute elem= findAttributeElement(curr.getKey());
  753. if (elem == null) {
  754. createAttributeElement(curr.getKey(), curr.getValue(), false);
  755. } else {
  756. elem.setValue(curr.getValue());
  757. }
  758. }
  759. }
  760. }