From 071e62b2ddf208e6663bfa9514ea4fcde754b24d Mon Sep 17 00:00:00 2001 From: jez_higgins <> Date: Wed, 14 Dec 2005 14:49:57 +0000 Subject: [PATCH] Factored out the XML escaping stuff --- DOM/Utils/Stream.h | 64 ++++-------------------------------------- SAX/SAX.vcproj | 7 +++-- SAX/filter/Writer.h | 62 +++-------------------------------------- XML/escaper.hpp | 68 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 119 deletions(-) create mode 100644 XML/escaper.hpp diff --git a/DOM/Utils/Stream.h b/DOM/Utils/Stream.h index 4e8cd030..e2182e4d 100644 --- a/DOM/Utils/Stream.h +++ b/DOM/Utils/Stream.h @@ -5,7 +5,7 @@ // DOM/Utils/Stream.h // // Written by Jez Higgins -// Copyright 2003 Jez UK Ltd, http://www.jezuk.co.uk +// Copyright 2003-2005 Jez UK Ltd, http://www.jezuk.co.uk // // Provides streaming operator<< for DOM::Nodes. Fully parameterised so // will work with wide and narrow char types, so long as an operator<< @@ -27,67 +27,13 @@ #include #include #include +#include namespace DOM { namespace StreamImpl { - template - class escaper - { - private: - typedef char_type charT; - typedef traits_type traitsT; - typedef std::basic_ostream ostreamT; - typedef Arabica::Unicode UnicodeT; - - public: - escaper(ostreamT& stream) : stream_(stream) { } - void operator()(charT ch) - { - if(ch == UnicodeT::LESS_THAN_SIGN) - { - 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) - if(ch == UnicodeT::AMPERSAND) - { - stream_ << UnicodeT::AMPERSAND - << UnicodeT::LOWERCASE_A - << UnicodeT::LOWERCASE_M - << UnicodeT::LOWERCASE_P - << UnicodeT::SEMI_COLON; - return; - } // if(ch == case UnicodeT::AMPERSAND) - if(ch == UnicodeT::QUOTATION_MARK) - { - stream_ << UnicodeT::AMPERSAND - << UnicodeT::LOWERCASE_Q - << UnicodeT::LOWERCASE_U - << UnicodeT::LOWERCASE_O - << UnicodeT::LOWERCASE_T - << UnicodeT::SEMI_COLON; - return; - } // if(ch == UnicodeT::QUOTATION_MARK) - stream_ << ch; - } // operator() - - private: - ostreamT& stream_; - }; // escaper - template void streamChildren(std::basic_ostream& stream, DOM::Node& node) { @@ -195,7 +141,7 @@ int prefix_mapper(std::basic_ostream& stream, stream << UnicodeT::EQUALS_SIGN << UnicodeT::QUOTATION_MARK; stringT value = attr.getNodeValue(); - std::for_each(value.begin(), value.end(), StreamImpl::escaper(stream)); + std::for_each(value.begin(), value.end(), Arabica::XML::escaper(stream)); stream << UnicodeT::QUOTATION_MARK; } @@ -212,7 +158,7 @@ int prefix_mapper(std::basic_ostream& stream, if(!(i->second.empty())) stream << UnicodeT::COLON << i->second; stream << UnicodeT::EQUALS_SIGN << UnicodeT::QUOTATION_MARK; - std::for_each(i->first.begin(), i->first.end(), StreamImpl::escaper(stream)); + std::for_each(i->first.begin(), i->first.end(), Arabica::XML::escaper(stream)); stream << UnicodeT::QUOTATION_MARK; } // for ... @@ -285,7 +231,7 @@ operator<<(std::basic_ostream& stream, case DOM::Node::TEXT_NODE: { stringT value = node.getNodeValue(); - std::for_each(value.begin(), value.end(), StreamImpl::escaper(stream)); + std::for_each(value.begin(), value.end(), Arabica::XML::escaper(stream)); } break; case DOM::Node::ENTITY_REFERENCE_NODE: diff --git a/SAX/SAX.vcproj b/SAX/SAX.vcproj index 9accd77c..1da0a288 100644 --- a/SAX/SAX.vcproj +++ b/SAX/SAX.vcproj @@ -362,6 +362,9 @@ + + @@ -754,8 +757,8 @@ Name="Debug|Win32"> diff --git a/SAX/filter/Writer.h b/SAX/filter/Writer.h index 1d587df4..78748cf8 100644 --- a/SAX/filter/Writer.h +++ b/SAX/filter/Writer.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -30,6 +31,7 @@ class basic_Writer : public basic_XMLFilterImpl, typedef SAX::basic_LexicalHandler lexicalHandlerT; typedef typename basic_XMLFilterImpl::AttributesT AttributesT; typedef Arabica::Unicode UnicodeT; + typedef Arabica::XML::escaper escaperT; private: typedef basic_LexicalHandler LexicalHandlerT; typedef basic_DeclHandler DeclHandlerT; @@ -166,62 +168,6 @@ private: enum { startTag, endTag, docTag } lastTag_; const SAX::PropertyNames properties_; - template - class escaper - { - private: - typedef char_type charT; - typedef traits_type traitsT; - typedef std::basic_ostream ostreamT; - typedef Arabica::Unicode UnicodeT; - - public: - escaper(ostreamT* stream) : stream_(stream) { } - void operator()(charT ch) - { - if(ch == UnicodeT::LESS_THAN_SIGN) - { - *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) - if(ch == UnicodeT::AMPERSAND) - { - *stream_ << UnicodeT::AMPERSAND - << UnicodeT::LOWERCASE_A - << UnicodeT::LOWERCASE_M - << UnicodeT::LOWERCASE_P - << UnicodeT::SEMI_COLON; - return; - } // if(ch == case UnicodeT::AMPERSAND) - if(ch == UnicodeT::QUOTATION_MARK) - { - *stream_ << UnicodeT::AMPERSAND - << UnicodeT::LOWERCASE_Q - << UnicodeT::LOWERCASE_U - << UnicodeT::LOWERCASE_O - << UnicodeT::LOWERCASE_T - << UnicodeT::SEMI_COLON; - return; - } // if(ch == UnicodeT::QUOTATION_MARK) - - *stream_ << ch; - } // operator() - - private: - ostreamT* stream_; - }; // escaper - }; // class basic_Writer template @@ -298,7 +244,7 @@ void basic_Writer::startElement( << UnicodeT::EQUALS_SIGN << UnicodeT::QUOTATION_MARK; stringT value = atts.getValue(i); - std::for_each(value.begin(), value.end(), escaper(stream_)); + std::for_each(value.begin(), value.end(), escaperT(*stream_)); *stream_ << UnicodeT::QUOTATION_MARK; } @@ -332,7 +278,7 @@ template void basic_Writer::characters(const stringT& ch) { if(!inCDATA_) - std::for_each(ch.begin(), ch.end(), escaper(stream_)); + std::for_each(ch.begin(), ch.end(), escaperT(*stream_)); else *stream_ << ch; diff --git a/XML/escaper.hpp b/XML/escaper.hpp new file mode 100644 index 00000000..61dbc9df --- /dev/null +++ b/XML/escaper.hpp @@ -0,0 +1,68 @@ +#ifndef ARABICA_UTILS_ESCAPER_HPP +#define ARABICA_UTILS_ESCAPER_HPP + +#include +#include + +namespace Arabica { +namespace XML { + +template > +class escaper +{ + private: + typedef char_type charT; + typedef traits_type traitsT; + typedef std::basic_ostream ostreamT; + typedef Arabica::Unicode UnicodeT; + + public: + escaper(ostreamT& stream) : stream_(stream) { } + void operator()(charT ch) + { + if(ch == UnicodeT::LESS_THAN_SIGN) + { + 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) + if(ch == UnicodeT::AMPERSAND) + { + stream_ << UnicodeT::AMPERSAND + << UnicodeT::LOWERCASE_A + << UnicodeT::LOWERCASE_M + << UnicodeT::LOWERCASE_P + << UnicodeT::SEMI_COLON; + return; + } // if(ch == case UnicodeT::AMPERSAND) + if(ch == UnicodeT::QUOTATION_MARK) + { + stream_ << UnicodeT::AMPERSAND + << UnicodeT::LOWERCASE_Q + << UnicodeT::LOWERCASE_U + << UnicodeT::LOWERCASE_O + << UnicodeT::LOWERCASE_T + << UnicodeT::SEMI_COLON; + return; + } // if(ch == UnicodeT::QUOTATION_MARK) + + stream_ << ch; + } // operator() + + private: + ostreamT& stream_; +}; // escaper + +} // namespace XML +} // namespace Arabica +#endif // ARABICA_UTILS_ESCAPER_HPP \ No newline at end of file