2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.security.util;
|
|
27 |
|
|
28 |
import java.net.URI;
|
|
29 |
import java.net.URISyntaxException;
|
|
30 |
import java.security.GeneralSecurityException;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* A utility class to expand properties embedded in a string.
|
|
34 |
* Strings of the form ${some.property.name} are expanded to
|
|
35 |
* be the value of the property. Also, the special ${/} property
|
|
36 |
* is expanded to be the same as file.separator. If a property
|
|
37 |
* is not set, a GeneralSecurityException will be thrown.
|
|
38 |
*
|
|
39 |
* @author Roland Schemers
|
|
40 |
*/
|
|
41 |
public class PropertyExpander {
|
|
42 |
|
|
43 |
|
|
44 |
public static class ExpandException extends GeneralSecurityException {
|
|
45 |
|
|
46 |
private static final long serialVersionUID = -7941948581406161702L;
|
|
47 |
|
|
48 |
public ExpandException(String msg) {
|
|
49 |
super(msg);
|
|
50 |
}
|
|
51 |
}
|
|
52 |
|
|
53 |
public static String expand(String value)
|
|
54 |
throws ExpandException
|
|
55 |
{
|
|
56 |
return expand(value, false);
|
|
57 |
}
|
|
58 |
|
|
59 |
public static String expand(String value, boolean encodeURL)
|
|
60 |
throws ExpandException
|
|
61 |
{
|
|
62 |
if (value == null)
|
|
63 |
return null;
|
|
64 |
|
|
65 |
int p = value.indexOf("${", 0);
|
|
66 |
|
|
67 |
// no special characters
|
|
68 |
if (p == -1) return value;
|
|
69 |
|
|
70 |
StringBuffer sb = new StringBuffer(value.length());
|
|
71 |
int max = value.length();
|
|
72 |
int i = 0; // index of last character we copied
|
|
73 |
|
|
74 |
scanner:
|
|
75 |
while (p < max) {
|
|
76 |
if (p > i) {
|
|
77 |
// copy in anything before the special stuff
|
|
78 |
sb.append(value.substring(i, p));
|
|
79 |
i = p;
|
|
80 |
}
|
|
81 |
int pe = p+2;
|
|
82 |
|
|
83 |
// do not expand ${{ ... }}
|
|
84 |
if (pe < max && value.charAt(pe) == '{') {
|
|
85 |
pe = value.indexOf("}}", pe);
|
|
86 |
if (pe == -1 || pe+2 == max) {
|
|
87 |
// append remaining chars
|
|
88 |
sb.append(value.substring(p));
|
|
89 |
break scanner;
|
|
90 |
} else {
|
|
91 |
// append as normal text
|
|
92 |
pe++;
|
|
93 |
sb.append(value.substring(p, pe+1));
|
|
94 |
}
|
|
95 |
} else {
|
|
96 |
while ((pe < max) && (value.charAt(pe) != '}')) {
|
|
97 |
pe++;
|
|
98 |
}
|
|
99 |
if (pe == max) {
|
|
100 |
// no matching '}' found, just add in as normal text
|
|
101 |
sb.append(value.substring(p, pe));
|
|
102 |
break scanner;
|
|
103 |
}
|
|
104 |
String prop = value.substring(p+2, pe);
|
|
105 |
if (prop.equals("/")) {
|
|
106 |
sb.append(java.io.File.separatorChar);
|
|
107 |
} else {
|
|
108 |
String val = System.getProperty(prop);
|
|
109 |
if (val != null) {
|
|
110 |
if (encodeURL) {
|
|
111 |
// encode 'val' unless it's an absolute URI
|
|
112 |
// at the beginning of the string buffer
|
|
113 |
try {
|
|
114 |
if (sb.length() > 0 ||
|
|
115 |
!(new URI(val)).isAbsolute()) {
|
|
116 |
val = sun.net.www.ParseUtil.encodePath(val);
|
|
117 |
}
|
|
118 |
} catch (URISyntaxException use) {
|
|
119 |
val = sun.net.www.ParseUtil.encodePath(val);
|
|
120 |
}
|
|
121 |
}
|
|
122 |
sb.append(val);
|
|
123 |
} else {
|
|
124 |
throw new ExpandException(
|
|
125 |
"unable to expand property " +
|
|
126 |
prop);
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}
|
|
130 |
i = pe+1;
|
|
131 |
p = value.indexOf("${", i);
|
|
132 |
if (p == -1) {
|
|
133 |
// no more to expand. copy in any extra
|
|
134 |
if (i < max) {
|
|
135 |
sb.append(value.substring(i, max));
|
|
136 |
}
|
|
137 |
// break out of loop
|
|
138 |
break scanner;
|
|
139 |
}
|
|
140 |
}
|
|
141 |
return sb.toString();
|
|
142 |
}
|
|
143 |
}
|