1 11 package org.eclipse.jdt.launching.sourcelookup; 12 13 import java.io.File ; 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.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 ; 35 import org.w3c.dom.Element ; 36 import org.xml.sax.InputSource ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.helpers.DefaultHandler ; 39 40 57 public class DirectorySourceLocation extends PlatformObject implements IJavaSourceLocation { 58 59 62 private File fDirectory; 63 64 68 public DirectorySourceLocation() { 69 } 70 71 77 public DirectorySourceLocation(File directory) { 78 setDirectory(directory); 79 } 80 81 84 public Object findSourceElement(String name) throws CoreException { 85 if (getDirectory() == null) { 86 return null; 87 } 88 89 String pathStr= name.replace('.', '/'); 90 int lastSlash = pathStr.lastIndexOf('/'); 91 try { 92 IPath root = new Path(getDirectory().getCanonicalPath()); 93 boolean possibleInnerType = false; 94 String typeName = pathStr; 95 do { 96 IPath filePath = root.append(new Path(typeName + ".java")); File 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 e) { 110 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION); 111 } 112 return null; 113 } 114 115 121 private void setDirectory(File directory) { 122 fDirectory = directory; 123 } 124 125 131 public File getDirectory() { 132 return fDirectory; 133 } 134 135 138 public boolean equals(Object object) { 139 return object instanceof DirectorySourceLocation && 140 getDirectory().equals(((DirectorySourceLocation)object).getDirectory()); 141 } 142 143 146 public int hashCode() { 147 return getDirectory().hashCode(); 148 } 149 150 153 public String getMemento() throws CoreException { 154 try { 155 Document doc = LaunchingPlugin.getDocument(); 156 Element node = doc.createElement("directorySourceLocation"); doc.appendChild(node); 158 node.setAttribute("path", getDirectory().getAbsolutePath()); return LaunchingPlugin.serializeDocument(doc); 160 } catch (IOException e) { 161 abort(MessageFormat.format(LaunchingMessages.DirectorySourceLocation_Unable_to_create_memento_for_directory_source_location__0__1, new String [] {getDirectory().getAbsolutePath()}), e); 162 } catch (ParserConfigurationException e) { 163 abort(MessageFormat.format(LaunchingMessages.DirectorySourceLocation_Unable_to_create_memento_for_directory_source_location__0__1, new String [] {getDirectory().getAbsolutePath()}), e); 164 } catch (TransformerException e) { 165 abort(MessageFormat.format(LaunchingMessages.DirectorySourceLocation_Unable_to_create_memento_for_directory_source_location__0__1, new String [] {getDirectory().getAbsolutePath()}), e); 166 } 167 return null; 169 } 170 171 174 public void initializeFrom(String memento) throws CoreException { 175 Exception ex = null; 176 try { 177 Element root = null; 178 DocumentBuilder parser = 179 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 180 parser.setErrorHandler(new DefaultHandler ()); 181 StringReader reader = new StringReader (memento); 182 InputSource source = new InputSource (reader); 183 root = parser.parse(source).getDocumentElement(); 184 185 String path = root.getAttribute("path"); if (isEmpty(path)) { 187 abort(LaunchingMessages.DirectorySourceLocation_Unable_to_initialize_source_location___missing_directory_path_3, null); 188 } else { 189 File dir = new File (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 [] {path}), null); 194 } 195 } 196 return; 197 } catch (ParserConfigurationException e) { 198 ex = e; 199 } catch (SAXException e) { 200 ex = e; 201 } catch (IOException e) { 202 ex = e; 203 } 204 abort(LaunchingMessages.DirectorySourceLocation_Exception_occurred_initializing_source_location__5, ex); 205 } 206 207 private boolean isEmpty(String string) { 208 return string == null || string.length() == 0; 209 } 210 211 214 private void abort(String message, Throwable 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 |