⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.core.internal.utils;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.IStreamListener;
import org.eclipse.debug.core.Launch;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.IStreamMonitor;
import org.eclipse.debug.core.model.IStreamsProxy;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.BinaryType;
import org.eclipse.jdt.internal.core.JavaModel;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.osgi.service.datalocation.Location;

import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.internal.EclipseMECorePlugin;

/**
 * Various utility functions.
 * <p />
 * Copyright (c) 2003-2005 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.25 $
 * <br>
 * $Date: 2006/11/26 21:41:57 $
 * <br>
 * @author Craig Setera
 */
public class Utils {
	// TODO This should probably be handled better... 
	private static final String PRIVATE_CONFIGURATION =
		"org.eclipse.debug.ui.private";
	
	// Private default file filter
	private static final FileFilter DEFAULT_FILTER = new FileFilter() {
		public boolean accept(File pathname) {
			// Default is to copy all files
			return true;
		}
	};

	// Copy buffer
	private static final byte[] buffer = new byte[1024];
	
	// Track whether the file system is case sensitive
	private static boolean caseSensitivityChecked;
	private static boolean caseSensitiveFileSystem;
	
	/**
	 * Return a boolean indicating whether the specified paths
	 * point to the same file.
	 * 
	 * @param path1
	 * @param path2
	 * @return
	 * @throws CoreException 
	 */
	public static boolean arePathTargetsEqual(IPath path1, IPath path2) 
		throws CoreException
	{
		boolean equal = false;
		
		File file1 = getPathTargetFile(path1);
		File file2 = getPathTargetFile(path2);
		
		if ((file1 != null) && (file2 != null)) {
			equal = file1.equals(file2);
		}
		
		return equal;
	}

	/**
	 * Clear the specified container of all resources recursively.
	 * 
	 * @param container
	 * @param monitor
	 * @throws CoreException
	 */
	public static void clearContainer(IContainer container, IProgressMonitor monitor)
		throws CoreException
	{
		if (container.exists()) {
			IResource[] resources = container.members();
			for (int i = 0; i < resources.length; i++) {
				IResource resource = resources[i];
				if (resource instanceof IContainer) {
					clearContainer((IContainer) resource, monitor);
					resource.delete(true, monitor);
				} else {
					resource.delete(true, monitor);
				}
			}
		}
	}

	/**
	 * Copy from the specified source to the specified destination, 
	 * recursively as necessary.
	 * 
	 * @param source
	 * @param destination
	 * @throws IOException
	 */
	public static void copy(File source, File destination)
		throws IOException
	{
		copy(source, destination, null);
	}

	/**
	 * Copy from the specified source to the specified destination, 
	 * recursively as necessary.  The file filter will be used to
	 * determine what files will be copied along the way.
	 * 
	 * @param source
	 * @param destination
	 * @param filter
	 * @throws IOException
	 */
	public static void copy(File source, File destination, FileFilter filter)
		throws IOException
	{
		if (source.exists()) {
			if (source.isDirectory()) {
				copyDirectory(source, destination, filter);
			} else {
				copyFile(source, destination, filter);
			}
		}
	}
	
	/**
	 * Copy the specified directory and all of its contents recursively
	 * to the specified destination.
	 * 
	 * @param source
	 * @param destination
	 * @param filter 
	 * @throws IOException
	 */
	public static void copyDirectory(File source, File destination, FileFilter filter) 
		throws IOException
	{
		if (filter == null) filter = DEFAULT_FILTER;
		
		if (source.exists() && source.isDirectory() && filter.accept(source)) {
			destination.mkdirs();
			
			File[] sources = source.listFiles();
			for (int i = 0; i < sources.length; i++) {
				File newSource = sources[i];
				File newDestination = new File(destination, newSource.getName());
				
				if (newSource.isDirectory()) {
					copyDirectory(newSource, newDestination, filter);
				} else {
					copyFile(newSource, newDestination, filter);
				}
			}
		}
	}

	/**
	 * Copy the specified source file into the specified target file.
	 * 
	 * @param sourceFile
	 * @param targetFile
	 * @throws IOException
	 * @throws CoreException 
	 */
	public static void copyFile(IFile sourceFile, IFile targetFile)
		throws IOException, CoreException
	{
		copyFile(
			sourceFile.getLocation().toFile(), 
			targetFile.getLocation().toFile(),
			null);
	}
	
	/**
	 * Copy the specified source file into the specified target file.
	 * 
	 * @param sourceFile
	 * @param targetFile
	 * @param filter 
	 * @throws IOException
	 */
	public static void copyFile(File sourceFile, File targetFile, FileFilter filter)
		throws IOException
	{
		if (filter == null) filter = DEFAULT_FILTER;
		if (filter.accept(sourceFile)) {
			FileInputStream fis = new FileInputStream(sourceFile);
			FileOutputStream fos = new FileOutputStream(targetFile);
			copyInputToOutput(fis, fos);
			fis.close();
			fos.close();
		}
	}
	
	/**
	 * Copy the contents of the input to the output stream.
	 * 
	 * @param input
	 * @param output
	 * @throws IOException
	 */
	public static void copyInputToOutput(InputStream input, OutputStream output)
		throws IOException
	{
		int bytesRead = 0;
		
		do {
			bytesRead = input.read(buffer, 0, buffer.length);
			if (bytesRead > 0) {
				output.write(buffer, 0, bytesRead);
			}
		} while (bytesRead != -1);
		
		output.flush();
	}
	
