路径字符替换(来自Spring源码)

/**
 * Abstract base class for PropertyEditors that need
 * to resolve placeholders in paths.
 *
 * <p>A path may contain ${...} placeholders, to be resolved as
 * system properties: e.g. ${user.dir}.
 *
 * @author Juergen Hoeller
 * @since 1.1.2
 * @see #PLACEHOLDER_PREFIX
 * @see #PLACEHOLDER_SUFFIX
 * @see System#getProperty(String)
 */
public class AbstractPathResolvingPropertyEditor extends PropertyEditorSupport {
	
	public static final String PLACEHOLDER_PREFIX = "${";

	public static final String PLACEHOLDER_SUFFIX = "}";

	protected static final Log logger = LogFactory.getLog(
			AbstractPathResolvingPropertyEditor.class);

	/**
	 * Resolve the given path, replacing ${...} placeholders with
	 * corresponding system property values if necessary.
	 * @param path the original file path
	 * @return the resolved file path
	 * @see #PLACEHOLDER_PREFIX
	 * @see #PLACEHOLDER_SUFFIX
	 */
    protected String resolvePath(String path) {
		StringBuilder buf = new StringBuilder(path);

		// The following code does not use JDK 1.4's StringBuffer.indexOf(String)
		// method to retain JDK 1.3 compatibility. The slight loss in performance
		// is not really relevant, as this code will typically just run on startup.
		int startIndex = path.indexOf(PLACEHOLDER_PREFIX);
		while (startIndex != -1) {
			int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX, 
					startIndex + PLACEHOLDER_PREFIX.length());
			if (endIndex != -1) {
				String placeholder = buf.substring(
						startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
				String propVal = System.getProperty(placeholder);
				if (propVal != null) {
					buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), 
							propVal);
					startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, 
							startIndex + propVal.length());
				} else {
					logger.warn("Could not resolve placeholder '" + 
							placeholder + "' in resource path [" + 
							path + "] as system property");
					startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, 
							endIndex + PLACEHOLDER_SUFFIX.length());
				}
			} else {
				startIndex = -1;
			}
		}
		return buf.toString();
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值