File: DOMXMLOutputter.m

package info (click to toggle)
sope 5.12.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,592 kB
  • sloc: objc: 169,229; sh: 3,823; ansic: 3,409; javascript: 446; python: 318; makefile: 185
file content (442 lines) | stat: -rw-r--r-- 11,844 bytes parent folder | download | duplicates (11)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
  Copyright (C) 2000-2009 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 "DOMXMLOutputter.h"
#include "DOMDocument.h"
#include "DOMElement.h"
#include "common.h"

@interface DOMXMLOutputter(Privates)
- (void)outputNode:(id<DOMNode>)_node to:(id)_target;
- (void)outputNodeList:(id<DOMNodeList>)_nodeList to:(id)_target;
@end

@interface DOMXMLOutputter(PrefixStack)
- (NSString *)topPrefix;
- (NSString *)topNamespace;
- (void)pushPrefix:(NSString *)_prefix namespace:(NSString *)_namespace;
- (void)popPrefixAndNamespace;
- (BOOL)isTagValidInStack:(id)_node;
- (NSArray *)newAttributePrefixesAndNamespaces:(NSArray *)_attrs;
@end /* DOMXMLOutputter(PrefixStack) */


@implementation DOMXMLOutputter

- (id)init {
  if ((self = [super init])) {
    self->stack = [[NSMutableArray alloc] initWithCapacity:32];
  }
  return self;
}

- (void)dealloc {
  [self->stack release];
  [super dealloc];
}

- (void)indentOn:(id)_target {
  int i;
  
  for (i = 0; i < (self->indent * 4); i++) {
    if (_target)
      [_target appendString:@" "];
    else
      fputc(' ', stdout);
  }
}

- (void)write:(NSString *)s to:(id)_target {
  if (_target)
    [_target appendString:s];
  else
#ifndef __APPLE__
    printf("%s", [s cString]);
#else
    printf("%s", [s UTF8String]);
#endif
}
- (BOOL)currentElementPreservesWhitespace {
  return NO;
}

- (void)outputAttributeNode:(id<DOMAttr>)_attrNode
  ofNode:(id<DOMNode>)_node
  to:(id)_target
{
  if ([[_attrNode prefix] length] > 0) {
    [self write:[_attrNode prefix] to:_target];
    [self write:@":"               to:_target];
  }
  [self write:[_attrNode name] to:_target];
  
  if ([_attrNode hasChildNodes]) {
    id children;
    unsigned i, count;

    [self write:@"=\"" to:_target];

    children = [_attrNode childNodes];
    for (i = 0, count = [children count]; i < count; i++) {
      id child;
      
      child = [children objectAtIndex:i];
      
      if ([child nodeType] == DOM_TEXT_NODE)
        [self write:[(id<DOMText>)child data] to:_target];
      else
        NSLog(@"WARNING: unsupported attribute value node %@", child);
    }
    
    [self write:@"\"" to:_target];
  }
  else
    NSLog(@"WARNING: attribute %@ has no content !", _attrNode);
}

- (void)outputAttributeNodes:(id<DOMNamedNodeMap>)_nodes
  list:(NSArray *)_list to:(id)_target
{
  unsigned i, count, count2;
  
  if ((count = [_nodes length]) == 0)
    return;

  // append required prefix and namespaces
  for (i = 0, count2 = [_list count]; i < count2; i = i + 2) {
    [self write:@" xmlns:" to:_target];
    [self write:[_list objectAtIndex:i]   to:_target];
    [self write:@"=\""     to:_target];
    [self write:[_list objectAtIndex:i+1] to:_target];
    [self write:@"\""      to:_target];
  }
  
  for (i = 0; i < count; i++) {
    id<DOMAttr> attrNode;
    
    attrNode = [_nodes objectAtIndex:i];
    
    [self write:@" " to:_target];
    [self outputAttributeNode:attrNode ofNode:nil to:_target];
  }
}

- (void)outputTextNode:(id<DOMText>)_node to:(id)_target {
  NSString *s;
  unsigned len;

  s = [_node data];
  if ((len = [s length]) == 0)
    return;
  
  if (![self currentElementPreservesWhitespace]) {
    unsigned i;
    
    for (i = 0; i < len; i++) {
      if (!isspace([s characterAtIndex:i]))
        break;
    }
    if (i == len)
      /* only whitespace */
      return;
    
    [self indentOn:_target];
  }
  
  [self write:[_node data] to:_target];
  
  if (![self currentElementPreservesWhitespace])
    [self write:@"\n" to:_target];
}
- (void)outputCommentNode:(id<DOMComment>)_node to:(id)_target {
  [self write:@"<!-- "     to:_target];
  [self write:[_node data] to:_target];
  [self write:@" -->"      to:_target];
  
  if (![self currentElementPreservesWhitespace])
    [self write:@"\n" to:_target];
}

