From 3d3fbaedcebf342d088c46eec7773dfa88011ae4 Mon Sep 17 00:00:00 2001 From: jez <> Date: Tue, 7 Aug 2007 21:29:27 +0000 Subject: [PATCH] pass through skippedEntity and processingInstruction --- include/SAX/filter/PYXWriter.hpp | 114 ++++++++++++++++++ .../SAX/filter/WhitespaceStripperFilter.hpp | 41 +++++++ include/SAX/filter/Writer.h | 4 + 3 files changed, 159 insertions(+) create mode 100644 include/SAX/filter/PYXWriter.hpp create mode 100644 include/SAX/filter/WhitespaceStripperFilter.hpp diff --git a/include/SAX/filter/PYXWriter.hpp b/include/SAX/filter/PYXWriter.hpp new file mode 100644 index 00000000..d7349748 --- /dev/null +++ b/include/SAX/filter/PYXWriter.hpp @@ -0,0 +1,114 @@ +#ifndef ARABICA_SAX_PYX_WRITER_H +#define ARABICA_SAX_PYX_WRITER_H + +#include +#include +#include +#include + +namespace SAX { + +template +class PYXWriter : public basic_XMLFilterImpl +{ + public: + typedef string_type stringT; + typedef PYXWriter PYXWriterT; + typedef typename string_type::value_type charT; + typedef typename string_type::traits_type traitsT; + typedef std::basic_ostream ostreamT; + typedef basic_XMLReader XMLReaderT; + typedef basic_XMLFilterImpl XMLFilterT; + typedef typename basic_XMLFilterImpl::AttributesT AttributesT; + typedef Arabica::Unicode UnicodeT; + private: + using XMLFilterT::getParent; + + public: + PYXWriter(ostreamT& stream) : + XMLFilterT(), + stream_(&stream) + { + } // PYXWriter + + PYXWriter(ostreamT& stream, XMLReaderT& parent) : + XMLFilterT(parent), + stream_(&stream) + { + } // PYXWriter + + public: + // ContentHandler + virtual void startElement(const stringT& namespaceURI, const stringT& localName, + const stringT& qName, const AttributesT& atts); + virtual void endElement(const stringT& namespaceURI, const stringT& localName, + const stringT& qName); + virtual void characters(const stringT& ch); + virtual void processingInstruction(const stringT& target, const stringT& data); + + private: + void escape(const stringT& ch); + + ostreamT* stream_; +}; // class PYXWriter + +template +void PYXWriter::startElement( + const stringT& namespaceURI, const stringT& localName, + const stringT& qName, const AttributesT& atts) +{ + *stream_ << UnicodeT::LEFT_PARENTHESIS << localName << std::endl; + for(int i = 0; i < atts.getLength(); ++i) + { + *stream_ << UnicodeT::CAPITAL_A << atts.getLocalName(i) + << UnicodeT::SPACE; + escape(atts.getValue(i)); + *stream_ << std::endl; + } + + XMLFilterT::startElement(namespaceURI, localName, qName, atts); +} // startElement + +template +void PYXWriter::endElement( + const stringT& namespaceURI, const stringT& localName, + const stringT& qName) +{ + *stream_ << UnicodeT::RIGHT_PARENTHESIS << localName << std::endl; + + XMLFilterT::endElement(namespaceURI, localName, qName); +} // endElement + +template +void PYXWriter::characters(const stringT& ch) +{ + *stream_ << UnicodeT::HYPHEN_MINUS; + escape(ch); + *stream_ << std::endl; + + XMLFilterT::characters(ch); +} // characters + +template +void PYXWriter::processingInstruction(const stringT& target, const stringT& data) +{ + *stream_ << UnicodeT::QUESTION_MARK << target + << UnicodeT::SPACE << data + << std::endl; + + XMLFilterT::processingInstruction(target, data); +} // processingInstruction + +template +void PYXWriter::escape(const stringT& ch) +{ + for(typename stringT::const_iterator s = ch.begin(), se = ch.end(); s != se; ++s) + if(*s == UnicodeT::LINE_FEED) + *stream_ << UnicodeT::BACK_SLASH << UnicodeT::LOWERCASE_N; + else + *stream_ << *s; +} // escape + +} // namespace SAX + +#endif diff --git a/include/SAX/filter/WhitespaceStripperFilter.hpp b/include/SAX/filter/WhitespaceStripperFilter.hpp new file mode 100644 index 00000000..7a62bea9 --- /dev/null +++ b/include/SAX/filter/WhitespaceStripperFilter.hpp @@ -0,0 +1,41 @@ +#ifndef WHITESPACE_STRIPPER_FILTER_HPP +#define WHITESPACE_STRIPPER_FILTER_HPP + +#include + +#include + +namespace SAX +{ + +template > +class WhitespaceStripper : public SAX::basic_XMLFilterImpl +{ + public: + typedef string_type stringT; + typedef SAX::basic_XMLFilterImpl baseT; + typedef SAX::basic_XMLReader XMLReaderT; + + WhitespaceStripper() : + baseT() + { + } // WhitespaceStripper + + WhitespaceStripper(XMLReaderT& parent) : + baseT(parent) + { + } // WhitespaceStripper + + virtual void characters(const stringT& ch) + { + baseT::characters(Arabica::string::normalize_whitespace(ch)); + } // characters + + virtual void ignorableWhitespace(const stringT& ch) + { + } // ignorableWhitespace +}; // class WhitespaceStripper + +} // namespace SAX +#endif +// end of file diff --git a/include/SAX/filter/Writer.h b/include/SAX/filter/Writer.h index e33f80ce..4192f7b7 100644 --- a/include/SAX/filter/Writer.h +++ b/include/SAX/filter/Writer.h @@ -286,6 +286,8 @@ void basic_Writer::processingInstruction(const stringT& target, con *stream_ << UnicodeT::QUESTION_MARK << UnicodeT::GREATER_THAN_SIGN; } + + XMLFilterT::processingInstruction(target, data); } // processingInstruction template @@ -293,6 +295,8 @@ void basic_Writer::skippedEntity(const stringT& name) { if(!isDtd(name)) *stream_ << UnicodeT::AMPERSAND << name << UnicodeT::SEMI_COLON; + + XMLFilterT::skippedEntity(name); } // skippedEntity template