001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.launching.sourcelookup;
011:
012: import java.io.IOException;
013: import java.io.StringReader;
014: import java.util.ArrayList;
015:
016: import javax.xml.parsers.DocumentBuilder;
017: import javax.xml.parsers.DocumentBuilderFactory;
018: import javax.xml.parsers.ParserConfigurationException;
019:
020: import org.eclipse.core.resources.IProject;
021: import org.eclipse.core.resources.ResourcesPlugin;
022: import org.eclipse.core.runtime.CoreException;
023: import org.eclipse.core.runtime.IStatus;
024: import org.eclipse.core.runtime.PlatformObject;
025: import org.eclipse.core.runtime.Status;
026: import org.eclipse.debug.core.DebugPlugin;
027: import org.eclipse.jdt.core.IJavaProject;
028: import org.eclipse.jdt.core.IPackageFragmentRoot;
029: import org.eclipse.jdt.core.JavaCore;
030: import org.eclipse.jdt.core.JavaModelException;
031: import org.eclipse.jdt.internal.launching.LaunchingMessages;
032: import org.eclipse.jdt.internal.launching.LaunchingPlugin;
033: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.xml.sax.InputSource;
037: import org.xml.sax.SAXException;
038: import org.xml.sax.helpers.DefaultHandler;
039:
040: /**
041: * Locates source elements in all source folders of the
042: * given Java project. Returns instances of <code>ICompilationUnit</code>
043: * and </code>IClassFile</code>.
044: * <p>
045: * This class may be instantiated; it is not intended to be subclassed.
046: * </p>
047: * @see IJavaSourceLocation
048: * @since 2.0
049: * @deprecated In 3.0, the debug platform provides source lookup facilities that
050: * should be used in place of the Java source lookup support provided in 2.0.
051: * The new facilities provide a source lookup director that coordinates source
052: * lookup among a set of participants, searching a set of source containers.
053: * See the following packages: <code>org.eclipse.debug.core.sourcelookup</code>
054: * and <code>org.eclipse.debug.core.sourcelookup.containers</code>. This class
055: * has been replaced by
056: * <code>org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer</code>.
057: */
058: public class JavaProjectSourceLocation extends PlatformObject implements
059: IJavaSourceLocation {
060:
061: /**
062: * The project associated with this source location
063: */
064: private IJavaProject fProject;
065:
066: /**
067: * Corresponding package fragment root locations.
068: */
069: private IJavaSourceLocation[] fRootLocations = null;
070:
071: /**
072: * Constructs a new empty source location to be initialized
073: * by a memento.
074: */
075: public JavaProjectSourceLocation() {
076: }
077:
078: /**
079: * Constructs a new source location that will retrieve source
080: * elements from the given Java project.
081: *
082: * @param project Java project
083: */
084: public JavaProjectSourceLocation(IJavaProject project) {
085: setJavaProject(project);
086: }
087:
088: /* (non-Javadoc)
089: * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#findSourceElement(java.lang.String)
090: */
091: public Object findSourceElement(String name) throws CoreException {
092: if (fRootLocations != null) {
093: for (int i = 0; i < fRootLocations.length; i++) {
094: Object element = fRootLocations[i]
095: .findSourceElement(name);
096: if (element != null) {
097: return element;
098: }
099: }
100: }
101: return null;
102: }
103:
104: /**
105: * Sets the Java project in which source elements will
106: * be searched for.
107: *
108: * @param project Java project
109: */
110: private void setJavaProject(IJavaProject project) {
111: fProject = project;
112: fRootLocations = null;
113: if (fProject != null) {
114: try {
115: IPackageFragmentRoot[] roots = project
116: .getPackageFragmentRoots();
117: ArrayList list = new ArrayList(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(
122: roots[i]));
123: }
124: }
125: fRootLocations = (IJavaSourceLocation[]) list
126: .toArray(new IJavaSourceLocation[list.size()]);
127: } catch (JavaModelException e) {
128: LaunchingPlugin.log(e);
129: }
130: }
131: }
132:
133: /**
134: * Returns the Java project associated with this source
135: * location.
136: *
137: * @return Java project
138: */
139: public IJavaProject getJavaProject() {
140: return fProject;
141: }
142:
143: /* (non-Javadoc)
144: * @see java.lang.Object#equals(java.lang.Object)
145: */
146: public boolean equals(Object object) {
147: return object instanceof JavaProjectSourceLocation
148: && getJavaProject().equals(
149: ((JavaProjectSourceLocation) object)
150: .getJavaProject());
151: }
152:
153: /* (non-Javadoc)
154: * @see java.lang.Object#hashCode()
155: */
156: public int hashCode() {
157: return getJavaProject().hashCode();
158: }
159:
160: /* (non-Javadoc)
161: * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#getMemento()
162: */
163: public String getMemento() throws CoreException {
164: Document doc = DebugPlugin.newDocument();
165: Element node = doc.createElement("javaProjectSourceLocation"); //$NON-NLS-1$
166: doc.appendChild(node);
167: node.setAttribute("name", getJavaProject().getElementName()); //$NON-NLS-1$
168: return DebugPlugin.serializeDocument(doc);
169: }
170:
171: /* (non-Javadoc)
172: * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#initializeFrom(java.lang.String)
173: */
174: public void initializeFrom(String memento) throws CoreException {
175: Exception ex = null;
176: try {
177: Element root = null;
178: DocumentBuilder parser = DocumentBuilderFactory
179: .newInstance().newDocumentBuilder();
180: parser.setErrorHandler(new DefaultHandler());
181: StringReader reader = new StringReader(memento);
182: InputSource source = new InputSource(reader);
183: root = parser.parse(source).getDocumentElement();
184:
185: String name = root.getAttribute("name"); //$NON-NLS-1$
186: if (isEmpty(name)) {
187: abort(
188: LaunchingMessages.JavaProjectSourceLocation_Unable_to_initialize_source_location___missing_project_name_3,
189: null);
190: } else {
191: IProject proj = ResourcesPlugin.getWorkspace()
192: .getRoot().getProject(name);
193: setJavaProject(JavaCore.create(proj));
194: }
195: return;
196: } catch (ParserConfigurationException e) {
197: ex = e;
198: } catch (SAXException e) {
199: ex = e;
200: } catch (IOException e) {
201: ex = e;
202: }
203: abort(
204: LaunchingMessages.JavaProjectSourceLocation_Exception_occurred_initializing_source_location__4,
205: ex);
206: }
207:
208: private boolean isEmpty(String string) {
209: return string == null || string.length() == 0;
210: }
211:
212: /*
213: * Throws an internal error exception
214: */
215: private void abort(String message, Throwable e)
216: throws CoreException {
217: IStatus s = new Status(IStatus.ERROR, LaunchingPlugin
218: .getUniqueIdentifier(),
219: IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR,
220: message, e);
221: throw new CoreException(s);
222: }
223:
224: }
|