-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiCalParser.cs
139 lines (109 loc) · 4.21 KB
/
iCalParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2011 Miyako Komooka
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace iCalLibrary // based on rfc5545
{
using Component;
public class iCalParser
{
iCalendarCollection Collection;
iCalComponentFactory factory;
iCalComponent Parent = null;
iCalComponent Current = null;
public iCalendarCollection ParseFile( String filename )
{
iCalSimpleParser sParser = new iCalSimpleParser();
sParser.ComponentStart += this.ComponentStartHandler;
sParser.ComponentEnd += this.ComponentEndHandler;
sParser.Property += this.PropertyHandler;
this.Collection = new iCalendarCollection();
this.Current = this.Collection;
this.Parent = null;
this.factory = new iCalComponentFactory();
sParser.ParseFile( filename );
return this.Collection;
}
public iCalendarCollection ParseStream(TextReader reader) {
iCalSimpleParser sParser = new iCalSimpleParser();
sParser.ComponentStart += this.ComponentStartHandler;
sParser.ComponentEnd += this.ComponentEndHandler;
sParser.Property += this.PropertyHandler;
this.Collection = new iCalendarCollection();
this.Current = this.Collection;
this.Parent = null;
this.factory = new iCalComponentFactory();
sParser.ParseStream( reader );
return this.Collection;
}
public void ComponentStartHandler( Object sender,
iCalParserEventArgs args )
{
iCalLineContent content = args.Content;
this.Parent = this.Current;
this.Current = factory.Create( content.Value );
this.Current.Parent = this.Parent;
}
public void ComponentEndHandler( Object sender,
iCalParserEventArgs args )
{
if( this.Parent != null ){
this.Parent.AddChild( this.Current );
}
this.Current = this.Current.Parent;
this.Parent = this.Current.Parent;
}
public void PropertyHandler ( Object sender, iCalParserEventArgs args )
{
iCalLineContent content = args.Content;
this.Current.SetProperty( content );
}
}
// event based iCal parser
public class iCalSimpleParser
{
public event EventHandler<iCalParserEventArgs> ComponentStart;
public event EventHandler<iCalParserEventArgs> ComponentEnd;
public event EventHandler<iCalParserEventArgs> Property;
public void ParseFile( String filename )
{
iCalReader icalReader = new iCalReader( filename );
this.Parse( icalReader );
}
public void ParseStream( TextReader reader )
{
iCalReader icalReader = new iCalReader( reader );
this.Parse( icalReader );
}
protected void Parse( iCalReader reader )
{
iCalLineContent content = null;
while( ( content = reader.ReadContent() ) != null ){
iCalParserEventArgs args = new iCalParserEventArgs( content );
if( content.Name == "begin" ){
String value = content.Value.ToLower();
if( this.ComponentStart != null ){
this.ComponentStart( this, args );
}
} else if( content.Name == "end" ) {
String value = content.Value.ToLower();
if( this.ComponentEnd != null ){
this.ComponentEnd( this, args );
}
} else {
if( this.Property != null ){
this.Property( this, args );
}
}
}
}
}
public class iCalParserEventArgs : EventArgs
{
public iCalLineContent Content;
public iCalParserEventArgs( iCalLineContent content ) {
this.Content = content;
}
}
}