<commit_before>package org.jboss.windup.rules.apps.java.config;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.jboss.forge.furnace.util.Predicate;
import org.jboss.forge.furnace.util.Visitor;
import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.config.WindupRuleProvider;
import org.jboss.windup.config.operation.GraphOperation;
import org.jboss.windup.config.phase.Initialization;
import org.jboss.windup.config.phase.RulePhase;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.rules.apps.java.model.WindupJavaConfigurationModel;
import org.jboss.windup.rules.apps.java.service.WindupJavaConfigurationService;
import org.jboss.windup.util.WindupPathUtil;
import org.jboss.windup.util.exception.WindupException;
import org.jboss.windup.util.file.FileSuffixPredicate;
import org.jboss.windup.util.file.FileVisit;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.config.Rule;
import org.ocpsoft.rewrite.context.EvaluationContext;

/**
 * Copies configuration data from {@link GraphContext#getOptionMap()} to the graph itself for easy use by other
 * {@link Rule}s.
 *
 */
public class CopyJavaConfigToGraphRuleProvider extends WindupRuleProvider
{
    @Override
    public Class<? extends RulePhase> getPhase()
    {
        return Initialization.class;
    }

    @Override
    public Configuration getConfiguration(GraphContext context)
    {
        GraphOperation copyConfigToGraph = new GraphOperation()
        {
            @Override
            public void perform(GraphRewrite event, EvaluationContext context)
            {
                Map<String, Object> config = event.getGraphContext().getOptionMap();
                Boolean sourceMode = (Boolean) config.get(SourceModeOption.NAME);
                @SuppressWarnings("unchecked")
                List<String> includeJavaPackages = (List<String>) config.get(ScanPackagesOption.NAME);
                @SuppressWarnings("unchecked")
                final List<String> excludeJavaPackages = new ArrayList<>((List<String>) config.get(ExcludePackagesOption.NAME));

                Predicate<File> predicate = new FileSuffixPredicate("\\.package-ignore\\.txt");
                Visitor<File> visitor = new Visitor<File>()
                {
                    @Override
                    public void visit(File file)
                    {
                        try (FileInputStream inputStream = new FileInputStream(file))
                        {
                            LineIterator it = IOUtils.lineIterator(inputStream, "UTF-8");
                            while (it.hasNext())
                            {
                                String line = it.next();
                                if (!line.startsWith("#") && !line.trim().isEmpty())
                                {
                                    excludeJavaPackages.add(line);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            throw new WindupException("Failed loading package ignore patterns from [" + file.toString() + "]", e);
                        }
                    }
                };

                FileVisit.visit(WindupPathUtil.getUserIgnoreDir().toFile(), predicate, visitor);
                FileVisit.visit(WindupPathUtil.getWindupIgnoreDir().toFile(), predicate, visitor);

                WindupJavaConfigurationModel javaCfg = WindupJavaConfigurationService.getJavaConfigurationModel(event
                            .getGraphContext());
                javaCfg.setSourceMode(sourceMode == null ? false : sourceMode);
                javaCfg.setScanJavaPackageList(includeJavaPackages);
                javaCfg.setExcludeJavaPackageList(excludeJavaPackages);
            }
        };

        return ConfigurationBuilder.begin()
                    .addRule()
                    .perform(copyConfigToGraph);
    }

}
<commit_msg>Fix NPE<commit_after>package org.jboss.windup.rules.apps.java.config;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.jboss.forge.furnace.util.Predicate;
import org.jboss.forge.furnace.util.Visitor;
import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.config.WindupRuleProvider;
import org.jboss.windup.config.operation.GraphOperation;
import org.jboss.windup.config.phase.Initialization;
import org.jboss.windup.config.phase.RulePhase;
import org.jboss.windup.graph.GraphContext;
import org.jboss.windup.rules.apps.java.model.WindupJavaConfigurationModel;
import org.jboss.windup.rules.apps.java.service.WindupJavaConfigurationService;
import org.jboss.windup.util.WindupPathUtil;
import org.jboss.windup.util.exception.WindupException;
import org.jboss.windup.util.file.FileSuffixPredicate;
import org.jboss.windup.util.file.FileVisit;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.config.Rule;
import org.ocpsoft.rewrite.context.EvaluationContext;

/**
 * Copies configuration data from {@link GraphContext#getOptionMap()} to the graph itself for easy use by other
 * {@link Rule}s.
 *
 */
public class CopyJavaConfigToGraphRuleProvider extends WindupRuleProvider
{
    @Override
    public Class<? extends RulePhase> getPhase()
    {
        return Initialization.class;
    }

    @Override
    public Configuration getConfiguration(GraphContext context)
    {
        GraphOperation copyConfigToGraph = new GraphOperation()
        {
            @Override
            public void perform(GraphRewrite event, EvaluationContext context)
            {
                Map<String, Object> config = event.getGraphContext().getOptionMap();
                Boolean sourceMode = (Boolean) config.get(SourceModeOption.NAME);

                @SuppressWarnings("unchecked")
                List<String> includeJavaPackages = (List<String>) config.get(ScanPackagesOption.NAME);

                @SuppressWarnings("unchecked")
                final List<String> excludedPackages = (List<String>) config.get(ExcludePackagesOption.NAME);
                final List<String> excludeJavaPackages =
                        excludedPackages == null ? new ArrayList() : new ArrayList(excludedPackages);

                Predicate<File> predicate = new FileSuffixPredicate("\\.package-ignore\\.txt");
                Visitor<File> visitor = new Visitor<File>()
                {
                    @Override
                    public void visit(File file)
                    {
                        try (FileInputStream inputStream = new FileInputStream(file))
                        {
                            LineIterator it = IOUtils.lineIterator(inputStream, "UTF-8");
                            while (it.hasNext())
                            {
                                String line = it.next();
                                if (!line.startsWith("#") && !line.trim().isEmpty())
                                {
                                    excludeJavaPackages.add(line);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            throw new WindupException("Failed loading package ignore patterns from [" + file.toString() + "]", e);
                        }
                    }
                };

                FileVisit.visit(WindupPathUtil.getUserIgnoreDir().toFile(), predicate, visitor);
                FileVisit.visit(WindupPathUtil.getWindupIgnoreDir().toFile(), predicate, visitor);

                WindupJavaConfigurationModel javaCfg = WindupJavaConfigurationService.getJavaConfigurationModel(event
                            .getGraphContext());
                javaCfg.setSourceMode(sourceMode == null ? false : sourceMode);
                javaCfg.setScanJavaPackageList(includeJavaPackages);
                javaCfg.setExcludeJavaPackageList(excludeJavaPackages);
            }
        };

        return ConfigurationBuilder.begin()
                    .addRule()
                    .perform(copyConfigToGraph);
    }

}

{'fraction_non_alphanumeric': 0.09454461396208969, 'fraction_numerical': 0.0002311604253351826, 'mean_word_length': 2.096993557623479, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2149828', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2162, 'n_words': 377}