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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/*
Copyright (C) 2000-2005 SKYRIX Software AG
This file is part of SOPE.
SOPE 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; either version 2, or (at your option) any
later version.
SOPE 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 SOPE; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include <DOM/DOMDocument.h>
#include <DOM/DOMImplementation.h>
#include <DOM/DOMElement.h>
#include <DOM/DOMNodeWalker.h>
#include <DOM/DOMProcessingInstruction.h>
#include <DOM/DOMElement.h>
#include <DOM/DOMAttribute.h>
#include <DOM/DOMEntityReference.h>
#include "DOMSaxBuilder.h"
#include "DOMNode+QueryPath.h"
#include "common.h"
//#define PROF_DEALLOC 1
@implementation NGDOMDocument
+ (id)documentFromData:(NSData *)_data {
id builder;
builder = [[[DOMSaxBuilder alloc] init] autorelease];
return [builder buildFromData:_data];
}
+ (id)documentFromString:(NSString *)_string {
return [self documentFromData:
[_string dataUsingEncoding:NSISOLatin1StringEncoding]];
}
+ (id)documentFromURI:(NSString *)_uri {
id builder;
builder = [[[DOMSaxBuilder alloc] init] autorelease];
return [builder buildFromContentsOfFile:_uri];
}
- (id)initWithName:(NSString *)_qname
namespaceURI:(NSString *)_uri
documentType:(id)_doctype
dom:(NGDOMImplementation *)_dom
{
self->dom = [_dom retain];
self->qname = [_qname copy];
self->uri = [_uri copy];
self->doctype = [_doctype retain];
return self;
}
- (void)dealloc {
#if PROF_DEALLOC
NSDate *start = [NSDate date];
#endif
[self->errors release];
[self->warnings release];
[self->dom release];
[self->qname release];
[self->uri release];
[self->doctype release];
[super dealloc];
#if PROF_DEALLOC
printf("%s: %.3fs\n", __PRETTY_FUNCTION__,
[NSDate timeIntervalSinceDate:start]);
#endif
}
/* errors/warnings */
- (void)addErrors:(NSArray *)_errors {
if (self->errors == nil)
self->errors = [_errors copy];
else {
NSArray *tmp;
tmp = self->errors;
self->errors = [[tmp arrayByAddingObjectsFromArray:_errors] copy];
[tmp release];
}
}
- (void)addWarnings:(NSArray *)_errors {
if (self->warnings == nil)
self->warnings = [_errors copy];
else {
NSArray *tmp;
tmp = self->warnings;
self->warnings = [[tmp arrayByAddingObjectsFromArray:_errors] copy];
[tmp release];
}
}
/* attributes */
- (id<NSObject,DOMDocumentType>)doctype {
return self->doctype;
}
- (NGDOMImplementation *)implementation {
return self->dom;
}
- (id<NSObject,DOMElement>)documentElement {
id children;
unsigned i, count;
if ((children = [self childNodes]) == nil)
return nil;
for (i = 0, count = [children count]; i < count; i++) {
id node;
node = [children objectAtIndex:i];
if ([node nodeType] == DOM_ELEMENT_NODE)
return node;
}
return nil;
}
/* node */
- (BOOL)_isValidChildNode:(id)_node {
switch ([_node nodeType]) {
case DOM_ELEMENT_NODE:
case DOM_PROCESSING_INSTRUCTION_NODE:
case DOM_COMMENT_NODE:
case DOM_DOCUMENT_TYPE_NODE:
return YES;
default:
return NO;
}
}
- (DOMNodeType)nodeType {
return DOM_DOCUMENT_NODE;
}
- (id<NSObject,DOMNamedNodeMap>)attributes {
return nil;
}
- (IDOMDocument)ownerDocument {
return nil;
}
- (id<NSObject,DOMNode>)parentNode {
/* document cannot be nested */
return nil;
}
- (id<NSObject,DOMNode>)nextSibling {
/* document cannot be nested */
return nil;
}
- (id<NSObject,DOMNode>)previousSibling {
/* document cannot be nested */
return nil;
}
- (void)_walk_getElementsByTagName:(id)_walker {
id node;
node = [_walker currentNode];
if ([node nodeType] != DOM_ELEMENT_NODE)
return;
if (![[node tagName] isEqualToString:
[(NSArray *)[_walker context] objectAtIndex:0]])
/* tagname doesn't match */
return;
[[(NSArray *)[_walker context] objectAtIndex:1] addObject:node];
}
- (void)_walk_getElementsByTagNameAddAll:(id)_walker {
id node;
node = [_walker currentNode];
if ([node nodeType] != DOM_ELEMENT_NODE)
return;
[(NSMutableArray *)[_walker context] addObject:node];
}
- (id<NSObject,DOMNodeList>)getElementsByTagName:(NSString *)_tagName {
/* introduced in DOM2, should return a *live* list ! */
NSMutableArray *array;
NGDOMNodePreorderWalker *walker;
SEL sel;
id ctx;
if (![self hasChildNodes])
return nil;
if (_tagName == nil)
return nil;
array = [NSMutableArray arrayWithCapacity:4];
if ([_tagName isEqualToString:@"*"]) {
_tagName = nil;
ctx = array;
sel = @selector(_walk_getElementsByTagNameAddAll:);
}
else {
ctx = [NSArray arrayWithObjects:_tagName, array, nil];
sel = @selector(_walk_getElementsByTagName:);
}
walker = [[NGDOMNodePreorderWalker alloc]
initWithTarget:self selector:sel context:ctx];
[walker walkNode:self];
[walker release]; walker = nil;
return [[array copy] autorelease];
}
- (id<NSObject,DOMNodeList>)getElementsByTagName:(NSString *)_tagName
namespaceURI:(NSString *)_uri
{
[self doesNotRecognizeSelector:_cmd];
return nil;
}
- (id<NSObject,DOMElement>)getElementById:(NSString *)_eid {
/*
Introduced in DOM2
Note: The DOM implementation must have information that says which
attributes are of type ID. Attributes with the name "ID" are not of
type ID unless so defined.
Implementations that do not know whether attributes are of type ID
or not are expected to return null.
*/
return nil;
}
/* factory */
- (Class)domElementClass {
return [self->dom domElementClass];
}
- (Class)domElementNSClass {
return [self->dom domElementNSClass];
}
- (Class)domDocumentFragmentClass {
return [self->dom domDocumentFragmentClass];
}
- (Class)domTextNodeClass {
return [self->dom domTextNodeClass];
}
- (Class)domCommentClass {
return [self->dom domCommentClass];
}
- (Class)domCDATAClass {
return [self->dom domCDATAClass];
}
- (Class)domProcessingInstructionClass {
return [self->dom domProcessingInstructionClass];
}
- (Class)domAttributeClass {
return [self->dom domAttributeClass];
}
- (Class)domAttributeNSClass {
return [self->dom domAttributeNSClass];
}
- (Class)domEntityReferenceClass {
return [self->dom domEntityReferenceClass];
}
- (id<NSObject,DOMElement>)createElement:(NSString *)_tagName {
id elem;
elem = [[[self domElementClass]
alloc]
initWithTagName:_tagName];
return [elem autorelease];
}
- (id<NSObject,DOMElement>)createElement:(NSString *)_tagName
namespaceURI:(NSString *)_uri
{
id elem;
elem = [[[self domElementNSClass]
alloc]
initWithTagName:_tagName namespaceURI:_uri];
return [elem autorelease];
}
- (id<NSObject,DOMDocumentFragment>)createDocumentFragment {
id elem;
elem = [[[self domDocumentFragmentClass] alloc] init];
return [elem autorelease];
}
- (id<NSObject,DOMText>)createTextNode:(NSString *)_data {
id elem;
elem = [[[self domTextNodeClass] alloc] initWithString:_data];
return [elem autorelease];
}
- (id<NSObject,DOMComment>)createComment:(NSString *)_data {
id elem;
elem = [[[self domCommentClass] alloc] initWithString:_data];
return [elem autorelease];
}
- (id<NSObject,DOMCDATASection>)createCDATASection:(NSString *)_data {
id elem;
elem = [[[self domCDATAClass] alloc] initWithString:_data];
return [elem autorelease];
}
- (id<NSObject,DOMProcessingInstruction>)createProcessingInstruction:
(NSString *)_target data:(NSString *)_data
{
id elem;
elem = [[[self domProcessingInstructionClass]
alloc]
initWithTarget:_target data:_data];
return [elem autorelease];
}
- (id<NSObject,DOMAttr>)createAttribute:(NSString *)_name {
id elem;
elem = [[[self domAttributeClass]
alloc]
initWithName:_name];
return [elem autorelease];
}
- (id<NSObject,DOMAttr>)createAttribute:(NSString *)_name
namespaceURI:(NSString *)_uri
{
id elem;
elem = [[[self domAttributeNSClass]
alloc]
initWithName:_name namespaceURI:_uri];
return [elem autorelease];
}
- (id<NSObject,DOMEntityReference>)createEntityReference:(NSString *)_name {
id elem;
elem = [[[self domEntityReferenceClass] alloc] initWithName:_name];
return [elem autorelease];
}
/* QPValues */
- (NSException *)setQueryPathValue:(id)_value {
return [NSException exceptionWithName:@"QueryPathEvalException"
reason:@"cannot set query-path value on DOMDocument !"
userInfo:nil];
}
- (id)queryPathValue {
return [self documentElement];
}
/* Key/Value Coding */
- (id)valueForKey:(NSString *)_key {
if ([_key hasPrefix:@"/"])
return [self lookupQueryPath:_key];
return [super valueForKey:_key];
}
@end /* NGDOMDocument */
|