コード例 #1
0
ファイル: RoundtripTest.cs プロジェクト: gjunge/MARC4J.Net
        public void TestConvertToXMLNormalizedRoundtrip()
        {
            MemoryStream out1 = null;

            using (var input = new FileStream(Environment.CurrentDirectory + StaticTestRecords.RESOURCES_BRKRTEST_MRC, FileMode.Open))
            {
                Assert.IsNotNull(input);
                out1 = new MemoryStream();
                MarcStreamReader marcReader = new MarcStreamReader(input);
                MarcXmlWriter    xmlWriter  = new MarcXmlWriter(out1);
                xmlWriter.Converter            = new AnselToUnicode();
                xmlWriter.UnicodeNormalization = true;
                while (marcReader.MoveNext())
                {
                    var record = marcReader.Current;
                    xmlWriter.Write(record);
                }
                input.Close();

                xmlWriter.Close();
                out1.Close();
            }
            using (MemoryStream out2 = new MemoryStream())
            {
                using (MemoryStream @in = new MemoryStream(out1.ToArray()))
                {
                    var xmlReader = new MarcXmlReader(@in).GetEnumerator();
                    MarcStreamWriter marcWriter = new MarcStreamWriter(out2);
                    marcWriter.Converter = new UnicodeToAnsel();
                    while (xmlReader.MoveNext())
                    {
                        var record = xmlReader.Current;
                        marcWriter.Write(record);
                    }
                    @in.Close();
                    marcWriter.Close();

                    out2.Close();

                    using (var inputCompare1 = new FileStream(Environment.CurrentDirectory + StaticTestRecords.RESOURCES_BRKRTEST_MRC, FileMode.Open))
                    {
                        Assert.IsNotNull(inputCompare1);
                        var inputCompare2 = new MemoryStream(out2.ToArray());
                        MarcStreamReader readComp1 = new MarcStreamReader(inputCompare1);
                        MarcStreamReader readComp2 = new MarcStreamReader(inputCompare2);
                        IRecord          r1, r2;
                        do
                        {
                            r1 = (readComp1.MoveNext()) ? readComp1.Current : null;
                            r2 = (readComp2.MoveNext()) ? readComp2.Current : null;
                            if (r1 != null && r2 != null)
                            {
                                RecordTestingUtils.AssertEqualsIgnoreLeader(r1, r2);
                            }
                        } while (r1 != null && r2 != null);
                    }
                }
            }
        }
コード例 #2
0
ファイル: RoundtripTest.cs プロジェクト: gjunge/MARC4J.Net
        public void TestWriteAndReadRoundtripConverted()
        {
            MemoryStream out2 = null;

            using (var input = new FileStream(Environment.CurrentDirectory + StaticTestRecords.RESOURCES_BRKRTEST_MRC, FileMode.Open))
            {
                Assert.IsNotNull(input);
                var out1 = new MemoryStream();
                MarcStreamReader marcReader1 = new MarcStreamReader(input);
                MarcStreamWriter marcWriter1 = new MarcStreamWriter(out1, "UTF-8");
                marcWriter1.Converter = new AnselToUnicode();
                while (marcReader1.MoveNext())
                {
                    var record = marcReader1.Current;
                    marcWriter1.Write(record);
                }
                input.Close();
                marcWriter1.Close();
                out1.Close();
                out2 = new MemoryStream();
                var @in = new MemoryStream(out1.ToArray());
                MarcStreamReader marcReader2 = new MarcStreamReader(@in);
                MarcStreamWriter marcWriter2 = new MarcStreamWriter(out2);
                marcWriter2.Converter = new UnicodeToAnsel();
                while (marcReader2.MoveNext())
                {
                    var record = marcReader2.Current;
                    marcWriter2.Write(record);
                }
                @in.Close();
            }

            using (var inputCompare1 = new FileStream(Environment.CurrentDirectory + StaticTestRecords.RESOURCES_BRKRTEST_MRC, FileMode.Open))
            {
                var     inputCompare2 = out2;
                var     readComp1 = new MarcStreamReader(inputCompare1);
                var     readComp2 = new MarcStreamReader(inputCompare2);
                IRecord r1, r2;
                do
                {
                    r1 = (readComp1.MoveNext()) ? readComp1.Current : null;
                    r2 = (readComp2.MoveNext()) ? readComp2.Current : null;
                    if (r1 != null && r2 != null)
                    {
                        RecordTestingUtils.AssertEqualsIgnoreLeader(r1, r2);
                    }
                } while (r1 != null && r2 != null);
            }

            out2.Dispose();
        }
