package org.eclipse.jdt.launching.sourcelookup;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.launching.LaunchingMessages;
import org.eclipse.jdt.internal.launching.LaunchingPlugin;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
@Deprecated
public class PackageFragmentRootSourceLocation extends PlatformObject implements IJavaSourceLocation {
private IPackageFragmentRoot fRoot = null;
public PackageFragmentRootSourceLocation() {
}
public PackageFragmentRootSourceLocation(IPackageFragmentRoot root) {
setPackageFragmentRoot(root);
}
@Override
public Object findSourceElement(String name) throws CoreException {
if (name != null && getPackageFragmentRoot() != null) {
IPackageFragment pkg = null;
int index = name.lastIndexOf('.');
if (index >= 0) {
String fragment = name.substring(0, index);
pkg = getPackageFragmentRoot().getPackageFragment(fragment);
name = name.substring(index + 1);
} else {
pkg = getPackageFragmentRoot().getPackageFragment("");
}
if (pkg.exists()) {
boolean possibleInnerType = false;
String typeName = name;
do {
ICompilationUnit cu = pkg.getCompilationUnit(typeName + ".java");
if (cu.exists()) {
return cu;
}
IClassFile cf = pkg.getClassFile(typeName + ".class");
if (cf.exists()) {
return cf;
}
index = typeName.lastIndexOf('$');
if (index >= 0) {
typeName = typeName.substring(0, index);
possibleInnerType = true;
} else {
possibleInnerType = false;
}
} while (possibleInnerType);
}
}
return null;
}
@Override
public String getMemento() throws CoreException {
Document doc = DebugPlugin.newDocument();
Element node = doc.createElement("javaPackageFragmentRootSourceLocation");
doc.appendChild(node);
String handle = "";
if (getPackageFragmentRoot() != null) {
handle = getPackageFragmentRoot().getHandleIdentifier();
}
node.setAttribute("handleId", handle);
return DebugPlugin.serializeDocument(doc);
}
@Override
public void initializeFrom(String memento) throws CoreException {
Exception ex = null;
try {
Element root = null;
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
parser.setErrorHandler(new DefaultHandler());
StringReader reader = new StringReader(memento);
InputSource source = new InputSource(reader);
root = parser.parse(source).getDocumentElement();
String handle = root.getAttribute("handleId");
if (handle == null) {
abort(LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_initialize_source_location___missing_handle_identifier_for_package_fragment_root__6, null);
} else {
if (handle.length() == 0) {
setPackageFragmentRoot(null);
} else {
IJavaElement element = JavaCore.create(handle);
if (element instanceof IPackageFragmentRoot) {
setPackageFragmentRoot((IPackageFragmentRoot)element);
} else {
abort(LaunchingMessages.PackageFragmentRootSourceLocation_Unable_to_initialize_source_location___package_fragment_root_does_not_exist__7, null);
}
}
}
return;
} catch (ParserConfigurationException e) {
ex = e;
} catch (SAXException e) {
ex = e;
} catch (IOException e) {
ex = e;
}
abort(LaunchingMessages.PackageFragmentRootSourceLocation_Exception_occurred_initializing_source_location__8, ex);
}
public IPackageFragmentRoot getPackageFragmentRoot() {
return fRoot;
}
private void setPackageFragmentRoot(IPackageFragmentRoot root) {
fRoot = root;
}
private void abort(String message, Throwable e) throws CoreException {
IStatus s = new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), IJavaLaunchConfigurationConstants.ERR_INTERNAL_ERROR, message, e);
throw new CoreException(s);
}
@Override
public boolean equals(Object object) {
if (object instanceof PackageFragmentRootSourceLocation) {
PackageFragmentRootSourceLocation root = (PackageFragmentRootSourceLocation)object;
if (getPackageFragmentRoot() == null) {
return root.getPackageFragmentRoot() == null;
}
return getPackageFragmentRoot().equals(root.getPackageFragmentRoot());
}
return false;
}
@Override
public int hashCode() {
if (getPackageFragmentRoot() == null) {
return getClass().hashCode();
}
return getPackageFragmentRoot().hashCode();
}
}