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 import java.util.ArrayList ; 18 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.ParserConfigurationException ; 22 import javax.xml.transform.TransformerException ; 23 24 import org.eclipse.core.resources.IProject; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IStatus; 28 import org.eclipse.core.runtime.PlatformObject; 29 import org.eclipse.core.runtime.Status; 30 import org.eclipse.jdt.core.IJavaProject; 31 import org.eclipse.jdt.core.IPackageFragmentRoot; 32 import org.eclipse.jdt.core.JavaCore; 33 import org.eclipse.jdt.core.JavaModelException; 34 import org.eclipse.jdt.internal.launching.LaunchingMessages; 35 import org.eclipse.jdt.internal.launching.LaunchingPlugin; 36 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.helpers.DefaultHandler ; 42 43 61 public class JavaProjectSourceLocation extends PlatformObject implements IJavaSourceLocation { 62 63 66 private IJavaProject fProject; 67 68 71 private IJavaSourceLocation[] fRootLocations = null; 72 73 77 public JavaProjectSourceLocation() { 78 } 79 80 86 public JavaProjectSourceLocation(IJavaProject project) { 87 setJavaProject(project); 88 } 89 90 93 public Object findSourceElement(String name) throws CoreException { 94 if (fRootLocations != null) { 95 for (int i = 0; i < fRootLocations.length; i++) { 96 Object element = fRootLocations[i].findSourceElement(name); 97 if (element != null) { 98 return element; 99 } 100 } 101 } 102 return null; 103 } 104 105 111 private void setJavaProject(IJavaProject project) { 112 fProject = project; 113 fRootLocations = null; 114 if (fProject != null) { 115 try { 116 IPackageFragmentRoot[] roots = project.getPackageFragmentRoots(); 117 ArrayList list = new ArrayList (roots.length); 118 119 for (int i = 0; i < roots.length; i++) { 120 if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) { 121 list.add(new PackageFragmentRootSourceLocation(roots[i])); 122 } 123 } 124 fRootLocations = (IJavaSourceLocation[])list.toArray(new IJavaSourceLocation[list.size()]); 125 } catch (JavaModelException e) { 126 LaunchingPlugin.log(e); 127 } 128 } 129 } 130 131 137 public IJavaProject getJavaProject() { 138 return fProject; 139 } 140 141 144 public boolean equals(Object object) { 145 return object instanceof JavaProjectSourceLocation && 146 getJavaProject().equals(((JavaProjectSourceLocation)object).getJavaProject()); 147 } 148 149 152 public int hashCode() { 153 return getJavaProject().hashCode(); 154 } 155 156 159 public String getMemento() throws CoreException { 160 try { 161 Document doc = LaunchingPlugin.getDocument(); 162 Element node = doc.createElement("javaProjectSourceLocation"); doc.appendChild(node); 164 node.setAttribute("name", getJavaProject().getElementName()); return LaunchingPlugin.serializeDocument(doc); 166 } catch (IOException e) { 167 abort(MessageFormat.format(LaunchingMessages.JavaProjectSourceLocation_Unable_to_create_memento_for_Java_project_source_location__0__1, new String [] {getJavaProject().getElementName()}), e); 168 } catch (ParserConfigurationException e) { 169 abort(MessageFormat.format(LaunchingMessages.JavaProjectSourceLocation_Unable_to_create_memento_for_Java_project_source_location__0__1, new String [] {getJavaProject().getElementName()}), e); 170 } catch (TransformerException e) { 171 abort(MessageFormat.format(LaunchingMessages.JavaProjectSourceLocation_Unable_to_create_memento_for_Java_project_source_location__0__1, new String [] {getJavaProject().getElementName()}), e); 172 } 173 return null; 175 } 176 177 180 public void initializeFrom(String memento) throws CoreException { 181 Exception ex = null; 182 try { 183 Element root = null; 184 DocumentBuilder parser = 185 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 186 parser.setErrorHandler(new DefaultHandler ()); 187 StringReader reader = new StringReader (memento); 188 InputSource source = new InputSource (reader); 189 root = parser.parse(source).getDocumentElement(); 190 191 String name = root.getAttribute("name"); if (isEmpty(name)) { 193 abort(LaunchingMessages.JavaProjectSourceLocation_Unable_to_initialize_source_location___missing_project_name_3, null); 194 } else { 195 IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(name); 196 setJavaProject(JavaCore.create(proj)); 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(LaunchingMessages.JavaProjectSourceLocation_Exception_occurred_initializing_source_location__4, ex); 207 } 208 209 private boolean isEmpty(String string) { 210 return string == null || string.length() == 0; 211 } 212 213 216 private void abort(String message, Throwable e) throws CoreException { 217 IStatus s = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR, message, e); 218 throw new CoreException(s); 219 } 220 221 } 222 | Popular Tags |