bes  Updated for version 3.20.6
OtherXMLParser.cc
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 #include "NCMLDebug.h"
30 #include "NCMLParser.h"
31 #include "OtherXMLParser.h"
32 #include "XMLHelpers.h"
33 
34 namespace ncml_module {
35 
36 OtherXMLParser::OtherXMLParser(NCMLParser& p) :
37  _rParser(p), _depth(0), _otherXML("")
38 {
39 }
40 
41 OtherXMLParser::~OtherXMLParser()
42 {
43  reset(); // for consistency
44 }
45 
46 int OtherXMLParser::getParseDepth() const
47 {
48  return _depth;
49 }
50 
51 const std::string&
52 OtherXMLParser::getString() const
53 {
54  return _otherXML;
55 }
56 
57 void OtherXMLParser::reset()
58 {
59  _depth = 0;
60  _otherXML = "";
61 }
62 
63 void OtherXMLParser::onStartDocument()
64 {
65  THROW_NCML_INTERNAL_ERROR("OtherXMLParser::onStartDocument called! This is a logic bug.");
66 }
67 
68 void OtherXMLParser::onEndDocument()
69 {
70  THROW_NCML_INTERNAL_ERROR("OtherXMLParser::onEndDocument called! This is a logic bug.");
71 }
72 
73 void OtherXMLParser::onStartElement(const std::string& name, const XMLAttributeMap& attrs)
74 {
75  appendOpenStartElementTag(name, "");
76  appendAttributes(attrs);
77  // no namespaces for this set of calls... presumably in the attributes
78  appendCloseStartElementTag();
79 
80  pushDepth();
81 }
82 
83 void OtherXMLParser::onEndElement(const std::string& name)
84 {
85  appendEndElementTag(name);
86  popDepth();
87 }
88 
89 void OtherXMLParser::onStartElementWithNamespace(const std::string& localname, const std::string& prefix,
90  const std::string& /* uri */, const XMLAttributeMap& attributes, const XMLNamespaceMap& namespaces)
91 {
92  appendOpenStartElementTag(localname, prefix);
93  appendAttributes(attributes);
94 
95  // if a root element, grab ALL the namespaces from the parents since we will be
96  // disconnected from them and will lose the namespaces if not
97  if (_depth == 0) {
98  BESDEBUG("ncml",
99  "Got depth 0 OtherXML element while parsing OtherXML attribute..." << " Pulling all un-shadowed ancestral namespaces into the element with localname=" << localname << std::endl);
100 
101  // initialize it with the local node namespaces, since they
102  // take precedence over the stack
103  XMLNamespaceMap ancestralNamespaces(namespaces);
104  // then fill in the rest from the current stack
105  _rParser.getXMLNamespaceStack().getFlattenedNamespacesUsingLexicalScoping(ancestralNamespaces);
106  appendNamespaces(ancestralNamespaces);
107  }
108  else // Append the ones local to JUST this element if not root
109  {
110  appendNamespaces(namespaces);
111  }
112 
113  appendCloseStartElementTag();
114 
115  pushDepth();
116 }
117 
118 void OtherXMLParser::onEndElementWithNamespace(const std::string& localname, const std::string& prefix,
119  const std::string& /*uri*/)
120 {
121  appendEndElementTag(XMLAttribute::getQName(prefix, localname));
122  popDepth();
123 }
124 
125 void OtherXMLParser::onCharacters(const std::string& content)
126 {
127  // Really just shove them on there, whitespace and all to maintain formatting I guess. Does this do that?
128  _otherXML.append(content);
129 }
130 
131 void OtherXMLParser::onParseWarning(std::string msg)
132 {
133  THROW_NCML_PARSE_ERROR(-1, // the SAX errors have the line in there already
134  "OtherXMLParser: got SAX parse warning while parsing OtherXML. Msg was: " + msg);
135 }
136 
137 void OtherXMLParser::onParseError(std::string msg)
138 {
139  THROW_NCML_PARSE_ERROR(-1, "OtherXMLParser: got SAX parse error while parsing OtherXML. Msg was: " + msg);
140 }
141 
142 void OtherXMLParser::appendOpenStartElementTag(const std::string& localname, const std::string& prefix)
143 {
144  // Append this element and all its attributes onto the string
145  _otherXML += string("<");
146  _otherXML += XMLAttribute::getQName(prefix, localname);
147 }
148 
149 void OtherXMLParser::appendAttributes(const XMLAttributeMap& attributes)
150 {
151  for (XMLAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it) {
152  _otherXML += (string(" ") + it->getQName() + "=\"" + it->value + "\"");
153  }
154 }
155 
156 void OtherXMLParser::appendNamespaces(const XMLNamespaceMap& namespaces)
157 {
158  _otherXML += namespaces.getAllNamespacesAsAttributeString();
159 }
160 
161 void OtherXMLParser::appendCloseStartElementTag()
162 {
163  _otherXML += ">";
164 }
165 
166 void OtherXMLParser::appendEndElementTag(const string& qname)
167 {
168  _otherXML += (string("</") + qname + ">");
169 }
170 
171 void OtherXMLParser::pushDepth()
172 {
173  ++_depth;
174 }
175 
176 void OtherXMLParser::popDepth()
177 {
178  --_depth;
179 
180  // Check for underflow
181  if (_depth < 0) {
182  // I am not sure this is internal or user can cause it... making it internal for now...
183  THROW_NCML_INTERNAL_ERROR("OtherXMLElement::onEndElement: _depth < 0! Logic error in parsing OtherXML.");
184  }
185 }
186 
187 } // namespace ncml_module
ncml_module::XMLAttributeMap
Definition: XMLHelpers.h:93
ncml_module
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
Definition: AggregationElement.cc:72
ncml_module::XMLNamespaceMap
Definition: XMLHelpers.h:150