Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/util/PropertyExpander.java
38830 views
/*1* Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.util;2627import java.net.URI;28import java.net.URISyntaxException;29import java.security.GeneralSecurityException;3031/**32* A utility class to expand properties embedded in a string.33* Strings of the form ${some.property.name} are expanded to34* be the value of the property. Also, the special ${/} property35* is expanded to be the same as file.separator. If a property36* is not set, a GeneralSecurityException will be thrown.37*38* @author Roland Schemers39*/40public class PropertyExpander {414243public static class ExpandException extends GeneralSecurityException {4445private static final long serialVersionUID = -7941948581406161702L;4647public ExpandException(String msg) {48super(msg);49}50}5152public static String expand(String value)53throws ExpandException54{55return expand(value, false);56}5758public static String expand(String value, boolean encodeURL)59throws ExpandException60{61if (value == null)62return null;6364int p = value.indexOf("${", 0);6566// no special characters67if (p == -1) return value;6869StringBuffer sb = new StringBuffer(value.length());70int max = value.length();71int i = 0; // index of last character we copied7273scanner:74while (p < max) {75if (p > i) {76// copy in anything before the special stuff77sb.append(value.substring(i, p));78i = p;79}80int pe = p+2;8182// do not expand ${{ ... }}83if (pe < max && value.charAt(pe) == '{') {84pe = value.indexOf("}}", pe);85if (pe == -1 || pe+2 == max) {86// append remaining chars87sb.append(value.substring(p));88break scanner;89} else {90// append as normal text91pe++;92sb.append(value.substring(p, pe+1));93}94} else {95while ((pe < max) && (value.charAt(pe) != '}')) {96pe++;97}98if (pe == max) {99// no matching '}' found, just add in as normal text100sb.append(value.substring(p, pe));101break scanner;102}103String prop = value.substring(p+2, pe);104if (prop.equals("/")) {105sb.append(java.io.File.separatorChar);106} else {107String val = System.getProperty(prop);108if (val != null) {109if (encodeURL) {110// encode 'val' unless it's an absolute URI111// at the beginning of the string buffer112try {113if (sb.length() > 0 ||114!(new URI(val)).isAbsolute()) {115val = sun.net.www.ParseUtil.encodePath(val);116}117} catch (URISyntaxException use) {118val = sun.net.www.ParseUtil.encodePath(val);119}120}121sb.append(val);122} else {123throw new ExpandException(124"unable to expand property " +125prop);126}127}128}129i = pe+1;130p = value.indexOf("${", i);131if (p == -1) {132// no more to expand. copy in any extra133if (i < max) {134sb.append(value.substring(i, max));135}136// break out of loop137break scanner;138}139}140return sb.toString();141}142}143144145