bes  Updated for version 3.20.5
XMLHelpers.h
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 using std::string;
53 using std::vector;
54 
55 struct XMLUtil {
56  static string xmlCharToString(const xmlChar* pChars);
57  static void xmlCharToString(string& stringToFill, const xmlChar* pChars);
58  static string xmlCharToStringFromIterators(const xmlChar* startPtr, const xmlChar* endPtr);
59 };
60 
61 struct XMLAttribute {
62  XMLAttribute(const string& localName = "", const string& value = "", const string& prefix = "",
63  const string& nsURI = "");
64 
69  XMLAttribute(const xmlChar** chunkOfFivePointers);
70  XMLAttribute(const XMLAttribute& proto);
71  XMLAttribute& operator=(const XMLAttribute& rhs);
72 
76  void fromSAX2NamespaceAttributes(const xmlChar** chunkOfFivePointers);
77 
79  string getQName() const;
80 
85  string getAsXMLString() const;
86 
88  static string getQName(const string& prefix, const string& localname);
89 
90  string localname;
91  string prefix;
92  string nsURI;
93  string value;
94 };
95 
97 public:
99  ~XMLAttributeMap();
100 
101  typedef vector<XMLAttribute>::const_iterator const_iterator;
102  typedef vector<XMLAttribute>::iterator iterator;
103 
104  XMLAttributeMap::const_iterator begin() const;
105  XMLAttributeMap::const_iterator end() const;
106 
107  bool empty() const;
108 
110  void clear();
111 
113  void addAttribute(const XMLAttribute& attribute);
114 
116  const string/*& jhrg 4/16/14*/getValueForLocalNameOrDefault(const string& localname,
117  const string& defVal = "") const;
118 
120  const XMLAttribute* getAttributeByLocalName(const string& localname) const;
121  const XMLAttribute* getAttributeByQName(const string& qname) const;
122  const XMLAttribute* getAttributeByQName(const string& prefix, const string& localname) const;
123 
125  string getAllAttributesAsString() const;
126 
127 private:
128  // helpers
129 
130  XMLAttributeMap::iterator findByQName(const string& qname);
131 
132 private:
133  // data rep
134  // We don't expect many, vector is fast to search and contiguous in memory for few items.
135  vector<XMLAttribute> _attributes;
136 };
137 
138 struct XMLNamespace {
139  XMLNamespace(const string& prefix = "", const string& uri = "");
140  XMLNamespace(const XMLNamespace& proto);
141  XMLNamespace& operator=(const XMLNamespace& rhs);
142 
144  void fromSAX2Namespace(const xmlChar** namespaces);
145 
147  string getAsAttributeString() const;
148 
149  string prefix;
150  string uri;
151 };
152 
154 public:
155  XMLNamespaceMap();
156  ~XMLNamespaceMap();
157  XMLNamespaceMap(const XMLNamespaceMap& proto);
158  XMLNamespaceMap& operator=(const XMLNamespaceMap& rhs);
159 
161  void fromSAX2Namespaces(const xmlChar** pNamespaces, int numNamespaces);
162 
166  string getAllNamespacesAsAttributeString() const;
167 
168  typedef vector<XMLNamespace>::const_iterator const_iterator;
169 
170  XMLNamespaceMap::const_iterator begin() const;
171  XMLNamespaceMap::const_iterator end() const;
172 
174  XMLNamespaceMap::const_iterator find(const string& prefix) const;
175 
176  bool isInMap(const string& prefix) const;
177 
179  void addNamespace(const XMLNamespace& ns);
180 
181  void clear();
182  bool empty() const;
183 
184 private:
185  // helpers
186 
187  typedef vector<XMLNamespace>::iterator iterator;
188 
189  XMLNamespaceMap::iterator findNonConst(const string& prefix);
190 
191 private:
192  vector<XMLNamespace> _namespaces;
193 };
194 
196 public:
199  XMLNamespaceStack(const XMLNamespaceStack& proto);
200  XMLNamespaceStack& operator=(const XMLNamespaceStack& rhs);
201 
202  void push(const XMLNamespaceMap& nsMap);
203  void pop();
204  const XMLNamespaceMap& top() const;
205 
206  bool empty() const;
207  void clear();
208 
209  // Change the direction since we use the vector as a stack.
210  typedef vector<XMLNamespaceMap>::const_reverse_iterator const_iterator;
211 
213  XMLNamespaceStack::const_iterator begin() const;
214  XMLNamespaceStack::const_iterator end() const;
215 
229 
230 private:
231  // helpers
232 
234  static void addMissingNamespaces(XMLNamespaceMap& intoMap, const XMLNamespaceMap& fromMap);
235 
236 private:
237  // data rep
238  // Vector for easy scanning.
239  vector<XMLNamespaceMap> _stack;
240 };
241 
242 } // namespace ncml_module
243 #endif // __NCML_MODULE__XML_HELPERS_H__
ncml_module::XMLAttributeMap::getAllAttributesAsString
string getAllAttributesAsString() const
Definition: XMLHelpers.cc:226
ncml_module::XMLNamespace
Definition: XMLHelpers.h:138
ncml_module::XMLAttribute::fromSAX2NamespaceAttributes
void fromSAX2NamespaceAttributes(const xmlChar **chunkOfFivePointers)
Definition: XMLHelpers.cc:92
ncml_module::XMLNamespaceMap::fromSAX2Namespaces
void fromSAX2Namespaces(const xmlChar **pNamespaces, int numNamespaces)
Definition: XMLHelpers.cc:318
ncml_module::XMLNamespaceMap::getAllNamespacesAsAttributeString
string getAllNamespacesAsAttributeString() const
Definition: XMLHelpers.cc:329
ncml_module::XMLNamespaceStack
Definition: XMLHelpers.h:195
ncml_module::XMLNamespaceMap::addNamespace
void addNamespace(const XMLNamespace &ns)
Definition: XMLHelpers.cc:365
ncml_module::XMLNamespaceMap::find
XMLNamespaceMap::const_iterator find(const string &prefix) const
Definition: XMLHelpers.cc:349
ncml_module::XMLAttributeMap
Definition: XMLHelpers.h:96
ncml_module::XMLAttributeMap::addAttribute
void addAttribute(const XMLAttribute &attribute)
Definition: XMLHelpers.cc:165
ncml_module
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
Definition: AggregationElement.cc:72
ncml_module::XMLAttribute
Definition: XMLHelpers.h:61
ncml_module::XMLAttribute::getQName
string getQName() const
Definition: XMLHelpers.cc:109
ncml_module::XMLNamespace::getAsAttributeString
string getAsAttributeString() const
Definition: XMLHelpers.cc:279
ncml_module::XMLUtil
Definition: XMLHelpers.h:55
ncml_module::XMLNamespaceMap
Definition: XMLHelpers.h:153
ncml_module::XMLAttribute::getAsXMLString
string getAsXMLString() const
Definition: XMLHelpers.cc:118
ncml_module::XMLAttributeMap::getValueForLocalNameOrDefault
const string getValueForLocalNameOrDefault(const string &localname, const string &defVal="") const
Definition: XMLHelpers.cc:179
ncml_module::XMLNamespace::fromSAX2Namespace
void fromSAX2Namespace(const xmlChar **namespaces)
Definition: XMLHelpers.cc:272
ncml_module::XMLNamespaceStack::getFlattenedNamespacesUsingLexicalScoping
void getFlattenedNamespacesUsingLexicalScoping(XMLNamespaceMap &nsFlattened) const
Definition: XMLHelpers.cc:463
ncml_module::XMLAttributeMap::clear
void clear()
Definition: XMLHelpers.cc:159
ncml_module::XMLNamespaceStack::begin
XMLNamespaceStack::const_iterator begin() const
Definition: XMLHelpers.cc:453
ncml_module::XMLAttributeMap::getAttributeByLocalName
const XMLAttribute * getAttributeByLocalName(const string &localname) const
Definition: XMLHelpers.cc:192