/usr/include/liborcus-0.8/orcus/sax_ns_parser.hpp is in liborcus-dev 0.7.0+dfsg-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef __ORCUS_SAX_NS_PARSER_HPP__
#define __ORCUS_SAX_NS_PARSER_HPP__
#include "sax_parser.hpp"
#include "xml_namespace.hpp"
#include <boost/unordered_set.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
namespace orcus {
struct sax_ns_parser_element
{
xmlns_id_t ns; // element namespace
pstring ns_alias; // element namespace alias
pstring name; // element name
const char* begin_pos; // position of the opening brace '<'.
const char* end_pos; // position of the char after the closing brace '>'.
};
struct sax_ns_parser_attribute
{
xmlns_id_t ns; // attribute namespace
pstring ns_alias; // attribute namespace alias
pstring name; // attribute name
pstring value; // attribute value
bool transient; // whether or not the attribute value is transient.
};
namespace __sax {
struct entity_name
{
pstring ns;
pstring name;
entity_name(const pstring& _ns, const pstring& _name) :
ns(_ns), name(_name) {}
bool operator== (const entity_name& other) const
{
return other.ns == ns && other.name == name;
}
struct hash
{
size_t operator() (const entity_name& v) const
{
static pstring::hash hasher;
return hasher(v.ns) + hasher(v.name);
}
};
};
typedef boost::unordered_set<pstring, pstring::hash> ns_keys_type;
typedef boost::unordered_set<entity_name, entity_name::hash> entity_names_type;
struct elem_scope
{
xmlns_id_t ns;
pstring name;
ns_keys_type ns_keys;
};
typedef boost::ptr_vector<elem_scope> elem_scopes_type;
class pop_ns_by_key : std::unary_function<pstring, void>
{
xmlns_context& m_cxt;
public:
pop_ns_by_key(xmlns_context& cxt) : m_cxt(cxt) {}
void operator() (const pstring& key)
{
m_cxt.pop(key);
}
};
}
/**
* SAX based XML parser with proper namespace handling.
*/
template<typename _Handler>
class sax_ns_parser
{
public:
typedef _Handler handler_type;
sax_ns_parser(const char* content, const size_t size, xmlns_context& ns_cxt, handler_type& handler);
~sax_ns_parser();
void parse();
private:
/**
* Re-route callbacks from the internal sax_parser into sax_ns_parser
* callbacks.
*/
class handler_wrapper
{
__sax::elem_scopes_type m_scopes;
__sax::ns_keys_type m_ns_keys;
__sax::entity_names_type m_attrs;
sax_ns_parser_element m_elem;
sax_ns_parser_attribute m_attr;
xmlns_context& m_ns_cxt;
handler_type& m_handler;
bool m_declaration;
public:
handler_wrapper(xmlns_context& ns_cxt, handler_type& handler) : m_ns_cxt(ns_cxt), m_handler(handler), m_declaration(false) {}
void doctype(const sax::doctype_declaration& dtd)
{
m_handler.doctype(dtd);
}
void start_declaration(const pstring& name)
{
m_declaration = true;
m_handler.start_declaration(name);
}
void end_declaration(const pstring& name)
{
m_declaration = false;
m_handler.end_declaration(name);
}
void start_element(const sax::parser_element& elem)
{
m_scopes.push_back(new __sax::elem_scope);
__sax::elem_scope& scope = m_scopes.back();
scope.ns = m_ns_cxt.get(elem.ns);
scope.name = elem.name;
scope.ns_keys.swap(m_ns_keys);
m_elem.ns = scope.ns;
m_elem.ns_alias = elem.ns;
m_elem.name = scope.name;
m_elem.begin_pos = elem.begin_pos;
m_elem.end_pos = elem.end_pos;
m_handler.start_element(m_elem);
m_attrs.clear();
}
void end_element(const sax::parser_element& elem)
{
__sax::elem_scope& scope = m_scopes.back();
if (scope.ns != m_ns_cxt.get(elem.ns) || scope.name != elem.name)
throw sax::malformed_xml_error("mis-matching closing element.");
m_elem.ns = scope.ns;
m_elem.ns_alias = elem.ns;
m_elem.name = scope.name;
m_elem.begin_pos = elem.begin_pos;
m_elem.end_pos = elem.end_pos;
m_handler.end_element(m_elem);
// Pop all namespaces declared in this scope.
std::for_each(scope.ns_keys.begin(), scope.ns_keys.end(), __sax::pop_ns_by_key(m_ns_cxt));
m_scopes.pop_back();
}
void characters(const pstring& val, bool transient)
{
m_handler.characters(val, transient);
}
void attribute(const sax::parser_attribute& attr)
{
if (m_declaration)
{
// XML declaration attribute. Pass it through to the handler without namespace.
m_handler.attribute(attr.name, attr.value);
return;
}
if (m_attrs.count(__sax::entity_name(attr.ns, attr.name)) > 0)
throw sax::malformed_xml_error("You can't define two attributes of the same name in the same element.");
m_attrs.insert(__sax::entity_name(attr.ns, attr.name));
if (attr.ns.empty() && attr.name == "xmlns")
{
// Default namespace
m_ns_cxt.push(pstring(), attr.value);
m_ns_keys.insert(pstring());
return;
}
if (attr.ns == "xmlns")
{
// Namespace alias
if (!attr.name.empty())
{
m_ns_cxt.push(attr.name, attr.value);
m_ns_keys.insert(attr.name);
}
return;
}
m_attr.ns = m_ns_cxt.get(attr.ns);
m_attr.ns_alias = attr.ns;
m_attr.name = attr.name;
m_attr.value = attr.value;
m_attr.transient = attr.transient;
m_handler.attribute(m_attr);
}
};
private:
handler_wrapper m_wrapper;
sax_parser<handler_wrapper> m_parser;
};
template<typename _Handler>
sax_ns_parser<_Handler>::sax_ns_parser(
const char* content, const size_t size, xmlns_context& ns_cxt, handler_type& handler) :
m_wrapper(ns_cxt, handler), m_parser(content, size, m_wrapper)
{
}
template<typename _Handler>
sax_ns_parser<_Handler>::~sax_ns_parser()
{
}
template<typename _Handler>
void sax_ns_parser<_Handler>::parse()
{
m_parser.parse();
}
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|