arabica/include/XML/escaper.hpp

115 lines
3.2 KiB
C++
Raw Normal View History

2005-12-14 15:49:57 +01:00
#ifndef ARABICA_UTILS_ESCAPER_HPP
#define ARABICA_UTILS_ESCAPER_HPP
#include <iostream>
#include <text/UnicodeCharacters.hpp>
2005-12-14 15:49:57 +01:00
namespace Arabica {
namespace XML {
template<typename char_type, typename traits_type = std::char_traits<char_type> >
2007-07-19 19:01:57 +02:00
class text_escaper
2005-12-14 15:49:57 +01:00
{
private:
typedef char_type charT;
typedef traits_type traitsT;
typedef std::basic_ostream<charT, traitsT> ostreamT;
typedef Arabica::text::Unicode<charT> UnicodeT;
2005-12-14 15:49:57 +01:00
public:
2007-07-19 19:01:57 +02:00
text_escaper(ostreamT& stream) : stream_(stream) { }
2005-12-14 15:49:57 +01:00
void operator()(charT ch)
{
if(ch == UnicodeT::LESS_THAN_SIGN)
{
2005-12-14 15:49:57 +01:00
stream_ << UnicodeT::AMPERSAND
<< UnicodeT::LOWERCASE_L
<< UnicodeT::LOWERCASE_T
<< UnicodeT::SEMI_COLON;
return;
} // if(ch == UnicodeT::LESS_THAN_SIGN)
if(ch == UnicodeT::GREATER_THAN_SIGN)
{
stream_ << UnicodeT::AMPERSAND
<< UnicodeT::LOWERCASE_G
<< UnicodeT::LOWERCASE_T
<< UnicodeT::SEMI_COLON;
return;
} // if(ch == UnicodeT::GREATER_THAN_SIGN)
2005-12-14 15:49:57 +01:00
if(ch == UnicodeT::AMPERSAND)
{
2005-12-14 15:49:57 +01:00
stream_ << UnicodeT::AMPERSAND
<< UnicodeT::LOWERCASE_A
<< UnicodeT::LOWERCASE_M
<< UnicodeT::LOWERCASE_P
<< UnicodeT::SEMI_COLON;
return;
} // if(ch == case UnicodeT::AMPERSAND)
2007-07-19 19:01:57 +02:00
stream_ << ch;
} // operator()
protected:
ostreamT& stream_;
}; // text_escaper
template<typename char_type, typename traits_type = std::char_traits<char_type> >
class attribute_escaper : private text_escaper<char_type, traits_type>
{
private:
typedef char_type charT;
typedef traits_type traitsT;
typedef std::basic_ostream<charT, traitsT> ostreamT;
typedef Arabica::text::Unicode<charT> UnicodeT;
2007-07-19 19:01:57 +02:00
public:
attribute_escaper(ostreamT& stream) : text_escaper<char_type, traits_type>(stream) { }
void operator()(charT ch)
{
2005-12-14 15:49:57 +01:00
if(ch == UnicodeT::QUOTATION_MARK)
2007-07-19 19:01:57 +02:00
{
this->stream_ << UnicodeT::AMPERSAND
2005-12-14 15:49:57 +01:00
<< UnicodeT::LOWERCASE_Q
<< UnicodeT::LOWERCASE_U
<< UnicodeT::LOWERCASE_O
<< UnicodeT::LOWERCASE_T
<< UnicodeT::SEMI_COLON;
return;
2007-07-19 19:01:57 +02:00
} // if(ch == UnicodeT::QUOTATION_MARK)
if(ch == UnicodeT::HORIZONTAL_TABULATION)
{
this->stream_ << UnicodeT::AMPERSAND
<< UnicodeT::NUMBER_SIGN
<< UnicodeT::LOWERCASE_X
<< UnicodeT::NUMBER_9
<< UnicodeT::SEMI_COLON;
return;
} // if(ch == UnicodeT::HORIZONTAL_TABULATION)
if(ch == UnicodeT::LINE_FEED)
{
this->stream_ << UnicodeT::AMPERSAND
<< UnicodeT::NUMBER_SIGN
<< UnicodeT::LOWERCASE_X
<< UnicodeT::CAPITAL_A
<< UnicodeT::SEMI_COLON;
return;
} // if(ch == UnicodeT::LINE_FEED)
if(ch == UnicodeT::CARRIAGE_RETURN)
{
this->stream_ << UnicodeT::AMPERSAND
<< UnicodeT::NUMBER_SIGN
<< UnicodeT::LOWERCASE_X
<< UnicodeT::CAPITAL_D
<< UnicodeT::SEMI_COLON;
return;
} // if(ch == UnicodeT::CARRIAGE_RETURN)
2005-12-14 15:49:57 +01:00
2007-07-19 19:01:57 +02:00
text_escaper<char_type, traits_type>::operator()(ch);
2005-12-14 15:49:57 +01:00
} // operator()
2007-07-19 19:01:57 +02:00
}; // attribute_escaper
2005-12-14 15:49:57 +01:00
} // namespace XML
} // namespace Arabica
2006-01-05 17:30:31 +01:00
#endif // ARABICA_UTILS_ESCAPER_HPP