Namespace.h
1 /*
2  * Copyright (C) 2012 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_Xml_Namespace_h
30 #define Pt_Xml_Namespace_h
31 
32 #include <Pt/Xml/Api.h>
33 #include <Pt/String.h>
34 #include <vector>
35 #include <cstddef>
36 
37 namespace Pt {
38 
39 namespace Xml {
40 
46 class Namespace
47 {
48  public:
51  Namespace(std::size_t depth, const String& prefix, const String& uri)
52  : _depth(depth)
53  , _prefix(prefix)
54  , _uri(uri)
55  { }
56 
59  std::size_t depth() const
60  { return _depth; }
61 
64  const String& prefix() const
65  { return _prefix; }
66 
69  void setPrefix(const String& prefix)
70  { _prefix = prefix; }
71 
74  const String& namespaceUri() const
75  { return _uri; }
76 
79  void setNamespaceUri(const String& uri)
80  { _uri = uri; }
81 
84  bool isDefaultNamespace() const
85  { return _prefix.empty(); }
86 
89  bool isUnset() const
90  { return _uri.empty(); }
91 
92  private:
93  std::size_t _depth;
94  String _prefix;
95  String _uri;
96 };
97 
98 
99 /* A list of namespaces, mapped or umapped to a prefix.
100 */
101 class NamespaceMapping
102 {
103  public:
104  class Entry
105  {
106  public:
107  Entry(const Namespace& ns, bool mapped)
108  : _ns(&ns)
109  , _mapped(mapped)
110  {}
111 
112  bool isMapped() const
113  { return _mapped; }
114 
115  bool isUnmapped() const
116  { return ! _mapped; }
117 
118  const String& namespaceUri() const
119  { return _ns->namespaceUri(); }
120 
121  const String& prefix() const
122  { return _ns->prefix(); }
123 
124  private:
125  const Namespace* _ns;
126  bool _mapped;
127  };
128 
129  typedef const Entry* ConstIterator;
130  typedef Entry* Iterator;
131 
132  public:
133  NamespaceMapping()
134  {}
135 
136  void clear()
137  { _entries.clear(); }
138 
139  std::size_t size() const
140  { return _entries.size(); }
141 
142  std::size_t empty() const
143  { return _entries.empty(); }
144 
145  ConstIterator begin() const
146  { return _entries.empty() ? 0 : &_entries[0]; }
147 
148  ConstIterator end() const
149  { return _entries.empty() ? 0 : &_entries[ _entries.size() ]; }
150 
151  Iterator begin()
152  { return _entries.empty() ? 0 : &_entries[0]; }
153 
154  Iterator end()
155  { return _entries.empty() ? 0 : &_entries[ _entries.size() ]; }
156 
157  void addMapped(const Namespace& ns)
158  { _entries.push_back( Entry(ns, true) ); }
159 
160  void addUnmapped(const Namespace& ns)
161  { _entries.push_back( Entry(ns, false) ); }
162 
163  private:
164  std::vector<Entry> _entries;
165 };
166 
167 } // namespace Xml
168 
169 } // namespace Pt
170 
171 #endif // Pt_Xml_Namespace_h
A namespace used in an XML document.
Definition: Namespace.h:46
bool isUnset() const
Returns true if explicitly unset.
Definition: Namespace.h:89
void setNamespaceUri(const String &uri)
Sets the URI of the namespace.
Definition: Namespace.h:79
bool empty() const
Returns true if empty.
Definition: String.h:244
bool isDefaultNamespace() const
Returns true if this is the default namespace.
Definition: Namespace.h:84
const String & namespaceUri() const
Returns the namespace URI.
Definition: Namespace.h:74
std::size_t depth() const
Returns the scope depth of the namespace.
Definition: Namespace.h:59
Unicode capable basic_string.
Definition: String.h:42
Namespace(std::size_t depth, const String &prefix, const String &uri)
Constructs a Namespace with scope depth, name and prefix.
Definition: Namespace.h:51
const String & prefix() const
Returns the prefix of this namespace.
Definition: Namespace.h:64
void setPrefix(const String &prefix)
Sets the prefix of this namespace.
Definition: Namespace.h:69