Click here to Skip to main content
15,886,756 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 "stylesheet.h"
#include "common.h"
#include "tag.h"
#include "xhtml_element.h"
#include "xhtml_comment.h"
#include "xhtml_processing_instruction.h"
#include "xhtml_parser.h"
#include "xhtml_formatter.h"
#include <string>
#include <fstream>
#include <sstream>
#include <cassert>

namespace Xport
{
  using namespace css;
  using namespace attribute;
  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_formatter;
  template<typename T, typename U> class xhtml_stylesheet;

  // convenience typedefs
  // xthml strict
  typedef xhtml_markup<xhtml_strict, char>                                                                      markup;
  typedef xhtml_doc<xhtml_strict, char, xhtml_markup<xhtml_strict, char>, xhtml_stylesheet_item<char> >                                      document;
  typedef xhtml_element<xhtml_strict, char, xhtml_markup<xhtml_strict, char>, xhtml_stylesheet_item<char> >                                  element;
  typedef xhtml_pcdata<xhtml_strict, char, xhtml_markup<xhtml_strict, char>, xhtml_stylesheet_item<char> >                                   pcdata;
  typedef xhtml_comment<xhtml_strict, char, xhtml_markup<xhtml_strict, char>, xhtml_stylesheet_item<char> >                                  comment;
  typedef xhtml_processing_instruction<xhtml_strict, char, xhtml_markup<xhtml_strict, char>, xhtml_stylesheet_item<char> >                   procinstr;
  typedef xhtml_parser<xhtml_strict, char, xhtml_markup<xhtml_strict, char>, xhtml_stylesheet_item<char> >                                   parser;
  typedef xhtml_formatter<xhtml_strict, char, xhtml_markup<xhtml_strict, char>, xhtml_stylesheet_item<char> >                                formatter;

  typedef xhtml_markup<xhtml_strict, wchar_t>                                                                   wmarkup;
  typedef xhtml_doc<xhtml_strict, wchar_t, xhtml_markup<xhtml_strict, wchar_t>, xhtml_stylesheet_item<wchar_t> >                                wdocument;
  typedef xhtml_element<xhtml_strict, wchar_t, xhtml_markup<xhtml_strict, wchar_t>, xhtml_stylesheet_item<wchar_t> >                            welement;
  typedef xhtml_pcdata<xhtml_strict, wchar_t, xhtml_markup<xhtml_strict, wchar_t>, xhtml_stylesheet_item<wchar_t> >                             wpcdata;
  typedef xhtml_comment<xhtml_strict, wchar_t, xhtml_markup<xhtml_strict, wchar_t>, xhtml_stylesheet_item<wchar_t> >                            wcomment;
  typedef xhtml_processing_instruction<xhtml_strict, wchar_t, xhtml_markup<xhtml_strict, wchar_t>, xhtml_stylesheet_item<wchar_t> >             wprocinstr;
  typedef xhtml_parser<xhtml_strict, wchar_t, xhtml_markup<xhtml_strict, wchar_t>, xhtml_stylesheet_item<wchar_t> >                             wparser;
  typedef xhtml_formatter<xhtml_strict, wchar_t, xhtml_markup<xhtml_strict, wchar_t>, xhtml_stylesheet_item<wchar_t> >                          wformatter;

  // xhtml transitional
  typedef xhtml_markup<xhtml_transitional, char>                                                                tmarkup;
  typedef xhtml_doc<xhtml_transitional, char, xhtml_markup<xhtml_transitional, char>, xhtml_stylesheet_item<char> >                          tdocument;
  typedef xhtml_element<xhtml_transitional, char, xhtml_markup<xhtml_transitional, char>, xhtml_stylesheet_item<char> >                      telement;
  typedef xhtml_pcdata<xhtml_transitional, char, xhtml_markup<xhtml_transitional, char>, xhtml_stylesheet_item<char> >                       tpcdata;
  typedef xhtml_comment<xhtml_transitional, char, xhtml_markup<xhtml_transitional, char>, xhtml_stylesheet_item<char> >                      tcomment;
  typedef xhtml_processing_instruction<xhtml_transitional, char, xhtml_markup<xhtml_transitional, char>, xhtml_stylesheet_item<char> >       tprocinstr;
  typedef xhtml_parser<xhtml_transitional, char, xhtml_markup<xhtml_transitional, char>, xhtml_stylesheet_item<char> >                       tparser;
  typedef xhtml_formatter<xhtml_transitional, char, xhtml_markup<xhtml_transitional, char>, xhtml_stylesheet_item<char> >                    tformatter;

