htmlpars.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/html/htmlpars.h
  3. // Purpose: wxHtmlParser class (generic parser)
  4. // Author: Vaclav Slavik
  5. // Copyright: (c) 1999 Vaclav Slavik
  6. // Licence: wxWindows licence
  7. /////////////////////////////////////////////////////////////////////////////
  8. #ifndef _WX_HTMLPARS_H_
  9. #define _WX_HTMLPARS_H_
  10. #include "wx/defs.h"
  11. #if wxUSE_HTML
  12. #include "wx/html/htmltag.h"
  13. #include "wx/filesys.h"
  14. #include "wx/hashmap.h"
  15. #include "wx/hashset.h"
  16. #include "wx/vector.h"
  17. #include "wx/fontenc.h"
  18. class WXDLLIMPEXP_FWD_BASE wxMBConv;
  19. class WXDLLIMPEXP_FWD_HTML wxHtmlParser;
  20. class WXDLLIMPEXP_FWD_HTML wxHtmlTagHandler;
  21. class WXDLLIMPEXP_FWD_HTML wxHtmlEntitiesParser;
  22. class wxHtmlTextPieces;
  23. class wxHtmlParserState;
  24. WX_DECLARE_HASH_SET_WITH_DECL_PTR(wxHtmlTagHandler*,
  25. wxPointerHash, wxPointerEqual,
  26. wxHtmlTagHandlersSet,
  27. class WXDLLIMPEXP_HTML);
  28. WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxHtmlTagHandler*,
  29. wxHtmlTagHandlersHash,
  30. class WXDLLIMPEXP_HTML);
  31. enum wxHtmlURLType
  32. {
  33. wxHTML_URL_PAGE,
  34. wxHTML_URL_IMAGE,
  35. wxHTML_URL_OTHER
  36. };
  37. // This class handles generic parsing of HTML document : it scans
  38. // the document and divides it into blocks of tags (where one block
  39. // consists of starting and ending tag and of text between these
  40. // 2 tags.
  41. class WXDLLIMPEXP_HTML wxHtmlParser : public wxObject
  42. {
  43. DECLARE_ABSTRACT_CLASS(wxHtmlParser)
  44. public:
  45. wxHtmlParser();
  46. virtual ~wxHtmlParser();
  47. // Sets the class which will be used for opening files
  48. void SetFS(wxFileSystem *fs) { m_FS = fs; }
  49. wxFileSystem* GetFS() const { return m_FS; }
  50. // Opens file if the parser is allowed to open given URL (may be forbidden
  51. // for security reasons)
  52. virtual wxFSFile *OpenURL(wxHtmlURLType type, const wxString& url) const;
  53. // You can simply call this method when you need parsed output.
  54. // This method does these things:
  55. // 1. call InitParser(source);
  56. // 2. call DoParsing();
  57. // 3. call GetProduct(); (its return value is then returned)
  58. // 4. call DoneParser();
  59. wxObject* Parse(const wxString& source);
  60. // Sets the source. This must be called before running Parse() method.
  61. virtual void InitParser(const wxString& source);
  62. // This must be called after Parse().
  63. virtual void DoneParser();
  64. // May be called during parsing to immediately return from Parse().
  65. virtual void StopParsing() { m_stopParsing = true; }
  66. // Parses the m_Source from begin_pos to end_pos-1.
  67. // (in noparams version it parses whole m_Source)
  68. void DoParsing(const wxString::const_iterator& begin_pos,
  69. const wxString::const_iterator& end_pos);
  70. void DoParsing();
  71. // Returns pointer to the tag at parser's current position
  72. wxHtmlTag *GetCurrentTag() const { return m_CurTag; }
  73. // Returns product of parsing
  74. // Returned value is result of parsing of the part. The type of this result
  75. // depends on internal representation in derived parser
  76. // (see wxHtmlWinParser for details).
  77. virtual wxObject* GetProduct() = 0;
  78. // adds handler to the list & hash table of handlers.
  79. virtual void AddTagHandler(wxHtmlTagHandler *handler);
  80. // Forces the handler to handle additional tags (not returned by GetSupportedTags).
  81. // The handler should already be in use by this parser.
  82. // Example: you want to parse following pseudo-html structure:
  83. // <myitems>
  84. // <it name="one" value="1">
  85. // <it name="two" value="2">
  86. // </myitems>
  87. // <it> This last it has different meaning, we don't want it to be parsed by myitems handler!
  88. // handler can handle only 'myitems' (e.g. its GetSupportedTags returns "MYITEMS")
  89. // you can call PushTagHandler(handler, "IT") when you find <myitems>
  90. // and call PopTagHandler() when you find </myitems>
  91. void PushTagHandler(wxHtmlTagHandler *handler, const wxString& tags);
  92. // Restores state before last call to PushTagHandler
  93. void PopTagHandler();
  94. const wxString* GetSource() {return m_Source;}
  95. void SetSource(const wxString& src);
  96. // Sets HTML source and remembers current parser's state so that it can
  97. // later be restored. This is useful for on-line modifications of
  98. // HTML source (for example, <pre> handler replaces spaces with &nbsp;
  99. // and newlines with <br>)
  100. virtual void SetSourceAndSaveState(const wxString& src);
  101. // Restores parser's state from stack or returns false if the stack is
  102. // empty
  103. virtual bool RestoreState();
  104. // Returns HTML source inside the element (i.e. between the starting
  105. // and ending tag)
  106. wxString GetInnerSource(const wxHtmlTag& tag);
  107. // Parses HTML string 'markup' and extracts charset info from <meta> tag
  108. // if present. Returns empty string if the tag is missing.
  109. // For wxHTML's internal use.
  110. static wxString ExtractCharsetInformation(const wxString& markup);
  111. // Returns entity parser object, used to substitute HTML &entities;
  112. wxHtmlEntitiesParser *GetEntitiesParser() const { return m_entitiesParser; }
  113. // Returns true if the tag starting at the given position is a comment tag
  114. //
  115. // p should point to '<' character and is modified to point to the closing
  116. // '>' of the end comment tag if this is indeed a comment
  117. static bool
  118. SkipCommentTag(wxString::const_iterator& p, wxString::const_iterator end);
  119. protected:
  120. // DOM structure
  121. void CreateDOMTree();
  122. void DestroyDOMTree();
  123. void CreateDOMSubTree(wxHtmlTag *cur,
  124. const wxString::const_iterator& begin_pos,
  125. const wxString::const_iterator& end_pos,
  126. wxHtmlTagsCache *cache);
  127. // Adds text to the output.
  128. // This is called from Parse() and must be overridden in derived classes.
  129. // txt is not guaranteed to be only one word. It is largest continuous part
  130. // of text (= not broken by tags)
  131. virtual void AddText(const wxString& txt) = 0;
  132. // Adds tag and proceeds it. Parse() may (and usually is) called from this method.
  133. // This is called from Parse() and may be overridden.
  134. // Default behaviour is that it looks for proper handler in m_Handlers. The tag is
  135. // ignored if no hander is found.
  136. // Derived class is *responsible* for filling in m_Handlers table.
  137. virtual void AddTag(const wxHtmlTag& tag);
  138. protected:
  139. // DOM tree:
  140. wxHtmlTag *m_CurTag;
  141. wxHtmlTag *m_Tags;
  142. wxHtmlTextPieces *m_TextPieces;
  143. size_t m_CurTextPiece;
  144. const wxString *m_Source;
  145. wxHtmlParserState *m_SavedStates;
  146. // handlers that handle particular tags. The table is accessed by
  147. // key = tag's name.
  148. // This attribute MUST be filled by derived class otherwise it would
  149. // be empty and no tags would be recognized
  150. // (see wxHtmlWinParser for details about filling it)
  151. // m_HandlersHash is for random access based on knowledge of tag name (BR, P, etc.)
  152. // it may (and often does) contain more references to one object
  153. // m_HandlersList is list of all handlers and it is guaranteed to contain
  154. // only one reference to each handler instance.
  155. wxHtmlTagHandlersSet m_HandlersSet;
  156. wxHtmlTagHandlersHash m_HandlersHash;
  157. wxDECLARE_NO_COPY_CLASS(wxHtmlParser);
  158. // class for opening files (file system)
  159. wxFileSystem *m_FS;
  160. // handlers stack used by PushTagHandler and PopTagHandler
  161. wxVector<wxHtmlTagHandlersHash*> m_HandlersStack;
  162. // entity parse
  163. wxHtmlEntitiesParser *m_entitiesParser;
  164. // flag indicating that the parser should stop
  165. bool m_stopParsing;
  166. };
  167. // This class (and derived classes) cooperates with wxHtmlParser.
  168. // Each recognized tag is passed to handler which is capable
  169. // of handling it. Each tag is handled in 3 steps:
  170. // 1. Handler will modifies state of parser
  171. // (using its public methods)
  172. // 2. Parser parses source between starting and ending tag
  173. // 3. Handler restores original state of the parser
  174. class WXDLLIMPEXP_HTML wxHtmlTagHandler : public wxObject
  175. {
  176. DECLARE_ABSTRACT_CLASS(wxHtmlTagHandler)
  177. public:
  178. wxHtmlTagHandler() : wxObject () { m_Parser = NULL; }
  179. // Sets the parser.
  180. // NOTE : each _instance_ of handler is guaranteed to be called
  181. // only by one parser. This means you don't have to care about
  182. // reentrancy.
  183. virtual void SetParser(wxHtmlParser *parser)
  184. { m_Parser = parser; }
  185. // Get the parser associated with this tag handler.
  186. wxHtmlParser* GetParser() const { return m_Parser; }
  187. // Returns list of supported tags. The list is in uppercase and
  188. // tags are delimited by ','.
  189. // Example : "I,B,FONT,P"
  190. // is capable of handling italic, bold, font and paragraph tags
  191. virtual wxString GetSupportedTags() = 0;
  192. // This is hadling core method. It does all the Steps 1-3.
  193. // To process step 2, you can call ParseInner()
  194. // returned value : true if it called ParseInner(),
  195. // false etherwise
  196. virtual bool HandleTag(const wxHtmlTag& tag) = 0;
  197. protected:
  198. // parses input between beginning and ending tag.
  199. // m_Parser must be set.
  200. void ParseInner(const wxHtmlTag& tag)
  201. { m_Parser->DoParsing(tag.GetBeginIter(), tag.GetEndIter1()); }
  202. // Parses given source as if it was tag's inner code (see
  203. // wxHtmlParser::GetInnerSource). Unlike ParseInner(), this method lets
  204. // you specify the source code to parse. This is useful when you need to
  205. // modify the inner text before parsing.
  206. void ParseInnerSource(const wxString& source);
  207. wxHtmlParser *m_Parser;
  208. wxDECLARE_NO_COPY_CLASS(wxHtmlTagHandler);
  209. };
  210. // This class is used to parse HTML entities in strings. It can handle
  211. // both named entities and &#xxxx entries where xxxx is Unicode code.
  212. class WXDLLIMPEXP_HTML wxHtmlEntitiesParser : public wxObject
  213. {
  214. DECLARE_DYNAMIC_CLASS(wxHtmlEntitiesParser)
  215. public:
  216. wxHtmlEntitiesParser();
  217. virtual ~wxHtmlEntitiesParser();
  218. // Sets encoding of output string.
  219. // Has no effect if wxUSE_UNICODE==1
  220. #if wxUSE_UNICODE
  221. void SetEncoding(wxFontEncoding WXUNUSED(encoding)) {}
  222. #else
  223. void SetEncoding(wxFontEncoding encoding);
  224. #endif
  225. // Parses entities in input and replaces them with respective characters
  226. // (with respect to output encoding)
  227. wxString Parse(const wxString& input) const;
  228. // Returns character for given entity or 0 if the enity is unknown
  229. wxChar GetEntityChar(const wxString& entity) const;
  230. // Returns character that represents given Unicode code
  231. #if wxUSE_UNICODE
  232. wxChar GetCharForCode(unsigned code) const { return (wxChar)code; }
  233. #else
  234. wxChar GetCharForCode(unsigned code) const;
  235. #endif
  236. protected:
  237. #if !wxUSE_UNICODE
  238. wxMBConv *m_conv;
  239. wxFontEncoding m_encoding;
  240. #endif
  241. wxDECLARE_NO_COPY_CLASS(wxHtmlEntitiesParser);
  242. };
  243. #endif
  244. #endif // _WX_HTMLPARS_H_