<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.uima.test.junit_extension;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * FileCompare class provides a several methods, which compare two files or input streams.
 * Most methods are static.
 * 
 * It has a facility to incorporate a regex ignore-differences filter
 * 
 */
public class FileCompare {

  /**
   * TODO Currently only tags containing word characters [a-zA-Z_0-9] are recognised.
   */
  // match "<" followed by 1 or more word_chars followed by ">" followed by "</" followed by
  //   (presumably) the same 1 or more word_chars followed by ">"
  private static final String EMPTY_TAG_REGEX = "(<([\\w]+)>[\\s]*</[\\w]+>)";

  private static Pattern emptyTagPattern = Pattern.compile(EMPTY_TAG_REGEX);
  
  // matches cr if it is followed by a new-line, will be repl with just a new line
  private static Pattern crnlPattern = Pattern.compile("\\r(?=\\n)");

  /**
   * compares two files and return true if the files have the same content.
   * 
   * @param filename1
   *          filename of the first file
   * @param filename2
   *          filename of the second file
   * @return - true if the files have the same content
   * 
   * @throws IOException
   */
  public static boolean compare(String filename1, String filename2) throws IOException {
    InputStream file1 = null;
    InputStream file2 = null;

    try {
      // create file input stream of the two bytes
      file1 = new FileInputStream(filename1);
      file2 = new FileInputStream(filename2);

      return compare(file1, file2);

    } finally {
      file1.close();
      file2.close();
    }
  }

  /**
   * compares two files and return true if the files have the same content.
   * 
   * @param file1
   *          first file
   * @param file2
   *          second file
   * @return - true if the files have the same content
   * 
   * @throws IOException
   */
  public static boolean compare(File file1, File file2) throws IOException {
    InputStream inputStream1 = null;
    InputStream inputStream2 = null;

    try {
      // create file input stream of the two bytes
      inputStream1 = new FileInputStream(file1);
      inputStream2 = new FileInputStream(file2);

      return compare(inputStream1, inputStream2);

    } finally {
      inputStream1.close();
      inputStream2.close();
    }
  }

  
  /**
   * compares two files and return true if the files have the same content.
   * 
   * @param filename1
   *          filename of the first file
   * @param in
   *          an input Sream
   * 
   * @return - true if the content is the same
   * 
   * @throws IOException
   */
  public static boolean compare(String filename1, InputStream in) throws IOException {
    InputStream file1 = null;

    try {
      // create file input stream of the two bytes
      file1 = new FileInputStream(filename1);

      return compare(file1, in);

    } finally {
      file1.close();
    }
  }

  public static boolean compare(InputStream in1, InputStream in2) throws IOException {
    int in1byte, in2byte;

    final int byteBufferSize = 10000;

    in1 = new BufferedInputStream(in1, byteBufferSize);
    in2 = new BufferedInputStream(in2, byteBufferSize);

    in1byte = 0;
    while (in1byte != -1) {
      // read one byte from file1
      in1byte = in1.read();

      // check if byte is whitespace or blank
      if ((!(Character.isWhitespace((char) in1byte))) && (in1byte != ' ') && (in1byte != '\n')
              && (in1byte != '\r')) {
        // read one byte form file2
        in2byte = in2.read();

        // read bytes until byte is no whitespace or blank
        while ((Character.isWhitespace((char) in2byte)) || (in2byte == ' ') || (in2byte == '\n')
                || (in2byte == '\r')) {
          // if byte is whitespace or blank read next byte
          in2byte = in2.read();
        }

        // check if byte from file1 and file2 are the same
        if (in1byte != in2byte) {
          return false; // file content of the two files are not the same
        }
      }
    }
    return true;
  }


