Beispiel #1
0
        public void TestEquals()
        {
            var o1 = new OutputFormat();
            var o2 = new OutputFormat();
            Assert.IsTrue(o1.Equals(o2));
            Assert.IsTrue(o1.Equals(o2));
            Assert.IsTrue(o2.Equals(o1));

            o2.Name = "a";
            Assert.IsTrue(!o1.Equals(o2));
            Assert.IsTrue(!o2.Equals(o1));
            o2.Name = o1.Name;

            o2.Header = "aa";
            Assert.IsTrue(!o1.Equals(o2));
            Assert.IsTrue(!o2.Equals(o1));
            o2.Header = o1.Header;

            o2.Footer = "bb";
            Assert.IsTrue(!o1.Equals(o2));
            Assert.IsTrue(!o2.Equals(o1));
            o2.Footer = o1.Footer;

            o2.EachItem = "cc";
            Assert.IsTrue(!o1.Equals(o2));
            Assert.IsTrue(!o2.Equals(o1));
            o2.EachItem = o1.EachItem;

            o2.EachItemAlternate = "dd";
            Assert.IsTrue(!o1.Equals(o2));
            Assert.IsTrue(!o2.Equals(o1));
            o2.EachItemAlternate = o1.EachItemAlternate;
        }
Beispiel #2
0
 private void BtnSave_Click(object sender, EventArgs e)
 {
     var of = new OutputFormat();
     of.Header = "header:" + (DateTime.Now).ToShortDateString();
     outputFormatBindingSource.DataSource = of;
     MessageBox.Show("Save");
 }
