Skip to content

Commit

Permalink
On reflection, it makes more sense to represent only the attributes o…
Browse files Browse the repository at this point in the history
…f an element.
  • Loading branch information
mikeabdullah committed Jul 14, 2011
1 parent 327cc56 commit 06fa83e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
6 changes: 3 additions & 3 deletions KSHTMLWriter.m
Expand Up @@ -27,7 +27,7 @@

#import "KSHTMLWriter.h"

#import "KSElementInfo.h"
#import "KSXMLAttributes.h"


NSString *KSHTMLWriterDocTypeHTML_4_01_Strict = @"HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\"";
Expand Down Expand Up @@ -134,9 +134,9 @@ - (void)pushAttribute:(NSString *)attribute value:(id)value;
[super pushAttribute:attribute value:value];
}

- (KSElementInfo *)currentElementInfo;
- (KSXMLAttributes *)currentAttributes;
{
KSElementInfo *result = [super currentElementInfo];
KSXMLAttributes *result = [super currentAttributes];

// Add in buffered class info
NSString *class = [self currentElementClassName];
Expand Down
11 changes: 4 additions & 7 deletions KSElementInfo.h → KSXMLAttributes.h
@@ -1,5 +1,5 @@
//
// KSElementInfo.h
// KSXMLAttributes.h
// Sandvox
//
// Created by Mike on 19/11/2010.
Expand All @@ -9,21 +9,18 @@
#import <Foundation/Foundation.h>


@interface KSElementInfo : NSObject <NSCopying>
@interface KSXMLAttributes : NSObject <NSCopying>
{
@private
NSString *_elementName;
NSMutableArray *_attributes;
}

- (id)initWithElementInfo:(KSElementInfo *)info;

@property(nonatomic, copy) NSString *name;
- (id)initWithXMLAttributes:(KSXMLAttributes *)info;

@property(nonatomic, copy) NSDictionary *attributesAsDictionary;
- (void)addAttribute:(NSString *)attribute value:(id)value;

- (void)close; // sets name to nil and removes all attributes
- (void)close; // removes all attributes


@end
20 changes: 8 additions & 12 deletions KSElementInfo.m → KSXMLAttributes.m
@@ -1,17 +1,19 @@
//
// KSElementInfo.m
// KSXMLAttributes.m
// Sandvox
//
// Created by Mike on 19/11/2010.
// Copyright 2010 Karelia Software. All rights reserved.
//

#import "KSElementInfo.h"
#import "KSXMLAttributes.h"

#import "KSXMLWriter.h"


@implementation KSElementInfo
@implementation KSXMLAttributes

#pragma mark Lifecycle

- (id)init;
{
Expand All @@ -20,26 +22,22 @@ - (id)init;
return self;
}

- (id)initWithElementInfo:(KSElementInfo *)info;
- (id)initWithXMLAttributes:(KSXMLAttributes *)info;
{
self = [self init];

[self setName:[info name]];
_attributes = [info->_attributes mutableCopy];

return self;
}

- (void)dealloc;
{
[_elementName release];
[_attributes release];

[super dealloc];
}

@synthesize name = _elementName;

- (NSDictionary *)attributesAsDictionary;
{
NSMutableDictionary *result = [NSMutableDictionary dictionary];
Expand Down Expand Up @@ -74,15 +72,14 @@ - (void)addAttribute:(NSString *)attribute value:(id)value;

- (void)close; // sets name to nil and removes all attributes
{
[self setName:nil];
[_attributes removeAllObjects];
}

#pragma mark NSCopying

- (id)copyWithZone:(NSZone *)zone;
{
return [[[self class] alloc] initWithElementInfo:self];
return [[[KSXMLAttributes class] alloc] initWithXMLAttributes:self];
}

#pragma mark Description
Expand All @@ -94,8 +91,7 @@ - (NSString *)description;
KSXMLWriter *writer = [[KSXMLWriter alloc] initWithOutputWriter:result];
[writer writeString:@" "];

[writer startElement:(self.name ? self.name : @"")
attributes:[self attributesAsDictionary]];
[writer startElement:@"" attributes:[self attributesAsDictionary]];
[writer endElement];

[writer release];
Expand Down
6 changes: 3 additions & 3 deletions KSXMLWriter.h
Expand Up @@ -27,7 +27,7 @@

#import "KSForwardingWriter.h"

#import "KSElementInfo.h"
#import "KSXMLAttributes.h"


@class KSXMLElementContentsProxy;
Expand All @@ -36,7 +36,7 @@
@interface KSXMLWriter : KSForwardingWriter
{
@private
KSElementInfo *_currentElement;
KSXMLAttributes *_attributes;
NSMutableArray *_openElements;
BOOL _elementIsEmpty;
NSUInteger _inlineWritingLevel; // the number of open elements at which inline writing began
Expand Down Expand Up @@ -113,7 +113,7 @@
* - Can sneak extra attributes in when using a convenience method (e.g. for HTML)
*/
- (void)pushAttribute:(NSString *)attribute value:(id)value;
- (KSElementInfo *)currentElementInfo; // modifying this object will not affect writing
- (KSXMLAttributes *)currentAttributes; // modifying this object will not affect writing


#pragma mark Attributes
Expand Down
18 changes: 9 additions & 9 deletions KSXMLWriter.m
Expand Up @@ -71,7 +71,7 @@ - (BOOL)elementCanBeEmpty:(NSString *)tagName; // YES for everything in pure XM
@end


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

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

_currentElement = [[KSElementInfo alloc] init];
_attributes = [[KSXMLAttributes alloc] init];
_openElements = [[NSMutableArray alloc] init];

// Inherit encoding where possible
Expand All @@ -113,7 +113,7 @@ - (id)initWithOutputWriter:(id <KSWriter>)output encoding:(NSStringEncoding)enco
- (void)dealloc
{
[_openElements release];
[_currentElement release];
[_attributes release];
[_contentsProxy release];

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


// Write attributes
[_currentElement writeAttributes:self];
[_currentElement close];
[_attributes writeAttributes:self];
[_attributes close];


[self didStartElement];
Expand Down Expand Up @@ -264,12 +264,12 @@ - (void)popElement;

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

- (KSElementInfo *)currentElementInfo;
- (KSXMLAttributes *)currentAttributes;
{
KSElementInfo *result = [[_currentElement copy] autorelease];
KSXMLAttributes *result = [[_attributes copy] autorelease];
return result;
}

Expand Down Expand Up @@ -622,7 +622,7 @@ - (void)writeString:(NSString *)string;
#pragma mark -


@implementation KSElementInfo (KSXMLWriter)
@implementation KSXMLAttributes (KSXMLWriter)

- (void)writeAttributes:(KSXMLWriter *)writer;
{
Expand Down

0 comments on commit 06fa83e

Please sign in to comment.