KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > AbstractPathResolvingPropertyEditor


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

16
17 package org.springframework.core.io;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 /**
25  * Abstract base class for PropertyEditors that need
26  * to resolve placeholders in paths.
27  *
28  * <p>A path may contain ${...} placeholders, to be resolved as
29  * system properties: e.g. ${user.dir}.
30  *
31  * @author Juergen Hoeller
32  * @since 1.1.2
33  * @see #PLACEHOLDER_PREFIX
34  * @see #PLACEHOLDER_SUFFIX
35  * @see System#getProperty(String)
36  */

37 public class AbstractPathResolvingPropertyEditor extends PropertyEditorSupport JavaDoc {
38
39     public static final String JavaDoc PLACEHOLDER_PREFIX = "${";
40
41     public static final String JavaDoc PLACEHOLDER_SUFFIX = "}";
42
43     protected static final Log logger = LogFactory.getLog(AbstractPathResolvingPropertyEditor.class);
44
45     /**
46      * Resolve the given path, replacing ${...} placeholders with
47      * corresponding system property values if necessary.
48      * @param path the original file path
49      * @return the resolved file path
50      * @see #PLACEHOLDER_PREFIX
51      * @see #PLACEHOLDER_SUFFIX
52      */

53     protected String JavaDoc resolvePath(String JavaDoc path) {
54         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(path);
55
56         // The following code does not use JDK 1.4's StringBuffer.indexOf(String)
57
// method to retain JDK 1.3 compatibility. The slight loss in performance
58
// is not really relevant, as this code will typically just run on startup.
59

60         int startIndex = path.indexOf(PLACEHOLDER_PREFIX);
61         while (startIndex != -1) {
62             int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
63             if (endIndex != -1) {
64                 String JavaDoc placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
65                 String JavaDoc propVal = System.getProperty(placeholder);
66                 if (propVal != null) {
67                     buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
68                     startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, startIndex + propVal.length());
69                 }
70                 else {
71                     logger.warn("Could not resolve placeholder '" + placeholder +
72                         "' in resource path [" + path + "] as system property");
73                     startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_SUFFIX.length());
74                 }
75             }
76             else {
77                 startIndex = -1;
78             }
79         }
80
81         return buf.toString();
82     }
83
84 }
85
Popular Tags