-
Notifications
You must be signed in to change notification settings - Fork 12
/
KSXMLWriter.m
668 lines (526 loc) · 19.7 KB
/
KSXMLWriter.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//
// KSXMLWriter.m
//
// Created by Mike Abdullah
// Copyright © 2010 Karelia Software
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "KSXMLWriter.h"
@interface KSXMLWriter (ImplementedBySubclasses)
/**
HTML has the notion of void elements (XML doesn't as far as I can tell). A void element MUST be
empty, and CANNOT have an end tag (it should be self-closing, or written as just a start tag,
whatever suits). We don't implement this method ourselves, but trust the HTML writer to do so.
*/
- (BOOL)isVoidElement:(NSString *)elementName;
@end
@interface KSXMLAttributes (KSXMLWriter)
- (void)writeAttributes:(KSXMLWriter *)writer;
@end
#pragma mark -
@implementation KSXMLWriter {
KSXMLAttributes *_attributes;
NSMutableArray *_openElements;
/// Tracks whether the current element's start tag is yet to be closed, so we know later if need
/// to write an end tag, or can have a single <foo /> type of tag
BOOL _yetToCloseStartTag;
// Pretty printing
BOOL _prettyPrintingDisabled;
NSUInteger _elementCountAtLastNewline;
}
#pragma mark Init & Dealloc
- (id)initWithOutputWriter:(KSWriter *)output {
if (self = [super init])
{
_outputWriter = [output retain];
_encoding = (output ? output.encoding : NSUTF8StringEncoding);
if (![[self class] isStringEncodingAvailable:_encoding])
{
CFStringRef encodingName = CFStringGetNameOfEncoding(CFStringConvertNSStringEncodingToEncoding(_encoding));
[NSException raise:NSInvalidArgumentException
format:@"Unsupported character encoding %@ (%lu)", encodingName, (unsigned long) _encoding];
}
_attributes = [[KSXMLAttributes alloc] init];
_openElements = [[NSMutableArray alloc] init];
[self resetPrettyPrinting];
}
return self;
}
- (id)init; { return [self initWithOutputWriter:nil]; }
- (void)dealloc
{
[_outputWriter release];
[_openElements release];
[_attributes release];
[_doctype release];
[super dealloc];
}
#pragma mark Document
- (void)writeDoctypeDeclaration
{
[self writeString:@"<!DOCTYPE "];
[self writeString:self.doctype];
[self writeString:@">"];
[self startNewline];
}
#pragma mark Elements
- (void)startElement:(NSString *)elementName {
if ([self shouldBeginNewlineForElement:elementName]) {
[self startNewline];
}
[self writeString:@"<"];
[self writeString:elementName];
// Must do this AFTER writing the string so subclasses can take early action in a -writeString: override
[self pushElement:elementName];
// Once an element has been written, it's time to resume normal service (if pretty-printing) and
// start a newline for any following elements which merit it.
_prettyPrintingDisabled = NO;
// With writing done, begin tracking to see if element is empty
_yetToCloseStartTag = YES;
// Add attributes
[_attributes writeAttributes:self];
[_attributes close];
[self increaseIndentationLevel];
}
- (void)endElement {
// We've reached the end of the element, so of course indentation needs to decrease
[self decreaseIndentationLevel];
// and same goes for the stack
NSString *element = self.topElement;
[self popElement];
// Write the tag itself, as a special empty one if we should
if (_yetToCloseStartTag) {
if (![self respondsToSelector:@selector(isVoidElement:)] || [self isVoidElement:element]) {
_yetToCloseStartTag = NO;
[self closeEmptyElementTag];
return;
}
}
// Did that element span multiple lines? If so, the end tag ought to go on its own line
if (self.openElementsCount < _elementCountAtLastNewline) {
[self startNewline]; // was this element written entirely inline?
}
[self writeEndTag:element];
}
- (void)writeElement:(NSString *)name content:(void (^)(void))content;
{
[self startElement:name];
if (content) content();
[self endElement];
}
- (void)writeElement:(NSString *)elementName text:(NSString *)text;
{
[self writeElement:elementName content:^{
[self writeCharacters:text];
}];
}
- (void)pushElement:(NSString *)element;
{
// Private method so that Sandvox can work for now
[_openElements addObject:element];
}
- (void)popElement;
{
[_openElements removeLastObject];
}
#pragma mark Attributes
- (void)addAttribute:(NSString * __nonnull)attribute value:(NSString * __nonnull)value {
if (_yetToCloseStartTag) {
// Temporarily turn off tracking so the write goes through without triggering closure
_yetToCloseStartTag = NO;
NSString *valueString = [value description];
[self writeString:@" "];
[self writeString:attribute];
[self writeString:@"=\""];
[self writeAttributeValue:valueString];
[self writeString:@"\""];
_yetToCloseStartTag = YES;
}
else {
if (self.openElementsCount) {
[NSException raise:NSInvalidArgumentException format:@"Can't add attributes to an element which already has content"];
}
else {
[NSException raise:NSInvalidArgumentException format:@"No element open to add attributes to"];
}
}
}
- (void)pushAttribute:(NSString *)attribute value:(id)value; // call before -startElement:
{
[_attributes addAttribute:attribute value:value];
}
- (KSXMLAttributes *)currentAttributes;
{
KSXMLAttributes *result = [[_attributes copy] autorelease];
return result;
}
- (BOOL)hasCurrentAttributes;
{
return [_attributes count];
}
- (void)writeAttributeValue:(NSString *)value;
{
// Make sure to escape the quote mark
[self.class writeString:value escapeXMLEntitiesIncludingQuotes:YES usingBlock:^(NSString *string, NSRange range) {
[self writeString:string range:range];
}];
}
+ (NSString *)stringFromAttributeValue:(NSString *)value;
{
__block NSString *result = @"";
[self.class writeString:value escapeXMLEntitiesIncludingQuotes:YES usingBlock:^(NSString *string, NSRange range) {
// Often the original string is valid. If so, use it whole
if (string == value && range.length == value.length)
{
result = value;
}
else
{
if (range.length != string.length) string = [string substringWithRange:range];
result = [result stringByAppendingString:string];
}
}];
return result;
}
#pragma mark Pretty Printing
- (void)startNewline {
[self writeString:@"\n"];
NSUInteger indentationLevel = [self indentationLevel];
for (NSUInteger i = 0; i < indentationLevel; i++)
{
[self writeString:@"\t"];
}
_elementCountAtLastNewline = self.openElementsCount;
}
/*! How it works:
*
* _inlineWritingLevel records the number of objects in the Elements Stack at the point inline writing began (-startWritingInline).
* A value of NSNotFound indicates that we are not writing inline (-stopWritingInline). This MUST be done whenever about to write non-inline content (-openTag: does so automatically).
* Finally, if _inlineWritingLevel is 0, this is a special value to indicate we're at the start of the document/section, so the next element to be written is inline, but then normal service shall resume.
*/
- (void)resetPrettyPrinting {
_prettyPrintingDisabled = YES;
}
- (BOOL)shouldBeginNewlineForElement:(NSString *)element;
{
if (_prettyPrintingDisabled) return NO;
if (!self.prettyPrint) return NO;
if ([self.class shouldPrettyPrintElementInline:element]) return NO;
return YES;
}
+ (BOOL)shouldPrettyPrintElementInline:(NSString *)element {
return NO;
}
#pragma mark Indentation
- (void)increaseIndentationLevel;
{
self.indentationLevel++;
}
- (void)decreaseIndentationLevel;
{
if ([self indentationLevel] > 0) {
self.indentationLevel--;
}
else {
NSLog(@"Ignoring attempt to decrease indentation level when already at 0");
}
}
#pragma mark Elements Stack
- (NSArray *)openElements; { return [[_openElements copy] autorelease]; }
- (NSUInteger)openElementsCount;
{
return [_openElements count];
}
- (BOOL)hasOpenElement:(NSString *)tagName;
{
// Seek an open element, matching case insensitively
for (NSString *anElement in _openElements)
{
if ([anElement isEqualToString:tagName])
{
return YES;
}
}
return NO;
}
- (NSString *)topElement;
{
return [_openElements lastObject];
}
#pragma mark Element Primitives
/**
Writes the raw \c > character that marks the close of a _tag_ (not the element, the tag)
*/
- (void)closeStartTag {
[self writeString:@">"];
}
/**
Much like \c closeStartTag, but for ending an element which has been found to be empty:
\c />
*/
- (void)closeEmptyElementTag {
[self writeString:@" />"];
}
/**
Primitive method that writes an end tag, ignoring the open elements stack
*/
- (void)writeEndTag:(NSString *)tagName {
[self writeString:@"</"];
[self writeString:tagName];
[self writeString:@">"];
}
#pragma mark String Encoding
static NSCharacterSet *sCharactersToEntityEscapeWithQuot;
static NSCharacterSet *sCharactersToEntityEscapeWithoutQuot;
+ (void)initialize
{
// Cache the characters to be escaped. Doing it in +initialize should be threadsafe
if (!sCharactersToEntityEscapeWithQuot)
{
// Don't want to escape apostrophes for HTML, but do for Javascript
sCharactersToEntityEscapeWithQuot = [[NSCharacterSet characterSetWithCharactersInString:@"&<>\""] retain];
}
if (!sCharactersToEntityEscapeWithoutQuot)
{
sCharactersToEntityEscapeWithoutQuot = [[NSCharacterSet characterSetWithCharactersInString:@"&<>"] retain];
}
}
/*! Escape & < > " ... does NOT escape anything else. Need to deal with character set in subsequent pass.
Escaping " so that strings work within HTML tags
*/
// Explicitly escape, or don't escape, double-quots as "
// Within a tag like <foo attribute="%@"> then we have to escape it.
// In just about all other contexts it's OK to NOT escape it, but many contexts we don't know if it's OK or not.
// So I think we want to gradually shift over to being explicit when we know when it's OK or not.
//
// Return value indicates whether any escaping actually needed doing
+ (void)writeString:(NSString *)string escapeXMLEntitiesIncludingQuotes:(BOOL)escapeQuotes usingBlock:(void (^)(NSString *string, NSRange range))block;
{
NSCharacterSet *charactersToEntityEscape = (escapeQuotes ?
sCharactersToEntityEscapeWithQuot :
sCharactersToEntityEscapeWithoutQuot);
// Look for characters to escape. If there are none can bail out quick without having had to allocate anything. #78710
NSRange searchRange = NSMakeRange(0, [string length]);
NSRange range = [string rangeOfCharacterFromSet:charactersToEntityEscape options:0 range:searchRange];
if (range.location == NSNotFound)
{
block(string, searchRange);
return;
}
while (searchRange.length)
{
// Write characters not needing to be escaped. Don't bother if there aren't any
NSRange unescapedRange = searchRange;
if (range.location != NSNotFound)
{
unescapedRange.length = range.location - searchRange.location;
}
if (unescapedRange.length)
{
block(string, unescapedRange);
}
// Process characters that need escaping
if (range.location != NSNotFound)
{
NSAssert(range.length == 1, @"trying to escaping non-single character string"); // that's all we should deal with for HTML escaping
unichar ch = [string characterAtIndex:range.location];
NSString *escaped;
switch (ch)
{
case '&': escaped = @"&"; break;
case '<': escaped = @"<"; break;
case '>': escaped = @">"; break;
case '"': escaped = @"""; break;
default: escaped = [NSString stringWithFormat:@"&#%d;",ch];
}
block(escaped, NSMakeRange(0, escaped.length));
}
else
{
break; // no escapable characters were found so we must be done
}
// Continue the search
searchRange.location = NSMaxRange(range);
searchRange.length = [string length] - searchRange.location;
range = [string rangeOfCharacterFromSet:charactersToEntityEscape options:0 range:searchRange];
}
}
#pragma mark String Encoding
+ (BOOL)isStringEncodingAvailable:(NSStringEncoding)encoding;
{
return (encoding == NSASCIIStringEncoding ||
encoding == NSUTF8StringEncoding ||
encoding == NSISOLatin1StringEncoding ||
encoding == NSWindowsCP1251StringEncoding ||
encoding == NSUnicodeStringEncoding);
}
- (void)writeString:(NSString *)string range:(NSRange)nsrange {
NSParameterAssert(string);
// Is this string some element content? If so, the element is no longer empty so must close the tag and mark as such
if (_yetToCloseStartTag && [string length])
{
// Complain if trying to write inside of a void element
if ([self respondsToSelector:@selector(isVoidElement:)] && [self isVoidElement:self.topElement]) {
[NSException raise:NSInvalidArgumentException format:@"Void elements can't have any contents — http://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements"];
}
_yetToCloseStartTag = NO; // comes first to avoid infinite recursion
[self closeStartTag];
}
CFRange range = CFRangeMake(nsrange.location, nsrange.length);
while (range.length)
{
CFIndex written = CFStringGetBytes((CFStringRef)string,
range,
CFStringConvertNSStringEncodingToEncoding([self encoding]),
0, // don't convert invalid characters
false,
NULL, // not interested in actually getting the bytes
0,
NULL);
if (written < range.length) // there was an invalid character
{
// Write what is valid
if (written)
{
NSRange validRange = NSMakeRange(range.location, written);
[_outputWriter writeString:string range:validRange];
}
// Convert the invalid char
unichar ch = [string characterAtIndex:(range.location + written)];
switch (ch)
{
// If we encounter a special character with a symbolic entity, use that
case 160: [_outputWriter writeString:@" "]; break;
case 169: [_outputWriter writeString:@"©"]; break;
case 174: [_outputWriter writeString:@"®"]; break;
case 8211: [_outputWriter writeString:@"–"]; break;
case 8212: [_outputWriter writeString:@"—"]; break;
case 8364: [_outputWriter writeString:@"€"]; break;
// Otherwise, use the decimal unicode value.
default:
{
NSString *escaped = [NSString stringWithFormat:@"&#%d;",ch];
[_outputWriter writeString:escaped]; break;
}
}
// Convert the rest
NSUInteger increment = written + 1;
range.location += increment; range.length -= increment;
}
else if (range.location == 0)
{
// Efficient route for if entire string can be written
[_outputWriter writeString:string range:nsrange];
break;
}
else
{
// Write what remains
[_outputWriter writeString:string range:NSMakeRange(range.location, range.length)];
break;
}
}
}
- (void)writeString:(NSString *)string {
[self writeString:string range:NSMakeRange(0, string.length)];
}
@end
@implementation KSXMLWriter (CharacterData)
#pragma mark Text
- (void)writeCharacters:(NSString *)string;
{
// Quotes are acceptable characters outside of attribute values
[self.class writeString:string escapeXMLEntitiesIncludingQuotes:NO usingBlock:^(NSString *string, NSRange range) {
[self writeString:string range:range];
}];
}
+ (NSString *)stringFromCharacters:(NSString *)characters;
{
__block NSString *result = @"";
[self.class writeString:characters escapeXMLEntitiesIncludingQuotes:NO usingBlock:^(NSString *string, NSRange range) {
// Often the original string is valid. If so, use it whole
if (string == characters && range.length == characters.length)
{
result = characters;
}
else
{
if (range.length != string.length) string = [string substringWithRange:range];
result = [result stringByAppendingString:string];
}
}];
return result;
}
#pragma mark Comments
- (void)writeComment:(NSString *)comment; // escapes the string, and wraps in a comment tag
{
[self openComment];
[self writeAttributeValue:comment];
[self closeComment];
}
- (void)openComment;
{
[self writeString:@"<!--"];
}
- (void)closeComment;
{
[self writeString:@"-->"];
}
#pragma mark CDATA
- (void)writeCDATAWithContentBlock:(void (^)(void))content;
{
[self startCDATA];
content();
[self endCDATA];
}
- (void)startCDATA;
{
[self writeString:@"<![CDATA["];
}
- (void)endCDATA;
{
[self writeString:@"]]>"];
}
@end
@implementation KSXMLWriter (Validation)
- (BOOL)validateElement:(NSString *)element;
{
NSParameterAssert(element);
return YES;
}
- (NSString *)validateAttribute:(NSString *)name value:(NSString *)value ofElement:(NSString *)element;
{
NSParameterAssert(name);
NSParameterAssert(element);
return value;
}
@end
#pragma mark -
@implementation KSXMLAttributes (KSXMLWriter)
- (void)writeAttributes:(KSXMLWriter *)writer;
{
for (NSUInteger i = 0; i < [_attributes count]; i+=2)
{
NSString *attribute = [_attributes objectAtIndex:i];
NSString *value = [_attributes objectAtIndex:i+1];
[writer addAttribute:attribute value:value];
}
}
@end