<commit_before>/*
 * Copyright 2010 DTO Labs, Inc. (http://dtolabs.com)
 *
 *  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.
 */

/*
* DataContextUtils.java
* 
* User: <NAME> <a href="mailto:<EMAIL>"><EMAIL></a>
* Created: Aug 16, 2010 6:27:40 PM
* $Id$
*/
package com.dtolabs.rundeck.core.dispatcher;

import com.dtolabs.rundeck.core.common.Framework;
import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.execution.script.ScriptfileUtils;
import com.dtolabs.rundeck.core.utils.Converter;
import org.apache.tools.ant.filters.ReplaceTokens;
import org.apache.tools.ant.taskdefs.ExecTask;
import org.apache.tools.ant.types.Environment;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * DataContextUtils provides methods for using a set of context data to substitute property references, generate
 * environment variables, and expand tokens in a file.
 *
 * @author <NAME> <a href="mailto:<EMAIL>"><EMAIL></a>
 * @version $Revision$
 */
public class DataContextUtils {
    /**
     * Prefix string used for all environment variable names
     */
    public static final String ENV_VAR_PREFIX = "RD_";


    /**
     * Replace the embedded  properties of the form '${key.name}' in the input Strings with the value from the data
     * context
     *
     * @param args argument string array
     * @param data data context
     *
     * @return string array with replaced embedded properties
     */
    public static String[] replaceDataReferences(final String[] args, final Map<String, Map<String, String>> data) {
        if (null == data || data.isEmpty()) {
            return args;
        }
        if(null==args || args.length<1){
            return args;
        }
        final String[] newargs = new String[args.length];

        for (int i = 0 ; i < args.length ; i++) {
            final String arg = args[i];
            newargs[i] = replaceDataReferences(arg, data);
        }

        return newargs;
    }

    /**
     * Recursively replace data references in the values in a map which contains either string, collection or Map
     * values.
     *
     * @param input input map
     * @param data  context data
     *
     * @return Map with all string values having references replaced
     */
    public static Map<String, Object> replaceDataReferences(final Map<String, Object> input,
                                                            final Map<String, Map<String, String>> data) {
        final HashMap<String, Object> output = new HashMap<String, Object>();
        for (final String s : input.keySet()) {
            Object o = input.get(s);
            output.put(s, replaceDataReferencesInObject(o, data));
        }
        return output;
    }
    private static Object replaceDataReferencesInObject(Object o, final Map<String, Map<String, String>> data){
        if (o instanceof String) {
            return replaceDataReferences((String) o, data);
        } else if (o instanceof Map) {
            Map<String, Object> sub = (Map<String, Object>) o;
            return replaceDataReferences(sub, data);
        } else if (o instanceof Collection) {
            ArrayList result = new ArrayList();
            Collection r = (Collection)o;
            for (final Object o1 : r) {
                result.add(replaceDataReferencesInObject(o1, data));
            }
            return result;
        }else{
            return o;
        }
    }

    /**
     * Replace the embedded  properties of the form '${key.name}' in the input Strings with the value from the data
     * context
     *
     * @param input input string
     * @param data  data context map
     *
     * @return string with values substituted, or original string
     */
    public static String replaceDataReferences(final String input, final Map<String, Map<String, String>> data) {
        return replaceDataReferences(input, data, null, false);
    }

    /**
     * Indicates that the value of a property reference could not be resolved.
     */
    public static class UnresolvedDataReferenceException extends RuntimeException{
        private String template;
        private String referenceName;

        public UnresolvedDataReferenceException(final String template, final String referenceName) {
            super("Property " + referenceName + " could not be resolved in template: " + template);
            this.template = template;
            this.referenceName = referenceName;
        }

        public String getTemplate() {
            return template;
        }

