1 11 package org.eclipse.jdt.internal.launching; 12 13 14 import java.io.ByteArrayOutputStream ; 15 import java.io.IOException ; 16 17 import javax.xml.transform.OutputKeys ; 18 import javax.xml.transform.Transformer ; 19 import javax.xml.transform.TransformerException ; 20 import javax.xml.transform.TransformerFactory ; 21 import javax.xml.transform.dom.DOMSource ; 22 import javax.xml.transform.stream.StreamResult ; 23 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.Path; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.debug.core.ILaunchConfiguration; 29 import org.eclipse.jdt.core.IClassFile; 30 import org.eclipse.jdt.core.ICompilationUnit; 31 import org.eclipse.jdt.core.IJavaElement; 32 import org.eclipse.jdt.core.IJavaProject; 33 import org.eclipse.jdt.core.IType; 34 import org.eclipse.jdt.core.JavaModelException; 35 import org.eclipse.jdt.core.Signature; 36 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 37 import org.w3c.dom.Document ; 38 39 42 public class JavaLaunchConfigurationUtils { 43 44 49 public static IType getMainType(ILaunchConfiguration configuration, IJavaProject javaProject) throws CoreException { 50 String mainTypeName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String )null); 51 return getMainType(mainTypeName, javaProject); 52 } 53 54 59 public static IType getMainType(String mainTypeName, IJavaProject javaProject) throws CoreException { 60 if ((mainTypeName == null) || (mainTypeName.trim().length() < 1)) { 61 abort(LaunchingMessages.JavaLaunchConfigurationUtils_Main_type_not_specified_3, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE); } 63 IType mainType = null; 64 try { 65 mainType = findType(javaProject, mainTypeName); 66 } catch (JavaModelException jme) { 67 } 68 if (mainType == null) { 69 abort(LaunchingMessages.JavaLaunchConfigurationUtils_Main_type_does_not_exist_4, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE); } 71 return mainType; 72 } 73 74 77 public static IType findType(IJavaProject javaProject, String mainTypeName) throws JavaModelException { 78 String pathStr= mainTypeName.replace('.', '/') + ".java"; IJavaElement javaElement= javaProject.findElement(new Path(pathStr)); 80 if (javaElement == null) { 81 return null; 82 } else if (javaElement instanceof IType) { 83 return (IType)javaElement; 84 } else if (javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) { 85 String simpleName= Signature.getSimpleName(mainTypeName); 86 return ((ICompilationUnit) javaElement).getType(simpleName); 87 } else if (javaElement.getElementType() == IJavaElement.CLASS_FILE) { 88 return ((IClassFile) javaElement).getType(); 89 } 90 return null; 91 } 92 93 102 protected static void abort(String message, Throwable exception, int code) throws CoreException { 103 throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(), 104 code, message, exception)); 105 } 106 107 114 public static String serializeDocument(Document doc) throws IOException , TransformerException { 115 ByteArrayOutputStream s= new ByteArrayOutputStream (); 116 117 TransformerFactory factory= TransformerFactory.newInstance(); 118 Transformer transformer= factory.newTransformer(); 119 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 122 DOMSource source= new DOMSource (doc); 123 StreamResult outputTarget= new StreamResult (s); 124 transformer.transform(source, outputTarget); 125 126 return s.toString("UTF8"); } 128 } 129 | Popular Tags |