moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kvoctrain / kvoctrain / kvt-core / kvt-xml / XmlWriter.cpp
blob585543468cec3af1a002ad5060f395176ad9da96
1 /* -*- C++ -*-
3 $Id$
5 This file is part of KIllustrator.
6 Copyright (C) 1998 Kai-Uwe Sattler (kus@iti.cs.uni-magdeburg.de)
8 modified for kvoctrain by Ewald Arnold kvoctrain@ewald-arnold.dein April ยด99
10 -----------------------------------------------------------------------
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Library General Public License as
14 published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU Library General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "XmlWriter.h"
30 XmlWriter::XmlWriter (KOXML_OSTREAM& os)
31 : strm (os)
33 autoendl = true;
34 isapo = false;
35 apo = 0;
36 strm << "<?xml version=\"1.0\"?>" << endl; // "encoding=UTF8"
40 XmlWriter::~XmlWriter () {
41 flush ();
45 void XmlWriter::startTag (KOXML_STRING id, bool closeIt, bool empty, bool eol)
47 if (!id) return;
49 strm << "<" << id;
50 if (!empty) {
51 lastTags.push (id);
54 if (closeIt) {
55 if (empty)
56 strm << "/";
57 strm << ">";
58 if (eol || autoendl) {
59 isapo = false;
60 apo = 0;
61 strm << endl;
67 void XmlWriter::endTag (KOXML_STRING id, bool eol)
69 strm << "</";
70 if (id.length() != 0)
71 strm << id;
72 else {
73 KOXML_STRING tag = lastTags.top ();
74 lastTags.pop ();
75 strm << tag;
77 strm << ">";
78 if (eol || autoendl) {
79 isapo = false;
80 apo = 0;
81 strm << endl;
85 void XmlWriter::closeTag (bool empty, bool eol)
87 if (empty) {
88 strm << "/";
89 lastTags.pop ();
91 strm << ">";
92 if (eol || autoendl) {
93 isapo = false;
94 apo = 0;
95 strm << endl;
100 void XmlWriter::setAutoEndl (const bool flag)
102 autoendl = flag;
106 void XmlWriter::endline()
108 isapo = false;
109 apo = 0;
110 strm << endl;
114 void XmlWriter::addAttribute (KOXML_STRING name, const KOXML_STRING& value)
116 if (!name) return;
118 KOXML_STRING val = value;
119 // escape dangerous characters in sgml-style
120 int pos = 0;
121 while ((pos = val.find ('&', pos)) >= 0) {
122 KOXML_STRING_INSERT( val, pos+1, "amp;");
123 pos += 5; // skip &amp;
125 pos = 0;
126 while ((pos = val.find ('<', pos)) >= 0) {
127 KOXML_STRING_REMOVE( val, pos, 1);
128 KOXML_STRING_INSERT( val, pos, "&lt;");
129 pos += 4; // skip &nl;
131 pos = 0;
132 while ((pos = val.find ('\n', pos)) >= 0) {
133 KOXML_STRING_REMOVE( val, pos, 1);
134 KOXML_STRING_INSERT( val, pos, "&nl;");
135 pos += 4; // skip &nl;
137 pos = 0;
138 while ((pos = val.find ('\r', pos)) >= 0) {
139 KOXML_STRING_REMOVE( val, pos, 1);
140 KOXML_STRING_INSERT( val, pos+1, "lf;");
141 pos += 4; // skip &lf;
143 pos = 0;
144 while ((pos = val.find ('\"', pos)) >= 0) {
145 KOXML_STRING_REMOVE( val, pos, 1);
146 KOXML_STRING_INSERT( val, pos, "&quot;");
147 pos += 6; // skip &qout;
150 strm << " ";
151 strm << name << "=\"";
152 strm << val;
153 strm << "\"";
157 void XmlWriter::addAttribute (KOXML_STRING name, int value)
159 if (name.length() == 0)
160 return;
162 strm << " ";
163 strm << name << "=\"";
164 strm << value;
165 strm << "\"";
169 void XmlWriter::addAttribute (KOXML_STRING name, float value)
171 if (name.length() == 0)
172 return;
174 strm << " ";
175 strm << name << "=\"";
176 strm << value;
177 strm << "\"";
181 void XmlWriter::addAttribute (KOXML_STRING name, double value)
183 if (name.length() == 0)
184 return;
186 strm << " ";
187 strm << name << "=\"";
188 strm << value;
189 strm << "\"";
193 void XmlWriter::writeText (KOXML_STRING c)
195 int i = 0;
196 while (i < (int) c.length()) {
197 if (c[i] == '<')
198 strm << "&lt;";
199 else if (c[i] == '&')
200 strm << "&amp;";
201 else if (c[i] == '>')
202 strm << "&gt;";
203 else if (c[i] == '\"' || c[i] == '\'' || c[i] == '`') {
204 strm << c[i];
205 if (isapo) {
206 if (apo == c[i])
207 isapo = false;
209 else {
210 isapo = true;
211 apo = c[i];
214 else if (c[i] == '\n') {
215 if (isapo)
216 strm << "&nl;";
217 else
218 strm << c[i];
220 else if (c[i] == '\r') {
221 if (isapo)
222 strm << "&lf;";
223 else
224 strm << c[i];
226 else
227 strm << c[i];
228 i++;
233 void XmlWriter::indent (int i)
235 for (; i > 0; i--)
236 strm << " ";
240 void XmlWriter::writeTag (KOXML_STRING s)
242 strm << "<"
243 << s
244 << ">";
248 void XmlWriter::flush ()
250 #ifndef KOXML_USE_STL
251 stream()->flush ();
252 #else
253 strm.flush();
254 #endif