-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySvnLogParser.m
193 lines (157 loc) · 4.15 KB
/
MySvnLogParser.m
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// MySvnLogParser.m
// svnX
//
#import "MySvnLogParser.h"
@implementation MySvnLogParser
- (id) init
{
self = [super init];
if (self)
{
bufString = [[NSMutableString string] retain];
}
return self;
}
- (void) dealloc
{
[bufString release];
[super dealloc];
}
+ (NSMutableArray*) parseData: (NSData*) data
{
#if 1
// HACK to fix 'Error NSXMLParserErrorDomain 1' on encountering control characters
int length;
BytePtr p = (BytePtr) [data bytes];
for (length = [data length]; length--; ++p)
{
UInt8 ch = *p;
if (ch < 32 && ch != 9 && ch != 10 && ch != 13)
*p = ' ';
}
#endif
MySvnLogParser* parser = [[self alloc] init];
NSMutableArray* result = [parser parseXML: data];
[parser release];
return result;
}
+ (NSMutableArray*) parseString: (NSString*) string
{
return [self parseData: [string dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion: YES]];
}
- (NSMutableArray*) parseXML: (NSData*) data
{
entry.revision =
entry.msg =
entry.date =
entry.author = @"";
entry.paths = nil;
entries = [NSMutableArray array];
NSXMLParser* parser = [[NSXMLParser alloc] initWithData: data];
[parser setDelegate: self];
[parser parse];
if ([parser parserError] != nil)
{
NSLog(@"Error while parsing log xml: %@", [[parser parserError] localizedDescription]);
}
[parser release];
return entries;
}
- (NSMutableArray*) parseXMLString: (NSString*) string
{
return [self parseXML: [string dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion: YES]];
}
- (void) parser: (NSXMLParser*) parser
didStartElement: (NSString*) elementName
namespaceURI: (NSString*) namespaceURI
qualifiedName: (NSString*) qualifiedName
attributes: (NSDictionary*) attributeDict
{
#pragma unused(parser, namespaceURI, qualifiedName)
[bufString setString: @""];
switch ([elementName characterAtIndex: 0])
{
case 'l':
if ([elementName isEqualToString: @"logentry"])
{
entry.revision = [attributeDict objectForKey: @"revision"];
}
break;
case 'p':
if ([elementName isEqualToString: @"path"])
{
action = [attributeDict objectForKey: @"action"];
copyfromPath = [attributeDict objectForKey: @"copyfrom-path"];
copyfromRev = copyfromPath ? [attributeDict objectForKey: @"copyfrom-rev"] : nil;
}
break;
}
}
- (void) parser: (NSXMLParser*) parser
didEndElement: (NSString*) elementName
namespaceURI: (NSString*) namespaceURI
qualifiedName: (NSString*) qualifiedName
{
#pragma unused(parser, namespaceURI, qualifiedName)
switch ([elementName characterAtIndex: 0])
{
case 'a':
if ([elementName isEqualToString: @"author"])
{
entry.author = [NSString stringWithString: bufString];
}
break;
case 'd':
if ([elementName isEqualToString: @"date"])
{
entry.date = [NSString stringWithString: bufString];
}
break;
case 'l':
if ([elementName isEqualToString: @"logentry"])
{
id revision_n = [NSNumber numberWithInt: [entry.revision intValue]];
[entries addObject: [NSDictionary dictionaryWithObjectsAndKeys:
entry.revision, @"revision",
revision_n, @"revision_n",
entry.msg, @"msg",
entry.date, @"date",
entry.author, @"author",
entry.paths, @"paths",
nil]];
entry.revision =
entry.msg =
entry.date =
entry.author = @"";
entry.paths = nil;
}
break;
case 'm':
if ([elementName isEqualToString: @"msg"])
{
entry.msg = [NSString stringWithString: bufString];
}
break;
case 'p':
if ([elementName isEqualToString: @"path"])
{
if (entry.paths == nil)
entry.paths = [NSMutableArray array];
[entry.paths addObject: [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithString: bufString], @"path",
action, @"action",
copyfromPath, @"copyfrompath",
copyfromRev, @"copyfromrev",
nil]];
}
break;
}
}
- (void) parser: (NSXMLParser*) parser
foundCharacters: (NSString*) string
{
#pragma unused(parser)
[bufString appendString: string];
}
@end