KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > configutil > PropertyResolver


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.configutil;
17
18 import java.util.Properties JavaDoc;
19 import java.util.Stack JavaDoc;
20 import java.util.regex.Pattern JavaDoc;
21 import java.util.regex.Matcher JavaDoc;
22 import java.net.URLEncoder JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.URISyntaxException JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.io.File JavaDoc;
27
28 public class PropertyResolver {
29     public static String JavaDoc resolveProperties(String JavaDoc input) {
30         return resolveProperties(input, System.getProperties());
31     }
32
33     /**
34      * Resolves properties in the input string referenced using
35      * ${property} syntax. Special 'function' properties are also
36      * supported, which use a syntax like ${url-encode:something}.
37      * Nested syntax is also supported, e.g.
38      * ${property and ${anotherproperty}}, which is mostly useful
39      * in combination with the function properties like 'url-encode:'.
40      */

41     public static String JavaDoc resolveProperties(String JavaDoc input, Properties JavaDoc properties) {
42         StringBuffer JavaDoc result = new StringBuffer JavaDoc(input.length());
43         StringBuffer JavaDoc propertyBuffer = null;
44         Stack JavaDoc openPropertyBuffers = new Stack JavaDoc();
45
46         final int STATE_DEFAULT = 0;
47         final int STATE_IN_PROP = 1;
48         int state = STATE_DEFAULT;
49
50         for (int i = 0; i < input.length(); i++) {
51             char c = input.charAt(i);
52             switch (c) {
53                 case '$':
54                     if (i + 1 < input.length() && input.charAt(i + 1) == '{') {
55                         if (state == STATE_IN_PROP) {
56                             openPropertyBuffers.push(propertyBuffer);
57                         }
58                         i++;
59                         state = STATE_IN_PROP;
60                         propertyBuffer = new StringBuffer JavaDoc();
61                     } else {
62                         (state == STATE_IN_PROP ? propertyBuffer : result).append("$");
63                     }
64                     break;
65                 case '}':
66                     if (state == STATE_IN_PROP) {
67                         String JavaDoc propName = propertyBuffer.toString();
68                         String JavaDoc propValue = evaluateProperty(propName, properties);
69                         String JavaDoc propEvalResult = propValue != null ? propValue : "${" + propName + "}";
70                         if (!openPropertyBuffers.empty()) {
71                             propertyBuffer = (StringBuffer JavaDoc)openPropertyBuffers.pop();
72                             propertyBuffer.append(propEvalResult);
73                             // stay in STATE_IN_PROP
74
} else {
75                             result.append(propEvalResult);
76                             state = STATE_DEFAULT;
77                         }
78                     } else {
79                         result.append(c);
80                     }
81                     break;
82                 default:
83                     (state == STATE_IN_PROP ? propertyBuffer : result).append(c);
84             }
85         }
86
87         if (state == STATE_IN_PROP) {
88             // process any property buffers still open
89
do {
90                 if (!openPropertyBuffers.empty()) {
91                     propertyBuffer = ((StringBuffer JavaDoc)openPropertyBuffers.pop()).append("${").append(propertyBuffer);
92                 } else {
93                     result.append("${").append(propertyBuffer);
94                     propertyBuffer = null;
95                 }
96             } while (propertyBuffer != null);
97         }
98
99         return result.toString();
100     }
101
102     private static Pattern JavaDoc PROP_PATTERN = Pattern.compile("^([^:]+):(.+)$");
103
104     /**
105      * Evaluates properties containing special syntax.
106      *
107      * <p>Allows for things like ${url-encode:propname}.
108      */

109     private static String JavaDoc evaluateProperty(String JavaDoc input, Properties JavaDoc properties) {
110         Matcher JavaDoc matcher = PROP_PATTERN.matcher(input);
111         if (matcher.matches()) {
112             String JavaDoc action = matcher.group(1);
113             String JavaDoc value = matcher.group(2);
114
115             try {
116                 if (action.equals("url-encode")) {
117                     return URLEncoder.encode(value, "UTF-8");
118                 } else if (action.equals("double-url-encode")) {
119                     return URLEncoder.encode(URLEncoder.encode(value, "UTF-8"), "UTF-8");
120                 } else if (action.equals("tripple-url-encode")) {
121                     return URLEncoder.encode(URLEncoder.encode(URLEncoder.encode(value, "UTF-8"), "UTF-8"), "UTF-8");
122                 } else if (action.equals("uri-path-encode")) {
123                     try {
124                         return new URI JavaDoc(null, null, value, null).getRawSchemeSpecificPart();
125                     } catch (URISyntaxException JavaDoc e) {
126                         throw new RuntimeException JavaDoc("Error in uri-path-encode function of property resolver.", e);
127                     }
128                 } else if (action.equals("file-to-uri")) {
129                     return new File JavaDoc(value).toURI().toString();
130                 } else {
131                     return null;
132                 }
133             } catch (UnsupportedEncodingException JavaDoc e) {
134                 throw new RuntimeException JavaDoc(e);
135             }
136         } else {
137             return properties.getProperty(input);
138         }
139     }
140 }
141
Popular Tags