Java Map Replace replace(String originalCommand, Map vars)

Here you can find the source of replace(String originalCommand, Map vars)

Description

Replace parameterized value on a given script

License

Open Source License

Parameter

Parameter Description
originalCommand a parameter
vars a parameter

Declaration

public static String replace(String originalCommand, Map<String, String> vars) 

Method Source Code

//package com.java2s;
/*/*  w  w w  . ja  v a 2s .  c  om*/
 * The MIT License
 *
 * Copyright (C) 2013 by Edmund Wagner
 *              2014-2015 by Lab
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.Map;

public class Main {
    /**
     * Replace parameterized value on a given script
     * 
     * @author Edmund Wagner (this plugin was initially forked from VariableReplacerUtil.java in the "Jenkins SSH plugin" :
     *                     https://wiki.jenkins-ci.org/display/JENKINS/SSH+plugin)
     * 
     * @param originalCommand
     * @param vars
     * @return
     */
    public static String replace(String originalCommand, Map<String, String> vars) {

        if (originalCommand == null) {
            return null;
        }
        vars.remove("_"); //why _ as key for build tool?
        StringBuilder sb = new StringBuilder();
        for (String variable : vars.keySet()) {
            if (originalCommand.contains(variable)) {
                sb.append(variable).append("=\"").append(vars.get(variable)).append("\"\n");
            }
        }
        sb.append("\n");
        sb.append(originalCommand);
        return sb.toString();
    }
}

Related

  1. replace(Map map, String content)
  2. replace(Map map, String text)
  3. replace(String orign, Map replaceStringMap)
  4. replace(String s, Map map)
  5. replace(String src, String prefix, String suffix, Map props)
  6. replace(String str, Map values)