Пример #1
0
        public static TransitAccountEventICALEmitter Parse(string url, TimeSpan utcoffset, string useragent)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url.Replace("webcal:", "http:"));

            request.UserAgent = useragent;
            WebResponse  response = request.GetResponse();
            StreamReader sr       = new StreamReader(response.GetResponseStream());
            TransitAccountEventICALEmitter emitter = new TransitAccountEventICALEmitter();

            emitter.UtcOffset = utcoffset;
            Semaview.Shared.ICalParser.Parser parser = new Semaview.Shared.ICalParser.Parser(sr, emitter);
            parser.Parse();
            return(emitter);
        }
Пример #2
0
 protected void TestMeetup()
 {
     string url = "http://dessert.meetup.com/3/calendar/7801237/ical/Cupcake+Social+2.0/";
     Console.WriteLine(SnCore.Tools.Web.Renderer.UrlEncode(url));
     TransitAccountEventICALEmitter emitter = new TransitAccountEventICALEmitter();
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
     WebResponse response = request.GetResponse();
     StreamReader sr = new StreamReader(response.GetResponseStream());
     Parser parser = new Parser(sr, emitter);
     parser.Parse();
     Console.WriteLine(emitter.AccountEvent.Name);
     Console.WriteLine(emitter.AccountEvent.Description);
     Console.WriteLine(emitter.AccountEvent.StartDateTime);
 }
Пример #3
0
 protected void TestEventful()
 {
     // string url = "http://eventful.com/ical/events/E0-001-003054648-3/E0-001-003054648-3.ics";
     string url = "http://eventful.com/ical/events/E0-001-002992261-4/E0-001-002992261-4.ics";
     Console.WriteLine(SnCore.Tools.Web.Renderer.UrlEncode(url));
     TransitAccountEventICALEmitter emitter = new TransitAccountEventICALEmitter();
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
     WebResponse response = request.GetResponse();
     StreamReader sr = new StreamReader(response.GetResponseStream());
     Parser parser = new Parser(sr, emitter);
     parser.Parse();
     Console.WriteLine(emitter.AccountEvent.Name);
     Console.WriteLine(emitter.AccountEvent.Description);
     Console.WriteLine(emitter.AccountEvent.StartDateTime);
 }
Пример #4
0
        /// <summary>
        /// Parse the iCal using supplied TextReader and base URI
        /// </summary>
        public void Parse(TextReader reader, string baseUri)
        {
            try {
                string       strippedContent = Regex.Replace(reader.ReadToEnd(), @"\r\n ", "");
                StringReader strippedReader  = new StringReader(strippedContent);

                Semaview.Shared.ICalParser.Parser icalParser = new Semaview.Shared.ICalParser.Parser(strippedReader, itsEmitter);
                icalParser.Parse();
                itsXsltParser.NewStatement += new SemPlan.Spiral.Core.StatementHandler(XsltParserNewStatement);
                itsXsltParser.Parse(new StringReader(itsEmitter.Rdf), baseUri);
                itsXsltParser.NewStatement -= new SemPlan.Spiral.Core.StatementHandler(XsltParserNewStatement);
            }
            catch (Exception e) {
                Console.WriteLine(itsEmitter.Rdf);
                throw new ParserException("Could not parse content because " + e);
            }
        }
 public static TransitAccountEventICALEmitter Parse(string url, TimeSpan utcoffset, string useragent)
 {
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url.Replace("webcal:", "http:"));
     request.UserAgent = useragent;
     WebResponse response = request.GetResponse();
     StreamReader sr = new StreamReader(response.GetResponseStream());
     TransitAccountEventICALEmitter emitter = new TransitAccountEventICALEmitter();
     emitter.UtcOffset = utcoffset;
     Semaview.Shared.ICalParser.Parser parser = new Semaview.Shared.ICalParser.Parser(sr, emitter);
     parser.Parse();
     return emitter;
 }
Пример #6
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 );
		    }
		}
	    }
	}