<commit_before>package org.jboss.windup.config.operation.ruleelement;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.util.ZipUtil;
import org.jboss.windup.util.exception.WindupException;
import org.jboss.windup.graph.GraphUtil;
import org.jboss.windup.graph.model.ApplicationReferenceModel;
import org.jboss.windup.graph.model.ArchiveModel;
import org.jboss.windup.graph.model.resource.FileResourceModel;
import org.ocpsoft.rewrite.context.EvaluationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UnzipArchiveToTemporaryFolder extends AbstractIterationOperator<ArchiveModel>
{
    private static final Logger LOG = LoggerFactory.getLogger(UnzipArchiveToTemporaryFolder.class);
    
    public static final String WINDUP_TEMP_ARCHIVE_FOLDER_NAME = "windup_temp_archive";

    public UnzipArchiveToTemporaryFolder(String variableName)
    {
        super(ArchiveModel.class, variableName);
    }

    public static UnzipArchiveToTemporaryFolder unzip(String variableName)
    {
        return new UnzipArchiveToTemporaryFolder(variableName);
    }

    @Override
    public void perform(GraphRewrite event, EvaluationContext context, ArchiveModel payload)
    {
        // create a temp folder for all archive contents
        Path windupTempFolder = event.getWindupTemporaryFolder();
        Path windupTempUnzippedArchiveFolder = Paths.get(windupTempFolder.toString(), "archives");
        if (!Files.isDirectory(windupTempUnzippedArchiveFolder))
        {
            try
            {
                Files.createDirectories(windupTempUnzippedArchiveFolder);
            }
            catch (IOException e)
            {
                throw new WindupException("Failed to create temporary folder: " + windupTempUnzippedArchiveFolder
                            + " due to: " + e.getMessage(), e);
            }
        }

        if (payload instanceof FileResourceModel)
        {
            FileResourceModel fileResourceModel = (FileResourceModel) payload;
            unzipToTempDirectory(event, windupTempUnzippedArchiveFolder, fileResourceModel.asFile(), payload);
        }
    }

    private void unzipToTempDirectory(final GraphRewrite event, final Path tempFolder,
                final File inputZipFile,
                final ArchiveModel archiveModel)
    {
        // Setup a temp folder for the archive
        String appArchiveName = archiveModel.getArchiveName();
        Path appArchiveFolder = Paths.get(tempFolder.toString(), appArchiveName);

        int fileIdx = 1;
        // if it is already created, try another folder name
        while (Files.exists(appArchiveFolder))
        {
            appArchiveFolder = Paths.get(tempFolder.toString(), appArchiveName + "." + fileIdx);
            fileIdx++;
        }

        try
        {
            Files.createDirectories(appArchiveFolder);
        }
        catch (IOException e)
        {
            throw new WindupException("Could not create a temporary directory for application \""
                        + appArchiveName + "\" at \"" + appArchiveFolder.toString() + "\" due to: " + e.getMessage(), e);
        }

        // unzip to the temp folder
        LOG.info("Unzipping " + inputZipFile.getPath() + " to " + appArchiveFolder.toString() );
        try
        {
            ZipUtil.unzipToFolder(inputZipFile, appArchiveFolder.toFile());
        }
        catch( Throwable e )
        {
            throw new WindupException("Unzipping archive: " + inputZipFile.getAbsolutePath() + " failed due to: "
                        + e.getMessage(), e);
        }

        // add a folder reference for this application
        ApplicationReferenceModel appRefModel = archiveModel.getApplicationReferenceModel();

        FileResourceModel newFileModel = event.getGraphContext().getFramed()
                    .addVertex(null, FileResourceModel.class);
        newFileModel.setFilePath(appArchiveFolder.toAbsolutePath().toString());
        // mark the path to the archive
        archiveModel.setUnzippedDirectory(newFileModel);

        if (appRefModel != null)
        {
            appRefModel.setFileResourceModel(newFileModel);
        }

        try
        {
            // scan for subarchives, unzip those, and set their parent objects
            Files.walkFileTree(appArchiveFolder, new SimpleFileVisitor<Path>()
            {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
                {
                    FileResourceModel childFile = event.getGraphContext().getFramed()
                                .addVertex(null, FileResourceModel.class);
                    childFile.setFilePath(file.toAbsolutePath().toString());
                    archiveModel.addContainedFileModel(childFile);

                    if (ZipUtil.endsWithZipExtension(file.toAbsolutePath().toString()))
                    {
                        File newZipFile = file.toFile();
                        ArchiveModel newArchiveModel = GraphUtil.addTypeToModel(event.getGraphContext(), childFile,
                                    ArchiveModel.class);
                        newArchiveModel.setParentArchive(archiveModel);
                        newArchiveModel.setArchiveName(newZipFile.getName());
                        archiveModel.addChildArchive(newArchiveModel);
                        unzipToTempDirectory(event, tempFolder, newZipFile, newArchiveModel);
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
        catch (IOException e)
        {
            throw new WindupException("Failed to walk directory tree: " + appArchiveFolder.toString()
                        + " due to: " + e.getMessage(), e);
        }
    }
}
<commit_msg>Added some safeguards against non-existent files to make unit test failures more clear<commit_after>package org.jboss.windup.config.operation.ruleelement;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import org.jboss.windup.config.GraphRewrite;
import org.jboss.windup.graph.GraphUtil;
import org.jboss.windup.graph.model.ApplicationReferenceModel;
import org.jboss.windup.graph.model.ArchiveModel;
import org.jboss.windup.graph.model.resource.FileResourceModel;
import org.jboss.windup.util.ZipUtil;
import org.jboss.windup.util.exception.WindupException;
import org.ocpsoft.rewrite.context.EvaluationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UnzipArchiveToTemporaryFolder extends AbstractIterationOperator<ArchiveModel>
{
    private static final Logger LOG = LoggerFactory.getLogger(UnzipArchiveToTemporaryFolder.class);

    public static final String WINDUP_TEMP_ARCHIVE_FOLDER_NAME = "windup_temp_archive";

    public UnzipArchiveToTemporaryFolder(String variableName)
    {
        super(ArchiveModel.class, variableName);
    }

    public static UnzipArchiveToTemporaryFolder unzip(String variableName)
    {
        return new UnzipArchiveToTemporaryFolder(variableName);
    }

    @Override
    public void perform(GraphRewrite event, EvaluationContext context, ArchiveModel payload)
    {
        if (payload instanceof FileResourceModel)
        {
            FileResourceModel fileResourceModel = (FileResourceModel) payload;
            File zipFile = fileResourceModel.asFile();

            if (!zipFile.isFile())
            {
                throw new WindupException("Input file \"" + zipFile.getAbsolutePath() + "\" does not exist.");
            }

            // create a temp folder for all archive contents
            Path windupTempFolder = event.getWindupTemporaryFolder();
            Path windupTempUnzippedArchiveFolder = Paths.get(windupTempFolder.toString(), "archives");
            if (!Files.isDirectory(windupTempUnzippedArchiveFolder))
            {
                try
                {
                    Files.createDirectories(windupTempUnzippedArchiveFolder);
                }
                catch (IOException e)
                {
                    throw new WindupException("Failed to create temporary folder: " + windupTempUnzippedArchiveFolder
                                + " due to: " + e.getMessage(), e);
                }
            }

            unzipToTempDirectory(event, windupTempUnzippedArchiveFolder, zipFile, payload);
        }
    }

    private void unzipToTempDirectory(final GraphRewrite event, final Path tempFolder,
                final File inputZipFile,
                final ArchiveModel archiveModel)
    {
        // Setup a temp folder for the archive
        String appArchiveName = archiveModel.getArchiveName();
        Path appArchiveFolder = Paths.get(tempFolder.toString(), appArchiveName);

        int fileIdx = 1;
        // if it is already created, try another folder name
        while (Files.exists(appArchiveFolder))
        {
            appArchiveFolder = Paths.get(tempFolder.toString(), appArchiveName + "." + fileIdx);
            fileIdx++;
        }

        try
        {
            Files.createDirectories(appArchiveFolder);
        }
        catch (IOException e)
        {
            throw new WindupException("Could not create a temporary directory for application \""
                        + appArchiveName + "\" at \"" + appArchiveFolder.toString() + "\" due to: " + e.getMessage(), e);
        }

        // unzip to the temp folder
        LOG.info("Unzipping " + inputZipFile.getPath() + " to " + appArchiveFolder.toString());
        try
        {
            ZipUtil.unzipToFolder(inputZipFile, appArchiveFolder.toFile());
        }
        catch (Throwable e)
        {
            throw new WindupException("Unzipping archive: " + inputZipFile.getAbsolutePath() + " failed due to: "
                        + e.getMessage(), e);
        }

        // add a folder reference for this application
        ApplicationReferenceModel appRefModel = archiveModel.getApplicationReferenceModel();

        FileResourceModel newFileModel = event.getGraphContext().getFramed()
                    .addVertex(null, FileResourceModel.class);
        newFileModel.setFilePath(appArchiveFolder.toAbsolutePath().toString());
        // mark the path to the archive
        archiveModel.setUnzippedDirectory(newFileModel);

        if (appRefModel != null)
        {
            appRefModel.setFileResourceModel(newFileModel);
        }

        try
        {
            // scan for subarchives, unzip those, and set their parent objects
            Files.walkFileTree(appArchiveFolder, new SimpleFileVisitor<Path>()
            {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
                {
                    FileResourceModel childFile = event.getGraphContext().getFramed()
                                .addVertex(null, FileResourceModel.class);
                    childFile.setFilePath(file.toAbsolutePath().toString());
                    archiveModel.addContainedFileModel(childFile);

                    if (ZipUtil.endsWithZipExtension(file.toAbsolutePath().toString()))
                    {
                        File newZipFile = file.toFile();
                        ArchiveModel newArchiveModel = GraphUtil.addTypeToModel(event.getGraphContext(), childFile,
                                    ArchiveModel.class);
                        newArchiveModel.setParentArchive(archiveModel);
                        newArchiveModel.setArchiveName(newZipFile.getName());
                        archiveModel.addChildArchive(newArchiveModel);
                        unzipToTempDirectory(event, tempFolder, newZipFile, newArchiveModel);
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
        catch (IOException e)
        {
            throw new WindupException("Failed to walk directory tree: " + appArchiveFolder.toString()
                        + " due to: " + e.getMessage(), e);
        }
    }
}

{'fraction_non_alphanumeric': 0.08379131819992035, 'fraction_numerical': 0.00047789725209080046, 'mean_word_length': 2.2352486472558617, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4745928', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3080, 'n_words': 641}