Exemple #1
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Usage();
            }
            else
            {
                ArrayList icalFiles        = new ArrayList();
                bool      startedFilesPart = false;
                bool      supressErrors    = false;
                bool      outputSameName   = false;
                string    rdfFilename      = null;
                int       urlcounter       = 0;

                // first process the flags and arguments
                for (int i = 0; i < args.Length; i++)
                {
                    string arg = args[i];

                    if (arg.StartsWith("-") && !startedFilesPart)
                    {
                        if (arg.Length == 2)
                        {
                            switch (arg[1])
                            {
                            case 'c':
                                Copyright();
                                return;

                            case 'h':
                                Usage();
                                return;

                            case 'p':
                                supressErrors = true;
                                break;

                            case 'x':
                                outputSameName = true;
                                break;

                            case 'f':
                                if (i + 1 >= args.Length)
                                {
                                    Usage();
                                    return;
                                }
                                rdfFilename = args[++i];
                                break;
                            }
                        }
                        else
                        {
                            Usage();
                            return;
                        }
                    }
                    else
                    {
                        startedFilesPart = true;  // ignore any options in the
                        icalFiles.Add(arg);
                    }
                }

                // deactivate the -f flag if there are more than one icsfiles - assume they want
                // file output, which will mean the -x flag...
                if (rdfFilename != null && icalFiles.Count > 1)
                {
                    rdfFilename    = null;
                    outputSameName = true;
                }

                // now loop through the ics files and translate them to RDF
                foreach (string file in icalFiles)
                {
                    TextReader reader;
                    bool       isURL = false;

                    try
                    {
                        if (file.StartsWith("http:"))
                        {
                            isURL = true;
                            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(file);
                            req.Proxy = WebProxy.GetDefaultProxy();
                            WebResponse resp = req.GetResponse();

                            // TODO: Found a 'bug' or 'feature' (depending on perspective), of the StreamReader.Peek()
                            // method.  It does not work on streams that are socket (network) based.  It will
                            // report EOF at the end of packet, instead of end of file.  So - let's read in the
                            // entire stream into memory, and rework the Scanner class so that it doesn't use
                            // Peek().
                            StreamReader tempReader = new StreamReader(resp.GetResponseStream( ));
                            reader = new StringReader(tempReader.ReadToEnd( ));
                        }
                        else
                        {
                            reader = new StreamReader(file);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Error encountered processing file: " + file + "\n   message: " + e.Message);
                        Usage();
                        return;
                    }

                    RDFEmitter emitter = new RDFEmitter( );
                    Parser     parser  = new Parser(reader, emitter);
                    parser.Parse( );

                    // check for errors
                    if (!supressErrors && parser.HasErrors)
                    {
                        foreach (ParserError error in parser.Errors)
                        {
                            Console.Error.WriteLine("Error on line: " + error.Linenumber + ".  " + error.Message);
                        }
                    }
                    else
                    {
                        // output it!
                        TextWriter writer;

                        if (rdfFilename != null)
                        {
                            writer = new StreamWriter(rdfFilename);
                        }
                        else if (outputSameName)
                        {
                            string outfile, infile;
                            if (isURL)
                            {
                                outfile = "calendar" + urlcounter + ".rdf";
                                urlcounter++;
                            }
                            else
                            {
                                int fslashindex = file.LastIndexOf('/');
                                int bslashindex = file.LastIndexOf('\\');
                                int slashindex  = Math.Max(fslashindex, bslashindex);

                                if (slashindex >= 0)
                                {
                                    infile = file.Substring(slashindex + 1);
                                }
                                else
                                {
                                    infile = file;
                                }

                                int dotindex = infile.LastIndexOf('.');
                                if (dotindex == -1)
                                {
                                    outfile = infile + ".rdf";
                                }
                                else
                                {
                                    outfile = infile.Substring(0, dotindex) + ".rdf";
                                }
                            }
                            writer = new StreamWriter(outfile);
                        }
                        else
                        {
                            writer = Console.Out;
                        }

                        writer.Write(emitter.Rdf);
                    }
                }
            }
        }
