Пример #1
0
        /// <summary>
        /// Converts a single file into a section in OneNote notebook
        /// </summary>
        /// <param name="converter"></param>
        /// <param name="inputFile"></param>
        /// <param name="outputDir"></param>
        private static void ConvertFile(IFormatConverter converter, string inputFile, string outputDir)
        {
            var file = new FileInfo(inputFile);

            if (Utility.WordSupportedExtenssions.Any(
                    ext => ext.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)))
            {
                converter.ConvertWordToOneNote(inputFile, outputDir);
            }
            else if (Utility.PowerPointSupportedExtenssions.Any(
                         ext => ext.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)))
            {
                converter.ConvertPowerPointToOneNote(inputFile, outputDir);
            }
            else if (Utility.PdfSupportedExtenssions.Any(
                         ext => ext.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)))
            {
                converter.ConvertPdfToOneNote(inputFile, outputDir);
            }
            else if (Utility.EpubSupportedExtenssions.Any(
                         ext => ext.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)))
            {
                converter.ConvertEpubToOneNote(inputFile, outputDir);
            }
            else if (Utility.InDesignSupportedExtenssions.Any(
                         ext => ext.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)))
            {
                converter.ConvertInDesignToOneNote(inputFile, outputDir);
            }
        }
Пример #2
0
        /// <summary>
        /// The internal implementation of the method that goes into the directory and tries to convert all
        /// files underneath it recursively.
        /// </summary>
        /// <param name="converter"></param>
        /// <param name="inputDir"></param>
        /// <param name="outputDir"></param>
        /// <returns></returns>
        private static ICollection <string> ConvertDirectoryImpl(IFormatConverter converter, string inputDir, string outputDir)
        {
            var files             = Directory.GetFiles(inputDir);
            var dirs              = Directory.GetDirectories(inputDir);
            var notSupportedFiles = new List <string>();

            // Iterate through all files in this directory
            foreach (var file in files)
            {
                try
                {
                    ConvertFile(converter, file, outputDir);
                }
                catch (NotSupportedException)
                {
                    //Do not block the conversion process, but keep the list of files unable to be converted
                    notSupportedFiles.Add(file);
                }
            }

            // Iterate through all directories in this directory
            foreach (var dir in dirs)
            {
                notSupportedFiles.AddRange(ConvertDirectoryImpl(converter, dir, outputDir));
            }

            return(notSupportedFiles);
        }
Пример #3
0
 public Table(string filePath, IFormatConverter conv)
 {
     //Trace.Assert(conv != null, "Converter == null");
     Trace.Assert(filePath != null, "tableName == null");
     name = Path.GetFileName(filePath);
     Contract.Ensures(name != null);
     Restore(filePath, conv);
 }
Пример #4
0
        /// <summary>
        /// Helper method to iterate through a directory and convert all the files underneath it
        /// </summary>
        /// <param name="converter"></param>
        /// <param name="inputDir"></param>
        /// <param name="outputDir"></param>
        private static void ConvertDirectory(IFormatConverter converter, string inputDir, string outputDir)
        {
            var notSupportedFiles = ConvertDirectoryImpl(converter, inputDir, outputDir);

            if (notSupportedFiles.Count > 0)
            {
                throw new NotSupportedException(string.Format("The following files were not converted:\n{0}",
                                                              string.Join("\n", notSupportedFiles.ToArray())));
            }
        }
