Skip to content

Commit

Permalink
KSXMLWriter uses a KSElementInfo object internally.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeabdullah committed Nov 19, 2010
1 parent beaf9b3 commit 3aeccf6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
13 changes: 6 additions & 7 deletions KSHTMLWriter.m
Expand Up @@ -27,6 +27,8 @@

#import "KSHTMLWriter.h"

#import "KSElementInfo.h"


@implementation KSHTMLWriter

Expand Down Expand Up @@ -102,16 +104,13 @@ - (void)pushAttribute:(NSString *)attribute value:(id)value;
[super pushAttribute:attribute value:value];
}

- (NSDictionary *)elementAttributes;
- (KSElementInfo *)currentElementInfo;
{
id result = [super elementAttributes];
KSElementInfo *result = [super currentElementInfo];

// Add in buffered class info
NSString *class = [self elementClassName];
if (class)
{
result = [NSMutableDictionary dictionaryWithDictionary:result];
[result setObject:class forKey:@"class"];
}
if (class) [result addAttribute:@"class" value:class];

return result;
}
Expand Down
9 changes: 5 additions & 4 deletions KSXMLWriter.h
Expand Up @@ -27,15 +27,17 @@

#import "KSForwardingWriter.h"

#import "KSElementInfo.h"

@class KSElementInfo, KSXMLElementContentsProxy;

@class KSXMLElementContentsProxy;


@interface KSXMLWriter : KSForwardingWriter
{
@private
KSElementInfo *_currentElement;
NSMutableArray *_openElements;
NSMutableArray *_attributes;
BOOL _elementIsEmpty;
NSUInteger _inlineWritingLevel; // the number of open elements at which inline writing began

Expand Down Expand Up @@ -99,8 +101,7 @@
* - Can sneak extra attributes in when using a convenience method (e.g. for HTML)
*/
- (void)pushAttribute:(NSString *)attribute value:(id)value;
- (NSDictionary *)elementAttributes;
- (KSElementInfo *)currentElementInfo;
- (KSElementInfo *)currentElementInfo; // modifying this object will not affect writing


#pragma mark Whitespace
Expand Down
58 changes: 30 additions & 28 deletions KSXMLWriter.m
Expand Up @@ -71,6 +71,11 @@ - (BOOL)elementCanBeEmpty:(NSString *)tagName; // YES for everything in pure XM
@end


@interface KSElementInfo (KSXMLWriter)
- (void)writeAttributes:(KSXMLWriter *)writer;
@end


#pragma mark -


Expand All @@ -82,8 +87,8 @@ - (id)initWithOutputWriter:(id <KSWriter>)output; // designated initializer
{
[super initWithOutputWriter:output];

_currentElement = [[KSElementInfo alloc] init];
_openElements = [[NSMutableArray alloc] init];
_attributes = [[NSMutableArray alloc] initWithCapacity:2];
_encoding = NSUTF8StringEncoding;

_contentsProxy = [KSXMLElementContentsProxy alloc];
Expand Down Expand Up @@ -113,7 +118,7 @@ - (id)initWithOutputWriter:(id <KSWriter>)output encoding:(NSStringEncoding)enco
- (void)dealloc
{
[_openElements release];
[_attributes release];
[_currentElement release];
[_illegalCharacters release];

[super dealloc];
Expand Down Expand Up @@ -187,13 +192,8 @@ - (void)startElement:(NSString *)elementName writeInline:(BOOL)writeInline;


// Write attributes
for (int i = 0; i < [_attributes count]; i+=2)
{
NSString *attribute = [_attributes objectAtIndex:i];
NSString *value = [_attributes objectAtIndex:i+1];
[self writeAttribute:attribute value:value];
}
[_attributes removeAllObjects];
[_currentElement writeAttributes:self];
[_currentElement close];


[self didStartElement];
Expand Down Expand Up @@ -245,29 +245,12 @@ - (void)endElement;

- (void)pushAttribute:(NSString *)attribute value:(id)value; // call before -startElement:
{
NSParameterAssert(value);
[_attributes addObject:attribute];
[_attributes addObject:value];
}

- (NSDictionary *)elementAttributes;
{
NSMutableDictionary *result = [NSMutableDictionary dictionary];

for (int i = 0; i < [_attributes count]; i+=2)
{
NSString *attribute = [_attributes objectAtIndex:i];
NSString *value = [_attributes objectAtIndex:i+1];
[result setObject:value forKey:attribute];
}

return result;
[_currentElement addAttribute:attribute value:value];
}

- (KSElementInfo *)currentElementInfo;
{
KSElementInfo *result = [[[KSElementInfo alloc] init] autorelease];
[result setAttributesAsDictionary:[self elementAttributes]];
KSElementInfo *result = [[_currentElement copy] autorelease];
return result;
}

Expand Down Expand Up @@ -590,6 +573,25 @@ - (void)writeString:(NSString *)string;
@end


#pragma mark -


@implementation KSElementInfo (KSXMLWriter)

- (void)writeAttributes:(KSXMLWriter *)writer;
{
for (int i = 0; i < [_attributes count]; i+=2)
{
NSString *attribute = [_attributes objectAtIndex:i];
NSString *value = [_attributes objectAtIndex:i+1];
[writer writeAttribute:attribute value:value];
}
}

@end



#pragma mark -


Expand Down

0 comments on commit 3aeccf6

Please sign in to comment.