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.File;
013: import java.io.IOException;
014: import java.io.StringReader;
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.runtime.CoreException;
021: import org.eclipse.core.runtime.IPath;
022: import org.eclipse.core.runtime.IStatus;
023: import org.eclipse.core.runtime.Path;
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.IJavaModelStatusConstants;
028: import org.eclipse.jdt.core.JavaModelException;
029: import org.eclipse.jdt.internal.launching.LaunchingMessages;
030: import org.eclipse.jdt.internal.launching.LaunchingPlugin;
031: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034: import org.xml.sax.InputSource;
035: import org.xml.sax.SAXException;
036: import org.xml.sax.helpers.DefaultHandler;
037:
038: import com.ibm.icu.text.MessageFormat;
039:
040: /**
041: * Locates source elements in a directory in the local
042: * file system. Returns instances of <code>LocalFileStorage</code>.
043: * <p>
044: * This class may be instantiated; it is not intended to be subclassed.
045: * </p>
046: * @see IJavaSourceLocation
047: * @since 2.0
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.debug.core.sourcelookup.containers.DirectorySourceContainer</code>.
056: */
057: public class DirectorySourceLocation extends PlatformObject implements
058: IJavaSourceLocation {
059:
060: /**
061: * The directory associated with this source location
062: */
063: private File fDirectory;
064:
065: /**
066: * Constructs a new empty source location to be initialized from
067: * a memento.
068: */
069: public DirectorySourceLocation() {
070: }
071:
072: /**
073: * Constructs a new source location that will retrieve source
074: * elements from the given directory.
075: *
076: * @param directory a directory
077: */
078: public DirectorySourceLocation(File directory) {
079: setDirectory(directory);
080: }
081:
082: /* (non-Javadoc)
083: * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#findSourceElement(java.lang.String)
084: */
085: public Object findSourceElement(String name) throws CoreException {
086: if (getDirectory() == null) {
087: return null;
088: }
089:
090: String pathStr = name.replace('.', '/');
091: int lastSlash = pathStr.lastIndexOf('/');
092: try {
093: IPath root = new Path(getDirectory().getCanonicalPath());
094: boolean possibleInnerType = false;
095: String typeName = pathStr;
096: do {
097: IPath filePath = root.append(new Path(typeName
098: + ".java")); //$NON-NLS-1$
099: File file = filePath.toFile();
100: if (file.exists()) {
101: return new LocalFileStorage(file);
102: }
103: int index = typeName.lastIndexOf('$');
104: if (index > lastSlash) {
105: typeName = typeName.substring(0, index);
106: possibleInnerType = true;
107: } else {
108: possibleInnerType = false;
109: }
110: } while (possibleInnerType);
111: } catch (IOException e) {
112: throw new JavaModelException(e,
113: IJavaModelStatusConstants.IO_EXCEPTION);
114: }
115: return null;
116: }
117:
118: /**
119: * Sets the directory in which source elements will
120: * be searched for.
121: *
122: * @param directory a directory
123: */
124: private void setDirectory(File directory) {
125: fDirectory = directory;
126: }
127:
128: /**
129: * Returns the directory associated with this source
130: * location.
131: *
132: * @return directory
133: */
134: public File getDirectory() {
135: return fDirectory;
136: }
137:
138: /* (non-Javadoc)
139: * @see java.lang.Object#equals(java.lang.Object)
140: */
141: public boolean equals(Object object) {
142: return object instanceof DirectorySourceLocation
143: && getDirectory().equals(
144: ((DirectorySourceLocation) object)
145: .getDirectory());
146: }
147:
148: /* (non-Javadoc)
149: * @see java.lang.Object#hashCode()
150: */
151: public int hashCode() {
152: return getDirectory().hashCode();
153: }
154:
155: /* (non-Javadoc)
156: * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#getMemento()
157: */
158: public String getMemento() throws CoreException {
159: Document doc = DebugPlugin.newDocument();
160: Element node = doc.createElement("directorySourceLocation"); //$NON-NLS-1$
161: doc.appendChild(node);
162: node.setAttribute("path", getDirectory().getAbsolutePath()); //$NON-NLS-1$
163: return DebugPlugin.serializeDocument(doc);
164: }
165:
166: /* (non-Javadoc)
167: * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#initializeFrom(java.lang.String)
168: */
169: public void initializeFrom(String memento) throws CoreException {
170: Exception ex = null;
171: try {
172: Element root = null;
173: DocumentBuilder parser = DocumentBuilderFactory
174: .newInstance().newDocumentBuilder();
175: parser.setErrorHandler(new DefaultHandler());
176: StringReader reader = new StringReader(memento);
177: InputSource source = new InputSource(reader);
178: root = parser.parse(source).getDocumentElement();
179:
180: String path = root.getAttribute("path"); //$NON-NLS-1$
181: if (isEmpty(path)) {
182: abort(
183: LaunchingMessages.DirectorySourceLocation_Unable_to_initialize_source_location___missing_directory_path_3,
184: null);
185: } else {
186: File dir = new File(path);
187: if (dir.exists() && dir.isDirectory()) {
188: setDirectory(dir);
189: } else {
190: abort(
191: MessageFormat
192: .format(
193: LaunchingMessages.DirectorySourceLocation_Unable_to_initialize_source_location___directory_does_not_exist___0__4,
194: new String[] { path }),
195: null);
196: }
197: }
198: return;
199: } catch (ParserConfigurationException e) {
200: ex = e;
201: } catch (SAXException e) {
202: ex = e;
203: } catch (IOException e) {
204: ex = e;
205: }
206: abort(
207: LaunchingMessages.DirectorySourceLocation_Exception_occurred_initializing_source_location__5,
208: ex);
209: }
210:
211: private boolean isEmpty(String string) {
212: return string == null || string.length() == 0;
213: }
214:
215: /*
216: * Throws an internal error exception
217: */
218: private void abort(String message, Throwable e)
219: throws CoreException {
220: IStatus s = new Status(IStatus.ERROR, LaunchingPlugin
221: .getUniqueIdentifier(),
222: IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR,
223: message, e);
224: throw new CoreException(s);
225: }
226: }
|