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
28 #include "wx/tokenzr.h"
29 #include "wx/wfstream.h"
31 #include "wx/fontmap.h"
32 #include "wx/html/htmldefs.h"
33 #include "wx/html/htmlpars.h"
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser
,wxObject
)
43 wxHtmlParser::wxHtmlParser()
44 : wxObject(), m_Cache(NULL
), m_HandlersHash(wxKEY_STRING
),
45 m_FS(NULL
), m_HandlersStack(NULL
)
47 m_entitiesParser
= new wxHtmlEntitiesParser
;
50 wxHtmlParser::~wxHtmlParser()
52 delete m_HandlersStack
;
53 m_HandlersHash
.Clear();
54 m_HandlersList
.DeleteContents(TRUE
);
55 m_HandlersList
.Clear();
56 delete m_entitiesParser
;
59 wxObject
* wxHtmlParser::Parse(const wxString
& source
)
65 result
= GetProduct();
70 void wxHtmlParser::InitParser(const wxString
& source
)
75 void wxHtmlParser::DoneParser()
81 void wxHtmlParser::SetSource(const wxString
& src
)
85 m_Cache
= new wxHtmlTagsCache(m_Source
);
88 void wxHtmlParser::DoParsing(int begin_pos
, int end_pos
)
90 if (end_pos
<= begin_pos
) return;
93 char *temp
= new char[end_pos
- begin_pos
+ 1];
102 c
= m_Source
[(unsigned int) i
];
104 // continue building word:
113 wxHtmlTag
tag(m_Source
, i
, end_pos
, m_Cache
, m_entitiesParser
);
122 if (tag
.HasEnding()) i
= tag
.GetEndPos2();
123 else i
= tag
.GetBeginPos();
128 { // last word of block :-(
135 void wxHtmlParser::AddTag(const wxHtmlTag
& tag
)
140 h
= (wxHtmlTagHandler
*) m_HandlersHash
.Get(tag
.GetName());
142 inner
= h
->HandleTag(tag
);
146 DoParsing(tag
.GetBeginPos(), tag
.GetEndPos1());
150 void wxHtmlParser::AddTagHandler(wxHtmlTagHandler
*handler
)
152 wxString
s(handler
->GetSupportedTags());
153 wxStringTokenizer
tokenizer(s
, ", ");
155 while (tokenizer
.HasMoreTokens())
156 m_HandlersHash
.Put(tokenizer
.NextToken(), handler
);
158 if (m_HandlersList
.IndexOf(handler
) == wxNOT_FOUND
)
159 m_HandlersList
.Append(handler
);
161 handler
->SetParser(this);
164 void wxHtmlParser::PushTagHandler(wxHtmlTagHandler
*handler
, wxString tags
)
166 wxStringTokenizer
tokenizer(tags
, ", ");
169 if (m_HandlersStack
== NULL
)
171 m_HandlersStack
= new wxList
;
172 m_HandlersStack
->DeleteContents(TRUE
);
175 m_HandlersStack
->Insert(new wxHashTable(m_HandlersHash
));
177 while (tokenizer
.HasMoreTokens())
179 key
= tokenizer
.NextToken();
180 m_HandlersHash
.Delete(key
);
181 m_HandlersHash
.Put(key
, handler
);
185 void wxHtmlParser::PopTagHandler()
189 if (m_HandlersStack
== NULL
||
190 (first
= m_HandlersStack
->GetFirst()) == NULL
)
192 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
195 m_HandlersHash
= *((wxHashTable
*) first
->GetData());
196 m_HandlersStack
->DeleteNode(first
);
199 //-----------------------------------------------------------------------------
201 //-----------------------------------------------------------------------------
203 IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler
,wxObject
)
206 //-----------------------------------------------------------------------------
207 // wxHtmlEntitiesParser
208 //-----------------------------------------------------------------------------
210 IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser
,wxObject
)
212 wxHtmlEntitiesParser::wxHtmlEntitiesParser()
213 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
214 : m_conv(NULL
), m_encoding(wxFONTENCODING_SYSTEM
)
219 wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
224 void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding
)
226 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
227 if (encoding
== m_encoding
) return;
230 m_encoding
= encoding
;
231 if (m_encoding
!= wxFONTENCODING_SYSTEM
)
232 m_conv
= new wxCSConv(wxFontMapper::GetEncodingName(m_encoding
));
236 wxString
wxHtmlEntitiesParser::Parse(const wxString
& input
)
238 const wxChar
*c
, *last
;
239 const wxChar
*in_str
= input
.c_str();
242 for (c
= in_str
, last
= in_str
; *c
!= wxT('\0'); c
++)
247 output
.append(last
, c
- last
);
248 if (++c
== wxT('\0')) break;
250 const wxChar
*ent_s
= c
;
251 for (; (*c
>= wxT('a') && *c
<= wxT('z')) ||
252 (*c
>= wxT('A') && *c
<= wxT('Z')) ||
253 (*c
>= wxT('0') && *c
<= wxT('9')) ||
254 *c
== wxT('_') || *c
== wxT('#'); c
++) {}
255 entity
.append(ent_s
, c
- ent_s
);
256 if (*c
== wxT(';')) c
++;
257 output
<< GetEntityChar(entity
);
261 if (*last
!= wxT('\0'))
266 struct wxHtmlEntityInfo
272 static int compar_entity(const void *key
, const void *item
)
274 return wxStrcmp((wxChar
*)key
, ((wxHtmlEntityInfo
*)item
)->name
);
277 wxChar
wxHtmlEntitiesParser::GetCharForCode(unsigned code
)
284 wbuf
[0] = (wchar_t)code
;
286 wxMBConv
*conv
= m_conv
? m_conv
: &wxConvLocal
;
287 if (conv
->WC2MB(buf
, wbuf
, 1) == (size_t)-1)
291 return (code
< 256) ? (wxChar
)code
: '?';
295 wxChar
wxHtmlEntitiesParser::GetEntityChar(const wxString
& entity
)
299 if (entity
[0] == wxT('#'))
301 const wxChar
*ent_s
= entity
.c_str();
302 const wxChar
*format
;
304 if (ent_s
[1] == wxT('x') || ent_s
[1] == wxT('X'))
313 if (wxSscanf(ent_s
, format
, &code
) != 1)
318 static wxHtmlEntityInfo substitutions
[] = {
319 { wxT("AElig"),198 },
320 { wxT("Aacute"),193 },
321 { wxT("Acirc"),194 },
322 { wxT("Agrave"),192 },
323 { wxT("Alpha"),913 },
324 { wxT("Aring"),197 },
325 { wxT("Atilde"),195 },
328 { wxT("Ccedil"),199 },
330 { wxT("Dagger"),8225 },
331 { wxT("Delta"),916 },
333 { wxT("Eacute"),201 },
334 { wxT("Ecirc"),202 },
335 { wxT("Egrave"),200 },
336 { wxT("Epsilon"),917 },
339 { wxT("Gamma"),915 },
340 { wxT("Iacute"),205 },
341 { wxT("Icirc"),206 },
342 { wxT("Igrave"),204 },
345 { wxT("Kappa"),922 },
346 { wxT("Lambda"),923 },
348 { wxT("Ntilde"),209 },
350 { wxT("OElig"),338 },
351 { wxT("Oacute"),211 },
352 { wxT("Ocirc"),212 },
353 { wxT("Ograve"),210 },
354 { wxT("Omega"),937 },
355 { wxT("Omicron"),927 },
356 { wxT("Oslash"),216 },
357 { wxT("Otilde"),213 },
361 { wxT("Prime"),8243 },
364 { wxT("Scaron"),352 },
365 { wxT("Sigma"),931 },
366 { wxT("THORN"),222 },
368 { wxT("Theta"),920 },
369 { wxT("Uacute"),218 },
370 { wxT("Ucirc"),219 },
371 { wxT("Ugrave"),217 },
372 { wxT("Upsilon"),933 },
375 { wxT("Yacute"),221 },
378 { wxT("aacute"),225 },
379 { wxT("acirc"),226 },
380 { wxT("acute"),180 },
381 { wxT("aelig"),230 },
382 { wxT("agrave"),224 },
383 { wxT("alefsym"),8501 },
384 { wxT("alpha"),945 },
388 { wxT("aring"),229 },
389 { wxT("asymp"),8776 },
390 { wxT("atilde"),227 },
392 { wxT("bdquo"),8222 },
394 { wxT("brvbar"),166 },
395 { wxT("bull"),8226 },
397 { wxT("ccedil"),231 },
398 { wxT("cedil"),184 },
402 { wxT("clubs"),9827 },
403 { wxT("cong"),8773 },
405 { wxT("crarr"),8629 },
407 { wxT("curren"),164 },
408 { wxT("dArr"),8659 },
409 { wxT("dagger"),8224 },
410 { wxT("darr"),8595 },
412 { wxT("delta"),948 },
413 { wxT("diams"),9830 },
414 { wxT("divide"),247 },
415 { wxT("eacute"),233 },
416 { wxT("ecirc"),234 },
417 { wxT("egrave"),232 },
418 { wxT("empty"),8709 },
419 { wxT("emsp"),8195 },
420 { wxT("ensp"),8194 },
421 { wxT("epsilon"),949 },
422 { wxT("equiv"),8801 },
426 { wxT("euro"),8364 },
427 { wxT("exist"),8707 },
429 { wxT("forall"),8704 },
430 { wxT("frac12"),189 },
431 { wxT("frac14"),188 },
432 { wxT("frac34"),190 },
433 { wxT("frasl"),8260 },
434 { wxT("gamma"),947 },
437 { wxT("hArr"),8660 },
438 { wxT("harr"),8596 },
439 { wxT("hearts"),9829 },
440 { wxT("hellip"),8230 },
441 { wxT("iacute"),237 },
442 { wxT("icirc"),238 },
443 { wxT("iexcl"),161 },
444 { wxT("igrave"),236 },
445 { wxT("image"),8465 },
446 { wxT("infin"),8734 },
449 { wxT("iquest"),191 },
450 { wxT("isin"),8712 },
452 { wxT("kappa"),954 },
453 { wxT("lArr"),8656 },
454 { wxT("lambda"),955 },
455 { wxT("lang"),9001 },
456 { wxT("laquo"),171 },
457 { wxT("larr"),8592 },
458 { wxT("lceil"),8968 },
459 { wxT("ldquo"),8220 },
461 { wxT("lfloor"),8970 },
462 { wxT("lowast"),8727 },
465 { wxT("lsaquo"),8249 },
466 { wxT("lsquo"),8216 },
469 { wxT("mdash"),8212 },
470 { wxT("micro"),181 },
471 { wxT("middot"),183 },
472 { wxT("minus"),8722 },
474 { wxT("nabla"),8711 },
476 { wxT("ndash"),8211 },
480 { wxT("notin"),8713 },
481 { wxT("nsub"),8836 },
482 { wxT("ntilde"),241 },
484 { wxT("oacute"),243 },
485 { wxT("ocirc"),244 },
486 { wxT("oelig"),339 },
487 { wxT("ograve"),242 },
488 { wxT("oline"),8254 },
489 { wxT("omega"),969 },
490 { wxT("omicron"),959 },
491 { wxT("oplus"),8853 },
495 { wxT("oslash"),248 },
496 { wxT("otilde"),245 },
497 { wxT("otimes"),8855 },
500 { wxT("part"),8706 },
501 { wxT("permil"),8240 },
502 { wxT("perp"),8869 },
506 { wxT("plusmn"),177 },
507 { wxT("pound"),163 },
508 { wxT("prime"),8242 },
509 { wxT("prod"),8719 },
510 { wxT("prop"),8733 },
513 { wxT("rArr"),8658 },
514 { wxT("radic"),8730 },
515 { wxT("rang"),9002 },
516 { wxT("raquo"),187 },
517 { wxT("rarr"),8594 },
518 { wxT("rceil"),8969 },
519 { wxT("rdquo"),8221 },
520 { wxT("real"),8476 },
522 { wxT("rfloor"),8971 },
525 { wxT("rsaquo"),8250 },
526 { wxT("rsquo"),8217 },
527 { wxT("sbquo"),8218 },
528 { wxT("scaron"),353 },
529 { wxT("sdot"),8901 },
532 { wxT("sigma"),963 },
533 { wxT("sigmaf"),962 },
535 { wxT("spades"),9824 },
537 { wxT("sube"),8838 },
543 { wxT("supe"),8839 },
544 { wxT("szlig"),223 },
546 { wxT("there4"),8756 },
547 { wxT("theta"),952 },
548 { wxT("thetasym"),977 },
549 { wxT("thinsp"),8201 },
550 { wxT("thorn"),254 },
551 { wxT("tilde"),732 },
552 { wxT("times"),215 },
553 { wxT("trade"),8482 },
554 { wxT("uArr"),8657 },
555 { wxT("uacute"),250 },
556 { wxT("uarr"),8593 },
557 { wxT("ucirc"),251 },
558 { wxT("ugrave"),249 },
560 { wxT("upsih"),978 },
561 { wxT("upsilon"),965 },
563 { wxT("weierp"),8472 },
565 { wxT("yacute"),253 },
570 { wxT("zwnj"),8204 },
572 static size_t substitutions_cnt
= 0;
574 if (substitutions_cnt
== 0)
575 while (substitutions
[substitutions_cnt
].code
!= 0)
578 wxHtmlEntityInfo
*info
;
579 info
= (wxHtmlEntityInfo
*) bsearch(entity
.c_str(), substitutions
,
581 sizeof(wxHtmlEntityInfo
),
590 return GetCharForCode(code
);