Click here to Skip to main content
15,884,986 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 "stylesheet_formatter.h"
#include "stylesheet_iterator.h"
#include <vector>
#include <iostream>
#include <string>

namespace Xport
{
  template<typename T, typename U> class xhtml_stylesheet;
  
  typedef xhtml_stylesheet<char, xhtml_stylesheet_item<char> >            stylesheet;
  typedef xhtml_stylesheet<wchar_t, xhtml_stylesheet_item<wchar_t> >         wstylesheet;

  typedef xhtml_stylesheet_item<char>       stylesheet_item;
  typedef xhtml_stylesheet_item<wchar_t>    wstylesheet_item;

  typedef xhtml_stylesheet_rule<char, xhtml_stylesheet_item<char> >       stylesheet_rule;
  typedef xhtml_stylesheet_rule<wchar_t, xhtml_stylesheet_item<wchar_t> >    wstylesheet_rule;

  typedef xhtml_stylesheet_import<char, xhtml_stylesheet_item<char> >     stylesheet_import;
  typedef xhtml_stylesheet_import<wchar_t, xhtml_stylesheet_item<wchar_t> >  wstylesheet_import;

  typedef xhtml_stylesheet_comment<char, xhtml_stylesheet_item<char> >    stylesheet_comment;
  typedef xhtml_stylesheet_comment<wchar_t, xhtml_stylesheet_item<wchar_t> > wstylesheet_comment;

  typedef stylesheet_declaration<char>      declaration;
  typedef stylesheet_declaration<wchar_t>   wdeclaration;

  typedef xhtml_stylesheet_formatter<char, xhtml_stylesheet_item<char> >      stylesheet_formatter;
  typedef xhtml_stylesheet_formatter<wchar_t, xhtml_stylesheet_item<wchar_t> >   wstylesheet_formatter;

  template<typename T, typename U> bool operator == (const xhtml_stylesheet<T, U>& lhs, const xhtml_stylesheet<T, U>& rhs) 
  { typename xhtml_stylesheet<T, U>::const_iterator it1 = lhs.begin(), it2 = rhs.begin(); 
  while (it1 != lhs.end() && it2 != rhs.end()) 
    if (*it1++ != *it2++) 
      return false; 
  return true; 
  }
  template<typename T, typename U> bool operator != (const xhtml_stylesheet<T, U>& lhs, const xhtml_stylesheet<T, U>& rhs) { return !(lhs == rhs); }
}

template<typename CT, typename SBT>
class Xport::xhtml_stylesheet
{
public:
  xhtml_stylesheet() {}
  xhtml_stylesheet(const xhtml_stylesheet& src) { const_iterator it = src.begin(); while (it != src.end()) push_back(*(it++)->clone()); }
  virtual ~xhtml_stylesheet() { clear(); }

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

  // iterators
  typedef stylesheet_iterator<CT, SBT, SBT*, SBT&>             iterator;
  typedef stylesheet_iterator<CT, SBT, const SBT*, const SBT&> const_iterator;

public:
  // public interface
  iterator insert(const SBT& item) { return iterator(items.insert(items.end(), item.clone())); }
  iterator insert(const const_iterator& pos, const SBT& item);
  void push_back(const SBT& item) { items.push_back(item.clone()); }
  xhtml_stylesheet& operator << (const SBT& item) { items.push_back(item.clone()); return *this; }
  xhtml_stylesheet& operator = (const xhtml_stylesheet& src) { if (&src == this) return *this; const_iterator it = src.begin(); while (it != src.end()) push_back(*(it++)->clone()); return *this; }

  iterator erase(const const_iterator& pos); 
  iterator erase(const const_iterator& beg_pos, const const_iterator& end_pos);
  bool erase(const std::basic_string<CT>& selector);
  SBT& operator[] (size_type n) { return *items[n]; }
  const SBT& operator[](size_type n) const { return *items[n]; }
  SBT& at(size_type n) { return *items.at(n); }
  const SBT& at(size_type n) const { return *items.at(n); }

  bool empty() const { return items.empty(); }
  void clear();
  bool write(const xhtml_stylesheet_formatter<CT, SBT>& fmtr) const;
  bool write(const std::basic_string<CT>& path) const;
  bool parse(std::basic_istream<CT>& strm);
  bool parse(const std::basic_string<CT>& filename);
  size_type size() const { return items.size(); }

  // iterator operations
  iterator begin() { return iterator(items.begin()); }
  const_iterator begin() const { return const_iterator(items.begin()); }
  iterator end() { return iterator(items.end()); }
  const_iterator end() const { return const_iterator(items.end()); }

protected:
  // implementation
  bool parse_input(std::basic_istream<CT>** ppInput);
  void parse_import(std::basic_istream<CT>& strm);
  void parse_rule(std::basic_istream<CT>& strm);
  void parse_comment(std::basic_istream<CT>& strm);
  std::basic_string<CT> parse_selector(std::basic_istream<CT>& strm);
  void parse_declarations(std::basic_istream<CT>& strm, xhtml_stylesheet_rule<CT, SBT>* pRule);
  css::css_property parse_css_property(std::basic_istream<CT>& strm);
  std::basic_string<CT> parse_decl_value(std::basic_istream<CT>& strm);

  // data
  std::vector<SBT*> items;

  // friends
  #if defined(_MSC_VER) && _MSC_VER < 1300
    friend bool operator == (const xhtml_stylesheet& lhs, const xhtml_stylesheet& rhs);
  #else
    friend bool operator ==<> (const xhtml_stylesheet& lhs, const xhtml_stylesheet& rhs);
  #endif
};

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