1 11 package org.eclipse.jdt.launching.sourcelookup; 12 13 14 import java.io.IOException ; 15 import java.io.StringReader ; 16 import com.ibm.icu.text.MessageFormat; 17 18 import javax.xml.parsers.DocumentBuilder ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import javax.xml.parsers.ParserConfigurationException ; 21 import javax.xml.transform.TransformerException ; 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 ; 37 import org.w3c.dom.Element ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.helpers.DefaultHandler ; 41 42 60 public class PackageFragmentRootSourceLocation extends PlatformObject implements IJavaSourceLocation { 61 62 65 private IPackageFragmentRoot fRoot = null; 66 67 70 public PackageFragmentRootSourceLocation() { 71 } 72 73 78 public PackageFragmentRootSourceLocation(IPackageFragmentRoot root) { 79 setPackageFragmentRoot(root); 80 } 81 82 85 public Object findSourceElement(String name) throws CoreException { 86 if (name != null && getPackageFragmentRoot() != null) { 87 IPackageFragment pkg = null; 88 int index = name.lastIndexOf('.'); 89 if (index >= 0) { 90 String fragment = name.substring(0, index); 91 pkg = getPackageFragmentRoot().getPackageFragment(fragment); 92 name = name.substring(index + 1); 93 } else { 94 pkg = getPackageFragmentRoot().getPackageFragment(""); } 96 if (pkg.exists()) { 97 boolean possibleInnerType = false; 98 String typeName = name; 99 do { 100 ICompilationUnit cu = pkg.getCompilationUnit(typeName + ".java"); if (cu.exists()) { 102 return cu; 103 } 104 IClassFile cf = pkg.getClassFile(typeName + ".class"); 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 124 public String getMemento() throws CoreException { 125 try { 126 Document doc = LaunchingPlugin.getDocument(); 127 Element node = doc.createElement("javaPackageFragmentRootSourceLocation"); doc.appendChild(node); 129 String handle = ""; if (getPackageFragmentRoot() != null) { 131 handle = getPackageFragmentRoot().getHandleIdentifier(); 132 } 133 node.setAttribute("handleId", handle); return LaunchingPlugin.serializeDocument(doc); 135 } catch (IOException e) { 136 abort(MessageFormat.format(LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_create_memento_for_package_fragment_root_source_location__0__5, new String [] {getPackageFragmentRoot().getElementName()}), e); 137 } catch (ParserConfigurationException e) { 138 abort(MessageFormat.format(LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_create_memento_for_package_fragment_root_source_location__0__5, new String [] {getPackageFragmentRoot().getElementName()}), e); 139 } catch (TransformerException e) { 140 abort(MessageFormat.format(LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_create_memento_for_package_fragment_root_source_location__0__5, new String [] {getPackageFragmentRoot().getElementName()}), e); 141 } 142 return null; 144 } 145 146 149 public void initializeFrom(String memento) throws CoreException { 150 Exception ex = null; 151 try { 152 Element root = null; 153 DocumentBuilder parser = 154 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 155 parser.setErrorHandler(new DefaultHandler ()); 156 StringReader reader = new StringReader (memento); 157 InputSource source = new InputSource (reader); 158 root = parser.parse(source).getDocumentElement(); 159 160 String handle = root.getAttribute("handleId"); 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 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 e) { 178 ex = e; 179 } catch (SAXException e) { 180 ex = e; 181 } catch (IOException e) { 182 ex = e; 183 } 184 abort(LaunchingMessages.PackageFragmentRootSourceLocation_Exception_occurred_initializing_source_location__8, ex); 185 } 186 187 194 public IPackageFragmentRoot getPackageFragmentRoot() { 195 return fRoot; 196 } 197 198 204 private void setPackageFragmentRoot(IPackageFragmentRoot root) { 205 fRoot = root; 206 } 207 208 211 private void abort(String message, Throwable 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 219 public boolean equals(Object 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 233 public int hashCode() { 234 if (getPackageFragmentRoot() == null) { 235 return getClass().hashCode(); 236 } 237 return getPackageFragmentRoot().hashCode(); 238 } 239 } 240 | Popular Tags |