bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
XMLHelpers.h
1
2// This file is part of the "NcML Module" project, a BES module designed
3// to allow NcML files to be used to be used as a wrapper to add
4// AIS to existing datasets of any format.
5//
6// Copyright (c) 2009 OPeNDAP, Inc.
7// Author: Michael Johnson <m.johnson@opendap.org>
8//
9// For more information, please also see the main website: http://opendap.org/
10//
11// This library is free software; you can redistribute it and/or
12// modify it under the terms of the GNU Lesser General Public
13// License as published by the Free Software Foundation; either
14// version 2.1 of the License, or (at your option) any later version.
15//
16// This library is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19// Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public
22// License along with this library; if not, write to the Free Software
23// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24//
25// Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26//
27// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29#ifndef __NCML_MODULE__XML_HELPERS_H__
30#define __NCML_MODULE__XML_HELPERS_H__
31
32/*
33 * This file contains a bunch of quick and dirty classes used to pass and store XML
34 * attribute tables and namespace tables.
35 *
36 * class XMLUtil: conversions from xmlChar to string, mostly.
37 * class XMLAttribute: holds namespace-augmented into on XML attribute.
38 * class XMLAttributeMap: a container of XMLAttribute that allows lookups, etc.
39 * class XMLNamespace: holds {prefix, uri} info for a namespace
40 * class XMLNamespaceMap: a container of XMLNamespace's, typically from a single element.
41 * class XMLNamespaceStack: container which is a stack of XMLNamespaceMap's to
42 * allow a SAX lexical scoping operation to be done.
43 */
44
45#include <exception>
46#include <libxml/xmlstring.h>
47#include <string>
48#include <vector>
49
50namespace ncml_module {
51
52struct XMLUtil {
53 static std::string xmlCharToString(const xmlChar* pChars);
54 static void xmlCharToString(std::string& stringToFill, const xmlChar* pChars);
55 static std::string xmlCharToStringFromIterators(const xmlChar* startPtr, const xmlChar* endPtr);
56};
57
58struct XMLAttribute {
59 XMLAttribute(const std::string& localName = "", const std::string& value = "", const std::string& prefix = "",
60 const std::string& nsURI = "");
61
66 XMLAttribute(const xmlChar** chunkOfFivePointers);
67 XMLAttribute(const XMLAttribute& proto);
68 XMLAttribute& operator=(const XMLAttribute& rhs);
69
73 void fromSAX2NamespaceAttributes(const xmlChar** chunkOfFivePointers);
74
76 std::string getQName() const;
77
82 std::string getAsXMLString() const;
83
85 static std::string getQName(const std::string& prefix, const std::string& localname);
86
87 std::string localname;
88 std::string prefix;
89 std::string nsURI;
90 std::string value;
91};
92
93class XMLAttributeMap {
94public:
95 XMLAttributeMap();
96 ~XMLAttributeMap();
97
98 typedef std::vector<XMLAttribute>::const_iterator const_iterator;
99 typedef std::vector<XMLAttribute>::iterator iterator;
100
101 XMLAttributeMap::const_iterator begin() const;
102 XMLAttributeMap::const_iterator end() const;
103
104 bool empty() const;
105
107 void clear();
108
110 void addAttribute(const XMLAttribute& attribute);
111
113 const std::string/*& jhrg 4/16/14*/getValueForLocalNameOrDefault(const std::string& localname,
114 const std::string& defVal = "") const;
115
117 const XMLAttribute* getAttributeByLocalName(const std::string& localname) const;
118 const XMLAttribute* getAttributeByQName(const std::string& qname) const;
119 const XMLAttribute* getAttributeByQName(const std::string& prefix, const std::string& localname) const;
120
122 std::string getAllAttributesAsString() const;
123
124private:
125 // helpers
126
127 XMLAttributeMap::iterator findByQName(const std::string& qname);
128
129private:
130 // data rep
131 // We don't expect many, vector is fast to search and contiguous in memory for few items.
132 std::vector<XMLAttribute> _attributes;
133};
134
135struct XMLNamespace {
136 XMLNamespace(const std::string& prefix = "", const std::string& uri = "");
137 XMLNamespace(const XMLNamespace& proto);
138 XMLNamespace& operator=(const XMLNamespace& rhs);
139
141 void fromSAX2Namespace(const xmlChar** namespaces);
142
144 std::string getAsAttributeString() const;
145
146 std::string prefix;
147 std::string uri;
148};
149
150class XMLNamespaceMap {
151public:
152 XMLNamespaceMap();
153 ~XMLNamespaceMap();
154 XMLNamespaceMap(const XMLNamespaceMap& proto);
155 XMLNamespaceMap& operator=(const XMLNamespaceMap& rhs);
156
158 void fromSAX2Namespaces(const xmlChar** pNamespaces, int numNamespaces);
159
163 std::string getAllNamespacesAsAttributeString() const;
164
165 typedef std::vector<XMLNamespace>::const_iterator const_iterator;
166
167 XMLNamespaceMap::const_iterator begin() const;
168 XMLNamespaceMap::const_iterator end() const;
169
171 XMLNamespaceMap::const_iterator find(const std::string& prefix) const;
172
173 bool isInMap(const std::string& prefix) const;
174
176 void addNamespace(const XMLNamespace& ns);
177
178 void clear();
179 bool empty() const;
180
181private:
182 // helpers
183
184 typedef std::vector<XMLNamespace>::iterator iterator;
185
186 XMLNamespaceMap::iterator findNonConst(const std::string& prefix);
187
188private:
189 std::vector<XMLNamespace> _namespaces;
190};
191
192class XMLNamespaceStack {
193public:
194 XMLNamespaceStack();
195 ~XMLNamespaceStack();
196 XMLNamespaceStack(const XMLNamespaceStack& proto);
197 XMLNamespaceStack& operator=(const XMLNamespaceStack& rhs);
198
199 void push(const XMLNamespaceMap& nsMap);
200 void pop();
201 const XMLNamespaceMap& top() const;
202
203 bool empty() const;
204 void clear();
205
206 // Change the direction since we use the vector as a stack.
207 typedef std::vector<XMLNamespaceMap>::const_reverse_iterator const_iterator;
208
210 XMLNamespaceStack::const_iterator begin() const;
211 XMLNamespaceStack::const_iterator end() const;
212
226
227private:
228 // helpers
229
231 static void addMissingNamespaces(XMLNamespaceMap& intoMap, const XMLNamespaceMap& fromMap);
232
233private:
234 // data rep
235 // Vector for easy scanning.
236 std::vector<XMLNamespaceMap> _stack;
237};
238
239} // namespace ncml_module
240#endif // __NCML_MODULE__XML_HELPERS_H__
const std::string getValueForLocalNameOrDefault(const std::string &localname, const std::string &defVal="") const
const XMLAttribute * getAttributeByLocalName(const std::string &localname) const
void addAttribute(const XMLAttribute &attribute)
std::string getAllAttributesAsString() const
void fromSAX2Namespaces(const xmlChar **pNamespaces, int numNamespaces)
std::string getAllNamespacesAsAttributeString() const
void addNamespace(const XMLNamespace &ns)
XMLNamespaceMap::const_iterator find(const std::string &prefix) const
void getFlattenedNamespacesUsingLexicalScoping(XMLNamespaceMap &nsFlattened) const
XMLNamespaceStack::const_iterator begin() const
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
std::string getAsXMLString() const
std::string getQName() const
void fromSAX2NamespaceAttributes(const xmlChar **chunkOfFivePointers)
Definition XMLHelpers.cc:94
std::string getAsAttributeString() const
void fromSAX2Namespace(const xmlChar **namespaces)