Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

KSXMLWriter uses a KSElementInfo object internally.

  • Loading branch information...
commit 3aeccf68250bf3d0d380ed66fb72319f9db66eb0 1 parent beaf9b3
@mikeabdullah mikeabdullah authored
Showing with 41 additions and 39 deletions.
  1. +6 −7 KSHTMLWriter.m
  2. +5 −4 KSXMLWriter.h
  3. +30 −28 KSXMLWriter.m
View
13 KSHTMLWriter.m
@@ -27,6 +27,8 @@
#import "KSHTMLWriter.h"
+#import "KSElementInfo.h"
+
@implementation KSHTMLWriter
@@ -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;
}
View
9 KSXMLWriter.h
@@ -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
@@ -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
View
58 KSXMLWriter.m
@@ -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 -
@@ -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];
@@ -113,7 +118,7 @@ - (id)initWithOutputWriter:(id <KSWriter>)output encoding:(NSStringEncoding)enco
- (void)dealloc
{
[_openElements release];
- [_attributes release];
+ [_currentElement release];
[_illegalCharacters release];
[super dealloc];
@@ -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];
@@ -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;
}
@@ -593,6 +576,25 @@ - (void)writeString:(NSString *)string;
#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 -
+
+
@implementation KSXMLElementContentsProxy
- (void)ks_prepareWithTarget:(id)target XMLWriter:(KSXMLWriter *)writer;
Please sign in to comment.
Something went wrong with that request. Please try again.