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