using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Packaging;
using System.Xml;
namespace MyConverter
{
class
ConverterShell
{
#region
Private Members
private
string[] inputArgs = null;
private
string localPathtoFile = null;
private
string localSettings = null;
private
string localLogPath = null;
private
string localOutputFullPath = null;
private
const string ConverterName = "Xml to Xml";
private
const string InputExtension = ".xml";
private
const string OutputExtension = ".xml";
private
const string InputArgInText = "-in";
private
const string InputArgSettingsText = "-settings";
private
const string InputArgOutText = "-out";
private
const string InputArgLogText = "-log";
private
StreamWriter streamLog = null;
#endregion
#region
main
static
void Main(string[] args)
{
//
Create New Converter
ConverterShell localConverter = new ConverterShell();
localConverter.inputArgs = args;
//
Get command line
localConverter.GetCommandLine(localConverter.inputArgs);
//
Verify the files and copy
if
(localConverter.VerifyFilesExist())
{
XmlToXml myConverter = new
XmlToXml(localConverter.localOutputFullPath);
}
return;
}
#endregion
#region
support routines
bool
VerifyFilesExist()
{
StreamReader streamIn = null;
bool disableCopy = false;
if
(this.localPathtoFile.Equals(this.localOutputFullPath))
{
disableCopy = true;
}
if
(this.localOutputFullPath.Length.Equals(0))
{
LogResult(" Output file " + this.localOutputFullPath +
"is null ");
LogResult(" Please use a different file for the output file
");
return false;
}
try
{
Stream streamInTmp = File.OpenRead(this.localPathtoFile);
streamIn = new StreamReader(streamInTmp);
streamIn.Close();
}
catch
{
LogResult("Error Input File for reading" +
this.localPathtoFile);
return false;
}
LogResult(" Converting file " + this.localPathtoFile +
" to " + this.localOutputFullPath);
//
Copy the file to the output path
if
(!disableCopy)
{
try
{
File.Copy(this.localPathtoFile, this.localOutputFullPath, true);
}
catch
{
LogResult("Error copying file to " +
this.localOutputFullPath);
return false;
}
}
return true;
}
// Get
Command Line Parameters
public
void GetCommandLine(string[] args)
{
int
i = 0;
//
Get all of the command line arguments
//
Console.WriteLine(" args " + args[0] + " " +
args[1] +
// " "
+ args[2] + " " + args[3] + " " + args[4] + " "
+ args[5]);
try
{
while (i < 8 && args[i] != null)
{
if (args[i] == InputArgInText && args[i + 1] != null)
{
this.localPathtoFile
= args[i + 1];
i += 2;
continue;
}
if (args[i] == InputArgSettingsText && args[i + 1] != null)
{
this.localSettings =
args[i + 1];
i += 2;
continue;
}
if (args[i] ==
InputArgOutText && args[i + 1] != null)
{
this.localOutputFullPath = args[i + 1];
i += 2;
continue;
}
if (args[i] == InputArgLogText
&& args[i + 1] != null)
{
this.localLogPath =
args[i + 1];
i += 2;
continue;
}
i += 2;
}
}
catch (IndexOutOfRangeException e)
{
// Need to eat the exception since we do not want to end because of
insufficient args
return;
}
return;
}
void
LogResult(string value)
{
try
{
Stream streamLogTmp = File.Open(this.localLogPath, FileMode.Append);
this.streamLog = new StreamWriter(streamLogTmp);
this.streamLog.WriteLine(value);
this.streamLog.Close();
}
catch
{
Console.WriteLine(value);
}
}
#endregion
}
}
|