]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/xml/xmlelement.h
ok
[xonotic/netradiant.git] / libs / xml / xmlelement.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #if !defined(INCLUDED_XML_XMLELEMENT_H)
23 #define INCLUDED_XML_XMLELEMENT_H
24
25 #include "xml/ixml.h"
26 #include "string/string.h"
27
28 #include <map>
29
30 class StaticElement : public XMLElement
31 {
32   struct strless
33   {
34     bool operator()(const char* s1, const char* s2) const
35     {
36       return strcmp(s1, s2) < 0;
37     }
38   };
39
40   typedef std::map<const char*, const char*, strless> attrs_t;
41 public:
42   StaticElement(const char* name)
43     : m_name(name)
44   {
45   }
46   void insertAttribute(const char* name, const char* value)
47   {
48     m_attrs.insert(attrs_t::value_type(name, value));
49   }
50   const char* name() const
51   {
52     return m_name;
53   }
54   const char* attribute(const char* name) const
55   {
56     attrs_t::const_iterator i = m_attrs.find(name);
57     if(i != m_attrs.end())
58       return i->second;
59     else
60       return "";
61   }
62   void forEachAttribute(XMLAttrVisitor& visitor) const
63   {
64     for(attrs_t::const_iterator i = m_attrs.begin(); i != m_attrs.end(); ++i)
65     {
66       visitor.visit(i->first, i->second);
67     }
68   }
69 private:
70   const char* m_name;
71   attrs_t m_attrs;
72 };
73
74 class DynamicElement : public XMLElement
75 {
76   typedef std::map<CopiedString, CopiedString> attrs_t;
77 public:
78   DynamicElement(const char* name)
79     : m_name(name)
80   {}
81   void insertAttribute(const char* name, const char* value)
82   {
83     m_attrs.insert(attrs_t::value_type(name, value));
84   }
85   const char* name() const
86   {
87     return m_name.c_str();
88   }
89   const char* attribute(const char* name) const
90   {
91     attrs_t::const_iterator i = m_attrs.find(name);
92     if(i != m_attrs.end())
93       return i->second.c_str();
94     else
95       return "";
96   }
97   void forEachAttribute(XMLAttrVisitor& visitor) const
98   {
99     for(attrs_t::const_iterator i = m_attrs.begin(); i != m_attrs.end(); ++i)
100     {
101       visitor.visit(i->first.c_str(), i->second.c_str());
102     }
103   }
104 private:
105   CopiedString m_name;
106   attrs_t m_attrs;
107 };
108
109 #endif