Click here to Skip to main content
15,886,362 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 60.1K   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 "markup_iterator.h"
#include "descendant_markup_iterator.h"
#include "reverse_markup_iterator.h"
#include "common.h"
#include "xhtml_parser.h"
#include "xhtml_formatter.h"
#include <string>

namespace Xport
{
  template<typename T, typename U> class xhtml_markup;
  template<typename T, typename U, typename V, typename W> class xhtml_element;
  template<typename T, typename U, typename V, typename W> class xhtml_doc;
  template<typename T, typename U, typename V, typename W> class xhtml_comment;
  template<typename T, typename U, typename V, typename W> class xhtml_pcdata;
  template<typename T, typename U, typename V, typename W> class xhtml_processing_instruction;
  template<typename T, typename U, typename V, typename W> class xhtml_doctype_declaration;
  template<typename T, typename U, typename V, typename W> class xhtml_end_tag;
  template<typename T, typename U, typename V, typename W> class xhtml_formatter;
  template<typename T, typename U, typename V, typename W> class xhtml_parser;
  template<typename T> class xhtml_stylesheet_item;
}

template<typename DT, typename CT> 
class Xport::xhtml_markup
{
private:
  // construction/destruction
  xhtml_markup() : pParent(0) {}
  xhtml_markup(const xhtml_markup& src); 
  virtual ~xhtml_markup();
  xhtml_markup& operator=(const xhtml_markup& rhs);

public:
  // iterator typedefs
  typedef markup_iterator<xhtml_markup, CT, const xhtml_markup*, const xhtml_markup&>               const_iterator;
  typedef markup_iterator<xhtml_markup, CT, xhtml_markup*, xhtml_markup&>                           iterator;
  typedef descendant_markup_iterator<xhtml_markup, CT, const xhtml_markup*, const xhtml_markup&>    const_descendant_iterator;
  typedef descendant_markup_iterator<xhtml_markup, CT, xhtml_markup*, xhtml_markup&>                descendant_iterator;
  typedef reverse_markup_iterator<xhtml_markup, CT, const xhtml_markup*, const xhtml_markup&>       const_reverse_iterator;
  typedef reverse_markup_iterator<xhtml_markup, CT, xhtml_markup*, xhtml_markup&>                   reverse_iterator;

  // typedefs
  typedef xhtml_markup value_type;
  typedef xhtml_markup& reference_type;
  typedef const xhtml_markup& const_reference_type;
  typedef xhtml_markup* pointer_type;
  typedef const xhtml_markup* const_pointer_type;
  typedef ptrdiff_t difference_type;
  typedef size_t size_type;
  typedef DT doc_type;

  // derived typedefs
  typedef xhtml_element<DT, CT, xhtml_markup, xhtml_stylesheet_item<CT> > element_type;

  // public interface
  virtual iterator insert(const xhtml_markup<DT, CT>& _xhtml_markup) { return end(); }
  virtual iterator insert(const std::basic_string<CT>& _pc_data) { return end(); }
  virtual iterator insert(const const_iterator& pos, const xhtml_markup<DT, CT>& _xhtml_markup) { return end(); }
  virtual iterator insert(const const_iterator& pos, const std::basic_string<CT>& _pc_data) { return end(); }
  virtual void insert(const const_iterator& pos, size_type num, const xhtml_markup<DT, CT>& _xhtml_markup) {}
  virtual void insert(const const_iterator& pos, const const_iterator& it_beg, const const_iterator& it_end) {}
  virtual xhtml_markup& operator << (const std::basic_string<CT>& _pc_data) { return *this; }
  virtual xhtml_markup& operator << (const xhtml_markup<DT, CT>& _xhtml_markup) { return *this; }
  virtual iterator push_back(const xhtml_markup<DT, CT>& _xhtml_markup) { return end(); }
  virtual iterator push_back(const std::basic_string<CT>& _pc_data) { return end(); }
  virtual iterator push_front(const xhtml_markup<DT, CT>& markup) { return end(); }
  virtual iterator push_front(const std::basic_string<CT>& pcdat) { return end(); }

  virtual std::basic_string<CT> attribute(attribute::xhtml_attribute attrib ) const { return std::basic_string<CT>(); }
  virtual bool attribute(attribute::xhtml_attribute attrib, const std::basic_string<CT>& value) { return false; }
  virtual const std::map<attribute::xhtml_attribute, std::basic_string<CT> >* attributes() const { return NULL; }
  virtual std::map<attribute::xhtml_attribute, std::basic_string<CT> >* attributes() { return NULL; }

