moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kvoctrain / kvoctrain / kvt-core / kvt-xml / XmlWriter.h
blob7334a83abd348cc352f65115271dc350c6890ed6
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
12 #ifndef XmlWriter_h_
13 #define XmlWriter_h_
15 #if defined(stack)
16 #undef stack
17 #endif
19 #include <stack>
20 #include <vector>
21 using namespace std;
23 #include "koxml_config.h"
25 class KOXML_OSTREAM;
26 /**
27 * The XMLWriter class provides support for writing XML streams.
28 * It contains methods for output XML elements with attributes.
30 * Sample code:
31 * <pre>
32 * KOXML_OSTREAM os (fname);
33 * XmlWriter xml (os); // writes the XML header
35 * xml.startTag ("head"); // writes <head>
37 * // write <layout format="a4" orientation="landscape">
38 * xml.startTag ("layout", false);
39 * xml.addAttribute ("format", "a4");
40 * xml.addAttribute ("orientation", "landscape");
41 * xml.closeTag (true);
42 * </pre>
44 * @short A helper class for writing XML.
45 * @author Kai-Uwe Sattler (kus@iti.cs.uni-magdeburg.de), modifications: Ewald Arnold (kvoctrain@ewald-arnold.de)
46 * @version 2000/07/02
49 class XmlWriter {
50 public:
51 /**
52 * Create a XmlWriter instance for the given output stream.
54 * @param os The open output stream for writing.
56 XmlWriter (KOXML_OSTREAM& os);
58 /**
59 * Desctructor.
61 ~XmlWriter ();
63 /**
64 * Write an element with the given ID to the XML stream. Attributes
65 * can be added later.
67 * @param id The element ID.
68 * @param closeIt If @p true the tag is closed by >,
69 * otherwise not.
70 * @param empty If @p true an empty element is written, which
71 * is closed by />.
72 * @param eol If @p true an eol char is appended
73 * even if autoendl is false
75 void startTag (KOXML_STRING id, bool closeIt = true, bool empty = false, bool eol = false);
77 /**
78 * Write the end tag according to the given element ID or to the
79 * last opened element.
81 * @param id The element ID. If @p "" the last opened
82 * element is ended. (default).
83 * @param eol If @p true an eol char is appended
84 * even if autoendl is false
86 void endTag (KOXML_STRING id = "", bool eol = false);
88 /**
89 * flag, indicating to automatically append end-of-line character after
90 * closing bracket
92 * @param auto If @p true, append eol character. Otherwise caller has to do it
94 void setAutoEndl (const bool flag = true);
96 /**
97 * append end-of-line char to stream
99 void endline ();
102 * Close the current open element.
104 * @param empty If @p true an empty element is closed.
105 * @param eol If @p true an eol char is appended
106 * even if autoendl is false
108 void closeTag (bool empty = false, bool eol = false);
111 * Add an attribute with the given value to the current element.
112 * This method doesn't check, if an element is open.
114 * @param name The attribute name.
115 * @param value The string value of the attribute.
117 void addAttribute (KOXML_STRING name, const KOXML_STRING& value);
120 * Add an attribute with the given value to the current element.
121 * This method doesn't check, if an element is open.
123 * @param name The attribute name.
124 * @param value The integer value of the attribute.
126 void addAttribute (KOXML_STRING name, int value);
129 * Add an attribute with the given value to the current element.
130 * This method doesn't check, if an element is open.
132 * @param name The attribute name.
133 * @param value The float value od the attribute.
135 void addAttribute (KOXML_STRING name, float value);
138 * Add an attribute with the given value to the current element.
139 * This method doesn't check, if an element is open.
141 * @param name The attribute name.
142 * @param value The double value of the attribute.
144 void addAttribute (KOXML_STRING name, double value);
147 * Write text to the XML stream. The method encodes the special
148 * characters @p <, @p > and @p &.
150 * @param s The text.
152 void writeText (KOXML_STRING s);
155 * Write a tag to the XML stream.
157 * @param s The tag without the brackets.
159 void writeTag (KOXML_STRING s);
162 * indents next text string with some spaces
164 * @param i number of spaces to indent
166 void indent (int i);
169 * Flush the XML output stream.
171 void flush ();
173 #ifndef KOXML_USE_STL
175 * Get the raw output stream.
177 inline QIODevice *stream () { return strm.device(); }
178 #endif
180 private:
181 stack<KOXML_STRING, vector<KOXML_STRING> > lastTags;
183 KOXML_OSTREAM &strm;
184 bool autoendl;
185 bool isapo;
186 KOXML_CHAR apo;
189 #endif