        public String getReferenceName() {
            return referenceName;
        }
    }
    /**<commit_msg>Add utility method<commit_after>/*
 * Copyright 2010 DTO Labs, Inc. (http://dtolabs.com)
 *
 *  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.
 */

/*
* DataContextUtils.java
* 
* User: <NAME> <a href="mailto:<EMAIL>"><EMAIL></a>
* Created: Aug 16, 2010 6:27:40 PM
* $Id$
*/
package com.dtolabs.rundeck.core.dispatcher;

import com.dtolabs.rundeck.core.common.Framework;
import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.execution.script.ScriptfileUtils;
import com.dtolabs.rundeck.core.utils.Converter;
import org.apache.tools.ant.filters.ReplaceTokens;
import org.apache.tools.ant.taskdefs.ExecTask;
import org.apache.tools.ant.types.Environment;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * DataContextUtils provides methods for using a set of context data to substitute property references, generate
 * environment variables, and expand tokens in a file.
 *
 * @author <NAME> <a href="mailto:<EMAIL>"><EMAIL></a>
 * @version $Revision$
 */
public class DataContextUtils {
    /**
     * Prefix string used for all environment variable names
     */
    public static final String ENV_VAR_PREFIX = "RD_";


    /**
     * Replace the embedded  properties of the form '${key.name}' in the input Strings with the value from the data
     * context
     *
     * @param args argument string array
     * @param data data context
     *
     * @return string array with replaced embedded properties
     */
    public static String[] replaceDataReferences(final String[] args, final Map<String, Map<String, String>> data) {
        if (null == data || data.isEmpty()) {
            return args;
        }
        if(null==args || args.length<1){
            return args;
        }
        final String[] newargs = new String[args.length];

        for (int i = 0 ; i < args.length ; i++) {
            final String arg = args[i];
            newargs[i] = replaceDataReferences(arg, data);
        }

        return newargs;
    }

    /**
     * Recursively replace data references in the values in a map which contains either string, collection or Map
     * values.
     *
     * @param input input map
     * @param data  context data
     *
     * @return Map with all string values having references replaced
     */
    public static Map<String, Object> replaceDataReferences(final Map<String, Object> input,
                                                            final Map<String, Map<String, String>> data) {
        final HashMap<String, Object> output = new HashMap<String, Object>();
        for (final String s : input.keySet()) {
            Object o = input.get(s);
            output.put(s, replaceDataReferencesInObject(o, data));
        }
        return output;
    }
    private static Object replaceDataReferencesInObject(Object o, final Map<String, Map<String, String>> data){
        if (o instanceof String) {
            return replaceDataReferences((String) o, data);
        } else if (o instanceof Map) {
            Map<String, Object> sub = (Map<String, Object>) o;
            return replaceDataReferences(sub, data);
        } else if (o instanceof Collection) {
            ArrayList result = new ArrayList();
            Collection r = (Collection)o;
            for (final Object o1 : r) {
                result.add(replaceDataReferencesInObject(o1, data));
            }
            return result;
        }else{
            return o;
        }
    }

    /**
     * Replace the embedded  properties of the form '${key.name}' in the input Strings with the value from the data
     * context
     *
     * @param input input string
     * @param data  data context map
     *
     * @return string with values substituted, or original string
     */
    public static String replaceDataReferences(final String input, final Map<String, Map<String, String>> data) {
        return replaceDataReferences(input, data, null, false);
    }

    /**
     * Merge one context onto another by adding or replacing values.
     */
    public static Map<String, Map<String, String>> merge(final Map<String, Map<String, String>> context,
                                                         final Map<String, Map<String, String>> context2) {
        final HashMap<String, Map<String, String>> newcontext = new HashMap<String, Map<String, String>>();
        for (final Map.Entry<String, Map<String, String>> entry : context2.entrySet()) {
            if (!context.containsKey(entry.getKey())) {
                newcontext.put(entry.getKey(), new HashMap<String, String>());
            }else {
                newcontext.put(entry.getKey(), new HashMap<String, String>(context.get(entry.getKey())));
            }
            newcontext.get(entry.getKey()).putAll(entry.getValue());
        }
        return newcontext;
    }

    /**
     * Indicates that the value of a property reference could not be resolved.
     */
    public static class UnresolvedDataReferenceException extends RuntimeException{
        private String template;
        private String referenceName;

        public UnresolvedDataReferenceException(final String template, final String referenceName) {
            super("Property " + referenceName + " could not be resolved in template: " + template);
            this.template = template;
            this.referenceName = referenceName;
        }

        public String getTemplate() {
            return template;
        }

        public String getReferenceName() {
            return referenceName;
        }
    }
    /**
{'fraction_non_alphanumeric': 0.0987435852061582, 'fraction_numerical': 0.004247035922845514, 'mean_word_length': 2.4982977406375735, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3753424', 'n_tokens_mistral': 3014, 'n_tokens_neox': 2929, 'n_words': 1005}