2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "DOMPYXOutputter.h"
23 #include "DOMDocument.h"
24 #include "DOMElement.h"
27 @interface NGDOMPYXOutputter(Privates)
28 - (void)outputNode:(id)_node to:(id)_target;
29 - (void)outputNodeList:(id)_nodeList to:(id)_target;
32 @implementation NGDOMPYXOutputter
34 - (void)write:(NSString *)s to:(id)_target {
35 printf("%s", [s cString]);
37 - (BOOL)currentElementPreservesWhitespace {
41 - (void)outputAttributeNode:(id<DOMAttr>)_attrNode
42 ofNode:(id<DOMNode>)_node
45 [self write:@"A" to:_target];
46 [self write:[_attrNode name] to:_target];
47 [self write:@" " to:_target];
48 [self write:[_attrNode nodeValue] to:_target];
49 [self write:@"\n" to:_target];
52 - (void)outputAttributeNodes:(id<DOMNamedNodeMap>)_nodes
53 ofNode:(id<DOMNode>)_node
58 if ((count = [_nodes length]) == 0)
61 for (i = 0; i < count; i++) {
62 [self outputAttributeNode:[_nodes objectAtIndex:i]
68 - (void)outputTextNode:(id<DOMText>)_node to:(id)_target {
73 if ((len = [s length]) == 0)
76 if ([s rangeOfString:@"\n"].length != 0) {
77 s = [[s componentsSeparatedByString:@"\n"]
78 componentsJoinedByString:@"\\n"];
81 [self write:@"-" to:_target];
82 [self write:s to:_target];
83 [self write:@"\n" to:_target];
85 - (void)outputCommentNode:(id<DOMComment>)_node to:(id)_target {
86 [self write:@"<!-- " to:_target];
87 [self write:[_node data] to:_target];
88 [self write:@" -->" to:_target];
90 if (![self currentElementPreservesWhitespace])
91 [self write:@"\n" to:_target];
94 - (void)outputElementNode:(id<DOMElement>)_node to:(id)_target {
98 /* needs to declare namespaces !!! */
99 tagName = [_node tagName];
100 if ([[_node prefix] length] > 0) {
101 NSString *p = [_node prefix];
103 p = [p stringByAppendingString:@":"];
104 tagName = [p stringByAppendingString:tagName];
106 ns = [NSString stringWithFormat:@" xmlns:%@=\"%@\"",
108 [_node namespaceURI]];
110 else if ([[_node namespaceURI] length] > 0) {
111 ns = [NSString stringWithFormat:@" xmlns=\"%@\"", [_node namespaceURI]];
116 [self write:@"(" to:_target];
117 [self write:tagName to:_target];
118 [self write:@"\n" to:_target];
120 [self outputAttributeNodes:[_node attributes] ofNode:_node to:_target];
122 if ([_node hasChildNodes])
123 [self outputNodeList:[_node childNodes] to:_target];
125 [self write:@")" to:_target];
126 [self write:tagName to:_target];
127 [self write:@"\n" to:_target];
130 - (void)outputCDATA:(id<DOMCharacterData>)_node to:(id)_target {
135 if ([s rangeOfString:@"\n"].length != 0) {
136 /* escape newlines */
137 s = [[s componentsSeparatedByString:@"\n"]
138 componentsJoinedByString:@"\\n"];
141 [self write:@"-" to:_target];
142 [self write:s to:_target];
143 [self write:@"\n" to:_target];
146 - (void)outputPI:(id<DOMProcessingInstruction>)_node to:(id)_target {
147 [self write:@"?" to:_target];
148 [self write:[_node target] to:_target];
149 [self write:@" " to:_target];
150 [self write:[_node data] to:_target];
151 [self write:@"\n" to:_target];
154 - (void)outputNode:(id)_node to:(id)_target {
155 switch ([_node nodeType]) {
156 case DOM_ELEMENT_NODE:
157 [self outputElementNode:_node to:_target];
159 case DOM_CDATA_SECTION_NODE:
160 [self outputCDATA:_node to:_target];
162 case DOM_PROCESSING_INSTRUCTION_NODE:
163 [self outputPI:_node to:_target];
166 [self outputTextNode:_node to:_target];
168 case DOM_COMMENT_NODE:
169 [self outputCommentNode:_node to:_target];
173 NSLog(@"cannot output node %@", _node);
177 - (void)outputNodeList:(id)_nodeList to:(id)_target {
181 children = _nodeList;
183 for (i = 0, count = [children count]; i < count; i++)
184 [self outputNode:[children objectAtIndex:i] to:_target];
187 - (void)outputDocument:(id)_document to:(id)_target {
188 if (![_document hasChildNodes])
192 [self outputNodeList:[_document childNodes] to:_target];
194 fprintf(stderr, "%s\n", [[localException description] cString]);
199 @end /* DOMPYXOutputter */