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