Skip to content

Commit

Permalink
refactor to consolidate console code
Browse files Browse the repository at this point in the history
  • Loading branch information
JDziurlaj committed May 4, 2019
1 parent 28aa9b3 commit e2db318
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 54 deletions.
2 changes: 1 addition & 1 deletion CdfValidator.sln → CdfTools.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CdfValidator", "CdfTools\CdfTools.csproj", "{50D1F743-698E-41A0-887D-A8E2727C529F}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CdfTools", "CdfTools\CdfTools.csproj", "{50D1F743-698E-41A0-887D-A8E2727C529F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CdfTools.Tests", "CdfTools.Tests\CdfTools.Tests.csproj", "{BB62A6B2-8651-4961-9E40-344EDDBB3497}"
EndProject
Expand Down
27 changes: 5 additions & 22 deletions CdfTools/JsonSchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ namespace CdfTools
{
class JsonSchemaValidator
{
public static void jsonSchema(string inputPath, string schemaPath)
{
System.Console.WriteLine("Invoking JSON Schema Validation");
public static List<String> jsonSchema(string inputPath, string schemaPath)
{
using (StreamReader schemaFile = File.OpenText(schemaPath))
{
using (StreamReader inputFile = File.OpenText(inputPath))
Expand All @@ -24,12 +23,10 @@ public static void jsonSchema(string inputPath, string schemaPath)
{
validatingReader.Schema = schema;

IList<string> errorList = new List<string>();
List<string> errorList = new List<string>();
validatingReader.ValidationEventHandler += (o, a) =>
{
errorList.Add(a.Message);
// send out immediately
// Console.WriteLine(a.Message);
errorList.Add(a.Message);
};

JsonSerializer serializer = new JsonSerializer();
Expand All @@ -39,21 +36,7 @@ public static void jsonSchema(string inputPath, string schemaPath)
{
}

if (errorList.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
foreach (var error in errorList)
{
Console.WriteLine(error);
}
Console.WriteLine("JSON Instance has one or more errors");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("JSON Instance is Valid");
}
Console.ResetColor();
return errorList;
}
}
}
Expand Down
37 changes: 34 additions & 3 deletions CdfTools/ValidateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class ValidateCommand

[LegalFilePath]
[Option(Template = "-t|--schematron", Description = "Input schematron file")]
// [Required]
public string SchematronFile { get; }
// You can use this pattern when the parent command may have options or methods you want to
// use from sub-commands.
Expand All @@ -39,12 +38,44 @@ public void OnExecute()
throw new NotImplementedException("Schematron support is not available at this time");
// XmlValidationHarness.schematron(this.InputFile, this.SchematronFile);
}
XmlValidationHarness.xsd(this.InputFile, this.SchemaFile);
System.Console.WriteLine("Invoking XSD Validation");
var errorList = XmlValidationHarness.xsd(this.InputFile, this.SchemaFile);
if (errorList.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
foreach (var error in errorList)
{
Console.WriteLine(error);
}
Console.WriteLine("XML Instance has one or more errors");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("XML Instance is Valid");
}
Console.ResetColor();
}
else if (this.InputFile.EndsWith(".json"))
{
// note that schematron flag is ignored in this case
JsonSchemaValidator.jsonSchema(this.InputFile, this.SchemaFile);
System.Console.WriteLine("Invoking JSON Schema Validation");
var errorList = JsonSchemaValidator.jsonSchema(this.InputFile, this.SchemaFile);
if (errorList.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
foreach (var error in errorList)
{
Console.WriteLine(error);
}
Console.WriteLine("JSON Instance has one or more errors");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("JSON Instance is Valid");
}
Console.ResetColor();
}
else
{
Expand Down
36 changes: 8 additions & 28 deletions CdfTools/XmlValidationHarness.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System.Collections.Generic;
using System.Xml;
namespace CdfTools
{
Expand All @@ -17,7 +17,7 @@ public static void schematron(string inputFile, string schematronFile)
var nsmgr = new XmlNamespaceManager(svrl.NameTable);
nsmgr.AddNamespace("svrl", "http://purl.oclc.org/dsdl/svrl");
var errors = svrl.SelectNodes("/svrl:schematron-output/svrl:failed-assert", nsmgr);
if (errors.Count > 0)
/* if (errors.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
foreach (XmlNode error in errors)
Expand All @@ -30,44 +30,24 @@ public static void schematron(string inputFile, string schematronFile)
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Schematron validation passed");
}
Console.ResetColor();
Console.ResetColor();*/
//TODO: remove short circuit
return;
}


public static void xsd(string inputFile, string schemaFile)
public static List<string> xsd(string inputFile, string schemaFile)
{
try
{
// TODO: sanitize uris/filenames
System.Console.WriteLine("Invoking XSD Validation");
var validator = new XsdValidator();
var errorList = validator.Validate(schemaFile, inputFile);
if (errorList.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
foreach (var error in errorList)
{
Console.WriteLine(error);
}
Console.WriteLine("XML Instance has one or more errors");
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("XML Instance is Valid");
}
Console.ResetColor();
return validator.Validate(schemaFile, inputFile);

}
catch (System.Xml.Schema.XmlSchemaException)
catch (System.Xml.Schema.XmlSchemaException ex)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Problem processing the XML Schema Definition");
// if (Parent.Verbose)
// {
// Console.WriteLine(ex.Message);
// }
throw new System.Exception("Problem processing the XML Schema Definition", ex);
}
}
}
Expand Down

0 comments on commit e2db318

Please sign in to comment.