]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlpars.cpp
Changed the pragmas to allow debugging under Linux
[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/wx.h"
26 #endif
27
28 #include "wx/tokenzr.h"
29 #include "wx/wfstream.h"
30 #include "wx/url.h"
31 #include "wx/fontmap.h"
32 #include "wx/html/htmldefs.h"
33 #include "wx/html/htmlpars.h"
34
35
36
37 //-----------------------------------------------------------------------------
38 // wxHtmlParser
39 //-----------------------------------------------------------------------------
40
41 IMPLEMENT_ABSTRACT_CLASS(wxHtmlParser,wxObject)
42
43 wxHtmlParser::wxHtmlParser()
44 : wxObject(), m_Cache(NULL), m_HandlersHash(wxKEY_STRING),
45 m_FS(NULL), m_HandlersStack(NULL)
46 {
47 m_entitiesParser = new wxHtmlEntitiesParser;
48 }
49
50 wxHtmlParser::~wxHtmlParser()
51 {
52 delete m_HandlersStack;
53 m_HandlersHash.Clear();
54 m_HandlersList.DeleteContents(TRUE);
55 m_HandlersList.Clear();
56 delete m_entitiesParser;
57 }
58
59 wxObject* wxHtmlParser::Parse(const wxString& source)
60 {
61 wxObject *result;
62
63 InitParser(source);
64 DoParsing();
65 result = GetProduct();
66 DoneParser();
67 return result;
68 }
69
70 void wxHtmlParser::InitParser(const wxString& source)
71 {
72 SetSource(source);
73 }
74
75 void wxHtmlParser::DoneParser()
76 {
77 delete m_Cache;
78 m_Cache = NULL;
79 }
80
81 void wxHtmlParser::SetSource(const wxString& src)
82 {
83 m_Source = src;
84 delete m_Cache;
85 m_Cache = new wxHtmlTagsCache(m_Source);
86 }
87
88 void wxHtmlParser::DoParsing(int begin_pos, int end_pos)
89 {
90 if (end_pos <= begin_pos) return;
91
92 char c;
93 char *temp = new char[end_pos - begin_pos + 1];
94 int i;
95 int templen;
96
97 templen = 0;
98 i = begin_pos;
99
100 while (i < end_pos)
101 {
102 c = m_Source[(unsigned int) i];
103
104 // continue building word:
105 if (c != '<')
106 {
107 temp[templen++] = c;
108 i++;
109 }
110
111 else if (c == '<')
112 {
113 wxHtmlTag tag(m_Source, i, end_pos, m_Cache, m_entitiesParser);
114
115 if (templen)
116 {
117 temp[templen] = 0;
118 AddText(temp);
119 templen = 0;
120 }
121 AddTag(tag);
122 if (tag.HasEnding()) i = tag.GetEndPos2();
123 else i = tag.GetBeginPos();
124 }
125 }
126
127 if (templen)
128 { // last word of block :-(
129 temp[templen] = 0;
130 AddText(temp);
131 }
132 delete[] temp;
133 }
134
135 void wxHtmlParser::AddTag(const wxHtmlTag& tag)
136 {
137 wxHtmlTagHandler *h;
138 bool inner = FALSE;
139
140 h = (wxHtmlTagHandler*) m_HandlersHash.Get(tag.GetName());
141 if (h)
142 inner = h->HandleTag(tag);
143 if (!inner)
144 {
145 if (tag.HasEnding())
146 DoParsing(tag.GetBeginPos(), tag.GetEndPos1());
147 }
148 }
149
150 void wxHtmlParser::AddTagHandler(wxHtmlTagHandler *handler)
151 {
152 wxString s(handler->GetSupportedTags());
153 wxStringTokenizer tokenizer(s, ", ");
154
155 while (tokenizer.HasMoreTokens())
156 m_HandlersHash.Put(tokenizer.NextToken(), handler);
157
158 if (m_HandlersList.IndexOf(handler) == wxNOT_FOUND)
159 m_HandlersList.Append(handler);
160
161 handler->SetParser(this);
162 }
163
164 void wxHtmlParser::PushTagHandler(wxHtmlTagHandler *handler, wxString tags)
165 {
166 wxStringTokenizer tokenizer(tags, ", ");
167 wxString key;
168
169 if (m_HandlersStack == NULL)
170 {
171 m_HandlersStack = new wxList;
172 m_HandlersStack->DeleteContents(TRUE);
173 }
174
175 m_HandlersStack->Insert(new wxHashTable(m_HandlersHash));
176
177 while (tokenizer.HasMoreTokens())
178 {
179 key = tokenizer.NextToken();
180 m_HandlersHash.Delete(key);
181 m_HandlersHash.Put(key, handler);
182 }
183 }
184
185 void wxHtmlParser::PopTagHandler()
186 {
187 wxNode *first;
188
189 if (m_HandlersStack == NULL ||
190 (first = m_HandlersStack->GetFirst()) == NULL)
191 {
192 wxLogWarning(_("Warning: attempt to remove HTML tag handler from empty stack."));
193 return;
194 }
195 m_HandlersHash = *((wxHashTable*) first->GetData());
196 m_HandlersStack->DeleteNode(first);
197 }
198
199 //-----------------------------------------------------------------------------
200 // wxHtmlTagHandler
201 //-----------------------------------------------------------------------------
202
203 IMPLEMENT_ABSTRACT_CLASS(wxHtmlTagHandler,wxObject)
204
205
206 //-----------------------------------------------------------------------------
207 // wxHtmlEntitiesParser
208 //-----------------------------------------------------------------------------
209
210 IMPLEMENT_DYNAMIC_CLASS(wxHtmlEntitiesParser,wxObject)
211
212 wxHtmlEntitiesParser::wxHtmlEntitiesParser()
213 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
214 : m_conv(NULL), m_encoding(wxFONTENCODING_SYSTEM)
215 #endif
216 {
217 }
218
219 wxHtmlEntitiesParser::~wxHtmlEntitiesParser()
220 {
221 delete m_conv;
222 }
223
224 void wxHtmlEntitiesParser::SetEncoding(wxFontEncoding encoding)
225 {
226 #if wxUSE_WCHAR_T && !wxUSE_UNICODE
227 if (encoding == m_encoding) return;
228 delete m_conv;
229 m_conv = NULL;
230 m_encoding = encoding;
231 if (m_encoding != wxFONTENCODING_SYSTEM)
232 m_conv = new wxCSConv(wxFontMapper::GetEncodingName(m_encoding));
233 #endif
234 }
235
236 wxString wxHtmlEntitiesParser::Parse(const wxString& input)
237 {
238 const wxChar *c, *last;
239 const wxChar *in_str = input.c_str();
240 wxString output;
241
242 for (c = in_str, last = in_str; *c != wxT('\0'); c++)
243 {
244 if (*c == wxT('&'))
245 {
246 if (c - last > 0)
247 output.append(last, c - last);
248 if (++c == wxT('\0')) break;
249 wxString entity;
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);
258 last = c;
259 }
260 }
261 if (*last != wxT('\0'))
262 output.append(last);
263 return output;
264 }
265
266 struct wxHtmlEntityInfo
267 {
268 const wxChar *name;
269 unsigned code;
270 };
271
272 static int compar_entity(const void *key, const void *item)
273 {
274 return wxStrcmp((wxChar*)key, ((wxHtmlEntityInfo*)item)->name);
275 }
276
277 wxChar wxHtmlEntitiesParser::GetCharForCode(unsigned code)
278 {
279 #if wxUSE_UNICODE
280 return (wxChar)code;
281 #elif wxUSE_WCHAR_T
282 char buf[2];
283 wchar_t wbuf[2];
284 wbuf[0] = (wchar_t)code;
285 wbuf[1] = 0;
286 wxMBConv *conv = m_conv ? m_conv : &wxConvLocal;
287 if (conv->WC2MB(buf, wbuf, 1) == (size_t)-1)
288 return '?';
289 return buf[0];
290 #else
291 return (code < 256) ? (wxChar)code : '?';
292 #endif
293 }
294
295 wxChar wxHtmlEntitiesParser::GetEntityChar(const wxString& entity)
296 {
297 unsigned code = 0;
298
299 if (entity[0] == wxT('#'))
300 {
301 const wxChar *ent_s = entity.c_str();
302 const wxChar *format;
303
304 if (ent_s[1] == wxT('x') || ent_s[1] == wxT('X'))
305 {
306 format = wxT("%x");
307 ent_s++;
308 }
309 else
310 format = wxT("%u");
311 ent_s++;
312
313 if (wxSscanf(ent_s, format, &code) != 1)
314 code = 0;
315 }
316 else
317 {
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 },
326 { wxT("Auml"),196 },
327 { wxT("Beta"),914 },
328 { wxT("Ccedil"),199 },
329 { wxT("Chi"),935 },
330 { wxT("Dagger"),8225 },
331 { wxT("Delta"),916 },
332 { wxT("ETH"),208 },
333 { wxT("Eacute"),201 },
334 { wxT("Ecirc"),202 },
335 { wxT("Egrave"),200 },
336 { wxT("Epsilon"),917 },
337 { wxT("Eta"),919 },
338 { wxT("Euml"),203 },
339 { wxT("Gamma"),915 },
340 { wxT("Iacute"),205 },
341 { wxT("Icirc"),206 },
342 { wxT("Igrave"),204 },
343 { wxT("Iota"),921 },
344 { wxT("Iuml"),207 },
345 { wxT("Kappa"),922 },
346 { wxT("Lambda"),923 },
347 { wxT("Mu"),924 },
348 { wxT("Ntilde"),209 },
349 { wxT("Nu"),925 },
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 },
358 { wxT("Ouml"),214 },
359 { wxT("Phi"),934 },
360 { wxT("Pi"),928 },
361 { wxT("Prime"),8243 },
362 { wxT("Psi"),936 },
363 { wxT("Rho"),929 },
364 { wxT("Scaron"),352 },
365 { wxT("Sigma"),931 },
366 { wxT("THORN"),222 },
367 { wxT("Tau"),932 },
368 { wxT("Theta"),920 },
369 { wxT("Uacute"),218 },
370 { wxT("Ucirc"),219 },
371 { wxT("Ugrave"),217 },
372 { wxT("Upsilon"),933 },
373 { wxT("Uuml"),220 },
374 { wxT("Xi"),926 },
375 { wxT("Yacute"),221 },
376 { wxT("Yuml"),376 },
377 { wxT("Zeta"),918 },
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 },
385 { wxT("amp"),38 },
386 { wxT("and"),8743 },
387 { wxT("ang"),8736 },
388 { wxT("aring"),229 },
389 { wxT("asymp"),8776 },
390 { wxT("atilde"),227 },
391 { wxT("auml"),228 },
392 { wxT("bdquo"),8222 },
393 { wxT("beta"),946 },
394 { wxT("brvbar"),166 },
395 { wxT("bull"),8226 },
396 { wxT("cap"),8745 },
397 { wxT("ccedil"),231 },
398 { wxT("cedil"),184 },
399 { wxT("cent"),162 },
400 { wxT("chi"),967 },
401 { wxT("circ"),710 },
402 { wxT("clubs"),9827 },
403 { wxT("cong"),8773 },
404 { wxT("copy"),169 },
405 { wxT("crarr"),8629 },
406 { wxT("cup"),8746 },
407 { wxT("curren"),164 },
408 { wxT("dArr"),8659 },
409 { wxT("dagger"),8224 },
410 { wxT("darr"),8595 },
411 { wxT("deg"),176 },
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 },
423 { wxT("eta"),951 },
424 { wxT("eth"),240 },
425 { wxT("euml"),235 },
426 { wxT("euro"),8364 },
427 { wxT("exist"),8707 },
428 { wxT("fnof"),402 },
429 { wxT("forall"),8704 },
430 { wxT("frac12"),189 },
431 { wxT("frac14"),188 },
432 { wxT("frac34"),190 },
433 { wxT("frasl"),8260 },
434 { wxT("gamma"),947 },
435 { wxT("ge"),8805 },
436 { wxT("gt"),62 },
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 },
447 { wxT("int"),8747 },
448 { wxT("iota"),953 },
449 { wxT("iquest"),191 },
450 { wxT("isin"),8712 },
451 { wxT("iuml"),239 },
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 },
460 { wxT("le"),8804 },
461 { wxT("lfloor"),8970 },
462 { wxT("lowast"),8727 },
463 { wxT("loz"),9674 },
464 { wxT("lrm"),8206 },
465 { wxT("lsaquo"),8249 },
466 { wxT("lsquo"),8216 },
467 { wxT("lt"),60 },
468 { wxT("macr"),175 },
469 { wxT("mdash"),8212 },
470 { wxT("micro"),181 },
471 { wxT("middot"),183 },
472 { wxT("minus"),8722 },
473 { wxT("mu"),956 },
474 { wxT("nabla"),8711 },
475 { wxT("nbsp"),160 },
476 { wxT("ndash"),8211 },
477 { wxT("ne"),8800 },
478 { wxT("ni"),8715 },
479 { wxT("not"),172 },
480 { wxT("notin"),8713 },
481 { wxT("nsub"),8836 },
482 { wxT("ntilde"),241 },
483 { wxT("nu"),957 },
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 },
492 { wxT("or"),8744 },
493 { wxT("ordf"),170 },
494 { wxT("ordm"),186 },
495 { wxT("oslash"),248 },
496 { wxT("otilde"),245 },
497 { wxT("otimes"),8855 },
498 { wxT("ouml"),246 },
499 { wxT("para"),182 },
500 { wxT("part"),8706 },
501 { wxT("permil"),8240 },
502 { wxT("perp"),8869 },
503 { wxT("phi"),966 },
504 { wxT("pi"),960 },
505 { wxT("piv"),982 },
506 { wxT("plusmn"),177 },
507 { wxT("pound"),163 },
508 { wxT("prime"),8242 },
509 { wxT("prod"),8719 },
510 { wxT("prop"),8733 },
511 { wxT("psi"),968 },
512 { wxT("quot"),34 },
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 },
521 { wxT("reg"),174 },
522 { wxT("rfloor"),8971 },
523 { wxT("rho"),961 },
524 { wxT("rlm"),8207 },
525 { wxT("rsaquo"),8250 },
526 { wxT("rsquo"),8217 },
527 { wxT("sbquo"),8218 },
528 { wxT("scaron"),353 },
529 { wxT("sdot"),8901 },
530 { wxT("sect"),167 },
531 { wxT("shy"),173 },
532 { wxT("sigma"),963 },
533 { wxT("sigmaf"),962 },
534 { wxT("sim"),8764 },
535 { wxT("spades"),9824 },
536 { wxT("sub"),8834 },
537 { wxT("sube"),8838 },
538 { wxT("sum"),8721 },
539 { wxT("sup"),8835 },
540 { wxT("sup1"),185 },
541 { wxT("sup2"),178 },
542 { wxT("sup3"),179 },
543 { wxT("supe"),8839 },
544 { wxT("szlig"),223 },
545 { wxT("tau"),964 },
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 },
559 { wxT("uml"),168 },
560 { wxT("upsih"),978 },
561 { wxT("upsilon"),965 },
562 { wxT("uuml"),252 },
563 { wxT("weierp"),8472 },
564 { wxT("xi"),958 },
565 { wxT("yacute"),253 },
566 { wxT("yen"),165 },
567 { wxT("yuml"),255 },
568 { wxT("zeta"),950 },
569 { wxT("zwj"),8205 },
570 { wxT("zwnj"),8204 },
571 {NULL, 0}};
572 static size_t substitutions_cnt = 0;
573
574 if (substitutions_cnt == 0)
575 while (substitutions[substitutions_cnt].code != 0)
576 substitutions_cnt++;
577
578 wxHtmlEntityInfo *info;
579 info = (wxHtmlEntityInfo*) bsearch(entity.c_str(), substitutions,
580 substitutions_cnt,
581 sizeof(wxHtmlEntityInfo),
582 compar_entity);
583 if (info)
584 code = info->code;
585 }
586
587 if (code == 0)
588 return wxT('?');
589 else
590 return GetCharForCode(code);
591 }
592
593 #endif