PageRenderTime 64ms CodeModel.GetById 17ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 0ms

/JMRXMLNode.m

http://rtm2cocoa.googlecode.com/
Objective C | 181 lines | 136 code | 36 blank | 9 comment | 16 complexity | 36a7a32e6700d596d8413d5af8c29ba7 MD5 | raw file
  1//
  2//  JMRXMLNode.m
  3
  4/*
  5
  6This work is licensed under the Creative Commons Attribution License. To view a copy of this license, visit http://creativecommons.org/licenses/by/1.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  7
  8*/
  9
 10#import "JMRXMLNode.h"
 11
 12@implementation JMRXMLNode
 13
 14-(id)initWithCFXMLNodeRef:(CFXMLNodeRef)nodeRef {
 15    [super init];
 16    if (nodeRef) {
 17      CFRetain(nodeRef);
 18      _xmlNode = nodeRef;
 19      return self;
 20    }
 21    
 22    return nil;
 23}
 24
 25-(id)initWithString:(NSString *)string type:(int)type info:(CFXMLElementInfo *)info {
 26    CFXMLNodeRef ref = CFXMLNodeCreate(kCFAllocatorDefault,
 27                                       type, (CFStringRef)string, info,
 28                                       kCFXMLNodeCurrentVersion);
 29    return [self initWithCFXMLNodeRef:ref];
 30}
 31
 32-(id)initWithName:(NSString *)tagName {
 33    CFXMLElementInfo *info = (CFXMLElementInfo *)malloc(sizeof(CFXMLElementInfo));
 34
 35    // keep references to these
 36    info->attributes = (CFDictionaryRef)[self getAttributes];
 37    info->attributeOrder = (CFArrayRef)[self getAttributeOrder];
 38    info->isEmpty = NO;
 39    
 40    return [self initWithString:tagName type:kCFXMLNodeTypeElement info:info];
 41}
 42
 43-(id)initWithText:(NSString *)text {
 44    return [self initWithString:text type:kCFXMLNodeTypeText info:NULL];
 45}
 46
 47-(id)initWithWhitespace:(NSString *)text {
 48    return [self initWithString:text type:kCFXMLNodeTypeWhitespace info:NULL];
 49}
 50
 51-(id)initWithCdata:(NSString *)cdata {
 52    return [self initWithString:cdata type:kCFXMLNodeTypeCDATASection info:NULL];
 53}
 54
 55+(JMRXMLNode *)nodeWithCFXMLNodeRef:(CFXMLNodeRef)nodeRef {
 56    return [[[JMRXMLNode alloc] initWithCFXMLNodeRef:nodeRef] autorelease];
 57}
 58
 59+(JMRXMLNode *)nodeWithName:(NSString *)tagName {
 60    return [[[JMRXMLNode alloc] initWithName:tagName] autorelease];
 61}
 62
 63+(JMRXMLNode *)nodeWithText:(NSString *)text {
 64    return [[[JMRXMLNode alloc] initWithText:text] autorelease];
 65}
 66
 67+(JMRXMLNode *)nodeWithCdata:(NSString *)cdata {
 68    return [[[JMRXMLNode alloc] initWithText:cdata] autorelease];
 69}
 70
 71-(CFXMLNodeRef)nodeRef {
 72    return _xmlNode;
 73}
 74
 75-(CFXMLElementInfo *)info {
 76    if (_xmlNode)
 77        return (CFXMLElementInfo *)CFXMLNodeGetInfoPtr(_xmlNode);
 78    return nil;
 79}
 80
 81-(NSMutableDictionary *)getAttributes {
 82    CFXMLElementInfo *info;
 83
 84    if (_xmlNode && ![self isElement])
 85        return nil;
 86    if (!_attributes) {
 87        info = [self info];
 88        [self setAttributes:((info && info->attributes) ?
 89                             [NSMutableDictionary dictionaryWithDictionary:(NSDictionary *)info->attributes] :
 90                             [NSMutableDictionary dictionary])];
 91        // need to allocate attribute order, too
 92        [self getAttributeOrder];
 93    }
 94    return _attributes;
 95}
 96-(void)setAttributes:(NSMutableDictionary *)value {
 97    CFXMLElementInfo *info = [self info];
 98
 99    [value retain];
100    [_attributes release];
101    _attributes = value;
102    if (info)
103        info->attributes = (CFDictionaryRef)_attributes;
104}
105
106-(NSMutableArray *)getAttributeOrder {
107    CFXMLElementInfo *info;
108
109    if (_xmlNode && ![self isElement])
110        return nil;
111    if (!_attributeOrder) {
112        info = [self info];
113        [self setAttributeOrder:((info && info->attributeOrder) ?
114                                 [NSMutableArray arrayWithArray:(NSArray *)info->attributeOrder] :
115                                 [NSMutableArray array])];
116    }
117    return _attributeOrder;
118}
119-(void)setAttributeOrder:(NSMutableArray *)value {
120    CFXMLElementInfo *info = [self info];
121    
122    [value retain];
123    [_attributeOrder release];
124    _attributeOrder = value;
125    if (info)
126        info->attributeOrder = (CFArrayRef)_attributeOrder;
127}
128
129-(NSString *)getAttributeForKey:(NSString *)key {
130    return [[self getAttributes] objectForKey:key];
131}
132
133-(void)setAttribute:(NSString *)value forKey:(NSString *)key {
134    if (!value) {
135        [[self getAttributeOrder] removeObject:key];
136        [[self getAttributes] removeObjectForKey:key];
137    } else {
138        if (![[self getAttributeOrder] containsObject:key])
139            [[self getAttributeOrder] addObject:key];
140        [[self getAttributes] setObject:value forKey:key];
141    }
142}
143
144-(NSString *)getString {
145    return (NSString *)CFXMLNodeGetString(_xmlNode);
146}
147
148-(BOOL)isType:(int)type {
149    return _xmlNode && CFXMLNodeGetTypeCode(_xmlNode)==type;
150}
151
152-(BOOL)isElement {
153    return [self isType:kCFXMLNodeTypeElement];
154}
155
156-(BOOL)isComment {
157    return [self isType:kCFXMLNodeTypeComment];
158}
159
160-(BOOL)isProcessingInstruction {
161    return [self isType:kCFXMLNodeTypeProcessingInstruction];
162}
163
164-(BOOL)isText {
165    return [self isType:kCFXMLNodeTypeText];
166}
167
168-(BOOL)isCdata {
169    return [self isType:kCFXMLNodeTypeCDATASection];
170}
171
172-(BOOL)isDocument {
173    return [self isType:kCFXMLNodeTypeDocument];
174}
175
176-(void)dealloc {
177    CFRelease(_xmlNode);
178    [super dealloc];
179}
180
181@end