KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > JavaLaunchConfigurationUtils


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.launching;
12
13
14 import java.io.ByteArrayOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 import javax.xml.transform.OutputKeys JavaDoc;
18 import javax.xml.transform.Transformer JavaDoc;
19 import javax.xml.transform.TransformerException JavaDoc;
20 import javax.xml.transform.TransformerFactory JavaDoc;
21 import javax.xml.transform.dom.DOMSource JavaDoc;
22 import javax.xml.transform.stream.StreamResult JavaDoc;
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 JavaDoc;
38
39 /**
40  * This class contains a number of static helper methods useful for the 'local Java' delegate.
41  */

42 public class JavaLaunchConfigurationUtils {
43                                                                                 
44     /**
45      * Return the <code>IType</code> referenced in the specified configuration and contained in
46      * the specified project or throw a <code>CoreException</code> whose message explains why
47      * this couldn't be done.
48      */

49     public static IType getMainType(ILaunchConfiguration configuration, IJavaProject javaProject) throws CoreException {
50         String JavaDoc mainTypeName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc)null);
51         return getMainType(mainTypeName, javaProject);
52     }
53     
54     /**
55      * Return the <code>IType</code> referenced by the specified name and contained in
56      * the specified project or throw a <code>CoreException</code> whose message explains why
57      * this couldn't be done.
58      */

59     public static IType getMainType(String JavaDoc 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); //$NON-NLS-1$
62
}
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); //$NON-NLS-1$
70
}
71         return mainType;
72     }
73     
74     /**
75      * Find the specified (fully-qualified) type name in the specified java project.
76      */

77     public static IType findType(IJavaProject javaProject, String JavaDoc mainTypeName) throws JavaModelException {
78         String JavaDoc pathStr= mainTypeName.replace('.', '/') + ".java"; //$NON-NLS-1$
79
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 JavaDoc 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     /**
94      * Throws a core exception with the given message and optional
95      * exception. The exception's status code will indicate an error.
96      *
97      * @param message error message
98      * @param exception cause of the error, or <code>null</code>
99      * @exception CoreException with the given message and underlying
100      * exception
101      */

102     protected static void abort(String JavaDoc message, Throwable JavaDoc exception, int code) throws CoreException {
103         throw new CoreException(new Status(IStatus.ERROR, LaunchingPlugin.getUniqueIdentifier(),
104           code, message, exception));
105     }
106
107     /**
108      * Serializes a XML document into a string - encoded in UTF8 format,
109      * with platform line separators.
110      *
111      * @param doc document to serialize
112      * @return the document as a string
113      */

114     public static String JavaDoc serializeDocument(Document JavaDoc doc) throws IOException JavaDoc, TransformerException JavaDoc {
115         ByteArrayOutputStream JavaDoc s= new ByteArrayOutputStream JavaDoc();
116         
117         TransformerFactory JavaDoc factory= TransformerFactory.newInstance();
118         Transformer JavaDoc transformer= factory.newTransformer();
119         transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
120
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
121

122         DOMSource JavaDoc source= new DOMSource JavaDoc(doc);
123         StreamResult JavaDoc outputTarget= new StreamResult JavaDoc(s);
124         transformer.transform(source, outputTarget);
125         
126         return s.toString("UTF8"); //$NON-NLS-1$
127
}
128 }
129
Popular Tags