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:
015: import javax.xml.parsers.DocumentBuilder;
016: import javax.xml.parsers.DocumentBuilderFactory;
017: import javax.xml.parsers.ParserConfigurationException;
018:
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IStatus;
021: import org.eclipse.core.runtime.PlatformObject;
022: import org.eclipse.core.runtime.Status;
023: import org.eclipse.debug.core.DebugPlugin;
024: import org.eclipse.jdt.core.IClassFile;
025: import org.eclipse.jdt.core.ICompilationUnit;
026: import org.eclipse.jdt.core.IJavaElement;
027: import org.eclipse.jdt.core.IPackageFragment;
028: import org.eclipse.jdt.core.IPackageFragmentRoot;
029: import org.eclipse.jdt.core.JavaCore;
030: import org.eclipse.jdt.internal.launching.LaunchingMessages;
031: import org.eclipse.jdt.internal.launching.LaunchingPlugin;
032: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
033: import org.w3c.dom.Document;
034: import org.w3c.dom.Element;
035: import org.xml.sax.InputSource;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.helpers.DefaultHandler;
038:
039: /**
040: * Locates source elements in a package fragment root. Returns
041: * instances of <code>ICompilationUnit</code> and
042: * </code>IClassFile</code>.
043: * <p>
044: * This class may be instantiated; it is not intended to be subclassed.
045: * </p>
046: * @see IJavaSourceLocation
047: * @since 2.1
048: * @deprecated In 3.0, the debug platform provides source lookup facilities that
049: * should be used in place of the Java source lookup support provided in 2.0.
050: * The new facilities provide a source lookup director that coordinates source
051: * lookup among a set of participants, searching a set of source containers.
052: * See the following packages: <code>org.eclipse.debug.core.sourcelookup</code>
053: * and <code>org.eclipse.debug.core.sourcelookup.containers</code>. This class
054: * has been replaced by
055: * <code>org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer</code>.
056: */
057: public class PackageFragmentRootSourceLocation extends PlatformObject
058: implements IJavaSourceLocation {
059:
060: /**
061: * Associated package fragment root
062: */
063: private IPackageFragmentRoot fRoot = null;
064:
065: /**
066: * Creates an empty source location.
067: */
068: public PackageFragmentRootSourceLocation() {
069: }
070:
071: /**
072: * Creates a source location on the given package fragment root.
073: *
074: * @param root package fragment root
075: */
076: public PackageFragmentRootSourceLocation(IPackageFragmentRoot root) {
077: setPackageFragmentRoot(root);
078: }
079:
080: /* (non-Javadoc)
081: * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#findSourceElement(java.lang.String)
082: */
083: public Object findSourceElement(String name) throws CoreException {
084: if (name != null && getPackageFragmentRoot() != null) {
085: IPackageFragment pkg = null;
086: int index = name.lastIndexOf('.');
087: if (index >= 0) {
088: String fragment = name.substring(0, index);
089: pkg = getPackageFragmentRoot().getPackageFragment(
090: fragment);
091: name = name.substring(index + 1);
092: } else {
093: pkg = getPackageFragmentRoot().getPackageFragment(""); //$NON-NLS-1$
094: }
095: if (pkg.exists()) {
096: boolean possibleInnerType = false;
097: String typeName = name;
098: do {
099: ICompilationUnit cu = pkg
100: .getCompilationUnit(typeName + ".java"); //$NON-NLS-1$
101: if (cu.exists()) {
102: return cu;
103: }
104: IClassFile cf = pkg.getClassFile(typeName
105: + ".class"); //$NON-NLS-1$
106: if (cf.exists()) {
107: return cf;
108: }
109: index = typeName.lastIndexOf('$');
110: if (index >= 0) {
111: typeName = typeName.substring(0, index);
112: possibleInnerType = true;
113: } else {
114: possibleInnerType = false;
115: }
116: } while (possibleInnerType);
117: }
118: }
119: return null;
120: }
121:
122: /* (non-Javadoc)
123: * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#getMemento()
124: */
125: public String getMemento() throws CoreException {
126: Document doc = DebugPlugin.newDocument();
127: Element node = doc
128: .createElement("javaPackageFragmentRootSourceLocation"); //$NON-NLS-1$
129: doc.appendChild(node);
130: String handle = ""; //$NON-NLS-1$
131: if (getPackageFragmentRoot() != null) {
132: handle = getPackageFragmentRoot().getHandleIdentifier();
133: }
134: node.setAttribute("handleId", handle); //$NON-NLS-1$
135: return DebugPlugin.serializeDocument(doc);
136: }
137:
138: /* (non-Javadoc)
139: * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#initializeFrom(java.lang.String)
140: */
141: public void initializeFrom(String memento) throws CoreException {
142: Exception ex = null;
143: try {
144: Element root = null;
145: DocumentBuilder parser = DocumentBuilderFactory
146: .newInstance().newDocumentBuilder();
147: parser.setErrorHandler(new DefaultHandler());
148: StringReader reader = new StringReader(memento);
149: InputSource source = new InputSource(reader);
150: root = parser.parse(source).getDocumentElement();
151:
152: String handle = root.getAttribute("handleId"); //$NON-NLS-1$
153: if (handle == null) {
154: abort(
155: LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_initialize_source_location___missing_handle_identifier_for_package_fragment_root__6,
156: null);
157: } else {
158: if (handle.length() == 0) {
159: // empty package fragment
160: setPackageFragmentRoot(null);
161: } else {
162: IJavaElement element = JavaCore.create(handle);
163: if (element instanceof IPackageFragmentRoot) {
164: setPackageFragmentRoot((IPackageFragmentRoot) element);
165: } else {
166: abort(
167: LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_initialize_source_location___package_fragment_root_does_not_exist__7,
168: null);
169: }
170: }
171: }
172: return;
173: } catch (ParserConfigurationException e) {
174: ex = e;
175: } catch (SAXException e) {
176: ex = e;
177: } catch (IOException e) {
178: ex = e;
179: }
180: abort(
181: LaunchingMessages.PackageFragmentRootSourceLocation_Exception_occurred_initializing_source_location__8,
182: ex);
183: }
184:
185: /**
186: * Returns the package fragment root associated with this
187: * source location, or <code>null</code> if none
188: *
189: * @return the package fragment root associated with this
190: * source location, or <code>null</code> if none
191: */
192: public IPackageFragmentRoot getPackageFragmentRoot() {
193: return fRoot;
194: }
195:
196: /**
197: * Sets the package fragment root associated with this
198: * source location.
199: *
200: * @param root package fragment root
201: */
202: private void setPackageFragmentRoot(IPackageFragmentRoot root) {
203: fRoot = root;
204: }
205:
206: /*
207: * Throws an internal error exception
208: */
209: private void abort(String message, Throwable e)
210: throws CoreException {
211: IStatus s = new Status(IStatus.ERROR, LaunchingPlugin
212: .getUniqueIdentifier(),
213: IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR,
214: message, e);
215: throw new CoreException(s);
216: }
217:
218: /* (non-Javadoc)
219: * @see java.lang.Object#equals(java.lang.Object)
220: */
221: public boolean equals(Object object) {
222: if (object instanceof PackageFragmentRootSourceLocation) {
223: PackageFragmentRootSourceLocation root = (PackageFragmentRootSourceLocation) object;
224: if (getPackageFragmentRoot() == null) {
225: return root.getPackageFragmentRoot() == null;
226: }
227: return getPackageFragmentRoot().equals(
228: root.getPackageFragmentRoot());
229: }
230: return false;
231: }
232:
233: /* (non-Javadoc)
234: * @see java.lang.Object#hashCode()
235: */
236: public int hashCode() {
237: if (getPackageFragmentRoot() == null) {
238: return getClass().hashCode();
239: }
240: return getPackageFragmentRoot().hashCode();
241: }
242: }
|