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

📄 directivefactory.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * 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 org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;

import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.internal.preprocessor.directive.DefineDirective;
import eclipseme.core.internal.preprocessor.directive.ElifDirective;
import eclipseme.core.internal.preprocessor.directive.ElifdefDirective;
import eclipseme.core.internal.preprocessor.directive.ElifndefDirective;
import eclipseme.core.internal.preprocessor.directive.ElseDirective;
import eclipseme.core.internal.preprocessor.directive.EndifDirective;
import eclipseme.core.internal.preprocessor.directive.EndincludeDirective;
import eclipseme.core.internal.preprocessor.directive.IfDirective;
import eclipseme.core.internal.preprocessor.directive.IfdefDirective;
import eclipseme.core.internal.preprocessor.directive.IfndefDirective;
import eclipseme.core.internal.preprocessor.directive.IncludeDirective;
import eclipseme.core.internal.preprocessor.directive.UndefineDirective;

/**
 * Factory for the creation of Directive instances.
 * <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.3 $
 * <br>
 * $Date: 2006/11/12 01:11:00 $
 * <br>
 * @author Craig Setera
 */
public class DirectiveFactory {
	/** The directives that we recognize */
	private static final String[] DIRECTIVE_STRINGS = new String[] {
		"#define",
		"#undefine",
		"#ifdef",
		"#if",
		"#elif",
		"#ifndef",
		"#else",
		"#endif",
		"#elifdef",
		"#elifndef",
		"#include",
		"#endinclude",
	};
	
	/**
	 * Return a new PreprocessorDirective for the specified comment.  Returns <code>null</code>
	 * if the comment does not represent a directive.
	 * 
	 * @param resource
	 * @param document 
	 * @param comment
	 * @return
	 * @throws CoreException 
	 */
	public static Directive getDirective(
		IResource resource, 
		IDocument document, 
		Comment comment) 
			throws CoreException 
	{
		Directive directive = null;
		
		if (comment.getNodeType() == Comment.LINE_COMMENT) {
			String commentText = getCommentText(document, comment);
			String[] strings = commentText.split(" ");
			
			if ((strings != null) && (strings.length > 0)) {
				for (int i = 0; i < DIRECTIVE_STRINGS.length; i++) {
					if (strings[0].equals(DIRECTIVE_STRINGS[i])) {
						switch (i) {
							case 0:	// #define
								directive = new DefineDirective(resource, comment, commentText);
								break;
							case 1: // #undefine
								directive = new UndefineDirective(resource, comment, commentText);
								break;
							case 2: // #ifdef
								directive = new IfdefDirective(resource, comment, commentText);
								break;
							case 3: // #if
								directive = new IfDirective(resource, comment, commentText);
								break;
							case 4: // #elif
								directive = new ElifDirective(resource, comment, commentText);
								break;
							case 5: // #ifndef
								directive = new IfndefDirective(resource, comment, commentText);
								break;
							case 6: // #else
								directive = new ElseDirective(resource, comment, commentText);
								break;
							case 7: // #endif
								directive = new EndifDirective(resource, comment, commentText);
								break;
							case 8: // #elifdef
								directive = new ElifdefDirective(resource, comment, commentText);
								break;
							case 9: // #elifndef
								directive = new ElifndefDirective(resource, comment, commentText);
								break;
							case 10: // #include
								directive = new IncludeDirective(resource, comment, commentText);
								break;
							case 11: // #endinclude
								directive = new EndincludeDirective(resource, comment, commentText);
								break;
						}

						break;
					}
				}
			}
		}
		
		return directive;
	}
	
	/**
	 * Return the text of the specified comment.
	 * 
	 * @param document
	 * @param comment
	 * @return
	 */
	private static String getCommentText(IDocument document, Comment comment)
		throws CoreException
	{
		String commentText = "";
		
		try {
			int start = comment.getStartPosition() + 2;
			int documentLength = document.getLength();
			if (start < documentLength) {
				int end = start + comment.getLength();
				if (end <= documentLength) {
					commentText = document.get(start, comment.getLength()).trim();
				}
			}
		} catch (BadLocationException e) {
			EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
		}
		
		return commentText;
	}
	
	private DirectiveFactory() {}
}

⌨️ 快捷键说明

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