  typedef xhtml_markup<xhtml_transitional, wchar_t>                                                             wtmarkup;
  typedef xhtml_doc<xhtml_transitional, wchar_t, xhtml_markup<xhtml_transitional, wchar_t>, xhtml_stylesheet_item<wchar_t> >                    wtdocument;
  typedef xhtml_element<xhtml_transitional, wchar_t, xhtml_markup<xhtml_transitional, wchar_t>, xhtml_stylesheet_item<wchar_t> >                wtelement;
  typedef xhtml_pcdata<xhtml_transitional, wchar_t, xhtml_markup<xhtml_transitional, wchar_t>, xhtml_stylesheet_item<wchar_t> >                 wtpcdata;
  typedef xhtml_comment<xhtml_transitional, wchar_t, xhtml_markup<xhtml_transitional, wchar_t>, xhtml_stylesheet_item<wchar_t> >                wtcomment;
  typedef xhtml_processing_instruction<xhtml_transitional, wchar_t, xhtml_markup<xhtml_transitional, wchar_t>, xhtml_stylesheet_item<wchar_t> > wtprocinstr;
  typedef xhtml_parser<xhtml_transitional, wchar_t, xhtml_markup<xhtml_transitional, wchar_t>, xhtml_stylesheet_item<wchar_t> >                 wtparser;
  typedef xhtml_formatter<xhtml_transitional, wchar_t, xhtml_markup<xhtml_transitional, wchar_t>, xhtml_stylesheet_item<wchar_t> >              wtformatter;

  // xhtml frameset
  typedef xhtml_markup<xhtml_frameset, char>                                                                    fmarkup;
  typedef xhtml_doc<xhtml_frameset, char, xhtml_markup<xhtml_frameset, char>, xhtml_stylesheet_item<char> >                                  fdocument;
  typedef xhtml_element<xhtml_frameset, char, xhtml_markup<xhtml_frameset, char>, xhtml_stylesheet_item<char> >                              felement;
  typedef xhtml_pcdata<xhtml_frameset, char, xhtml_markup<xhtml_frameset, char>, xhtml_stylesheet_item<char> >                               fpcdata;
  typedef xhtml_comment<xhtml_frameset, char, xhtml_markup<xhtml_frameset, char>, xhtml_stylesheet_item<char> >                              fcomment;
  typedef xhtml_processing_instruction<xhtml_frameset, char, xhtml_markup<xhtml_frameset, char>, xhtml_stylesheet_item<char> >               fprocinstr;
  typedef xhtml_parser<xhtml_frameset, char, xhtml_markup<xhtml_frameset, char>, xhtml_stylesheet_item<char> >                               fparser;
  typedef xhtml_formatter<xhtml_frameset, char, xhtml_markup<xhtml_frameset, char>, xhtml_stylesheet_item<char> >                            fformatter;

