KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > launching > sourcelookup > containers > PackageFragmentRootSourceContainer


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.launching.sourcelookup.containers;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
16 import org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainer;
17 import org.eclipse.jdt.core.IClassFile;
18 import org.eclipse.jdt.core.ICompilationUnit;
19 import org.eclipse.jdt.core.IPackageFragment;
20 import org.eclipse.jdt.core.IPackageFragmentRoot;
21 import org.eclipse.jdt.core.JavaCore;
22 import org.eclipse.jdt.internal.launching.LaunchingPlugin;
23
24 /**
25  * Package fragment root source container. Represents an archive
26  * or folder in the Java model containing class files, with a possible
27  * source attachment.
28  * <p>
29  * This class may be instantiated; this class is not intended to be
30  * subclassed.
31  * </p>
32  *
33  * @since 3.0
34  */

35 public class PackageFragmentRootSourceContainer extends AbstractSourceContainer {
36     
37     private IPackageFragmentRoot fRoot;
38     /**
39      * Unique identifier for Java project source container type
40      * (value <code>org.eclipse.jdt.launching.sourceContainer.packageFragmentRoot</code>).
41      */

42     public static final String JavaDoc TYPE_ID = LaunchingPlugin.getUniqueIdentifier() + ".sourceContainer.packageFragmentRoot"; //$NON-NLS-1$
43

44     /**
45      * Constructs a new package fragment root source container on the
46      * given root. The root must be of kind <code>K_BINARY</code>.
47      *
48      * @param root package fragment root
49      */

50     public PackageFragmentRootSourceContainer(IPackageFragmentRoot root) {
51         fRoot = root;
52     }
53     
54     /* (non-Javadoc)
55      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#findSourceElements(java.lang.String)
56      */

57     public Object JavaDoc[] findSourceElements(String JavaDoc name) throws CoreException {
58         // look for a class file
59
int index = name.lastIndexOf('.');
60         String JavaDoc typeName = name;
61         if (index >= 0) {
62             // remove file type suffix
63
typeName = typeName.substring(0, index);
64         }
65         typeName = typeName.replace('/', '.');
66         typeName = typeName.replace('\\', '.');
67         index = typeName.lastIndexOf('.');
68         String JavaDoc packageName = ""; //$NON-NLS-1$
69
if (index >= 0) {
70             packageName = typeName.substring(0, index);
71             typeName = typeName.substring(index + 1);
72         }
73         IPackageFragment fragment = fRoot.getPackageFragment(packageName);
74         if (fragment.exists()) {
75             switch (fragment.getKind()) {
76                 case IPackageFragmentRoot.K_BINARY:
77                     IClassFile file = fragment.getClassFile(typeName + ".class"); //$NON-NLS-1$
78
if (file.exists()) {
79                         return new Object JavaDoc[]{file};
80                     }
81                     break;
82                 case IPackageFragmentRoot.K_SOURCE:
83                     String JavaDoc[] extensions = JavaCore.getJavaLikeExtensions();
84                     for (int i = 0; i < extensions.length; i++) {
85                         String JavaDoc ext = extensions[i];
86                         ICompilationUnit unit = fragment.getCompilationUnit(typeName + '.' + ext);
87                         if (unit.exists()) {
88                             return new Object JavaDoc[]{unit};
89                         }
90                     }
91                     break;
92             }
93
94         }
95         return EMPTY;
96     }
97     /* (non-Javadoc)
98      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getName()
99      */

100     public String JavaDoc getName() {
101         return fRoot.getElementName();
102     }
103     /* (non-Javadoc)
104      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getType()
105      */

106     public ISourceContainerType getType() {
107         return getSourceContainerType(TYPE_ID);
108     }
109     
110     /* (non-Javadoc)
111      * @see java.lang.Object#equals(java.lang.Object)
112      */

113     public boolean equals(Object JavaDoc obj) {
114         return obj instanceof PackageFragmentRootSourceContainer &&
115          ((PackageFragmentRootSourceContainer)obj).getPackageFragmentRoot().equals(getPackageFragmentRoot());
116     }
117     
118     /**
119      * Returns the package fragment root this container searches for source.
120      *
121      * @return the package fragment root this container searches for source
122      */

123     public IPackageFragmentRoot getPackageFragmentRoot() {
124         return fRoot;
125     }
126
127     /* (non-Javadoc)
128      * @see java.lang.Object#hashCode()
129      */

130     public int hashCode() {
131         return fRoot.hashCode();
132     }
133     /* (non-Javadoc)
134      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getPath()
135      */

136     public IPath getPath() {
137         return getPackageFragmentRoot().getPath();
138     }
139 }
140
Popular Tags