コード例 #3
0
ファイル: RoundtripTest.cs プロジェクト: gjunge/MARC4J.Net
        public void TestWriteAndReadRoundtrip()
        {
            MemoryStream out2 = null;

            using (var input = new FileStream(Environment.CurrentDirectory + StaticTestRecords.RESOURCES_CHABON_MRC, FileMode.Open))
            {
                Assert.IsNotNull(input);
                using (var out1 = new MemoryStream())
                {
                    MarcStreamReader marcReader = new MarcStreamReader(input);
                    using (MarcXmlWriter xmlWriter = new MarcXmlWriter(out1))
                    {
                        while (marcReader.MoveNext())
                        {
                            var record = marcReader.Current;
                            xmlWriter.Write(record);
                        }
                    }
                    out2 = new MemoryStream();
                    var @in       = new MemoryStream(out1.ToArray());
                    var xmlReader = new MarcXmlReader(@in).GetEnumerator();
                    MarcStreamWriter marcWriter = new MarcStreamWriter(out2);
                    while (xmlReader.MoveNext())
                    {
                        var record = xmlReader.Current;
                        marcWriter.Write(record);
                    }
                    @in.Close();
                }
            }
            using (var inputCompare1 = new FileStream(Environment.CurrentDirectory + StaticTestRecords.RESOURCES_CHABON_MRC, FileMode.Open))
            {
                var     inputCompare2 = out2;
                var     readComp1 = new MarcStreamReader(inputCompare1);
                var     readComp2 = new MarcStreamReader(inputCompare2);
                IRecord r1, r2;
                do
                {
                    r1 = (readComp1.MoveNext()) ? readComp1.Current : null;
                    r2 = (readComp2.MoveNext()) ? readComp2.Current : null;
                    if (r1 != null && r2 != null)
                    {
                        RecordTestingUtils.AssertEqualsIgnoreLeader(r1, r2);
                    }
                } while (r1 != null && r2 != null);
            }
            out2.Dispose();
        }
コード例 #4
0
ファイル: ReaderTest.cs プロジェクト: gjunge/MARC4J.Net
        public void TestMarcStreamReader()
        {
            using (var fs = new FileStream(Environment.CurrentDirectory + StaticTestRecords.RESOURCES_CHABON_MRC, FileMode.Open))
            {
                Assert.IsNotNull(fs);
                using (MarcStreamReader reader = new MarcStreamReader(fs))
                {
                    Assert.IsTrue(reader.MoveNext(), "Should have at least one record");

                    var record1 = reader.Current;
                    TestUtils.ValidateKavalieAndClayRecord(record1);

                    Assert.IsTrue(reader.MoveNext(), "Should have at least two records");
                    var record2 = reader.Current;
                    TestUtils.ValidateSummerlandRecord(record2);

                    Assert.IsFalse(reader.MoveNext(), " have more than two records");
                }
            }
        }