  typedef xhtml_markup<xhtml_frameset, wchar_t>                                                                 wfmarkup;
  typedef xhtml_doc<xhtml_frameset, wchar_t, xhtml_markup<xhtml_frameset, wchar_t>, xhtml_stylesheet_item<wchar_t> >                            wfdocument;
  typedef xhtml_element<xhtml_frameset, wchar_t, xhtml_markup<xhtml_frameset, wchar_t>, xhtml_stylesheet_item<wchar_t> >                        wfelement;
  typedef xhtml_pcdata<xhtml_frameset, wchar_t, xhtml_markup<xhtml_frameset, wchar_t>, xhtml_stylesheet_item<wchar_t> >                         wfpcdata;
  typedef xhtml_comment<xhtml_frameset, wchar_t, xhtml_markup<xhtml_frameset, wchar_t>, xhtml_stylesheet_item<wchar_t> >                        wfcomment;
  typedef xhtml_processing_instruction<xhtml_frameset, wchar_t, xhtml_markup<xhtml_frameset, wchar_t>, xhtml_stylesheet_item<wchar_t> >         wfprocinstr;
  typedef xhtml_parser<xhtml_frameset, wchar_t, xhtml_markup<xhtml_frameset, wchar_t>, xhtml_stylesheet_item<wchar_t> >                         wfparser;
  typedef xhtml_formatter<xhtml_frameset, wchar_t, xhtml_markup<xhtml_frameset, wchar_t>, xhtml_stylesheet_item<wchar_t> >                      wfformatter;

  enum document_formation {root_doc = 1};
}

template <typename DT, typename CT, typename MBT, typename SBT>
class Xport::xhtml_doc
{
public:
  inline explicit xhtml_doc(document_formation formation = document_formation(0));
  xhtml_doc(const xhtml_doc& rhs);
  virtual ~xhtml_doc();
  xhtml_doc& operator =(const xhtml_doc& rhs);

  typedef DT doc_type;
  typedef typename MBT::iterator        base_iterator_type;         // vc6 compatibility
  typedef typename MBT::const_iterator  const_base_iterator_type;   // vc6 compatibility

  // interface
  MBT* body() { return get_body(); }
  const MBT* body() const { return get_body(); }
  MBT* frameset() { return get_frameset(); }
  const MBT* frameset() const { return get_frameset(); }
  MBT* head() { return get_head(); }
  const MBT* head() const { return get_head(); }
  MBT* title() { return get_title(); }
  const MBT* title() const { return get_title(); }
  MBT* html() { return pHtml; }
  const MBT* html() const { return pHtml; }

  void clear(document_formation formation = document_formation(0)); 
  std::basic_string<CT> document_type() const { std::basic_string<CT> temp; DT::name(temp); return temp; }
  bool parse(const std::basic_string<CT>& path);
  bool parse(const xhtml_parser<DT, CT, MBT, SBT>& prsr);
  bool write(const xhtml_formatter<DT, CT, MBT, SBT>& fmtr) const;
  bool write(const std::basic_string<char>& path) const;

  typename MBT::iterator insert(const MBT& mkup) { return insert(pRoot->end(), mkup); }
  base_iterator_type insert(const const_base_iterator_type& pos, const MBT& mkup);
  base_iterator_type insert(const xhtml_stylesheet<CT, SBT>& ss);
  xhtml_doc& operator << (const MBT& mkup) { insert(mkup); return *this; }

  // iterator accessors
  typename MBT::iterator begin() { return pRoot->begin(); }
  typename MBT::iterator end() { return pRoot->end(); }
  typename MBT::const_iterator begin() const { return pRoot->begin(); }
  typename MBT::const_iterator end() const { return pRoot->end(); }
  
  // descendant iterator accessors
  typename MBT::const_descendant_iterator descendant_begin() const { return pRoot->descendant_begin(); }
  typename MBT::const_descendant_iterator descendant_end() const { return pRoot->descendant_end(); }
  typename MBT::descendant_iterator descendant_begin()  { return pRoot->descendant_begin(); }
  typename MBT::descendant_iterator descendant_end()  { return pRoot->descendant_end(); }

private:
  // implementation
  std::basic_string<CT> get_doctype_definition() const;
  MBT* get_main_frameset_frame();
  base_iterator_type insert_html(const MBT& htmlElem);
  base_iterator_type insert_root(const MBT& rootElem);
  void create_root_doc_elements();
  void assign_root_doc_elements();
  MBT* get_body() const;
  MBT* get_head() const;
  MBT* get_title() const;
  MBT* get_frameset() const;

// data
private:
  MBT* pRoot;
  MBT* pHtml;
  mutable MBT* pHead;
  mutable MBT* pTitle;
  mutable MBT* pBody;
  mutable MBT* pFrameset;
};


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