Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Xport: XHTML Parsing and Objective Reporting Toolkit

, 4 May 2008
Open source C++ class template library for generating and parsing xhtml documents.
xport_1.6.1.zip
descendant_markup_iterator.inl
stylesheet.inl
stylesheet_formatter.inl
stylesheet_rule.inl
tag.inl
tag_trait.inl
tag_traits.inl
xhtml_comment.inl
xhtml_doc.inl
xhtml_element.inl
xhtml_formatter.inl
xhtml_frameset.inl
xhtml_markup.inl
xhtml_parser.inl
xhtml_processing_instruction.inl
xhtml_strict.inl
xhtml_transitional.inl
xport_1.6.5.zip
descendant_markup_iterator.inl
stylesheet.inl
stylesheet_formatter.inl
stylesheet_rule.inl
tag.inl
tag_trait.inl
tag_traits.inl
xhtml_comment.inl
xhtml_doc.inl
xhtml_element.inl
xhtml_formatter.inl
xhtml_frameset.inl
xhtml_markup.inl
xhtml_parser.inl
xhtml_processing_instruction.inl
xhtml_strict.inl
xhtml_transitional.inl
xport_1.6.7.zip
descendant_markup_iterator.inl
stylesheet.inl
stylesheet_formatter.inl
stylesheet_rule.inl
tag.inl
tag_trait.inl
tag_traits.inl
xhtml_comment.inl
xhtml_doc.inl
xhtml_element.inl
xhtml_formatter.inl
xhtml_frameset.inl
xhtml_markup.inl
xhtml_parser.inl
xhtml_processing_instruction.inl
xhtml_strict.inl
xhtml_transitional.inl
xport_documentation.zip
Xport.chm
xport_htmlhelp.zip
Xport.chm
/************************************************************************
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 <iosfwd>
#include <vector>

namespace Xport 
{
  // forward declarations
  template<typename T, typename U> class xhtml_formatter;
  template<typename T, typename U> class xhtml_element;
  template<typename T, typename U> class xhtml_comment;
  template<typename T, typename U> class xhtml_doctype_declaration;
  template<typename T, typename U> class xhtml_processing_instruction;
  template<typename T, typename U> class xhtml_pcdata;
  // inline_layout            00000
  // newline_before_start_tag 00001
  // newline_after_start_tag  00010
  // block_level_start_tag    00011
  // newline_before_end_tag   00100
  // newline_after_end_tag    01000
  // block_level_end_tag      01100
  // block_level_layout       01111
  // pre_layout               10000

  enum formatter_integer_option {max_line_length, nesting_indent};
  enum formatter_boolean_option { attributes_double_quoted, attributes_spaced};
  enum formatter_string_option {};
}

template<typename DT, typename CT>
class Xport::xhtml_formatter
{
public:
  explicit xhtml_formatter(const std::basic_string<char>& filename);
  explicit xhtml_formatter(std::basic_ostream<CT>& out);
  ~xhtml_formatter();
private:
   xhtml_formatter(const xhtml_formatter<DT, CT>& src);

public:
  // public interface
  bool operator !() const { return output->fail(); }
  xhtml_formatter<DT, CT>& operator << (const xhtml_doc<DT, CT>& doc) { doc.write(*this); return *this; }
  xhtml_formatter<DT, CT>& operator << (const xhtml_markup<DT, CT>& mkup) { mkup.write(*this); return *this; }
  void layout_style(xhtml_tag_enum tg, tag_layout_style tls) { tag_layout_styles[tg] = tls; }
  tag_layout_style layout_style(xhtml_tag_enum tg) const { return tag_layout_styles[tg]; }
  // get option
  int option(formatter_integer_option opt) const;
  bool option(formatter_boolean_option opt) const;
  std::basic_string<CT> option(formatter_string_option opt) const;
  encoding option(formatter_encoding_option opt) const;
  // set option
  bool option(formatter_integer_option opt, int value);
  bool option(formatter_boolean_option opt, bool value);
  bool option(formatter_string_option opt, const std::basic_string<CT>& value);
  bool option(formatter_encoding_option opt, encoding enc);

private:
  // implementation
  enum output_type { file, stream };
  bool empty_tag(const xhtml_element<DT, CT>* pElem) const { return pElem->empty_tag(); }
  std::basic_string<CT> end_tag(const xhtml_element<DT, CT>* pElem) const { return pElem->end_tag(); }
  void get_comment_xhtml(const std::basic_string<CT>& data) const;
  void get_doctype_decl_xhtml(const xhtml_doctype_declaration<DT, CT>* _decl) const;
  std::basic_string<CT> get_element_attributes(const xhtml_element<DT, CT>* _pElement, const std::map<attribute::xhtml_attribute, std::basic_string<CT> >& attributes) const;
  std::basic_string<CT> get_element_styles(const xhtml_element<DT, CT>* _pElement, const std::map<css::css_property, std::basic_string<CT> >& styles) const;
  void get_element_xhtml(const xhtml_element<DT, CT>* _element, const std::map<attribute::xhtml_attribute, std::basic_string<CT> >& attributes, const std::map<css::css_property, std::basic_string<CT> >& styles) const;
  void get_pcdata_xhtml(const xhtml_pcdata<DT, CT>* _pcdata, const std::basic_string<CT>& data) const;
  void get_proc_instr_xhtml(const std::basic_string<CT>& data) const;
  std::basic_string<CT> get_tag_xhtml(const xhtml_element<DT, CT>* _pElement) const;
  tag_layout_style layout_style(const xhtml_markup<DT, CT>& mkup) const;
  void reset_line_length() const { cur_line_len = 0; }
  void process_bom(xhtml_markup<DT, CT>* pRoot) const;

  // data
  output_type out_type;
  int nesting_ind;
  mutable short indent_lvl;
  size_t max_line_len;
  mutable size_t cur_line_len;
  std::vector<tag_layout_style> tag_layout_styles;
  mutable std::basic_ostream<CT>* output;
  bool attrib_double_quoted;
  bool attrib_spaced;
  encoding file_encoding;

  // friends
  #if defined(_MSC_VER) && _MSC_VER < 1300
    friend class xhtml_element<DT, CT>;
    friend class xhtml_pcdata<DT, CT>;
    friend class xhtml_comment<DT, CT>;
    friend class xhtml_processing_instruction<DT, CT>;
    friend class xhtml_doctype_declaration<DT, CT>;
    friend class xhtml_doc<DT, CT>;
  #else
    template<typename T, typename U> friend class xhtml_element;
    template<typename T, typename U> friend class xhtml_pcdata;
    template<typename T, typename U> friend class xhtml_comment;
    template<typename T, typename U> friend class xhtml_processing_instruction;
    template<typename T, typename U> friend class xhtml_doctype_declaration;
    template<typename T, typename U> friend class xhtml_doc;
  #endif
};


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

Share

About the Author

Mitchel Haas
Software Developer Datasoft Solutions
United States United States
I'm a c++ programmer in the midwest, now using VC7 at work and at home. I enjoy creating generic libraries, and template based programming.
 
I also enjoy web development (xhtml, css, javascript, php).

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 4 May 2008
Article Copyright 2008 by Mitchel Haas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid