1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlParser class (generic parser)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation
15 #include "wx/wxprec.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
29 #include "wx/tokenzr.h"
30 #include "wx/wfstream.h"
32 #include "wx/fontmap.h"
33 #include "wx/html/htmldefs.h"
34 #include "wx/html/htmlpars.h"
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser
,wxObject
)
44 wxHtmlParser::wxHtmlParser()
45 : wxObject(), m_Cache(NULL
), m_HandlersHash(wxKEY_STRING
),
46 m_FS(NULL
), m_HandlersStack(NULL
)
48 m_entitiesParser
= new wxHtmlEntitiesParser
;
51 wxHtmlParser::~wxHtmlParser()
53 delete m_HandlersStack
;
54 m_HandlersHash
.Clear();
55 m_HandlersList
.DeleteContents(TRUE
);
56 m_HandlersList
.Clear();
57 delete m_entitiesParser
;
60 wxObject
* wxHtmlParser::Parse(const wxString
& source
)
66 result
= GetProduct();
71 void wxHtmlParser::InitParser(const wxString
& source
)
76 void wxHtmlParser::DoneParser()
82 void wxHtmlParser::SetSource(const wxString
& src
)
86 m_Cache
= new wxHtmlTagsCache(m_Source
);
89 void wxHtmlParser::DoParsing(int begin_pos
, int end_pos
)
91 if (end_pos
<= begin_pos
) return;
94 char *temp
= new char[end_pos
- begin_pos
+ 1];
103 c
= m_Source
[(unsigned int) i
];
105 // continue building word:
114 wxHtmlTag
tag(m_Source
, i
, end_pos
, m_Cache
, m_entitiesParser
);
123 if (tag
.HasEnding()) i
= tag
.GetEndPos2();
124 else i
= tag
.GetBeginPos();
129 { // last word of block :-(
136 void wxHtmlParser::AddTag(const wxHtmlTag
& tag
)
141 h
= (wxHtmlTagHandler
*) m_HandlersHash
.Get(tag
.GetName());
143 inner
= h
->HandleTag(tag
);
147 DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1());
151 void wxHtmlParser::AddTagHandler(wxHtmlTagHandler
*handler
)
153 wxString
s(handler
->GetSupportedTags());
154 wxStringTokenizer
tokenizer(s
, ", ");
156 while (tokenizer
.HasMoreTokens())
157 m_HandlersHash
.Put(tokenizer
.NextToken(), handler
);
159 if (m_HandlersList
.IndexOf(handler
) == wxNOT_FOUND
)
160 m_HandlersList
.Append(handler
);
162 handler
->SetParser(this);
165 void wxHtmlParser::PushTagHandler(wxHtmlTagHandler
*handler
, wxString tags
)
167 wxStringTokenizer
tokenizer(tags
, ", ");
170 if (m_HandlersStack
== NULL
)
172 m_HandlersStack
= new wxList
;
173 m_HandlersStack
->DeleteContents(TRUE
);
176 m_HandlersStack
->Insert(new wxHashTable(m_HandlersHash
));
178 while (tokenizer
.HasMoreTokens())
180 key
= tokenizer
.NextToken();
181 m_HandlersHash
.Delete(key
);
182 m_HandlersHash
.Put(key
, handler
);
186 void wxHtmlParser::PopTagHandler()
190 if (m_HandlersStack
== NULL
||
191 (first
= m_HandlersStack
->GetFirst()) == NULL
)
193 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
196 m_HandlersHash
= *((wxHashTable
*) first
->GetData());
197 m_HandlersStack
->DeleteNode(first
);
200 //-----------------------------------------------------------------------------
202 //-----------------------------------------------------------------------------
204 IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler
,wxObject
)
207 //-----------------------------------------------------------------------------
208 // wxHtmlEntitiesParser
209 //-----------------------------------------------------------------------------
211 IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser
,wxObject
)
213 wxHtmlEntitiesParser::wxHtmlEntitiesParser()
214 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
215 : m_conv(NULL
), m_encoding(wxFONTENCODING_SYSTEM
)
220 wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
225 void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding
)
227 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
228 if (encoding
== m_encoding
) return;
231 m_encoding
= encoding
;
232 if (m_encoding
!= wxFONTENCODING_SYSTEM
)
233 m_conv
= new wxCSConv(wxFontMapper::GetEncodingName(m_encoding
));
237 wxString
wxHtmlEntitiesParser::Parse(const wxString
& input
)
239 const wxChar
*c
, *last
;
240 const wxChar
*in_str
= input
.c_str();
243 for (c
= in_str
, last
= in_str
; *c
!= wxT('\0'); c
++)
248 output
.append(last
, c
- last
);
249 if (++c
== wxT('\0')) break;
251 const wxChar
*ent_s
= c
;
252 for (; (*c
>= wxT('a') && *c
<= wxT('z')) ||
253 (*c
>= wxT('A') && *c
<= wxT('Z')) ||
254 (*c
>= wxT('0') && *c
<= wxT('9')) ||
255 *c
== wxT('_') || *c
== wxT('#'); c
++) {}
256 entity
.append(ent_s
, c
- ent_s
);
257 if (*c
== wxT(';')) c
++;
258 output
<< GetEntityChar(entity
);
262 if (*last
!= wxT('\0'))
267 struct wxHtmlEntityInfo
273 static int compar_entity(const void *key
, const void *item
)
275 return wxStrcmp((wxChar
*)key
, ((wxHtmlEntityInfo
*)item
)->name
);
278 wxChar
wxHtmlEntitiesParser::GetCharForCode(unsigned code
)
285 wbuf
[0] = (wchar_t)code
;
287 wxMBConv
*conv
= m_conv
? m_conv
: &wxConvLocal
;
288 if (conv
->WC2MB(buf
, wbuf
, 1) == (size_t)-1)
292 return (code
< 256) ? (wxChar
)code
: '?';
296 wxChar
wxHtmlEntitiesParser::GetEntityChar(const wxString
& entity
)
300 if (entity
[0] == wxT('#'))
302 const wxChar
*ent_s
= entity
.c_str();
303 const wxChar
*format
;
305 if (ent_s
[1] == wxT('x') || ent_s
[1] == wxT('X'))
314 if (wxSscanf(ent_s
, format
, &code
) != 1)
319 static wxHtmlEntityInfo substitutions
[] = {
320 { wxT("AElig"),198 },
321 { wxT("Aacute"),193 },
322 { wxT("Acirc"),194 },
323 { wxT("Agrave"),192 },
324 { wxT("Alpha"),913 },
325 { wxT("Aring"),197 },
326 { wxT("Atilde"),195 },
329 { wxT("Ccedil"),199 },
331 { wxT("Dagger"),8225 },
332 { wxT("Delta"),916 },
334 { wxT("Eacute"),201 },
335 { wxT("Ecirc"),202 },
336 { wxT("Egrave"),200 },
337 { wxT("Epsilon"),917 },
340 { wxT("Gamma"),915 },
341 { wxT("Iacute"),205 },
342 { wxT("Icirc"),206 },
343 { wxT("Igrave"),204 },
346 { wxT("Kappa"),922 },
347 { wxT("Lambda"),923 },
349 { wxT("Ntilde"),209 },
351 { wxT("OElig"),338 },
352 { wxT("Oacute"),211 },
353 { wxT("Ocirc"),212 },
354 { wxT("Ograve"),210 },
355 { wxT("Omega"),937 },
356 { wxT("Omicron"),927 },
357 { wxT("Oslash"),216 },
358 { wxT("Otilde"),213 },
362 { wxT("Prime"),8243 },
365 { wxT("Scaron"),352 },
366 { wxT("Sigma"),931 },
367 { wxT("THORN"),222 },
369 { wxT("Theta"),920 },
370 { wxT("Uacute"),218 },
371 { wxT("Ucirc"),219 },
372 { wxT("Ugrave"),217 },
373 { wxT("Upsilon"),933 },
376 { wxT("Yacute"),221 },
379 { wxT("aacute"),225 },
380 { wxT("acirc"),226 },
381 { wxT("acute"),180 },
382 { wxT("aelig"),230 },
383 { wxT("agrave"),224 },
384 { wxT("alefsym"),8501 },
385 { wxT("alpha"),945 },
389 { wxT("aring"),229 },
390 { wxT("asymp"),8776 },
391 { wxT("atilde"),227 },
393 { wxT("bdquo"),8222 },
395 { wxT("brvbar"),166 },
396 { wxT("bull"),8226 },
398 { wxT("ccedil"),231 },
399 { wxT("cedil"),184 },
403 { wxT("clubs"),9827 },
404 { wxT("cong"),8773 },
406 { wxT("crarr"),8629 },
408 { wxT("curren"),164 },
409 { wxT("dArr"),8659 },
410 { wxT("dagger"),8224 },
411 { wxT("darr"),8595 },
413 { wxT("delta"),948 },
414 { wxT("diams"),9830 },
415 { wxT("divide"),247 },
416 { wxT("eacute"),233 },
417 { wxT("ecirc"),234 },
418 { wxT("egrave"),232 },
419 { wxT("empty"),8709 },
420 { wxT("emsp"),8195 },
421 { wxT("ensp"),8194 },
422 { wxT("epsilon"),949 },
423 { wxT("equiv"),8801 },
427 { wxT("euro"),8364 },
428 { wxT("exist"),8707 },
430 { wxT("forall"),8704 },
431 { wxT("frac12"),189 },
432 { wxT("frac14"),188 },
433 { wxT("frac34"),190 },
434 { wxT("frasl"),8260 },
435 { wxT("gamma"),947 },
438 { wxT("hArr"),8660 },
439 { wxT("harr"),8596 },
440 { wxT("hearts"),9829 },
441 { wxT("hellip"),8230 },
442 { wxT("iacute"),237 },
443 { wxT("icirc"),238 },
444 { wxT("iexcl"),161 },
445 { wxT("igrave"),236 },
446 { wxT("image"),8465 },
447 { wxT("infin"),8734 },
450 { wxT("iquest"),191 },
451 { wxT("isin"),8712 },
453 { wxT("kappa"),954 },
454 { wxT("lArr"),8656 },
455 { wxT("lambda"),955 },
456 { wxT("lang"),9001 },
457 { wxT("laquo"),171 },
458 { wxT("larr"),8592 },
459 { wxT("lceil"),8968 },
460 { wxT("ldquo"),8220 },
462 { wxT("lfloor"),8970 },
463 { wxT("lowast"),8727 },
466 { wxT("lsaquo"),8249 },
467 { wxT("lsquo"),8216 },
470 { wxT("mdash"),8212 },
471 { wxT("micro"),181 },
472 { wxT("middot"),183 },
473 { wxT("minus"),8722 },
475 { wxT("nabla"),8711 },
477 { wxT("ndash"),8211 },
481 { wxT("notin"),8713 },
482 { wxT("nsub"),8836 },
483 { wxT("ntilde"),241 },
485 { wxT("oacute"),243 },
486 { wxT("ocirc"),244 },
487 { wxT("oelig"),339 },
488 { wxT("ograve"),242 },
489 { wxT("oline"),8254 },
490 { wxT("omega"),969 },
491 { wxT("omicron"),959 },
492 { wxT("oplus"),8853 },
496 { wxT("oslash"),248 },
497 { wxT("otilde"),245 },
498 { wxT("otimes"),8855 },
501 { wxT("part"),8706 },
502 { wxT("permil"),8240 },
503 { wxT("perp"),8869 },
507 { wxT("plusmn"),177 },
508 { wxT("pound"),163 },
509 { wxT("prime"),8242 },
510 { wxT("prod"),8719 },
511 { wxT("prop"),8733 },
514 { wxT("rArr"),8658 },
515 { wxT("radic"),8730 },
516 { wxT("rang"),9002 },
517 { wxT("raquo"),187 },
518 { wxT("rarr"),8594 },
519 { wxT("rceil"),8969 },
520 { wxT("rdquo"),8221 },
521 { wxT("real"),8476 },
523 { wxT("rfloor"),8971 },
526 { wxT("rsaquo"),8250 },
527 { wxT("rsquo"),8217 },
528 { wxT("sbquo"),8218 },
529 { wxT("scaron"),353 },
530 { wxT("sdot"),8901 },
533 { wxT("sigma"),963 },
534 { wxT("sigmaf"),962 },
536 { wxT("spades"),9824 },
538 { wxT("sube"),8838 },
544 { wxT("supe"),8839 },
545 { wxT("szlig"),223 },
547 { wxT("there4"),8756 },
548 { wxT("theta"),952 },
549 { wxT("thetasym"),977 },
550 { wxT("thinsp"),8201 },
551 { wxT("thorn"),254 },
552 { wxT("tilde"),732 },
553 { wxT("times"),215 },
554 { wxT("trade"),8482 },
555 { wxT("uArr"),8657 },
556 { wxT("uacute"),250 },
557 { wxT("uarr"),8593 },
558 { wxT("ucirc"),251 },
559 { wxT("ugrave"),249 },
561 { wxT("upsih"),978 },
562 { wxT("upsilon"),965 },
564 { wxT("weierp"),8472 },
566 { wxT("yacute"),253 },
571 { wxT("zwnj"),8204 },
573 static size_t substitutions_cnt
= 0;
575 if (substitutions_cnt
== 0)
576 while (substitutions
[substitutions_cnt
].code
!= 0)
579 wxHtmlEntityInfo
*info
;
580 info
= (wxHtmlEntityInfo
*) bsearch(entity
.c_str(), substitutions
,
582 sizeof(wxHtmlEntityInfo
),
591 return GetCharForCode(code
);