/*
 * Copyright 2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Helper class for resolving placeholders in texts.
 * Usually applied to file paths.
 *
 * <p>A text may contain ${...} placeholders, to be resolved as
 * system properties: e.g. ${user.dir}.
 *
 * @author Juergen Hoeller
 * @since 1.2.5
 * @see #PLACEHOLDER_PREFIX
 * @see #PLACEHOLDER_SUFFIX
 * @see System#getProperty(String)
 */
public abstract class SystemPropertyUtils {

	/** Prefix for system property placeholders: "${" */
	public static final String PLACEHOLDER_PREFIX = "${";

	/** Suffix for system property placeholders: "}" */
	public static final String PLACEHOLDER_SUFFIX = "}";

	private static final Log logger = LogFactory.getLog(SystemPropertyUtils.class);


	/**
	 * Resolve ${...} placeholders in the given text,
	 * replacing them with corresponding system property values.
	 * @param text the String to resolve
	 * @return the resolved String
	 * @see #PLACEHOLDER_PREFIX
	 * @see #PLACEHOLDER_SUFFIX
	 */
	public static String resolvePlaceholders(String text) {
		StringBuffer buf = new StringBuffer(text);

		// 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 = text.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 {
					if (logger.isWarnEnabled()) {
						logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "] as system property");
					}
					startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_SUFFIX.length());
				}
			}
			else {
				startIndex = -1;
			}
		}

		return buf.toString();
	}

}

@@ -17,17 +17,17 @@
 2002-200
-5
+6
  the ori
@@ -792,19 +792,16 @@
 n texts.
-%0A *
  Usually
@@ -852,22 +852,35 @@
 contain 
-$%7B...%7D
+%3Ccode%3E$%7B...%7D%3C/code%3E
  placeho
@@ -931,16 +931,22 @@
 s: e.g. 
+%3Ccode%3E
 $%7Buser.d
@@ -948,16 +948,23 @@
 ser.dir%7D
+%3C/code%3E
 .%0A *%0A * 
@@ -2340,58 +2340,265 @@
 %09%09%09%09
-String propVal = System.getProperty(placeholder);%0A
+int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();%0A%09%09%09%09try %7B%0A%09%09%09%09%09String propVal = System.getProperty(placeholder);%0A%09%09%09%09%09if (propVal == null) %7B%0A%09%09%09%09%09%09// Fall back to searching the system environment.%0A%09%09%09%09%09%09propVal = System.getenv(placeholder);%0A%09%09%09%09%09%7D%0A%09
 %09%09%09%09
@@ -2616,24 +2616,25 @@
  != null) %7B%0A
+%09
 %09%09%09%09%09buf.rep
@@ -2709,110 +2709,337 @@
 %09%09%09%09
-startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, startInd
+%09nextIndex = startIndex + propVal.length();%0A%09%09%09%09%09%7D%0A%09%09%09%09%09else %7B%0A%09%09%09%09%09%09if (logger.isWarnEnabled()) %7B%0A%09%09%09%09%09%09%09logger.warn(%22Could not resolve placeholder '%22 + placeholder + %22' in %5B%22 + t
 ex
+t
  +
- propVal.length());%0A%09%09%09%09%7D%0A%09%09%09%09else
+%0A%09%09%09%09%09%09%09%09%09%22%5D as system property: neither system property nor environment variable found%22);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09catch (SecurityException ex)
  %7B%0A%09
@@ -3157,17 +3157,25 @@
 + text +
- 
+%0A%09%09%09%09%09%09%09%09
 %22%5D as sy
@@ -3187,17 +3187,24 @@
 property
-%22
+: %22 + ex
 );%0A%09%09%09%09%09
@@ -3202,24 +3202,29 @@
 x);%0A%09%09%09%09%09%7D%0A%09
+%09%09%09%7D%0A
 %09%09%09%09startInd
@@ -3275,54 +3275,19 @@
 IX, 
-end
+next
 Index
- + PLACEHOLDER_SUFFIX.length());%0A%09%09%09%09%7D
+);
 %0A%09%09%09

resolve placeholders in resource locations against system environment variables as well