00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <QtCore/QtAlgorithms>
00025 #include <QtCore/QRegExp>
00026 #include <QtCore/QStringList>
00027 #include <QtCore/QtDebug>
00028
00029 #include "HtmlEntities.hh"
00030
00031
00032
00033 const int HTML::MaxEntities = 248;
00034 const int HTML::FirstEntities = 10;
00035
00036
00037 const HtmlEntity HTML::first[FirstEntities] =
00038 {
00039 { "acirc", 0x00e2 },
00040 { "agrave", 0x00e0 },
00041 { "ccedil", 0x00e7 },
00042 { "eacute", 0x00e9 },
00043 { "ecirc", 0x00ea },
00044 { "egrave", 0x00e8 },
00045 { "icirc", 0x00ee },
00046 { "ocirc", 0x00f4 },
00047 { "ucirc", 0x00fb },
00048 { "ugrave", 0x00f9 },
00049 };
00050
00051 const HtmlEntity HTML::entities[MaxEntities] =
00052 {
00053 { "AElig", 0x00c6 },
00054 { "Aacute", 0x00c1 },
00055 { "Acirc", 0x00c2 },
00056 { "Agrave", 0x00c0 },
00057 { "Alpha", 0x0391 },
00058 { "AMP", 38 },
00059 { "Aring", 0x00c5 },
00060 { "Atilde", 0x00c3 },
00061 { "Auml", 0x00c4 },
00062 { "Beta", 0x0392 },
00063 { "Ccedil", 0x00c7 },
00064 { "Chi", 0x03a7 },
00065 { "Dagger", 0x2021 },
00066 { "Delta", 0x0394 },
00067 { "ETH", 0x00d0 },
00068 { "Eacute", 0x00c9 },
00069 { "Ecirc", 0x00ca },
00070 { "Egrave", 0x00c8 },
00071 { "Epsilon", 0x0395 },
00072 { "Eta", 0x0397 },
00073 { "Euml", 0x00cb },
00074 { "Gamma", 0x0393 },
00075 { "GT", 62 },
00076 { "Iacute", 0x00cd },
00077 { "Icirc", 0x00ce },
00078 { "Igrave", 0x00cc },
00079 { "Iota", 0x0399 },
00080 { "Iuml", 0x00cf },
00081 { "Kappa", 0x039a },
00082 { "Lambda", 0x039b },
00083 { "LT", 60 },
00084 { "Mu", 0x039c },
00085 { "Ntilde", 0x00d1 },
00086 { "Nu", 0x039d },
00087 { "OElig", 0x0152 },
00088 { "Oacute", 0x00d3 },
00089 { "Ocirc", 0x00d4 },
00090 { "Ograve", 0x00d2 },
00091 { "Omega", 0x03a9 },
00092 { "Omicron", 0x039f },
00093 { "Oslash", 0x00d8 },
00094 { "Otilde", 0x00d5 },
00095 { "Ouml", 0x00d6 },
00096 { "Phi", 0x03a6 },
00097 { "Pi", 0x03a0 },
00098 { "Prime", 0x2033 },
00099 { "Psi", 0x03a8 },
00100 { "QUOT", 34 },
00101 { "Rho", 0x03a1 },
00102 { "Scaron", 0x0160 },
00103 { "Sigma", 0x03a3 },
00104 { "THORN", 0x00de },
00105 { "Tau", 0x03a4 },
00106 { "Theta", 0x0398 },
00107 { "Uacute", 0x00da },
00108 { "Ucirc", 0x00db },
00109 { "Ugrave", 0x00d9 },
00110 { "Upsilon", 0x03a5 },
00111 { "Uuml", 0x00dc },
00112 { "Xi", 0x039e },
00113 { "Yacute", 0x00dd },
00114 { "Yuml", 0x0178 },
00115 { "Zeta", 0x0396 },
00116 { "aacute", 0x00e1 },
00117 { "acute", 0x00b4 },
00118 { "aelig", 0x00e6 },
00119 { "alefsym", 0x2135 },
00120 { "alpha", 0x03b1 },
00121 { "amp", 38 },
00122 { "and", 0x22a5 },
00123 { "ang", 0x2220 },
00124 { "apos", 0x0027 },
00125 { "aring", 0x00e5 },
00126 { "asymp", 0x2248 },
00127 { "atilde", 0x00e3 },
00128 { "auml", 0x00e4 },
00129 { "bdquo", 0x201e },
00130 { "beta", 0x03b2 },
00131 { "brvbar", 0x00a6 },
00132 { "bull", 0x2022 },
00133 { "cap", 0x2229 },
00134 { "cedil", 0x00b8 },
00135 { "cent", 0x00a2 },
00136 { "chi", 0x03c7 },
00137 { "circ", 0x02c6 },
00138 { "clubs", 0x2663 },
00139 { "cong", 0x2245 },
00140 { "copy", 0x00a9 },
00141 { "crarr", 0x21b5 },
00142 { "cup", 0x222a },
00143 { "cur" "ren", 0x00a4 },
00144 { "dArr", 0x21d3 },
00145 { "dagger", 0x2020 },
00146 { "darr", 0x2193 },
00147 { "deg", 0x00b0 },
00148 { "delta", 0x03b4 },
00149 { "diams", 0x2666 },
00150 { "divide", 0x00f7 },
00151 { "empty", 0x2205 },
00152 { "emsp", 0x2003 },
00153 { "ensp", 0x2002 },
00154 { "epsilon", 0x03b5 },
00155 { "equiv", 0x2261 },
00156 { "eta", 0x03b7 },
00157 { "eth", 0x00f0 },
00158 { "euml", 0x00eb },
00159 { "euro", 0x20ac },
00160 { "exist", 0x2203 },
00161 { "fnof", 0x0192 },
00162 { "forall", 0x2200 },
00163 { "frac12", 0x00bd },
00164 { "frac14", 0x00bc },
00165 { "frac34", 0x00be },
00166 { "frasl", 0x2044 },
00167 { "gamma", 0x03b3 },
00168 { "ge", 0x2265 },
00169 { "gt", 62 },
00170 { "hArr", 0x21d4 },
00171 { "harr", 0x2194 },
00172 { "hearts", 0x2665 },
00173 { "hellip", 0x2026 },
00174 { "iacute", 0x00ed },
00175 { "iexcl", 0x00a1 },
00176 { "igrave", 0x00ec },
00177 { "image", 0x2111 },
00178 { "infin", 0x221e },
00179 { "int", 0x222b },
00180 { "iota", 0x03b9 },
00181 { "iquest", 0x00bf },
00182 { "isin", 0x2208 },
00183 { "iuml", 0x00ef },
00184 { "kappa", 0x03ba },
00185 { "lArr", 0x21d0 },
00186 { "lambda", 0x03bb },
00187 { "lang", 0x2329 },
00188 { "laquo", 0x00ab },
00189 { "larr", 0x2190 },
00190 { "lceil", 0x2308 },
00191 { "ldquo", 0x201c },
00192 { "le", 0x2264 },
00193 { "lfloor", 0x230a },
00194 { "lowast", 0x2217 },
00195 { "loz", 0x25ca },
00196 { "lrm", 0x200e },
00197 { "lsaquo", 0x2039 },
00198 { "lsquo", 0x2018 },
00199 { "lt", 60 },
00200 { "macr", 0x00af },
00201 { "mdash", 0x2014 },
00202 { "micro", 0x00b5 },
00203 { "middot", 0x00b7 },
00204 { "minus", 0x2212 },
00205 { "mu", 0x03bc },
00206 { "nabla", 0x2207 },
00207 { "nbsp", 0x00a0 },
00208 { "ndash", 0x2013 },
00209 { "ne", 0x2260 },
00210 { "ni", 0x220b },
00211 { "not", 0x00ac },
00212 { "notin", 0x2209 },
00213 { "nsub", 0x2284 },
00214 { "ntilde", 0x00f1 },
00215 { "nu", 0x03bd },
00216 { "oacute", 0x00f3 },
00217 { "oelig", 0x0153 },
00218 { "ograve", 0x00f2 },
00219 { "oline", 0x203e },
00220 { "omega", 0x03c9 },
00221 { "omicron", 0x03bf },
00222 { "oplus", 0x2295 },
00223 { "or", 0x22a6 },
00224 { "ordf", 0x00aa },
00225 { "ordm", 0x00ba },
00226 { "oslash", 0x00f8 },
00227 { "otilde", 0x00f5 },
00228 { "otimes", 0x2297 },
00229 { "ouml", 0x00f6 },
00230 { "para", 0x00b6 },
00231 { "part", 0x2202 },
00232 { "percnt", 0x0025 },
00233 { "permil", 0x2030 },
00234 { "perp", 0x22a5 },
00235 { "phi", 0x03c6 },
00236 { "pi", 0x03c0 },
00237 { "piv", 0x03d6 },
00238 { "plusmn", 0x00b1 },
00239 { "pound", 0x00a3 },
00240 { "prime", 0x2032 },
00241 { "prod", 0x220f },
00242 { "prop", 0x221d },
00243 { "psi", 0x03c8 },
00244 { "quot", 34 },
00245 { "rArr", 0x21d2 },
00246 { "radic", 0x221a },
00247 { "rang", 0x232a },
00248 { "raquo", 0x00bb },
00249 { "rarr", 0x2192 },
00250 { "rceil", 0x2309 },
00251 { "rdquo", 0x201d },
00252 { "real", 0x211c },
00253 { "reg", 0x00ae },
00254 { "rfloor", 0x230b },
00255 { "rho", 0x03c1 },
00256 { "rlm", 0x200f },
00257 { "rsaquo", 0x203a },
00258 { "rsquo", 0x2019 },
00259 { "sbquo", 0x201a },
00260 { "scaron", 0x0161 },
00261 { "sdot", 0x22c5 },
00262 { "sect", 0x00a7 },
00263 { "shy", 0x00ad },
00264 { "sigma", 0x03c3 },
00265 { "sigmaf", 0x03c2 },
00266 { "sim", 0x223c },
00267 { "spades", 0x2660 },
00268 { "sub", 0x2282 },
00269 { "sube", 0x2286 },
00270 { "sum", 0x2211 },
00271 { "sup1", 0x00b9 },
00272 { "sup2", 0x00b2 },
00273 { "sup3", 0x00b3 },
00274 { "sup", 0x2283 },
00275 { "supe", 0x2287 },
00276 { "szlig", 0x00df },
00277 { "tau", 0x03c4 },
00278 { "there4", 0x2234 },
00279 { "theta", 0x03b8 },
00280 { "thetasym", 0x03d1 },
00281 { "thinsp", 0x2009 },
00282 { "thorn", 0x00fe },
00283 { "tilde", 0x02dc },
00284 { "times", 0x00d7 },
00285 { "trade", 0x2122 },
00286 { "uArr", 0x21d1 },
00287 { "uacute", 0x00fa },
00288 { "uarr", 0x2191 },
00289 { "uml", 0x00a8 },
00290 { "upsih", 0x03d2 },
00291 { "upsilon", 0x03c5 },
00292 { "uuml", 0x00fc },
00293 { "weierp", 0x2118 },
00294 { "xi", 0x03be },
00295 { "yacute", 0x00fd },
00296 { "yen", 0x00a5 },
00297 { "yuml", 0x00ff },
00298 { "zeta", 0x03b6 },
00299 { "zwj", 0x200d },
00300 { "zwnj", 0x200c }
00301 };
00302
00303
00304 const HtmlEntity* HTML::end_ent = &entities[MaxEntities];
00305 const HtmlEntity* HTML::start_ent = &entities[0];
00306
00307 const HtmlEntity* HTML::end_first = &first[FirstEntities];
00308 const HtmlEntity* HTML::start_first = &first[0];
00309
00310
00311 static bool operator < (const QString &entityStr, const HtmlEntity &entity){
00312 return entityStr < QLatin1String(entity.name);
00313 }
00314
00315
00316 static bool operator < (const HtmlEntity &entity, const QString &entityStr){
00317 return QLatin1String(entity.name) < entityStr;
00318 }
00319
00320
00321 static bool operator == (const HtmlEntity &entity, const quint16 entityCode){
00322 return entityCode == entity.code;
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 QChar HTML::resolveEntity(const QString &entity)
00356 {
00357 HtmlEntity* he = (HtmlEntity*) qBinaryFind(start_first,end_first,entity);
00358 if (he != end_first) return he->code;
00359 he = (HtmlEntity*) qBinaryFind(start_ent,end_ent,entity);
00360 if (he == end_ent) return QChar();
00361 return he->code;
00362 }
00363
00364
00365
00366
00367
00368
00369 QString HTML::resolveEntity(quint16 entityCode)
00370 {
00371 HtmlEntity* he = (HtmlEntity*) qFind(start_first,end_first,entityCode);
00372 if (he != end_first) return he->name;
00373 he = (HtmlEntity*) qFind(start_ent,end_ent,entityCode);
00374 if (he == end_ent) return QString();
00375 return he->name;
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 void HTML::decodeStr(QString* str)
00390 {
00391 int index = 0;
00392 QChar decodedChar;
00393 QRegExp entityParser("(&([a-zA-Z]+);)",Qt::CaseInsensitive);
00394
00395 while((index = entityParser.indexIn(*str,index)) != -1)
00396 {
00397 index++;
00398 decodedChar = resolveEntity(entityParser.cap(2));
00399 if (!decodedChar.isNull())
00400 str->replace(entityParser.cap(1),decodedChar);
00401 }
00402 }
00403
00404
00405
00406
00407
00408
00409 QString* HTML::decode(const QString* src)
00410 {
00411 QString* str = new QString(*src);
00412 decodeStr(str);
00413 return str;
00414 }
00415
00416
00417
00418
00419
00420
00421 QString* HTML::decode(const QString& src)
00422 {
00423 QString* str = new QString(src);
00424 decodeStr(str);
00425 return str;
00426 }
00427
00428
00429
00430
00431
00432
00433
00434
00435 QString* HTML::decode(const char* src)
00436 {
00437 QString* str = new QString(src);
00438 decodeStr(str);
00439 return str;
00440 }
00441
00442
00443
00444
00445
00446
00447
00448
00449 QString* HTML::decode(const string* src)
00450 {
00451 return decode(src->c_str());
00452 }
00453
00454
00455
00456
00457
00458
00459 QString* HTML::decode(const string& src)
00460 {
00461 return decode(src.c_str());
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 void HTML::encodeStr(QString* str)
00478 {
00479 QChar c;
00480 QString encodedChar;
00481
00482 for(int pos=0; pos<str->size(); pos++)
00483 {
00484 c = str->at(pos);
00485 encodedChar = resolveEntity(c.unicode());
00486 if (!encodedChar.isNull())
00487 {
00488 str->remove(pos, 1);
00489 str->insert(pos, QString("&%1;").arg(encodedChar.toLower()));
00490 pos += encodedChar.size() + 2;
00491 }
00492 }
00493 }
00494
00495
00496
00497
00498
00499
00500
00501 QString* HTML::encode(const QString* src)
00502 {
00503 QString* str = new QString(*src);
00504 encodeStr(str);
00505 return str;
00506 }
00507
00508
00509
00510
00511
00512
00513 QString* HTML::encode(const QString& src)
00514 {
00515 QString* str = new QString(src);
00516 encodeStr(str);
00517 return str;
00518 }
00519
00520
00521
00522
00523
00524
00525
00526
00527 QString* HTML::encode(const char* src)
00528 {
00529 QString* str = new QString(src);
00530 encodeStr(str);
00531 return str;
00532 }
00533
00534
00535
00536
00537
00538
00539
00540
00541 QString* HTML::encode(const string* src)
00542 {
00543 return encode(src->c_str());
00544 }
00545
00546
00547
00548
00549
00550
00551 QString* HTML::encode(const string& src)
00552 {
00553 return encode(src.c_str());
00554 }
00555
00556
00557
00558
00559
00560
00561