using it.stefanochizzolini.clown;
using bytesit.stefanochizzolini.clown.bytes;
using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.interaction;
using it.stefanochizzolini.clown.documents.interchange.metadata;
using it.stefanochizzolini.clown.documents.interaction.viewer;
using filesit.stefanochizzolini.clown.files;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
namespace it.stefanochizzolini.clown.samples{
public class SampleLoader
{
#region static
#region fields
private static readonly string ClassName = (typeof(SampleLoader)).FullName;
private static readonly string Properties_InputPath = ClassName + ".inputPath";
private static readonly string Properties_OutputPath = ClassName + ".outputPath";
#endregion
#region interface
#region public
public static void Main(
string[] args
)
{
Console.WriteLine("\nSampleLoader running...");
{
Assembly pdfClownAssembly = Assembly.GetAssembly(typeof(Engine));
Console.WriteLine("\n" + ((AssemblyTitleAttribute)pdfClownAssembly.GetCustomAttributes(typeof(AssemblyTitleAttribute),false)[0]).Title + " version " + pdfClownAssembly.GetName().Version);
}
SampleLoader loader = new SampleLoader(
ConfigurationSettings.AppSettings.Get(Properties_InputPath),
ConfigurationSettings.AppSettings.Get(Properties_OutputPath)
);
loader.Run();
Console.WriteLine("\nSampleLoader finished.\n");
}
#endregion
#endregion
#endregion
#region dynamic
#region fields
private string inputPath;
private string outputPath;
#endregion
#region constructors
public SampleLoader(
string inputPath,
string outputPath
)
{
this.inputPath = inputPath;
this.outputPath = outputPath;
}
#endregion
#region interface
#region public
public string GetFileChoice(
string fileExtension,
string listDescription,
string inputDescription
)
{
Console.WriteLine("\n" + listDescription + ":");
// Get the list of available PDF files!
string[] filePaths = Directory.GetFiles(inputPath + "pdf" + Path.DirectorySeparatorChar,"*." + fileExtension);
// Display files!
for(
int i = 0;
i < filePaths.Length;
i++
)
{Console.WriteLine("[" + i + "] {0}", System.IO.Path.GetFileName(filePaths[i]));}
// Get the user's choice!
Console.Write(inputDescription + ": ");
try
{return filePaths[Int32.Parse(Console.ReadLine())]; /* Custom choice. */}
catch
{return filePaths[0]; /* Default choice. */}
}
public string GetPdfFileChoice(
string inputDescription
)
{
return GetFileChoice(
"pdf",
"Available PDF files",
inputDescription
);
}
public string InputPath
{get{return inputPath;}}
public string OutputPath
{get{return outputPath;}}
public void Run(
)
{
while(true)
{
// Get the current assembly!
Assembly assembly = Assembly.GetExecutingAssembly();
// Get all the types inside the current assembly!
Type[] types = assembly.GetTypes();
Console.WriteLine("\nAvailable samples:");
// Instantiate the list of available samples!
List<Type> sampleTypes = new List<Type>();
// Picking available samples...
foreach(Type type in types)
{
if(type.GetInterface("ISample") != null)
{
sampleTypes.Add(type);
Console.WriteLine("[" + sampleTypes.IndexOf(type) + "] {0}", type.Name);
}
}
Console.WriteLine("[Q] (Quit)");
// Getting the user's choice...
Type sampleType = null;
Console.Write("Please select a sample: ");
try
{
string choice = Console.ReadLine();
if(choice.ToUpper().Equals("Q")) // Quit.
break;
sampleType = sampleTypes[Int32.Parse(choice)]; // Custom choice.
}
catch
{
sampleType = sampleTypes[0]; // Default choice.
}
Console.WriteLine("\nInstantiating " + sampleType.Name + " sample...");
// Instantiate the sample!
ISample sample = (ISample)Activator.CreateInstance(sampleType);
Console.WriteLine("Running " + sampleType.Name + " sample...");
// Run the sample!
sample.Run(this);
}
}
public void Serialize(
files::File file,
string outputFileName
)
{
Serialize(
file,
outputFileName,
true
);
}
public void Serialize(
files::File file,
string outputFileName,
bool choiceMode
)
{
files::SerializationModeEnum serializationMode = files::SerializationModeEnum.Incremental;
if(choiceMode)
{
Console.WriteLine("[0] Standard serialization");
Console.WriteLine("[1] Incremental update");
// Get the user's choice.
Console.Write("Please select a serialization mode: ");
try
{serializationMode = (files::SerializationModeEnum)Int32.Parse(Console.ReadLine());}
catch
{/* Default. */}
}
string outputFilePath = outputPath + outputFileName + "." + serializationMode + ".pdf";
// Save the file!
file.WriteTo(
outputFilePath,
serializationMode
);
// Alternatively, defining an appropriate target stream:
/*
FileStream outputStream = new System.IO.FileStream(
outputFilePath,
System.IO.FileMode.Create,
System.IO.FileAccess.Write
);
file.WriteTo(
new bytes::Stream(outputStream),
serializationMode
);
outputStream.Flush();
outputStream.Close();
*/
Console.WriteLine("Output: "+ outputFilePath);
}
public void BuildAccessories(
Document document,
Type creator,
string title,
string subject
)
{
// Viewer preferences.
ViewerPreferences view = new ViewerPreferences(document); // Instantiates viewer preferences inside the document context.
document.ViewerPreferences = view; // Assigns the viewer preferences object to the viewer preferences function.
view.DisplayDocTitle = true;
// Document metadata.
Information info = new Information(document);
document.Information = info;
info.Author = "Stefano Chizzolini";
info.CreationDate = DateTime.Now;
info.Creator = creator.AssemblyQualifiedName;
info.Title = "PDF Clown - " + title + " sample";
info.Subject = "Sample about " + subject + " using PDF Clown";
}
#endregion
#endregion
#endregion
}
}
|