<commit_before>/*
 * 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 io.github.zlika.reproducible;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Sorts a MANIFEST file by attribute.
 */
final class SortManifestFileStripper implements Stripper
{
    private static final Comparator<String> MANIFEST_ENTRY_COMPARATOR = new Comparator<String>()
    {
        @Override
        public int compare(String o1, String o2)
        {
            if (o1.startsWith("Manifest-Version") || o2.trim().isEmpty())
            {
                return -1;
            }
            else if (o2.startsWith("Manifest-Version") || o1.trim().isEmpty())
            {
                return 1;
            }
            else
            {
                return o1.compareTo(o2);
            }
        }
    };
    
    @Override
    public void strip(File in, File out) throws IOException
    {
        final List<String> lines = Files.readAllLines(in.toPath(), StandardCharsets.UTF_8);
        
        try (final BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(out), StandardCharsets.UTF_8)))
        {
            final String sortedManifest = sortManifestSections(lines).stream()
                                            .collect(Collectors.joining("\r\n"));
            try<commit_msg>Move META-INF/MANIFEST.MF in first position in the JAR file (#16)<commit_after>/*
 * 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 io.github.zlika.reproducible;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Sorts a MANIFEST file by attribute.
 */
final class SortManifestFileStripper implements Stripper
{
    private static final Comparator<String> MANIFEST_ENTRY_COMPARATOR = new Comparator<String>()
    {
        // CHECKSTYLE IGNORE LINE: ReturnCount
        @Override
        public int compare(String o1, String o2)
        {
            if (o1.startsWith("Manifest-Version:"))
            {
                return -1;
            }
            else if (o2.startsWith("Manifest-Version:"))
            {
                return 1;
            }
            // From the "JAR File Specification":
            // Each section must start with an attribute with the name as "Name"
            else if (o1.startsWith("Name:") && !o2.startsWith("Name:"))
            {
                return -1;
            }
            else if (o2.startsWith("Name:") && !o2.startsWith("Name:"))
            {
                return 1;
            }
            else
            {
                return o1.compareTo(o2);
            }
        }
    };
    
    @Override
    public void strip(File in, File out) throws IOException
    {
        final List<String> lines = Files.readAllLines(in.toPath(), StandardCharsets.UTF_8);
        
        try (final BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(out), StandardCharsets.UTF_8)))
        {
            final String sortedManifest = sortManifestSections(lines).stream()
                                            .collect(Collectors.joining("\r\n"));
            try
{'fraction_non_alphanumeric': 0.09291005291005291, 'fraction_numerical': 0.008042328042328042, 'mean_word_length': 2.272853185595568, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, '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': '260316', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1255, 'n_words': 383}