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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
// Copyright Maarten L. Hekkelman, Radboud University 2010-2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef ZEEP_XML_DOCTYPE_HPP
#define ZEEP_XML_DOCTYPE_HPP
#include <zeep/config.hpp>
#include <set>
#include <vector>
#include <list>
#include <boost/tuple/tuple.hpp>
#include <boost/filesystem/operations.hpp>
namespace zeep { namespace xml { namespace doctype {
// --------------------------------------------------------------------
// doctype support with full validation.
class element;
class attlist;
class entity;
class attribute;
typedef std::vector<entity*> entity_list;
typedef std::vector<element*> element_list;
typedef std::vector<attribute*> attribute_list;
// --------------------------------------------------------------------
// validation of elements is done by the validator classes
struct allowed_base;
typedef allowed_base* allowed_ptr;
typedef std::list<allowed_ptr> allowed_list;
struct state_base;
typedef state_base* state_ptr;
class validator
{
public:
validator();
validator(allowed_ptr allowed);
validator(const validator& other);
validator& operator=(const validator& other);
~validator();
void reset();
bool allow(const std::string& name);
bool allow_char_data();
bool done();
bool operator()(const std::string& name) { return allow(name); }
private:
friend std::ostream& operator<<(std::ostream& lhs, validator& rhs);
state_ptr m_state;
allowed_ptr m_allowed;
int m_nr;
static int s_next_nr;
bool m_done;
};
std::ostream& operator<<(std::ostream& lhs, validator& rhs);
struct allowed_base
{
allowed_base() {}
virtual ~allowed_base() {}
virtual state_ptr create_state() const = 0;
virtual bool element_content() const { return false; }
virtual void print(std::ostream& os) = 0;
};
struct allowed_any : public allowed_base
{
virtual state_ptr create_state() const;
virtual void print(std::ostream& os);
};
struct allowed_empty : public allowed_base
{
virtual state_ptr create_state() const;
virtual void print(std::ostream& os);
};
struct allowed_element : public allowed_base
{
allowed_element(const std::string& name)
: m_name(name) {}
virtual state_ptr create_state() const;
virtual bool element_content() const { return true; }
virtual void print(std::ostream& os);
std::string m_name;
};
struct allowed_repeated : public allowed_base
{
allowed_repeated(allowed_ptr allowed, char repetion)
: m_allowed(allowed), m_repetition(repetion)
{
assert(allowed);
}
~allowed_repeated();
virtual state_ptr create_state() const;
virtual bool element_content() const;
virtual void print(std::ostream& os);
allowed_ptr m_allowed;
char m_repetition;
};
struct allowed_seq : public allowed_base
{
allowed_seq(allowed_ptr a) { add(a); }
~allowed_seq();
void add(allowed_ptr a);
virtual state_ptr create_state() const;
virtual bool element_content() const;
virtual void print(std::ostream& os);
allowed_list m_allowed;
};
struct allowed_choice : public allowed_base
{
allowed_choice(bool mixed)
: m_mixed(mixed) {}
allowed_choice(allowed_ptr a, bool mixed)
: m_mixed(mixed) { add(a); }
~allowed_choice();
void add(allowed_ptr a);
virtual state_ptr create_state() const;
virtual bool element_content() const;
virtual void print(std::ostream& os);
allowed_list m_allowed;
bool m_mixed;
};
// --------------------------------------------------------------------
enum AttributeType
{
attTypeString,
attTypeTokenizedID,
attTypeTokenizedIDREF,
attTypeTokenizedIDREFS,
attTypeTokenizedENTITY,
attTypeTokenizedENTITIES,
attTypeTokenizedNMTOKEN,
attTypeTokenizedNMTOKENS,
attTypeNotation,
attTypeEnumerated
};
enum AttributeDefault
{
attDefNone,
attDefRequired,
attDefImplied,
attDefFixed,
attDefDefault
};
class attribute
{
public:
attribute(const std::string& name, AttributeType type)
: m_name(name), m_type(type), m_default(attDefNone), m_external(false) {}
attribute(const std::string& name, AttributeType type,
const std::vector<std::string>& enums)
: m_name(name), m_type(type), m_default(attDefNone)
, m_enum(enums), m_external(false) {}
const std::string& name() const { return m_name; }
bool validate_value(std::string& value, const entity_list& entities) const;
void set_default(AttributeDefault def, const std::string& value)
{
m_default = def;
m_default_value = value;
}
boost::tuple<AttributeDefault,std::string>
get_default() const { return boost::make_tuple(m_default, m_default_value); }
AttributeType get_type() const { return m_type; }
AttributeDefault get_default_type() const { return m_default; }
const std::vector<std::string>&
get_enums() const { return m_enum; }
void external(bool external) { m_external = external; }
bool external() const { return m_external; }
private:
// routines used to check _and_ reformat attribute value strings
bool is_name(std::string& s) const;
bool is_names(std::string& s) const;
bool is_nmtoken(std::string& s) const;
bool is_nmtokens(std::string& s) const;
bool is_unparsed_entity(const std::string& s, const entity_list& l) const;
std::string m_name;
AttributeType m_type;
AttributeDefault m_default;
std::string m_default_value;
std::vector<std::string>
m_enum;
bool m_external;
};
// --------------------------------------------------------------------
class element : boost::noncopyable
{
public:
element(const std::string& name, bool declared, bool external)
: m_name(name), m_allowed(nullptr), m_declared(declared), m_external(external) {}
~element();
void add_attribute(attribute* attr);
const attribute* get_attribute(const std::string& name) const;
const std::string& name() const { return m_name; }
const attribute_list&
attributes() const { return m_attlist; }
void set_allowed(allowed_ptr allowed);
void declared(bool declared) { m_declared = declared; }
bool declared() const { return m_declared; }
void external(bool external) { m_external = external; }
bool external() const { return m_external; }
bool empty() const;
bool element_content() const;
validator get_validator() const;
private:
std::string m_name;
attribute_list m_attlist;
allowed_ptr m_allowed;
bool m_declared, m_external;
};
// --------------------------------------------------------------------
class entity : boost::noncopyable
{
public:
const std::string& name() const { return m_name; }
const std::string& replacement() const { return m_replacement; }
const std::string& path() const { return m_path; }
bool parameter() const { return m_parameter; }
bool parsed() const { return m_parsed; }
void parsed(bool parsed) { m_parsed = parsed; }
const std::string& ndata() const { return m_ndata; }
void ndata(const std::string& ndata) { m_ndata = ndata; }
bool external() const { return m_external; }
bool externally_defined() const { return m_externally_defined; }
void externally_defined(bool externally_defined)
{ m_externally_defined = externally_defined; }
protected:
entity(const std::string& name, const std::string& replacement,
bool external, bool parsed)
: m_name(name), m_replacement(replacement), m_parameter(false), m_parsed(parsed), m_external(external)
, m_externally_defined(false) {}
entity(const std::string& name, const std::string& replacement,
const std::string& path)
: m_name(name), m_replacement(replacement), m_path(path), m_parameter(true), m_parsed(true), m_external(true)
, m_externally_defined(false) {}
std::string m_name;
std::string m_replacement;
std::string m_ndata;
std::string m_path;
bool m_parameter;
bool m_parsed;
bool m_external;
bool m_externally_defined;
};
class general_entity : public entity
{
public:
general_entity(const std::string& name, const std::string& replacement,
bool external = false, bool parsed = true)
: entity(name, replacement, external, parsed) {}
};
class parameter_entity : public entity
{
public:
parameter_entity(const std::string& name, const std::string& replacement,
const std::string& path)
: entity(name, replacement, path) {}
};
}
}
}
#endif
|