OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
XMLHelpers.h
Go to the documentation of this file.
1 // 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 
50 namespace ncml_module
51 {
52 
53 using std::string;
54 using std::vector;
55 
56 struct XMLUtil
57 {
58  static string xmlCharToString(const xmlChar* pChars);
59  static void xmlCharToString(string& stringToFill, const xmlChar* pChars);
60  static string xmlCharToStringFromIterators(const xmlChar* startPtr, const xmlChar* endPtr);
61 };
62 
64 {
65  XMLAttribute(const string& localName="", const string& value="", const string& prefix="", const string& nsURI="");
66 
71  XMLAttribute(const xmlChar** chunkOfFivePointers);
72  XMLAttribute(const XMLAttribute& proto);
73  XMLAttribute& operator=(const XMLAttribute& rhs);
74 
78  void fromSAX2NamespaceAttributes(const xmlChar** chunkOfFivePointers);
79 
81  string getQName() const;
82 
87  string getAsXMLString() const;
88 
90  static string getQName(const string& prefix, const string& localname);
91 
92  string localname;
93  string prefix;
94  string nsURI;
95  string value;
96 };
97 
99 {
100 public:
101  XMLAttributeMap();
103 
104  typedef vector<XMLAttribute>::const_iterator const_iterator;
105  typedef vector<XMLAttribute>::iterator iterator;
106 
109 
110  bool empty() const;
111 
113  void clear();
114 
116  void addAttribute(const XMLAttribute& attribute);
117 
119  const string/*& jhrg 4/16/14*/getValueForLocalNameOrDefault(const string& localname, const string& defVal="") const;
120 
121 
123  const XMLAttribute* getAttributeByLocalName(const string& localname) const;
124  const XMLAttribute* getAttributeByQName(const string& qname) const;
125  const XMLAttribute* getAttributeByQName(const string& prefix, const string& localname) const;
126 
128  string getAllAttributesAsString() const;
129 
130 private: // helpers
131 
132  XMLAttributeMap::iterator findByQName(const string& qname);
133 
134 private: // data rep
135  // We don't expect many, vector is fast to search and contiguous in memory for few items.
136  vector<XMLAttribute> _attributes;
137 };
138 
140 {
141  XMLNamespace(const string& prefix="", const string& uri="");
142  XMLNamespace(const XMLNamespace& proto);
143  XMLNamespace& operator=(const XMLNamespace& rhs);
144 
146  void fromSAX2Namespace(const xmlChar** namespaces);
147 
149  string getAsAttributeString() const;
150 
151  string prefix;
152  string uri;
153 };
154 
156 {
157 public:
158  XMLNamespaceMap();
160  XMLNamespaceMap(const XMLNamespaceMap& proto);
162 
164  void fromSAX2Namespaces(const xmlChar** pNamespaces, int numNamespaces);
165 
169  string getAllNamespacesAsAttributeString() const;
170 
171  typedef vector<XMLNamespace>::const_iterator const_iterator;
172 
175 
177  XMLNamespaceMap::const_iterator find(const string& prefix) const;
178 
179  bool isInMap(const string& prefix) const;
180 
182  void addNamespace(const XMLNamespace& ns);
183 
184  void clear();
185  bool empty() const;
186 
187 private: // helpers
188 
189  typedef vector<XMLNamespace>::iterator iterator;
190 
191  XMLNamespaceMap::iterator findNonConst(const string& prefix);
192 
193 private:
194  vector<XMLNamespace> _namespaces;
195 };
196 
198 {
199 public:
202  XMLNamespaceStack(const XMLNamespaceStack& proto);
204 
205  void push(const XMLNamespaceMap& nsMap);
206  void pop();
207  const XMLNamespaceMap& top() const;
208 
209  bool empty() const;
210  void clear();
211 
212  // Change the direction since we use the vector as a stack.
213  typedef vector<XMLNamespaceMap>::const_reverse_iterator const_iterator;
214 
218 
232 
233 private: // helpers
234 
236  static void addMissingNamespaces(XMLNamespaceMap& intoMap, const XMLNamespaceMap& fromMap);
237 
238 private: // data rep
239  // Vector for easy scanning.
240  vector<XMLNamespaceMap> _stack;
241 };
242 
243 } // namespace ncml_module
244 #endif // __NCML_MODULE__XML_HELPERS_H__
void fromSAX2Namespace(const xmlChar **namespaces)
Assuming the pointer is an array of two strings: {prefix, uri}.
Definition: XMLHelpers.cc:319
XMLNamespaceMap::const_iterator find(const string &prefix) const
Return the iterator to the element or end() if not found.
Definition: XMLHelpers.cc:406
XMLNamespaceMap::const_iterator end() const
Definition: XMLHelpers.cc:400
vector< XMLNamespaceMap >::const_reverse_iterator const_iterator
Definition: XMLHelpers.h:213
XMLAttributeMap::const_iterator begin() const
Definition: XMLHelpers.cc:169
vector< XMLAttribute >::iterator iterator
Definition: XMLHelpers.h:105
XMLNamespace & operator=(const XMLNamespace &rhs)
Definition: XMLHelpers.cc:306
bool isInMap(const string &prefix) const
Definition: XMLHelpers.cc:420
XMLNamespaceStack & operator=(const XMLNamespaceStack &rhs)
Definition: XMLHelpers.cc:486
An abstract superclass for NCMLArray that handles the non-parameterized functionality and allows u...
const XMLNamespaceMap & top() const
Definition: XMLHelpers.cc:509
static string xmlCharToStringFromIterators(const xmlChar *startPtr, const xmlChar *endPtr)
Definition: XMLHelpers.cc:53
string getAsXMLString() const
Get the standard string version as found in an element: prefix:localname="value" localname="value" if...
Definition: XMLHelpers.cc:139
const string getValueForLocalNameOrDefault(const string &localname, const string &defVal="") const
If there is an attribute with localname, return its value, else return default.
Definition: XMLHelpers.cc:209
const XMLAttribute * getAttributeByLocalName(const string &localname) const
These return null if the attribute was not found.
Definition: XMLHelpers.cc:224
XMLNamespace(const string &prefix="", const string &uri="")
Definition: XMLHelpers.cc:293
void fromSAX2Namespaces(const xmlChar **pNamespaces, int numNamespaces)
Read them all in from the xmlChar array.
Definition: XMLHelpers.cc:369
XMLAttribute & operator=(const XMLAttribute &rhs)
Definition: XMLHelpers.cc:97
XMLNamespaceMap::const_iterator begin() const
Definition: XMLHelpers.cc:394
string getAllNamespacesAsAttributeString() const
Get a big string full of xmlns:prefix="uri" attributes, separated by spaces.
Definition: XMLHelpers.cc:382
void addAttribute(const XMLAttribute &attribute)
TODO how do we tell if this exists? Does it replace? Do we care?
Definition: XMLHelpers.cc:194
void getFlattenedNamespacesUsingLexicalScoping(XMLNamespaceMap &nsFlattened) const
Scanning from the stack top downwards, add the first found new XMLNamespace (in terms of its prefix) ...
Definition: XMLHelpers.cc:539
string getAsAttributeString() const
Get the namespace as attribute string, ie "xmlns:prefix=\"uri"" for serializing.
Definition: XMLHelpers.cc:327
XMLAttribute(const string &localName="", const string &value="", const string &prefix="", const string &nsURI="")
Definition: XMLHelpers.cc:69
static string xmlCharToString(const xmlChar *pChars)
Definition: XMLHelpers.cc:38
XMLAttributeMap::const_iterator end() const
Definition: XMLHelpers.cc:175
XMLNamespaceStack::const_iterator begin() const
Starts from the top (most recently pushed) and iterates to the bottom (first pushed).
Definition: XMLHelpers.cc:527
void clear()
make empty
Definition: XMLHelpers.cc:187
vector< XMLAttribute >::const_iterator const_iterator
Definition: XMLHelpers.h:104
string getAllAttributesAsString() const
The classic {prefix:}foo="value" whitespace separated.
Definition: XMLHelpers.cc:263
const XMLAttribute * getAttributeByQName(const string &qname) const
Definition: XMLHelpers.cc:240
void push(const XMLNamespaceMap &nsMap)
Definition: XMLHelpers.cc:497
void addNamespace(const XMLNamespace &ns)
If the given prefix is already in the map, ns REPLACES it.
Definition: XMLHelpers.cc:426
XMLNamespaceStack::const_iterator end() const
Definition: XMLHelpers.cc:533
string getQName() const
get the name with the prefix:localname if prefix not empty else localname
Definition: XMLHelpers.cc:129
XMLNamespaceMap & operator=(const XMLNamespaceMap &rhs)
Definition: XMLHelpers.cc:358
vector< XMLNamespace >::const_iterator const_iterator
Definition: XMLHelpers.h:171
void fromSAX2NamespaceAttributes(const xmlChar **chunkOfFivePointers)
Fill in the fields from the SAX2 namespace attributes array.
Definition: XMLHelpers.cc:111