com.baidu.rigel.biplatform.ac.util.PlaceHolderUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.baidu.rigel.biplatform.ac.util.PlaceHolderUtils.java

Source

/**
 * Copyright (c) 2014 Baidu, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baidu.rigel.biplatform.ac.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

/**
 * ??,???? ${name}
 * 
 * @author xiaoming.chen
 *
 */
public class PlaceHolderUtils {

    /**
     * ?????  abc/${1}/${2}/abc.action,?? ${1},${2}
     * 
     * @param source ?
     * @return ????
     */
    public static List<String> getPlaceHolders(String source) {
        if (StringUtils.isBlank(source)) {
            throw new IllegalArgumentException("can not get place hode list by blank source");
        }
        Pattern pattern = Pattern.compile("\\$\\{[^\\}]+\\}");
        Matcher match = pattern.matcher(source);
        List<String> result = new ArrayList<String>();
        while (match.find()) {
            result.add(match.group());
        }
        return result;
    }

    /**
     * ???KEY?? ${abc}
     * <p>
     * ?????^\\$\\{[^\\}]+\\}$ 
     * </p>
     * 
     * @param placeHolder ??
     * @return ????KEY
     */
    public static String getKeyFromPlaceHolder(String placeHolder) {
        if (StringUtils.isBlank(placeHolder)) {
            throw new IllegalArgumentException("can not get key from empty placeHolder");
        }
        if (!Pattern.matches("^\\$\\{[^\\}]+\\}$", placeHolder)) {
            return placeHolder;
        } else {
            return placeHolder.substring(2, placeHolder.length() - 1);
        }
    }

    /** 
     * ?????  abc/${1}/${2}/${1}/abc.action,??1,2
     * getPlaceHolderKeys
     * @param source
     * @return
     */
    public static Set<String> getPlaceHolderKeys(String source) {
        List<String> placeHolders = getPlaceHolders(source);
        Set<String> result = new HashSet<String>();
        if (CollectionUtils.isNotEmpty(placeHolders)) {
            for (String placeHolder : placeHolders) {
                result.add(getKeyFromPlaceHolder(placeHolder));
            }
        }
        return result;
    }

    /**
     * ?????value ???????
     * 
     * @param source ?
     * @param placeHolder ???KEY???? url  ${url}
     * @param value ????null
     * @return ???
     * @throws IllegalArgumentException ???
     */
    public static String replacePlaceHolderWithValue(String source, String placeHolder, String value) {
        if (StringUtils.isBlank(source) || StringUtils.isBlank(placeHolder)) {
            throw new IllegalArgumentException("params error, source : " + source + " placeHolder:" + placeHolder);
        }
        String oldKey = placeHolder;
        if (!Pattern.matches("^\\$\\{[^\\}]+\\}$", placeHolder)) {
            oldKey = "${" + oldKey + "}";
        }

        return source.replace(oldKey, value);

    }

}