diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-03-28 16:53:35 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-03-28 16:53:35 +0200 |
commit | 4332a60585a14ff51bc54d29a4a81d37b3b1df81 (patch) | |
tree | fd3b807e3f5b3841371d509505a50498d21a2f99 /libxsde/xsde/cxx/serializer/validating | |
parent | c2d201480002a24c05f1f4bc934036555b79cad6 (diff) |
Validate enumerations in the xsd:string parser/serializer
Diffstat (limited to 'libxsde/xsde/cxx/serializer/validating')
6 files changed, 179 insertions, 94 deletions
diff --git a/libxsde/xsde/cxx/serializer/validating/string-common.cxx b/libxsde/xsde/cxx/serializer/validating/string-common.cxx new file mode 100644 index 0000000..20a1b34 --- /dev/null +++ b/libxsde/xsde/cxx/serializer/validating/string-common.cxx @@ -0,0 +1,69 @@ +// file : xsde/cxx/serializer/validating/string-common.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#include <string.h> // strlen, strcmp +#include <xsde/cxx/serializer/validating/string-common.hxx> + +namespace xsde +{ + namespace cxx + { + namespace serializer + { + namespace validating + { + void string_common:: + validate_facets (const char* s, +#ifdef XSDE_STL + size_t n, +#endif + const string_facets::facets& f, + context& ctx) + { +#ifndef XSDE_STL + size_t n; + if (f.length_set_ || f.min_length_set_ || f.max_length_set_) + n = strlen (s); +#endif + + if (f.length_set_ && n != f.length_) + { + ctx.schema_error (schema_error::length_not_equal_prescribed); + return; + } + + if (f.min_length_set_ && n < f.min_length_) + { + ctx.schema_error (schema_error::length_less_than_min); + return; + } + + if (f.max_length_set_ && n > f.max_length_) + { + ctx.schema_error (schema_error::length_greater_than_max); + return; + } + + if (f.enum_count_ != 0) + { + size_t i = 0; + + for (; i < f.enum_count_; ++i) + { + if (strcmp (s, f.enum_[i]) == 0) + break; + } + + if (i == f.enum_count_) + { + ctx.schema_error (schema_error::value_not_in_enumeration); + return; + } + } + } + } + } + } +} diff --git a/libxsde/xsde/cxx/serializer/validating/string-common.hxx b/libxsde/xsde/cxx/serializer/validating/string-common.hxx new file mode 100644 index 0000000..d7f221d --- /dev/null +++ b/libxsde/xsde/cxx/serializer/validating/string-common.hxx @@ -0,0 +1,35 @@ +// file : xsde/cxx/serializer/validating/string-common.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSDE_CXX_SERIALIZER_VALIDATING_STRING_COMMON_HXX +#define XSDE_CXX_SERIALIZER_VALIDATING_STRING_COMMON_HXX + +#include <xsde/cxx/config.hxx> +#include <xsde/cxx/serializer/validating/xml-schema-sskel.hxx> + +namespace xsde +{ + namespace cxx + { + namespace serializer + { + namespace validating + { + struct string_common + { + static void + validate_facets (const char* s, +#ifdef XSDE_STL + size_t n, +#endif + const string_facets::facets&, + context&); + }; + } + } + } +} + +#endif // XSDE_CXX_SERIALIZER_VALIDATING_STRING_COMMON_HXX diff --git a/libxsde/xsde/cxx/serializer/validating/string-stl.cxx b/libxsde/xsde/cxx/serializer/validating/string-stl.cxx index 1e9a37b..ef13746 100644 --- a/libxsde/xsde/cxx/serializer/validating/string-stl.cxx +++ b/libxsde/xsde/cxx/serializer/validating/string-stl.cxx @@ -4,6 +4,7 @@ // license : GNU GPL v2 + exceptions; see accompanying LICENSE file #include <xsde/cxx/serializer/validating/string-stl.hxx> +#include <xsde/cxx/serializer/validating/string-common.hxx> namespace xsde { @@ -22,27 +23,8 @@ namespace xsde void string_simpl:: _serialize_content () { - // Check facets. - // - const facets& f = _facets (); - - if (f.length_set_ && value_.size () != f.length_) - { - _schema_error (schema_error::length_not_equal_prescribed); - return; - } - - if (f.min_length_set_ && value_.size () < f.min_length_) - { - _schema_error (schema_error::length_less_than_min); - return; - } - - if (f.max_length_set_ && value_.size () > f.max_length_) - { - _schema_error (schema_error::length_greater_than_max); - return; - } + string_common::validate_facets ( + value_.c_str (), value_.size (), _facets (), _context ()); // Make sure we don't hold any references to the string. // diff --git a/libxsde/xsde/cxx/serializer/validating/string.cxx b/libxsde/xsde/cxx/serializer/validating/string.cxx index 0def094..befe450 100644 --- a/libxsde/xsde/cxx/serializer/validating/string.cxx +++ b/libxsde/xsde/cxx/serializer/validating/string.cxx @@ -3,8 +3,8 @@ // copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC // license : GNU GPL v2 + exceptions; see accompanying LICENSE file -#include <string.h> // strlen #include <xsde/cxx/serializer/validating/string.hxx> +#include <xsde/cxx/serializer/validating/string-common.hxx> namespace xsde { @@ -30,31 +30,7 @@ namespace xsde void string_simpl:: _serialize_content () { - // Check facets. - // - if (length_set_ || min_length_set_ || max_length_set_) - { - size_t n = strlen (value_); - const facets& f = _facets (); - - if (f.length_set_ && n != f.length_) - { - _schema_error (schema_error::length_not_equal_prescribed); - return; - } - - if (f.min_length_set_ && n < f.min_length_) - { - _schema_error (schema_error::length_less_than_min); - return; - } - - if (f.max_length_set_ && n > f.max_length_) - { - _schema_error (schema_error::length_greater_than_max); - return; - } - } + string_common::validate_facets (value_, _facets (), _context ()); _characters (value_); diff --git a/libxsde/xsde/cxx/serializer/validating/xml-schema-sskel.hxx b/libxsde/xsde/cxx/serializer/validating/xml-schema-sskel.hxx index 940f256..c206e78 100644 --- a/libxsde/xsde/cxx/serializer/validating/xml-schema-sskel.hxx +++ b/libxsde/xsde/cxx/serializer/validating/xml-schema-sskel.hxx @@ -636,9 +636,46 @@ namespace xsde #endif }; + // // String-based types. // - struct string_sskel: simple_content + + struct string_facets + { + string_facets (); + + void + _length_facet (size_t); + + void + _max_length_facet (size_t); + + void + _min_length_facet (size_t); + + void + _enumeration_facet (const char* const*, size_t count); + + public: + struct facets + { + size_t length_; + size_t min_length_; + size_t max_length_; + + const char* const* enum_; + size_t enum_count_; + + unsigned int length_set_ : 1; + unsigned int min_length_set_ : 1; + unsigned int max_length_set_ : 1; + }; + + protected: + facets facets_; + }; + + struct string_sskel: simple_content, string_facets { virtual void #ifdef XSDE_STL @@ -655,9 +692,8 @@ namespace xsde _dynamic_type () const; #endif - string_sskel (); - #ifdef XSDE_REUSE_STYLE_TIEIN + string_sskel (); string_sskel (string_sskel* impl, void*); protected: @@ -665,33 +701,8 @@ namespace xsde #endif // Facets. // - public: - void - _length_facet (size_t); - - void - _max_length_facet (size_t); - - void - _min_length_facet (size_t); - - protected: - struct facets - { - size_t length_; - size_t min_length_; - size_t max_length_; - - unsigned int length_set_ : 1; - unsigned int min_length_set_ : 1; - unsigned int max_length_set_ : 1; - }; - const facets& _facets () const; - - private: - facets facets_; }; #ifdef XSDE_REUSE_STYLE_MIXIN diff --git a/libxsde/xsde/cxx/serializer/validating/xml-schema-sskel.ixx b/libxsde/xsde/cxx/serializer/validating/xml-schema-sskel.ixx index fd84bf0..b2eb40f 100644 --- a/libxsde/xsde/cxx/serializer/validating/xml-schema-sskel.ixx +++ b/libxsde/xsde/cxx/serializer/validating/xml-schema-sskel.ixx @@ -447,51 +447,63 @@ namespace xsde } #endif - // string_sskel + // string_facets // - inline string_sskel:: - string_sskel () + inline string_facets:: + string_facets () { -#ifdef XSDE_REUSE_STYLE_TIEIN - string_impl_ = 0; -#endif facets_.length_set_ = 0; facets_.min_length_set_ = 0; facets_.max_length_set_ = 0; - } -#ifdef XSDE_REUSE_STYLE_TIEIN - inline string_sskel:: - string_sskel (string_sskel* impl, void*) - : simple_content (impl, 0), string_impl_ (impl) - { - facets_.length_set_ = 0; - facets_.min_length_set_ = 0; - facets_.max_length_set_ = 0; + facets_.enum_ = 0; + facets_.enum_count_ = 0; } -#endif - inline void string_sskel:: + inline void string_facets:: _length_facet (size_t v) { facets_.length_ = v; facets_.length_set_ = 1; } - inline void string_sskel:: + inline void string_facets:: _max_length_facet (size_t v) { facets_.max_length_ = v; facets_.max_length_set_ = 1; } - inline void string_sskel:: + inline void string_facets:: _min_length_facet (size_t v) { facets_.min_length_ = v; facets_.min_length_set_ = 1; } + inline void string_facets:: + _enumeration_facet (const char* const* e, size_t count) + { + facets_.enum_ = e; + facets_.enum_count_ = count; + } + + // string_sskel + // +#ifdef XSDE_REUSE_STYLE_TIEIN + inline string_sskel:: + string_sskel () + : string_impl_ (0) + { + } + + inline string_sskel:: + string_sskel (string_sskel* impl, void*) + : simple_content (impl, 0), string_impl_ (impl) + { + } +#endif + inline const string_sskel::facets& string_sskel:: _facets () const { |