1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
|
/*
* xsltutils.c: Utilities for the XSL Transformation 1.0 engine
*
* Reference:
* http://www.w3.org/TR/1999/REC-xslt-19991116
*
* See Copyright for the status of this software.
*
* daniel@veillard.com
*/
#define IN_LIBXSLT
#include "libxslt.h"
#ifndef XSLT_NEED_TRIO
#include <stdio.h>
#else
#include <trio.h>
#endif
#include <string.h>
#include <time.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <stdarg.h>
#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlerror.h>
#include <libxml/xmlIO.h>
#include "xsltutils.h"
#include "templates.h"
#include "xsltInternals.h"
#include "imports.h"
#include "transform.h"
#if defined(_WIN32) && !defined(__CYGWIN__)
#define XSLT_WIN32_PERFORMANCE_COUNTER
#endif
/************************************************************************
* *
* Convenience function *
* *
************************************************************************/
/**
* xsltGetCNsProp:
* @style: the stylesheet
* @node: the node
* @name: the attribute name
* @nameSpace: the URI of the namespace
*
* Similar to xmlGetNsProp() but with a slightly different semantic
*
* Search and get the value of an attribute associated to a node
* This attribute has to be anchored in the namespace specified,
* or has no namespace and the element is in that namespace.
*
* This does the entity substitution.
* This function looks in DTD attribute declaration for #FIXED or
* default declaration values unless DTD use has been turned off.
*
* Returns the attribute value or NULL if not found. The string is allocated
* in the stylesheet dictionary.
*/
const xmlChar *
xsltGetCNsProp(xsltStylesheetPtr style, xmlNodePtr node,
const xmlChar *name, const xmlChar *nameSpace) {
xmlAttrPtr prop;
xmlDocPtr doc;
xmlNsPtr ns;
xmlChar *tmp;
const xmlChar *ret;
if ((node == NULL) || (style == NULL) || (style->dict == NULL))
return(NULL);
if (nameSpace == NULL)
return xmlGetProp(node, name);
if (node->type == XML_NAMESPACE_DECL)
return(NULL);
if (node->type == XML_ELEMENT_NODE)
prop = node->properties;
else
prop = NULL;
while (prop != NULL) {
/*
* One need to have
* - same attribute names
* - and the attribute carrying that namespace
*/
if ((xmlStrEqual(prop->name, name)) &&
(((prop->ns == NULL) && (node->ns != NULL) &&
(xmlStrEqual(node->ns->href, nameSpace))) ||
((prop->ns != NULL) &&
(xmlStrEqual(prop->ns->href, nameSpace))))) {
tmp = xmlNodeListGetString(node->doc, prop->children, 1);
if (tmp == NULL)
ret = xmlDictLookup(style->dict, BAD_CAST "", 0);
else {
ret = xmlDictLookup(style->dict, tmp, -1);
xmlFree(tmp);
}
return ret;
}
prop = prop->next;
}
tmp = NULL;
/*
* Check if there is a default declaration in the internal
* or external subsets
*/
doc = node->doc;
if (doc != NULL) {
if (doc->intSubset != NULL) {
xmlAttributePtr attrDecl;
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
/*
* The DTD declaration only allows a prefix search
*/
ns = xmlSearchNs(doc, node, attrDecl->prefix);
if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
return(xmlDictLookup(style->dict,
attrDecl->defaultValue, -1));
}
}
}
return(NULL);
}
/**
* xsltGetNsProp:
* @node: the node
* @name: the attribute name
* @nameSpace: the URI of the namespace
*
* Similar to xmlGetNsProp() but with a slightly different semantic
*
* Search and get the value of an attribute associated to a node
* This attribute has to be anchored in the namespace specified,
* or has no namespace and the element is in that namespace.
*
* This does the entity substitution.
* This function looks in DTD attribute declaration for #FIXED or
* default declaration values unless DTD use has been turned off.
*
* Returns the attribute value or NULL if not found.
* It's up to the caller to free the memory.
*/
xmlChar *
xsltGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
xmlAttrPtr prop;
xmlDocPtr doc;
xmlNsPtr ns;
if (node == NULL)
return(NULL);
if (nameSpace == NULL)
return xmlGetProp(node, name);
if (node->type == XML_NAMESPACE_DECL)
return(NULL);
if (node->type == XML_ELEMENT_NODE)
prop = node->properties;
else
prop = NULL;
/*
* TODO: Substitute xmlGetProp() for xmlGetNsProp(), since the former
* is not namespace-aware and will return an attribute with equal
* name regardless of its namespace.
* Example:
* <xsl:element foo:name="myName"/>
* So this would return "myName" even if an attribute @name
* in the XSLT was requested.
*/
while (prop != NULL) {
/*
* One need to have
* - same attribute names
* - and the attribute carrying that namespace
*/
if ((xmlStrEqual(prop->name, name)) &&
(((prop->ns == NULL) && (node->ns != NULL) &&
(xmlStrEqual(node->ns->href, nameSpace))) ||
((prop->ns != NULL) &&
(xmlStrEqual(prop->ns->href, nameSpace))))) {
xmlChar *ret;
ret = xmlNodeListGetString(node->doc, prop->children, 1);
if (ret == NULL) return(xmlStrdup((xmlChar *)""));
return(ret);
}
prop = prop->next;
}
/*
* Check if there is a default declaration in the internal
* or external subsets
*/
doc = node->doc;
if (doc != NULL) {
if (doc->intSubset != NULL) {
xmlAttributePtr attrDecl;
attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
if ((attrDecl == NULL) && (doc->extSubset != NULL))
attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
/*
* The DTD declaration only allows a prefix search
*/
ns = xmlSearchNs(doc, node, attrDecl->prefix);
if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
return(xmlStrdup(attrDecl->defaultValue));
}
}
}
return(NULL);
}
/**
* xsltGetUTF8Char:
* @utf: a sequence of UTF-8 encoded bytes
* @len: a pointer to @bytes len
*
* Read one UTF8 Char from @utf
* Function copied from libxml2 xmlGetUTF8Char() ... to discard ultimately
* and use the original API
*
* Returns the char value or -1 in case of error and update @len with the
* number of bytes used
*/
int
xsltGetUTF8Char(const unsigned char *utf, int *len) {
unsigned int c;
if (utf == NULL)
goto error;
if (len == NULL)
goto error;
if (*len < 1)
goto error;
c = utf[0];
if (c & 0x80) {
if (*len < 2)
goto error;
if ((utf[1] & 0xc0) != 0x80)
goto error;
if ((c & 0xe0) == 0xe0) {
if (*len < 3)
goto error;
if ((utf[2] & 0xc0) != 0x80)
goto error;
if ((c & 0xf0) == 0xf0) {
if (*len < 4)
goto error;
if ((c & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
goto error;
*len = 4;
/* 4-byte code */
c = (utf[0] & 0x7) << 18;
c |= (utf[1] & 0x3f) << 12;
c |= (utf[2] & 0x3f) << 6;
c |= utf[3] & 0x3f;
} else {
/* 3-byte code */
*len = 3;
c = (utf[0] & 0xf) << 12;
c |= (utf[1] & 0x3f) << 6;
c |= utf[2] & 0x3f;
}
} else {
/* 2-byte code */
*len = 2;
c = (utf[0] & 0x1f) << 6;
c |= utf[1] & 0x3f;
}
} else {
/* 1-byte code */
*len = 1;
}
return(c);
error:
if (len != NULL)
*len = 0;
return(-1);
}
#ifdef XSLT_REFACTORED
/**
* xsltPointerListAddSize:
* @list: the pointer list structure
* @item: the item to be stored
* @initialSize: the initial size of the list
*
* Adds an item to the list.
*
* Returns the position of the added item in the list or
* -1 in case of an error.
*/
int
xsltPointerListAddSize(xsltPointerListPtr list,
void *item,
int initialSize)
{
if (list->items == NULL) {
if (initialSize <= 0)
initialSize = 1;
list->items = (void **) xmlMalloc(
initialSize * sizeof(void *));
if (list->items == NULL) {
xsltGenericError(xsltGenericErrorContext,
"xsltPointerListAddSize: memory allocation failure.\n");
return(-1);
}
list->number = 0;
list->size = initialSize;
} else if (list->size <= list->number) {
list->size *= 2;
list->items = (void **) xmlRealloc(list->items,
list->size * sizeof(void *));
if (list->items == NULL) {
xsltGenericError(xsltGenericErrorContext,
"xsltPointerListAddSize: memory re-allocation failure.\n");
list->size = 0;
return(-1);
}
}
list->items[list->number++] = item;
return(0);
}
/**
* xsltPointerListCreate:
* @initialSize: the initial size for the list
*
* Creates an xsltPointerList structure.
*
* Returns a xsltPointerList structure or NULL in case of an error.
*/
xsltPointerListPtr
xsltPointerListCreate(int initialSize)
{
xsltPointerListPtr ret;
ret = xmlMalloc(sizeof(xsltPointerList));
if (ret == NULL) {
xsltGenericError(xsltGenericErrorContext,
"xsltPointerListCreate: memory allocation failure.\n");
return (NULL);
}
memset(ret, 0, sizeof(xsltPointerList));
if (initialSize > 0) {
xsltPointerListAddSize(ret, NULL, initialSize);
ret->number = 0;
}
return (ret);
}
/**
* xsltPointerListFree:
* @list: pointer to the list to be freed
*
* Frees the xsltPointerList structure. This does not free
* the content of the list.
*/
void
xsltPointerListFree(xsltPointerListPtr list)
{
if (list == NULL)
return;
if (list->items != NULL)
xmlFree(list->items);
xmlFree(list);
}
/**
* xsltPointerListClear:
* @list: pointer to the list to be cleared
*
* Resets the list, but does not free the allocated array
* and does not free the content of the list.
*/
void
xsltPointerListClear(xsltPointerListPtr list)
{
if (list->items != NULL) {
xmlFree(list->items);
list->items = NULL;
}
list->number = 0;
list->size = 0;
}
#endif /* XSLT_REFACTORED */
/************************************************************************
* *
* Handling of XSLT stylesheets messages *
* *
************************************************************************/
/**
* xsltMessage:
* @ctxt: an XSLT processing context
* @node: The current node
* @inst: The node containing the message instruction
*
* Process and xsl:message construct
*/
void
xsltMessage(xsltTransformContextPtr ctxt, xmlNodePtr node, xmlNodePtr inst) {
xmlGenericErrorFunc error = xsltGenericError;
void *errctx = xsltGenericErrorContext;
xmlChar *prop, *message;
int terminate = 0;
if ((ctxt == NULL) || (inst == NULL))
return;
if (ctxt->error != NULL) {
error = ctxt->error;
errctx = ctxt->errctx;
}
prop = xmlGetNsProp(inst, (const xmlChar *)"terminate", NULL);
if (prop != NULL) {
if (xmlStrEqual(prop, (const xmlChar *)"yes")) {
terminate = 1;
} else if (xmlStrEqual(prop, (const xmlChar *)"no")) {
terminate = 0;
} else {
xsltTransformError(ctxt, NULL, inst,
"xsl:message : terminate expecting 'yes' or 'no'\n");
}
xmlFree(prop);
}
message = xsltEvalTemplateString(ctxt, node, inst);
if (message != NULL) {
int len = xmlStrlen(message);
error(errctx, "%s", (const char *)message);
if ((len > 0) && (message[len - 1] != '\n'))
error(errctx, "\n");
xmlFree(message);
}
if (terminate)
ctxt->state = XSLT_STATE_STOPPED;
}
/************************************************************************
* *
* Handling of out of context errors *
* *
************************************************************************/
#define XSLT_GET_VAR_STR(msg, str) { \
int size; \
int chars; \
char *larger; \
va_list ap; \
\
str = (char *) xmlMalloc(150); \
if (str == NULL) \
return; \
\
size = 150; \
\
while (size < 64000) { \
va_start(ap, msg); \
chars = vsnprintf(str, size, msg, ap); \
va_end(ap); \
if ((chars > -1) && (chars < size)) \
break; \
if (chars > -1) \
size += chars + 1; \
else \
size += 100; \
if ((larger = (char *) xmlRealloc(str, size)) == NULL) {\
xmlFree(str); \
return; \
} \
str = larger; \
} \
}
/**
* xsltGenericErrorDefaultFunc:
* @ctx: an error context
* @msg: the message to display/transmit
* @...: extra parameters for the message display
*
* Default handler for out of context error messages.
*/
static void LIBXSLT_ATTR_FORMAT(2,3)
xsltGenericErrorDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
va_list args;
if (xsltGenericErrorContext == NULL)
xsltGenericErrorContext = (void *) stderr;
va_start(args, msg);
vfprintf((FILE *)xsltGenericErrorContext, msg, args);
va_end(args);
}
xmlGenericErrorFunc xsltGenericError = xsltGenericErrorDefaultFunc;
void *xsltGenericErrorContext = NULL;
/**
* xsltSetGenericErrorFunc:
* @ctx: the new error handling context
* @handler: the new handler function
*
* Function to reset the handler and the error context for out of
* context error messages.
* This simply means that @handler will be called for subsequent
* error messages while not parsing nor validating. And @ctx will
* be passed as first argument to @handler
* One can simply force messages to be emitted to another FILE * than
* stderr by setting @ctx to this file handle and @handler to NULL.
*/
void
xsltSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
xsltGenericErrorContext = ctx;
if (handler != NULL)
xsltGenericError = handler;
else
xsltGenericError = xsltGenericErrorDefaultFunc;
}
/**
* xsltGenericDebugDefaultFunc:
* @ctx: an error context
* @msg: the message to display/transmit
* @...: extra parameters for the message display
*
* Default handler for out of context error messages.
*/
static void LIBXSLT_ATTR_FORMAT(2,3)
xsltGenericDebugDefaultFunc(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) {
va_list args;
if (xsltGenericDebugContext == NULL)
return;
va_start(args, msg);
vfprintf((FILE *)xsltGenericDebugContext, msg, args);
va_end(args);
}
xmlGenericErrorFunc xsltGenericDebug = xsltGenericDebugDefaultFunc;
void *xsltGenericDebugContext = NULL;
/**
* xsltSetGenericDebugFunc:
* @ctx: the new error handling context
* @handler: the new handler function
*
* Function to reset the handler and the error context for out of
* context error messages.
* This simply means that @handler will be called for subsequent
* error messages while not parsing or validating. And @ctx will
* be passed as first argument to @handler
* One can simply force messages to be emitted to another FILE * than
* stderr by setting @ctx to this file handle and @handler to NULL.
*/
void
xsltSetGenericDebugFunc(void *ctx, xmlGenericErrorFunc handler) {
xsltGenericDebugContext = ctx;
if (handler != NULL)
xsltGenericDebug = handler;
else
xsltGenericDebug = xsltGenericDebugDefaultFunc;
}
/**
* xsltPrintErrorContext:
* @ctxt: the transformation context
* @style: the stylesheet
* @node: the current node being processed
*
* Display the context of an error.
*/
void
xsltPrintErrorContext(xsltTransformContextPtr ctxt,
xsltStylesheetPtr style, xmlNodePtr node) {
int line = 0;
const xmlChar *file = NULL;
const xmlChar *name = NULL;
const char *type = "error";
xmlGenericErrorFunc error = xsltGenericError;
void *errctx = xsltGenericErrorContext;
if (ctxt != NULL) {
if (ctxt->state == XSLT_STATE_OK)
ctxt->state = XSLT_STATE_ERROR;
if (ctxt->error != NULL) {
error = ctxt->error;
errctx = ctxt->errctx;
}
}
if ((node == NULL) && (ctxt != NULL))
node = ctxt->inst;
if (node != NULL) {
if ((node->type == XML_DOCUMENT_NODE) ||
(node->type == XML_HTML_DOCUMENT_NODE)) {
xmlDocPtr doc = (xmlDocPtr) node;
file = doc->URL;
} else {
line = xmlGetLineNo(node);
if ((node->doc != NULL) && (node->doc->URL != NULL))
file = node->doc->URL;
if (node->name != NULL)
name = node->name;
}
}
if (ctxt != NULL)
type = "runtime error";
else if (style != NULL) {
#ifdef XSLT_REFACTORED
if (XSLT_CCTXT(style)->errSeverity == XSLT_ERROR_SEVERITY_WARNING)
type = "compilation warning";
else
type = "compilation error";
#else
type = "compilation error";
#endif
}
if ((file != NULL) && (line != 0) && (name != NULL))
error(errctx, "%s: file %s line %d element %s\n",
type, file, line, name);
else if ((file != NULL) && (name != NULL))
error(errctx, "%s: file %s element %s\n", type, file, name);
else if ((file != NULL) && (line != 0))
error(errctx, "%s: file %s line %d\n", type, file, line);
else if (file != NULL)
error(errctx, "%s: file %s\n", type, file);
else if (name != NULL)
error(errctx, "%s: element %s\n", type, name);
else
error(errctx, "%s\n", type);
}
/**
* xsltSetTransformErrorFunc:
* @ctxt: the XSLT transformation context
* @ctx: the new error handling context
* @handler: the new handler function
*
* Function to reset the handler and the error context for out of
* context error messages specific to a given XSLT transromation.
*
* This simply means that @handler will be called for subsequent
* error messages while running the transformation.
*/
void
xsltSetTransformErrorFunc(xsltTransformContextPtr ctxt,
void *ctx, xmlGenericErrorFunc handler)
{
ctxt->error = handler;
ctxt->errctx = ctx;
}
/**
* xsltTransformError:
* @ctxt: an XSLT transformation context
* @style: the XSLT stylesheet used
* @node: the current node in the stylesheet
* @msg: the message to display/transmit
* @...: extra parameters for the message display
*
* Display and format an error messages, gives file, line, position and
* extra parameters, will use the specific transformation context if available
*/
void
xsltTransformError(xsltTransformContextPtr ctxt,
xsltStylesheetPtr style,
xmlNodePtr node,
const char *msg, ...) {
xmlGenericErrorFunc error = xsltGenericError;
void *errctx = xsltGenericErrorContext;
char * str;
if (ctxt != NULL) {
if (ctxt->state == XSLT_STATE_OK)
ctxt->state = XSLT_STATE_ERROR;
if (ctxt->error != NULL) {
error = ctxt->error;
errctx = ctxt->errctx;
}
}
if ((node == NULL) && (ctxt != NULL))
node = ctxt->inst;
xsltPrintErrorContext(ctxt, style, node);
XSLT_GET_VAR_STR(msg, str);
error(errctx, "%s", str);
if (str != NULL)
xmlFree(str);
}
/************************************************************************
* *
* QNames *
* *
************************************************************************/
/**
* xsltSplitQName:
* @dict: a dictionary
* @name: the full QName
* @prefix: the return value
*
* Split QNames into prefix and local names, both allocated from a dictionary.
*
* Returns: the localname or NULL in case of error.
*/
const xmlChar *
xsltSplitQName(xmlDictPtr dict, const xmlChar *name, const xmlChar **prefix) {
int len = 0;
const xmlChar *ret = NULL;
*prefix = NULL;
if ((name == NULL) || (dict == NULL)) return(NULL);
if (name[0] == ':')
return(xmlDictLookup(dict, name, -1));
while ((name[len] != 0) && (name[len] != ':')) len++;
if (name[len] == 0) return(xmlDictLookup(dict, name, -1));
*prefix = xmlDictLookup(dict, name, len);
ret = xmlDictLookup(dict, &name[len + 1], -1);
return(ret);
}
/**
* xsltGetQNameURI:
* @node: the node holding the QName
* @name: pointer to the initial QName value
*
* This function analyzes @name, if the name contains a prefix,
* the function seaches the associated namespace in scope for it.
* It will also replace @name value with the NCName, the old value being
* freed.
* Errors in the prefix lookup are signalled by setting @name to NULL.
*
* NOTE: the namespace returned is a pointer to the place where it is
* defined and hence has the same lifespan as the document holding it.
*
* Returns the namespace URI if there is a prefix, or NULL if @name is
* not prefixed.
*/
const xmlChar *
xsltGetQNameURI(xmlNodePtr node, xmlChar ** name)
{
int len = 0;
xmlChar *qname;
xmlNsPtr ns;
if (name == NULL)
return(NULL);
qname = *name;
if ((qname == NULL) || (*qname == 0))
return(NULL);
if (node == NULL) {
xsltGenericError(xsltGenericErrorContext,
"QName: no element for namespace lookup %s\n",
qname);
xmlFree(qname);
*name = NULL;
return(NULL);
}
/* nasty but valid */
if (qname[0] == ':')
return(NULL);
/*
* we are not trying to validate but just to cut, and yes it will
* work even if this is a set of UTF-8 encoded chars
*/
while ((qname[len] != 0) && (qname[len] != ':'))
len++;
if (qname[len] == 0)
return(NULL);
/*
* handle xml: separately, this one is magical
*/
if ((qname[0] == 'x') && (qname[1] == 'm') &&
(qname[2] == 'l') && (qname[3] == ':')) {
if (qname[4] == 0)
return(NULL);
*name = xmlStrdup(&qname[4]);
xmlFree(qname);
return(XML_XML_NAMESPACE);
}
qname[len] = 0;
ns = xmlSearchNs(node->doc, node, qname);
if (ns == NULL) {
xsltGenericError(xsltGenericErrorContext,
"%s:%s : no namespace bound to prefix %s\n",
qname, &qname[len + 1], qname);
*name = NULL;
xmlFree(qname);
return(NULL);
}
*name = xmlStrdup(&qname[len + 1]);
xmlFree(qname);
return(ns->href);
}
/**
* xsltGetQNameURI2:
* @style: stylesheet pointer
* @node: the node holding the QName
* @name: pointer to the initial QName value
*
* This function is similar to xsltGetQNameURI, but is used when
* @name is a dictionary entry.
*
* Returns the namespace URI if there is a prefix, or NULL if @name is
* not prefixed.
*/
const xmlChar *
xsltGetQNameURI2(xsltStylesheetPtr style, xmlNodePtr node,
const xmlChar **name) {
int len = 0;
xmlChar *qname;
xmlNsPtr ns;
if (name == NULL)
return(NULL);
qname = (xmlChar *)*name;
if ((qname == NULL) || (*qname == 0))
return(NULL);
if (node == NULL) {
xsltGenericError(xsltGenericErrorContext,
"QName: no element for namespace lookup %s\n",
qname);
*name = NULL;
return(NULL);
}
/*
* we are not trying to validate but just to cut, and yes it will
* work even if this is a set of UTF-8 encoded chars
*/
while ((qname[len] != 0) && (qname[len] != ':'))
len++;
if (qname[len] == 0)
return(NULL);
/*
* handle xml: separately, this one is magical
*/
if ((qname[0] == 'x') && (qname[1] == 'm') &&
(qname[2] == 'l') && (qname[3] == ':')) {
if (qname[4] == 0)
return(NULL);
*name = xmlDictLookup(style->dict, &qname[4], -1);
return(XML_XML_NAMESPACE);
}
qname = xmlStrndup(*name, len);
ns = xmlSearchNs(node->doc, node, qname);
if (ns == NULL) {
if (style) {
xsltTransformError(NULL, style, node,
"No namespace bound to prefix '%s'.\n",
qname);
style->errors++;
} else {
xsltGenericError(xsltGenericErrorContext,
"%s : no namespace bound to prefix %s\n",
*name, qname);
}
*name = NULL;
xmlFree(qname);
return(NULL);
}
*name = xmlDictLookup(style->dict, (*name)+len+1, -1);
xmlFree(qname);
return(ns->href);
}
/************************************************************************
* *
* Sorting *
* *
************************************************************************/
/**
* xsltDocumentSortFunction:
* @list: the node set
*
* reorder the current node list @list accordingly to the document order
* This function is slow, obsolete and should not be used anymore.
*/
void
xsltDocumentSortFunction(xmlNodeSetPtr list) {
int i, j;
int len, tst;
xmlNodePtr node;
if (list == NULL)
return;
len = list->nodeNr;
if (len <= 1)
return;
/* TODO: sort is really not optimized, does it needs to ? */
for (i = 0;i < len -1;i++) {
for (j = i + 1; j < len; j++) {
tst = xmlXPathCmpNodes(list->nodeTab[i], list->nodeTab[j]);
if (tst == -1) {
node = list->nodeTab[i];
list->nodeTab[i] = list->nodeTab[j];
list->nodeTab[j] = node;
}
}
}
}
/**
* xsltComputeSortResultiInternal:
* @ctxt: a XSLT process context
* @sort: node list
* @xfrm: Transform strings according to locale
*
* reorder the current node list accordingly to the set of sorting
* requirement provided by the array of nodes.
*
* Returns a ordered XPath nodeset or NULL in case of error.
*/
static xmlXPathObjectPtr *
xsltComputeSortResultInternal(xsltTransformContextPtr ctxt, xmlNodePtr sort,
int xfrm) {
#ifdef XSLT_REFACTORED
xsltStyleItemSortPtr comp;
#else
xsltStylePreCompPtr comp;
#endif
xmlXPathObjectPtr *results = NULL;
xmlNodeSetPtr list = NULL;
xmlXPathObjectPtr res;
int len = 0;
int i;
xmlNodePtr oldNode;
xmlNodePtr oldInst;
int oldPos, oldSize ;
int oldNsNr;
xmlNsPtr *oldNamespaces;
comp = sort->psvi;
if (comp == NULL) {
xsltGenericError(xsltGenericErrorContext,
"xsl:sort : compilation failed\n");
return(NULL);
}
if ((comp->select == NULL) || (comp->comp == NULL))
return(NULL);
list = ctxt->nodeList;
if ((list == NULL) || (list->nodeNr <= 1))
return(NULL);
len = list->nodeNr;
/* TODO: xsl:sort lang attribute */
/* TODO: xsl:sort case-order attribute */
results = xmlMalloc(len * sizeof(xmlXPathObjectPtr));
if (results == NULL) {
xsltGenericError(xsltGenericErrorContext,
"xsltComputeSortResult: memory allocation failure\n");
return(NULL);
}
oldInst = ctxt->inst;
oldNode = ctxt->xpathCtxt->node;
oldPos = ctxt->xpathCtxt->proximityPosition;
oldSize = ctxt->xpathCtxt->contextSize;
oldNsNr = ctxt->xpathCtxt->nsNr;
oldNamespaces = ctxt->xpathCtxt->namespaces;
for (i = 0;i < len;i++) {
ctxt->inst = sort;
ctxt->xpathCtxt->contextSize = len;
ctxt->xpathCtxt->proximityPosition = i + 1;
ctxt->node = list->nodeTab[i];
ctxt->xpathCtxt->node = ctxt->node;
#ifdef XSLT_REFACTORED
if (comp->inScopeNs != NULL) {
ctxt->xpathCtxt->namespaces = comp->inScopeNs->list;
ctxt->xpathCtxt->nsNr = comp->inScopeNs->xpathNumber;
} else {
ctxt->xpathCtxt->namespaces = NULL;
ctxt->xpathCtxt->nsNr = 0;
}
#else
ctxt->xpathCtxt->namespaces = comp->nsList;
ctxt->xpathCtxt->nsNr = comp->nsNr;
#endif
res = xmlXPathCompiledEval(comp->comp, ctxt->xpathCtxt);
if (res != NULL) {
if (res->type != XPATH_STRING)
res = xmlXPathConvertString(res);
if (comp->number)
res = xmlXPathConvertNumber(res);
res->index = i; /* Save original pos for dupl resolv */
if (comp->number) {
if (res->type == XPATH_NUMBER) {
results[i] = res;
} else {
#ifdef WITH_XSLT_DEBUG_PROCESS
xsltGenericDebug(xsltGenericDebugContext,
"xsltComputeSortResult: select didn't evaluate to a number\n");
#endif
results[i] = NULL;
}
} else {
if (res->type == XPATH_STRING) {
if ((xfrm) && (comp->locale != (xsltLocale)0)) {
xmlChar *str = res->stringval;
res->stringval = (xmlChar *) xsltStrxfrm(comp->locale, str);
xmlFree(str);
}
results[i] = res;
} else {
#ifdef WITH_XSLT_DEBUG_PROCESS
xsltGenericDebug(xsltGenericDebugContext,
"xsltComputeSortResult: select didn't evaluate to a string\n");
#endif
results[i] = NULL;
}
}
} else {
ctxt->state = XSLT_STATE_STOPPED;
results[i] = NULL;
}
}
ctxt->inst = oldInst;
ctxt->xpathCtxt->node = oldNode;
ctxt->xpathCtxt->contextSize = oldSize;
ctxt->xpathCtxt->proximityPosition = oldPos;
ctxt->xpathCtxt->nsNr = oldNsNr;
ctxt->xpathCtxt->namespaces = oldNamespaces;
return(results);
}
/**
* xsltComputeSortResult:
* @ctxt: a XSLT process context
* @sort: node list
*
* reorder the current node list accordingly to the set of sorting
* requirement provided by the array of nodes.
*
* Returns a ordered XPath nodeset or NULL in case of error.
*/
xmlXPathObjectPtr *
xsltComputeSortResult(xsltTransformContextPtr ctxt, xmlNodePtr sort) {
return xsltComputeSortResultInternal(ctxt, sort, /* xfrm */ 0);
}
/**
* xsltDefaultSortFunction:
* @ctxt: a XSLT process context
* @sorts: array of sort nodes
* @nbsorts: the number of sorts in the array
*
* reorder the current node list accordingly to the set of sorting
* requirement provided by the arry of nodes.
*/
void
xsltDefaultSortFunction(xsltTransformContextPtr ctxt, xmlNodePtr *sorts,
int nbsorts) {
#ifdef XSLT_REFACTORED
xsltStyleItemSortPtr comp;
#else
xsltStylePreCompPtr comp;
#endif
xmlXPathObjectPtr *resultsTab[XSLT_MAX_SORT];
xmlXPathObjectPtr *results = NULL, *res;
xmlNodeSetPtr list = NULL;
int descending, number, desc, numb;
int len = 0;
int i, j, incr;
int tst;
int depth;
xmlNodePtr node;
xmlXPathObjectPtr tmp;
int tempstype[XSLT_MAX_SORT], temporder[XSLT_MAX_SORT],
templang[XSLT_MAX_SORT];
if ((ctxt == NULL) || (sorts == NULL) || (nbsorts <= 0) ||
(nbsorts >= XSLT_MAX_SORT))
return;
if (sorts[0] == NULL)
return;
comp = sorts[0]->psvi;
if (comp == NULL)
return;
list = ctxt->nodeList;
if ((list == NULL) || (list->nodeNr <= 1))
return; /* nothing to do */
for (j = 0; j < nbsorts; j++) {
comp = sorts[j]->psvi;
tempstype[j] = 0;
if ((comp->stype == NULL) && (comp->has_stype != 0)) {
comp->stype =
xsltEvalAttrValueTemplate(ctxt, sorts[j],
(const xmlChar *) "data-type",
NULL);
if (comp->stype != NULL) {
tempstype[j] = 1;
if (xmlStrEqual(comp->stype, (const xmlChar *) "text"))
comp->number = 0;
else if (xmlStrEqual(comp->stype, (const xmlChar *) "number"))
comp->number = 1;
else {
xsltTransformError(ctxt, NULL, sorts[j],
"xsltDoSortFunction: no support for data-type = %s\n",
comp->stype);
comp->number = 0; /* use default */
}
}
}
temporder[j] = 0;
if ((comp->order == NULL) && (comp->has_order != 0)) {
comp->order = xsltEvalAttrValueTemplate(ctxt, sorts[j],
(const xmlChar *) "order",
NULL);
if (comp->order != NULL) {
temporder[j] = 1;
if (xmlStrEqual(comp->order, (const xmlChar *) "ascending"))
comp->descending = 0;
else if (xmlStrEqual(comp->order,
(const xmlChar *) "descending"))
comp->descending = 1;
else {
xsltTransformError(ctxt, NULL, sorts[j],
"xsltDoSortFunction: invalid value %s for order\n",
comp->order);
comp->descending = 0; /* use default */
}
}
}
templang[j] = 0;
if ((comp->lang == NULL) && (comp->has_lang != 0)) {
xmlChar *lang = xsltEvalAttrValueTemplate(ctxt, sorts[j],
(xmlChar *) "lang",
NULL);
if (lang != NULL) {
templang[j] = 1;
comp->locale = xsltNewLocale(lang);
xmlFree(lang);
}
}
}
len = list->nodeNr;
resultsTab[0] = xsltComputeSortResultInternal(ctxt, sorts[0],
/* xfrm */ 1);
for (i = 1;i < XSLT_MAX_SORT;i++)
resultsTab[i] = NULL;
results = resultsTab[0];
comp = sorts[0]->psvi;
descending = comp->descending;
number = comp->number;
if (results == NULL)
goto cleanup;
/* Shell's sort of node-set */
for (incr = len / 2; incr > 0; incr /= 2) {
for (i = incr; i < len; i++) {
j = i - incr;
if (results[i] == NULL)
continue;
while (j >= 0) {
if (results[j] == NULL)
tst = 1;
else {
if (number) {
/* We make NaN smaller than number in accordance
with XSLT spec */
if (xmlXPathIsNaN(results[j]->floatval)) {
if (xmlXPathIsNaN(results[j + incr]->floatval))
tst = 0;
else
tst = -1;
} else if (xmlXPathIsNaN(results[j + incr]->floatval))
tst = 1;
else if (results[j]->floatval ==
results[j + incr]->floatval)
tst = 0;
else if (results[j]->floatval >
results[j + incr]->floatval)
tst = 1;
else tst = -1;
} else if(comp->locale != (xsltLocale)0) {
tst = xsltLocaleStrcmp(
comp->locale,
(xsltLocaleChar *) results[j]->stringval,
(xsltLocaleChar *) results[j + incr]->stringval);
} else {
tst = xmlStrcmp(results[j]->stringval,
results[j + incr]->stringval);
}
if (descending)
tst = -tst;
}
if (tst == 0) {
/*
* Okay we need to use multi level sorts
*/
depth = 1;
while (depth < nbsorts) {
if (sorts[depth] == NULL)
break;
comp = sorts[depth]->psvi;
if (comp == NULL)
break;
desc = comp->descending;
numb = comp->number;
/*
* Compute the result of the next level for the
* full set, this might be optimized ... or not
*/
if (resultsTab[depth] == NULL)
resultsTab[depth] =
xsltComputeSortResultInternal(ctxt,
sorts[depth],
/* xfrm */ 1);
res = resultsTab[depth];
if (res == NULL)
break;
if (res[j] == NULL) {
if (res[j+incr] != NULL)
tst = 1;
} else if (res[j+incr] == NULL) {
tst = -1;
} else {
if (numb) {
/* We make NaN smaller than number in
accordance with XSLT spec */
if (xmlXPathIsNaN(res[j]->floatval)) {
if (xmlXPathIsNaN(res[j +
incr]->floatval))
tst = 0;
else
tst = -1;
} else if (xmlXPathIsNaN(res[j + incr]->
floatval))
tst = 1;
else if (res[j]->floatval == res[j + incr]->
floatval)
tst = 0;
else if (res[j]->floatval >
res[j + incr]->floatval)
tst = 1;
else tst = -1;
} else if(comp->locale != (xsltLocale)0) {
tst = xsltLocaleStrcmp(
comp->locale,
(xsltLocaleChar *) res[j]->stringval,
(xsltLocaleChar *) res[j + incr]->stringval);
} else {
tst = xmlStrcmp(res[j]->stringval,
res[j + incr]->stringval);
}
if (desc)
tst = -tst;
}
/*
* if we still can't differenciate at this level
* try one level deeper.
*/
if (tst != 0)
break;
depth++;
}
}
if (tst == 0) {
tst = results[j]->index > results[j + incr]->index;
}
if (tst > 0) {
tmp = results[j];
results[j] = results[j + incr];
results[j + incr] = tmp;
node = list->nodeTab[j];
list->nodeTab[j] = list->nodeTab[j + incr];
list->nodeTab[j + incr] = node;
depth = 1;
while (depth < nbsorts) {
if (sorts[depth] == NULL)
break;
if (resultsTab[depth] == NULL)
break;
res = resultsTab[depth];
tmp = res[j];
res[j] = res[j + incr];
res[j + incr] = tmp;
depth++;
}
j -= incr;
} else
break;
}
}
}
cleanup:
for (j = 0; j < nbsorts; j++) {
comp = sorts[j]->psvi;
if (tempstype[j] == 1) {
/* The data-type needs to be recomputed each time */
xmlFree((void *)(comp->stype));
comp->stype = NULL;
}
if (temporder[j] == 1) {
/* The order needs to be recomputed each time */
xmlFree((void *)(comp->order));
comp->order = NULL;
}
if (templang[j] == 1) {
xsltFreeLocale(comp->locale);
comp->locale = (xsltLocale)0;
}
if (resultsTab[j] != NULL) {
for (i = 0;i < len;i++)
xmlXPathFreeObject(resultsTab[j][i]);
xmlFree(resultsTab[j]);
}
}
}
static xsltSortFunc xsltSortFunction = xsltDefaultSortFunction;
/**
* xsltDoSortFunction:
* @ctxt: a XSLT process context
* @sorts: array of sort nodes
* @nbsorts: the number of sorts in the array
*
* reorder the current node list accordingly to the set of sorting
* requirement provided by the arry of nodes.
* This is a wrapper function, the actual function used is specified
* using xsltSetCtxtSortFunc() to set the context specific sort function,
* or xsltSetSortFunc() to set the global sort function.
* If a sort function is set on the context, this will get called.
* Otherwise the global sort function is called.
*/
void
xsltDoSortFunction(xsltTransformContextPtr ctxt, xmlNodePtr * sorts,
int nbsorts)
{
if (ctxt->sortfunc != NULL)
(ctxt->sortfunc)(ctxt, sorts, nbsorts);
else if (xsltSortFunction != NULL)
xsltSortFunction(ctxt, sorts, nbsorts);
}
/**
* xsltSetSortFunc:
* @handler: the new handler function
*
* Function to reset the global handler for XSLT sorting.
* If the handler is NULL, the default sort function will be used.
*/
void
xsltSetSortFunc(xsltSortFunc handler) {
if (handler != NULL)
xsltSortFunction = handler;
else
xsltSortFunction = xsltDefaultSortFunction;
}
/**
* xsltSetCtxtSortFunc:
* @ctxt: a XSLT process context
* @handler: the new handler function
*
* Function to set the handler for XSLT sorting
* for the specified context.
* If the handler is NULL, then the global
* sort function will be called
*/
void
xsltSetCtxtSortFunc(xsltTransformContextPtr ctxt, xsltSortFunc handler) {
ctxt->sortfunc = handler;
}
/************************************************************************
* *
* Parsing options *
* *
************************************************************************/
/**
* xsltSetCtxtParseOptions:
* @ctxt: a XSLT process context
* @options: a combination of libxml2 xmlParserOption
*
* Change the default parser option passed by the XSLT engine to the
* parser when using document() loading.
*
* Returns the previous options or -1 in case of error
*/
int
xsltSetCtxtParseOptions(xsltTransformContextPtr ctxt, int options)
{
int oldopts;
if (ctxt == NULL)
return(-1);
oldopts = ctxt->parserOptions;
if (ctxt->xinclude)
oldopts |= XML_PARSE_XINCLUDE;
ctxt->parserOptions = options;
if (options & XML_PARSE_XINCLUDE)
ctxt->xinclude = 1;
else
ctxt->xinclude = 0;
return(oldopts);
}
/************************************************************************
* *
* Output *
* *
************************************************************************/
/**
* xsltSaveResultTo:
* @buf: an output buffer
* @result: the result xmlDocPtr
* @style: the stylesheet
*
* Save the result @result obtained by applying the @style stylesheet
* to an I/O output channel @buf
*
* Returns the number of byte written or -1 in case of failure.
*/
int
xsltSaveResultTo(xmlOutputBufferPtr buf, xmlDocPtr result,
xsltStylesheetPtr style) {
const xmlChar *encoding;
int base;
const xmlChar *method;
int indent;
if ((buf == NULL) || (result == NULL) || (style == NULL))
return(-1);
if ((result->children == NULL) ||
((result->children->type == XML_DTD_NODE) &&
(result->children->next == NULL)))
return(0);
if ((style->methodURI != NULL) &&
((style->method == NULL) ||
(!xmlStrEqual(style->method, (const xmlChar *) "xhtml")))) {
xsltGenericError(xsltGenericErrorContext,
"xsltSaveResultTo : unknown output method\n");
return(-1);
}
base = buf->written;
XSLT_GET_IMPORT_PTR(method, style, method)
XSLT_GET_IMPORT_PTR(encoding, style, encoding)
XSLT_GET_IMPORT_INT(indent, style, indent);
if ((method == NULL) && (result->type == XML_HTML_DOCUMENT_NODE))
method = (const xmlChar *) "html";
if ((method != NULL) &&
(xmlStrEqual(method, (const xmlChar *) "html"))) {
if (encoding != NULL) {
htmlSetMetaEncoding(result, (const xmlChar *) encoding);
} else {
htmlSetMetaEncoding(result, (const xmlChar *) "UTF-8");
}
if (indent == -1)
indent = 1;
htmlDocContentDumpFormatOutput(buf, result, (const char *) encoding,
indent);
xmlOutputBufferFlush(buf);
} else if ((method != NULL) &&
(xmlStrEqual(method, (const xmlChar *) "xhtml"))) {
if (encoding != NULL) {
htmlSetMetaEncoding(result, (const xmlChar *) encoding);
} else {
htmlSetMetaEncoding(result, (const xmlChar *) "UTF-8");
}
htmlDocContentDumpOutput(buf, result, (const char *) encoding);
xmlOutputBufferFlush(buf);
} else if ((method != NULL) &&
(xmlStrEqual(method, (const xmlChar *) "text"))) {
xmlNodePtr cur;
cur = result->children;
while (cur != NULL) {
if (cur->type == XML_TEXT_NODE)
xmlOutputBufferWriteString(buf, (const char *) cur->content);
/*
* Skip to next node
*/
if (cur->children != NULL) {
if ((cur->children->type != XML_ENTITY_DECL) &&
(cur->children->type != XML_ENTITY_REF_NODE) &&
(cur->children->type != XML_ENTITY_NODE)) {
cur = cur->children;
continue;
}
}
if (cur->next != NULL) {
cur = cur->next;
continue;
}
do {
cur = cur->parent;
if (cur == NULL)
break;
if (cur == (xmlNodePtr) style->doc) {
cur = NULL;
break;
}
if (cur->next != NULL) {
cur = cur->next;
break;
}
} while (cur != NULL);
}
xmlOutputBufferFlush(buf);
} else {
int omitXmlDecl;
int standalone;
XSLT_GET_IMPORT_INT(omitXmlDecl, style, omitXmlDeclaration);
XSLT_GET_IMPORT_INT(standalone, style, standalone);
if (omitXmlDecl != 1) {
xmlOutputBufferWriteString(buf, "<?xml version=");
if (result->version != NULL) {
xmlOutputBufferWriteString(buf, "\"");
xmlOutputBufferWriteString(buf, (const char *)result->version);
xmlOutputBufferWriteString(buf, "\"");
} else
xmlOutputBufferWriteString(buf, "\"1.0\"");
if (encoding == NULL) {
if (result->encoding != NULL)
encoding = result->encoding;
else if (result->charset != XML_CHAR_ENCODING_UTF8)
encoding = (const xmlChar *)
xmlGetCharEncodingName((xmlCharEncoding)
result->charset);
}
if (encoding != NULL) {
xmlOutputBufferWriteString(buf, " encoding=");
xmlOutputBufferWriteString(buf, "\"");
xmlOutputBufferWriteString(buf, (const char *) encoding);
xmlOutputBufferWriteString(buf, "\"");
}
switch (standalone) {
case 0:
xmlOutputBufferWriteString(buf, " standalone=\"no\"");
break;
case 1:
xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
break;
default:
break;
}
xmlOutputBufferWriteString(buf, "?>\n");
}
if (result->children != NULL) {
xmlNodePtr children = result->children;
xmlNodePtr child = children;
/*
* Hack to avoid quadratic behavior when scanning
* result->children in xmlGetIntSubset called by
* xmlNodeDumpOutput.
*/
result->children = NULL;
while (child != NULL) {
xmlNodeDumpOutput(buf, result, child, 0, (indent == 1),
(const char *) encoding);
if (indent && ((child->type == XML_DTD_NODE) ||
((child->type == XML_COMMENT_NODE) &&
(child->next != NULL))))
xmlOutputBufferWriteString(buf, "\n");
child = child->next;
}
if (indent)
xmlOutputBufferWriteString(buf, "\n");
result->children = children;
}
xmlOutputBufferFlush(buf);
}
return(buf->written - base);
}
/**
* xsltSaveResultToFilename:
* @URL: a filename or URL
* @result: the result xmlDocPtr
* @style: the stylesheet
* @compression: the compression factor (0 - 9 included)
*
* Save the result @result obtained by applying the @style stylesheet
* to a file or @URL
*
* Returns the number of byte written or -1 in case of failure.
*/
int
xsltSaveResultToFilename(const char *URL, xmlDocPtr result,
xsltStylesheetPtr style, int compression) {
xmlOutputBufferPtr buf;
const xmlChar *encoding;
int ret;
if ((URL == NULL) || (result == NULL) || (style == NULL))
return(-1);
if (result->children == NULL)
return(0);
XSLT_GET_IMPORT_PTR(encoding, style, encoding)
if (encoding != NULL) {
xmlCharEncodingHandlerPtr encoder;
encoder = xmlFindCharEncodingHandler((char *)encoding);
if ((encoder != NULL) &&
(xmlStrEqual((const xmlChar *)encoder->name,
(const xmlChar *) "UTF-8")))
encoder = NULL;
buf = xmlOutputBufferCreateFilename(URL, encoder, compression);
} else {
buf = xmlOutputBufferCreateFilename(URL, NULL, compression);
}
if (buf == NULL)
return(-1);
xsltSaveResultTo(buf, result, style);
ret = xmlOutputBufferClose(buf);
return(ret);
}
/**
* xsltSaveResultToFile:
* @file: a FILE * I/O
* @result: the result xmlDocPtr
* @style: the stylesheet
*
* Save the result @result obtained by applying the @style stylesheet
* to an open FILE * I/O.
* This does not close the FILE @file
*
* Returns the number of bytes written or -1 in case of failure.
*/
int
xsltSaveResultToFile(FILE *file, xmlDocPtr result, xsltStylesheetPtr style) {
xmlOutputBufferPtr buf;
const xmlChar *encoding;
int ret;
if ((file == NULL) || (result == NULL) || (style == NULL))
return(-1);
if (result->children == NULL)
return(0);
XSLT_GET_IMPORT_PTR(encoding, style, encoding)
if (encoding != NULL) {
xmlCharEncodingHandlerPtr encoder;
encoder = xmlFindCharEncodingHandler((char *)encoding);
if ((encoder != NULL) &&
(xmlStrEqual((const xmlChar *)encoder->name,
(const xmlChar *) "UTF-8")))
encoder = NULL;
buf = xmlOutputBufferCreateFile(file, encoder);
} else {
buf = xmlOutputBufferCreateFile(file, NULL);
}
if (buf == NULL)
return(-1);
xsltSaveResultTo(buf, result, style);
ret = xmlOutputBufferClose(buf);
return(ret);
}
/**
* xsltSaveResultToFd:
* @fd: a file descriptor
* @result: the result xmlDocPtr
* @style: the stylesheet
*
* Save the result @result obtained by applying the @style stylesheet
* to an open file descriptor
* This does not close the descriptor.
*
* Returns the number of bytes written or -1 in case of failure.
*/
int
xsltSaveResultToFd(int fd, xmlDocPtr result, xsltStylesheetPtr style) {
xmlOutputBufferPtr buf;
const xmlChar *encoding;
int ret;
if ((fd < 0) || (result == NULL) || (style == NULL))
return(-1);
if (result->children == NULL)
return(0);
XSLT_GET_IMPORT_PTR(encoding, style, encoding)
if (encoding != NULL) {
xmlCharEncodingHandlerPtr encoder;
encoder = xmlFindCharEncodingHandler((char *)encoding);
if ((encoder != NULL) &&
(xmlStrEqual((const xmlChar *)encoder->name,
(const xmlChar *) "UTF-8")))
encoder = NULL;
buf = xmlOutputBufferCreateFd(fd, encoder);
} else {
buf = xmlOutputBufferCreateFd(fd, NULL);
}
if (buf == NULL)
return(-1);
xsltSaveResultTo(buf, result, style);
ret = xmlOutputBufferClose(buf);
return(ret);
}
/**
* xsltSaveResultToString:
* @doc_txt_ptr: Memory pointer for allocated XML text
* @doc_txt_len: Length of the generated XML text
* @result: the result xmlDocPtr
* @style: the stylesheet
*
* Save the result @result obtained by applying the @style stylesheet
* to a new allocated string.
*
* Returns 0 in case of success and -1 in case of error
*/
int
xsltSaveResultToString(xmlChar **doc_txt_ptr, int * doc_txt_len,
xmlDocPtr result, xsltStylesheetPtr style) {
xmlOutputBufferPtr buf;
const xmlChar *encoding;
*doc_txt_ptr = NULL;
*doc_txt_len = 0;
if (result->children == NULL)
return(0);
XSLT_GET_IMPORT_PTR(encoding, style, encoding)
if (encoding != NULL) {
xmlCharEncodingHandlerPtr encoder;
encoder = xmlFindCharEncodingHandler((char *)encoding);
if ((encoder != NULL) &&
(xmlStrEqual((const xmlChar *)encoder->name,
(const xmlChar *) "UTF-8")))
encoder = NULL;
buf = xmlAllocOutputBuffer(encoder);
} else {
buf = xmlAllocOutputBuffer(NULL);
}
if (buf == NULL)
return(-1);
xsltSaveResultTo(buf, result, style);
#ifdef LIBXML2_NEW_BUFFER
if (buf->conv != NULL) {
*doc_txt_len = xmlBufUse(buf->conv);
*doc_txt_ptr = xmlStrndup(xmlBufContent(buf->conv), *doc_txt_len);
} else {
*doc_txt_len = xmlBufUse(buf->buffer);
*doc_txt_ptr = xmlStrndup(xmlBufContent(buf->buffer), *doc_txt_len);
}
#else
if (buf->conv != NULL) {
*doc_txt_len = buf->conv->use;
*doc_txt_ptr = xmlStrndup(buf->conv->content, *doc_txt_len);
} else {
*doc_txt_len = buf->buffer->use;
*doc_txt_ptr = xmlStrndup(buf->buffer->content, *doc_txt_len);
}
#endif
(void)xmlOutputBufferClose(buf);
return 0;
}
/**
* xsltGetSourceNodeFlags:
* @node: Node from source document
*
* Returns the flags for a source node.
*/
int
xsltGetSourceNodeFlags(xmlNodePtr node) {
/*
* Squeeze the bit flags into the upper bits of
*
* - 'int properties' member in struct _xmlDoc
* - 'xmlAttributeType atype' member in struct _xmlAttr
* - 'unsigned short extra' member in struct _xmlNode
*/
switch (node->type) {
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
return ((xmlDocPtr) node)->properties >> 27;
case XML_ATTRIBUTE_NODE:
return ((xmlAttrPtr) node)->atype >> 27;
case XML_ELEMENT_NODE:
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
return node->extra >> 12;
default:
return 0;
}
}
/**
* xsltSetSourceNodeFlags:
* @node: Node from source document
* @flags: Flags
*
* Sets the specified flags to 1.
*
* Returns 0 on success, -1 on error.
*/
int
xsltSetSourceNodeFlags(xsltTransformContextPtr ctxt, xmlNodePtr node,
int flags) {
if (node->doc == ctxt->initialContextDoc)
ctxt->sourceDocDirty = 1;
switch (node->type) {
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
((xmlDocPtr) node)->properties |= flags << 27;
return 0;
case XML_ATTRIBUTE_NODE:
((xmlAttrPtr) node)->atype |= flags << 27;
return 0;
case XML_ELEMENT_NODE:
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
node->extra |= flags << 12;
return 0;
default:
return -1;
}
}
/**
* xsltClearSourceNodeFlags:
* @node: Node from source document
* @flags: Flags
*
* Sets the specified flags to 0.
*
* Returns 0 on success, -1 on error.
*/
int
xsltClearSourceNodeFlags(xmlNodePtr node, int flags) {
switch (node->type) {
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
((xmlDocPtr) node)->properties &= ~(flags << 27);
return 0;
case XML_ATTRIBUTE_NODE:
((xmlAttrPtr) node)->atype &= ~(flags << 27);
return 0;
case XML_ELEMENT_NODE:
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
node->extra &= ~(flags << 12);
return 0;
default:
return -1;
}
}
/**
* xsltGetPSVIPtr:
* @cur: Node
*
* Returns a pointer to the psvi member of a node or NULL on error.
*/
void **
xsltGetPSVIPtr(xmlNodePtr cur) {
switch (cur->type) {
case XML_DOCUMENT_NODE:
case XML_HTML_DOCUMENT_NODE:
return &((xmlDocPtr) cur)->psvi;
case XML_ATTRIBUTE_NODE:
return &((xmlAttrPtr) cur)->psvi;
case XML_ELEMENT_NODE:
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_PI_NODE:
case XML_COMMENT_NODE:
return &cur->psvi;
default:
return NULL;
}
}
#ifdef WITH_PROFILER
/************************************************************************
* *
* Generating profiling information *
* *
************************************************************************/
static long calibration = -1;
/**
* xsltCalibrateTimestamps:
*
* Used for to calibrate the xsltTimestamp() function
* Should work if launched at startup and we don't loose our quantum :-)
*
* Returns the number of milliseconds used by xsltTimestamp()
*/
#if !defined(XSLT_WIN32_PERFORMANCE_COUNTER) && \
(defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETTIMEOFDAY))
static long
xsltCalibrateTimestamps(void) {
register int i;
for (i = 0;i < 999;i++)
xsltTimestamp();
return(xsltTimestamp() / 1000);
}
#endif
/**
* xsltCalibrateAdjust:
* @delta: a negative dealy value found
*
* Used for to correct the calibration for xsltTimestamp()
*/
void
xsltCalibrateAdjust(long delta) {
calibration += delta;
}
/**
* xsltTimestamp:
*
* Used for gathering profiling data
*
* Returns the number of tenth of milliseconds since the beginning of the
* profiling
*/
long
xsltTimestamp(void)
{
#ifdef XSLT_WIN32_PERFORMANCE_COUNTER
BOOL ok;
LARGE_INTEGER performanceCount;
LARGE_INTEGER performanceFrequency;
LONGLONG quadCount;
double seconds;
static LONGLONG startupQuadCount = 0;
static LONGLONG startupQuadFreq = 0;
ok = QueryPerformanceCounter(&performanceCount);
if (!ok)
return 0;
quadCount = performanceCount.QuadPart;
if (calibration < 0) {
calibration = 0;
ok = QueryPerformanceFrequency(&performanceFrequency);
if (!ok)
return 0;
startupQuadFreq = performanceFrequency.QuadPart;
startupQuadCount = quadCount;
return (0);
}
if (startupQuadFreq == 0)
return 0;
seconds = (quadCount - startupQuadCount) / (double) startupQuadFreq;
return (long) (seconds * XSLT_TIMESTAMP_TICS_PER_SEC);
#else /* XSLT_WIN32_PERFORMANCE_COUNTER */
#ifdef HAVE_CLOCK_GETTIME
# if defined(CLOCK_MONOTONIC)
# define XSLT_CLOCK CLOCK_MONOTONIC
# elif defined(CLOCK_HIGHRES)
# define XSLT_CLOCK CLOCK_HIGHRES
# else
# define XSLT_CLOCK CLOCK_REALTIME
# endif
static struct timespec startup;
struct timespec cur;
long tics;
if (calibration < 0) {
clock_gettime(XSLT_CLOCK, &startup);
calibration = 0;
calibration = xsltCalibrateTimestamps();
clock_gettime(XSLT_CLOCK, &startup);
return (0);
}
clock_gettime(XSLT_CLOCK, &cur);
tics = (cur.tv_sec - startup.tv_sec) * XSLT_TIMESTAMP_TICS_PER_SEC;
tics += (cur.tv_nsec - startup.tv_nsec) /
(1000000000l / XSLT_TIMESTAMP_TICS_PER_SEC);
tics -= calibration;
return(tics);
#elif HAVE_GETTIMEOFDAY
static struct timeval startup;
struct timeval cur;
long tics;
if (calibration < 0) {
gettimeofday(&startup, NULL);
calibration = 0;
calibration = xsltCalibrateTimestamps();
gettimeofday(&startup, NULL);
return (0);
}
gettimeofday(&cur, NULL);
tics = (cur.tv_sec - startup.tv_sec) * XSLT_TIMESTAMP_TICS_PER_SEC;
tics += (cur.tv_usec - startup.tv_usec) /
(1000000l / XSLT_TIMESTAMP_TICS_PER_SEC);
tics -= calibration;
return(tics);
#else
/* Neither gettimeofday() nor Win32 performance counter available */
return (0);
#endif /* HAVE_GETTIMEOFDAY */
#endif /* XSLT_WIN32_PERFORMANCE_COUNTER */
}
static char *
pretty_templ_match(xsltTemplatePtr templ) {
static char dst[1001];
char *src = (char *)templ->match;
int i=0,j;
/* strip white spaces */
for (j=0; i<1000 && src[j]; i++,j++) {
for(;src[j]==' ';j++);
dst[i]=src[j];
}
if(i<998 && templ->mode) {
/* append [mode] */
dst[i++]='[';
src=(char *)templ->mode;
for (j=0; i<999 && src[j]; i++,j++) {
dst[i]=src[j];
}
dst[i++]=']';
}
dst[i]='\0';
return dst;
}
#define MAX_TEMPLATES 10000
/**
* xsltSaveProfiling:
* @ctxt: an XSLT context
* @output: a FILE * for saving the information
*
* Save the profiling information on @output
*/
void
xsltSaveProfiling(xsltTransformContextPtr ctxt, FILE *output) {
int nb, i,j,k,l;
int max;
int total;
unsigned long totalt;
xsltTemplatePtr *templates;
xsltStylesheetPtr style;
xsltTemplatePtr templ1,templ2;
int *childt;
if ((output == NULL) || (ctxt == NULL))
return;
if (ctxt->profile == 0)
return;
nb = 0;
max = MAX_TEMPLATES;
templates = xmlMalloc(max * sizeof(xsltTemplatePtr));
if (templates == NULL)
return;
style = ctxt->style;
while (style != NULL) {
templ1 = style->templates;
while (templ1 != NULL) {
if (nb >= max)
break;
if (templ1->nbCalls > 0)
templates[nb++] = templ1;
templ1 = templ1->next;
}
style = xsltNextImport(style);
}
for (i = 0;i < nb -1;i++) {
for (j = i + 1; j < nb; j++) {
if ((templates[i]->time <= templates[j]->time) ||
((templates[i]->time == templates[j]->time) &&
(templates[i]->nbCalls <= templates[j]->nbCalls))) {
templ1 = templates[j];
templates[j] = templates[i];
templates[i] = templ1;
}
}
}
/* print flat profile */
fprintf(output, "%6s%20s%20s%10s Calls Tot 100us Avg\n\n",
"number", "match", "name", "mode");
total = 0;
totalt = 0;
for (i = 0;i < nb;i++) {
templ1 = templates[i];
fprintf(output, "%5d ", i);
if (templ1->match != NULL) {
if (xmlStrlen(templ1->match) > 20)
fprintf(output, "%s\n%26s", templ1->match, "");
else
fprintf(output, "%20s", templ1->match);
} else {
fprintf(output, "%20s", "");
}
if (templ1->name != NULL) {
if (xmlStrlen(templ1->name) > 20)
fprintf(output, "%s\n%46s", templ1->name, "");
else
fprintf(output, "%20s", templ1->name);
} else {
fprintf(output, "%20s", "");
}
if (templ1->mode != NULL) {
if (xmlStrlen(templ1->mode) > 10)
fprintf(output, "%s\n%56s", templ1->mode, "");
else
fprintf(output, "%10s", templ1->mode);
} else {
fprintf(output, "%10s", "");
}
fprintf(output, " %6d", templ1->nbCalls);
fprintf(output, " %6ld %6ld\n", templ1->time,
templ1->time / templ1->nbCalls);
total += templ1->nbCalls;
totalt += templ1->time;
}
fprintf(output, "\n%30s%26s %6d %6ld\n", "Total", "", total, totalt);
/* print call graph */
childt = xmlMalloc((nb + 1) * sizeof(int));
if (childt == NULL)
return;
/* precalculate children times */
for (i = 0; i < nb; i++) {
templ1 = templates[i];
childt[i] = 0;
for (k = 0; k < nb; k++) {
templ2 = templates[k];
for (l = 0; l < templ2->templNr; l++) {
if (templ2->templCalledTab[l] == templ1) {
childt[i] +=templ2->time;
}
}
}
}
childt[i] = 0;
fprintf(output, "\nindex %% time self children called name\n");
for (i = 0; i < nb; i++) {
char ix_str[20], timep_str[20], times_str[20], timec_str[20], called_str[20];
unsigned long t;
templ1 = templates[i];
/* callers */
for (j = 0; j < templ1->templNr; j++) {
templ2 = templ1->templCalledTab[j];
for (k = 0; k < nb; k++) {
if (templates[k] == templ2)
break;
}
t=templ2?templ2->time:totalt;
snprintf(times_str,sizeof(times_str),"%8.3f",(float)t/XSLT_TIMESTAMP_TICS_PER_SEC);
snprintf(timec_str,sizeof(timec_str),"%8.3f",(float)childt[k]/XSLT_TIMESTAMP_TICS_PER_SEC);
snprintf(called_str,sizeof(called_str),"%6d/%d",
templ1->templCountTab[j], /* number of times caller calls 'this' */
templ1->nbCalls); /* total number of calls to 'this' */
fprintf(output, " %-8s %-8s %-12s %s [%d]\n",
times_str,timec_str,called_str,
(templ2?(templ2->name?(char *)templ2->name:pretty_templ_match(templ2)):"-"),k);
}
/* this */
snprintf(ix_str,sizeof(ix_str),"[%d]",i);
snprintf(timep_str,sizeof(timep_str),"%6.2f",(float)templ1->time*100.0/totalt);
snprintf(times_str,sizeof(times_str),"%8.3f",(float)templ1->time/XSLT_TIMESTAMP_TICS_PER_SEC);
snprintf(timec_str,sizeof(timec_str),"%8.3f",(float)childt[i]/XSLT_TIMESTAMP_TICS_PER_SEC);
fprintf(output, "%-5s %-6s %-8s %-8s %6d %s [%d]\n",
ix_str, timep_str,times_str,timec_str,
templ1->nbCalls,
templ1->name?(char *)templ1->name:pretty_templ_match(templ1),i);
/* callees
* - go over templates[0..nb] and their templCalledTab[]
* - print those where we in the the call-stack
*/
total = 0;
for (k = 0; k < nb; k++) {
templ2 = templates[k];
for (l = 0; l < templ2->templNr; l++) {
if (templ2->templCalledTab[l] == templ1) {
total+=templ2->templCountTab[l];
}
}
}
for (k = 0; k < nb; k++) {
templ2 = templates[k];
for (l = 0; l < templ2->templNr; l++) {
if (templ2->templCalledTab[l] == templ1) {
snprintf(times_str,sizeof(times_str),"%8.3f",(float)templ2->time/XSLT_TIMESTAMP_TICS_PER_SEC);
snprintf(timec_str,sizeof(timec_str),"%8.3f",(float)childt[k]/XSLT_TIMESTAMP_TICS_PER_SEC);
snprintf(called_str,sizeof(called_str),"%6d/%d",
templ2->templCountTab[l], /* number of times 'this' calls callee */
total); /* total number of calls from 'this' */
fprintf(output, " %-8s %-8s %-12s %s [%d]\n",
times_str,timec_str,called_str,
templ2->name?(char *)templ2->name:pretty_templ_match(templ2),k);
}
}
}
fprintf(output, "-----------------------------------------------\n");
}
fprintf(output, "\f\nIndex by function name\n");
for (i = 0; i < nb; i++) {
templ1 = templates[i];
fprintf(output, "[%d] %s (%s:%d)\n",
i, templ1->name?(char *)templ1->name:pretty_templ_match(templ1),
templ1->style->doc->URL,templ1->elem->line);
}
fprintf(output, "\f\n");
xmlFree(childt);
xmlFree(templates);
}
/************************************************************************
* *
* Fetching profiling information *
* *
************************************************************************/
/**
* xsltGetProfileInformation:
* @ctxt: a transformation context
*
* This function should be called after the transformation completed
* to extract template processing profiling information if available.
* The information is returned as an XML document tree like
* <?xml version="1.0"?>
* <profile>
* <template rank="1" match="*" name=""
* mode="" calls="6" time="48" average="8"/>
* <template rank="2" match="item2|item3" name=""
* mode="" calls="10" time="30" average="3"/>
* <template rank="3" match="item1" name=""
* mode="" calls="5" time="17" average="3"/>
* </profile>
* The caller will need to free up the returned tree with xmlFreeDoc()
*
* Returns the xmlDocPtr corresponding to the result or NULL if not available.
*/
xmlDocPtr
xsltGetProfileInformation(xsltTransformContextPtr ctxt)
{
xmlDocPtr ret = NULL;
xmlNodePtr root, child;
char buf[100];
xsltStylesheetPtr style;
xsltTemplatePtr *templates;
xsltTemplatePtr templ;
int nb = 0, max = 0, i, j;
if (!ctxt)
return NULL;
if (!ctxt->profile)
return NULL;
nb = 0;
max = 10000;
templates =
(xsltTemplatePtr *) xmlMalloc(max * sizeof(xsltTemplatePtr));
if (templates == NULL)
return NULL;
/*
* collect all the templates in an array
*/
style = ctxt->style;
while (style != NULL) {
templ = style->templates;
while (templ != NULL) {
if (nb >= max)
break;
if (templ->nbCalls > 0)
templates[nb++] = templ;
templ = templ->next;
}
style = (xsltStylesheetPtr) xsltNextImport(style);
}
/*
* Sort the array by time spent
*/
for (i = 0; i < nb - 1; i++) {
for (j = i + 1; j < nb; j++) {
if ((templates[i]->time <= templates[j]->time) ||
((templates[i]->time == templates[j]->time) &&
(templates[i]->nbCalls <= templates[j]->nbCalls))) {
templ = templates[j];
templates[j] = templates[i];
templates[i] = templ;
}
}
}
/*
* Generate a document corresponding to the results.
*/
ret = xmlNewDoc(BAD_CAST "1.0");
root = xmlNewDocNode(ret, NULL, BAD_CAST "profile", NULL);
xmlDocSetRootElement(ret, root);
for (i = 0; i < nb; i++) {
child = xmlNewChild(root, NULL, BAD_CAST "template", NULL);
snprintf(buf, sizeof(buf), "%d", i + 1);
xmlSetProp(child, BAD_CAST "rank", BAD_CAST buf);
xmlSetProp(child, BAD_CAST "match", BAD_CAST templates[i]->match);
xmlSetProp(child, BAD_CAST "name", BAD_CAST templates[i]->name);
xmlSetProp(child, BAD_CAST "mode", BAD_CAST templates[i]->mode);
snprintf(buf, sizeof(buf), "%d", templates[i]->nbCalls);
xmlSetProp(child, BAD_CAST "calls", BAD_CAST buf);
snprintf(buf, sizeof(buf), "%ld", templates[i]->time);
xmlSetProp(child, BAD_CAST "time", BAD_CAST buf);
snprintf(buf, sizeof(buf), "%ld", templates[i]->time / templates[i]->nbCalls);
xmlSetProp(child, BAD_CAST "average", BAD_CAST buf);
};
xmlFree(templates);
return ret;
}
#endif /* WITH_PROFILER */
/************************************************************************
* *
* Hooks for libxml2 XPath *
* *
************************************************************************/
/**
* xsltXPathCompileFlags:
* @style: the stylesheet
* @str: the XPath expression
* @flags: extra compilation flags to pass down to libxml2 XPath
*
* Compile an XPath expression
*
* Returns the xmlXPathCompExprPtr resulting from the compilation or NULL.
* the caller has to free the object.
*/
xmlXPathCompExprPtr
xsltXPathCompileFlags(xsltStylesheetPtr style, const xmlChar *str, int flags) {
xmlXPathContextPtr xpathCtxt;
xmlXPathCompExprPtr ret;
if (style != NULL) {
xpathCtxt = style->principal->xpathCtxt;
if (xpathCtxt == NULL)
return NULL;
xpathCtxt->dict = style->dict;
} else {
xpathCtxt = xmlXPathNewContext(NULL);
if (xpathCtxt == NULL)
return NULL;
}
xpathCtxt->flags = flags;
/*
* Compile the expression.
*/
ret = xmlXPathCtxtCompile(xpathCtxt, str);
if (style == NULL) {
xmlXPathFreeContext(xpathCtxt);
}
/*
* TODO: there is a lot of optimizations which should be possible
* like variable slot precomputations, function precomputations, etc.
*/
return(ret);
}
/**
* xsltXPathCompile:
* @style: the stylesheet
* @str: the XPath expression
*
* Compile an XPath expression
*
* Returns the xmlXPathCompExprPtr resulting from the compilation or NULL.
* the caller has to free the object.
*/
xmlXPathCompExprPtr
xsltXPathCompile(xsltStylesheetPtr style, const xmlChar *str) {
return(xsltXPathCompileFlags(style, str, 0));
}
/************************************************************************
* *
* Hooks for the debugger *
* *
************************************************************************/
int xslDebugStatus;
/**
* xsltGetDebuggerStatus:
*
* Get xslDebugStatus.
*
* Returns the value of xslDebugStatus.
*/
int
xsltGetDebuggerStatus(void)
{
return(xslDebugStatus);
}
#ifdef WITH_DEBUGGER
/*
* There is currently only 3 debugging callback defined
* Debugger callbacks are disabled by default
*/
#define XSLT_CALLBACK_NUMBER 3
typedef struct _xsltDebuggerCallbacks xsltDebuggerCallbacks;
typedef xsltDebuggerCallbacks *xsltDebuggerCallbacksPtr;
struct _xsltDebuggerCallbacks {
xsltHandleDebuggerCallback handler;
xsltAddCallCallback add;
xsltDropCallCallback drop;
};
static xsltDebuggerCallbacks xsltDebuggerCurrentCallbacks = {
NULL, /* handler */
NULL, /* add */
NULL /* drop */
};
/**
* xsltSetDebuggerStatus:
* @value : the value to be set
*
* This function sets the value of xslDebugStatus.
*/
void
xsltSetDebuggerStatus(int value)
{
xslDebugStatus = value;
}
/**
* xsltSetDebuggerCallbacks:
* @no : number of callbacks
* @block : the block of callbacks
*
* This function allow to plug a debugger into the XSLT library
* @block points to a block of memory containing the address of @no
* callback routines.
*
* Returns 0 in case of success and -1 in case of error
*/
int
xsltSetDebuggerCallbacks(int no, void *block)
{
xsltDebuggerCallbacksPtr callbacks;
if ((block == NULL) || (no != XSLT_CALLBACK_NUMBER))
return(-1);
callbacks = (xsltDebuggerCallbacksPtr) block;
xsltDebuggerCurrentCallbacks.handler = callbacks->handler;
xsltDebuggerCurrentCallbacks.add = callbacks->add;
xsltDebuggerCurrentCallbacks.drop = callbacks->drop;
return(0);
}
/**
* xslHandleDebugger:
* @cur : source node being executed
* @node : data node being processed
* @templ : temlate that applies to node
* @ctxt : the xslt transform context
*
* If either cur or node are a breakpoint, or xslDebugStatus in state
* where debugging must occcur at this time then transfer control
* to the xslDebugBreak function
*/
void
xslHandleDebugger(xmlNodePtr cur, xmlNodePtr node, xsltTemplatePtr templ,
xsltTransformContextPtr ctxt)
{
if (xsltDebuggerCurrentCallbacks.handler != NULL)
xsltDebuggerCurrentCallbacks.handler(cur, node, templ, ctxt);
}
/**
* xslAddCall:
* @templ : current template being applied
* @source : the source node being processed
*
* Add template "call" to call stack
* Returns : 1 on sucess 0 otherwise an error may be printed if
* WITH_XSLT_DEBUG_BREAKPOINTS is defined
*/
int
xslAddCall(xsltTemplatePtr templ, xmlNodePtr source)
{
if (xsltDebuggerCurrentCallbacks.add != NULL)
return(xsltDebuggerCurrentCallbacks.add(templ, source));
return(0);
}
/**
* xslDropCall:
*
* Drop the topmost item off the call stack
*/
void
xslDropCall(void)
{
if (xsltDebuggerCurrentCallbacks.drop != NULL)
xsltDebuggerCurrentCallbacks.drop();
}
#endif /* WITH_DEBUGGER */
|