1 /**
2 * Phoebe DOM Implementation.
3 *
4 * This is a C++ approximation of the W3C DOM model, which follows
5 * fairly closely the specifications in the various .idl files, copies of
6 * which are provided for reference. Most important is this one:
7 *
8 * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
9 *
10 * Authors:
11 * Bob Jamison
12 *
13 * Copyright (C) 2005-2008 Bob Jamison
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
33 #include <stdio.h>
34 #include <stdarg.h>
36 #include "xmlwriter.h"
38 namespace org
39 {
40 namespace w3c
41 {
42 namespace dom
43 {
47 //#########################################################################
48 //# O U T P U T
49 //#########################################################################
51 /**
52 *
53 */
54 void XmlWriter::spaces()
55 {
56 for (int i=0 ; i<indent ; i++)
57 {
58 buf.push_back(' ');
59 }
60 }
62 /**
63 *
64 */
65 void XmlWriter::po(const char *fmt, ...)
66 {
67 char str[257];
68 va_list args;
69 va_start(args, fmt);
70 vsnprintf(str, 256, fmt, args);
71 va_end(args) ;
73 buf.append(str);
74 }
77 void XmlWriter::pos(const DOMString &str)
78 {
79 buf.append(str);
80 }
82 /**
83 *
84 */
85 void XmlWriter::write(const NodePtr nodeArg)
86 {
87 NodePtr node = nodeArg;
89 indent+=2;
91 NamedNodeMap attributes = node->getAttributes();
92 int nrAttrs = attributes.getLength();
94 //### Start open tag
95 spaces();
96 po("<");
97 pos(node->getNodeName());
98 if (nrAttrs>0)
99 po("\n");
101 //### Attributes
102 for (int i=0 ; i<nrAttrs ; i++)
103 {
104 NodePtr attr = attributes.item(i);
105 spaces();
106 pos(attr->getNodeName());
107 po("=\"");
108 pos(attr->getNodeValue());
109 po("\"\n");
110 }
112 //### Finish open tag
113 if (nrAttrs>0)
114 spaces();
115 po(">\n");
117 //### Contents
118 spaces();
119 pos(node->getNodeValue());
121 //### Children
122 for (NodePtr child = node->getFirstChild() ;
123 child.get() ;
124 child=child->getNextSibling())
125 {
126 write(child);
127 }
129 //### Close tag
130 spaces();
131 po("</");
132 pos(node->getNodeName());
133 po(">\n");
135 indent-=2;
136 }
139 /**
140 *
141 */
142 void XmlWriter::writeFile(FILE *f, const NodePtr node)
143 {
144 if (!node)
145 {
146 po("XmlWriter: NULL document\n");
147 return;
148 }
150 indent = 0;
152 //po("Document\n");
154 buf = "";
156 write(node);
158 for (unsigned int i=0 ; i<buf.size() ; i++)
159 {
160 int ch = buf[i];
161 fputc(ch, f);
162 }
163 fflush(f);
165 buf = "";
167 }
171 //#########################################################################
172 //# C O N S T R U C T O R / D E S T R U C T O R
173 //#########################################################################
175 /**
176 *
177 */
178 XmlWriter::XmlWriter()
179 {
180 }
182 /**
183 *
184 */
185 XmlWriter::~XmlWriter()
186 {
187 }
191 } //namespace dom
192 } //namespace w3c
193 } //namespace org
195 //#########################################################################
196 //# E N D O F F I L E
197 //#########################################################################