forked from veryweblog/ichm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHMTableOfContent.m
498 lines (420 loc) · 10.1 KB
/
CHMTableOfContent.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//
// CHMTableOfContent.m
// ichm
//
// Created by Robin Lu on 7/18/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "CHMTableOfContent.h"
#import <libxml/HTMLparser.h>
#import "CHMDocument.h"
@implementation LinkItem
@synthesize pageID;
- (id)init
{
_children = [[NSMutableArray alloc] init];
_name = nil;
_path = nil;
return self;
}
- (void) dealloc
{
[_children release];
[_path release];
[_name release];
[super dealloc];
}
- (id)initWithName:(NSString *)name Path:(NSString *)path
{
[self init];
_name = name;
_path = path;
[_name retain];
[_path retain];
return self;
}
- (void)setName:(NSString *)name
{
[_name release];
_name = name;
[_name retain];
}
- (void)setPath:(NSString *)path
{
[_path release];
_path = path;
[_path retain];
}
- (void)setPageID:(NSUInteger)pid
{
pageID = pid;
}
- (int)numberOfChildren
{
return _children ? [_children count] : 0;
}
- (LinkItem *)childAtIndex:(int)n
{
return [_children objectAtIndex:n];
}
- (NSString *)name
{
return _name;
}
- (NSString *)uppercaseName
{
return [_name uppercaseString];
}
- (NSString *)path
{
return _path;
}
- (NSMutableArray*)children
{
return _children;
}
- (void)appendChild:(LinkItem *)item
{
if(!_children)
_children = [[NSMutableArray alloc] init];
[_children addObject:item];
}
- (LinkItem*)find_by_path:(NSString *)path withStack:(NSMutableArray*)stack
{
if ([_path isEqualToString:path])
return self;
if(!_children)
return nil;
for (LinkItem* item in _children) {
LinkItem * rslt = [item find_by_path:path withStack:stack];
if (rslt != nil)
{
if(stack)
[stack addObject:self];
return rslt;
}
}
return nil;
}
- (void)enumerateItemsWithSEL:(SEL)selector ForTarget:(id)target
{
if (![_path isEqualToString:@"/"])
[target performSelector:selector withObject:self];
for (LinkItem* item in _children)
{
[item enumerateItemsWithSEL:selector ForTarget:target];
}
}
- (void)sort
{
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"uppercaseName" ascending:YES];
NSMutableArray * sda = [[NSMutableArray alloc] init];
[sda addObject:sd];
[_children sortUsingDescriptors:sda];
[sda release];
[sd release];
}
- (void)purge
{
NSMutableIndexSet *set = [[NSMutableIndexSet alloc] init];
for (LinkItem * item in _children) {
if ([item name] == nil && [item path] == nil && [item numberOfChildren] == 0)
[set addIndex:[_children indexOfObject:item]];
else
[item purge];
}
[_children removeObjectsAtIndexes:set];
[set release];
}
-(NSString *)description
{
return [NSString stringWithFormat:@"{\n\tname:%@\n\tpath:%@\n\tchildren:%@\n}", _name, _path, _children];
}
@end
@interface CHMTableOfContent (Private)
- (void)push_item;
- (void)pop_item;
- (void)new_item;
- (void)addToPageList:(LinkItem*)item;
@end
@implementation CHMTableOfContent
@synthesize rootItems;
@synthesize pageList;
static void elementDidStart( CHMTableOfContent *toc, const xmlChar *name, const xmlChar **atts );
static void elementDidEnd( CHMTableOfContent *toc, const xmlChar *name );
static htmlSAXHandler saxHandler = {
NULL, /* internalSubset */
NULL, /* isStandalone */
NULL, /* hasInternalSubset */
NULL, /* hasExternalSubset */
NULL, /* resolveEntity */
NULL, /* getEntity */
NULL, /* entityDecl */
NULL, /* notationDecl */
NULL, /* attributeDecl */
NULL, /* elementDecl */
NULL, /* unparsedEntityDecl */
NULL, /* setDocumentLocator */
NULL, /* startDocument */
NULL, /* endDocument */
(startElementSAXFunc) elementDidStart, /* startElement */
(endElementSAXFunc) elementDidEnd, /* endElement */
NULL, /* reference */
NULL, /* characters */
NULL, /* ignorableWhitespace */
NULL, /* processingInstruction */
NULL, /* comment */
NULL, /* xmlParserWarning */
NULL, /* xmlParserError */
NULL, /* xmlParserError */
NULL, /* getParameterEntity */
};
- (id)initWithData:(NSData *)data encodingName:(NSString*)encodingName
{
itemStack = [[NSMutableArray alloc] init];
pageList = [[NSMutableArray alloc] init];
rootItems = [[LinkItem alloc] initWithName:@"root" Path:@"/"];
curItem = rootItems;
if(!encodingName || [encodingName length] == 0)
encodingName = @"iso_8859_1";
htmlDocPtr doc = htmlSAXParseDoc( (xmlChar *)[data bytes], [encodingName UTF8String],
&saxHandler, self);
[itemStack release];
if( doc ) {
xmlFreeDoc( doc );
}
[rootItems purge];
[rootItems enumerateItemsWithSEL:@selector(addToPageList:) ForTarget:self];
return self;
}
- (id)initWithTOC:(CHMTableOfContent*)toc filterByPredicate:(NSPredicate*)predicate
{
rootItems = [[LinkItem alloc] initWithName:@"root" Path:@"/"];
NSMutableArray *children = [rootItems children];
if (toc)
{
LinkItem * items = [toc rootItems];
NSArray *src_children = [items children];
NSArray *results = [src_children filteredArrayUsingPredicate:predicate];
[children addObjectsFromArray:results];
}
return self;
}
- (void) dealloc
{
[rootItems release];
[super dealloc];
}
- (LinkItem *)itemForPath:(NSString*)path withStack:(NSMutableArray*)stack
{
if( [path hasPrefix:@"/"] ) {
path = [path substringFromIndex:1];
}
LinkItem *item = [rootItems find_by_path:path withStack:stack];
if (!item)
{
NSString *encoded_path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
item = [rootItems find_by_path:encoded_path withStack:stack];
}
return item;
}
- (int)rootChildrenCount
{
return [rootItems numberOfChildren];
}
- (void)sort
{
[rootItems sort];
}
- (LinkItem*)getNextPage:(LinkItem*)item
{
NSUInteger idx = [item pageID] + 1;
if (idx == [pageList count])
return nil;
return [pageList objectAtIndex:idx];
}
- (LinkItem*)getPrevPage:(LinkItem*)item
{
NSUInteger idx = [item pageID] - 1;
if (idx == -1)
return nil;
return [pageList objectAtIndex:idx];
}
# pragma mark NSOutlineView datasource
- (int)outlineView:(NSOutlineView *)outlineView
numberOfChildrenOfItem:(id)item
{
if (!item)
item = rootItems;
return [item numberOfChildren];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView
isItemExpandable:(id)item
{
return [item numberOfChildren] > 0;
}
- (id)outlineView:(NSOutlineView *)outlineView
child:(int)theIndex
ofItem:(id)item
{
if (!item)
item = rootItems;
return [item childAtIndex:theIndex];
}
- (id)outlineView:(NSOutlineView *)outlineView
objectValueForTableColumn:(NSTableColumn *)tableColumn
byItem:(id)item
{
return [item name];
}
- (LinkItem *)curItem
{
return curItem;
}
- (void)push_item
{
[itemStack addObject:curItem];
}
- (void)new_item
{
if ([itemStack count] == 0) {
[self push_item];
}
LinkItem * parent = [itemStack lastObject];
curItem = [[LinkItem alloc] init];
[parent appendChild:curItem];
}
- (void)pop_item
{
curItem = [itemStack lastObject];
[itemStack removeLastObject];
}
- (void)addToPageList:(LinkItem*)item
{
if ([item path] == nil)
return;
LinkItem* latest = [pageList lastObject];
if(nil == latest)
{
[pageList addObject:item];
}
else
{
NSURL *baseURL = [NSURL URLWithString:@"http://dummy.com"];
NSURL *url = [NSURL URLWithString:[item path] relativeToURL:baseURL];
NSURL *curUrl = [NSURL URLWithString:[latest path] relativeToURL:baseURL];
if (![[url path] isEqualToString:[curUrl path]])
[pageList addObject:item];
}
[item setPageID:([pageList count] - 1)];
}
# pragma mark NSXMLParser delegation
static void elementDidStart( CHMTableOfContent *context, const xmlChar *name, const xmlChar **atts )
{
if (!context)
return;
if ( !strcasecmp( "ul", (char *)name ) ) {
[context push_item];
return;
}
if ( !strcasecmp( "li", (char *)name ) ) {
[context new_item];
return;
}
if ( !strcasecmp( "param", (char *)name ) && ( atts != NULL ) ) {
// Topic properties
const xmlChar *type = NULL;
const xmlChar *value = NULL;
for( int i = 0; atts[ i ] != NULL ; i += 2 ) {
if( !strcasecmp( "name", (char *)atts[ i ] ) ) {
type = atts[ i + 1 ];
}
else if( !strcasecmp( "value", (char *)atts[ i ] ) ) {
value = atts[ i + 1 ];
}
}
if( ( type != NULL ) && ( value != NULL ) ) {
if( !strcasecmp( "Name", (char *)type ) || !strcasecmp( "Keyword", (char *)type )) {
// Name of the topic
NSString *str = [[NSString alloc] initWithUTF8String:(char *)value];
if (![[context curItem] name])
[[context curItem] setName:str];
[str release];
}
else if( !strcasecmp( "Local", (char *)type ) ) {
// Path of the topic
NSString *str = [[NSString alloc] initWithUTF8String:(char *)value];
[[context curItem] setPath:str];
[str release];
}
}
return;
}
}
static void elementDidEnd( CHMTableOfContent *context, const xmlChar *name )
{
if ( !strcasecmp( "ul", (char *)name ) ) {
[context pop_item];
return;
}
}
@end
@implementation CHMSearchResult
- (id) init
{
rootItems = [[ScoredLinkItem alloc] initWithName:@"root" Path:@"/" Score:0];
return self;
}
- (id)initwithTOC:(CHMTableOfContent*)toc withIndex:(CHMTableOfContent*)index
{
[self init];
tableOfContent = toc;
if (tableOfContent)
[tableOfContent retain];
indexContent = index;
if (indexContent)
[indexContent retain];
return self;
}
- (void) dealloc
{
if (tableOfContent)
[tableOfContent release];
if (indexContent)
[indexContent release];
[super dealloc];
}
- (void)addPath:(NSString*)path Score:(float)score
{
LinkItem * item = nil;
if (tableOfContent)
item = [tableOfContent itemForPath:path withStack:nil];
if (!item && indexContent)
item = [indexContent itemForPath:path withStack:nil];
if (!item)
return;
ScoredLinkItem * newitem = [[ScoredLinkItem alloc] initWithName:[item name] Path:[item path] Score:score];
[rootItems appendChild:newitem];
}
- (void)sort
{
[(ScoredLinkItem*)rootItems sort];
}
@end
@implementation ScoredLinkItem
@synthesize relScore;
- (void)sort
{
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"relScore" ascending:NO];
NSMutableArray * sda = [[NSMutableArray alloc] init];
[sda addObject:sd];
[_children sortUsingDescriptors:sda];
[sda release];
[sd release];
}
- (id)initWithName:(NSString *)name Path:(NSString *)path Score:(float)score
{
relScore = score;
return [self initWithName:name Path:path];
}
@end