bes  Updated for version 3.20.6
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 struct 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 
58 struct 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 
94 public:
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 
124 private:
125  // helpers
126 
127  XMLAttributeMap::iterator findByQName(const std::string& qname);
128 
129 private:
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 
135 struct 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 
151 public:
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 
181 private:
182  // helpers
183 
184  typedef std::vector<XMLNamespace>::iterator iterator;
185 
186  XMLNamespaceMap::iterator findNonConst(const std::string& prefix);
187 
188 private:
189  std::vector<XMLNamespace> _namespaces;
190 };
191 
193 public:
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 
227 private:
228  // helpers
229 
231  static void addMissingNamespaces(XMLNamespaceMap& intoMap, const XMLNamespaceMap& fromMap);
232 
233 private:
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__
ncml_module::XMLAttributeMap::getAllAttributesAsString
std::string getAllAttributesAsString() const
Definition: XMLHelpers.cc:228
ncml_module::XMLNamespace
Definition: XMLHelpers.h:135
ncml_module::XMLAttribute::fromSAX2NamespaceAttributes
void fromSAX2NamespaceAttributes(const xmlChar **chunkOfFivePointers)
Definition: XMLHelpers.cc:94
ncml_module::XMLNamespaceMap::fromSAX2Namespaces
void fromSAX2Namespaces(const xmlChar **pNamespaces, int numNamespaces)
Definition: XMLHelpers.cc:320
ncml_module::XMLNamespaceMap::getAllNamespacesAsAttributeString
std::string getAllNamespacesAsAttributeString() const
Definition: XMLHelpers.cc:331
ncml_module::XMLNamespaceStack
Definition: XMLHelpers.h:192
ncml_module::XMLNamespaceMap::addNamespace
void addNamespace(const XMLNamespace &ns)
Definition: XMLHelpers.cc:367
ncml_module::XMLAttributeMap
Definition: XMLHelpers.h:93
ncml_module::XMLAttributeMap::getAttributeByLocalName
const XMLAttribute * getAttributeByLocalName(const std::string &localname) const
Definition: XMLHelpers.cc:194
ncml_module::XMLAttributeMap::addAttribute
void addAttribute(const XMLAttribute &attribute)
Definition: XMLHelpers.cc:167
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:58
ncml_module::XMLAttribute::getQName
std::string getQName() const
Definition: XMLHelpers.cc:111
ncml_module::XMLNamespace::getAsAttributeString
std::string getAsAttributeString() const
Definition: XMLHelpers.cc:281
ncml_module::XMLUtil
Definition: XMLHelpers.h:52
ncml_module::XMLAttributeMap::getValueForLocalNameOrDefault
const std::string getValueForLocalNameOrDefault(const std::string &localname, const std::string &defVal="") const
Definition: XMLHelpers.cc:181
ncml_module::XMLNamespaceMap
Definition: XMLHelpers.h:150
ncml_module::XMLAttribute::getAsXMLString
std::string getAsXMLString() const
Definition: XMLHelpers.cc:120
ncml_module::XMLNamespaceMap::find
XMLNamespaceMap::const_iterator find(const std::string &prefix) const
Definition: XMLHelpers.cc:351
ncml_module::XMLNamespace::fromSAX2Namespace
void fromSAX2Namespace(const xmlChar **namespaces)
Definition: XMLHelpers.cc:274
ncml_module::XMLNamespaceStack::getFlattenedNamespacesUsingLexicalScoping
void getFlattenedNamespacesUsingLexicalScoping(XMLNamespaceMap &nsFlattened) const
Definition: XMLHelpers.cc:465
ncml_module::XMLAttributeMap::clear
void clear()
Definition: XMLHelpers.cc:161
ncml_module::XMLNamespaceStack::begin
XMLNamespaceStack::const_iterator begin() const
Definition: XMLHelpers.cc:455