  /**
   * Compares two XML files and returns true, if both have the same content. Different notations for
   * empty tags are considered equal.
   * 
   * @param filename1
   *          Filename of the first XML file.
   * @param filename2
   *          Filename of the second XML file.
   * @return
   * @throws IOException
   */
  public static boolean compareXML(String filename1, String filename2) throws IOException {
    File file1 = null;<commit_msg>UIMA-1582 add a new compareStrings method which ignores whitespace (including, therefore, different platform line endings)<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.uima.test.junit_extension;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * FileCompare class provides a several methods, which compare two files or input streams.
 * Most methods are static.
 * 
 * It has a facility to incorporate a regex ignore-differences filter
 * 
 */
public class FileCompare {

  /**
   * TODO Currently only tags containing word characters [a-zA-Z_0-9] are recognised.
   */
  // match "<" followed by 1 or more word_chars followed by ">" followed by "</" followed by
  //   (presumably) the same 1 or more word_chars followed by ">"
  private static final String EMPTY_TAG_REGEX = "(<([\\w]+)>[\\s]*</[\\w]+>)";

  private static Pattern emptyTagPattern = Pattern.compile(EMPTY_TAG_REGEX);
  
  // matches cr if it is followed by a new-line, will be repl with just a new line
  private static Pattern crnlPattern = Pattern.compile("\\r(?=\\n)");

  /**
   * compares two files and return true if the files have the same content.
   * 
   * @param filename1
   *          filename of the first file
   * @param filename2
   *          filename of the second file
   * @return - true if the files have the same content
   * 
   * @throws IOException
   */
  public static boolean compare(String filename1, String filename2) throws IOException {
    InputStream file1 = null;
    InputStream file2 = null;

    try {
      // create file input stream of the two bytes
      file1 = new FileInputStream(filename1);
      file2 = new FileInputStream(filename2);

      return compare(file1, file2);

    } finally {
      file1.close();
      file2.close();
    }
  }

  /**
   * compares two files and return true if the files have the same content.
   * 
   * @param file1
   *          first file
   * @param file2
   *          second file
   * @return - true if the files have the same content
   * 
   * @throws IOException
   */
  public static boolean compare(File file1, File file2) throws IOException {
    InputStream inputStream1 = null;
    InputStream inputStream2 = null;

    try {
      // create file input stream of the two bytes
      inputStream1 = new FileInputStream(file1);
      inputStream2 = new FileInputStream(file2);

      return compare(inputStream1, inputStream2);

    } finally {
      inputStream1.close();
      inputStream2.close();
    }
  }

  
  /**
   * compares two files and return true if the files have the same content.
   * 
   * @param filename1
   *          filename of the first file
   * @param in
   *          an input Sream
   * 
   * @return - true if the content is the same
   * 
   * @throws IOException
   */
  public static boolean compare(String filename1, InputStream in) throws IOException {
    InputStream file1 = null;

    try {
      // create file input stream of the two bytes
      file1 = new FileInputStream(filename1);

      return compare(file1, in);

    } finally {
      file1.close();
    }
  }

  public static boolean compare(InputStream in1, InputStream in2) throws IOException {
    int in1byte, in2byte;

    final int byteBufferSize = 10000;

    in1 = new BufferedInputStream(in1, byteBufferSize);
    in2 = new BufferedInputStream(in2, byteBufferSize);

    in1byte = 0;
    while (in1byte != -1) {
      // read one byte from file1
      in1byte = in1.read();

      // check if byte is whitespace or blank
      if ((!(Character.isWhitespace((char) in1byte))) && (in1byte != ' ') && (in1byte != '\n')
              && (in1byte != '\r')) {
        // read one byte form file2
        in2byte = in2.read();

        // read bytes until byte is no whitespace or blank
        while ((Character.isWhitespace((char) in2byte)) || (in2byte == ' ') || (in2byte == '\n')
                || (in2byte == '\r')) {
          // if byte is whitespace or blank read next byte
          in2byte = in2.read();
        }

        // check if byte from file1 and file2 are the same
        if (in1byte != in2byte) {
          return false; // file content of the two files are not the same
        }
      }
    }
    return true;
  }
  
  /**
   * Compare 2 strings, ignoring whitespace characters
   * @param in1
   * @param in2
   * @return
   */
  public static boolean compareStrings(String in1, String in2) {
    char c1, c2;

    int i1 = 0;
    int i2 = 0;
    
    while (i1 < in1.length()) {
      
      c1 = in1.charAt(i1 ++);

      // check if char is whitespace, and skip it
      if (Character.isWhitespace(c1)) {
        continue;
      }
      
      while (true) {
        if (i2 >= in2.length()) {
          return false;  // ran off the end of string 2
        }

        c2 = in2.charAt(i2 ++);
        if (!Character.isWhitespace(c2)) {
          break;
        }
      }
      
      if (c1 != c2) {
        return false;
      }
    }
    return true;
  }

  /**
   * Compares two XML files and returns true, if both have the same content. Different notations for
   * empty tags are considered equal.
   * 
   * @param filename1
   *          Filename of the first XML file.
   * @param filename2
   *          Filename of the second XML file.
   * @return
   * @throws IOException
   */
  public static boolean compareXML(String filename1, String filename2) throws IOException {
    File file1 = null;
{'fraction_non_alphanumeric': 0.09315273392055842, 'fraction_numerical': 0.016619577862722286, 'mean_word_length': 2.5179771996492253, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3960472', 'n_tokens_mistral': 3468, 'n_tokens_neox': 3339, 'n_words': 1284}