Beispiel #3
0
 /// <summary>
 /// コピーコンストラクタ
 /// </summary>
 /// <param name="o"></param>
 public OutputFormat(OutputFormat o)
 {
     this.Name = o.Name;
     this.Header = o.Header;
     this.EachItem = o.EachItem;
     this.EachItemAlternate = o.EachItemAlternate;
     this.Footer = o.Footer;
 }
        /// <summary>
        /// 指定したディレクトリ上のOutputFormatオブジェクトを削除し、trueを返す.
        /// 指定したディレクトリにファイルがないか、読み取り専用である場合は削除せずfalseを返す.
        /// 削除中に失敗した場合は例外が発生する.
        /// </summary>
        /// <param name="of">OutputFormatオブジェクト</param>
        /// <param name="dirInfo">対象ディレクトリ</param>
        /// <returns>削除された場合はtrue、削除できない場合はfalse</returns>
        public static bool Delete(OutputFormat of, DirectoryInfo dirInfo)
        {
            if (of == null || dirInfo == null) throw new ArgumentException("nullは指定できません");
            string fileName = GetRealFileName(of.Name) + "." + FILE_EXT;

            var path = Path.Combine(dirInfo.FullName, fileName);
            System.Diagnostics.Trace.WriteLine("Delete: " + path);

            var fileInfo = new FileInfo(path);
            if (fileInfo.Exists && !fileInfo.IsReadOnly)
            {
                fileInfo.Delete();
                return true;
            }
            return false;
        }
        public void TestSaveAndLoadAndDelete()
        {
            string dir = Path.Combine(Path.GetTempPath(), "enumfile-test");
            var dirInfo = new DirectoryInfo(dir);
            if (dirInfo.Exists)
            {
                dirInfo.Delete(true);
            }

            var of = new OutputFormat();
            of.Name = "of1";
            of.Header = "header1";
            of.Footer = "footer1";
            of.EachItem = "eachItem1";
            of.EachItemAlternate = "eachItemAlt1";

            OutputFormatPersistent.Save(of, dirInfo);

            var of2 = of.duplicate();
            of2.Name = "of?2";

            OutputFormatPersistent.Save(of2, dirInfo);

            var results = OutputFormatPersistent.Load(dir).ToArray();

            foreach (OutputFormat iof in results)
            {
                Console.WriteLine(iof);
            }

            Assert.AreEqual(results.Length, 2);

            OutputFormatPersistent.Delete(of, dirInfo);
            OutputFormatPersistent.Delete(of2, dirInfo);

            dirInfo.Delete();
        }
        public void TestCascade()
        {
            string progDir = Path.Combine(this.dirName, "prog");
            string allDir = Path.Combine(this.dirName, "all");
            string justDir = Path.Combine(this.dirName, "just");

            {
                Directory.CreateDirectory(progDir);
                Directory.CreateDirectory(allDir);
                Directory.CreateDirectory(justDir);

                var progDirInfo = new DirectoryInfo(progDir);
                var allDirInfo = new DirectoryInfo(allDir);
                var justDirInfo = new DirectoryInfo(justDir);

                Assert.AreEqual(progDirInfo.GetFiles().Count(), 0);
                Assert.AreEqual(allDirInfo.GetFiles().Count(), 0);
                Assert.AreEqual(justDirInfo.GetFiles().Count(), 0);

                var of1 = new OutputFormat()
                {
                    Name = "name1"
                };
                of1.Header = "a";

                var of2 = of1.duplicate();
                of2.Header = "b";

                var of3 = of1.duplicate();
                of3.Header = "c";

                var of4 = of1.duplicate();
                of4.Name = "name2";
                of4.Header = "d";

                var of5 = of1.duplicate();
                of5.Name = "name2";
                of5.Header = "e";

                OutputFormatPersistent.Save(of1, progDirInfo);
                OutputFormatPersistent.Save(of2, allDirInfo);
                OutputFormatPersistent.Save(of3, justDirInfo);

                OutputFormatPersistent.Save(of4, progDirInfo);
                OutputFormatPersistent.Save(of5, allDirInfo);
            }

            {
                var mgr = new OutputFormatManager();
                mgr.AddDirectory(new PersistentDirectoryInfo(progDir, false));
                mgr.AddDirectory(new PersistentDirectoryInfo(allDir, true));
                mgr.AddDirectory(new PersistentDirectoryInfo(justDir, true));

                mgr.Load();

                Assert.AreEqual(mgr.AllItems.Count(), 2);

                Assert.AreEqual(mgr["name1"].Header, "c");
                Assert.AreEqual(mgr["name2"].Header, "e");

                OutputFormat x;
                bool ret;

                ret = mgr.TryGetValue("name1", out x);
                Assert.IsTrue(ret);
                x.Footer = "x";

                mgr.Update(x);

                Assert.AreEqual(mgr["name1"].Footer, "x");

                ret = mgr.TryGetValue("name2", out x);
                Assert.IsTrue(ret);
                x.Footer = "y";

                mgr.Update(x);

                Assert.AreEqual(mgr["name2"].Footer, "y");

                mgr.Save();
            }

            {
                var mgr = new OutputFormatManager();
                mgr.AddDirectory(new PersistentDirectoryInfo(progDir, false));
                mgr.AddDirectory(new PersistentDirectoryInfo(allDir, true));
                mgr.AddDirectory(new PersistentDirectoryInfo(justDir, true));

                mgr.Load();

                Assert.AreEqual(mgr.AllItems.Count(), 2);

                Assert.AreEqual(mgr["name1"].Header, "c");
                Assert.AreEqual(mgr["name2"].Header, "e");
                Assert.AreEqual(mgr["name1"].Footer, "x");
                Assert.AreEqual(mgr["name2"].Footer, "y");

                mgr["name1"] = null;
                mgr.Remove(mgr["name2"]);

                Assert.AreEqual(mgr["name1"].Header, "b");
                Assert.AreEqual(mgr["name2"].Header, "d");

                mgr.Save();
            }

            {
                var mgr = new OutputFormatManager();
                mgr.AddDirectory(new PersistentDirectoryInfo(progDir, false));
                mgr.AddDirectory(new PersistentDirectoryInfo(allDir, true));
                mgr.AddDirectory(new PersistentDirectoryInfo(justDir, true));

                mgr.Load();

                Assert.AreEqual(mgr.AllItems.Count(), 2);

                Assert.AreEqual(mgr["name1"].Header, "b");
                Assert.AreEqual(mgr["name2"].Header, "d");
            }
        }
        public void TestSimpleLoadAndSave()
        {
            string progDir = Path.Combine(this.dirName, "prog");
            string allDir = Path.Combine(this.dirName, "all");
            string justDir = Path.Combine(this.dirName, "just");

            Directory.CreateDirectory(progDir);
            Directory.CreateDirectory(allDir);
            Directory.CreateDirectory(justDir);

            var mgr = new OutputFormatManager();
            mgr.AddDirectory(new PersistentDirectoryInfo(progDir, false));
            mgr.AddDirectory(new PersistentDirectoryInfo(allDir, true));
            mgr.AddDirectory(new PersistentDirectoryInfo(justDir, true));

            var of1 = new OutputFormat()
            {
                Name = "name1",
                Header = "header1",
                EachItem = "eachItem1",
                EachItemAlternate = "eachItem1Alt",
                Footer = "footer1"
            };

            Assert.AreEqual(mgr.AllItems.Count(), 0);
            Assert.IsNull(mgr["name1"]);
            Assert.IsNull(mgr["name2"]);

            mgr.Update(of1);

            Assert.AreEqual(mgr.AllItems.Count(), 1);
            Assert.IsNotNull(mgr["name1"]);
            Assert.IsNull(mgr["name2"]);

            var of2 = of1.duplicate();
            of2.Name = "name2";

            mgr.Update(of2);

            Assert.AreEqual(mgr.AllItems.Count(), 2);
            Assert.IsNotNull(mgr["name1"]);
            Assert.IsNotNull(mgr["name2"]);

            Assert.AreEqual(new DirectoryInfo(justDir).GetFiles().Count(), 0);

            mgr.Save();

            Assert.AreEqual(new DirectoryInfo(justDir).GetFiles().Count(), 2);

            mgr.Clear();

            Assert.AreEqual(mgr.AllItems.Count(), 0);

            mgr.Load();

            Assert.AreEqual(mgr.AllItems.Count(), 2);
            Assert.IsNotNull(mgr["name1"]);
            Assert.IsNotNull(mgr["name2"]);

            var of_o1 = mgr["name1"];
            Assert.AreEqual(of1, of_o1);

            var of_o2 = mgr["name2"];
            Assert.AreEqual(of2, of_o2);
        }
