KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > launching > sourcelookup > JavaProjectSourceLocation


1 /*******************************************************************************
2  * Copyright (c) 2000, 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;
12
13
14 import java.io.IOException JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import com.ibm.icu.text.MessageFormat;
17 import java.util.ArrayList JavaDoc;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22 import javax.xml.transform.TransformerException JavaDoc;
23
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.PlatformObject;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jdt.core.IPackageFragmentRoot;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jdt.core.JavaModelException;
34 import org.eclipse.jdt.internal.launching.LaunchingMessages;
35 import org.eclipse.jdt.internal.launching.LaunchingPlugin;
36 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.helpers.DefaultHandler JavaDoc;
42  
43 /**
44  * Locates source elements in all source folders of the
45  * given Java project. Returns instances of <code>ICompilationUnit</code>
46  * and </code>IClassFile</code>.
47  * <p>
48  * This class may be instantiated; it is not intended to be subclassed.
49  * </p>
50  * @see IJavaSourceLocation
51  * @since 2.0
52  * @deprecated In 3.0, the debug platform provides source lookup facilities that
53  * should be used in place of the Java source lookup support provided in 2.0.
54  * The new facilities provide a source lookup director that coordinates source
55  * lookup among a set of participants, searching a set of source containers.
56  * See the following packages: <code>org.eclipse.debug.core.sourcelookup</code>
57  * and <code>org.eclipse.debug.core.sourcelookup.containers</code>. This class
58  * has been replaced by
59  * <code>org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer</code>.
60  */

61 public class JavaProjectSourceLocation extends PlatformObject implements IJavaSourceLocation {
62
63     /**
64      * The project associated with this source location
65      */

66     private IJavaProject fProject;
67     
68     /**
69      * Corresponding package fragment root locations.
70      */

71     private IJavaSourceLocation[] fRootLocations = null;
72     
73     /**
74      * Constructs a new empty source location to be initialized
75      * by a memento.
76      */

77     public JavaProjectSourceLocation() {
78     }
79     
80     /**
81      * Constructs a new source location that will retrieve source
82      * elements from the given Java project.
83      *
84      * @param project Java project
85      */

86     public JavaProjectSourceLocation(IJavaProject project) {
87         setJavaProject(project);
88     }
89     
90     /* (non-Javadoc)
91      * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#findSourceElement(java.lang.String)
92      */

93     public Object JavaDoc findSourceElement(String JavaDoc name) throws CoreException {
94         if (fRootLocations != null) {
95             for (int i = 0; i < fRootLocations.length; i++) {
96                 Object JavaDoc element = fRootLocations[i].findSourceElement(name);
97                 if (element != null) {
98                     return element;
99                 }
100             }
101         }
102         return null;
103     }
104
105     /**
106      * Sets the Java project in which source elements will
107      * be searched for.
108      *
109      * @param project Java project
110      */

111     private void setJavaProject(IJavaProject project) {
112         fProject = project;
113         fRootLocations = null;
114         if (fProject != null) {
115             try {
116                 IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
117                 ArrayList JavaDoc list = new ArrayList JavaDoc(roots.length);
118                 
119                 for (int i = 0; i < roots.length; i++) {
120                     if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
121                         list.add(new PackageFragmentRootSourceLocation(roots[i]));
122                     }
123                 }
124                 fRootLocations = (IJavaSourceLocation[])list.toArray(new IJavaSourceLocation[list.size()]);
125             } catch (JavaModelException e) {
126                 LaunchingPlugin.log(e);
127             }
128         }
129     }
130     
131     /**
132      * Returns the Java project associated with this source
133      * location.
134      *
135      * @return Java project
136      */

137     public IJavaProject getJavaProject() {
138         return fProject;
139     }
140     
141     /* (non-Javadoc)
142      * @see java.lang.Object#equals(java.lang.Object)
143      */

144     public boolean equals(Object JavaDoc object) {
145         return object instanceof JavaProjectSourceLocation &&
146              getJavaProject().equals(((JavaProjectSourceLocation)object).getJavaProject());
147     }
148     
149     /* (non-Javadoc)
150      * @see java.lang.Object#hashCode()
151      */

152     public int hashCode() {
153         return getJavaProject().hashCode();
154     }
155     
156     /* (non-Javadoc)
157      * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#getMemento()
158      */

159     public String JavaDoc getMemento() throws CoreException {
160         try {
161             Document JavaDoc doc = LaunchingPlugin.getDocument();
162             Element JavaDoc node = doc.createElement("javaProjectSourceLocation"); //$NON-NLS-1$
163
doc.appendChild(node);
164             node.setAttribute("name", getJavaProject().getElementName()); //$NON-NLS-1$
165
return LaunchingPlugin.serializeDocument(doc);
166         } catch (IOException JavaDoc e) {
167             abort(MessageFormat.format(LaunchingMessages.JavaProjectSourceLocation_Unable_to_create_memento_for_Java_project_source_location__0__1, new String JavaDoc[] {getJavaProject().getElementName()}), e);
168         } catch (ParserConfigurationException JavaDoc e) {
169             abort(MessageFormat.format(LaunchingMessages.JavaProjectSourceLocation_Unable_to_create_memento_for_Java_project_source_location__0__1, new String JavaDoc[] {getJavaProject().getElementName()}), e);
170         } catch (TransformerException JavaDoc e) {
171             abort(MessageFormat.format(LaunchingMessages.JavaProjectSourceLocation_Unable_to_create_memento_for_Java_project_source_location__0__1, new String JavaDoc[] {getJavaProject().getElementName()}), e);
172         }
173         // execution will not reach here
174
return null;
175     }
176
177     /* (non-Javadoc)
178      * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#initializeFrom(java.lang.String)
179      */

180     public void initializeFrom(String JavaDoc memento) throws CoreException {
181         Exception JavaDoc ex = null;
182         try {
183             Element JavaDoc root = null;
184             DocumentBuilder JavaDoc parser =
185                 DocumentBuilderFactory.newInstance().newDocumentBuilder();
186             parser.setErrorHandler(new DefaultHandler JavaDoc());
187             StringReader JavaDoc reader = new StringReader JavaDoc(memento);
188             InputSource JavaDoc source = new InputSource JavaDoc(reader);
189             root = parser.parse(source).getDocumentElement();
190                                                 
191             String JavaDoc name = root.getAttribute("name"); //$NON-NLS-1$
192
if (isEmpty(name)) {
193                 abort(LaunchingMessages.JavaProjectSourceLocation_Unable_to_initialize_source_location___missing_project_name_3, null);
194             } else {
195                 IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
196                 setJavaProject(JavaCore.create(proj));
197             }
198             return;
199         } catch (ParserConfigurationException JavaDoc e) {
200             ex = e;
201         } catch (SAXException JavaDoc e) {
202             ex = e;
203         } catch (IOException JavaDoc e) {
204             ex = e;
205         }
206         abort(LaunchingMessages.JavaProjectSourceLocation_Exception_occurred_initializing_source_location__4, ex);
207     }
208
209     private boolean isEmpty(String JavaDoc string) {
210         return string == null || string.length() == 0;
211     }
212     
213     /*
214      * Throws an internal error exception
215      */

216     private void abort(String JavaDoc message, Throwable JavaDoc e) throws CoreException {
217         IStatus s = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR, message, e);
218         throw new CoreException(s);
219     }
220
221 }
222
Popular Tags