Exemple #2
0
	static void Main( string[] args )
	{
	    if( args.Length < 1 )
	    {
		Usage();
	    }
	    else
	    {
		ArrayList icalFiles = new ArrayList();
		bool startedFilesPart = false;
		bool supressErrors = false;
		bool outputSameName = false;
		string rdfFilename = null;
		int urlcounter = 0;

		// first process the flags and arguments
		for( int i = 0; i < args.Length; i++ )
		{
		    string arg = args[i];

		    if( arg.StartsWith( "-" ) && !startedFilesPart )
		    {
			if( arg.Length == 2 )
			{
			    switch( arg[1] )
			    {
				case 'c':
				    Copyright();
				    return;
				case 'h':
				    Usage();
				    return;
				case 'p':
				    supressErrors = true;
				    break;
				case 'x':
				    outputSameName = true;
				    break;
				case 'f':
				    if( i+1 >= args.Length )
				    {
					Usage();
					return;
				    }
				    rdfFilename = args[ ++i ];
				    break;
			    }
			}
			else
			{
			    Usage();
			    return;
			}
		    }
		    else
		    {
			startedFilesPart = true;  // ignore any options in the
			icalFiles.Add( arg );
		    }
		}

		// deactivate the -f flag if there are more than one icsfiles - assume they want
		// file output, which will mean the -x flag...
		if( rdfFilename != null && icalFiles.Count > 1 )
		{
		    rdfFilename = null;
		    outputSameName = true;
		}

		// now loop through the ics files and translate them to RDF
		foreach( string file in icalFiles )
		{
		    TextReader reader;
		    bool isURL = false;

		    try
		    {
			if( file.StartsWith( "http:" ))
			{
			    isURL = true;
			    HttpWebRequest req = (HttpWebRequest)WebRequest.Create( file );
			    req.Proxy = WebProxy.GetDefaultProxy();
			    WebResponse resp = req.GetResponse();

			    // TODO: Found a 'bug' or 'feature' (depending on perspective), of the StreamReader.Peek()
			    // method.  It does not work on streams that are socket (network) based.  It will 
			    // report EOF at the end of packet, instead of end of file.  So - let's read in the
			    // entire stream into memory, and rework the Scanner class so that it doesn't use
			    // Peek().  
			    StreamReader tempReader = new StreamReader( resp.GetResponseStream( ));
			    reader = new StringReader( tempReader.ReadToEnd( ));
			}
			else
			{
			    reader = new StreamReader( file );
			}
		    }
		    catch( Exception e )
		    {
			Console.Error.WriteLine( "Error encountered processing file: " + file + "\n   message: " + e.Message );
			Usage();
			return;
		    }

		    RDFEmitter emitter = new RDFEmitter( );
		    Parser parser = new Parser( reader, emitter );
		    parser.Parse( );

		    // check for errors
		    if( !supressErrors && parser.HasErrors )
		    {
			foreach( ParserError error in parser.Errors )
			{
			    Console.Error.WriteLine( "Error on line: " + error.Linenumber + ".  " + error.Message );
			}
		    }
		    else
		    {
			// output it!
			TextWriter writer;

			if( rdfFilename != null )
			{
			    writer = new StreamWriter( rdfFilename );
			}
			else if( outputSameName )
			{
			    string outfile, infile;
			    if( isURL )
			    {
				outfile = "calendar" + urlcounter + ".rdf";
				urlcounter++;
			    }
			    else
			    {
				int fslashindex = file.LastIndexOf( '/' );
				int bslashindex = file.LastIndexOf( '\\' );
				int slashindex = Math.Max( fslashindex, bslashindex );

				if( slashindex >= 0 )
				{
				    infile = file.Substring( slashindex+1 );
				}
				else
				{
				    infile = file;
				}
			    
				int dotindex = infile.LastIndexOf( '.' );
				if( dotindex == -1 )
				{
				    outfile = infile + ".rdf";
				}
				else
				{
				    outfile = infile.Substring( 0, dotindex) + ".rdf";
				}
			    }
			    writer = new StreamWriter( outfile );
			}
			else
			{
			    writer = Console.Out;
			}

			writer.Write( emitter.Rdf );
		    }
		}
	    }
	}