<commit_before>/*
 * Copyright (c) 2013, The National Archives <EMAIL>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package uk.gov.tna.dri.validator

import java.io.{FileReader, File}
import uk.gov.tna.dri.EOL
import uk.gov.tna.dri.schema.{Schema, SchemaParser}
import scalaz.{Success => SuccessZ, Failure => FailureZ, _}
import Scalaz._
import scala.App
import uk.gov.tna.dri.schema.Validator.{JWarningMessage, JFailMessage, JErrorMessage}
import java.util

object  SystemExits {
  val ValidCsv = 0
  val IncorrectArguments = 1
  val InvalidSchema = 2
  val InvalidCsv = 3
}

object MetaDataValidatorCommandLineApp extends App {
  type AppValidation[S] = ValidationNEL[FailMessage, S]

  val (exitMsg, exitCode) = run(args)
  println(exitMsg)
  System.exit(exitCode)

  def run(args: Array[String]): (String,Int) = {

    val result = for {
      ((metaDataFile, schemaFile), remainderArgs) <- findFiles(args.toList)
      (failFast, remainderArgs1) <- findFailFast(remainderArgs)
      (pathSubstitutionsList, remainderArgs2) <- findPaths(remainderArgs1)
      (_,_) <- unknownParams(remainderArgs2)
    } yield  {
      processMetaData(metaDataFile, schemaFile, failFast, pathSubstitutionsList)
    }

    result match {
      case Right(r) => (r._1, r._2)
      case Left(errMsg) => (errMsg, SystemExits.IncorrectArguments)
    }
  }

  private def createValidator(failFast: Boolean, pathSubstitutionsList: List[(String,String)]) =
    if (failFast) new MetaDataValidatorApp with FailFastMetaDataValidator { val pathSubstitutions = pathSubstitutionsList }
    else new MetaDataValidatorApp with AllErrorsMetaDataValidator { val pathSubstitutions = pathSubstitutionsList }


  private def processMetaData(metaDataFile: String, schemaFile: String, failFast: Boolean, pathSubstitutionsList: List[(String,String)] ): (String,Int) = {
    val validator = createValidator (failFast, pathSubstitutionsList)
    validator.parseSchema(schemaFile) match {
      case FailureZ(errors) => (prettyPrint(errors), SystemExits.InvalidSchema)
      case SuccessZ(schema) =>
        validator.validate(metaDataFile, schema) match {
          case FailureZ(errors) => (prettyPrint(errors), SystemExits.InvalidCsv)
          case SuccessZ(_) => ("PASS", SystemExits.ValidCsv)
        }
    }
  }
<commit_msg>[feature]  missing from last commit, make function available for external library calls<commit_after>/*
 * Copyright (c) 2013, The National Archives <EMAIL>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package uk.gov.tna.dri.validator

import java.io.{FileReader, File}
import uk.gov.tna.dri.EOL
import uk.gov.tna.dri.schema.{Schema, SchemaParser}
import scalaz.{Success => SuccessZ, Failure => FailureZ, _}
import Scalaz._
import scala.App
import uk.gov.tna.dri.schema.Validator.{JWarningMessage, JFailMessage, JErrorMessage}
import java.util

object  SystemExits {
  val ValidCsv = 0
  val IncorrectArguments = 1
  val InvalidSchema = 2
  val InvalidCsv = 3
}

object MetaDataValidatorCommandLineApp extends App {
  type AppValidation[S] = ValidationNEL[FailMessage, S]

  val (exitMsg, exitCode) = run(args)
  println(exitMsg)
  System.exit(exitCode)

  def run(args: Array[String]): (String,Int) = {

    val result = for {
      ((metaDataFile, schemaFile), remainderArgs) <- findFiles(args.toList)
      (failFast, remainderArgs1) <- findFailFast(remainderArgs)
      (pathSubstitutionsList, remainderArgs2) <- findPaths(remainderArgs1)
      (_,_) <- unknownParams(remainderArgs2)
    } yield  {
      processMetaData(metaDataFile, schemaFile, failFast, pathSubstitutionsList)
    }

    result match {
      case Right(r) => (r._1, r._2)
      case Left(errMsg) => (errMsg, SystemExits.IncorrectArguments)
    }
  }

  private def createValidator(failFast: Boolean, pathSubstitutionsList: List[(String,String)]) =
    if (failFast) new MetaDataValidatorApp with FailFastMetaDataValidator { val pathSubstitutions = pathSubstitutionsList }
    else new MetaDataValidatorApp with AllErrorsMetaDataValidator { val pathSubstitutions = pathSubstitutionsList }


  def processMetaData(metaDataFile: String, schemaFile: String, failFast: Boolean, pathSubstitutionsList: List[(String,String)] ): (String,Int) = {
    val validator = createValidator (failFast, pathSubstitutionsList)
    validator.parseSchema(schemaFile) match {
      case FailureZ(errors) => (prettyPrint(errors), SystemExits.InvalidSchema)
      case SuccessZ(schema) =>
        validator.validate(metaDataFile, schema) match {
          case FailureZ(errors) => (prettyPrint(errors), SystemExits.InvalidCsv)
          case SuccessZ(_) => ("PASS", SystemExits.ValidCsv)
        }
    }
  }

{'fraction_non_alphanumeric': 0.10506227400562475, 'fraction_numerical': 0.007231820008035355, 'mean_word_length': 4.263213530655391, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4446676', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1525, 'n_words': 382}