Пример #5
0
        public override void Execute(object parameter)
        {
            if (parameter is Format format)
            {
                switch (format)
                {
                case Format.PNG:
                    converter = new PngConverter();
                    break;

                case Format.NAT:
                    converter = new NatConverter();
                    break;

                case Format.Unknown:
                    throw new Exception("Unknown Format");

                default:
                    break;
                }

                if (string.IsNullOrEmpty(vm.SelectedPath))
                {
                    return;
                }

                var paths = new string[] { vm.SelectedPath };

                if ((File.GetAttributes(vm.SelectedPath) & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    paths = Directory.GetFiles(vm.SelectedPath, "*.bmp", SearchOption.AllDirectories);
                }

                foreach (var path in paths)
                {
                    var succeed = converter.Convert(path);
                    if (succeed)
                    {
                        vm.UpdateView();
                    }
                    else
                    {
                        vm.ConvertionError = new ConvertionError {
                            Format = format
                        };
                    }
                }
            }
        }
Пример #6
0
        public static string RESTORE_Command(string param)
        {
            Trace.Assert(param != null, "param == null");
            if (param == "-h")
            {
                return("Restore [path [CSV]]          |-> Restore the database from a provided or default path " +
                       "\nRestore tbName [path [CSV]]   |-> Restore a table from current DB from a provided or default path");
            }
            check(currentDb != noDb, "Not allowed");
            if (param == "")
            {
                currentDb.Restore(); return("Database " + currentDb.name + " restored.");
            }
            IFormatConverter conv = null;

            if (param.EndsWith(" CSV"))
            {
                conv  = new CSVformatConverter();
                param = param.Substring(0, param.Length - 4);
            }
            if (param[1] == ':')
            {
                Contract.Ensures(conv != null);
                currentDb.Restore(param, conv);
                Contract.Requires(currentDb.name != null);
                return("Database " + currentDb.name + " restored.");
            }
            int fSpacePos = param.IndexOf(' ');

            if (fSpacePos == -1)
            {
                fSpacePos = param.Length;
            }
            string tbName  = param.Substring(0, fSpacePos).Trim();
            string dstPath = param.Substring(fSpacePos).Trim();

            if (dstPath.Length > 0)
            {
                currentDb[tbName].Restore(dstPath, conv);
            }
            else
            {
                currentDb[tbName].Restore(Path.Combine(currentDb.GetDirName(), tbName));
            }
            Contract.Requires(tbName != null);
            return("Table " + tbName + " restored.");
        }
Пример #7
0
 public bool Save(string dirPath, IFormatConverter conv = null)
 {
     Trace.Assert(dirPath != null, "dirPath == null");
     try
     {
         Directory.CreateDirectory(dirPath);
         if (conv == null)
         {
             conv = new DefaultFormatConverter();
         }
         StreamWriter file = new StreamWriter(Path.Combine(dirPath, name));
         file.WriteLine(conv.Encode(columnName.ToArray()));
         for (int i = 0; i < Count; i++)
         {
             file.WriteLine(conv.Encode(this[i].ToArray()));
         }
         file.Close();
     }
     catch (Exception e) { throw new TableException(e.ToString()); }
     return(true);
 }
Пример #8
0
 public bool Restore(string srcPath = "", IFormatConverter conv = null)
 {
     Trace.Assert(srcPath != null, "srcPath == null");
     Debug.Write("Restoring db:" + name);
     if (srcPath.Length == 0)
     {
         srcPath = GetDirName();
     }
     check(Directory.Exists(srcPath), "Invalid db load path path:" + srcPath);
     Clear();
     foreach (string tablePath in Directory.EnumerateFiles(srcPath))
     {
         Contract.Invariant(tablePath != null);
         Contract.Requires(tablePath != null);
         Contract.Requires(tablePath != "");
         try { Add(new Table(tablePath, conv)); }
         catch { Debug.Write("Can't add table from " + tablePath); }
     }
     Contract.Ensures(this != null);
     Contract.Ensures(this.Count > 0);
     return(true);
 }
Пример #9
0
 public bool Save(string destDir = "", IFormatConverter conv = null)
 {
     Trace.Assert(destDir != null, "destDir == null");
     if (destDir.Length == 0)
     {
         destDir = GetDirName();
     }
     try
     {
         Directory.Delete(destDir);
     }
     catch { }
     try
     {
         Directory.CreateDirectory(destDir);
         foreach (Table table in Values)
         {
             table.Save(destDir, conv);
         }
     }
     catch { throw new DatabaseException("Save to " + destDir + " failed!"); }
     return(true);
 }
Пример #10
0
        public bool Restore(string filePath, IFormatConverter conv = null)
        {
            Trace.Assert(filePath != null, "dirPath == null");
            string line;

            try
            {
                if (conv == null)
                {
                    conv = new DefaultFormatConverter();
                }
                StreamReader file = new StreamReader(filePath);
                Clear();
                columnName = new ColumnNames(conv.Decode(file.ReadLine()));
                while ((line = file.ReadLine()) != null)
                {
                    Contract.Invariant(line != null);
                    AddLine(conv.Decode(line.Trim()));
                }
            }
            catch (Exception e) { Debug.Write("Error restoring:" + name); throw new TableException(e.ToString()); }
            return(true);
        }
Пример #11
0
 public Converter(IFormatConverter specialist)
 {
     converterSpecialist = specialist;
 }
		/// <summary>
		/// Helper method to iterate through a directory and convert all the files underneath it
		/// </summary>
		/// <param name="converter"></param>
		/// <param name="inputDir"></param>
		/// <param name="outputDir"></param>
		private static void ConvertDirectory(IFormatConverter converter, string inputDir, string outputDir)
		{
			var notSupportedFiles = ConvertDirectoryImpl(converter, inputDir, outputDir);
			
			if (notSupportedFiles.Count > 0)
			{
				throw new NotSupportedException(string.Format("The following files were not converted:\n{0}",
					string.Join("\n", notSupportedFiles.ToArray())));
			}
		}
		/// <summary>
		/// Converts a single file into a section in OneNote notebook
		/// </summary>
		/// <param name="converter"></param>
		/// <param name="inputFile"></param>
		/// <param name="outputDir"></param>
		private static void ConvertFile(IFormatConverter converter, string inputFile, string outputDir)
		{
			var file = new FileInfo(inputFile);
			if (Utility.WordSupportedExtenssions.Any(
				ext => ext.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)))
			{
				converter.ConvertWordToOneNote(inputFile, outputDir);
			}
			else if (Utility.PowerPointSupportedExtenssions.Any(
				ext => ext.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)))
			{
				converter.ConvertPowerPointToOneNote(inputFile, outputDir);
			}
			else if (Utility.PdfSupportedExtenssions.Any(
				ext => ext.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)))
			{
				converter.ConvertPdfToOneNote(inputFile, outputDir);
			}
			else if (Utility.EpubSupportedExtenssions.Any(
				ext => ext.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)))
			{
				converter.ConvertEpubToOneNote(inputFile, outputDir);
			}
			else if (Utility.InDesignSupportedExtenssions.Any(
				ext => ext.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)))
			{
				converter.ConvertInDesignToOneNote(inputFile, outputDir);
			}
		}
		/// <summary>
		/// The internal implementation of the method that goes into the directory and tries to convert all
		/// files underneath it recursively.
		/// </summary>
		/// <param name="converter"></param>
		/// <param name="inputDir"></param>
		/// <param name="outputDir"></param>
		/// <returns></returns>
		private static ICollection<string> ConvertDirectoryImpl(IFormatConverter converter, string inputDir, string outputDir)
		{
			var files = Directory.GetFiles(inputDir);
			var dirs = Directory.GetDirectories(inputDir);
			var notSupportedFiles = new List<string>();

			// Iterate through all files in this directory
			foreach (var file in files)
			{
				try
				{
					ConvertFile(converter, file, outputDir);
				}
				catch (NotSupportedException)
				{
					//Do not block the conversion process, but keep the list of files unable to be converted
					notSupportedFiles.Add(file);
				}
			}

			// Iterate through all directories in this directory
			foreach (var dir in dirs)
			{
				notSupportedFiles.AddRange(ConvertDirectoryImpl(converter, dir, outputDir));
			}

			return notSupportedFiles;
		}