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()
222 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
227 void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding
)
229 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
230 if (encoding
== m_encoding
) return;
233 m_encoding
= encoding
;
234 if (m_encoding
!= wxFONTENCODING_SYSTEM
)
235 m_conv
= new wxCSConv(wxFontMapper::GetEncodingName(m_encoding
));
239 wxString
wxHtmlEntitiesParser::Parse(const wxString
& input
)
241 const wxChar
*c
, *last
;
242 const wxChar
*in_str
= input
.c_str();
245 for (c
= in_str
, last
= in_str
; *c
!= wxT('\0'); c
++)
250 output
.append(last
, c
- last
);
251 if (++c
== wxT('\0')) break;
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
);
264 if (*last
!= wxT('\0'))
269 struct wxHtmlEntityInfo
275 static int LINKAGEMODE
compar_entity(const void *key
, const void *item
)
277 return wxStrcmp((wxChar
*)key
, ((wxHtmlEntityInfo
*)item
)->name
);
280 wxChar
wxHtmlEntitiesParser::GetCharForCode(unsigned code
)
287 wbuf
[0] = (wchar_t)code
;
289 wxMBConv
*conv
= m_conv
? m_conv
: &wxConvLocal
;
290 if (conv
->WC2MB(buf
, wbuf
, 1) == (size_t)-1)
294 return (code
< 256) ? (wxChar
)code
: '?';
298 wxChar
wxHtmlEntitiesParser::GetEntityChar(const wxString
& entity
)
302 if (entity
[0] == wxT('#'))
304 const wxChar
*ent_s
= entity
.c_str();
305 const wxChar
*format
;
307 if (ent_s
[1] == wxT('x') || ent_s
[1] == wxT('X'))
316 if (wxSscanf(ent_s
, format
, &code
) != 1)
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 },
331 { wxT("Ccedil"),199 },
333 { wxT("Dagger"),8225 },
334 { wxT("Delta"),916 },
336 { wxT("Eacute"),201 },
337 { wxT("Ecirc"),202 },
338 { wxT("Egrave"),200 },
339 { wxT("Epsilon"),917 },
342 { wxT("Gamma"),915 },
343 { wxT("Iacute"),205 },
344 { wxT("Icirc"),206 },
345 { wxT("Igrave"),204 },
348 { wxT("Kappa"),922 },
349 { wxT("Lambda"),923 },
351 { wxT("Ntilde"),209 },
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 },
364 { wxT("Prime"),8243 },
367 { wxT("Scaron"),352 },
368 { wxT("Sigma"),931 },
369 { wxT("THORN"),222 },
371 { wxT("Theta"),920 },
372 { wxT("Uacute"),218 },
373 { wxT("Ucirc"),219 },
374 { wxT("Ugrave"),217 },
375 { wxT("Upsilon"),933 },
378 { wxT("Yacute"),221 },
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 },
391 { wxT("aring"),229 },
392 { wxT("asymp"),8776 },
393 { wxT("atilde"),227 },
395 { wxT("bdquo"),8222 },
397 { wxT("brvbar"),166 },
398 { wxT("bull"),8226 },
400 { wxT("ccedil"),231 },
401 { wxT("cedil"),184 },
405 { wxT("clubs"),9827 },
406 { wxT("cong"),8773 },
408 { wxT("crarr"),8629 },
410 { wxT("curren"),164 },
411 { wxT("dArr"),8659 },
412 { wxT("dagger"),8224 },
413 { wxT("darr"),8595 },
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 },
429 { wxT("euro"),8364 },
430 { wxT("exist"),8707 },
432 { wxT("forall"),8704 },
433 { wxT("frac12"),189 },
434 { wxT("frac14"),188 },
435 { wxT("frac34"),190 },
436 { wxT("frasl"),8260 },
437 { wxT("gamma"),947 },
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 },
452 { wxT("iquest"),191 },
453 { wxT("isin"),8712 },
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 },
464 { wxT("lfloor"),8970 },
465 { wxT("lowast"),8727 },
468 { wxT("lsaquo"),8249 },
469 { wxT("lsquo"),8216 },
472 { wxT("mdash"),8212 },
473 { wxT("micro"),181 },
474 { wxT("middot"),183 },
475 { wxT("minus"),8722 },
477 { wxT("nabla"),8711 },
479 { wxT("ndash"),8211 },
483 { wxT("notin"),8713 },
484 { wxT("nsub"),8836 },
485 { wxT("ntilde"),241 },
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 },
498 { wxT("oslash"),248 },
499 { wxT("otilde"),245 },
500 { wxT("otimes"),8855 },
503 { wxT("part"),8706 },
504 { wxT("permil"),8240 },
505 { wxT("perp"),8869 },
509 { wxT("plusmn"),177 },
510 { wxT("pound"),163 },
511 { wxT("prime"),8242 },
512 { wxT("prod"),8719 },
513 { wxT("prop"),8733 },
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 },
525 { wxT("rfloor"),8971 },
528 { wxT("rsaquo"),8250 },
529 { wxT("rsquo"),8217 },
530 { wxT("sbquo"),8218 },
531 { wxT("scaron"),353 },
532 { wxT("sdot"),8901 },
535 { wxT("sigma"),963 },
536 { wxT("sigmaf"),962 },
538 { wxT("spades"),9824 },
540 { wxT("sube"),8838 },
546 { wxT("supe"),8839 },
547 { wxT("szlig"),223 },
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 },
563 { wxT("upsih"),978 },
564 { wxT("upsilon"),965 },
566 { wxT("weierp"),8472 },
568 { wxT("yacute"),253 },
573 { wxT("zwnj"),8204 },
575 static size_t substitutions_cnt
= 0;
577 if (substitutions_cnt
== 0)
578 while (substitutions
[substitutions_cnt
].code
!= 0)
581 wxHtmlEntityInfo
*info
;
582 info
= (wxHtmlEntityInfo
*) bsearch(entity
.c_str(), substitutions
,
584 sizeof(wxHtmlEntityInfo
),
593 return GetCharForCode(code
);