- (void)outputElementNode:(id<DOMElement>)_node to:(id)_target {
  NSArray  *list;  // new attribute prefixes and namespaces
  NSString *tagName;
  NSString *ns = nil;
  NSString *tagURI;
  NSString *tagPrefix;
  BOOL     isNodeValid;
  unsigned i, count;
  
  // getting new attributes prefixes and namespaces
  list = (NSArray *)[_node attributes];
  list = [self newAttributePrefixesAndNamespaces:list];

  // push new attribute prefixes and namespaces to stack
  for (i = 0, count = [list count]; i < count; i = i + 2) {
    [self pushPrefix:[list objectAtIndex:i]
          namespace:[list objectAtIndex:i+1]];
  }
  
  tagURI       = [_node namespaceURI];
  tagPrefix    = [_node prefix];
  isNodeValid  = [self isTagValidInStack:_node];
  if (!isNodeValid) [self pushPrefix:tagPrefix namespace:tagURI];

  /* needs to declare namespaces !!! */
  tagName = [_node tagName];
  if ([[_node prefix] length] > 0) {
    NSString *p;

    if (!isNodeValid) {
      ns = [NSString stringWithFormat:@" xmlns:%@=\"%@\"",
                     tagPrefix,
                     tagURI];
    }
    p       = [_node prefix];
    p       = [p stringByAppendingString:@":"];
    tagName = [p stringByAppendingString:tagName];
  }
  else if ([tagURI length] > 0) {
    id   parent;
    BOOL addNS;

    addNS = YES;
    if ((parent = [_node parentNode])) {
      if ([parent nodeType] == DOM_ELEMENT_NODE) {
        if ([[parent namespaceURI] isEqualToString:tagURI]) {
          if ([[parent prefix] length] == 0)
            addNS = NO;
        }
      }
    }
    else
      addNS = YES;
    
    if (addNS)
      ns = [NSString stringWithFormat:@" xmlns=\"%@\"", [_node namespaceURI]];
    else
      ns = nil;
  }
  else
    ns = nil;
  
  if ([_node hasChildNodes]) {
    [self indentOn:_target];
    [self write:@"<"    to:_target];
    [self write:tagName to:_target];
    if (ns) [self write:ns to:_target];
    
    [self outputAttributeNodes:[_node attributes] list:list to:_target];
    [self write:@">\n"  to:_target];

    self->indent++;
    [self outputNodeList:[_node childNodes] to:_target];
    self->indent--;

    [self indentOn:_target];
    [self write:@"</"   to:_target];
    [self write:tagName to:_target];
    [self write:@">\n"  to:_target];
  }
  else {
    [self indentOn:_target];
    [self write:@"<"    to:_target];
    [self write:tagName to:_target];
    [self outputAttributeNodes:[_node attributes] list:list to:_target];
    [self write:@"/>\n" to:_target];
  }
  // pop attributes prefixes and namespaces from stack
  for (i = 0; i < count; i = i + 2) {
    [self popPrefixAndNamespace];
  }
  if (!isNodeValid) [self popPrefixAndNamespace];
}

- (void)outputCDATA:(id<DOMCharacterData>)_node to:(id)_target {
  [self write:@"<![CDATA[" to:_target];
  [self outputNodeList:[_node childNodes] to:_target];
  [self write:@"]]>" to:_target];
}

- (void)outputPI:(id<DOMProcessingInstruction>)_node to:(id)_target {
  [self indentOn:_target];
  [self write:@"<?"          to:_target];
  [self write:[_node target] to:_target];
  [self write:@" "           to:_target];
  [self write:[_node data]   to:_target];
  [self write:@"?>\n"        to:_target];
}

