Click here to Skip to main content
15,897,891 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.3K   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> class xhtml_element;
  template<typename T, typename U> class xhtml_doc;
  template<typename T, typename U> class xhtml_formatter;
  template<typename T> class xhtml_stylesheet;

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

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

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

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

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

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

  enum document_formation {root_doc = 1};
}

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

  typedef DT doc_type;

  // interface
  xhtml_markup<DT, CT>* body() { return get_body(); }
  const xhtml_markup<DT, CT>* body() const { return get_body(); }
  xhtml_markup<DT, CT>* frameset() { return get_frameset(); }
  const xhtml_markup<DT, CT>* frameset() const { return get_frameset(); }
  xhtml_markup<DT, CT>* head() { return get_head(); }
  const xhtml_markup<DT, CT>* head() const { return get_head(); }
  xhtml_markup<DT, CT>* title() { return get_title(); }
  const xhtml_markup<DT, CT>* title() const { return get_title(); }
  xhtml_markup<DT, CT>* html() { return pHtml; }
  const xhtml_markup<DT, CT>* 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>& prsr);
  bool write(const xhtml_formatter<DT, CT>& fmtr) const;
  bool write(const std::basic_string<char>& path) const;

  typename xhtml_markup<DT, CT>::iterator insert(const xhtml_markup<DT, CT>& mkup);
  typename xhtml_markup<DT, CT>::iterator insert(const typename xhtml_markup<DT, CT>::const_iterator& pos, const xhtml_markup<DT, CT>& mkup);
  typename xhtml_markup<DT, CT>::iterator insert(const xhtml_stylesheet<CT>& ss);
  xhtml_doc& operator << (const xhtml_markup<DT, CT>& mkup) { insert(mkup); return *this; }

  // iterator accessors
  typename xhtml_markup<DT, CT>::iterator begin() { return pRoot->begin(); }
  typename xhtml_markup<DT, CT>::iterator end() { return pRoot->end(); }
  typename xhtml_markup<DT, CT>::const_iterator begin() const { return pRoot->begin(); }
  typename xhtml_markup<DT, CT>::const_iterator end() const { return pRoot->end(); }
  
  // descendant iterator accessors
  typename xhtml_markup<DT, CT>::const_descendant_iterator descendant_begin() const { return pRoot->descendant_begin(); }
  typename xhtml_markup<DT, CT>::const_descendant_iterator descendant_end() const { return pRoot->descendant_end(); }
  typename xhtml_markup<DT, CT>::descendant_iterator descendant_begin()  { return pRoot->descendant_begin(); }
  typename xhtml_markup<DT, CT>::descendant_iterator descendant_end()  { return pRoot->descendant_end(); }

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

// data
private:
  xhtml_markup<DT, CT>* pRoot;
  xhtml_markup<DT, CT>* pHtml;
  mutable xhtml_markup<DT, CT>* pHead;
  mutable xhtml_markup<DT, CT>* pTitle;
  mutable xhtml_markup<DT, CT>* pBody;
  mutable xhtml_markup<DT, CT>* 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