KickJava   Java API By Example, From Geeks To Geeks.

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


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

57 public class DirectorySourceLocation extends PlatformObject implements IJavaSourceLocation {
58
59     /**
60      * The directory associated with this source location
61      */

62     private File JavaDoc fDirectory;
63     
64     /**
65      * Constructs a new empty source location to be initialized from
66      * a memento.
67      */

68     public DirectorySourceLocation() {
69     }
70         
71     /**
72      * Constructs a new source location that will retrieve source
73      * elements from the given directory.
74      *
75      * @param directory a directory
76      */

77     public DirectorySourceLocation(File JavaDoc directory) {
78         setDirectory(directory);
79     }
80     
81     /* (non-Javadoc)
82      * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#findSourceElement(java.lang.String)
83      */

84     public Object JavaDoc findSourceElement(String JavaDoc name) throws CoreException {
85         if (getDirectory() == null) {
86             return null;
87         }
88         
89         String JavaDoc pathStr= name.replace('.', '/');
90         int lastSlash = pathStr.lastIndexOf('/');
91         try {
92             IPath root = new Path(getDirectory().getCanonicalPath());
93             boolean possibleInnerType = false;
94             String JavaDoc typeName = pathStr;
95             do {
96                 IPath filePath = root.append(new Path(typeName + ".java")); //$NON-NLS-1$
97
File JavaDoc file = filePath.toFile();
98                 if (file.exists()) {
99                     return new LocalFileStorage(file);
100                 }
101                 int index = typeName.lastIndexOf('$');
102                 if (index > lastSlash) {
103                     typeName = typeName.substring(0, index);
104                     possibleInnerType = true;
105                 } else {
106                     possibleInnerType = false;
107                 }
108             } while (possibleInnerType);
109         } catch (IOException JavaDoc e) {
110             throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
111         }
112         return null;
113     }
114
115     /**
116      * Sets the directory in which source elements will
117      * be searched for.
118      *
119      * @param directory a directory
120      */

121     private void setDirectory(File JavaDoc directory) {
122         fDirectory = directory;
123     }
124     
125     /**
126      * Returns the directory associated with this source
127      * location.
128      *
129      * @return directory
130      */

131     public File JavaDoc getDirectory() {
132         return fDirectory;
133     }
134     
135     /* (non-Javadoc)
136      * @see java.lang.Object#equals(java.lang.Object)
137      */

138     public boolean equals(Object JavaDoc object) {
139         return object instanceof DirectorySourceLocation &&
140              getDirectory().equals(((DirectorySourceLocation)object).getDirectory());
141     }
142     
143     /* (non-Javadoc)
144      * @see java.lang.Object#hashCode()
145      */

146     public int hashCode() {
147         return getDirectory().hashCode();
148     }
149     
150     /* (non-Javadoc)
151      * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#getMemento()
152      */

153     public String JavaDoc getMemento() throws CoreException {
154         try {
155             Document JavaDoc doc = LaunchingPlugin.getDocument();
156             Element JavaDoc node = doc.createElement("directorySourceLocation"); //$NON-NLS-1$
157
doc.appendChild(node);
158             node.setAttribute("path", getDirectory().getAbsolutePath()); //$NON-NLS-1$
159
return LaunchingPlugin.serializeDocument(doc);
160         } catch (IOException JavaDoc e) {
161             abort(MessageFormat.format(LaunchingMessages.DirectorySourceLocation_Unable_to_create_memento_for_directory_source_location__0__1, new String JavaDoc[] {getDirectory().getAbsolutePath()}), e);
162         } catch (ParserConfigurationException JavaDoc e) {
163             abort(MessageFormat.format(LaunchingMessages.DirectorySourceLocation_Unable_to_create_memento_for_directory_source_location__0__1, new String JavaDoc[] {getDirectory().getAbsolutePath()}), e);
164         } catch (TransformerException JavaDoc e) {
165             abort(MessageFormat.format(LaunchingMessages.DirectorySourceLocation_Unable_to_create_memento_for_directory_source_location__0__1, new String JavaDoc[] {getDirectory().getAbsolutePath()}), e);
166         }
167         // execution will not reach here
168
return null;
169     }
170
171     /* (non-Javadoc)
172      * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#initializeFrom(java.lang.String)
173      */

174     public void initializeFrom(String JavaDoc memento) throws CoreException {
175         Exception JavaDoc ex = null;
176         try {
177             Element JavaDoc root = null;
178             DocumentBuilder JavaDoc parser =
179                 DocumentBuilderFactory.newInstance().newDocumentBuilder();
180             parser.setErrorHandler(new DefaultHandler JavaDoc());
181             StringReader JavaDoc reader = new StringReader JavaDoc(memento);
182             InputSource JavaDoc source = new InputSource JavaDoc(reader);
183             root = parser.parse(source).getDocumentElement();
184                                                 
185             String JavaDoc path = root.getAttribute("path"); //$NON-NLS-1$
186
if (isEmpty(path)) {
187                 abort(LaunchingMessages.DirectorySourceLocation_Unable_to_initialize_source_location___missing_directory_path_3, null);
188             } else {
189                 File JavaDoc dir = new File JavaDoc(path);
190                 if (dir.exists() && dir.isDirectory()) {
191                     setDirectory(dir);
192                 } else {
193                     abort(MessageFormat.format(LaunchingMessages.DirectorySourceLocation_Unable_to_initialize_source_location___directory_does_not_exist___0__4, new String JavaDoc[] {path}), null);
194                 }
195             }
196             return;
197         } catch (ParserConfigurationException JavaDoc e) {
198             ex = e;
199         } catch (SAXException JavaDoc e) {
200             ex = e;
201         } catch (IOException JavaDoc e) {
202             ex = e;
203         }
204         abort(LaunchingMessages.DirectorySourceLocation_Exception_occurred_initializing_source_location__5, ex);
205     }
206
207     private boolean isEmpty(String JavaDoc string) {
208         return string == null || string.length() == 0;
209     }
210     
211     /*
212      * Throws an internal error exception
213      */

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