-
Notifications
You must be signed in to change notification settings - Fork 0
/
SvnListParser.m
179 lines (134 loc) · 4.36 KB
/
SvnListParser.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
//----------------------------------------------------------------------------------------
// SvnListParser.m - Parse the XML output of `svn list`.
//
// Created by Dominique PERETTI on 05/01/06.
// Copyright 2006 Dominique PERETTI. All rights reserved.
// Copyright © Chris, 2008 - 2009. All rights reserved.
//----------------------------------------------------------------------------------------
#import "CommonUtils.h"
#import "NSString+MyAdditions.h"
#import "RepoItem.h"
#import "SvnListParser.h"
@implementation SvnListParser
//----------------------------------------------------------------------------------------
- (id) init
{
if (self = [super init])
{
bufString = [[NSMutableString string] retain];
}
return self;
}
//----------------------------------------------------------------------------------------
- (void) dealloc
{
[bufString release];
[super dealloc];
}
//----------------------------------------------------------------------------------------
+ (NSArray*) parseData: (NSData*) data
{
SvnListParser* parser = [[self alloc] init];
NSArray* result = [parser parseXML: data];
[parser release];
return result;
}
//----------------------------------------------------------------------------------------
+ (NSArray*) parseString: (NSString*) string
{
return [self parseData: [string dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion: YES]];
}
//----------------------------------------------------------------------------------------
- (NSArray*) parseXML: (NSData*) data
{
curEntry = nil;
entries = [NSMutableArray array];
NSXMLParser* parser = [[NSXMLParser alloc] initWithData: data];
[parser setDelegate: self];
[parser parse];
if ([parser parserError] != nil)
{
dprintf("Error while parsing list xml: %@", [[parser parserError] localizedDescription]);
}
[parser release];
return entries;
}
//----------------------------------------------------------------------------------------
- (NSArray*) 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 'e':
if ([elementName isEqualToString: @"entry"])
{
curEntry = [RepoItem repoItem: [[attributeDict objectForKey: @"kind"] isEqual: @"dir"]];
[entries addObject: curEntry];
[curEntry release]; // Was retained by addObject:
}
break;
case 'c':
if ([elementName isEqualToString: @"commit"])
{
[curEntry setModRev: [[attributeDict objectForKey: @"revision"] intValue]];
}
break;
}
}
//----------------------------------------------------------------------------------------
- (void) parser: (NSXMLParser*) parser
didEndElement: (NSString*) elementName
namespaceURI: (NSString*) namespaceURI
qualifiedName: (NSString*) qualifiedName
{
#pragma unused(parser, namespaceURI, qualifiedName)
RepoItem* repoItem = curEntry;
switch ([elementName characterAtIndex: 0])
{
case 'a':
if ([elementName isEqualToString: @"author"])
{
[repoItem setAuthor: [NSString stringWithString: bufString]];
}
break;
case 'd':
if ([elementName isEqualToString: @"date"])
{
[repoItem setTime: ParseDateTime([bufString substringToIndex: 10],
[bufString substringWithRange: NSMakeRange(11, 8)])];
}
break;
case 'n':
if ([elementName isEqualToString: @"name"])
{
[repoItem setName: [NSString stringWithString: bufString]];
}
break;
case 's':
if ([elementName isEqualToString: @"size"])
{
[repoItem setSize: (SInt64) [bufString doubleValue]];
}
break;
}
}
//----------------------------------------------------------------------------------------
- (void) parser: (NSXMLParser*) parser
foundCharacters: (NSString*) string
{
#pragma unused(parser)
[bufString appendString: string];
}
@end
//----------------------------------------------------------------------------------------
// End of SvnListParser.m