]> err.no Git - sope/blob - sope-xml/DOM/DOMPYXOutputter.m
fixed a Tiger warning
[sope] / sope-xml / DOM / DOMPYXOutputter.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "DOMPYXOutputter.h"
23 #include "DOMDocument.h"
24 #include "DOMElement.h"
25 #include "common.h"
26
27 @interface NGDOMPYXOutputter(Privates)
28 - (void)outputNode:(id)_node to:(id)_target;
29 - (void)outputNodeList:(id)_nodeList to:(id)_target;
30 @end
31
32 @implementation NGDOMPYXOutputter
33
34 - (void)write:(NSString *)s to:(id)_target {
35   printf("%s", [s cString]);
36 }
37 - (BOOL)currentElementPreservesWhitespace {
38   return NO;
39 }
40
41 - (void)outputAttributeNode:(id<DOMAttr>)_attrNode
42   ofNode:(id<DOMNode>)_node
43   to:(id)_target
44 {
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];
50 }
51
52 - (void)outputAttributeNodes:(id<DOMNamedNodeMap>)_nodes
53   ofNode:(id<DOMNode>)_node
54   to:(id)_target
55 {
56   unsigned i, count;
57   
58   if ((count = [_nodes length]) == 0)
59     return;
60   
61   for (i = 0; i < count; i++) {
62     [self outputAttributeNode:[_nodes objectAtIndex:i]
63           ofNode:_node
64           to:_target];
65   }
66 }
67
68 - (void)outputTextNode:(id<DOMText>)_node to:(id)_target {
69   NSString *s;
70   unsigned len;
71   
72   s = [_node data];
73   if ((len = [s length]) == 0)
74     return;
75   
76   if ([s rangeOfString:@"\n"].length != 0) {
77     s = [[s componentsSeparatedByString:@"\n"]
78             componentsJoinedByString:@"\\n"];
79   }
80   
81   [self write:@"-"  to:_target];
82   [self write:s     to:_target];
83   [self write:@"\n" to:_target];
84 }
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];
89   
90   if (![self currentElementPreservesWhitespace])
91     [self write:@"\n" to:_target];
92 }
93
94 - (void)outputElementNode:(id<DOMElement>)_node to:(id)_target {
95   NSString *tagName;
96   NSString *ns;
97   
98   /* needs to declare namespaces !!! */
99   tagName = [_node tagName];
100   if ([[_node prefix] length] > 0) {
101     NSString *p = [_node prefix];
102     
103     p       = [p stringByAppendingString:@":"];
104     tagName = [p stringByAppendingString:tagName];
105
106     ns = [NSString stringWithFormat:@" xmlns:%@=\"%@\"",
107                      [_node prefix],
108                      [_node namespaceURI]];
109   }
110   else if ([[_node namespaceURI] length] > 0) {
111     ns = [NSString stringWithFormat:@" xmlns=\"%@\"", [_node namespaceURI]];
112   }
113   else
114     ns = nil;
115
116   [self write:@"("    to:_target];
117   [self write:tagName to:_target];
118   [self write:@"\n"   to:_target];
119     
120   [self outputAttributeNodes:[_node attributes] ofNode:_node to:_target];
121   
122   if ([_node hasChildNodes])
123     [self outputNodeList:[_node childNodes] to:_target];
124     
125   [self write:@")"    to:_target];
126   [self write:tagName to:_target];
127   [self write:@"\n"   to:_target];
128 }
129
130 - (void)outputCDATA:(id<DOMCharacterData>)_node to:(id)_target {
131   NSString *s;
132
133   s = [_node data];
134   
135   if ([s rangeOfString:@"\n"].length != 0) {
136     /* escape newlines */
137     s = [[s componentsSeparatedByString:@"\n"]
138             componentsJoinedByString:@"\\n"];
139   }
140   
141   [self write:@"-"  to:_target];
142   [self write:s     to:_target];
143   [self write:@"\n" to:_target];
144 }
145
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];
152 }
153
154 - (void)outputNode:(id)_node to:(id)_target {
155   switch ([_node nodeType]) {
156     case DOM_ELEMENT_NODE:
157       [self outputElementNode:_node to:_target];
158       break;
159     case DOM_CDATA_SECTION_NODE:
160       [self outputCDATA:_node to:_target];
161       break;
162     case DOM_PROCESSING_INSTRUCTION_NODE:
163       [self outputPI:_node to:_target];
164       break;
165     case DOM_TEXT_NODE:
166       [self outputTextNode:_node to:_target];
167       break;
168     case DOM_COMMENT_NODE:
169       [self outputCommentNode:_node to:_target];
170       break;
171       
172     default:
173       NSLog(@"cannot output node %@", _node);
174       break;
175   }
176 }
177 - (void)outputNodeList:(id)_nodeList to:(id)_target {
178   id       children;
179   unsigned i, count;
180   
181   children = _nodeList;
182   
183   for (i = 0, count = [children count]; i < count; i++)
184     [self outputNode:[children objectAtIndex:i] to:_target];
185 }
186
187 - (void)outputDocument:(id)_document to:(id)_target {
188   if (![_document hasChildNodes])
189     return;
190
191   NS_DURING
192     [self outputNodeList:[_document childNodes] to:_target];
193   NS_HANDLER
194     fprintf(stderr, "%s\n", [[localException description] cString]);
195     abort();
196   NS_ENDHANDLER;
197 }
198
199 @end /* DOMPYXOutputter */