Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / Win32

Xport: XHTML Parsing and Objective Reporting Toolkit

Rate me:
Please Sign up or sign in to vote.
4.73/5 (10 votes)
4 May 2008GPL313 min read 60K   682   32  
Open source C++ class template library for generating and parsing xhtml documents.
/************************************************************************
Xport: XHTML Parsing & Objective Reporting Toolkit
Copyright (C) 2007  Mitchel Haas

This file is part of Xport.

Xport is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Xport is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Xport.  If not, see <http://www.gnu.org/licenses/>.

For complete documentation on this library and alternative
licensing options, visit http://www.xportpro.com
Email questions, comments or suggestions to mitchel.haas@xportpro.com
************************************************************************/
#pragma once
#include "common.h"
#include "xhtml_transitional.h"
#include <set>
#include <string>


namespace Xport 
{
  template<typename T, typename U> class tag_traits;
  template<typename T, typename U> class tag_trait;
  template<typename T> struct tag_info;
}



template <typename DT, typename CT = char>
class Xport::tag_trait
{
private: // only tag_traits can create these objects
  tag_trait(  xhtml_tag_enum tg_enum, 
        const std::string& tname, 
        xhtml_nesting_type ntype, 
        xhtml_nesting_type allowed_ntype, 
        const std::set<attribute::xhtml_attribute>& allowed_attrs,
        const std::set<xhtml_tag_enum>& allowed_nested_tgs,
        const std::set<xhtml_tag_enum>& nested_except)
          : tag_enum(tg_enum), tag_nesting_type(ntype), allowed_tag_nested_type(allowed_ntype), 
            allowed_nested_tags(allowed_nested_tgs), nested_tag_exceptions(nested_except), attributes(allowed_attrs)  
  {
    std::basic_ostringstream<CT> otname;
    otname << tname.c_str();
    tg_name = otname.str();
  }


  ~tag_trait() {}

public:
  // public interface
  xhtml_nesting_type allowed_nested_type() const { return allowed_tag_nested_type; }
  bool check_attribute(const attribute::xhtml_attribute attrib) const { return attributes.find(attrib) != attributes.end(); }
  bool check_style(const css::css_property _property) const;
  bool empty_tag() const { return allowed_tag_nested_type == xhtml_nesting_type(0) && allowed_nested_tags.empty();; }
  const std::set<xhtml_tag_enum>& nested_exceptions() const { return nested_tag_exceptions; }
  const std::set<xhtml_tag_enum>& nested_tags() const { return allowed_nested_tags; }
  xhtml_nesting_type nesting_type() const { return tag_nesting_type; }
  xhtml_tag_enum tag_enumeration() const { return tag_enum; }
  std::basic_string<CT> tag_name() const { return tg_name; }

private:
  // private interface & implementation
  void add_allowed_nested_tag(DT _tag_enum) { allowed_nested_tags.insert(_tag_enum); }
  void add_nested_exception(DT _tag_enum) { nested_tag_exceptions.insert(_tag_enum); }
  bool allow_pcdata() const { return (allowed_nested_type & (in_line|pc_data) > 0); }
  friend bool operator < (const tag_trait& lhs, const tag_trait& rhs) { return lhs.tag_enum < rhs.tag_enum; }
  void remove_allowed_nested_tag(DT _tag_enum) { allowed_nested_tags.erase(_tag_enum); }
  void remove_attributes(const attribute::xhtml_attribute* attribs, int count);
  void remove_nested_exception(DT _tag_enum) { nested_tag_exceptions.erase(_tag_enum); }

private:
  xhtml_tag_enum tag_enum;
  xhtml_nesting_type tag_nesting_type;
  xhtml_nesting_type allowed_tag_nested_type;
  std::basic_string<CT> tg_name;
  std::set<xhtml_tag_enum> allowed_nested_tags;
  std::set<xhtml_tag_enum> nested_tag_exceptions;
  std::set<attribute::xhtml_attribute> attributes;
  std::set<css::css_property> properties;

  // friends
  friend class tag_traits<DT, CT>;
  friend struct tag_info<DT>;
};



#include "tag_trait.inl"

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)



Comments and Discussions