Missing LINKAGEMODE on static declaration (for OS/2).
[wxWidgets.git] / src / html / htmlpars.cpp
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
42 IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser,wxObject)
43
44 wxHtmlParser::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
51 wxHtmlParser::~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
60 wxObject* 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
71 void wxHtmlParser::InitParser(const wxString& source)
72 {
73 SetSource(source);
74 }
75
76 void wxHtmlParser::DoneParser()
77 {
78 delete m_Cache;
79 m_Cache = NULL;
80 }
81
82 void wxHtmlParser::SetSource(const wxString& src)
83 {
84 m_Source = src;
85 delete m_Cache;
86 m_Cache = new wxHtmlTagsCache(m_Source);
87 }
88
89 void 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
136 void 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
151 void 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
165 void 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
186 void 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
204 IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler,wxObject)
205
206
207 //-----------------------------------------------------------------------------
208 // wxHtmlEntitiesParser
209 //-----------------------------------------------------------------------------
210
211 IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser,wxObject)
212
213 wxHtmlEntitiesParser::wxHtmlEntitiesParser()
214 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
215 : m_conv(NULL), m_encoding(wxFONTENCODING_SYSTEM)
216 #endif
217 {
218 }
219
220 wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
221 {
222 delete m_conv;
223 }
224
225 void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding)
226 {
227 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
228 if (encoding == m_encoding) return;
229 delete m_conv;
230 m_conv = NULL;
231 m_encoding = encoding;
232 if (m_encoding != wxFONTENCODING_SYSTEM)
233 m_conv = new wxCSConv(wxFontMapper::GetEncodingName(m_encoding));
234 #endif
235 }
236
237 wxString wxHtmlEntitiesParser::Parse(const wxString& input)
238 {
239 const wxChar *c, *last;
240 const wxChar *in_str = input.c_str();
241 wxString output;
242
243 for (c = in_str, last = in_str; *c != wxT('\0'); c++)
244 {
245 if (*c == wxT('&'))
246 {
247 if (c - last > 0)
248 output.append(last, c - last);
249 if (++c == wxT('\0')) break;
250 wxString entity;
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);
259 last = c;
260 }
261 }
262 if (*last != wxT('\0'))
263 output.append(last);
264 return output;
265 }
266
267 struct wxHtmlEntityInfo
268 {
269 const wxChar *name;
270 unsigned code;
271 };
272
273 static int LINKAGEMODE compar_entity(const void *key, const void *item)
274 {
275 return wxStrcmp((wxChar*)key, ((wxHtmlEntityInfo*)item)->name);
276 }
277
278 wxChar wxHtmlEntitiesParser::GetCharForCode(unsigned code)
279 {
280 #if wxUSE_UNICODE
281 return (wxChar)code;
282 #elif wxUSE_WCHAR_T
283 char buf[2];
284 wchar_t wbuf[2];
285 wbuf[0] = (wchar_t)code;
286 wbuf[1] = 0;
287 wxMBConv *conv = m_conv ? m_conv : &wxConvLocal;
288 if (conv->WC2MB(buf, wbuf, 1) == (size_t)-1)
289 return '?';
290 return buf[0];
291 #else
292 return (code < 256) ? (wxChar)code : '?';
293 #endif
294 }
295
296 wxChar wxHtmlEntitiesParser::GetEntityChar(const wxString& entity)
297 {
298 unsigned code = 0;
299
300 if (entity[0] == wxT('#'))
301 {
302 const wxChar *ent_s = entity.c_str();
303 const wxChar *format;
304
305 if (ent_s[1] == wxT('x') || ent_s[1] == wxT('X'))
306 {
307 format = wxT("%x");
308 ent_s++;
309 }
310 else
311 format = wxT("%u");
312 ent_s++;
313
314 if (wxSscanf(ent_s, format, &code) != 1)
315 code = 0;
316 }
317 else
318 {
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 },
327 { wxT("Auml"),196 },
328 { wxT("Beta"),914 },
329 { wxT("Ccedil"),199 },
330 { wxT("Chi"),935 },
331 { wxT("Dagger"),8225 },
332 { wxT("Delta"),916 },
333 { wxT("ETH"),208 },
334 { wxT("Eacute"),201 },
335 { wxT("Ecirc"),202 },
336 { wxT("Egrave"),200 },
337 { wxT("Epsilon"),917 },
338 { wxT("Eta"),919 },
339 { wxT("Euml"),203 },
340 { wxT("Gamma"),915 },
341 { wxT("Iacute"),205 },
342 { wxT("Icirc"),206 },
343 { wxT("Igrave"),204 },
344 { wxT("Iota"),921 },
345 { wxT("Iuml"),207 },
346 { wxT("Kappa"),922 },
347 { wxT("Lambda"),923 },
348 { wxT("Mu"),924 },
349 { wxT("Ntilde"),209 },
350 { wxT("Nu"),925 },
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 },
359 { wxT("Ouml"),214 },
360 { wxT("Phi"),934 },
361 { wxT("Pi"),928 },
362 { wxT("Prime"),8243 },
363 { wxT("Psi"),936 },
364 { wxT("Rho"),929 },
365 { wxT("Scaron"),352 },
366 { wxT("Sigma"),931 },
367 { wxT("THORN"),222 },
368 { wxT("Tau"),932 },
369 { wxT("Theta"),920 },
370 { wxT("Uacute"),218 },
371 { wxT("Ucirc"),219 },
372 { wxT("Ugrave"),217 },
373 { wxT("Upsilon"),933 },
374 { wxT("Uuml"),220 },
375 { wxT("Xi"),926 },
376 { wxT("Yacute"),221 },
377 { wxT("Yuml"),376 },
378 { wxT("Zeta"),918 },
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 },
386 { wxT("amp"),38 },
387 { wxT("and"),8743 },
388 { wxT("ang"),8736 },
389 { wxT("aring"),229 },
390 { wxT("asymp"),8776 },
391 { wxT("atilde"),227 },
392 { wxT("auml"),228 },
393 { wxT("bdquo"),8222 },
394 { wxT("beta"),946 },
395 { wxT("brvbar"),166 },
396 { wxT("bull"),8226 },
397 { wxT("cap"),8745 },
398 { wxT("ccedil"),231 },
399 { wxT("cedil"),184 },
400 { wxT("cent"),162 },
401 { wxT("chi"),967 },
402 { wxT("circ"),710 },
403 { wxT("clubs"),9827 },
404 { wxT("cong"),8773 },
405 { wxT("copy"),169 },
406 { wxT("crarr"),8629 },
407 { wxT("cup"),8746 },
408 { wxT("curren"),164 },
409 { wxT("dArr"),8659 },
410 { wxT("dagger"),8224 },
411 { wxT("darr"),8595 },
412 { wxT("deg"),176 },
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 },
424 { wxT("eta"),951 },
425 { wxT("eth"),240 },
426 { wxT("euml"),235 },
427 { wxT("euro"),8364 },
428 { wxT("exist"),8707 },
429 { wxT("fnof"),402 },
430 { wxT("forall"),8704 },
431 { wxT("frac12"),189 },
432 { wxT("frac14"),188 },
433 { wxT("frac34"),190 },
434 { wxT("frasl"),8260 },
435 { wxT("gamma"),947 },
436 { wxT("ge"),8805 },
437 { wxT("gt"),62 },
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 },
448 { wxT("int"),8747 },
449 { wxT("iota"),953 },
450 { wxT("iquest"),191 },
451 { wxT("isin"),8712 },
452 { wxT("iuml"),239 },
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 },
461 { wxT("le"),8804 },
462 { wxT("lfloor"),8970 },
463 { wxT("lowast"),8727 },
464 { wxT("loz"),9674 },
465 { wxT("lrm"),8206 },
466 { wxT("lsaquo"),8249 },
467 { wxT("lsquo"),8216 },
468 { wxT("lt"),60 },
469 { wxT("macr"),175 },
470 { wxT("mdash"),8212 },
471 { wxT("micro"),181 },
472 { wxT("middot"),183 },
473 { wxT("minus"),8722 },
474 { wxT("mu"),956 },
475 { wxT("nabla"),8711 },
476 { wxT("nbsp"),160 },
477 { wxT("ndash"),8211 },
478 { wxT("ne"),8800 },
479 { wxT("ni"),8715 },
480 { wxT("not"),172 },
481 { wxT("notin"),8713 },
482 { wxT("nsub"),8836 },
483 { wxT("ntilde"),241 },
484 { wxT("nu"),957 },
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 },
493 { wxT("or"),8744 },
494 { wxT("ordf"),170 },
495 { wxT("ordm"),186 },
496 { wxT("oslash"),248 },
497 { wxT("otilde"),245 },
498 { wxT("otimes"),8855 },
499 { wxT("ouml"),246 },
500 { wxT("para"),182 },
501 { wxT("part"),8706 },
502 { wxT("permil"),8240 },
503 { wxT("perp"),8869 },
504 { wxT("phi"),966 },
505 { wxT("pi"),960 },
506 { wxT("piv"),982 },
507 { wxT("plusmn"),177 },
508 { wxT("pound"),163 },
509 { wxT("prime"),8242 },
510 { wxT("prod"),8719 },
511 { wxT("prop"),8733 },
512 { wxT("psi"),968 },
513 { wxT("quot"),34 },
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 },
522 { wxT("reg"),174 },
523 { wxT("rfloor"),8971 },
524 { wxT("rho"),961 },
525 { wxT("rlm"),8207 },
526 { wxT("rsaquo"),8250 },
527 { wxT("rsquo"),8217 },
528 { wxT("sbquo"),8218 },
529 { wxT("scaron"),353 },
530 { wxT("sdot"),8901 },
531 { wxT("sect"),167 },
532 { wxT("shy"),173 },
533 { wxT("sigma"),963 },
534 { wxT("sigmaf"),962 },
535 { wxT("sim"),8764 },
536 { wxT("spades"),9824 },
537 { wxT("sub"),8834 },
538 { wxT("sube"),8838 },
539 { wxT("sum"),8721 },
540 { wxT("sup"),8835 },
541 { wxT("sup1"),185 },
542 { wxT("sup2"),178 },
543 { wxT("sup3"),179 },
544 { wxT("supe"),8839 },
545 { wxT("szlig"),223 },
546 { wxT("tau"),964 },
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 },
560 { wxT("uml"),168 },
561 { wxT("upsih"),978 },
562 { wxT("upsilon"),965 },
563 { wxT("uuml"),252 },
564 { wxT("weierp"),8472 },
565 { wxT("xi"),958 },
566 { wxT("yacute"),253 },
567 { wxT("yen"),165 },
568 { wxT("yuml"),255 },
569 { wxT("zeta"),950 },
570 { wxT("zwj"),8205 },
571 { wxT("zwnj"),8204 },
572 {NULL, 0}};
573 static size_t substitutions_cnt = 0;
574
575 if (substitutions_cnt == 0)
576 while (substitutions[substitutions_cnt].code != 0)
577 substitutions_cnt++;
578
579 wxHtmlEntityInfo *info;
580 info = (wxHtmlEntityInfo*) bsearch(entity.c_str(), substitutions,
581 substitutions_cnt,
582 sizeof(wxHtmlEntityInfo),
583 compar_entity);
584 if (info)
585 code = info->code;
586 }
587
588 if (code == 0)
589 return wxT('?');
590 else
591 return GetCharForCode(code);
592 }
593
594 #endif