コード例 #1
0
ファイル: VCardParser.cs プロジェクト: machocr/PDI
        /// <summary>
        /// This static method can be used to load property values into a new vCard collection from a file.  The
        /// filename can be a disk file or a URL.
        /// </summary>
        /// <param name="filename">A path or URL to a file containing one or more vCards</param>
        /// <returns>A new vCard collection as created from the file</returns>
        /// <example>
        /// <code language="cs">
        /// VCardCollection vCards1 = VCardParser.ParseFromFile(@"C:\AddressBook.vcf");
        /// VCardCollection vCards2 = VCardParser.ParseFromFile(
        ///     "http://www.mydomain.com/VCards/AddressBook.vcf");
        /// </code>
        /// <code language="vbnet">
        /// Dim vCards1 As VCardCollection = VCardParser.ParseFromFile("C:\AddressBook.vcf")
        /// Dim vCards2 As VCardCollection = VCardParser.ParseFromFile(
        ///     "http://www.mydomain.com/VCards/AddressBook.vcf")
        /// </code>
        /// </example>
        public static VCardCollection ParseFromFile(string filename)
        {
            VCardParser vcp = new VCardParser();

            vcp.ParseFile(filename);

            return(vcp.VCards);
        }
コード例 #2
0
ファイル: PDIParserTest.cs プロジェクト: modulexcite/PDI
		static void Main(string[] args)
		{
            string outputFolder, outputFile;

            if(args.GetUpperBound(0) < 1)
            {
                Console.WriteLine("Specify a folder name containing PDI files and a different one for the " +
                    "output files.");
                return;
            }

            try
            {
                // Set the default encoding to iso-8859-1 (Western European) as several of the files are encoded
                // that way.
                PDIParser.DefaultEncoding = BaseProperty.DefaultEncoding = Encoding.GetEncoding("iso-8859-1");

                outputFolder = args[1];

                if(!outputFolder.EndsWith(@"\", StringComparison.Ordinal))
                    outputFolder += @"\";

                if(!Directory.Exists(outputFolder))
                    Directory.CreateDirectory(outputFolder);

                Console.WriteLine("Parsing vCard files...");

                // Since we'll be parsing multiple files, we'll create an instance of the parser and re-use it
                // rather than using the static parsing methods.
                VCardParser vcardp = new VCardParser();

                foreach(string file in Directory.EnumerateFiles(args[0], "*.vcf"))
                {
                    Console.WriteLine("\n{0}", file);

                    vcardp.ParseFile(file);

                    Console.WriteLine(vcardp.VCards.ToString());

                    // Write it to a stream
                    outputFile = outputFolder + Path.GetFileName(file);

                    // NOTE: We must use the same encoding method when writing the file back out
                    using(var sw = new StreamWriter(outputFile, false, PDIParser.DefaultEncoding))
                    {
                        vcardp.VCards.WriteToStream(sw);
                    }

                    // Clear the collection ready for the next run
                    vcardp.VCards.Clear();
                }

                Console.WriteLine("\nParsing vCalendar files...");

                // Since we'll be parsing multiple files, we'll create an instance of the parser and re-use it
                // rather than using the static parser methods.
                VCalendarParser vcalp = new VCalendarParser();

                foreach(string file in Directory.EnumerateFiles(args[0], "*.vcs"))
                {
                    Console.WriteLine("\n{0}", file);

                    vcalp.ParseFile(file);

                    Console.WriteLine(vcalp.VCalendar.ToString());

                    // Write it to a stream
                    outputFile = outputFolder + Path.GetFileName(file);

                    // NOTE: We must use the same encoding method when writing the file back out
                    using(var sw = new StreamWriter(outputFile, false, PDIParser.DefaultEncoding))
                    {
                        vcalp.VCalendar.WriteToStream(sw);
                    }

                    // Clear the calendar ready for the next run
                    vcalp.VCalendar.ClearProperties();
                }

                Console.WriteLine("\nParsing iCalendar files...");

                // Get a list of iCalendar files to parse.  It uses the same parser as the vCalendar files
                foreach(string file in Directory.EnumerateFiles(args[0], "*.ics"))
                {
                    Console.WriteLine("\n{0}", file);

                    vcalp.ParseFile(file);

                    Console.WriteLine(vcalp.VCalendar.ToString());

                    // Write it to a stream
                    outputFile = outputFolder + Path.GetFileName(file);

                    // NOTE: We must use the same encoding method when writing the file back out
                    using(var sw = new StreamWriter(outputFile, false, PDIParser.DefaultEncoding))
                    {
                        vcalp.VCalendar.WriteToStream(sw);
                    }

                    // Clear the calendar ready for the next run
                    vcalp.VCalendar.ClearProperties();
                }

                Console.WriteLine("\nParsing vNote files...");

                // Since we'll be parsing multiple files, we'll create an instance of the parser and re-use it
                // rather than using the static parser methods.
                VNoteParser vnp = new VNoteParser();

                foreach(string file in Directory.EnumerateFiles(args[0], "*.vnt"))
                {
                    Console.WriteLine("\n{0}", file);

                    vnp.ParseFile(file);

                    Console.WriteLine(vnp.VNotes.ToString());

                    // Write it to a stream
                    outputFile = outputFolder + Path.GetFileName(file);

                    // NOTE: We must use the same encoding method when writing the file back out
                    using(var sw = new StreamWriter(outputFile, false, PDIParser.DefaultEncoding))
                    {
                        vnp.VNotes.WriteToStream(sw);
                    }

                    // Clear the collection ready for the next run
                    vnp.VNotes.Clear();
                }
            }
            catch(PDIParserException pe)
            {
                Console.WriteLine("\n\nError at line #{0}\n\n{1}", pe.LineNumber, pe.ToString());
            }
            catch(Exception ex)
            {
                Console.WriteLine("Unexpected failure:\n{0}", ex.ToString());
            }
        }
コード例 #3
0
ファイル: VCardParser.cs プロジェクト: modulexcite/PDI
        /// <summary>
        /// This static method can be used to load property values into a new vCard collection from a file.  The
        /// filename can be a disk file or a URL.
        /// </summary>
        /// <param name="filename">A path or URL to a file containing one or more vCards</param>
        /// <returns>A new vCard collection as created from the file</returns>
        /// <example>
        /// <code language="cs">
        /// VCardCollection vCards1 = VCardParser.ParseFromFile(@"C:\AddressBook.vcf");
        /// VCardCollection vCards2 = VCardParser.ParseFromFile(
        ///     "http://www.mydomain.com/VCards/AddressBook.vcf");
        /// </code>
        /// <code language="vbnet">
        /// Dim vCards1 As VCardCollection = VCardParser.ParseFromFile("C:\AddressBook.vcf")
        /// Dim vCards2 As VCardCollection = VCardParser.ParseFromFile(
        ///     "http://www.mydomain.com/VCards/AddressBook.vcf")
        /// </code>
        /// </example>
        public static VCardCollection ParseFromFile(string filename)
        {
            VCardParser vcp = new VCardParser();
            vcp.ParseFile(filename);

            return vcp.VCards;
        }