<commit_before>/*
 * Copyright 2002-2004 The Apache Software Foundation.
 * 
 * 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 org.apache.commons.lang.text;

import java.util.Map;
import java.util.ArrayList;

/**
 * Provides the ability to substitute values from one or more maps into
 * designated places within a string.
 * <p>
 * For example:
 * <pre>
 *   "User ${id} has uid ${uid} and path of env{path}"
 * </pre>
 * can be expanded using values from a map associated with "$" and a different
 * map associated with "env".
 * <p> 
 * This class is similar to the java.text.MessageFormat class, but allows
 * data to be retrieved from one or maps instead of from a single list.
 * Unlike the MessageFormat class, however, it simply uses the result of 
 * applying toString() to the referenced values in the map(s) and does not 
 * support any "format" operations applied to the objects before substitution 
 * into the target string.
 *
 * @since 2.1
 */

public class MappedMessageFormat {
    private int nEntries = 0;
    private ArrayList markers = new ArrayList(2);
    private ArrayList sources = new ArrayList(2);
    
    public MappedMessageFormat() {
    }
    
    /**
     * Defines a source of data that can later be substituted into
     * strings passed to the "format" methods.
     */
    public void addSource(String marker, Map source) {
        ++nEntries;
        markers.add(marker);
        sources.add(source);
    }

    /**
     * Expands any variable declarations using any of the known
     * variable marker strings.
     * 
     * @throws IllegalArgumentException if the input param references
     * a variable which is not known to the specified source.
     */
    public String format(String param) {
        for(int i=0; i<nEntries; ++i) {
            param = format(
                param, 
                (String) markers.get(i), 
                (Map) sources.get(i));
        }
        return param;
    }
    
    /**
     * Replace any occurrences within the string of the form
     * "marker{key}" with the value from source[key].
     * <p>
     * Commonly, the variable marker is "$", in which case variables
     * are indicated by ${key} in the string.
     * <p>
     * Returns the string after performing all substitutions.
     * <p>
     * If no substitutions were made, the input string object is
     * returned (not a copy).
     *
     * @throws IllegalArgumentException if the input param references
     * a variable which is not known to the specified source.
     */
    public static String format(String str, String marker, Map source) {
        String startMark = marker + "{";
        int markLen = startMark.length();
        
        int index = 0;
        for(;;)
        {
            index = str.indexOf(startMark, index);
            if (index == -1)
            {
                return str;
            }
            
            int startIndex = index + markLen;
            if (startIndex > str.length())
            {
                throw new IllegalArgumentException(
                    "var expression starts at end of string");
            }
            
            int endIndex = str.indexOf("}", index + markLen);
            if (endIndex == -1)
            {
                throw new IllegalArgumentException(
                    "var expression starts but does not end");
            }
            
            String key = str.substring(index+markLen, endIndex);
            Object value =  source.get(key);
            if (value == null) {
                throw new IllegalArgumentException(
                    "parameter [" + key + "] is not defined.");
            }
            String varValue = value.toString();
            <commit_msg>reformatted to match other Lang code<commit_after>/*
 * Copyright 2002-2004 The Apache Software Foundation.
 * 
 * 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 org.apache.commons.lang.text;

import java.util.Map;
import java.util.ArrayList;

/**
 * Provides the ability to substitute values from one or more maps into
 * designated places within a string.
 * <p>
 * For example:
 * <pre>
 *   "User ${id} has uid ${uid} and path of env{path}"
 * </pre>
 * can be expanded using values from a map associated with "$" and a different
 * map associated with "env".
 * <p> 
 * This class is similar to the java.text.MessageFormat class, but allows
 * data to be retrieved from one or maps instead of from a single list.
 * Unlike the MessageFormat class, however, it simply uses the result of 
 * applying toString() to the referenced values in the map(s) and does not 
 * support any "format" operations applied to the objects before substitution 
 * into the target string.
 *
 * Originally org.apache.commons.digester.substitution.MultiVariableExpander
 *
 * @since 2.1
 */
public class MappedMessageFormat {

    private int nEntries = 0;
    private ArrayList markers = new ArrayList(2);
    private ArrayList sources = new ArrayList(2);
    
    public MappedMessageFormat() {
    }
    
    /**
     * Defines a source of data that can later be substituted into
     * strings passed to the "format" methods.
     */
    public void addSource(String marker, Map source) {
        nEntries++;
        markers.add(marker);
        sources.add(source);
    }

    /**
     * Expands any variable declarations using any of the known
     * variable marker strings.
     * 
     * @throws IllegalArgumentException if the input param references
     * a variable which is not known to the specified source.
     */
    public String format(String param) {
        for(int i=0; i<nEntries; i++) {
            param = format( param, (String) markers.get(i), (Map) sources.get(i));
        }
        return param;
    }
    
    /**
     * Replace any occurrences within the string of the form
     * "marker{key}" with the value from source[key].
     * <p>
     * Commonly, the variable marker is "$", in which case variables
     * are indicated by ${key} in the string.
     * <p>
     * Returns the string after performing all substitutions.
     * <p>
     * If no substitutions were made, the input string object is
     * returned (not a copy).
     *
     * @throws IllegalArgumentException if the input param references
     * a variable which is not known to the specified source.
     */
    public static String format(String str, String marker, Map source) {
        String startMark = marker + "{";
        int markLen = startMark.length();
        
        int index = 0;
        while(true) {
            index = str.indexOf(startMark, index);
            if (index == -1) {
                return str;
            }
            
            int startIndex = index + markLen;
            if (startIndex > str.length()) {
                throw new IllegalArgumentException("var expression starts at end of string");
            }
            
            int endIndex = str.indexOf("}", index + markLen);
            if (endIndex == -1) {
                throw new IllegalArgumentException("var expression starts but does not end");
            }
            
            String key = str.substring(index+markLen, endIndex);
            Object value =  source.get(key);
            if (value == null) {
                throw new IllegalArgumentException("parameter [" + key + "] is not defined.");
            }
            String varValue = value.toString();
            
{'fraction_non_alphanumeric': 0.08666351383305745, 'fraction_numerical': 0.004965712934499882, 'mean_word_length': 2.2041666666666666, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7248705', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2096, 'n_words': 902}