[1114] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.IO;
|
---|
| 4 | using System.Text.RegularExpressions;
|
---|
| 5 |
|
---|
| 6 | namespace Oni
|
---|
| 7 | {
|
---|
| 8 | internal abstract class Exporter
|
---|
| 9 | {
|
---|
| 10 | private readonly InstanceFileManager fileManager;
|
---|
| 11 | private readonly string outputDirPath;
|
---|
| 12 | private readonly Dictionary<string, string> fileNames;
|
---|
| 13 | private Regex nameFilter;
|
---|
| 14 |
|
---|
| 15 | protected Exporter(InstanceFileManager fileManager, string outputDirPath)
|
---|
| 16 | {
|
---|
| 17 | this.fileManager = fileManager;
|
---|
| 18 | this.outputDirPath = outputDirPath;
|
---|
| 19 | this.fileNames = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | public InstanceFileManager InstanceFileManager => fileManager;
|
---|
| 23 |
|
---|
| 24 | public string OutputDirPath => outputDirPath;
|
---|
| 25 |
|
---|
| 26 | public Regex NameFilter
|
---|
| 27 | {
|
---|
| 28 | get { return nameFilter; }
|
---|
| 29 | set { nameFilter = value; }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | public void ExportFiles(IEnumerable<string> sourceFilePaths)
|
---|
| 33 | {
|
---|
| 34 | Directory.CreateDirectory(outputDirPath);
|
---|
| 35 |
|
---|
| 36 | foreach (var sourceFilePath in sourceFilePaths)
|
---|
| 37 | ExportFile(sourceFilePath);
|
---|
| 38 |
|
---|
| 39 | Flush();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | protected virtual void ExportFile(string sourceFilePath)
|
---|
| 43 | {
|
---|
| 44 | Console.WriteLine(sourceFilePath);
|
---|
| 45 |
|
---|
| 46 | var file = fileManager.OpenFile(sourceFilePath);
|
---|
| 47 | var descriptors = GetSupportedDescriptors(file);
|
---|
| 48 |
|
---|
| 49 | if (nameFilter != null)
|
---|
| 50 | descriptors = descriptors.FindAll(x => x.HasName && nameFilter.IsMatch(x.FullName));
|
---|
| 51 |
|
---|
| 52 | foreach (var descriptor in descriptors)
|
---|
| 53 | ExportInstance(descriptor);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | protected abstract void ExportInstance(InstanceDescriptor descriptor);
|
---|
| 57 |
|
---|
| 58 | protected virtual void Flush()
|
---|
| 59 | {
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | protected virtual List<InstanceDescriptor> GetSupportedDescriptors(InstanceFile file)
|
---|
| 63 | {
|
---|
| 64 | return file.GetNamedDescriptors();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | protected string CreateFileName(InstanceDescriptor descriptor, string fileExtension)
|
---|
| 68 | {
|
---|
| 69 | string name = Importer.EncodeFileName(descriptor.FullName, fileNames);
|
---|
| 70 | return Path.Combine(outputDirPath, name + fileExtension);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | }
|
---|