<commit_before>/*
 * IzPack - Copyright 2001-2008 <NAME>, All Rights Reserved.
 *
 * http://izpack.org/
 * http://izpack.codehaus.org/
 *
 * Copyright 2003 <NAME>
 *
 * 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.izforge.izpack.installer.base;

import com.izforge.izpack.api.data.AutomatedInstallData;
import com.izforge.izpack.api.data.DynamicVariable;
import com.izforge.izpack.api.data.ResourceManager;
import com.izforge.izpack.api.installer.InstallerRequirementDisplay;
import com.izforge.izpack.api.rules.RulesEngine;
import com.izforge.izpack.api.substitutor.VariableSubstitutor;
import com.izforge.izpack.core.substitutor.DynamicVariableSubstitutor;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Common utility functions for the GUI and text installers. (Do not import swing/awt classes to
 * this class.)
 *
 * @author <NAME>
 * @author <NAME>
 */
public abstract class InstallerBase implements InstallerRequirementDisplay
{

    private static final Logger LOGGER = Logger.getLogger(InstallerBase.class.getName());

    protected ResourceManager resourceManager;

    /**
     * Abstract constructor which need resource manager
     *
     * @param resourceManager
     */
    protected InstallerBase(ResourceManager resourceManager)
    {
        this.resourceManager = resourceManager;
    }

    public abstract void showMissingRequirementMessage(String message);

    /**
     * Refreshes Dynamic Variables.
     */
    public static void refreshDynamicVariables(AutomatedInstallData installdata,
                                               VariableSubstitutor... substitutors) throws Exception
    {
        Map<String, List<DynamicVariable>> dynamicvariables = installdata.getDynamicvariables();
        RulesEngine rules = installdata.getRules();

        LOGGER.info("refreshing dynamic variables");
        if (dynamicvariables == null)
        {
            return;
        }

        for (List<DynamicVariable> dynamicVariableList : dynamicvariables.values())
        {
            for (DynamicVariable dynamicVariable : dynamicVariableList)
            {
                String conditionid = dynamicVariable.getConditionid();
                if ((conditionid != null) && (conditionid.length() > 0) && !rules.isConditionTrue(conditionid))
                {
                    continue;
                }

                // Add self replacing of previously replaced dynamic variables

                VariableSubstitutor[] newsubstitutors = Arrays.copyOf(substitutors, substitutors.length + 1);
                newsubstitutors[substitutors.length] = new DynamicVariableSubstitutor(dynamicvariables, rules);

                String newValue = dynamicVariable.evaluate(newsubstitutors);
                if (newValue != null)
                {
                    //Debug.log("dynamic variable " + dynamicVariable.getName() + ": " + newValue);
                    installdata.getVariables().setProperty(dynamicVariable.getName(), newValue);
                }
            }
        }
    }

}<commit_msg>Fixed refreshing of dynamic variables in InstallerBase<commit_after>/*
 * IzPack - Copyright 2001-2008 <NAME>, All Rights Reserved.
 *
 * http://izpack.org/
 * http://izpack.codehaus.org/
 *
 * Copyright 2003 <NAME>
 *
 * 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.izforge.izpack.installer.base;

import com.izforge.izpack.api.data.AutomatedInstallData;
import com.izforge.izpack.api.data.DynamicVariable;
import com.izforge.izpack.api.data.ResourceManager;
import com.izforge.izpack.api.installer.InstallerRequirementDisplay;
import com.izforge.izpack.api.rules.RulesEngine;
import com.izforge.izpack.api.substitutor.VariableSubstitutor;
import com.izforge.izpack.core.substitutor.DynamicVariableSubstitutor;
import com.izforge.izpack.util.Debug;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Common utility functions for the GUI and text installers. (Do not import swing/awt classes to
 * this class.)
 *
 * @author <NAME>
 * @author <NAME>
 */
public abstract class InstallerBase implements InstallerRequirementDisplay
{

    private static final Logger LOGGER = Logger.getLogger(InstallerBase.class.getName());

    protected ResourceManager resourceManager;

    /**
     * Abstract constructor which need resource manager
     *
     * @param resourceManager
     */
    protected InstallerBase(ResourceManager resourceManager)
    {
        this.resourceManager = resourceManager;
    }

    public abstract void showMissingRequirementMessage(String message);

    /**
     * Refreshes Dynamic Variables.
     */
    public static void refreshDynamicVariables(AutomatedInstallData installdata,
                                               VariableSubstitutor... substitutors) throws Exception
    {
        Map<String, List<DynamicVariable>> dynamicvariables = installdata.getDynamicvariables();
        RulesEngine rules = installdata.getRules();

        LOGGER.info("refreshing dynamic variables");
        if (dynamicvariables != null)
        {
            for (String dynvarname : dynamicvariables.keySet())
            {
                LOGGER.info("Dynamic variable: " + dynvarname);
                for (DynamicVariable dynvar : dynamicvariables.get(dynvarname))
                {
                    boolean refresh = true;
                    String conditionid = dynvar.getConditionid();
                    if ((conditionid != null) && (conditionid.length() > 0))
                    {
                        if ((rules != null) && !rules.isConditionTrue(conditionid))
                        {
                            LOGGER.info("skipped refreshing dynamic variable due to unmet condition " + conditionid);
                            refresh = false;
                        }
                    }
                    if (refresh)
                    {
                        String newValue = dynvar.evaluate(substitutors);
                        if (newValue != null) {
                            LOGGER.info("dynamic variable " + dynvar.getName() + ": " + newValue);
                            installdata.getVariables().setProperty(dynvar.getName(), newValue);
                        } else {
                            LOGGER.info("dynamic variable " + dynvar.getName() + " unchanged: " + dynvar.getValue());
                        }
                    }
                }
            }
        }

    }

}
{'fraction_non_alphanumeric': 0.09036782217517862, 'fraction_numerical': 0.004630854723471818, 'mean_word_length': 2.4281179138321995, 'pattern_counts': {'":': 2, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7663964', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1860, 'n_words': 501}