路径字符替换(来自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();
	}

}

你可能感兴趣的:(spring)