source: tags/arb_5.0/XML/xml.hxx

Last change on this file was 5675, checked in by westram, 15 years ago
  • removed automatic timestamps (the best they were good for, were vc-conflicts)
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2001
4// Ralf Westram
5//
6// Permission to use, copy, modify, distribute and sell this software
7// and its documentation for any purpose is hereby granted without fee,
8// provided that the above copyright notice appear in all copies and
9// that both that copyright notice and this permission notice appear
10// in supporting documentation.  Ralf Westram makes no
11// representations about the suitability of this software for any
12// purpose.  It is provided "as is" without express or implied warranty.
13//
14// This code is part of my library.
15// You may find a more recent version at http://www.reallysoft.de/
16//
17/////////////////////////////////////////////////////////////////////////////
18
19#ifndef XML_HXX
20#define XML_HXX
21
22#ifndef __STRING__
23#include <string>
24#endif
25#ifndef __CSTDIO__
26#include <cstdio>
27#endif
28
29#ifndef ATTRIBUTES_H
30#include <attributes.h>
31#endif
32
33
34#ifndef ARB_ASSERT_H
35#include <arb_assert.h>
36#endif
37#define xml_assert(bed) arb_assert(bed)
38
39
40/** @memo Classes used to write xml
41    @doc  In order to write xml to a stream just open an XML_Document and create and destroy the needed tags.
42*/
43
44class                XML_Document;
45extern XML_Document *the_XML_Document; // there can only be one at a time
46
47//  ----------------------------
48//      class XML_Attribute
49//  ----------------------------
50class XML_Attribute {
51private:
52    std::string         name;
53    std::string         content;
54    XML_Attribute *next;
55
56public:
57    XML_Attribute(const std::string& name_, const std::string& content_);
58    virtual ~XML_Attribute();
59
60    XML_Attribute *append_to(XML_Attribute *queue);
61
62    void print(FILE *out) const;
63};
64
65
66//  -----------------------
67//      class XML_Node
68//  -----------------------
69class XML_Node {
70protected:
71    XML_Node *father;
72    bool      opened;
73    int       indent;
74
75public:
76    XML_Node(bool is_tag);
77    virtual ~XML_Node();
78
79    int Indent() const { return indent; }
80    bool Opened() const { return opened; }
81
82    virtual void add_son(XML_Node *son_, bool son_is_tag) = 0;
83    virtual void remove_son(XML_Node *son_)               = 0;
84    virtual void open(FILE *out)                          = 0;
85    virtual void close(FILE *out)                         = 0;
86};
87
88//  ----------------------------------------
89//      class XML_Tag : public XML_Node
90//  ----------------------------------------
91
92/// xml element
93class XML_Tag : public XML_Node {
94private:
95    std::string    name;
96    XML_Node      *son;
97    XML_Attribute *attribute;
98    int            state;       // 0        = no son; 1 = only content; 2 = son-tag;
99    bool           onExtraLine; // default = true; if false -> does not print linefeed before tag
100
101public:
102    /** Create a new xml element
103        @param name_ element name
104    */
105    XML_Tag(const std::string &name_);
106    virtual ~XML_Tag();
107
108    /** add an attribute to the XML_Tag
109        @param name_ attribute name
110        @param content_ attribute value
111    */
112    void         add_attribute(const std::string& name_, const std::string& content_);
113    void         add_attribute(const std::string& name_, int value);
114    virtual void add_son(XML_Node *son_, bool son_is_tag);
115    virtual void remove_son(XML_Node *son_);
116    virtual void open(FILE *out);
117    virtual void close(FILE *out);
118
119    void set_on_extra_line(bool oel) { onExtraLine = oel; }
120};
121
122//  -----------------------------------------
123//      class XML_Text : public XML_Node
124//  -----------------------------------------
125/// a xml text node
126class XML_Text : public XML_Node {
127private:
128    std::string content;
129
130public:
131    /** Create text (content) in xml
132        @param content_ the content
133    */
134    XML_Text(const std::string& content_) : XML_Node(false), content(content_) {}
135    virtual ~XML_Text();
136
137    virtual void add_son(XML_Node *son_, bool son_is_tag) __ATTR__NORETURN;
138    virtual void remove_son(XML_Node *son_) __ATTR__NORETURN;
139    virtual void open(FILE *);
140    virtual void close(FILE *out);
141};
142
143//  --------------------------------------------
144//      class XML_Comment : public XML_Text
145//  --------------------------------------------
146
147class XML_Comment : public XML_Node {
148    std::string content;
149public:
150    XML_Comment(const std::string& content_) : XML_Node(false), content(content_) {}
151    virtual ~XML_Comment();
152
153    virtual void add_son(XML_Node *son_, bool son_is_tag) __ATTR__NORETURN;
154    virtual void remove_son(XML_Node *son_) __ATTR__NORETURN;
155    virtual void open(FILE *) ;
156    virtual void close(FILE *out);
157};
158
159//  ---------------------------
160//      class XML_Document
161//  ---------------------------
162/// an entire xml document
163class XML_Document {
164private:
165    std::string  dtd;
166    XML_Tag     *root;
167    XML_Node    *latest_son;
168    FILE        *out;
169
170public:
171    /** Create and stream (at destruction) a xml document
172        @param name_ name of the root node
173        @param dtd_ filename of dtd
174        @param out_ FILE where xml document will be written to
175    */
176    XML_Document(const std::string& name_, const std::string& dtd_, FILE *out_);
177    virtual ~XML_Document();
178
179    /// true -> tags w/o content or attributes are skipped (default = false)
180    bool skip_empty_tags;
181
182    /// how many columns are used per indentation level (defaults to 1)
183    size_t indentation_per_level;
184
185    XML_Node* LatestSon() { return latest_son; }
186    void set_LatestSon(XML_Node* latest_son_) { latest_son = latest_son_; }
187
188    XML_Tag& getRoot() { return *root; }
189
190    void add_attribute(const std::string& name_, const std::string& content_) {
191        getRoot().add_attribute(name_, content_);
192    }
193
194    FILE *Out() { return out; }
195};
196
197#else
198#error xml.hxx included twice
199#endif // XML_HXX
Note: See TracBrowser for help on using the repository browser.