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
|
/* -*-objc-*-
*
* GNUstep RSS Kit
* Copyright (C) 2006 Guenther Noack
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, in version 2.1
* of the License
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#import "DOMParser.h"
#import "GNUstep.h"
//#define DEBUG 1
@implementation XMLText
-(NSString*) contentAndNextContents
{
return [NSString stringWithFormat: @"%@%@",
( (_content==nil) ? @"" : _content ),
( (_next==nil) ? @"" : [_next contentAndNextContents])];
}
-(NSString*) content
{
return ( (_content==nil) ? @"" : _content );
}
-(void) _setNext: (id<XMLTextOrNode>) node
{
ASSIGN(_next, node);
}
-(XMLNode*) nextElement
{
// !!! If you change this, change it in XMLNode, too!
// XXX: Write a macro
// we only return *XML elements* here, *not* contents!
if ([_next isKindOfClass: [XMLText class]]) {
return [_next nextElement];
} else {
return AUTORELEASE(RETAIN(_next));
}
}
/**
* @deprecated
* Please don't call init on XMLText objects. It won't work.
* Instead, use initWithString:
*/
-(id)init
{
[self release];
return nil;
}
-(id)initWithString: (NSString*) str
{
self = [super init];
if (self != nil) {
ASSIGN(_content, str);
}
return self;
}
-(void)dealloc
{
DESTROY(_next);
DESTROY(_content);
[super dealloc];
}
@end
@implementation XMLNode
-(XMLNode*) firstChildElement
{
if (_child == nil)
return nil;
if ([[_child class] isSubclassOfClass: [XMLNode class]]) {
return AUTORELEASE(RETAIN(_child));
} else {
return [_child nextElement];
}
}
-(XMLNode*) nextElement
{
// !!! If you change this, change it in XMLText, too!
// XXX: Write a macro
// we only return *XML elements* here, *not* contents!
if ([_next isKindOfClass: [XMLText class]]) {
return [_next nextElement];
} else {
return AUTORELEASE(RETAIN(_next));
}
}
-(NSString*) name
{
return AUTORELEASE(RETAIN(_name));
}
-(NSString*) contentAndNextContents
{
NSString* result;
// XXX: attributes are still not shown here! Do we need it?
if (_child == nil) {
result = [NSString stringWithFormat:
@"<%@/>%@", _name,
(_next==nil?@"":[_next contentAndNextContents])];
} else {
result = [NSString stringWithFormat:
@"<%@>%@</%@>%@",
_name, [_child contentAndNextContents], _name,
(_next==nil?@"":[_next contentAndNextContents])];
}
return result;
}
-(NSString*) content
{
NSString* result;
// XXX: attributes are still not shown here! Do we need it?
if (_child == nil) {
result = @"";
} else {
result = [_child contentAndNextContents];
}
return result;
}
-(NSDictionary*) attributes
{
return AUTORELEASE(RETAIN(_attributes));
}
-(NSString*) namespace
{
return AUTORELEASE(RETAIN(_namespace));
}
-(id) initWithName: (NSString*) name
namespace: (NSString*) namespace
attributes: (NSDictionary*) attributes
parent: (XMLNode*) parent;
{
self = [super init];
if (self != nil) {
ASSIGN(_name, name);
ASSIGN(_namespace, namespace);
ASSIGN(_parent, parent);
ASSIGN(_attributes, attributes);
}
return self;
}
-(void) dealloc
{
DESTROY(_child);
DESTROY(_next);
DESTROY(_namespace);
DESTROY(_name);
DESTROY(_current);
DESTROY(_parent);
DESTROY(_attributes);
[super dealloc];
}
- (void) _setNext: (id<XMLTextOrNode>) node
{
#ifdef DEBUG
NSLog(@"_setNext: %@ --> %@", self, node);
#endif
ASSIGN(_next, node);
}
- (void) appendTextOrNode: (id<XMLTextOrNode>) aThing
fromParser: (NSXMLParser*) aParser
{
#ifdef DEBUG
NSLog(@"appendTextOrNode: %@ at: %@", aThing, [self name]);
#endif
if (_child == nil) {
ASSIGN(_child, aThing);
}
if (_current == nil) {
ASSIGN(_current, aThing);
} else {
[_current _setNext: aThing];
ASSIGN(_current, aThing);
}
if ([[aThing class] isSubclassOfClass: [XMLNode class]]) {
[aParser setDelegate: aThing];
}
}
@end
@implementation XMLNode (NSXMLParserDelegateEventAdditions)
- (void) parser: (NSXMLParser*)aParser
didEndElement: (NSString*)anElementName
namespaceURI: (NSString*)aNamespaceURI
qualifiedName: (NSString*)aQualifierName
{
#ifdef DEBUG
NSLog(@"closing XML node %@", anElementName);
#endif
if (![anElementName isEqualToString: _name]) {
NSLog(@"badly nested XML elements!");
}
if (_parent != nil) {
[aParser setDelegate: _parent];
DESTROY(_parent);
}
}
- (void) parser: (NSXMLParser*)aParser
didStartElement: (NSString*)anElementName
namespaceURI: (NSString*)aNamespaceURI
qualifiedName: (NSString*)aQualifierName
attributes: (NSDictionary*)anAttributeDict
{
XMLNode *item = [[XMLNode alloc]
initWithName: anElementName
namespace: aNamespaceURI
attributes: anAttributeDict
parent: self ];
#ifdef DEBUG
NSLog(@"starting XML node %@ (NS=%@)", anElementName, aNamespaceURI);
#endif
[self appendTextOrNode: item
fromParser: aParser];
DESTROY(item);
}
- (void) parser: (NSXMLParser*)aParser
parseErrorOccured: (NSError*)parseError
{
NSLog(@"XML-DOM Parser: %@ at line %@, col %@",
[parseError localizedDescription],
[aParser lineNumber], [aParser columnNumber]);
}
- (void) parser: (NSXMLParser*)aParser
foundCharacters: (NSString*)aString
{
XMLText *text = [[XMLText alloc] initWithString: aString];
[self appendTextOrNode: text
fromParser: aParser];
DESTROY(text);
}
- (void) parser: (NSXMLParser*)aParser
foundCDATA: (NSData*)CDATABlock
{
// FIXME: Find out how to retieve the XML file's encoding from here!
// At the moment, I'm just using UTF8 here, as it's the most commonly
// used encoding in RSS feeds (and XML in general).
NSString* blockContents = [[NSString alloc] initWithData: CDATABlock
encoding: NSUTF8StringEncoding];
[blockContents autorelease];
// delegate to the foundCharacters method.
[self parser: aParser foundCharacters: blockContents];
}
@end
|