htmlpars.h 9.6 KB

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