	/**
	 * Create a new zip archive with the contents specified by the source folder.
	 * 
	 * @param tgtArchiveFile
	 * @param srcFolder
	 * @throws IOException
	 */
	public static void createArchive(File tgtArchiveFile, File srcFolder) 
		throws IOException
	{
		if (srcFolder.exists() && srcFolder.isDirectory()) {
			ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tgtArchiveFile));
			try {
				addFolderToArchive(zos, srcFolder.getAbsolutePath().length() + 1, srcFolder);
			} finally {
				if (zos != null) {
					try { zos.close(); } catch (IOException e) {}
				}
			}
		}
	}

	/**
	 * Delete the specified file, recursively as necessary.
	 * 
	 * @param file
	 */
	public static void delete(File file) {
		if (file.exists()) {
			if (file.isDirectory()) {
				File[] files = file.listFiles();
				for (int i = 0; i < files.length; i++) {
					delete(files[i]);
				}
			}
			
			file.delete();
		}
	}

	/**
	 * If the system property "eclipseme.dump.launch" is set to "true",
	 * dump the command line that was used to launch the emulator.
	 */
	public static void dumpCommandLine(ILaunch launch) {
		// Search for the process command-line
		IProcess[] processes = launch.getProcesses();
		if ((processes != null) && (processes.length > 0)) {
			IProcess process = processes[0];
			if (process != null) {
				String commandLine = process.getAttribute(IProcess.ATTR_CMDLINE);
				dumpCommandLine(commandLine);
			}
		}
	}

	/**
	 * If the system property "eclipseme.dump.launch" is set to "true",
	 * dump the command line that was used to launch the emulator.
	 * 
	 * @param commandLine
	 */
	public static void dumpCommandLine(String commandLine) {
		// Pull the dump choice from system properties
		String propValue =
			System.getProperty(IEclipseMECoreConstants.PROP_DUMP_LAUNCH, "false");
		boolean doDump = propValue.equalsIgnoreCase("TRUE");
	
		// Only do this if requested
		if (doDump) {
			// Let the user know what happened via the log file
			String text = (commandLine == null) ?
				"Command line not found" :
				"Command line: " + commandLine;
			EclipseMECorePlugin.log(IStatus.INFO, text);
//			if (commandLine != null) {
//				EclipseMECorePlugin.log(IStatus.INFO, "" + commandLine.length());
//			}
		}		
	}
	
	/**
	 * If the system property "eclipseme.dump.launch" is set to "true",
	 * dump the command line that was used to launch the emulator.
	 * 
	 * @param commandLine
	 */
	public static void dumpCommandLine(String[] commandLine) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < commandLine.length; i++) {
			String string = commandLine[i];
			if (i != 0) sb.append(' ');
			sb.append(string);
		}

		dumpCommandLine(sb.toString());
	}

	/**
	 * Exec a new process via the debug plugin.
	 * 
	 * @param commandLine
	 * @param workingDirectory
	 * @param env The environment to be used or <code>null</code>
	 * to take the default.
	 * @return
	 * @throws CoreException
	 */
	public static Process exec(String[] commandLine, File workingDirectory, String[] env) 
		throws CoreException
	{
		return DebugPlugin.exec(commandLine, workingDirectory, env);
	}
	
	/**
	 * Test if the specified executable file exist.  Add a ".exe"
	 * if necessary to handle Windows systems.
	 * 
	 * @param executableFile
	 * @return
	 */
	public static boolean executableExists(File executableFile) {
		File winExecutable = new File(
			executableFile.getParentFile(), 
			executableFile.getName() + ".exe");
		
		return 
			executableFile.exists() ||
			winExecutable.exists();
	}

	/**
	 * Extract the archive specified by the file into the provided directory.
	 * 
	 * @param jarFile
	 * @param tgtDirectory
	 * @throws IOException 
	 */
	public static void extractArchive(File jarFile, File tgtDirectory) 
		throws IOException 
	{
		ZipInputStream zis = new ZipInputStream(new FileInputStream(jarFile));
		try {
			ZipEntry entry = null;
			while ((entry = zis.getNextEntry()) != null) {
				if (entry.isDirectory()) {
					File directory = new File(tgtDirectory, entry.getName());
					directory.mkdirs();
				} else {
					File file = new File(tgtDirectory, entry.getName());
					file.getParentFile().mkdirs();
					FileOutputStream fos = new FileOutputStream(file);
					try {
						Utils.copyInputToOutput(zis, fos);
					} finally {
						if (fos != null) {
							try { fos.close(); } catch (IOException e) {}
						}
					}
				}
			}
		} finally {
			try { zis.close(); } catch (IOException e) {}
		}
	}
	/**
	 * Extract the class name from the specified
	 * IResource within the specified java project.
	 * 
	 * @param javaProject the java project to provide the relative name
	 * @param resource the resource to extract a class name
	 * @return the class name or <code>null</code> if the resource
	 * name cannot be converted for some reason.
	 * @throws JavaModelException
	 */
	public static String extractClassName(
		IJavaProject javaProject, 
		IResource resource) 
			throws JavaModelException 
	{
		IPath classPath = extractsSourceFolderRelativePath(javaProject, resource);
		return (classPath == null) ? null :
			classPath.removeFileExtension().toString().replace('/', '.');
	}

	/**
	 * Extract the class path from the specified
	 * IResource within the specified java project.
	 * 
	 * @param javaProject
	 * @param resource
	 * @return the extracted resource path
	 * @throws JavaModelException
	 */
	public static IPath extractsSourceFolderRelativePath(
		IJavaProject javaProject, 
		IResource resource) 
			throws JavaModelException 
	{
		IPath resultPath = null;
		IPath projectOutputPath =
			javaProject.getOutputLocation().makeAbsolute();
				
		IPath resourcePath = resource.getFullPath();
			
		IClasspathEntry[] classpath = javaProject.getRawClasspath();
		for (int i = 0; i < classpath.length; i++) {
			IClasspathEntry entry = classpath[i];
			if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -