]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/html/htmlpars.cpp
implemented attributes support for native virtual list control
[wxWidgets.git] / src / html / htmlpars.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: htmlpars.cpp
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#ifdef __GNUG__
12#pragma implementation
13#endif
14
15#include "wx/wxprec.h"
16
17#include "wx/defs.h"
18#if wxUSE_HTML && wxUSE_STREAMS
19
20#ifdef __BORDLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WXPRECOMP
25 #include "wx/log.h"
26 #include "wx/intl.h"
27#endif
28
29#include "wx/tokenzr.h"
30#include "wx/wfstream.h"
31#include "wx/url.h"
32#include "wx/fontmap.h"
33#include "wx/html/htmldefs.h"
34#include "wx/html/htmlpars.h"
35
36
37
38//-----------------------------------------------------------------------------
39// wxHtmlParser
40//-----------------------------------------------------------------------------
41
42IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser,wxObject)
43
44wxHtmlParser::wxHtmlParser()
45 : wxObject(), m_Cache(NULL), m_HandlersHash(wxKEY_STRING),
46 m_FS(NULL), m_HandlersStack(NULL)
47{
48 m_entitiesParser = new wxHtmlEntitiesParser;
49}
50
51wxHtmlParser::~wxHtmlParser()
52{
53 delete m_HandlersStack;
54 m_HandlersHash.Clear();
55 m_HandlersList.DeleteContents(TRUE);
56 m_HandlersList.Clear();
57 delete m_entitiesParser;
58}
59
60wxObject* wxHtmlParser::Parse(const wxString& source)
61{
62 wxObject *result;
63
64 InitParser(source);
65 DoParsing();
66 result = GetProduct();
67 DoneParser();
68 return result;
69}
70
71void wxHtmlParser::InitParser(const wxString& source)
72{
73 SetSource(source);
74}
75
76void wxHtmlParser::DoneParser()
77{
78 delete m_Cache;
79 m_Cache = NULL;
80}
81
82void wxHtmlParser::SetSource(const wxString& src)
83{
84 m_Source = src;
85 delete m_Cache;
86 m_Cache = new wxHtmlTagsCache(m_Source);
87}
88
89void wxHtmlParser::DoParsing(int begin_pos, int end_pos)
90{
91 if (end_pos <= begin_pos) return;
92
93 char c;
94 char *temp = new char[end_pos - begin_pos + 1];
95 int i;
96 int templen;
97
98 templen = 0;
99 i = begin_pos;
100
101 while (i < end_pos)
102 {
103 c = m_Source[(unsigned int) i];
104
105 // continue building word:
106 if (c != '<')
107 {
108 temp[templen++] = c;
109 i++;
110 }
111
112 else if (c == '<')
113 {
114 wxHtmlTag tag(m_Source, i, end_pos, m_Cache, m_entitiesParser);
115
116 if (templen)
117 {
118 temp[templen] = 0;
119 AddText(temp);
120 templen = 0;
121 }
122 AddTag(tag);
123 if (tag.HasEnding()) i = tag.GetEndPos2();
124 else i = tag.GetBeginPos();
125 }
126 }
127
128 if (templen)
129 { // last word of block :-(
130 temp[templen] = 0;
131 AddText(temp);
132 }
133 delete[] temp;
134}
135
136void wxHtmlParser::AddTag(const wxHtmlTag& tag)
137{
138 wxHtmlTagHandler *h;
139 bool inner = FALSE;
140
141 h = (wxHtmlTagHandler*) m_HandlersHash.Get(tag.GetName());
142 if (h)
143 inner = h->HandleTag(tag);
144 if (!inner)
145 {
146 if (tag.HasEnding())
147 DoParsing(tag.GetBeginPos(), tag.GetEndPos1());
148 }
149}
150
151void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler)
152{
153 wxString s(handler->GetSupportedTags());
154 wxStringTokenizer tokenizer(s, ", ");
155
156 while (tokenizer.HasMoreTokens())
157 m_HandlersHash.Put(tokenizer.NextToken(), handler);
158
159 if (m_HandlersList.IndexOf(handler) == wxNOT_FOUND)
160 m_HandlersList.Append(handler);
161
162 handler->SetParser(this);
163}
164
165void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, wxString tags)
166{
167 wxStringTokenizer tokenizer(tags, ", ");
168 wxString key;
169
170 if (m_HandlersStack == NULL)
171 {
172 m_HandlersStack = new wxList;
173 m_HandlersStack->DeleteContents(TRUE);
174 }
175
176 m_HandlersStack->Insert(new wxHashTable(m_HandlersHash));
177
178 while (tokenizer.HasMoreTokens())
179 {
180 key = tokenizer.NextToken();
181 m_HandlersHash.Delete(key);
182 m_HandlersHash.Put(key, handler);
183 }
184}
185
186void wxHtmlParser::PopTagHandler()
187{
188 wxNode *first;
189
190 if (m_HandlersStack == NULL ||
191 (first = m_HandlersStack->GetFirst()) == NULL)
192 {
193 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
194 return;
195 }
196 m_HandlersHash = *((wxHashTable*) first->GetData());
197 m_HandlersStack->DeleteNode(first);
198}
199
200//-----------------------------------------------------------------------------
201// wxHtmlTagHandler
202//-----------------------------------------------------------------------------
203
204IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler,wxObject)
205
206
207//-----------------------------------------------------------------------------
208// wxHtmlEntitiesParser
209//-----------------------------------------------------------------------------
210
211IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser,wxObject)
212
213wxHtmlEntitiesParser::wxHtmlEntitiesParser()
214#if wxUSE_WCHAR_T && !wxUSE_UNICODE
215 : m_conv(NULL), m_encoding(wxFONTENCODING_SYSTEM)
216#endif
217{
218}
219
220wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
221{
222#if wxUSE_WCHAR_T && !wxUSE_UNICODE
223 delete m_conv;
224#endif
225}
226
227void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding)
228{
229#if wxUSE_WCHAR_T && !wxUSE_UNICODE
230 if (encoding == m_encoding) return;
231 delete m_conv;
232 m_conv = NULL;
233 m_encoding = encoding;
234 if (m_encoding != wxFONTENCODING_SYSTEM)
235 m_conv = new wxCSConv(wxFontMapper::GetEncodingName(m_encoding));
236#endif
237}
238
239wxString wxHtmlEntitiesParser::Parse(const wxString& input)
240{
241 const wxChar *c, *last;
242 const wxChar *in_str = input.c_str();
243 wxString output;
244
245 for (c = in_str, last = in_str; *c != wxT('\0'); c++)
246 {
247 if (*c == wxT('&'))
248 {
249 if (c - last > 0)
250 output.append(last, c - last);
251 if (++c == wxT('\0')) break;
252 wxString entity;
253 const wxChar *ent_s = c;
254 for (; (*c >= wxT('a') && *c <= wxT('z')) ||
255 (*c >= wxT('A') && *c <= wxT('Z')) ||
256 (*c >= wxT('0') && *c <= wxT('9')) ||
257 *c == wxT('_') || *c == wxT('#'); c++) {}
258 entity.append(ent_s, c - ent_s);
259 if (*c == wxT(';')) c++;
260 output << GetEntityChar(entity);
261 last = c;
262 }
263 }
264 if (*last != wxT('\0'))
265 output.append(last);
266 return output;
267}
268
269struct wxHtmlEntityInfo
270{
271 const wxChar *name;
272 unsigned code;
273};
274
275static int LINKAGEMODE compar_entity(const void *key, const void *item)
276{
277 return wxStrcmp((wxChar*)key, ((wxHtmlEntityInfo*)item)->name);
278}
279
280wxChar wxHtmlEntitiesParser::GetCharForCode(unsigned code)
281{
282#if wxUSE_UNICODE
283 return (wxChar)code;
284#elif wxUSE_WCHAR_T
285 char buf[2];
286 wchar_t wbuf[2];
287 wbuf[0] = (wchar_t)code;
288 wbuf[1] = 0;
289 wxMBConv *conv = m_conv ? m_conv : &wxConvLocal;
290 if (conv->WC2MB(buf, wbuf, 1) == (size_t)-1)
291 return '?';
292 return buf[0];
293#else
294 return (code < 256) ? (wxChar)code : '?';
295#endif
296}
297
298wxChar wxHtmlEntitiesParser::GetEntityChar(const wxString& entity)
299{
300 unsigned code = 0;
301
302 if (entity[0] == wxT('#'))
303 {
304 const wxChar *ent_s = entity.c_str();
305 const wxChar *format;
306
307 if (ent_s[1] == wxT('x') || ent_s[1] == wxT('X'))
308 {
309 format = wxT("%x");
310 ent_s++;
311 }
312 else
313 format = wxT("%u");
314 ent_s++;
315
316 if (wxSscanf(ent_s, format, &code) != 1)
317 code = 0;
318 }
319 else
320 {
321 static wxHtmlEntityInfo substitutions[] = {
322 { wxT("AElig"),198 },
323 { wxT("Aacute"),193 },
324 { wxT("Acirc"),194 },
325 { wxT("Agrave"),192 },
326 { wxT("Alpha"),913 },
327 { wxT("Aring"),197 },
328 { wxT("Atilde"),195 },
329 { wxT("Auml"),196 },
330 { wxT("Beta"),914 },
331 { wxT("Ccedil"),199 },
332 { wxT("Chi"),935 },
333 { wxT("Dagger"),8225 },
334 { wxT("Delta"),916 },
335 { wxT("ETH"),208 },
336 { wxT("Eacute"),201 },
337 { wxT("Ecirc"),202 },
338 { wxT("Egrave"),200 },
339 { wxT("Epsilon"),917 },
340 { wxT("Eta"),919 },
341 { wxT("Euml"),203 },
342 { wxT("Gamma"),915 },
343 { wxT("Iacute"),205 },
344 { wxT("Icirc"),206 },
345 { wxT("Igrave"),204 },
346 { wxT("Iota"),921 },
347 { wxT("Iuml"),207 },
348 { wxT("Kappa"),922 },
349 { wxT("Lambda"),923 },
350 { wxT("Mu"),924 },
351 { wxT("Ntilde"),209 },
352 { wxT("Nu"),925 },
353 { wxT("OElig"),338 },
354 { wxT("Oacute"),211 },
355 { wxT("Ocirc"),212 },
356 { wxT("Ograve"),210 },
357 { wxT("Omega"),937 },
358 { wxT("Omicron"),927 },
359 { wxT("Oslash"),216 },
360 { wxT("Otilde"),213 },
361 { wxT("Ouml"),214 },
362 { wxT("Phi"),934 },
363 { wxT("Pi"),928 },
364 { wxT("Prime"),8243 },
365 { wxT("Psi"),936 },
366 { wxT("Rho"),929 },
367 { wxT("Scaron"),352 },
368 { wxT("Sigma"),931 },
369 { wxT("THORN"),222 },
370 { wxT("Tau"),932 },
371 { wxT("Theta"),920 },
372 { wxT("Uacute"),218 },
373 { wxT("Ucirc"),219 },
374 { wxT("Ugrave"),217 },
375 { wxT("Upsilon"),933 },
376 { wxT("Uuml"),220 },
377 { wxT("Xi"),926 },
378 { wxT("Yacute"),221 },
379 { wxT("Yuml"),376 },
380 { wxT("Zeta"),918 },
381 { wxT("aacute"),225 },
382 { wxT("acirc"),226 },
383 { wxT("acute"),180 },
384 { wxT("aelig"),230 },
385 { wxT("agrave"),224 },
386 { wxT("alefsym"),8501 },
387 { wxT("alpha"),945 },
388 { wxT("amp"),38 },
389 { wxT("and"),8743 },
390 { wxT("ang"),8736 },
391 { wxT("aring"),229 },
392 { wxT("asymp"),8776 },
393 { wxT("atilde"),227 },
394 { wxT("auml"),228 },
395 { wxT("bdquo"),8222 },
396 { wxT("beta"),946 },
397 { wxT("brvbar"),166 },
398 { wxT("bull"),8226 },
399 { wxT("cap"),8745 },
400 { wxT("ccedil"),231 },
401 { wxT("cedil"),184 },
402 { wxT("cent"),162 },
403 { wxT("chi"),967 },
404 { wxT("circ"),710 },
405 { wxT("clubs"),9827 },
406 { wxT("cong"),8773 },
407 { wxT("copy"),169 },
408 { wxT("crarr"),8629 },
409 { wxT("cup"),8746 },
410 { wxT("curren"),164 },
411 { wxT("dArr"),8659 },
412 { wxT("dagger"),8224 },
413 { wxT("darr"),8595 },
414 { wxT("deg"),176 },
415 { wxT("delta"),948 },
416 { wxT("diams"),9830 },
417 { wxT("divide"),247 },
418 { wxT("eacute"),233 },
419 { wxT("ecirc"),234 },
420 { wxT("egrave"),232 },
421 { wxT("empty"),8709 },
422 { wxT("emsp"),8195 },
423 { wxT("ensp"),8194 },
424 { wxT("epsilon"),949 },
425 { wxT("equiv"),8801 },
426 { wxT("eta"),951 },
427 { wxT("eth"),240 },
428 { wxT("euml"),235 },
429 { wxT("euro"),8364 },
430 { wxT("exist"),8707 },
431 { wxT("fnof"),402 },
432 { wxT("forall"),8704 },
433 { wxT("frac12"),189 },
434 { wxT("frac14"),188 },
435 { wxT("frac34"),190 },
436 { wxT("frasl"),8260 },
437 { wxT("gamma"),947 },
438 { wxT("ge"),8805 },
439 { wxT("gt"),62 },
440 { wxT("hArr"),8660 },
441 { wxT("harr"),8596 },
442 { wxT("hearts"),9829 },
443 { wxT("hellip"),8230 },
444 { wxT("iacute"),237 },
445 { wxT("icirc"),238 },
446 { wxT("iexcl"),161 },
447 { wxT("igrave"),236 },
448 { wxT("image"),8465 },
449 { wxT("infin"),8734 },
450 { wxT("int"),8747 },
451 { wxT("iota"),953 },
452 { wxT("iquest"),191 },
453 { wxT("isin"),8712 },
454 { wxT("iuml"),239 },
455 { wxT("kappa"),954 },
456 { wxT("lArr"),8656 },
457 { wxT("lambda"),955 },
458 { wxT("lang"),9001 },
459 { wxT("laquo"),171 },
460 { wxT("larr"),8592 },
461 { wxT("lceil"),8968 },
462 { wxT("ldquo"),8220 },
463 { wxT("le"),8804 },
464 { wxT("lfloor"),8970 },
465 { wxT("lowast"),8727 },
466 { wxT("loz"),9674 },
467 { wxT("lrm"),8206 },
468 { wxT("lsaquo"),8249 },
469 { wxT("lsquo"),8216 },
470 { wxT("lt"),60 },
471 { wxT("macr"),175 },
472 { wxT("mdash"),8212 },
473 { wxT("micro"),181 },
474 { wxT("middot"),183 },
475 { wxT("minus"),8722 },
476 { wxT("mu"),956 },
477 { wxT("nabla"),8711 },
478 { wxT("nbsp"),160 },
479 { wxT("ndash"),8211 },
480 { wxT("ne"),8800 },
481 { wxT("ni"),8715 },
482 { wxT("not"),172 },
483 { wxT("notin"),8713 },
484 { wxT("nsub"),8836 },
485 { wxT("ntilde"),241 },
486 { wxT("nu"),957 },
487 { wxT("oacute"),243 },
488 { wxT("ocirc"),244 },
489 { wxT("oelig"),339 },
490 { wxT("ograve"),242 },
491 { wxT("oline"),8254 },
492 { wxT("omega"),969 },
493 { wxT("omicron"),959 },
494 { wxT("oplus"),8853 },
495 { wxT("or"),8744 },
496 { wxT("ordf"),170 },
497 { wxT("ordm"),186 },
498 { wxT("oslash"),248 },
499 { wxT("otilde"),245 },
500 { wxT("otimes"),8855 },
501 { wxT("ouml"),246 },
502 { wxT("para"),182 },
503 { wxT("part"),8706 },
504 { wxT("permil"),8240 },
505 { wxT("perp"),8869 },
506 { wxT("phi"),966 },
507 { wxT("pi"),960 },
508 { wxT("piv"),982 },
509 { wxT("plusmn"),177 },
510 { wxT("pound"),163 },
511 { wxT("prime"),8242 },
512 { wxT("prod"),8719 },
513 { wxT("prop"),8733 },
514 { wxT("psi"),968 },
515 { wxT("quot"),34 },
516 { wxT("rArr"),8658 },
517 { wxT("radic"),8730 },
518 { wxT("rang"),9002 },
519 { wxT("raquo"),187 },
520 { wxT("rarr"),8594 },
521 { wxT("rceil"),8969 },
522 { wxT("rdquo"),8221 },
523 { wxT("real"),8476 },
524 { wxT("reg"),174 },
525 { wxT("rfloor"),8971 },
526 { wxT("rho"),961 },
527 { wxT("rlm"),8207 },
528 { wxT("rsaquo"),8250 },
529 { wxT("rsquo"),8217 },
530 { wxT("sbquo"),8218 },
531 { wxT("scaron"),353 },
532 { wxT("sdot"),8901 },
533 { wxT("sect"),167 },
534 { wxT("shy"),173 },
535 { wxT("sigma"),963 },
536 { wxT("sigmaf"),962 },
537 { wxT("sim"),8764 },
538 { wxT("spades"),9824 },
539 { wxT("sub"),8834 },
540 { wxT("sube"),8838 },
541 { wxT("sum"),8721 },
542 { wxT("sup"),8835 },
543 { wxT("sup1"),185 },
544 { wxT("sup2"),178 },
545 { wxT("sup3"),179 },
546 { wxT("supe"),8839 },
547 { wxT("szlig"),223 },
548 { wxT("tau"),964 },
549 { wxT("there4"),8756 },
550 { wxT("theta"),952 },
551 { wxT("thetasym"),977 },
552 { wxT("thinsp"),8201 },
553 { wxT("thorn"),254 },
554 { wxT("tilde"),732 },
555 { wxT("times"),215 },
556 { wxT("trade"),8482 },
557 { wxT("uArr"),8657 },
558 { wxT("uacute"),250 },
559 { wxT("uarr"),8593 },
560 { wxT("ucirc"),251 },
561 { wxT("ugrave"),249 },
562 { wxT("uml"),168 },
563 { wxT("upsih"),978 },
564 { wxT("upsilon"),965 },
565 { wxT("uuml"),252 },
566 { wxT("weierp"),8472 },
567 { wxT("xi"),958 },
568 { wxT("yacute"),253 },
569 { wxT("yen"),165 },
570 { wxT("yuml"),255 },
571 { wxT("zeta"),950 },
572 { wxT("zwj"),8205 },
573 { wxT("zwnj"),8204 },
574 {NULL, 0}};
575 static size_t substitutions_cnt = 0;
576
577 if (substitutions_cnt == 0)
578 while (substitutions[substitutions_cnt].code != 0)
579 substitutions_cnt++;
580
581 wxHtmlEntityInfo *info;
582 info = (wxHtmlEntityInfo*) bsearch(entity.c_str(), substitutions,
583 substitutions_cnt,
584 sizeof(wxHtmlEntityInfo),
585 compar_entity);
586 if (info)
587 code = info->code;
588 }
589
590 if (code == 0)
591 return wxT('?');
592 else
593 return GetCharForCode(code);
594}
595
596#endif