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

📄 preprocessorbuilder.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.preprocessor;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourceAttributes;
import org.eclipse.core.resources.ResourcesPlugin;
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.Path;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.text.IDocument;

import eclipseme.core.BuildLoggingConfiguration;
import eclipseme.core.console.BuildConsoleProxy;
import eclipseme.core.hook.sourceMapper.SourceMapperAccess;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.utils.Utils;
import eclipseme.core.model.IMidletSuiteProject;
import eclipseme.core.model.MidletSuiteFactory;
import eclipseme.core.model.SymbolDefinitionSet;
import eclipseme.core.persistence.PersistenceException;


/**
 * An incremental builder implementation that provides preprocess
 * support as defined for the Antenna Ant tools.<br/>
 * <p>
The preprocessor supports the following directives inside a Java
		source file. All directives must immediately follow a "//" comment that
		starts at the beginning of a line (whitespace is allowed left of them, but
		no Java code). That way, they don't interfere with normal Java compilation.
		Directives must not span multiple lines of code.
		
		<p />

		<table class="color">
			<tr>
				<th width="30%">
		  			Directive
		  		</th>

				<th>
		  			Decription
		  		</th>
			</tr>

			<tr>
				<td>
		  			#define &lt;identifier&gt;

		  		</td>

				<td>
		  			Defines an identifier, thus making its value "true" when it is
		  			referenced in further expressions.
		  		</td>
			</tr>

			<tr>
				<td>
		  			#undefine &lt;identifier&gt;

		  		</td>

				<td>
		  			Undefines an identifier, thus making its value "false" when it is
		  			referenced in further expressions.
		  		</td>
			</tr>

			<tr>
				<td>
		  			#ifdef &lt;identifier&gt;

		  		</td>

				<td rowspan="8">
		  			The following lines are compiled only if the given identifier
		  			is defined (or undefined, in the case of an "#ifndef"
		  			directive). "#else" does exactly what your think it does. Each
		  			directive must be ultimately closed by an "#endif" directive.
		  			The "#elifdef" and "#elifndef" directives help to specify longer conditional
		  			cascades without having to nest each level.
		  			
		  			<p />
		  			
		  			The "#if" and "#elif" directives even allow to use complex expressions.
		  			These expressions are very much like Java boolean expressions. They
		  			may consist of identifiers, parentheses and the usual "&amp;&amp;", "||",
		  			"^", and "!" operators.
		  			
		  			<p />
		  			
		  			Please note that "#ifdef" and "#ifndef" don't
		  			support complex expressions. They only expect a single argument - the
		  			symbol to be checked.
		  		</td>
			</tr>

			<tr>
				<td>
		  			#ifndef &lt;identifier&gt;
		  		</td>
			</tr>


			<tr>

				<td>
		  			#else
		  		</td>
			</tr>

			<tr>
				<td>
		  			#endif
		  		</td>
			</tr>

			<tr>
				<td>
		  			#elifdef &lt;identifier&gt;
		  		</td>
			</tr>

			<tr>
				<td>

		  			#elifndef &lt;identifier&gt;
		  		</td>
			</tr>

			<tr>
				<td>
		  			#if &lt;expression&gt;

		  		</td>
			</tr>

			<tr>
				<td>
		  			#elif &lt;expression&gt;
		  		</td>
			</tr>

			<tr>
				<td>
		  			#include &lt;filename&gt;
		  		</td>

				<td rowspan="2">
		  			Includes the given source file at the current position. Must
		  			be terminated by "#endinclude" for technical reasons. The
		  			filename may also be given as an Ant-style property (${name}).
		  			The property needs to be defined in your build.xml file then. Note
		  			that relative file names are interpreted as relative to the
		  			build.xml file.
		  		</td>

			</tr>

			<tr>
				<td>
		  			#endinclude
		  		</td>
			</tr>

		</table>
 * <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.6 $
 * <br>
 * $Date: 2006/12/18 02:13:46 $
 * <br>
 * @author Craig Setera
 */
public class PreprocessorBuilder extends IncrementalProjectBuilder {
	
	// The subdirectory into which the processed output will go
	public static final String PROCESSED_DIRECTORY = ".processed";
	private static final String PROCESSED_PATH = "/" + PROCESSED_DIRECTORY;
	
	/**
	 * Return an appropriate output file for the specified resource.
	 * This file is not guaranteed to exist.
	 * 
	 * @param resource
	 * @return
	 */
	public static IFile getOutputFile(IResource resource) {
		IPath projectRelativePath = new Path(PROCESSED_PATH).append(resource.getProjectRelativePath());
		return resource.getProject().getFile(projectRelativePath);
	}

	/**
	 * Return the project that is to hold the preprocess output results for the
	 * specified project.
	 * 
	 * @param project
	 * @return
	 */
	public static IProject getOutputProject(IProject project) {
		String ppProjectName = "." + project.getName() + "_PP";
		return ResourcesPlugin.getWorkspace().getRoot().getProject(ppProjectName);
	}
	
	/**
	 * Return a boolean indicating whether or not the specified
	 * resource has already been preprocessed and should be ignored
	 * for further processing.
	 * 
	 * @param resource
	 * @return
	 */
	public static boolean isPreprocessed(IResource resource) {
		IPath projectPath = resource.getProjectRelativePath();
		return projectPath.segment(0).equals(PROCESSED_DIRECTORY);
	}

	/**
	 * Resource visitor for visiting java files for preprocessing.
	 */
	protected class ResourceVisitor implements IResourceVisitor {
		protected SymbolDefinitionSet symbols;
		protected IProgressMonitor monitor;
		
		protected ResourceVisitor(SymbolDefinitionSet symbols, IProgressMonitor monitor) {
			this.symbols = symbols;
			this.monitor = monitor;
		}
		
		public boolean visit(IResource resource) 
			throws CoreException 
		{
			if (shouldBeProcessed(resource, monitor)) {
				preprocess(resource, symbols, monitor);
			} 
	
			return true;
		}
	}

	/**
	 * Resource delta visitor for visiting changed java files for
	 * preprocessing.
	 */
	private class ResourceDeltaVisitor 
		extends ResourceVisitor 
		implements IResourceDeltaVisitor 
	{
		protected ResourceDeltaVisitor(SymbolDefinitionSet symbols, IProgressMonitor monitor) {
			super(symbols, monitor);
		}
		
		public boolean visit(IResourceDelta delta) 
			throws CoreException 
		{
			boolean value = false;
			
			switch (delta.getKind()) {
				case IResourceDelta.ADDED:
				case IResourceDelta.CHANGED:
					value = visit(delta.getResource());
					break;
					
				case IResourceDelta.REMOVED: {
						IFile outputFile = getOutputFile(delta.getResource());
						if (outputFile.exists()) {
							outputFile.delete(true, monitor);
						}
					}
					break;
					
				default:
					// Do nothing
			}
			
			return value;
		}
	}

	private BuildLoggingConfiguration buildLoggingConfig;
	
	// Tracker for a warning check
	private boolean checkedForHook;
	
	/**
	 * Construct a new builder instance. 
	 */

⌨️ 快捷键说明

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