KickJava   Java API By Example, From Geeks To Geeks.

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


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
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.IStatus;
25 import org.eclipse.core.runtime.PlatformObject;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.jdt.core.IClassFile;
28 import org.eclipse.jdt.core.ICompilationUnit;
29 import org.eclipse.jdt.core.IJavaElement;
30 import org.eclipse.jdt.core.IPackageFragment;
31 import org.eclipse.jdt.core.IPackageFragmentRoot;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jdt.internal.launching.LaunchingMessages;
34 import org.eclipse.jdt.internal.launching.LaunchingPlugin;
35 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.helpers.DefaultHandler JavaDoc;
41
42 /**
43  * Locates source elements in a package fragment root. Returns
44  * instances of <code>ICompilationUnit</code> and
45  * </code>IClassFile</code>.
46  * <p>
47  * This class may be instantiated; it is not intended to be subclassed.
48  * </p>
49  * @see IJavaSourceLocation
50  * @since 2.1
51  * @deprecated In 3.0, the debug platform provides source lookup facilities that
52  * should be used in place of the Java source lookup support provided in 2.0.
53  * The new facilities provide a source lookup director that coordinates source
54  * lookup among a set of participants, searching a set of source containers.
55  * See the following packages: <code>org.eclipse.debug.core.sourcelookup</code>
56  * and <code>org.eclipse.debug.core.sourcelookup.containers</code>. This class
57  * has been replaced by
58  * <code>org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer</code>.
59  */

60 public class PackageFragmentRootSourceLocation extends PlatformObject implements IJavaSourceLocation {
61     
62     /**
63      * Associated package fragment root
64      */

65     private IPackageFragmentRoot fRoot = null;
66     
67     /**
68      * Creates an empty source location.
69      */

70     public PackageFragmentRootSourceLocation() {
71     }
72     
73     /**
74      * Creates a source location on the given package fragment root.
75      *
76      * @param root package fragment root
77      */

78     public PackageFragmentRootSourceLocation(IPackageFragmentRoot root) {
79         setPackageFragmentRoot(root);
80     }
81
82     /* (non-Javadoc)
83      * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#findSourceElement(java.lang.String)
84      */

85     public Object JavaDoc findSourceElement(String JavaDoc name) throws CoreException {
86         if (name != null && getPackageFragmentRoot() != null) {
87             IPackageFragment pkg = null;
88             int index = name.lastIndexOf('.');
89             if (index >= 0) {
90                 String JavaDoc fragment = name.substring(0, index);
91                 pkg = getPackageFragmentRoot().getPackageFragment(fragment);
92                 name = name.substring(index + 1);
93             } else {
94                 pkg = getPackageFragmentRoot().getPackageFragment(""); //$NON-NLS-1$
95
}
96             if (pkg.exists()) {
97                 boolean possibleInnerType = false;
98                 String JavaDoc typeName = name;
99                 do {
100                     ICompilationUnit cu = pkg.getCompilationUnit(typeName + ".java"); //$NON-NLS-1$
101
if (cu.exists()) {
102                         return cu;
103                     }
104                     IClassFile cf = pkg.getClassFile(typeName + ".class"); //$NON-NLS-1$
105
if (cf.exists()) {
106                         return cf;
107                     }
108                     index = typeName.lastIndexOf('$');
109                     if (index >= 0) {
110                         typeName = typeName.substring(0, index);
111                         possibleInnerType = true;
112                     } else {
113                         possibleInnerType = false;
114                     }
115                 } while (possibleInnerType);
116             }
117         }
118         return null;
119     }
120
121     /* (non-Javadoc)
122      * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#getMemento()
123      */

124     public String JavaDoc getMemento() throws CoreException {
125         try {
126             Document JavaDoc doc = LaunchingPlugin.getDocument();
127             Element node = doc.createElement("javaPackageFragmentRootSourceLocation"); //$NON-NLS-1$
128
doc.appendChild(node);
129             String JavaDoc handle = ""; //$NON-NLS-1$
130
if (getPackageFragmentRoot() != null) {
131                 handle = getPackageFragmentRoot().getHandleIdentifier();
132             }
133             node.setAttribute("handleId", handle); //$NON-NLS-1$
134
return LaunchingPlugin.serializeDocument(doc);
135         } catch (IOException JavaDoc e) {
136             abort(MessageFormat.format(LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_create_memento_for_package_fragment_root_source_location__0__5, new String JavaDoc[] {getPackageFragmentRoot().getElementName()}), e);
137         } catch (ParserConfigurationException JavaDoc e) {
138             abort(MessageFormat.format(LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_create_memento_for_package_fragment_root_source_location__0__5, new String JavaDoc[] {getPackageFragmentRoot().getElementName()}), e);
139         } catch (TransformerException JavaDoc e) {
140             abort(MessageFormat.format(LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_create_memento_for_package_fragment_root_source_location__0__5, new String JavaDoc[] {getPackageFragmentRoot().getElementName()}), e);
141         }
142         // execution will not reach here
143
return null;
144     }
145
146     /* (non-Javadoc)
147      * @see org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation#initializeFrom(java.lang.String)
148      */

149     public void initializeFrom(String JavaDoc memento) throws CoreException {
150         Exception JavaDoc ex = null;
151         try {
152             Element root = null;
153             DocumentBuilder JavaDoc parser =
154                 DocumentBuilderFactory.newInstance().newDocumentBuilder();
155             parser.setErrorHandler(new DefaultHandler JavaDoc());
156             StringReader JavaDoc reader = new StringReader JavaDoc(memento);
157             InputSource JavaDoc source = new InputSource JavaDoc(reader);
158             root = parser.parse(source).getDocumentElement();
159                                                 
160             String JavaDoc handle = root.getAttribute("handleId"); //$NON-NLS-1$
161
if (handle == null) {
162                 abort(LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_initialize_source_location___missing_handle_identifier_for_package_fragment_root__6, null);
163             } else {
164                 if (handle.length() == 0) {
165                     // empty package fragment
166
setPackageFragmentRoot(null);
167                 } else {
168                     IJavaElement element = JavaCore.create(handle);
169                     if (element instanceof IPackageFragmentRoot) {
170                         setPackageFragmentRoot((IPackageFragmentRoot)element);
171                     } else {
172                         abort(LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_initialize_source_location___package_fragment_root_does_not_exist__7, null);
173                     }
174                 }
175             }
176             return;
177         } catch (ParserConfigurationException JavaDoc e) {
178             ex = e;
179         } catch (SAXException JavaDoc e) {
180             ex = e;
181         } catch (IOException JavaDoc e) {
182             ex = e;
183         }
184         abort(LaunchingMessages.PackageFragmentRootSourceLocation_Exception_occurred_initializing_source_location__8, ex);
185     }
186
187     /**
188      * Returns the package fragment root associated with this
189      * source location, or <code>null</code> if none
190      *
191      * @return the package fragment root associated with this
192      * source location, or <code>null</code> if none
193      */

194     public IPackageFragmentRoot getPackageFragmentRoot() {
195         return fRoot;
196     }
197
198     /**
199      * Sets the package fragment root associated with this
200      * source location.
201      *
202      * @param root package fragment root
203      */

204     private void setPackageFragmentRoot(IPackageFragmentRoot root) {
205         fRoot = root;
206     }
207     
208     /*
209      * Throws an internal error exception
210      */

211     private void abort(String JavaDoc message, Throwable JavaDoc e) throws CoreException {
212         IStatus s = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR, message, e);
213         throw new CoreException(s);
214     }
215     
216     /* (non-Javadoc)
217      * @see java.lang.Object#equals(java.lang.Object)
218      */

219     public boolean equals(Object JavaDoc object) {
220         if (object instanceof PackageFragmentRootSourceLocation) {
221              PackageFragmentRootSourceLocation root = (PackageFragmentRootSourceLocation)object;
222              if (getPackageFragmentRoot() == null) {
223                 return root.getPackageFragmentRoot() == null;
224              }
225              return getPackageFragmentRoot().equals(root.getPackageFragmentRoot());
226         }
227         return false;
228     }
229     
230     /* (non-Javadoc)
231      * @see java.lang.Object#hashCode()
232      */

233     public int hashCode() {
234         if (getPackageFragmentRoot() == null) {
235             return getClass().hashCode();
236         }
237         return getPackageFragmentRoot().hashCode();
238     }
239 }
240
Popular Tags