- (void)outputNode:(id<DOMNode>)_node to:(id)_target {
  switch ([_node nodeType]) {
    case DOM_ELEMENT_NODE:
      [self outputElementNode:(id)_node to:_target];
      break;
    case DOM_CDATA_SECTION_NODE:
      [self outputCDATA:(id)_node to:_target];
      break;
    case DOM_PROCESSING_INSTRUCTION_NODE:
      [self outputPI:(id)_node to:_target];
      break;
    case DOM_TEXT_NODE:
      [self outputTextNode:(id)_node to:_target];
      break;
    case DOM_COMMENT_NODE:
      [self outputCommentNode:(id)_node to:_target];
      break;
      
    default:
      NSLog(@"cannot output node '%@'", _node);
      break;
  }
}
- (void)outputNodeList:(id<DOMNodeList>)_nodeList to:(id)_target {
  id       children;
  unsigned i, count;
  
  children = _nodeList;
  
  for (i = 0, count = [children count]; i < count; i++)
    [self outputNode:[children objectAtIndex:i] to:_target];
}

- (void)outputDocument:(id)_document to:(id)_target {
  if (![_document hasChildNodes]) {
    NSLog(@"ERROR: document has no childnodes !");
    return;
  }
  
  [self write:@"<?xml version=\"1.0\"?>\n" to:_target];
  
  [self->stack removeAllObjects];
  [self outputNodeList:[_document childNodes] to:_target];
  
#if 0
  NS_DURING {
  }
  NS_HANDLER
    abort();
  NS_ENDHANDLER;
#endif
}

@end /* DOMXMLOutputter */


@implementation DOMXMLOutputter(PrefixStack)

- (void)_checkPrefixStack {
  NSAssert2(([self->stack count] % 2 == 0),
            @"%s: prefixStack is not valid (%@)!!!",
            __PRETTY_FUNCTION__,
            self->stack);
}

- (NSString *)topPrefix {
  [self _checkPrefixStack];
  if ([self->stack count] == 0) return nil;
  return [self->stack objectAtIndex:[self->stack count] -2];
}

- (NSString *)topNamespace {
  [self _checkPrefixStack];
  if ([self->stack count] == 0) return nil;
  return [self->stack lastObject];
}

- (void)pushPrefix:(NSString *)_prefix namespace:(NSString *)_namespace {
  [self _checkPrefixStack];
  [self->stack addObject:(_prefix)    ? _prefix    : (NSString *)@""];
  [self->stack addObject:(_namespace) ? _namespace : (NSString *)@""];
}

- (void)popPrefixAndNamespace {
  [self _checkPrefixStack];
  NSAssert1(([self->stack count] > 0), @"%s: prefixStack.count == 0",
            __PRETTY_FUNCTION__);
  [self->stack removeLastObject]; // namespace
  [self->stack removeLastObject]; // prefix
}

- (BOOL)isTagValidInStack:(id)_node {
  NSString *nodeNamespace;
  NSString *nodePrefix;
  int      i;

  nodePrefix    = [_node prefix];
  nodeNamespace = [_node namespaceURI];
  
  for (i = [self->stack count]; i >= 2; i = i - 2) {
    NSString *namespace;
    NSString *prefix;

    prefix    = [self->stack objectAtIndex:i-2];
    namespace = [self->stack objectAtIndex:i-1];
    if ([nodePrefix isEqualToString:prefix] &&
        [nodeNamespace isEqualToString:namespace])
      return YES;
  }
  return NO;
}

- (NSArray *)newAttributePrefixesAndNamespaces:(NSArray *)_attrs {
  NSMutableArray *result;
  int            i, j, count;

  count = [_attrs count];
  
  if (count == 0) return [NSArray array];

  result = [[NSMutableArray alloc] initWithCapacity:count];
  for (j = 0; j < count; j++) {
    id       attr;
    NSString *attrNamespace;
    NSString *attrPrefix;
    BOOL     didMatch = NO;

    attr          = [_attrs objectAtIndex:j];
    attrNamespace = [attr namespaceURI];
    attrPrefix    = [attr prefix];
    attrNamespace = (attrNamespace) ? attrNamespace : (NSString *)@"";
    attrPrefix    = (attrPrefix)    ? attrPrefix    : (NSString *)@"";

    if (([attrNamespace length] == 0 && [attrPrefix length] == 0)) continue;
    
    for (i = [self->stack count]; i >= 2; i = i - 2) {
      NSString *namespace;
      NSString *prefix;

      prefix    = [self->stack objectAtIndex:i-2];
      namespace = [self->stack objectAtIndex:i-1];
      if ([attrPrefix isEqualToString:prefix] &&
          [attrNamespace isEqualToString:namespace]) {
        didMatch = YES;
        break;
      }
    }
    if (didMatch == NO) {
      [result addObject:attrPrefix];
      [result addObject:attrNamespace];
    }
  }
  return [result autorelease];
}

@end /* DOMXMLOutputter(PrefixStack) */