Richard S. Hall | 85bafab | 2009-07-13 13:25:46 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package org.cauldron.bld.config; |
| 21 | |
| 22 | import java.util.Map; |
| 23 | import java.util.Properties; |
| 24 | import java.util.regex.Matcher; |
| 25 | import java.util.regex.Pattern; |
| 26 | |
| 27 | // taken from Newton Launcher |
| 28 | |
| 29 | public class BldUtil { |
| 30 | /** |
| 31 | * expands property references embedded in strings. Each occurrence of ${name} is replaced with the value of |
| 32 | * p.getProperty("name"); If the property is not set, then the original reference, is returned as follows "?<name>". |
| 33 | * |
| 34 | * Strings to be expanded should not contain $ or }, except to indicate expansions. |
| 35 | * |
| 36 | * Value is expanded recursively and so can contain further ${name} references. Also supports shell-expansions |
| 37 | * ${name:-value}, ${name:=value} and ${name:+value}. |
| 38 | * |
| 39 | * <pre> |
| 40 | * ${parameter} |
| 41 | * The value of parameter is substituted. |
| 42 | * ${parameter:-word} |
| 43 | * Use Default Values. If parameter is null, the expansion of word |
| 44 | * is substituted. Otherwise, the value of parameter is substituted. |
| 45 | * ${parameter:=word} |
| 46 | * Assign Default Values. If parameter is null, the expansion of |
| 47 | * word is assigned to parameter. The value of parameter is then |
| 48 | * substituted. |
| 49 | * ${parameter:+word} |
| 50 | * Use Alternate Value. If parameter is null, nothing is |
| 51 | * substituted, otherwise the expansion of word is substituted. |
| 52 | * ${parameter:?word} |
| 53 | * Raise Error. If parameter is null, a RuntimeException is thown, |
| 54 | * with word as the message. |
| 55 | * </pre> |
| 56 | */ |
| 57 | public static String expand(String s, Properties p) { |
| 58 | // regex to match property references e.g. ${name} |
| 59 | // TODO this is very simplistic, so strings to be expanded should not |
| 60 | // contain $ or }, except where substitution is expected. |
| 61 | // Update: propRef regex now allows substitutions to contain $, |
| 62 | // e.g. where a Windows ${user.name} is $Admin or similar. |
| 63 | final Pattern propRef = Pattern.compile("\\$\\{(((\\$[^\\{\\}])|[^\\$\\}])+\\$?)\\}"); |
| 64 | final Pattern backslash = Pattern.compile("\\\\"); |
| 65 | final Pattern dollar = Pattern.compile("\\$"); |
| 66 | |
| 67 | if (s == null) { |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | if (s.indexOf("${") == -1) { // shortcut if no expansions |
| 72 | return s; |
| 73 | } |
| 74 | |
| 75 | for (int i = 0; i < 20; i++) { // avoids self-referencing expansions |
| 76 | // System.out.println("XXX expand[" + i + "] = [" + s + "]"); |
| 77 | Matcher matcher = propRef.matcher(s); |
| 78 | |
| 79 | if (!matcher.find()) { |
| 80 | // replace unmatched items |
| 81 | s = s.replaceAll("\\Q??[\\E", "\\${"); |
| 82 | s = s.replaceAll("\\Q??]\\E", "}"); |
| 83 | // debug("expanded: " + s); |
| 84 | if (s.indexOf("${") != -1) { |
| 85 | throw new RuntimeException("Can't expand: " + s); |
| 86 | } |
| 87 | return s; |
| 88 | } |
| 89 | |
| 90 | String key = matcher.group(1); |
| 91 | String[] keydef = key.split(":[=+-?@]", 2); |
| 92 | String replace; |
| 93 | |
| 94 | if (keydef.length != 2) { |
| 95 | replace = key.length() == 0 ? null : p.getProperty(key); |
| 96 | } |
| 97 | else { |
| 98 | replace = keydef[0].length() == 0 ? null : p.getProperty(keydef[0]); |
| 99 | |
| 100 | if (replace != null && (replace.length() == 0 || replace.indexOf("${") != -1)) { |
| 101 | // don't want unexpanded replacement, as it may stop ${...:-default} |
| 102 | replace = null; |
| 103 | } |
| 104 | |
| 105 | if (key.indexOf(":+") != -1) { |
| 106 | replace = ((replace == null) ? "" : keydef[1]); |
| 107 | } |
| 108 | else if (replace == null) { |
| 109 | replace = keydef[1]; |
| 110 | |
| 111 | if (key.indexOf(":?") != -1) { |
| 112 | String msg = "${" + keydef[0] + ":?" + keydef[1] + "} property not set"; |
| 113 | throw new RuntimeException(msg); |
| 114 | } |
| 115 | |
| 116 | if (key.indexOf(":=") != -1) { |
| 117 | p.setProperty(keydef[0], keydef[1]); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (replace == null) { |
| 123 | // TODO: this is a hack to avoid looping on unmatched references |
| 124 | // should really leave unchanged and process rest of string. |
| 125 | // We use "]" as delimiter to avoid non-matched "}" |
| 126 | // terminating potential _propRef match |
| 127 | replace = "??[" + key + "??]"; |
| 128 | } |
| 129 | |
| 130 | // Excerpt from replaceAll() javadoc: |
| 131 | // |
| 132 | // Note that backslashes (\) and dollar signs ($) in the replacement |
| 133 | // string may cause the results to be different than if it were |
| 134 | // being |
| 135 | // treated as a literal replacement string. Dollar signs may be |
| 136 | // treated |
| 137 | // as references to captured subsequences, and backslashes are used |
| 138 | // to |
| 139 | // escape literal characters in the replacement string. |
| 140 | // escape any \ or $ in replacement string |
| 141 | replace = backslash.matcher(replace).replaceAll("\\\\\\\\"); |
| 142 | replace = dollar.matcher(replace).replaceAll("\\\\\\$"); |
| 143 | |
| 144 | s = s.replaceAll("\\Q${" + key + "}\\E", replace); |
| 145 | } |
| 146 | |
| 147 | throw new RuntimeException("expand: loop expanding: " + s); |
| 148 | } |
| 149 | |
| 150 | public static String expand(String s) { |
| 151 | final Map<String, String> env = System.getenv(); |
| 152 | |
| 153 | return expand(s, new Properties() { |
| 154 | public String getProperty(String name) { |
| 155 | return System.getProperty(name, env.get(name)); |
| 156 | } |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | } |