htmlpars.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: html/htmlpars.h
  3. // Purpose: interface of wxHtmlTagHandler
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. enum wxHtmlURLType
  8. {
  9. wxHTML_URL_PAGE,
  10. wxHTML_URL_IMAGE,
  11. wxHTML_URL_OTHER
  12. };
  13. /**
  14. @class wxHtmlTagHandler
  15. @todo describe me
  16. @library{wxhtml}
  17. @category{html}
  18. @see @ref overview_html_handlers, wxHtmlTag
  19. */
  20. class wxHtmlTagHandler : public wxObject
  21. {
  22. public:
  23. /**
  24. Constructor.
  25. */
  26. wxHtmlTagHandler();
  27. /**
  28. Returns list of supported tags.
  29. The list is in uppercase and tags are delimited by ','.
  30. Example: @c "I,B,FONT,P"
  31. */
  32. virtual wxString GetSupportedTags() = 0;
  33. /**
  34. This is the core method of each handler. It is called each time
  35. one of supported tags is detected. @a tag contains all necessary
  36. info (see wxHtmlTag for details).
  37. Example:
  38. @code
  39. bool MyHandler::HandleTag(const wxHtmlTag& tag)
  40. {
  41. ...
  42. // change state of parser (e.g. set bold face)
  43. ParseInner(tag);
  44. ...
  45. // restore original state of parser
  46. }
  47. @endcode
  48. You shouldn't call ParseInner() if the tag is not paired with an ending one.
  49. @return @true if ParseInner() was called, @false otherwise.
  50. */
  51. virtual bool HandleTag(const wxHtmlTag& tag) = 0;
  52. /**
  53. Assigns @a parser to this handler. Each @b instance of handler
  54. is guaranteed to be called only from the one parser.
  55. */
  56. virtual void SetParser(wxHtmlParser* parser);
  57. /**
  58. Returns the parser associated with this tag handler.
  59. @since 2.9.5
  60. */
  61. wxHtmlParser* GetParser() const;
  62. protected:
  63. /**
  64. This method calls parser's wxHtmlParser::DoParsing method
  65. for the string between this tag and the paired ending tag:
  66. @code
  67. ...<A HREF="x.htm">Hello, world!</A>...
  68. @endcode
  69. In this example, a call to ParseInner() (with @a tag pointing to A tag)
  70. will parse 'Hello, world!'.
  71. */
  72. void ParseInner(const wxHtmlTag& tag);
  73. /**
  74. Parses given source as if it was tag's inner code (see
  75. wxHtmlParser::GetInnerSource). Unlike ParseInner(), this method lets
  76. you specify the source code to parse. This is useful when you need to
  77. modify the inner text before parsing.
  78. */
  79. void ParseInnerSource(const wxString& source);
  80. /**
  81. This attribute is used to access parent parser. It is protected so that
  82. it can't be accessed by user but can be accessed from derived classes.
  83. */
  84. wxHtmlParser* m_Parser;
  85. };
  86. /**
  87. @class wxHtmlParser
  88. Classes derived from this handle the @b generic parsing of HTML documents: it
  89. scans the document and divide it into blocks of tags (where one block consists
  90. of beginning and ending tag and of text between these two tags).
  91. It is independent from wxHtmlWindow and can be used as stand-alone parser.
  92. It uses system of tag handlers to parse the HTML document. Tag handlers
  93. are not statically shared by all instances but are created for each
  94. wxHtmlParser instance. The reason is that the handler may contain
  95. document-specific temporary data used during parsing (e.g. complicated
  96. structures like tables).
  97. Typically the user calls only the wxHtmlParser::Parse method.
  98. @library{wxhtml}
  99. @category{html}
  100. @see @ref overview_html_cells, @ref overview_html_handlers, wxHtmlTag
  101. */
  102. class wxHtmlParser
  103. {
  104. public:
  105. /**
  106. Constructor.
  107. */
  108. wxHtmlParser();
  109. /**
  110. Adds handler to the internal list ( hash table) of handlers.
  111. This method should not be called directly by user but rather by derived class'
  112. constructor.
  113. This adds the handler to this @b instance of wxHtmlParser, not to
  114. all objects of this class!
  115. (Static front-end to AddTagHandler is provided by wxHtmlWinParser).
  116. All handlers are deleted on object deletion.
  117. */
  118. virtual void AddTagHandler(wxHtmlTagHandler* handler);
  119. /**
  120. Must be overwritten in derived class.
  121. This method is called by DoParsing() each time a part of text is parsed.
  122. @a txt is NOT only one word, it is substring of input.
  123. It is not formatted or preprocessed (so white spaces are unmodified).
  124. */
  125. virtual void AddWord(const wxString& txt);
  126. /**
  127. Parses the m_Source from @a begin_pos to @a end_pos - 1.
  128. */
  129. void DoParsing(const const_iterator& begin_pos, const const_iterator& end_pos);
  130. /**
  131. Parses the whole m_Source.
  132. */
  133. void DoParsing();
  134. /**
  135. This must be called after DoParsing().
  136. */
  137. virtual void DoneParser();
  138. /**
  139. Returns pointer to the file system. Because each tag handler has
  140. reference to it is parent parser it can easily request the file by
  141. calling:
  142. @code
  143. wxFSFile *f = m_Parser -> GetFS() -> OpenFile("image.jpg");
  144. @endcode
  145. */
  146. wxFileSystem* GetFS() const;
  147. /**
  148. Returns product of parsing.
  149. Returned value is result of parsing of the document.
  150. The type of this result depends on internal representation in derived
  151. parser (but it must be derived from wxObject!).
  152. See wxHtmlWinParser for details.
  153. */
  154. virtual wxObject* GetProduct() = 0;
  155. /**
  156. Returns pointer to the source being parsed.
  157. */
  158. const wxString* GetSource();
  159. /**
  160. Setups the parser for parsing the @a source string.
  161. (Should be overridden in derived class)
  162. */
  163. virtual void InitParser(const wxString& source);
  164. /**
  165. Opens given URL and returns @c wxFSFile object that can be used to read data
  166. from it. This method may return @NULL in one of two cases: either the URL doesn't
  167. point to any valid resource or the URL is blocked by overridden implementation
  168. of @e OpenURL in derived class.
  169. @param type
  170. Indicates type of the resource. Is one of:
  171. - wxHTML_URL_PAGE: Opening a HTML page.
  172. - wxHTML_URL_IMAGE: Opening an image.
  173. - wxHTML_URL_OTHER: Opening a resource that doesn't fall into
  174. any other category.
  175. @param url
  176. URL being opened.
  177. @note
  178. Always use this method in tag handlers instead of GetFS()->OpenFile()
  179. because it can block the URL and is thus more secure.
  180. Default behaviour is to call wxHtmlWindow::OnOpeningURL of the associated
  181. wxHtmlWindow object (which may decide to block the URL or redirect it to
  182. another one),if there's any, and always open the URL if the parser is not
  183. used with wxHtmlWindow.
  184. Returned wxFSFile object is not guaranteed to point to url, it might have
  185. been redirected!
  186. */
  187. virtual wxFSFile* OpenURL(wxHtmlURLType type, const wxString& url) const;
  188. /**
  189. Proceeds parsing of the document. This is end-user method. You can simply
  190. call it when you need to obtain parsed output (which is parser-specific).
  191. The method does these things:
  192. -# calls InitParser(source)
  193. -# calls DoParsing()
  194. -# calls GetProduct()
  195. -# calls DoneParser()
  196. -# returns value returned by GetProduct()
  197. You shouldn't use InitParser(), DoParsing(), GetProduct() or DoneParser() directly.
  198. */
  199. wxObject* Parse(const wxString& source);
  200. /**
  201. Restores parser's state before last call to PushTagHandler().
  202. */
  203. void PopTagHandler();
  204. /**
  205. Forces the handler to handle additional tags
  206. (not returned by wxHtmlTagHandler::GetSupportedTags).
  207. The handler should already be added to this parser.
  208. @param handler
  209. the handler
  210. @param tags
  211. List of tags (in same format as GetSupportedTags()'s return value).
  212. The parser will redirect these tags to handler (until call to PopTagHandler()).
  213. Example:
  214. Imagine you want to parse following pseudo-html structure:
  215. @code
  216. <myitems>
  217. <param name="one" value="1">
  218. <param name="two" value="2">
  219. </myitems>
  220. <execute>
  221. <param program="text.exe">
  222. </execute>
  223. @endcode
  224. It is obvious that you cannot use only one tag handler for \<param\> tag.
  225. Instead you must use context-sensitive handlers for \<param\> inside \<myitems\>
  226. and \<param\> inside \<execute\>.
  227. This is the preferred solution:
  228. @code
  229. TAG_HANDLER_BEGIN(MYITEM, "MYITEMS")
  230. TAG_HANDLER_PROC(tag)
  231. {
  232. // ...something...
  233. m_Parser -> PushTagHandler(this, "PARAM");
  234. ParseInner(tag);
  235. m_Parser -> PopTagHandler();
  236. // ...something...
  237. }
  238. TAG_HANDLER_END(MYITEM)
  239. @endcode
  240. */
  241. void PushTagHandler(wxHtmlTagHandler* handler,
  242. const wxString& tags);
  243. /**
  244. Sets the virtual file system that will be used to request additional files.
  245. (For example @c IMG tag handler requests wxFSFile with the image data.)
  246. */
  247. void SetFS(wxFileSystem* fs);
  248. /**
  249. Call this function to interrupt parsing from a tag handler.
  250. No more tags will be parsed afterward. This function may only be called
  251. from Parse() or any function called by it (i.e. from tag handlers).
  252. */
  253. virtual void StopParsing();
  254. protected:
  255. /**
  256. This may (and may not) be overwritten in derived class.
  257. This method is called each time new tag is about to be added.
  258. @a tag contains information about the tag. (See wxHtmlTag for details.)
  259. Default (wxHtmlParser) behaviour is this: first it finds a handler capable
  260. of handling this tag and then it calls handler's HandleTag() method.
  261. */
  262. virtual void AddTag(const wxHtmlTag& tag);
  263. };