// file      : xsde/cxx/parser/validating/nmtokens.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2007 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <xsde/cxx/config.hxx>

#include <xsde/cxx/parser/validating/nmtokens.hxx>

namespace xsde
{
  namespace cxx
  {
    namespace parser
    {
      namespace validating
      {
        nmtokens_pimpl::
        ~nmtokens_pimpl ()
        {
          delete seq_;
        }

        nmtokens_pimpl::
        nmtokens_pimpl ()
            : seq_ (0)
        {
        }

        void nmtokens_pimpl::
        _pre ()
        {
          delete seq_;
          seq_ = new string_sequence ();

#ifndef XSDE_EXCEPTIONS
          if (seq_ == 0)
          {
            sys_error (sys_error::no_memory);
            return;
          }
#endif

          nmtokens_pskel::_pre ();
        }

        void nmtokens_pimpl::
        _post ()
        {
          nmtokens_pskel::_post ();

          // Should have at least one element.
          //
          if (!error_p_ () && seq_->size () < 1)
            schema_error (schema_error::invalid_nmtokens_value);
        }

        string_sequence* nmtokens_pimpl::
        post_nmtokens ()
        {
          string_sequence* r = seq_;
          seq_ = 0;
          return r;
        }

        void nmtokens_pimpl::
        _xsde_parse_item (const ro_string& s)
        {
          parser_.pre ();

          if (!parser_.error_p_ ())
            parser_._pre ();

          if (!parser_.error_p_ ())
            parser_._characters (s);

          if (!parser_.error_p_ ())
            parser_._post ();

          if (!parser_.error_p_ ())
          {
            char* s = parser_.post_nmtoken ();

            if (!parser_.error_p_ ())
            {
#ifdef XSDE_EXCEPTIONS
              seq_->push_back_attach (s);
#else
              if (seq_->push_back_attach (s))
              {
                sys_error (sys_error::no_memory);
                return;
              }
#endif
            }
          }

          if (parser_.error_p_ ())
            copy_error_ (&parser_);
        }
      }
    }
  }
}
