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

📄 replaceableparametersprocessor.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2006 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.model;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This class provides processing for a string containing replaceable
 * values.  This class supports strings that are formatted as follows:
 * <pre>
 * [%heapsize%|-Xheapsize:%heapsize%] [%securityDomain%|-Xdomain:%securityDomain%] %userSpecifiedArguments%
 * </pre>
 * Strings specified within '%' characters are replaceable.  The string
 * between the characters provides the name of a property to be found
 * within the provided properties object.  Expressions of the form
 * <code>[%value1%|optional expression]</code> specify an optional expression.  If the 
 * result of evaluating <code>%value1%</code> is non-null and has a length
 * greater than zero, the entire optional expression with be replaced with the
 * value of <code>optional expression</code>.  If the result of evaluating
 * <code>%value1%</code> is <code>null</code> or an empty string, the entire
 * conditional expression will be removed.
 * <p />
 * Copyright (c) 2003-2006 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.1 $
 * <br>
 * $Date: 2006/02/11 21:26:57 $
 * <br>
 * @author Craig Setera
 */
public class ReplaceableParametersProcessor {
	// Regular expression for matching replaceable optional values within
	// a device command-line.
	private static Pattern OPTIONAL_BLOCK_PATTERN = Pattern.compile("\\[%(.*?)%\\|(.*?)\\]");

	// Regular expression for matching replaceable values within
	// a device XML property file.
	private static Pattern PROPS_PATTERN = Pattern.compile("%(.*?)%");

	private static final String TEMP_PROP = "__eclipsemeTemp";
	
	/**
	 * Process the replaceable values in the specified string with values
	 * from the replacement values provided.
	 * 
	 * @param replaceableString
	 * @param replacementValues
	 * @return
	 */
	public static String processReplaceableValues(String replaceableString, Map replacementValues) {
		// Stuff the replaceable string into the values and pull it out later
		replacementValues.put(TEMP_PROP, replaceableString);
		String resolved = getResolvedPropertyValue(TEMP_PROP, replacementValues);
		replacementValues.remove(TEMP_PROP);
		
		return resolved;
	}
	
	/**
	 * Get the property value correctly resolved.
	 * 
	 * @param propValue
	 * @param resolvedValueCache
	 * @return
	 */
	private static String getResolvedPropertyValue(String key, Map values) {
		Object valueObject = values.get(key);
		String value = (valueObject == null) ? "" : valueObject.toString(); 

		StringBuffer sb = new StringBuffer(value);
		
		// First, resolve the optional blocks
		resolveOptionalBlocks(sb, values);

		// Now, resolve the rest of the remaining values
		resolvePropertyValues(sb, values);
		
		value = sb.toString();
		values.put(key, value);

		return value;
	}

	/**
	 * Resolve the property references that are found in the string buffer.
	 * 
	 * @param sb
	 * @param values
	 */
	private static void resolvePropertyValues(StringBuffer sb, Map values) {
		int offset = 0;
		
		Matcher matcher = PROPS_PATTERN.matcher(sb);
		if (matcher.find()) {
			while (matcher.find(offset)) {
				String propertyName = matcher.group(1);
				String propertyValue = getResolvedPropertyValue(propertyName, values);

				if (propertyValue != null) {
					// Figure out the new offset, based on the replaced
					// string length
					sb.replace(matcher.start(), matcher.end(), propertyValue);
					offset = matcher.start() + propertyValue.length();
				}
			}
		}
	}

	/**
	 * Resolve the optional blocks that are found in the
	 * string buffer.
	 * 
	 * @param sb
	 * @param values
	 */
	private static void resolveOptionalBlocks(StringBuffer sb, Map values) {
		int offset = 0;
		Matcher matcher = OPTIONAL_BLOCK_PATTERN.matcher(sb);
		if (matcher.find()) {
			while (matcher.find(offset)) {
				String booleanProperty = matcher.group(1);
				String blockValue = matcher.group(2);
				String booleanPropertyValue = getResolvedPropertyValue(booleanProperty, values);

				String replacement = "";
				if ((booleanPropertyValue != null) && (booleanPropertyValue.length() > 0)) {
					replacement = blockValue;
				}
				
				// Figure out the new offset, based on the replaced
				// string length
				sb.replace(matcher.start(), matcher.end(), replacement);
				offset = matcher.start() + replacement.length();
			}
		}
	}

	/**
	 * Static utility functions only.
	 */
	private ReplaceableParametersProcessor() {
		super();
	}
}

⌨️ 快捷键说明

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