Skip to content

Commit

Permalink
Even if failed to move file to a different directory, app continues t…
Browse files Browse the repository at this point in the history
…o process other files, instead of break (stopping)

Creating the directory for the files to be moved to, if it doesn't already exist
Not attempting to load validation resolver if no schematron validation path was specified
  • Loading branch information
seanmcilvenna committed Jul 20, 2020
1 parent 6455a76 commit ef21e11
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ static void Main(string[] args)
};
db2Converter.Convert();
});

#if DEBUG
Console.WriteLine("Press <enter> to continue...");
Console.ReadLine();
#endif
}
}
}
17 changes: 13 additions & 4 deletions lib/BaseConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ public void Convert()
{
string[] xmlFiles = Directory.GetFiles(this.inputDirectory, "*.xml");

if (xmlFiles.Length == 0)
this.Log("No XML files found in input directory.");

foreach (var xmlFile in xmlFiles)
{
FileInfo fileInfo = new FileInfo(xmlFile);
Expand Down Expand Up @@ -216,11 +219,12 @@ public void Convert()
try
{
this.ProcessFile(xmlDoc, nsManager, fileInfo);
this.Log(string.Format("Done parsing/processing file {0}", fileInfo.Name));
}
catch (Exception ex)
{
this.Log(String.Format("Failed to process file {0} data due to: {1}", fileInfo.Name, ex.Message));
break;
continue;
}

try
Expand All @@ -246,21 +250,26 @@ public void Convert()

if (!String.IsNullOrEmpty(this.moveDirectory))
{
DirectoryInfo di = new DirectoryInfo(this.moveDirectory);

if (!di.Exists)
di.Create();

string destinationFilePath = Path.Combine(this.moveDirectory, fileInfo.Name);

// If configured to validate, move the file to a subdirectory "valid" or "invalid" depending on the validation results
if (this.validator.WillValidate)
destinationFilePath = Path.Combine(destinationFilePath, isSchemaValid ? "valid" : "invalid");

fileInfo.MoveTo(destinationFilePath);
}

this.Log(string.Format("Done processing file {0}", fileInfo.Name));
Console.WriteLine("Moved file to " + destinationFilePath);
}
}
catch (Exception ex)
{
this.Log(String.Format("Failed to validate and/or move file {0} data due to: {1}", fileInfo.Name, ex.Message));
break;
continue;
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions lib/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ public Validator(string schemaPath, string schematronPath)
this.schemaPath = schemaPath;
this.schematronPath = schematronPath;

this.processor.XmlResolver = new ValidatorResolver(new FileInfo(schematronPath).DirectoryName);
this.builder = this.processor.NewDocumentBuilder();
this.builder.BaseUri = new Uri("file://");

if (!String.IsNullOrEmpty(this.schemaPath))
{
XmlDocument xsdDoc = new XmlDocument();
Expand All @@ -64,6 +60,13 @@ public Validator(string schemaPath, string schematronPath)
this.readerSettings.ValidationType = ValidationType.Schema;
}
}

if (!string.IsNullOrEmpty(this.schematronPath))
{
this.processor.XmlResolver = new ValidatorResolver(new FileInfo(schematronPath).DirectoryName);
this.builder = this.processor.NewDocumentBuilder();
this.builder.BaseUri = new Uri("file://");
}
}

public bool ValidateSchema(string filePath)
Expand Down

0 comments on commit ef21e11

Please sign in to comment.