コード例 #5
0
        private void btnConectar_Click(object sender, EventArgs e)
        {
            string           user    = txtUser.Text;
            string           host    = txtHost.Text;
            string           senha   = txtSenha.Text;
            NpgsqlConnection Conexao = new NpgsqlConnection("Server=" + host + ";User Id=" + user + "; " +
                                                            "Password="******";Database=biblivre4;");

            Conexao.Open();
            NpgsqlCommand    sql    = new NpgsqlCommand("select iso2709 from single.biblio_records", Conexao);
            NpgsqlDataReader result = sql.ExecuteReader();

            System.IO.StreamWriter file = new System.IO.StreamWriter(Directory.GetCurrentDirectory() + "Livro.mrc");



            while (result.Read())
            {
                for (int i = 0; i < result.FieldCount; i++)
                {
                    file.Write(result[i].ToString());
                }
            }
            file.Close();

            using (var fs = new FileStream(Directory.GetCurrentDirectory() + "Livro.mrc", FileMode.Open))
            {
                using (IMarcReader reader = new MarcStreamReader(fs, "UTF-8"))
                {
                    foreach (var record in reader)
                    {
                        Livro livro = new Livro();
                        try {
                            livro.autor = (string)((record.GetVariableField("100").ToString()).Split(new string[] { "$a" }, StringSplitOptions.None)[1]).Split(new string[] { "$b" }, StringSplitOptions.None)[0];
                        }
                        catch {
                            livro.autor = "";
                        }

                        try
                        {
                            livro.data = ((record.GetVariableField("260").ToString()).Split(new string[] { "$g" }, StringSplitOptions.None)[1]);
                        }
                        catch
                        {
                            livro.data = "";
                        }
                        try
                        {
                            livro.nome = ((record.GetVariableField("130").ToString()).Split(new string[] { "$a" }, StringSplitOptions.None)[1]).Split(new string[] { "$d" }, StringSplitOptions.None)[0];
                        }
                        catch
                        {
                            livro.nome = "";
                        }

                        livros.Add(livro);
                    }
                }
            }


            dGVResult.DataSource = livros;
            var json = JsonConvert.SerializeObject(livros);

            System.IO.StreamWriter json_file = new System.IO.StreamWriter(Directory.GetCurrentDirectory() + "livros.json");
            json_file.Write(json);
            json_file.Close();

            fileJson = json;
        }