Beispiel #8
0
 /// <summary>
 /// 変更の有無を判定する.
 /// </summary>
 /// <param name="o">比較対象、null可</param>
 /// <returns>内容が一致すればfalse、一致しなければtrue</returns>
 public bool IsModified(OutputFormat o)
 {
     if ((object)o != null)
     {
         return !(this.Name == o.Name &&
             this.Header == o.Header &&
             this.EachItem == o.EachItem &&
             this.EachItemAlternate == o.EachItemAlternate &&
             this.Footer == o.Footer);
     }
     return false;
 }
Beispiel #9
0
        public void TestXmlSerialize()
        {
            var obj1 = new OutputFormat()
            {
                Name = "test1",
                Header = "header-1",
                EachItem = "item1",
                EachItemAlternate = "item2",
                Footer = "footer-1"
            };

            var xmlSerializer = new XmlSerializer(typeof(OutputFormat));
            using (var txtWr = new StringWriter())
            {
                using (XmlTextWriter wr = new XmlTextWriter(txtWr))
                {
                    wr.Formatting = Formatting.Indented;
                    xmlSerializer.Serialize(wr, obj1);
                }
                Console.WriteLine(txtWr.ToString());
            }
        }
        /// <summary>
        /// 既定の出力フォーマット(固定)を生成する.
        /// </summary>
        /// <returns></returns>
        protected virtual OutputFormatHolder CreateDefaultFormat(string typ)
        {
            OutputFormat of;
            switch (typ)
            {
                case "xml":
                    of = new OutputFormat()
                    {
                        Name = "Simple XML",
                        Header = "<files>",
                        EachItem = "<file sha1=\"%SHA1:XML%\">%PATH:XML%</file>",
                        Footer = "</files>"
                    };
                    break;

                case "text":
                default:
                    of = new OutputFormat()
                    {
                        Name = "Plain Text",
                        EachItem = "%PATH%\t%SHA1%"
                    };
                    break;
            }
            return new OutputFormatHolder()
                {
                    directoryInfo = new PersistentDirectoryInfo(".", false),
                    original = of,
                    current = of.duplicate()
                };
        }
        /// <summary>
        /// OutputFormatの設定を登録または更新する.
        /// 実際にファイルに反映するにはSaveメソッドを呼び出す必要がある.
        /// </summary>
        /// <param name="outputFormat">対象となる設定</param>
        /// <returns>更新または登録が許可された場合はtrue、許可されない場合はfalse</returns>
        public virtual bool Update(OutputFormat outputFormat)
        {
            if (outputFormat == null) throw new ArgumentException("nullは指定できません");

            string name = outputFormat.Name ?? "";

            OutputFormatHolder holder;
            PersistentDirectoryInfo nextWritableDir;
            if (dict.TryGetValue(name, out holder))
            {
                // すでに読み込み済みのものがある場合
                if (holder.directoryInfo.Writable)
                {
                    // 書き込み可能なディレクトリのものである場合は更新する
                    holder.current = outputFormat;
                    return true;
                }
                else
                {
                    // 書き込み可能でない場合、後続の書き込み可能な
                    // 末端のディレクトリを探す
                    var curdir = holder.directoryInfo;
                    nextWritableDir = GetAfterDirectories(curdir)
                        .Where(dir => dir.Writable).LastOrDefault();
                    if (nextWritableDir == null)
                    {
                        // 後続に書き込み可能なディレクトリがなければ更新不可能
                        return false;
                    }
                }
            }
            else
            {
                // 既存にない新しい名前の場合、もっとも末端の書込み可能なディレクトリを選択し、
                // 新規にエントリを作成する
                nextWritableDir = dirs.Where(dir => dir.Writable).LastOrDefault();
            }

            // 後続の末端ディレクトリにエントリを書き込む
            var newHolder = new OutputFormatHolder()
            {
                directoryInfo = nextWritableDir,
                original = null,
                current = outputFormat
            };
            caches.Add(newHolder);
            dict[name] = newHolder;

            return false;
        }
 /// <summary>
 /// 名前を指定して該当するOutputFormat設定を取得し、trueを返す.
 /// 設定が存在しない場合はfalseを返す.
 /// </summary>
 /// <param name="name">名前</param>
 /// <param name="of">設定の格納先</param>
 /// <returns>設定が存在したらtrue、存在しなければfalse</returns>
 public virtual bool TryGetValue(string name, out OutputFormat of)
 {
     OutputFormatHolder ret;
     if (dict.TryGetValue(name, out ret))
     {
         of = ret.current;
     }
     else
     {
         of = null;
     }
     return of != null;
 }
        /// <summary>
        /// 指定したOutputFormatの設定を削除する.
        /// 設定が存在しないか、削除可能でない場合は何もしない.
        /// </summary>
        /// <param name="outputFormat">対象となる設定</param>
        /// <returns>削除が許可された場合はtrue、許可されなかった場合はfalse</returns>
        public virtual bool Remove(OutputFormat outputFormat)
        {
            if (outputFormat == null) throw new ArgumentException("nullは指定できません");

            string name = outputFormat.Name ?? "";
            return Remove(name);
        }
        /// <summary>
        /// 指定した出力フォーマットが変更されているか判定する.
        /// 指定された名前のオリジナルの出力フォーマットとの差異を比較する.
        /// 指定された名前が、まだ保持されていない場合は変更ありとみなす.
        /// </summary>
        /// <param name="of">現在の出力フォーマット(名前による区別)</param>
        /// <returns>変更の有無</returns>
        public virtual bool IsModified(OutputFormat of)
        {
            if (of == null) return true;

            string name = of.Name;
            OutputFormatHolder holder;
            if (dict.TryGetValue(name, out holder))
            {
                return of.IsModified(holder.original);
            }
            return true;
        }
        /// <summary>
        /// 指定したディレクトリ上にOutputFormatオブジェクトを保存する.
        /// </summary>
        /// <param name="of">保存するOutputFormatオブジェクト</param>
        /// <param name="dirInfo">保存先ディレクトリ</param>
        public static void Save(OutputFormat of, DirectoryInfo dirInfo)
        {
            if (of == null || dirInfo == null) throw new ArgumentException("nullは指定できません");
            string fileName = GetRealFileName(of.Name) + "." + FILE_EXT;

            // ディレクトリが存在しなければ作成する.
            if (!dirInfo.Exists)
            {
                dirInfo.Create();
            }

            var path = Path.Combine(dirInfo.FullName, fileName);
            System.Diagnostics.Trace.WriteLine("Save: " + path);

            var fileInfo = new FileInfo(path);
            using (var fs = fileInfo.OpenWrite())
            {
                xmlSerializer.Serialize(fs, of);
            }
        }
Beispiel #16
0
        /// <summary>
        /// 出力フォーマット名コンボボックスのリストの選択を変更した場合のハンドラ
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboFormatName_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 選択した出力フォーマット名を取得する.
            string selName = ComboFormatName.Text;

            // 名前に該当する出力フォーマットを取得する
            var outputFormatMgr = EnumFileApp.OutputFormatManager;
            OutputFormat of = null;
            if (!string.IsNullOrWhiteSpace(selName))
            {
                of = outputFormatMgr[selName];
            }
            if (of == null)
            {
                // 該当がなければ空とする
                of = new OutputFormat();
            }
            // 選択を変更する
            Current = of;
        }