  virtual xhtml_markup_type markup_type() const { return mt_unknown; }
  virtual std::basic_string<CT> style(css::css_property prop ) const { return std::basic_string<CT>(); }
  virtual bool style(css::css_property prop, const std::basic_string<CT>& value) { return false; }
  virtual const std::map<css::css_property, std::basic_string<CT> >* styles() const { return NULL; }
  virtual std::map<css::css_property, std::basic_string<CT> >* styles() { return NULL; }
  virtual xhtml_tag_enum tag() const { return invalid_tag; }
  virtual std::basic_string<CT> tag_name() const { return std::basic_string<CT>(); }
  virtual void write(const xhtml_formatter<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >& _formatter) const { }

  const xhtml_markup* parent() const { return pParent; }
  xhtml_markup* parent() { return pParent; }
  
  xhtml_markup& back() { return *children.back(); }
  const xhtml_markup& back() const { return *children.back(); }
  xhtml_markup& front() { return *children.front(); }
  const xhtml_markup& front() const { return *children.front(); }

  void clear();
  bool empty() const { return children.empty(); }
  iterator erase(const const_iterator& it); 
  iterator erase(const const_iterator& beg_it, const const_iterator& end_it);
  void pop_back() { iterator it = end(); --it;  erase(it); }
  void pop_front() { iterator it = begin(); erase(it); }
  size_type size() const { return children.size(); }

  // child iterator accessors
  iterator begin() { return iterator(children.begin(), this); }
  const_iterator begin() const { return const_iterator(children.begin(), this); }
  iterator end() { return iterator(children.end(), this); }
  const_iterator end() const { return const_iterator(children.end(), this); }

  // child reverse accessors
  reverse_iterator rbegin() { return reverse_iterator(end()); }
  const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
  reverse_iterator rend() { return reverse_iterator(begin()); }
  const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }

  // descendant iterator accessors
  descendant_iterator descendant_begin()  { return descendant_iterator(this, true); }
  const_descendant_iterator descendant_begin() const { return const_descendant_iterator(this, true); }
  descendant_iterator descendant_end()  { return descendant_iterator(this, false); }
  const_descendant_iterator descendant_end() const { return const_descendant_iterator(this, false); }

private:
  // implementation
  virtual bool allow_pcdata() const { return false; }
  virtual xhtml_nesting_type allowed_nested_type() const { return xhtml_nesting_type(0); }
  virtual xhtml_markup* clone() const { return 0; }
  virtual bool empty_tag() const { return false; }
  virtual std::basic_string<CT> end_tag() const { return std::basic_string<CT>(); }
  virtual const Xport::tag<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >* get_tag() const { return NULL; }
  virtual iterator insert(xhtml_markup<DT, CT>* pMarkup) { return end(); }
  virtual bool is_end_tag() const { return false; }
  virtual xhtml_nesting_type nesting_type() const { return xhtml_nesting_type(0); }
  virtual void remove_trailing_whitespace() {}
  void set_parent(xhtml_markup* parent)  { pParent = parent; }
  virtual bool validate_nesting(const xhtml_markup& _xhtml_markup) const { return false; }

  // data
  std::list<xhtml_markup*> children;
  xhtml_markup* pParent;

  // friends
  #if defined(_MSC_VER) && _MSC_VER < 1300
    friend class descendant_markup_iterator<xhtml_markup, CT, const xhtml_markup*, const xhtml_markup&>;
    friend class descendant_markup_iterator<xhtml_markup, CT, xhtml_markup*, xhtml_markup&>;
    friend class xhtml_element<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >;
    friend class xhtml_doc<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >;
    friend class xhtml_pcdata<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >;
    friend class xhtml_processing_instruction<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >;
    friend class xhtml_comment<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >;
    friend class xhtml_end_tag<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >;
    friend class xhtml_doctype_declaration<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >;
    friend class xhtml_parser<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >;
    friend class xhtml_formatter<DT, CT, xhtml_markup<DT, CT>, xhtml_stylesheet_item<CT> >;
  #else
    template<typename T, typename U, typename V, typename W> friend class descendant_markup_iterator;
    template<typename T, typename U, typename V, typename W> friend class xhtml_element;
    template<typename T, typename U, typename V, typename W> friend class xhtml_doc;
    template<typename T, typename U, typename V, typename W> friend class xhtml_pcdata;
    template<typename T, typename U, typename V, typename W> friend class xhtml_processing_instruction;
    template<typename T, typename U, typename V, typename W> friend class xhtml_comment;
    template<typename T, typename U, typename V, typename W> friend class xhtml_end_tag;
    template<typename T, typename U, typename V, typename W> friend class xhtml_doctype_declaration;
    template<typename T, typename U, typename V, typename W> friend class xhtml_parser;
    template<typename T, typename U, typename V, typename W> friend class xhtml_formatter;
  #endif
};

#include "xhtml_markup.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