コード例 #6
0
 public void TestMarcXmlWriterNormalized()
 {
     using (var ms = new MemoryStream())
     {
         using (var fs = new FileStream(Environment.CurrentDirectory + StaticTestRecords.RESOURCES_BRKRTEST_MRC, FileMode.Open))
         {
             Assert.IsNotNull(fs);
             MarcXmlWriter writer = new MarcXmlWriter(fs, true);
             writer.Converter = new AnselToUnicode();
             MarcStreamReader reader = new MarcStreamReader(ms);
             while (reader.MoveNext())
             {
                 var record = reader.Current;
                 writer.Write(record);
             }
             using (StreamReader Testoutput = new StreamReader(fs, Encoding.UTF8))
             {
                 String line;
                 while ((line = Testoutput.ReadLine()) != null)
                 {
                     if (Regex.IsMatch(line, "[ ]*<subfield code=\"a\">This is a Test of diacritics.*"))
                     {
                         String[] lineParts = line.Split(new string[] { ", " }, StringSplitOptions.None);
                         foreach (var linePart in lineParts)
                         {
                             if (linePart.StartsWith("the tilde in "))
                             {
                                 Assert.IsTrue(linePart.Equals("the tilde in man\u0303ana"), "Incorrect value for tilde");
                             }
                             else if (linePart.StartsWith("the grave accent in "))
                             {
                                 Assert.IsTrue(linePart.Equals("the grave accent in tre\u0300s"), "Incorrect value for grave");
                             }
                             else if (linePart.StartsWith("the acute accent in "))
                             {
                                 Assert.IsTrue(linePart.Equals("the acute accent in de\u0301sire\u0301e"), "Incorrect value for acute");
                             }
                             else if (linePart.StartsWith("the circumflex in "))
                             {
                                 Assert.IsTrue(linePart.Equals("the circumflex in co\u0302te"), "Incorrect value for macron");
                             }
                             else if (linePart.StartsWith("the macron in "))
                             {
                                 Assert.IsTrue(linePart.Equals("the macron in To\u0304kyo"), "Incorrect value for macron");
                             }
                             else if (linePart.StartsWith("the breve in "))
                             {
                                 Assert.IsTrue(linePart.Equals("the breve in russkii\u0306"), "Incorrect value for breve");
                             }
                             else if (linePart.StartsWith("the dot above in "))
                             {
                                 Assert.IsTrue(linePart.Equals("the dot above in z\u0307aba"), "Incorrect value for dot above");
                             }
                             else if (linePart.StartsWith("the dieresis (umlaut) in "))
                             {
                                 Assert.IsTrue(linePart.Equals("the dieresis (umlaut) in Lo\u0308wenbra\u0308u"), "Incorrect value for umlaut");
                             }
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #7
0
 public void TestMarcXmlWriterConvertedToUTF8AndNormalized()
 {
     using (var output = new MemoryStream())
     {
         using (var fs = new FileStream(Environment.CurrentDirectory + StaticTestRecords.RESOURCES_BRKRTEST_MRC, FileMode.Open))
         {
             Assert.IsNotNull(fs);
             MarcXmlWriter writer = new MarcXmlWriter(output, true);
             writer.Converter            = new AnselToUnicode();
             writer.UnicodeNormalization = true;
             MarcStreamReader reader = new MarcStreamReader(fs);
             while (reader.MoveNext())
             {
                 var record = reader.Current;
                 writer.Write(record);
             }
             using (StreamReader Testoutput = new StreamReader(fs, Encoding.UTF8))
             {
                 String line;
                 while ((line = Testoutput.ReadLine()) != null)
                 {
                     if (Regex.IsMatch(line, "[ ]*<subfield code=\"a\">This is a Test of diacritics.*"))
                     {
                         String[] lineParts = line.Split(new string[] { ", " }, StringSplitOptions.None);
                         for (int i = 0; i < lineParts.Length; i++)
                         {
                             if (lineParts[i].StartsWith("the tilde in "))
                             {
                                 Assert.IsTrue(lineParts[i].Equals("the tilde in ma\u00F1ana"), "Incorrect normalized value for tilde accent");
                             }
                             else if (lineParts[i].StartsWith("the grave accent in "))
                             {
                                 Assert.IsTrue(lineParts[i].Equals("the grave accent in tr\u00E8s"), "Incorrect normalized value for grave accent");
                             }
                             else if (lineParts[i].StartsWith("the acute accent in "))
                             {
                                 Assert.IsTrue(lineParts[i].Equals("the acute accent in d\u00E9sir\u00E9e"), "Incorrect normalized value for acute accent");
                             }
                             else if (lineParts[i].StartsWith("the circumflex in "))
                             {
                                 Assert.IsTrue(lineParts[i].Equals("the circumflex in c\u00F4te"), "Incorrect normalized value for circumflex");
                             }
                             else if (lineParts[i].StartsWith("the macron in "))
                             {
                                 Assert.IsTrue(lineParts[i].Equals("the macron in T\u014Dkyo"), "Incorrect normalized value for macron");
                             }
                             else if (lineParts[i].StartsWith("the breve in "))
                             {
                                 Assert.IsTrue(lineParts[i].Equals("the breve in russki\u012D"), "Incorrect normalized value for breve");
                             }
                             else if (lineParts[i].StartsWith("the dot above in "))
                             {
                                 Assert.IsTrue(lineParts[i].Equals("the dot above in \u017Caba"), "Incorrect normalized value for dot above");
                             }
                             else if (lineParts[i].StartsWith("the dieresis (umlaut) in "))
                             {
                                 Assert.IsTrue(lineParts[i].Equals("the dieresis (umlaut) in L\u00F6wenbr\u00E4u"), "Incorrect normalized value for umlaut");
                             }
                         }
                     }
                 }
             }
         }
     }
 }