1 /* 2 * saxfilter.h 3 * 4 * 5 * Created by Andreas Vox on 21.09.06. 6 * Copyright 2006 under GPL2. All rights reserved. 7 * 8 */ 9 10 11 12 #ifndef SAXFILTER_H 13 #define SAXFILTER_H 14 15 #include "saxhandler.h" 16 17 /** 18 Abstract super class for filtering a SAX stream. 19 */ 20 class SaxFilter : public SaxHandler { 21 public: SaxFilter(SaxHandler * delegate)22 SaxFilter(SaxHandler* delegate) : m_delegate(delegate) {} setDelegate(SaxHandler * delegate)23 void setDelegate(SaxHandler* delegate) { m_delegate = delegate; } delegate()24 SaxHandler* delegate() { return m_delegate; } 25 virtual void beginDoc(); 26 virtual void endDoc(); 27 virtual void begin(const Xml_string& tag, Xml_attr attr); 28 virtual void end(const Xml_string& tag); 29 virtual void chars(const Xml_string& text); 30 private: 31 SaxHandler* m_delegate; 32 }; 33 34 35 /** 36 * This filter will suppress the closing tag for the 37 * next opening tag. This allows to add content to 38 * the element, eg: 39 * KeepOpen keepOpenHandler( &myhandler ); 40 * myElem.saxx(keepOpenHandler); 41 * more_output (myhandler); 42 * myhandler.end(keepOpenhandler.opentag()); 43 */ 44 class KeepOpen : public SaxFilter { 45 public: 46 KeepOpen(SaxHandler* delegate); 47 virtual void begin(const Xml_string& tag, Xml_attr attr); 48 virtual void end(const Xml_string& tag); 49 Xml_string openTag(); 50 private: 51 int m_level {0}; 52 Xml_string m_tag; 53 }; 54 55 56 /** 57 * Renames the next opening(and corresponding closing) tag. 58 * If oldname is not empty, it will also rename all inner tags with name oldname 59 */ 60 class RenameElem : public SaxFilter { 61 public: 62 RenameElem(SaxHandler* delegate, const Xml_string& oldname, const Xml_string& newname); 63 RenameElem(SaxHandler* delegate, const Xml_string& newname); 64 virtual void begin(const Xml_string& tag, Xml_attr attr); 65 virtual void end(const Xml_string& tag); 66 private: 67 int m_level {0}; 68 Xml_string m_old; 69 Xml_string m_new; 70 }; 71 72 73 /** 74 * Adds more attributes to the next opening tag 75 */ 76 class AddAttributes : public SaxFilter { 77 public: 78 AddAttributes(SaxHandler* delegate, Xml_attr add); 79 virtual void begin(const Xml_string& tag, Xml_attr attr); 80 private: 81 bool m_oneshot {false}; 82 Xml_attr m_attributes; 83 }; 84 85 86 /** 87 * Can be used to suppress output to a filter, eg. 88 * SaxHandlerNull nullhandler(); 89 * myfilter.setDelegate( & nullhandler ); 90 * ... (continue parsing) 91 */ 92 class SaxHandlerNull : public SaxHandler { 93 public: SaxHandlerNull()94 SaxHandlerNull() {} beginDoc()95 virtual void beginDoc() {} endDoc()96 virtual void endDoc() {} begin(const Xml_string & tag,Xml_attr attr)97 virtual void begin(const Xml_string& tag, Xml_attr attr) {} end(const Xml_string & tag)98 virtual void end(const Xml_string& tag) {} chars(const Xml_string & text)99 virtual void chars(const Xml_string& text) {} 100 }; 101 102 103 #endif 104