This file is indexed.

/usr/lib/ruby/1.8/cgikit.rb is in libcgikit-ruby1.8 1.2.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
# = CGIKit - Web Application Framework
#
# == See also
# * www  - http://www.spice-of-life.net/download/cgikit/
# * mail - info@spice-of-life.net
#
# == License
# CGIKit is copyright (C) 2002- SPICE OF LIFE.
# It is a free software distributed under the Ruby license.
#
# == Author
# SUZUKI Tetsuya <suzuki@spice-of-life.net>


require 'stringio'
require 'webrick/httpservlet/abstract'

# Classes under CKAdapter are interface between CGIKit and web servers.
# 
# The role of adapter is to provide the consistent interface to 
# CKApplication. The difference of the relationship between 
# CGIKit and a web server is aboserbed by the obejct. 
module CKAdapter
	class CGI
		attr_accessor :headers, :params
		attr_reader :input, :output, :error

		CR  = "\r"
		LF  = "\n"
		EOL = CR + LF

		def initialize
			@headers = ENV
			@input   = $stdin
			@output  = $stdout
			@error   = $stderr
			_initialize_query
		end

		def create_request
			CKRequest.new( headers, params )
		end

		def create_response
			CKResponse.new headers
		end

		def run( request, response, &block )
			if request then params.update request end
			ckrequest  = create_request
			ckresponse = response || create_response

			if block_given?
				block.call( ckrequest, ckresponse )
			end

			print ckresponse.to_s
		end

		private

		# Copied from cgi.rb.
		# Other interfaces should override _parse* methods.
		def _initialize_query
			if ("POST" == @headers['REQUEST_METHOD']) and
				 %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n.match(@headers['CONTENT_TYPE'])
				boundary = $1.dup
				@params = _parse_multipart(boundary, Integer(@headers['CONTENT_LENGTH']))
			else
				case @headers['REQUEST_METHOD']
				when "HEAD" then query = _parse_head
				when "GET"  then query = _parse_get
				when "POST" then query = _parse_post
				else             query = _parse_else end
				@params = CKRequest.parse_query_string query
			end
		end

		def _parse_get
			@headers['QUERY_STRING'] or ""
		end

		alias :_parse_head :_parse_get

		def _parse_post
			@input.binmode
			@input.read(Integer(@headers['CONTENT_LENGTH'])) or ''
		end

		def _parse_else
			require "shellwords"

			string = unless ARGV.empty?
				ARGV.join(' ')
			else
				if STDIN.tty?
					STDERR.print(
						%|(offline mode: enter name=value pairs on standard input)\n|
					)
				end
				readlines.join(' ').gsub(/\n/n, '')
			end.gsub(/\\=/n, '%3D').gsub(/\\&/n, '%26')

			words = Shellwords.shellwords(string)

			if words.find{|x| /=/n.match(x) }
				words.join('&')
			else
				words.join('+')
			end
		end

		def _parse_multipart( boundary, content_length )
			params   = Hash.new([])
			boundary = "--" + boundary
			buf      = ""
			bufsize  = 10 * 1024

			# start multipart/form-data
			@input.binmode
			boundary_size   = boundary.size + EOL.size
			content_length -= boundary_size
			status          = @input.read boundary_size
			if status == nil then
				raise EOFError, "no content body"
			elsif boundary + EOL != status
				raise EOFError, "bad content body"
			end

			until content_length == -1
				head = nil
				data = CKByteData.new

				until head and /#{boundary}(?:#{EOL}|--)/n.match(buf)
					if (not head) and /#{EOL}#{EOL}/n.match(buf)
						buf = buf.sub(/\A((?:.|\n)*?#{EOL})#{EOL}/n) do
						head = $1.dup
						""
					end
					next
				end

				if head and ( (EOL + boundary + EOL).size < buf.size )
					data << buf[0 ... (buf.size - (EOL + boundary + EOL).size)]
					buf[0 ... (buf.size - (EOL + boundary + EOL).size)] = ""
				end

				if bufsize < content_length then
					c = @input.read(bufsize) or ''
				else
					c = @input.read(content_length) or ''
				end
				buf += c
				content_length -= c.size
			end

			buf = buf.sub(/\A((?:.|\n)*?)(?:#{EOL})?#{boundary}(#{EOL}|--)/n) do
				data << $1
				if "--" == $2
					content_length = -1
					end
					""
				end

				/Content-Disposition:.* filename="?([^\";]*)"?/ni.match(head)
				filename = ($1 || "").dup
				if /Mac/ni.match(@headers['HTTP_USER_AGENT']) and
					/Mozilla/ni.match(@headers['HTTP_USER_AGENT']) and
					(not /MSIE/ni.match(@headers['HTTP_USER_AGENT']))
					filename = CKUtilities.unescape_url filename
				end
				data.path = filename

				/Content-Type: ([^\r\n]*)/ni.match(head)
				if $1 then
					data.content_type = $1.dup
				else
					data = data.to_s
				end

				/Content-Disposition:.* name="?([^\";]*)"?/ni.match(head)
				name = $1.dup

				if params.has_key? name then
					params[name].push data
				else
					params[name] = [data]
				end
			end

			params
		end
	end


	class ModRuby < CGI
		def run( request, response, &block )
			if request then params.update request end
			ckrequest  = create_request
			ckresponse = response || create_response

			if block_given?
				block.call( ckrequest, ckresponse )
			end

			print ckresponse.status_line
			print ckresponse.to_s
		end
	end


	class Template < CGI
		def initialize
			@headers = ENV
			@input   = StringIO.new
			@output  = StringIO.new
			@error   = StringIO.new
		end

		def run( request, response, &block )
			ckrequest  = request || create_request
			ckresponse = response || create_response

			if block_given?
				block.call( ckrequest, ckresponse )
			end

			ckresponse
		end
	end


	# The adapter and WEBrick::CGIKitServlet are for WEBrick
	# writtend by Ken-ichi HASHIMOTO and Shinya Kawaji. Thanks!
	class WEBrick
		GENERAL_HEADER = %w[
			Cache-Control
			Connection
			Date
			Pragma
			Trailer
			Transfet-Encoding
			Upgrade
			Via
			Warning
		]

		RESPONSE_HEADER = %w[
			Accept-Ranges
			Age
			ETag
			Location
			Proxy-Authenticate
			Retry-After
			Server
			Vary
			WWW-Authenticate
		]

		ENTITY_HEADER = %w[
			Allow
			Content-Encoding
			Content-Language
			Content-Length
			Content-Location
			Content-MD5
			Content-Range
			Content-Disposition
			Expires
			Last-Modified
		]

		LEAVE_HEADER = {}
		(GENERAL_HEADER + RESPONSE_HEADER + ENTITY_HEADER).each{|key|
			LEAVE_HEADER[key] = true
		}

		def run(request, response)
			# set request
			headers = request.meta_vars
			form_values = Hash.new([])
			request.query.each{|key, val|
				form_values[key] = val.list
			}

			ckrequest  = CKRequest.new(headers, form_values)
			ckresponse = CKResponse.new(headers)

			# run
			if block_given?
				yield(ckrequest, ckresponse)
			end

			# set response
			response.request_http_version = ckresponse.http_version
			response.status = ckresponse.status
			ckresponse.headers.each{|key, val|
				if LEAVE_HEADER[key]
					response[key] = val
				end
			}
			response['Content-Type'] = ckresponse.headers['Content-Type']
			if ckresponse.encoding
				response['Content-Type'] << "; charset=#{ckresponse.encoding}"
			end
			ckresponse.cookies.each{|cookie|
				response.cookies << cookie
			}
			response.body = ckresponse.content
		end
	end

end


module WEBrick
	module CGIKitServlet
		class ApplicationHandler < HTTPServlet::AbstractServlet
			def initialize(server, app)
				@app = app
				@app.interface = CKAdapter::WEBrick
			end

			def do_GET(req, res)
				@app.run(req, res)
			end
			alias_method :do_POST, :do_GET
		end

		class PathHandler < ApplicationHandler
			def initialize(server, component_path)
				app = CKApplication.new
				app.component_path = component_path
				super(server, app)
			end
		end

		class HashHandler < ApplicationHandler
			def initialize(server, hash)
				app = CKApplication.new
				hash.each{|key, val|
					app.__send__("#{key}=", val)
				}
				super(server, app)
			end
		end
	end
end


# The super class of CGIKit error classes.
class CKError < StandardError; end


# A class for session management. CKSession objects have a hash of
# arbitary objects and information about browser name, IP address, etc.
# However, you can't set objects that can't be marshal ( IO, Proc, etc. )
# to the session with default database manager CKSessionStore::FileStore.
#
# == Programming Topics
# * SessionManagement[www.spice-of-life.net/download/cgikit/en/userguide/session.html]
class CKSession
	class << self
		def session_id?( session_id )
			if ( session_id == nil ) or ( session_id == "" ) then
				false
			elsif session_id =~ /([0-9A-Za-z]*)/ then
				true
			else
				false
			end
		end

		def create_session_id
			require 'digest/md5'
			md5 = Digest::MD5::new
			md5.update Time.now.to_s
			md5.update rand(0).to_s
			md5.update $$.to_s
			md5.hexdigest[0,16]
		end
	end

	# A hash of arbitary objects.
	attr_accessor :session

	# Session ID.
	attr_accessor :session_id

	# Creation time of the session.
	attr_accessor :creation_time

	# Last accessed time for the session.
	attr_accessor :last_accessed_time

	# Name of browser.
	attr_accessor :user_agent

	# IP address.
	attr_accessor :remote_addr


	def initialize( session_id = nil )
		unless CKSession.session_id? session_id then
			session_id     = CKSession.create_session_id
		end

		@session            = {}
		@session_id         = session_id
		@clear              = false
		@non_access_time    = 0
		@creation_time      = Time.new
		@last_accessed_time = Time.new
	end

	public

	def fetch( key )
		restore unless @session
		@session[key]
	end

	def store( key, value )
		restore unless @session
		@session[key] = value
	end

	alias []  fetch
	alias []= store

	def remove( key )
		@session.delete key
	end

	def clear
		@session.clear
		@clear = true
	end

	# Returns true if "clear" method of the session is called.
	def clear?
		@clear
	end

	# Returns true if the browser is equal to one when the session created.
	def user_agent?( user_agent )
		if @user_agent == user_agent then
			true
		else
			false
		end
	end

	# Returns true if the IP address is equal to one when the session created.
	def remote_addr?( remote_addr )
		if @remote_addr == remote_addr then
			true
		else
			false
		end
	end

	# Returns true if the session isn't expired.
	def timeout?( timeout )
		non_access_time = @last_accessed_time - @creation_time

		if timeout == 0 then
			false
		elsif timeout <= non_access_time then
			true
		else
			false
		end
	end
end


# CKSessionStore is a class for saving session with database manager.
# However this class is for internal use. Use methods of CKApplication to
# manage sessions.
#
# == Customizing database manager
# Database manager objects, such as CKSessionStore::FileStore, save sessions.
# The objects has these 3 methods, implement the methods if you develop or
# customize database manager class.
#
# <b>save</b>::    Saves the session.
# <b>clear</b>::   Clear the session.
# <b>restore</b>:: Returns session restored from the saved. 
#
class CKSessionStore
	attr_accessor :database_manager

	def initialize( application )
		@application      = application
		@database_manager = @application.database_manager.new @application
	end

	def save( session )
		if session.clear? then
			@database_manager.clear session.session_id
		else
			@database_manager.save session
		end
	end

	# Restore a session.
	# Returns a restored session or nil if the session ID is not existed.
	def restore( session_id )
		@database_manager.restore session_id
	end

	def clear( session_id )
		@database_manager.clear session_id
	end


	# A class that saves sessions to files by marshaling.
	class FileStore
		TMPDIR = 'session'

		def initialize( application )
			@application = application
			@tmpdir      = File.join(@application.tmpdir, TMPDIR).untaint
		end

		def save( session )
			unless FileTest.directory? @tmpdir
				require 'ftools'
				File.makedirs @tmpdir
			end

			CKFileLock.exclusive_lock( _tmpfile(session.session_id) ) do | file |
				Marshal.dump( session, file )
			end
		end

		# Restore a session.
		#
		# <b>Returns</b>
		#
		# A restored session or nil if the session ID is not existed.
		def restore( session_id )
			session = nil
			if _exist? session_id then
				CKFileLock.shared_lock( _tmpfile(session_id) ) do | file |
					session = Marshal.load file
				end
				session.last_accessed_time = Time.new
			end
			session
		end

		private
		def _tmpfile( session_id )
			File.join( @tmpdir, session_id ).untaint
		end

		def _exist?( session_id )
			if FileTest.exist? _tmpfile( session_id )
				true
			else
				false
			end
		end

		public
		def clear( session_id )
			if FileTest.exist? _tmpfile( session_id )
				File.delete _tmpfile( session_id )
			end
		end
	end
end


# This class is one of the most important classes in CGIKit.
# CKApplication has two major roles. 
# 
# One role is that CKAplication provides parameters which 
# effect the whole behavior of CGIKit application.
# The other role is that CKApplication communicates with CKAdapter, 
# an interface between a web server and CGIKit.
# 
# When CGIKit receives a request from a client,
# CKApplication arranges its parameters and creates a CKAdapter object.
# Next, CKApplication gets a request object from the CKAdapter object. 
# In reponse to the request object, CKApplication creates 
# a response object and sends it to the CKAdapter object.  
#
# == Application Process
#
# === 1. Ready a request and response object
# First, CKApplication creates a CKAdapter object and gets a request
# ( CKRequest ) object from it. Then, CKApplication creates a response 
# ( CKResponse ) object. 
#
# === 2. Event loop
# Next, CKApplication goes into event loop. In this loop, CKApplication creates 
# components depending on the request and invokes the specified method of 
# the components until it returns an object whose class is not CKComponent.
#
# === 3. Return the response object to the CKAdapter object
# Finally, CKApplication converts the components created in event loop to HTML
# and adds the HTML to the CKResponse object created in 1.
# CKApplication sends the response object to the CKAdapter object.
# Then, the CKAdapter object displays the response object to a client.
# 
# == Programming Topics
# * Architecture[www.spice-of-life.net/download/cgikit/en/userguide/architecture.html]
# * SessionManagement[www.spice-of-life.net/download/cgikit/en/userguide/session.html]
class CKApplication
	class SessionAuthorizationError < CKError; end
	class SessionTimeoutError       < CKError; end

	# Main component. If the element ID  isn't specified, this component is shown.
	# The default value is 'MainPage'.
	attr_accessor :main

	# Locale of an application in a transaction. If the locale is specified,
	# CGIKit change a template for a component. The name of the template
	# includes the component name and the locale name. Also, the template name
	# is seprated by underscore("_"). For example, if the locale is "ja" and
	# the component name is "MainPage", the template name is "MainPage_ja.html".
	attr_accessor :locale

	# Main locale of an application in a transaction. If the master_locale
	# is equal to the specified locale, the components use templates whose name
	# doesn't include the locale name. For instance, if the master_locale is
	# "ja", the locale is "ja" and the component name is "MainPage",
	# the template name is "MainPage.html".
	attr_accessor :master_locale

	# Document root directory.
	attr_accessor :document_root

	# The file system path of the application.
	attr_accessor :path

	# The file system path for components. Components are searched under it.
	attr_accessor :component_path

	# Resource directory. It has files that don't be send to browser.
	attr_accessor :resources

	# Web server resources directory.
	# If "file" attribute of CKImage is set,
	# the element searches an image file from the directory.
	attr_accessor :web_server_resources

	# CKResourceManager object.
	attr_accessor :resource_manager

	# Adapter object.
	attr_accessor :adapter

	# Interface of adapter. The default value is an interface for CGI.
	attr_accessor :interface

	# HTTP request object ( CKRequest ).
	attr_accessor :request

	# HTTP response object ( CKResponse ).
	attr_accessor :response

	# Hash of logging options.
	#
	# level::         Log level. Select in CKLog::DEBUG, CKLog::INFO, CKLog::WARN,
	#                 CKLog::ERROR, CKLog::FATAL.
	# name::          Program name.
	# out::           Outputter.
	# file::          File name to output logs. The attribute has priority over "out".
	# max_file_size:: Max file size to log. Enables if "file" is setted.
	attr_accessor :log_options

	# Name of an error page component to show caught errors.
	attr_accessor :error_page

	# Temporary directory for CGIKit.
	attr_accessor :tmpdir

	# Session object. If session don't exist, creates a new session.
	attr_writer :session

	# Session ID.
	attr_accessor :session_id

	# Session key.
	attr_accessor :session_key

	# Seconds until the session has timed out.
	attr_accessor :timeout

	# Expiry date of cookie for session. If you set the value to nil,
	# session cookies will be invalid when closing browser.
	attr_accessor :session_cookie_expires

	# Enables or disables the use of URLs for storing session IDs.
	attr_accessor :store_in_url

	# Enables or disables the use of cookies for storing session IDs.
	attr_accessor :store_in_cookie

	# Enables or disables automatic session management.
	attr_accessor :manage_session

	# Enables or disables session authorization by browsers.
	attr_accessor :auth_by_user_agent

	# Enables or disables session authorization by IP addresses.
	attr_accessor :auth_by_remote_addr

	# Database manager class saving the session.
	attr_accessor :database_manager

	# Element ID ( CKElementID object ).
	attr_accessor :element_id

	# Session database object ( CKSessionStore ).
	attr_accessor :session_store

	# Character code to convert form data used in CKComponent#canvert_char_code.
	# Select these Japanese codes: 'jis', 'sjis' or 'euc'. If the value is nil,
	# raw form data is substituted for variables. The default value is nil.
	#
	# The attribute and method is setted for Japanese character codes by default.
	# If you convert form data to non-Japanese character codes, override the method.
	attr_accessor :char_code

	# Handled exception.
	attr_reader :error

	VERSION  = '1.2.1'

	class << self
		# Returns version of CGIKit.
		def version
			VERSION
		end
	end

	def initialize
		@path                   = $0
		@component_path         = Dir.pwd
		@main                   = 'MainPage'
		@error_page             = 'CKErrorPage'
		@tmpdir                 = './tmp' || ENV['TMP'] || ENV['TEMP']
		@session_key            = '_session_id'
		@manage_session         = false
		@timeout                = 60 * 60 * 24 * 7
		@session_cookie_expires = 60 * 60 * 24 * 7
		@store_in_url           = true
		@store_in_cookie        = true
		@auth_by_user_agent     = false
		@auth_by_remote_addr    = false
		@database_manager       = CKSessionStore::FileStore
		@log_options            = {}
		@char_code              = nil
		@resources              = './'

		# decides interface of adapter
		if defined?(MOD_RUBY) then
			@interface = CKAdapter::ModRuby
		else
			@interface = CKAdapter::CGI
		end

		CKElement.load_element_file(@component_path, 'CKErrorPage')

		init
	end

	# Hook method to initialize for convenience.
	def init; end

	# Returns the name of the application without file extension.
	def name
		File.basename( @path, '.*' )
	end

	# The application URL based on SCRIPT_NAME with session ID.
	def baseurl( session = false )
		if (session == true) and session? then
			@baseurl + "?#@session_key=#@session_id"
		else
			@baseurl
		end
	end

	# Creates a session.
	def create_session
		session             = CKSession.new
		session.user_agent  = @request.user_agent
		session.remote_addr = @request.remote_addr
		session
	end

	# Returns a restored session objects with the session ID.
	def restore_session( session_id )
		session_store.restore session_id
	end

	# Deletes the session.
	def clear_session( session )
		session.clear
		save_session session
		@session_id = nil
		@session = nil
	end

	# Saves the session, and set a cookie if "store_in_cookie" attribute is
	# setted. If "clear" method of the session is called, the session is deleted.
	def save_session( session )
		if session.clear? then
			if session.session_id == @session_id then
				_clear_session_cookie
			end
		elsif (@session_id == session.session_id) and @store_in_cookie then
			cookie = CKCookie.new(@session_key, @session_id)
			if @session_cookie_expires then
				cookie.expires = Time.new + @session_cookie_expires
			end
			@response.add_cookie cookie
		end

		session_store.save session
	end

	private

	def _clear_session_cookie
		@session_id = nil

		if @store_in_cookie then
			cookie         = CKCookie.new @session_key
			cookie.expires = Time.new - 60
			@response.add_cookie cookie
		end
	end

	public

	# Returns the session object. If the session isn't existed, returns a new session.
	def session
		@session ||= create_session
	end

	# Returns true if the session is existed.
	def session?
		if @session and @session_key and @session_id then
			true
		else
			false
		end
	end

	# Returns true if the locale is master locale.
	def master_locale?
		if @locale == @master_locale then
			true
		else
			false
		end
	end

	# Runs the application.
	# This method calls hook methods "pre_run" and "pre_respond".
	def run( request = nil, response = nil )
		@adapter = create_adapter
		@adapter.run( request, response ) do | ckrequest, ckresponse |
			@request  = ckrequest
			@response = ckresponse

			begin
				# trap generic components
				_ready_request_response
				_ready_session
				pre_action # hook

				unless ckresponse.redirect?
					_event_loop ckresponse
				end
			rescue Exception => e
				@error = e

				# trap custom components' error
				begin
					component = handle_error e
					ckresponse.content = component.to_s
				rescue Exception => e
					component = _default_error_page e
					ckresponse.content = component.to_s
				end
			end

			pre_respond # hook
		end
	end

	# Hook method called before generating specified component.
	# When calling the method, objects of request and session are already setted.
	def pre_action; end

	# Hook method called just before sending the response to browser.
	# When calling the method, a web page converted to HTML code
	# has already setted to the response.
	def pre_respond; end

	private

	def _ready_request_response
		@baseurl       = @request.script_name              unless @baseurl
		@document_root = @request.headers['DOCUMENT_ROOT'] unless @document_root
		@web_server_resources ||= @document_root
		@resource_manager       = CKResourceManager.new self
		unless @request.languages.empty? then
			@locale = @request.languages.first
		end

		if @element_id then
			@target = @element_id.component
		elsif @request['element_id'] then
			@element_id = CKElementID.new @request['element_id'].to_s
			@target     = @element_id.component
		else
			@target = @main.dup
		end
	end

	def _ready_session
		@session_store = CKSessionStore.new self

		# get the session ID.
		cookie    = @request.cookie(@session_key)
		id_cookie = cookie.value if cookie
		id_query  = @request[@session_key]
		unless CKSession.session_id? id_cookie then id_cookie   = nil       end
		unless CKSession.session_id? id_query  then id_query    = nil       end
		if     @store_in_cookie and id_cookie then @session_id = id_cookie
		elsif                       id_query  then @session_id = id_query  end

		# restore the session.
		if @session_id then
			@session = restore_session @session_id
		else
			@session_id = nil
		end

		# ready a new session in automatic session management
		if @manage_session and @session_id.nil? and @session.nil? then
			@session    = create_session
			@session_id = @session.session_id
			save_session @session

			if @store_in_url and (@store_in_cookie == false) then
				@response.set_redirect url(@element_id, @request.form_values)
			end
		end

		# check 1 - session ID and session
		if @session_id and @session.nil? then
			_session_timeout
		end

		# check 2 - authorization and timeout
		if @session then
			if (@auth_by_user_agent and \
			   (not (@session.user_agent? @request.user_agent))) or \
			   (@auth_by_remote_addr and \
			   (not (@session.remote_addr? @request.remote_addr))) then
				raise SessionAuthorizationError, 'Your session is not authorized.'
			elsif (@session.timeout? @timeout) then
				_session_timeout
			end
		end
	end

	def _session_timeout
		if @session then
			clear_session @session
		elsif @session_id then
			_clear_session_cookie
		end

		raise SessionTimeoutError, 'Your session has timed out.'
	end

	def _event_loop( response )
		result = page @target
		result.is_top = true

		begin
			component = result
			result    = component.run

			if @manage_session and @session then
				save_session @session
			end
		end while result.is_a? CKComponent

		# Recreates a main component if the component includes CKPartsMaker.
		if component.is_a? CKPartsMaker
			@target   = component.substitute_page || @main
			component = page @target
			component.run
		end

		# for displaying images
		element = nil
		if @element_id then
			if defs = component.definitions[@element_id.element] then
				element = defs['element']
			end
		end

		if (CKByteData === result) and (element == 'CKImage') then
			raw_mime = component.definitions[@element_id.element]['mime']
			unless raw_mime then
				mime = result.content_type
			else
				mime = component.parse_ckd_value raw_mime
			end
			response.headers['Content-Type'] = mime
			response.content = result.to_s
		else
			response.content = component.to_s
		end
	end

	public

	# Creates an adapter object.
	def create_adapter
		@interface.new
	end

	# Handles every errors and return an error page component.
	def handle_error( error )
		error_page = nil
		if ( error.class == SessionTimeoutError )       or \
		   ( error.class == SessionAuthorizationError ) then
			error_page = handle_session_error error
		end
		unless error_page.is_a? CKComponent then
			error_page       = page @error_page
			error_page.error = error
		end
		error_page
	end

	# Hook method to handle session errors.
	def handle_session_error( error ); end

	private

	# Return a default error page component.
	def _default_error_page( error )
		error_page       = page 'CKErrorPage'
		error_page.error = error
		error_page
	end

	public

	# Creates a specified page component.
	def page( name )
		CKElement.instance( name, self, nil, nil, nil )
	end

	# Creates a URL.
	def url( id = nil, query = nil, secure = false, direct = false )
		protocol = nil
		string   = '?'

		if @secure then
			protocol = 'https://'
		end

		string << "element_id=#{id};"  if id
		string << _query_string(query) if query
		string << _query_for_session   if direct != true
		string.chop!                   if string =~ /&$/

		url = ''
		if protocol then
			url << protocol
			url << @request.server_name if @request.server_name
		end
		url << self.baseurl if self.baseurl
		url << string
	end

	private

	def _query_string( hash )
		str = ''
		hash.each do | key, value |
			if key == @session_key then
				next
			elsif value.is_a? Array then
				value.each do | _value |
					str << "#{key}=#{CKUtilities.escape_url(_value)};"
				end
			else
				str << "#{key}=#{CKUtilities.escape_url(value)};"
			end
		end
		str
	end

	def _query_for_session
		if @store_in_url and @session_id and @session_key then
			"#@session_key=#@session_id;"
		else
			''
		end		
	end

end


# CKElementID is a class for element IDs.
# The object is used to specify an element when substituting form values and
# running an action.
#
# Element ID has 3 elements, these are "component, repetitions, element".
# "repetitions" are definition names enclosed the element with CKRepetition
# and indexes in the CKRepetition elements.
#
# The each elements are splitted by '.'. Definition names and indexes in
# repetitions are splitted by ':'.
# 
# * MainPage component
#  MainPage
#
# * String element of MainPage component
#  MainPage.String
#
# * String element in third repeat for Repetition element as 
#   CKRepetition of MainPage component 
#  MainPage.Repetition:3.String
#
# Component is required. If you specify elements in CKRepetition, 
# repetitions are required.
class CKElementID
	# Name of the component.
	attr_reader :component

	# Array of the repetitions.
	attr_accessor :repetitions

	# Name of the element.
	attr_accessor :element

	def initialize( string = nil )
		@repetitions = []
		if string then
			tokens     = string.split '.'
			@component = tokens.shift
			@element   = tokens.pop

			# parses repetitions
			token = repetition = nil
			unless tokens.empty?
				tokens.each do | token |
					repetition = token.split ':'
					@repetitions << [ repetition.first, repetition.last.to_i ]
				end
			end
		end
	end

	# Returns true if the ID is in repetitions.
	def repetitions?
		if @repetitions.empty? then
			false
		else
			true
		end
	end

	# Executes the block for every definition names and repetition indexes.
	def each
		definition = index = nil
		@repetitions.each do | name, index |
			yield name, index
		end
	end

	# Returns true if a component of the ID and specified are equal.
	def component?( name )
		if @component == name then
			true
		else
			false
		end
	end

	def component=( name )
		@component = name.split('::').last
	end

	# Returns the object as a string.
	def to_s
		str = @component + '.'
		if repetitions? then
			@repetitions.each do | name, index |
				str << "#{name}:#{index}."
			end
		end
		str << @element if @element
		str.chop! if str =~ /\.$/
		str
	end
end


# CKKeyValueCoding provides methods to access instance variables of the object.
# The methods try accessing instance variables by using accessor method.
# If it is failure, its try accessing directly.
#
# If a class method "access_instance_variables?" defines in the class and
# the method returns "true", the direct access is success. Or failure.
module CKKeyValueCoding
	class UnknownKeyError < CKError; end

	# Retrieves value of the instance variable with method chain.
	def retrieve_value( key )
		keypath = key.split '.'
		object  = self
		keypath.each do |path|
			if object.respond_to? path then
				object = object.__send__ path
			elsif _directly?(object) == true then
				object = object.instance_eval "@#{path}"
			else
				_raise_error(object, path)
			end
		end
		object
	end

	# Sets value for the instance variable with method chain.
	def take_value( key, value )
		keypath = key.split '.'
		object  = self
		keypath.each_with_index do |path, index|
			writer = "#{path}="

			if (index + 1) == keypath.size then
				# takes value for key
				if object.respond_to? writer then
					object.__send__(writer, value)
				elsif _directly?(object) == true then
					object.instance_eval "@#{path}=value"
				else
					_raise_error(object, writer)
				end
			else
				# get and set value for the object
				if object.respond_to? path then
					object = object.__send__ path
				elsif _directly?(object) == true then
					object = object.instance_eval "@#{path}"
				else
					_raise_error(object, path)
				end
			end
		end
	end

	alias []  retrieve_value
	alias []= take_value

	private

	def _directly?( object )
		if object.class.respond_to? 'access_instance_variables?' then
			object.class.access_instance_variables?
		else
			false
		end
	end

	def _raise_error( object, key )
		msg =  "This \"#{object.class}\" object does not have a method "
		msg << "\"#{key}\", nor an instance variable \"@#{key.sub(/=$/,'')}\"."
		raise UnknownKeyError, msg
	end

end


# The super class of dynamic element classes.
#
# Dynamic elements convert themselves to HTML.
# These are very important in development with CGIKit.
#
# CKElement returns HTML when to_s() is called.
# In components development, you bind elements to component's methods. 
# At runtime, the binding is executed by run(). 
# These two methods are expected to be overrided in the subclasses of CKElement.
#
# == Paths for searching elements and components
# CKElement objects, including components, are usually instantiated
# by CKElement.instance. This method loads elements/components and creates
# element objects. In this method, some paths are searched to require files.
#
# The searched paths:
# 1. CKApplicaton#component_path
# 2. ($LOAD_PATH)/cgikit/elements
# 3. ($LOAD_PATH)/cgikit/components
#
# The latter two paths are for extention elements or components.
# It is recommended that your own elements or components are installed
# in the 1st path.
#
# == Creation of custom elements
# If you create custom elements, the class of the custom elements must 
# inherit CKElement. Simple dynamic element can be created only 
# by overriding to_s(). to_s() is called when CGIKit
# converts the dynamic elements to HTML. If you add new attributes
# to your custom elements, run() must be overrided. 
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/elements.html]
class CKElement
	class UnknownElementError < CKError; end #:nodoc:
	class AttributeError      < CKError; end #:nodoc:

	attr_accessor :name
	attr_accessor :body
	attr_writer   :definition
	attr_accessor :parent
	attr_accessor :application

	# Array of repetitions.
	attr_accessor :repetitions

	# Index in the last repetition.
	attr_accessor :repetition_index

	class << self
		def instance( element, app, parent, name, body )
			body = '' unless body
			path = load_element_file(app.component_path, element)
			klass = app.class.const_get element
			klass.new(app, parent, name, body, path)
		end

		# Internal method.
		def load_element_file( component_path, element )
			component = "#{component_path}/#{element}/#{element}"

			# check paths for extension elements and components
			ext_element   = "cgikit/elements/#{element}"
			ext_component = "cgikit/components/#{element}/#{element}"
			paths         = [ ext_element, ext_component ]
			$LOAD_PATH.each do | load_path |
				paths.each do | path |
					ext_path = "#{load_path}/#{path}"
					if FileTest.exist? "#{ext_path}.rb".untaint
						require path.untaint
						return ext_path
					else
						next
					end
				end
			end

			# components
			if FileTest.exist? "#{component}.rb".untaint then
				require component.untaint
				return nil
			# starndard elements
			elsif Object.const_defined? element then
				return nil
			end

			raise UnknownElementError, "No such '#{element}' element or component."
		end

		# Internal method that will be overridden in subclasses.
		def bind_request( component, definition, value ); end

		# Return an array of names of the element's own attributes.
		# The value is used to generate optional HTML attributes.
		# Subclasses must override this method.
		def own_attributes
			[]
		end
	end

	# Returns the element ID.
	def element_id
		unless @element_id then
			@element_id             = CKElementID.new
			@element_id.component   = parent.class.to_s
			@element_id.element     = name

			unless repetitions.empty? then
				last_repetition = repetitions.pop
				last_repetition = [ last_repetition.first,repetition_index ]
				repetitions.push last_repetition
			end
			@element_id.repetitions = repetitions || []
		end
		@element_id
	end

	def initialize( app, parent, name, body, path = nil )
		@application      = app
		@parent           = parent
		@name             = name
		@body             = body
		@repetitions      = []
		@repetition_index = 0
	end

	# Fetches the result of the parent component's methods.
	# When is_binding is true, fetch() returns String without binding from . 
	def fetch( key, is_binding = true )
		data = definition[ key ]
		if is_binding == false
			data
		else
			parent.parse_ckd_value( data )
		end
	end

	# Return a definition hash of the element.
	def definition
		unless @definition then
			@definition = parent.definitions[name]
		end
		@definition
	end

	# Return a definition name of the element with the class name.
	def name_with_class
		"\"#{name}\" at #{self.class}"
	end

	# Internal method. This method will be overridden in subclasses.
	def run; end

	# Returns true if the element is a top level.
	def top_level?
		unless @parent then
			true
		else
			false
		end
	end

	# Return a string of values of optional attributes with
	# "other" attribute string excepting the element's own attributes.
	def other_attributes_string
		optional = ''
		each do |key, value|
			if (self.class.own_attributes.include?(key) == false) and \
				(value != nil) and (key != 'other') then
				value = parent.parse_ckd_value value
				optional << " #{key}=\"#{value}\""
			end
		end

		unless other = fetch('other') then
			other = ''
		else
			other = ' ' + other.to_s
		end			

		optional + other
	end

	# Calls block once for each attributes in definition
	# passing the key and value as parameters.
	def each
		definition.each do |key, value|
			unless (key == 'oid') or (key == 'element') then
				yield key, value
			end
		end
	end


	# ElementeAttribute module manages attributes of elements.
	# The module does that following:
	#
	# * add attributes for HTML common attributes to elements 
	# * add attributes for form to elements
	# * add "others" attributes to elements
	# * check attributes of elements
	#
	# == Added HTML common attributes
	# class, id, style, title, dir, lang, onclick, ondblcclick, onmousedown,
	# onmouseup, onmouseover, onmousemove, onmouseout, onkeypress,
	# onkeydown, onkeyup
	#
	# == Added form common attributes
	# enabled
	#
	# == "others" attributes
	#
	module ElementAttribute
		HTML_COMMON_ATTRIBUTES =
			[ 'class', 'id', 'style', 'title', 'dir', 'lang',
			  'onclick',     'ondblcclick', 'onmousedown', 'onmouseup',
			  'onmouseover', 'onmousemove', 'onmouseout',  'onkeypress',
			  'onkeydown',   'onkeyup' ]
		FORM_COMMON_ATTRIBUTES = ['enabled']
		OTHERS_ATTRIBUTE       = ['others']

		# Raises exception if the element has undefined attributes.
		def check_undefined_attributes( *attrs )
			attrs.flatten!
			attrs << 'oid'
			attrs << 'element'

			attr = name = nil
			definition.each_key do | attr |
				if ( attrs.include? attr ) == false then
					raise AttributeError, \
					"Attribute '#{attr}' is undefined attribute - #{name_with_class}"
				end
			end
		end

		# Raises an exception if the element doesn't have required attributes.
		def check_required_attributes( *patterns )
			requires = 0
			attrs    = []
			pattern  = attr = nil

			patterns.each do | pattern |
				pattern.each do | attr |
					requires += 1 if definition.include? attr
				end

				return true if requires == pattern.size
				requires = 0
				attrs << ( "'" + pattern.join(', ') + "'" )
			end

			message =  "#{self.class} requires #{attrs.join(' or ')} "
			message << "of attribute - #{name_with_class}"
			raise AttributeError, message
		end

		# Raises an exception if the element has attributes conflicting with 
		# each other.
		def check_conflicted_attributes( *attributes )
			combination = []
			attr = nil

			attributes.each do | attr |
				if definition.include? attr then
					combination << attr
				end
			end

			if combination.size >= 2 then
				_attr    = nil
				string   = ''
				pop1_str = combination.pop
				pop2_str = combination.pop
	
				combination.each do | _attr |
					string << "\"#{_attr}\", "
				end
				string << "\"#{pop2_str}\" or \"#{pop1_str}\""

				msg = "Exactly one of #{string} must be bound - "
				msg << name_with_class
				raise AttributeError, msg
			end
		end

		# Returns a string with HTML common attributes setted in the element.
		def string_for_html_common_attributes
			string = ''
			attr = value = nil
			HTML_COMMON_ATTRIBUTES.each do | attr |
				value = fetch attr
				if value then
					string << " #{attr}=\"#{value}\"" 
				end
			end
			string
		end

		# Returns a string with form common attributes setted in the element.
		def string_for_form_common_attributes
			string = ''
			attr = value = nil
			FORM_COMMON_ATTRIBUTES.each do | attr |
				value = fetch attr
				if ( attr == 'enabled' ) and ( value == false ) then
					string << " disabled" 
				elsif value and (attr != 'enabled') then
					string << " #{attr}=\"#{value}\"" 
				end
			end
			string
		end

		# Returns an array of names of HTML common attributes.
		def html_common_attributes
			HTML_COMMON_ATTRIBUTES
		end

		# Returns an array of names of form common attributes.
		def form_common_attributes
			FORM_COMMON_ATTRIBUTES
		end
	end


	# ElementState module manages states of elements.
	module ElementState
		class << self
			# Returns a hash parsed a string of state.
			def parse( component, string )
				hash     = {}
				splitted = []

				items = string.split ','
				items.each do | item |
					splitted = item.split ':'
					hash[splitted.first] = component.parse_ckd_value splitted.last
				end
				hash
			end
		end

		# Returns a hash of state for CKConditional.
		def state_for_conditional
			state( 'CKConditional', 'condition', 'negate' )
		end

		# Returns a hash of state.
		def state( element, *attributes )
			value  = nil
			values = []
			query  = {}

			parent.definitions.each do | key, definition |
				if definition['element'] == element then
					id           = CKElementID.new
					id.component = parent.class.to_s
					id.element   = definition['oid']

					values = []
					attributes.each do | attr |
						value = parent.parse_ckd_value( definition[attr] )
						values << "#{attr}:#{value.inspect}"
					end
					query[id.to_s] = values.join ','
				end
			end
			query
		end
	end

end



module CKValidatable
	def validate_with_format_key( key, format_key, pass_key )
		format = parse_ckd_value format_key
		validate(key, format, pass_key)
	end

	def validate( key, format, pass_key )
		qualifier = CKQualifier.format format

		if qualifier.eval? self then
			self[pass_key] = true
		else
			self[pass_key] = false
		end
	end
end


# When you define your component, its class must inherit CKComponent in code
# file. The component dynamically renders HTML with template and binding file. 
#
# A component has 3 files in a directory whose name is the same as the
# component's name. These three files composite Model-View-Controller
# architecture.
#
# Template(View)::      A HTML file includes CGIKit tags ( <cgikit> ). One
#                       CGIKit tag corresponds to one dynamic element.
#
# Binding(Controller):: A definition file for elements is used by a component.
#                       The suffix, ckd, is abbreviation of "CGIKit Definition".
#                       The definition file connects Template to Code.
#
# Code(Model)::         A Ruby source where a component's class is defined. 
#                       It must inherit CKComponent. The name of the component's
#                       class must be the same as the component name. 
#
# ex) An application with a MainPage component
#  /cgi-bin/cgikit-app
#    cgikit-app.cgi
#    /MainPage
#      MainPage.html
#      MainPage.ckd
#      MainPage.rb
#
# == Programming Topics
# * Architecture[www.spice-of-life.net/download/cgikit/en/userguide/architecture.html]
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKComponent < CKElement
	include CKKeyValueCoding, CKValidatable

	class FileNotFoundError < CKError; end #:nodoc:

	# Path for structual files of the component.
	attr_reader :path

	# Hash of definitions in a binding file of the component.	
	attr_accessor :definitions

	attr_accessor :is_top

	class << self
		# Enables accessing instance variables directly with value(), take_value().
		def access_instance_variables?
			true
		end
	end

	def initialize( app, parent, name, body, path = nil )
		super( app, parent, name, body, path )
		@path = path || "#{app.component_path}/#{_component_name}/#{_component_name}"
		@definitions = CKDefinition.parse_ckd_file definition_file
		@is_top = false
		init
	end

	# Hook method for initialization instead of initialize().
	# Form data is not setted when the method is called.
	def init; end

	# Returns a request object of the application.
	def request
		application.request
	end

	# Returns a response object of the application.
	def response
		application.response
	end

	# Returns the session object. If the session isn't existed, returns a new session.
	def session
		application.session
	end

	# Returns a resource manager object.
	def resource_manager
		application.resource_manager
	end

	# Internal method.
	def parse_ckd_value( string )
		object = nil
		if string =~ /\A([^"'].*)\[(.*)\]/ then
			variable = retrieve_value $1
			key      = parse_ckd_value $2
			object   = variable[key]
		elsif string == 'true'   then object = true
		elsif string == 'false'  then object = false
		elsif string == 'nil'    then object = nil
		elsif string =~ /"(.*)"/ then object = $1
		elsif string =~ /'(.*)'/ then object = $1
		elsif ((not string =~ /[\D]/) and (string =~ /[\d]/)) then
			object = string.to_i
		else
			object = retrieve_value string if string
		end

		object
	end

	# Internal method.
	def variable?( string )
		if ( string == 'true' )   or ( string == 'false'  ) or \
		   ( string == 'nil'  )   or ( string =~ /"(.*)"/ ) or \
		   ( string =~ /'(.*)'/ ) or \
		   ( ( not string =~ /[\D]/ ) and ( string =~ /[\d]/ ) ) or \
			( self.respond_to?("#{string}=") == false ) then
			false
		else
			true
		end
	end

	# Returns name of the template file.
	def template_file
		if application.master_locale? then
			"#{path}.html"
		else
			file = "#{path}_#{application.locale}.html".untaint
			if FileTest.exist? file then
				return file
			end

			unless request.languages.empty? then
				request.languages.each do |lang|
					file = "#{path}_#{lang}.html".untaint
					if FileTest.exist? file then
						return file
					end
				end
			end

			"#{path}.html"
		end
	end

	# Returns body of the template file as string.
	def template_string
		begin
			f = File.new template_file.untaint
			@template_string = f.read
			f.close
		rescue
			msg = "Can't read a template file - #{template_file}"
			raise FileNotFoundError, msg
		end

		@template_string
	end

	# Returns name of the definition( binding ) file.
	def definition_file
		"#{path}.ckd".untaint
	end

	# Returns body of the definition( binding ) file as string.
	def definition_string
		unless @definition_string then
			begin
				f = File.new definition_file
				@definition_string = f.readlines.join
				f.close
			rescue
				msg = "Can't read a binding file - #{definition_file} #{self.class}"
				raise FileNotFoundError, msg
			end
		end

		@definition_string
	end

	# Creates a specified page component.
	def page( page )
		CKElement.instance( page, application, parent, nil, nil )
	end

	# Invokes the action after binding component's variables to attributes
	# for definitions, values from request to component's variables.
	def run
		_bind_attribute
		_bind_request

		if is_top then
			pre_action
			result = _invoke_action
			post_action
		end

		result
	end

	private

	# Binds attribute setted in parent component to self.
	# This method is for nested components.
	def _bind_attribute
		if parent and definition then
			defaults = { "name" => true, "oid" => true, "element" => true }
			definition.each do | key, value |
				unless defaults[key] then
					take_value( key, parent.parse_ckd_value(value) )
				end
			end
		end
	end

	def _bind_request
		id = key = value = klass = klass_name = list_name = item_name = nil

		application.request.form_values.sort.each do | key, value |
			id = CKElementID.new key.to_s
			next unless _component_name == id.component

			if definitions[id.element] then
				klass_name  = definitions[id.element]['element']
			else
				# for radio buttons of CKRadioButton and CKGenericElement
				def_name, attr_value = value.last.split( '.', 2 )
				klass_name           = definitions[def_name]['element']
				id.element           = def_name
				value                = attr_value
			end

			_bind_item_in_repetitions id
			value = convert_char_code value

			CKElement.load_element_file( application.component_path, klass_name )
			klass = Object.const_get klass_name
			klass.bind_request( self, definitions[id.element], value, id )
		end
	end

	def _bind_item_in_repetitions( id )
		rep_name = index = list_name = item_name = list = nil

		# check repetitions reflexively
		if id.repetitions? then
			id.each do | rep_name, index |
				if definitions.include? rep_name then
					list_name = definitions[rep_name]['list']
					item_name = definitions[rep_name]['item']
				end

				if list_name and item_name then
					list = retrieve_value list_name

					if list then
						list.each_with_index do |_item, _index|
							if index == _index then
								take_value(item_name, _item)
								break
							end
						end
					end
				end
			end
		end
	end

	public

	# Hook method to convert character code for the form values.
	def convert_char_code( values )
		if code = application.char_code then
			require 'kconv'

			case code.downcase
			when 'jis'  then kcode = Kconv::JIS
			when 'sjis' then kcode = Kconv::SJIS
			when 'euc'  then kcode = Kconv::EUC
			else
				return values
			end

			k_value = []
			values.each do |value|
				if String === value then
					k_value << Kconv.kconv(value, kcode)
				else
					k_value << value
				end
			end
			values = k_value
		end
		values
	end

	private

	def _invoke_action
		result = nil
		if id = application.element_id then
			_bind_item_in_repetitions id

			# get action
			def_action = nil
			if id.component == _component_name then
				definitions.each_value do | definition |
					if definition['oid'] == id.element then
						case definition['element']
						when 'CKGenericElement' then
							def_action = 'invoke_action'
						when 'CKFrame' then
							def_action = 'value'
						when 'CKImage' then
							def_action = 'data'
							mime = parse_ckd_value definition['mime']
							response.headers['Content-Type'] = mime
						else
							def_action = 'action'
						end

						@action = definition[def_action]
					end
				end

				result = retrieve_value @action if @action
			end
		end

		result
	end

	# Returns name of the component in name space package.
	def _component_name
		self.class.to_s.split('::').last
	end

	public

	# Hook method called after setting form data, before invoking action.
	def pre_action; end

	# Hook method called after run() that are setting form data and invoking action.
	def post_action; end

	# Converts the component to HTML.
	def to_s
		parser = CKHTMLParser.new( self, template_string )
		parser.parse
	end
end

# CKString shows the result of binding as string.
#
# == Bindings
# Required attributes: <b>value</b>
#
# <b>value</b>::  Text to be displayed.   
# <b>escape</b>:: Escapes HTML control characters in "value" if the "escape"
#                 is true. The default value is true.
# <b>empty</b>::  Text that is substituted for "value"
#                 when the "value" is nil.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKString < CKElement
	include ElementAttribute

	class << self
		def own_attributes
			['value', 'escape', 'empty']
		end
	end

	def run	
		@attr_value  = fetch( 'value' )  # String
		@attr_escape = fetch( 'escape' ) # true or false
		@attr_empty  = fetch( 'empty' )  # String
		if @attr_escape == nil then
			@attr_escape = true
		end

		check_required_attributes( ['value'], ['empty'] )
	end

	def to_s
		to_s = nil

		if @attr_value then
			to_s = @attr_value
		elsif @attr_empty then
			to_s = @attr_empty
		end

		if to_s and (@attr_escape == true) then
			to_s = CKUtilities.escape_html to_s
		end

		to_s
	end
end


# CKHyperlink generates a hypertext link.
#
# == Bindings
# Required attributes: <b>action</b>, <b>href</b> or <b>page</b>.
#
# <b>action</b>::  Method to be invoked when the link is clicked.
# <b>enabled</b>:: Generates a non-active link if the value is false.
# <b>href</b>::    You specify the URL to other web page directly.
#                  This attribute prevails over "action" or "page" attribute.
# <b>page</b>::    Name of component to display when the link is clicked.
# <b>string</b>::  Text of the link. If the body of CKHyperlink tag is
#                  not empty, the body is displayed. For example,
#                  if the template includes "<cgikit name=link>foo</cgikit>",
#                  this element shows "foo" as the link.
# <b>target</b>::  target attribute of HTML's <a> tag. 
# <b>secure</b>::  Appends "https://" to the URL if the value is true.
#                  The default value is false.
# <b>query</b>::   Hash as the query string. The value of "query" attribute is
#                  converted to string. Then, the string is added to append
#                  the URL.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKHyperlink < CKElement
	include ElementAttribute

	class << self
		def own_attributes
			['action', 'enabled', 'href', 'page', 'string', 'target',
			 'secure', 'query']
		end
	end

	def run
		@attr_action  = fetch( 'action', false ) # CKComponent
		@attr_enabled = fetch( 'enabled' )       # true or false
		@attr_href    = fetch( 'href' )          # String
		@attr_page    = fetch( 'page' )          # String
		@attr_string  = fetch( 'string' )        # String
		@attr_target  = fetch( 'target' )        # String
		@attr_secure  = fetch( 'secure' )        # true or false
		@attr_query   = fetch( 'query' ) || {}   # Hash

		if @attr_string then
			@attr_string = @attr_string.to_s
		end

		check_required_attributes( ['action'], ['href'], ['page'] )
		check_conflicted_attributes( 'action', 'href', 'page' )
	end

	def to_s
		container = nil
		unless body.empty? then
			parser                  = CKHTMLParser.new( parent, body )
			parser.repetitions      = @repetitions
			parser.repetition_index = @repetition_index
			container               = parser.parse
		end

		# action
		if    @attr_page   then id = CKElementID.new @attr_page
		elsif @attr_action then id = element_id
		else                    id = nil end
		@attr_action = application.url( id, @attr_query, @attr_secure )

		# to_s
		to_s = '<a'
		if   @attr_href    then to_s << " href=\"#@attr_href\""
		else                    to_s << " href=\"#@attr_action\""   end
		if   @attr_target  then to_s << " target=\"#@attr_target\"" end
		to_s << other_attributes_string
		to_s << '>'
		if    @attr_string then to_s << @attr_string
		elsif container    then to_s << container end
		to_s << '</a>'
		
		if @attr_enabled == false then
			to_s = @attr_string
		end
		if to_s.nil? || to_s.length == 0 then
			to_s = container
		end

		to_s
	end
end


# Creates an image tag.
#
# == Bindings
# Required attributes: <b>file</b>, <b>src</b>, or <b>data</b>.
#
# <b>alt</b>::    Alternative text to the picture.
# <b>border</b>:: Size of image border.
# <b>width</b>::  Width of image.
# <b>height</b>:: Height of image.
# <b>file</b>::   Name of an image file in web server resource directory.
# <b>src</b>::    You specify an image file directly. This value prevails 
#                 over "file" attribute.
# <b>data</b>::   A CKByteData object to display as image.
#                 If you create the object without resource manager,
#                 You must use this with "mime" attribute.
# <b>mime</b>::   MIME type for a resource of "data" attribute.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKImage < CKElement
	include ElementAttribute

	class << self
		def own_attributes
			['alt', 'border', 'width', 'height', 'file', 'src', 'data', 'mime']
		end
	end

	def run
		@attr_alt    = fetch( 'alt' )         # String
		@attr_border = fetch( 'border' )      # Integer
		@attr_width  = fetch( 'width' )       # Integer
		@attr_height = fetch( 'height' )      # Integer
		@attr_file   = fetch( 'file' )        # String
		@attr_src    = fetch( 'src' )         # String
		@attr_data   = fetch( 'data', false ) # String

		check_required_attributes( ['file'], ['src'], ['data','mime'] )
		check_conflicted_attributes( 'file', 'src', 'data' )
	end

	def to_s
		if @attr_file then
			@attr_src = application.resource_manager.url @attr_file
		elsif @attr_data then
			@attr_src = application.url element_id
		end

		to_s = "<img src=\"#@attr_src\" alt=\"#@attr_alt\""
		if @attr_border then to_s << " border=\"#@attr_border\"" end
		if @attr_width  then to_s << " width=\"#@attr_width\"" end
		if @attr_height then to_s << " height=\"#@attr_height\"" end
		to_s << other_attributes_string
		to_s << '>'
	end
end


# Controls generating HTML.
#
# == Bindings
# Required attribute: <b>condition</b>
#
# <b>condition</b>:: If the value is true and "negate" is false, the body of the 
#                    CKCoditional tag is displayed.
# <b>negate</b>::    Inverts the meaning of the "condition".
#
# == Control table
#   condition  negate  result
#   true       false   show
#   false      false   not show
#   true       true    not show
#   false      false   show
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKConditional < CKElement
	include ElementAttribute, ElementState

	class << self
		def bind_request( component, definition, value, id )
			parsed = CKElement::ElementState.parse( component, value.first )

			# change if the definition is variable
			if component.variable? definition['condition'] then
				component.take_value( definition['condition'], parsed['condition'])
			end
			if component.variable? definition['negate'] then
				component.take_value( definition['negate'], parsed['negate'] )
			end
		end

		def own_attributes
			['condition', 'negate']
		end
	end

	def run
		@attr_condition = fetch( 'condition' )
		@attr_negate    = fetch( 'negate' )

		check_required_attributes( ['condition'] )
	end

	def to_s
		to_s = ''
		if ( ( ( not @attr_negate    ) and @attr_condition ) or \
		     ( ( not @attr_condition ) and @attr_negate    ) ) then
			parser                  = CKHTMLParser.new( parent, body )
			parser.repetitions      = @repetitions
			parser.repetition_index = @repetition_index
			to_s = parser.parse
		end
		to_s
	end
end


# A CKRepeition object repeats its contents. 
#
# == Bindings
# Required attributes: <b>list</b> and <b>item</b>, or <b>count</b>
#
# <b>count</b>:: CKRepeition repeats its contents this number of times.
#                The attribute conflicts with "list".
# <b>list</b>::  Array which is iterated through.
# <b>item</b>::  Current item when the list is iterated through.
# <b>index</b>:: Index of the current item.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKRepetition < CKElement
	include ElementAttribute

	class << self
		def own_attributes
			['count', 'list', 'item', 'index']
		end
	end

	def run
		@attr_count = fetch( 'count' )        # Integer
		@attr_item  = fetch( 'item', false )
		@attr_list  = fetch( 'list' )         # Enumerable
		@attr_index = fetch( 'index', false )

		@repetitions << [ @name, @repetition_index ]

		check_required_attributes( ['list', 'item'], ['count'] )
		check_conflicted_attributes( 'list', 'count' )
	end

 	def to_s
 		to_s  = ''

 		if @attr_list then
			@attr_list.each_with_index do | item, index |
				parent.take_value( @attr_item, item )
				to_s << _iteration( index )
			end
		elsif @attr_count then
			@attr_count.times do | index |
				to_s << _iteration( index )
 			end
 		end
 		to_s
 	end

	private
	def _iteration( index )
		parent.take_value( @attr_index, index ) if @attr_index
		parser                  = CKHTMLParser.new( parent, body )
		parser.repetitions      = @repetitions
		parser.repetition_index = index
		parser.parse
	end
end


# CKContent is used in nested components.
# This element tag in the template is replaced
# with a part of the template of its grandparent component.
#
# == Bindings
# CKContent doesn't have any attribute.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKContent < CKElement
	include ElementAttribute

	def to_s
		unless parent.parent
			return
		end

		parser = CKHTMLParser.new( parent.parent, parent.body )
		parser.parse
	end
end


# CKFrame generates frame tag in HTML.
#
# == Bindings
# Required attributes: <b>page</b>, <b>src</b> or <b>value</b>.
#
# The priorities of "src", "page" and "value" is "src", 
# "page", "value". 
# 
# <b>name</b>::  "name" attribute of HTML's <frame> tag.
# <b>page</b>::  Name of component that supplies the content
#                for the frame.
# <b>src</b>::   You specify the URL or file for the frame. 
# <b>value</b>:: Method that supplies the content. The parent of this element
#                must have the specified method.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKFrame < CKElement
	include ElementAttribute

	class << self
		def own_attributes
			['name', 'page', 'src', 'value']
		end
	end

	def run
		@attr_name  = fetch( 'name' )  # String
		@attr_page  = fetch( 'page' )  # String
		@attr_src   = fetch( 'src' )   # String
		@attr_value = fetch( 'value' ) # CKComponent

		check_required_attributes( ['page'], ['src'], ['value'] )
		check_conflicted_attributes( 'page', 'src', 'value' )
	end

	def to_s
		if @attr_src then
		elsif @attr_page then
			id = CKElementID.new @attr_page
			@attr_src = application.url id
		elsif @attr_value then
			@attr_src = application.url element_id
		end

		to_s =  "<frame name=\"#@attr_name\" src=\"#@attr_src\""
		to_s << other_attributes_string
		to_s << ">"
	end
end


# CKGenericElement generates generic HTML tags.
#
# == Bindings
#
# Required attributes: <b>tag</b>
#
# <b>tag</b>::           Name of the HTML tag. If the attribute is nil,
#                        body enclosed by the element or "string"
#                        attribute are displayed.
# <b>enabled</b>::       Enables or disables the tag. If the attribute is false,
#                        body enclosed by the element or "string"
#                        attribute are displayed.
# <b>string</b>::        String to display if body enclosed by the element
#                        isn't exist.
# <b>option</b>::        String to append for the open tag. For example,
#                        "checked" or "selected".
# <b>form_value</b>::    If the element is form, the attribute is setted
#                        form datas as a string.
# <b>form_values</b>::   If the element is form, the attribute is setted
#                        form datas as an array.
# <b>invoke_action</b>:: If the element is executable ( hyperlink, button,
#                        etc. ), the method is called when clicked.
#
# You can define other voluntary attributes.
# The attributes is appended to the tag in format as "attribute=value".
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKGenericElement < CKElement
	include ElementAttribute

	NO_CLOSE_TAGS = [ 'area', 'base', 'basefont', 'br', 'col', 'frame', 'hr',
	                  'img', 'input', 'link', 'map', 'meta', 'param' ]

	class << self
		def bind_request( component, definition, value, id )
			if definition['invoke_action'] then
				component.application.element_id = id
			end

			if action = definition['form_value'] then
				if id.repetitions? then
					list          = component.retrieve_value action
					list_id       = id.repetitions.last.last
					list[list_id] = value.first
				else
					component.take_value( action, value.first )
				end
			end

			if action = definition['form_values'] then
				if id.repetitions? then
					list          = component.retrieve_value action
					list_id       = id.repetitions.last.last
					list[list_id] = value
				else
					component.take_value( action, value )
				end
			end
		end

		def own_attributes
			['tag', 'enabled', 'string', 'option', 'form_value',
			 'form_values', 'invoke_action']
		end
	end

	def run
		@attr_name          = element_id                      # String
		@attr_tag           = fetch( 'tag' )                  # String
		@attr_enabled       = fetch( 'enabled' ) == false ? false : true # true/false
		@attr_string        = fetch( 'string' )               # String
		@attr_value         = fetch( 'value' )                # String
		@attr_form_value    = fetch( 'form_value' )           # String
		@attr_form_values   = fetch( 'form_values' )          # String
		@attr_invoke_action = fetch( 'invoke_action', false ) # String
		@attr_option        = fetch( 'option' )               # String

		check_required_attributes( ['tag'] )
	end

	def to_s
		string = nil
		if @attr_string then
			string = @attr_string
		elsif body.empty? == false then
			parser                  = CKHTMLParser.new( parent, body )
			parser.repetitions      = @repetitions
			parser.repetition_index = @repetition_index
			container               = parser.parse

			if container then string = container
			else              string = @attr_string end
		end

		to_s = value = href = nil
		if @attr_enabled == true then
			# hyperlink, <form> or <a>
			if    @attr_invoke_action   then href = application.url element_id
			elsif _href = fetch('href') then href = _href end

			# value
			if    @attr_value       then value = @attr_value
			elsif @attr_form_value  then value = @attr_form_value
			elsif @attr_form_values then
				value = @attr_form_values[@repetition_index]
			end

			# radiobutton
			if (@attr_tag.downcase == 'input') and \
				(fetch('type').downcase == 'radio') and (_name = fetch('name')) then
				@attr_name.element = _name
				if value == nil then value = 'on' end
				value = "#{definition['oid']}.#{value}"
			end

			to_s =  "<#@attr_tag name=\"#@attr_name\""
			to_s << " value=\"#{value}\"" if value
			to_s << " href=\"#{href}\""   if href
			to_s << " #@attr_option"      if @attr_option
			to_s << other_attributes_string
			to_s << '>'
			to_s << string if string

			unless NO_CLOSE_TAGS.include? @attr_tag then
				to_s << "</#@attr_tag>"
			end
		else
			to_s = string
		end

		to_s
	end
end


# Creates a fill-in form.
# Dynamic elements of form, for example, CKBrowser, CKCheckbox, CKRadioButton,
# CKPopUpButton, CKText, CKTextField, CKSubmitButton, CKResetButton and
# CKFileUpload are used within CKForm or HTML form.
#
# == Bindings
# Required attributes: none
#
# <b>method</b>::     Encode type to send form data.
#                     You can use "POST" or "GET" as the value.
# <b>enctype</b>::    Encode type for form data. When you use CKFileUpload,
#                     set this attribute to "multipart/form-data".
# <b>fileupload</b>:: If you set this attribute to true,
#                     "enctype" attribute is setted to "multipart/form-data".
#                     You can use this instead of "enctype" when using CKFileUpload.
# <b>href</b>::       URL to which the browser directs.
# <b>target</b>::     Frame in a frameset that receive the page.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKForm < CKElement
	include ElementState, ElementAttribute

	class << self
		def bind_request( component, definition, value, id )
			component.application.element_id = id
		end

		def own_attributes
			['method', 'enctype', 'fileupload', 'href', 'target']
		end
	end

	def run
		@attr_method     = fetch('method') || 'post'              # String
		@attr_enctype    = fetch 'enctype'                        # String
		@attr_href       = fetch('href')   || application.baseurl # String
		@attr_target     = fetch 'target'                         # String
		@attr_query      = fetch('query')  || {}                  # Hash
		@attr_fileupload = fetch 'fileupload'                     # true / false

		unless ( @attr_method.downcase == 'post' ) or \
		       ( @attr_method.downcase == 'get'  ) then
			@attr_method = 'post'
		end

		if @attr_fileupload == true then
			@attr_enctype = "multipart/form-data"
		end

		@attr_query.update state_for_conditional
	end

	def to_s
		parser               = CKHTMLParser.new( parent, body )
		value                = parser.parse
		element_id           = CKElementID.new
		element_id.component = parent.class.to_s
		element_id.element   = name
		
		to_s =  "<form method=\"#@attr_method\""
		to_s << " action=\"#@attr_href\""     if @attr_href
		to_s << " target=\"#@attr_target\""   if @attr_target
		to_s << " enctype=\"#@attr_enctype\"" if @attr_enctype
		to_s << other_attributes_string
		to_s << ">\n<div>\n"
		to_s << "<input type=\"hidden\" name=\"element_id\" "
		to_s << "value=\"#{element_id}\">\n"

		if application.session? then
			to_s << "<input type=\"hidden\" "
			to_s << "name=\"#{application.session_key}\" "
			to_s << "value=\"#{application.session_id}\">\n"
		end
		if @attr_query then
			to_s << _create_hidden_field( @attr_query )
		end

		to_s << "</div>\n#{value}\n</form>\n"
	end

	private

	def _create_hidden_field( query )
		fields = ''
		query.each do | key, value |
			fields << \
				"<input type=\"hidden\" name=\"#{key}\" value=\"#{value}\">\n"
		end
		fields
	end
end


# Creates a list whose multiple items can be selected.
# This element must be used within CKForm or HTML form.
#
# == Bindings
# Required attribute: <b>list</b>
#
# <b>escape</b>::   Escapes HTML control characters in "value"
#                   if the "escape" is true. The default value is true.
# <b>list</b>::     Array which is iterated through.
# <b>values</b>::   Array which is value for each "value" attributes of
#                   <option> elements.
# <b>selected</b>:: Items which are chosen from the list.
# <b>multiple</b>:: Multiple items of the list can be selected
#                   if the value is true.
# <b>size</b>::     Size of item in appearance.
# <b>enabled</b>::  If the value is false, the element appears but is not active.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKBrowser < CKElement
	include ElementAttribute

	class << self
		def bind_request( component, definition, value, id )
			if component.parse_ckd_value definition['escape'] then
				value.each do | _value |
					_value = CKUtilities.unescape_html _value
				end
			end

			# set value as Array
			component.take_value( definition['selected'], value )
		end

		def own_attributes
			['escape', 'list', 'values', 'selected', 'multiple', 'size', 'enabled']
		end
	end

	def run
		@attr_name     = element_id
		@attr_escape   = fetch( 'escape' )
		@attr_list     = fetch( 'list' )
		@attr_values   = fetch( 'values' )
		@attr_selected = fetch( 'selected' ) || []
		@attr_multiple = fetch( 'multiple' )
		@attr_size     = fetch( 'size' )

		@attr_escape = true if @attr_escape.nil?

		check_required_attributes( ['list'] )
	end

	def to_s
		to_s = '<select'
		if @attr_name     then to_s << " name=\"#@attr_name\"" end
		if @attr_size     then to_s << " size=\"#@attr_size\"" end
		if @attr_multiple then to_s << " multiple" end
		to_s << other_attributes_string
		to_s << ">\n"

		if @attr_list then
			item = index = value = nil
			@attr_list.each_with_index do | item, index |
				to_s << "<option"
				if (@attr_selected == item) or @attr_selected.include?(item) then
					to_s << " selected"
				end
				if @attr_values then
					value = @attr_values[index]
					if (@attr_selected == value) or @attr_selected.include?(value) then
						to_s << " selected"
					end
					if @attr_escape then
						value = CKUtilities.escape_html value
					end
					to_s << " value=\"#{value}\""
				end
				to_s << ">"

				if @attr_escape then
					item = CKUtilities.escape_html item
				end
				to_s << "#{item}</option>\n"
			end
		end

		to_s << '</select>'
	end
end


# Creates a checkbox.
# This element must be used within CKForm or HTML form.
#
# == Bindings
# Required attributes: <b>selection</b> and <b>value</b>, or <b>checked</b>
#
# You use this element in two ways. One it the way to use "checked" attribute. 
# The other is the way to use both "selection" and "value" attributes. 
#
# <b>checked</b>::   If neither "value" nor "selection" attribute is nil and
#                    the value of "selection" is equal to that of "value",
#                    the check box is checked.
# <b>value</b>::     When the check box is checked,
#                    the value of "value" attribute is set to the component by
#                    the method specified by "selection" attribute.
# <b>selection</b>:: Object that the user chose from the check box.
# <b>enabled</b>::   If the value is false, the element appears but is not active.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKCheckbox < CKElement
	include ElementAttribute

	class << self
		def bind_request( component, definition, value, id )
			if definition['checked']
				action = definition['checked']
				value  = true
			else
				action = definition['selection']
				value  = value.last
			end
			component.take_value( action, value )
		end

		def own_attributes
			['checked', 'value', 'selection', 'enabled']
		end
	end

	def run
		@attr_name      = element_id.to_s
		@attr_checked   = fetch( 'checked' )   # true or false
		@attr_value     = fetch( 'value' )     # String
		@attr_selection = fetch( 'selection' ) # String
		if ( ( @attr_value and @attr_selection ) and \
		     ( @attr_value ==  @attr_selection ) ) then
			@attr_checked = true
		end

		if application.request.form_values[element_id.to_s].empty? then
			@attr_checked = false
			if attr_name = fetch('checked', false) then
				parent.take_value(attr_name, false)
			end
		end

		check_required_attributes( ['selection','value'], ['checked'] )
		check_conflicted_attributes( 'selection', 'checked' )
		check_conflicted_attributes( 'value', 'checked' )
	end

	def to_s
		to_s =  "<input type=\"checkbox\" name=\"#@attr_name\""
		to_s << %Q' value="#{@attr_value || 1}"'
		if @attr_checked then to_s << " checked" end
		to_s << other_attributes_string
		to_s << '>'
	end
end


# Creates a pop-up menu.
# This element must be used within CKForm or HTML form.
#
# == Bindings
# Required attribute: <b>list</b>
#
# <b>escape</b>::   Escapes HTML control characters in the items of the list
#                   if the "escape" is true. The default value is true.
# <b>list</b>::     Array which is iterated through.
# <b>values</b>::   Array which is value for each "value" attributes of
#                   <option> elements.
# <b>default</b>::  The first item if no item is selected.
# <b>selected</b>:: Item that are chosen from the selection list.
# <b>enabled</b>::  If the value is true, the element appears but is not active.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKPopUpButton < CKElement
	include ElementAttribute

	class << self
		def bind_request( component, definition, value, id )
			if component.parse_ckd_value definition['escape'] then
				value = CKUtilities.unescape_html value.last
			else
				value = value.last
			end

			component.take_value( definition['selected'], value )
		end

		def own_attributes
			['escape', 'list', 'values', 'default', 'selected', 'enabled']
		end
	end

	def run
		@attr_name     = element_id.to_s
		@attr_escape   = fetch( 'escape' ) || true # true or false
		@attr_list     = fetch( 'list' )           # Enumerable
		@attr_values   = fetch( 'values' )         # Enumerable
		@attr_default  = fetch( 'default' )        # String
		@attr_selected = fetch( 'selected' )       # String

		check_required_attributes( ['list'] )
	end

	def to_s
		to_s = '<select'
		if @attr_name then to_s << " name=\"#@attr_name\"" end
		to_s << other_attributes_string
		to_s << ">\n"

		if @attr_default then
			to_s << '<option value=""'
			if @attr_selected == nil then
				to_s << " selected"
			end
			to_s << ">#@attr_default</option>\n"
		end

		if @attr_list then
			item = index = value = nil
			@attr_list.each_with_index do | item, index |
				to_s << "<option"
				if @attr_selected == item then
					to_s << " selected"
				end
				if @attr_values then
					value = @attr_values[index].to_s
					if @attr_selected == value then
						to_s << " selected"
					end
					if @attr_escape then
						value = CKUtilities.escape_html value
					end
					to_s << " value=\"#{value}\""
				end
				to_s << ">"

				# escape
				if @attr_escape then
					item = CKUtilities.escape_html item
				end
				to_s << "#{item}</option>\n"
			end
		end

		to_s << '</select>'
	end
end


# Creates a radio button.
# This element must be used within CKForm or HTML form.
#
# == Bindings
# Required attributes: <b>selection</b> and <b>value</b>, or <b>checked</b>
#
# <b>name</b>::      Name that identifies the radio button's group.
# <b>checked</b>::   If neither "value" nor "selection" attribute is nil and
#                    the value of "selection" is equal to that of "value",
#                    the check box is checked.
# <b>value</b>::     When the check box is checked,
#                    the value of "value" attribute is set to the component by
#                    the method specified by "selection" attribute.
# <b>selection</b>:: Object that the user chose from the check box.
# <b>enabled</b>::   If the value is true, the element appears but is not active.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKRadioButton < CKElement
	include ElementAttribute

	class << self
		def bind_request( component, definition, value, id )
			if definition['checked'] then
				action = definition['checked']
				value  = true
			else
				action = definition['selection']
			end
			component.take_value( action, value )
		end

		def own_attributes
			['name', 'checked', 'value', 'selection', 'enabled']
		end
	end

	def element_id
		id = super
		id.element = @attr_name
		id
	end

	def run
		@attr_name      = fetch( 'name' )      # String
		@attr_checked   = fetch( 'checked' )   # true or false
		@attr_value     = fetch( 'value' )     # String
		@attr_selection = fetch( 'selection' ) # String
		if ( ( @attr_value and @attr_selection ) and \
			( @attr_value == @attr_selection ) ) then
			@attr_checked = true
		end

		check_required_attributes( ['selection','value'], ['checked'] )
		check_conflicted_attributes( 'selection', 'checked' )
		check_conflicted_attributes( 'value', 'checked' )
	end

	def to_s
		value = "#{definition['oid']}.#@attr_value"
		to_s  = "<input type=\"radio\" name=\"#{element_id}\" value=\"#{value}\""
		to_s << " checked"  if @attr_checked
		to_s << other_attributes_string
		to_s << ">"
	end
end


# Creates a reset button.
# This element must be used within CKForm or HTML form.
#
# == Bindings
# Required attributes: none
#
# <b>value</b>:: Title of the button.
# <b>enabled</b>::  If the value is true, the element appears but is not active.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKResetButton < CKElement
	include ElementAttribute

	class << self
		def own_attributes
			['value', 'enabled']
		end
	end

	def run
		@attr_value = fetch( 'value' )    # String
		unless @attr_value then @attr_value = 'Reset' end
	end

	def to_s
		to_s  = "<input type=\"reset\" value=\"#@attr_value\""
		to_s << other_attributes_string
		to_s << ">"
		to_s
	end
end


# Creates a submit button.
# This element must be used within HTML form.
#
# == Bindings
# Required attributes: none
#
# <b>action</b>::   Method to invoke when the button is clicked.
# <b>value</b>::    Title of the button.
# <b>enabled</b>::  If the value is true, the element appears but is not active.
#                   In addition, it doesn't send the form data 
#                   although the button is clicked.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKSubmitButton < CKElement
	include ElementAttribute

	class << self
		def bind_request( component, definition, value, id )
			component.application.element_id = id
		end

		def own_attributes
			['action', 'value', 'enabled']
		end
	end

	def run
		@attr_name     = element_id
		@attr_action   = fetch( 'action', false ) # String
		@attr_value    = fetch( 'value' )         # String
		if @attr_value == nil then @attr_value = 'Submit' end
	end

	def to_s
		to_s = "<input type=\"submit\" name=\"#@attr_name\" value=\"#@attr_value\""
		to_s << other_attributes_string
		to_s << ">"
	end
end


# Creates a text area.
# This element must be used within CKForm or HTML form.
#
# == Bindings
# Required attribute: <b>value</b>
#
# <b>value</b>::    Value of the text area. 
# <b>columns</b>::  Column size.
# <b>rows</b>::     Row size.
# <b>validate</b>:: Format string to validate input value.
# <b>pass</b>::     If validating is passed, the value is true.
# <b>enabled</b>::  If the value is true, the element appears but is not active.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKText < CKElement
	include ElementAttribute

	class << self
		def bind_request( component, definition, value, id )
			key = definition['value']
			component[key] = value.last

			if (format_key = definition['validate']) and \
			   (pass_key = definition['pass']) then
				component.validate_with_format_key(key, format_key, pass_key)
			end
		end

		def own_attributes
			['value', 'columns', 'rows', 'enabled', 'validate', 'pass']
		end
	end

	def run
		@attr_name     = element_id
		@attr_value    = fetch( 'value' )    # String
		@attr_columns  = fetch( 'columns' )  # Integer
		@attr_rows     = fetch( 'rows' )     # Integer

		check_required_attributes( ['value'] )
	end

	def to_s
		to_s = "<textarea name=\"#@attr_name\""
		if @attr_columns  then to_s << " cols=\"#@attr_columns\"" end
		if @attr_rows     then to_s << " rows=\"#@attr_rows\""    end
		to_s << other_attributes_string
		to_s << '>'
		if @attr_value   then to_s << CKUtilities.escape_html(@attr_value) end
		to_s << '</textarea>'
	end
end


# Creates a text input field.
# This element must be used within CKForm or HTML form.
#
# == Bindings
# Required attribute: <b>value</b>
#
# <b>value</b>::     Value of the text field. If you set an accessor method to
#                    this element, the form data is set to a component automatically
#                    by the method.
# <b>type</b>::      Type of the text field. "text" is for a normal text input field,
#                    "password" is for a password field and 
#                    "hidden" is for a hidden field.
# <b>size</b>::      Size of the text field.
# <b>maxlength</b>:: Max length of data for the text field.
# <b>validate</b>::  Format string to validate input value.
# <b>pass</b>::      If validating is passed, the value is true.
# <b>enabled</b>::   If the value is true, the element appears but is not active.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKTextField < CKElement
	include ElementAttribute

	class << self
		def bind_request( component, definition, value, id )
			key = definition['value']
			component[key] = value.last

			if (format_key = definition['validate']) and \
			   (pass_key = definition['pass']) then
				component.validate_with_format_key(key, format_key, pass_key)
			end
		end

		def own_attributes
			['value', 'type', 'size', 'maxlength', 'enabled', 'validate', 'pass']
		end
	end

	def run
		@attr_name      = element_id.to_s
		@attr_type      = fetch( 'type' )      # String
		@attr_value     = fetch( 'value' )     # String
		@attr_size      = fetch( 'size' )      # Integer
		@attr_maxlength = fetch( 'maxlength' ) # Integer
		if @attr_type == nil then @attr_type = 'text' end

		check_required_attributes( ['value'] )

		@attr_value = CKUtilities.escape_html @attr_value
	end

	def to_s
		to_s = "<input type=\"#@attr_type\""
		if @attr_name      then to_s << " name=\"#@attr_name\""           end
		if @attr_value     then to_s << " value=\"#@attr_value\""         end
		if @attr_size      then to_s << " size=\"#@attr_size\""           end
		if @attr_maxlength then to_s << " maxlength=\"#@attr_maxlength\"" end
		to_s << other_attributes_string
		to_s << '>'
	end
end


# CKFileUpload generates an input form to upload files.
# To use this, set "enctype" attribute of CKForm to "multipart/form-data" or
# "fileupload" attribute to true.
#
# == Bindings
# Required attribute: <b>data</b> and <b>file</b>
#
# <b>data</b>::    Variable of the attribute is set the uploaded file as
#                  a CKByteData object. 
# <b>file</b>::    Path of the uploaded file.
# <b>enabled</b>:: If the value is false, the element appears but is not active.
#
# == Programming Topics
# * DynamicElements[www.spice-of-life.net/download/cgikit/en/userguide/elements.html]
class CKFileUpload < CKElement
	include ElementAttribute

	class << self
		def bind_request( component, definition, value, id )
			data = value.last
			if data.is_a? CKByteData then
				component.take_value( definition['data'], data )
				component.take_value( definition['file'], data.path )
			end
		end

		def own_attributes
			['data', 'file', 'enabled']
		end
	end

	def run
		check_required_attributes( ['data', 'file'] )
	end

	def to_s
		to_s  = "<input type=\"file\" name=\"#{element_id}\""
		to_s << other_attributes_string
		to_s << ">"
		to_s
	end
end


# CKPartsMaker is a Ruby module to deal with a component as parts of web page.
# A component which includes this module isn't displayed even if CGIKit recieves
# requests to show it as the target component ( CKApplication#target ).
#
# A name of parts component is recommended to have "Parts" at
# the end of the name to distinguish it from page component.
#
# == Usage
# Subclass of CKComponent includes CKPartsMaker.
#
#  class AnyParts < CKComponent
#    include CKPartsMaker
#    ...
#  end
#
module CKPartsMaker
	# When CGIKit recieves requests to show component parts,
	# CGIKit shows the page specified by this attribute.
	# A main page of an application is displayed when the value is not defined.
	attr_accessor :substitute_page
end


require 'strscan'

# CKDefinition parses .ckd ( CGIKit Definition ) file.
# CKComponent stores the result of parse and uses it for binding.
#
# The format of CKD file is very different from Ruby or HTML
# Here, the format of CKD file is described as pseudo-BNF like Ruby's document.
#
#   STMTS           : (STMT)*
#   STMT            : ELEMENT_NAME ':' ELEMENT_TYPE '{' ATTRS '}'
#   ATTRS           : (ATTRIBUTE '=' VALUE TERM)*
#   ATTRIBUTE       : ruby_method_name
#   VALUE           : METHODS 
#                   | LITERAL 
#   METHODS         : ruby_method_name('.'ruby_method_name)*
#                   | METHODS BLITERAL
#   LITERAL         : 'true'
#                   | 'false'
#                   | DIGIT_SEQUENCE
#                   | STRING
#   BLITERAL        : ARRAY
#                   : HASH
#   ARRAY           : '[' DIGIT_SEQUENCE ']'
#   HASH            : '[' LITERAL ']'
#   DIGIT_SEQUENCE  : DIGIT+
#   DIGIT           : [0-9]
#   STRING          : ''' any_character* '''
#                   | '"' any_character* '"'
#   ELEMENT_NAME    : (LETTER | DIGIT | '_')+
#   ELEMENT_TYPE    : ruby_class_name
#   LETTER          : [A-Za-z]
#   TERM            : ';'
#                   | '\n'
#
# ruby_method_name means Ruby's Method Name: For example, 'foo', 'to_s' and 'is_a?'.
# Also, ruby_class means Ruby's Class Name: For instance, 'CKString' and 'CKComponent'.
#
# CKDefinition regards a part which begins from '#' to the end of the line as comment.
# In the next case, '# foo bar comment' is not interpreted by CKDefinition.
#
#   foo : CKString {
#     # foo bar comment
#     value = "foo";
#   }
#
class CKDefinition
	class CKDParseError < CKError ; end #:nodoc:

	class << self
		def parse_ckd_file(filename)
			filename.untaint
			string = nil

			File.open(filename) do |f|
				string = f.read
			end

			parse(string, filename)
		end

		def parse(string, filename = '')
			scanner = StringScanner.new( string, false )

			# The way to use these local variables is bad.			
			definitions     = {}
			in_attrs        = false
			line_num        = 1			
			current_element = nil
			element_name    = nil
			element_type    = nil
			term            = "(;|\n)"

			# in the future, we may have to change this Proc object.
			pos_proc = proc do 
				"#{filename}:#{line_num}: "
			end
			
			while scanner.rest?
				# line break
				if scanner.skip(/(\r)?\n/) then
					line_num += 1
					next
				end

				# skip commnet or space
				if scanner.skip(/(?:#.*$|[ \r\f\t]+)/) then
					next
				end

				if in_attrs then
					# attribute
					if scanner.skip(/([a-zA-Z0-9_]+[\!\?]?)[ \r\f\t]*=[ \r\f\t]*/)
						key = scanner[1]

						# array and hash literal
						if scanner.skip(/^([^"'][^"';\n]*)\[/) then
							list   = nil
							object = scanner[1]

							if scanner.match?(/^(["'])/) then
								# string literal
								close = scanner[1]
								if	   close == '"' and scanner.skip(/"(.*?)"\]/)
								elsif	close == "'" and scanner.skip(/'(.*?)'\]/)
								else
									raise CKDParseError, \
										pos_proc.call << scanner.peek(60).inspect << \
										"\nString literal error. Or, \";\" doesn't exist."
								end
								list = "\"#{scanner[1]}\""
							else
								if scanner.skip(/([a-zA-Z0-9_\.\!\^?]+)\][ \r\t\f]*#{term}/)
									list = scanner[1]
								else
									raise CKDParseError, pos_proc.call << \
										scanner.peek(60).inspect << \
										"\nArray or Hash literal error. Or, \";\" doesn't exist.\n"
								end
							end
							value = "#{object}[#{list}]"
							line_num += value.count("\n")

						# value of attribute
						elsif scanner.match?(/^(["'])/) then
							# string lietral
							close = scanner[1]
							if	   close == '"' and scanner.skip(/"(.*?)"[ \r\f\t]*#{term}/m)
							elsif	close == "'" and scanner.skip(/'(.*?)'[ \r\f\t]*#{term}/m)
							else
								raise CKDParseError, \
									pos_proc.call << scanner.peek(60).inspect << \
									"\nString literal error. Or, \";\" doesn't exist."
							end

							value = "\"#{scanner[1]}\""
							line_num += value.count("\n")
						else
							# others
							#
							# This regexp is not accurate.
							if scanner.skip(/([a-zA-Z0-9_\.\!\^?]+)[ \r\t\f]*#{term}/)
								value = scanner[1]
							else
								raise CKDParseError, pos_proc.call << \
									scanner.peek(60).inspect << \
									"\nMethod name error. Or, \";\" doesn't exist." "\n" 
							end
						end

						current_element[key] = value			
						next
					end

					# end of definition
					if scanner.skip(/\s*\}/)
						definitions[element_name] = current_element
						current_element = nil
						in_attrs = false
						next
					end

					# skips space and separator.
					#
					# (ex.)
					# foo : CKString{;}
					if scanner.skip(/[ \r\t\f]*#{term}/)
						next
					end
				else
					# extracts name of the element and its class
					# class's regexp is not precise.
				if scanner.skip(/([a-zA-Z0-9_]+)\s*:\s*([a-zA-Z0-9_]+)\s*\{/)
					element_name = scanner[1]
					element_type = scanner[2]
					current_element = {}
					current_element['oid'] = element_name
					current_element['element'] = element_type

					if definitions.key? element_name then
						raise CKDParseError, pos_proc.call << \
							scanner.peek(60).inspect << \
							"\n'#{element_name}' definition is already existed." "\n" 
					end

			
					in_attrs = true
					next
				end
			end

			def_str = _pretty_print(definitions)
				raise CKDParseError, pos_proc.call << scanner.peek(60).inspect << \
					"\nNot match any rule.\n\n" << def_str << "\n" 
			end

			# Now, there is no data to be parsed.			
			if in_attrs 
				raise CKDParseError, pos_proc.call << \
					"The last element is not enclosed.\n\n"
			end
			
			definitions
		end

		def pretty_print(defs)
			print _pretty_print(defs)
		end

		private
		def _pretty_print(defs)
			s = ''
			indent = '		'

			defs.keys.sort.each do |name|
				attrs = defs[name]
				s << name
				s << ":\n"
	
				attrs.keys.sort.each do |k|
					v = attrs[k]
					s << indent 
					s << k 
					s << ': '
					s << v.inspect
					s << "\n"
				end

				s << "\n"
			end

			s
		end
	end
end


# CKHTMLParser parses a template of a component.
# The template is HTML, except one fact that it has
# "<cgikit>" tag. You can use two types of formats as CGIKit tag,
# "<cgikit>...</cgikit>" and "<cgikit />". The difference of the
# formats is whether the tag's body is empty or not. So, if you write
# "<cgikit></cgikit>", it has the same meaning as "<cgikit />".
#
# The CGIKit tag has only one attribute, "name". The "name" attribute is
# a name of CGIKit element. The "name" attribute is composed of "[a-zA-Z0-9_]".
# You can enclose the value of the "name" attribute by double quotation mark.
# And, The value is case-sensitive.
#
# For instance, these have the same meaning.
#
#   <cgikit name=foo></cgikit>
#   <cgikit name=foo />
#   <cgikit name="foo"></cgikit>
#   <cgikit name="foo" />
#
# On the other hand, these have different meanings.
#   <cgikit name=Foo />
#   <cgikit name="foo" />
#   <cgikit name=FOO></cgikit>
#
# Comment of HTML is interpreted by CKHTMLParser because it is
# necessary to include something like CSS and Javascript.
# If you want to comment out some parts of a template, you use "<!--- ... --->".
# The format is like HTML comments, but with an additional hyphen. 
#
#   <html>
#   <head>
#   </head>
#   <body>
#   <!-- HTML comment. This is interpreted. -->
#   <!--- CGIKit comment. This is not interpreted.
#     <!-- HTML comment. Because this is in CGIKit comment,
#          this is not also interpreted. -->
#   --->
#   </body>
#   </html>
#
# As the template is explained in the example, the first HTML comment
# is interpreted and the second HTML comment is not interpreted.
#
# CGIKit comment is not allowed to be nested. So, the next example
# is not allowed.
#
#   <!--- foo bar
#    <!--- foo bar --->
#   --->
class CKHTMLParser

	class CKHTMLParseError < CKError; end #:nodoc:
	
	attr_accessor :application, :html_string, :parent, :repetitions, \
	              :repetition_index

	def initialize( parent, string )
		@parent      = parent
		@application = parent.application
		@html_string = string
		@scanner     = StringScanner.new(string, false)
	end

	def parse
		contents = ''

		each_block_string do |str, kind|
			case kind 
			when :normal
				contents << str
			when :cgikit_tag
				element = _create_element( *_extract_name_body(str) )
				if parsed = element.to_s then 
					contents << parsed 
				end
			when :comment
			else
				raise CKHTMLParseError, "BUGS: CKHTMLParser#parse unknown token"
			end	
		end

		contents
	end

	private
	def _create_element( name, body )
		element_def = parent.definitions[ name ]
		if element_def.nil?
			raise CKHTMLParseError, "Can't create element: " << \
			                        "#{name} of #{parent.name_with_class}"
		end
	
		element = element_def[ 'element' ]
		object = CKElement.instance( element, @application, @parent, name, body )
		if @repetitions then
			object.repetitions      = @repetitions
			object.repetition_index = @repetition_index
		end
		object.run
		object
	end

	public
	def each_block_string
		rough_reg = /<(?:\!|c|\/c)/i #/
		
		comment_reg     = /<\!---[^-](.*?)[^-]--->/mi #/
		cktag_reg       = /<cgikit\s+[^>]*?\/>/i #/
		start_cktag_reg = /<cgikit\s+[^>]*?>/mi	
		end_cktag_reg   = /<\/cgikit\s*>/i #/		
		other_reg       = /(?:.+?(?=<(?:\!|c|\/c))|.+)/mi #/
		
		nest         = 0
		nested_cktag = ''
		str          = nil

		while @scanner.rest?
			# comment or cgikit tag
			if @scanner.match?(rough_reg)
				if str = @scanner.scan(comment_reg)
					yield str, :comment
					next
				end

				if str = @scanner.scan(cktag_reg)
					if nest > 0
						nested_cktag << str
					else
						yield str, :cgikit_tag
					end
					next
				end
		
				if str = @scanner.scan(start_cktag_reg) 	
					nested_cktag << str
					nest += 1
					next
				end
		
				if str = @scanner.scan(end_cktag_reg) 	
					nested_cktag << str
					nest -= 1
		
					if nest == 0
						yield nested_cktag, :cgikit_tag
						nested_cktag = ''
					elsif nest < 0
						raise CKHTMLParseError, \
							"too many end-tags(</cgikit>) in #{@parent.template_file}."
					end
		
					next	
				end
			end
			
			if str = @scanner.scan(other_reg)
				if nest > 0
					nested_cktag << str
				else
					yield str, :normal
				end
				next
			end

			raise CKHTMLParseError, "not match any rule in #{@parent.template_file}."
		end

		if nest != 0
			raise CKHTMLParseError, \
				"a cgikit tag is not closed in #{@parent.template_file}."
		end
	end

	private
	def _extract_name(name)
		if /(['"])(.*?)\1/ =~ name
			name = $2
		end

		name
	end

	def _extract_name_body(str)		
		if str =~ /\A<cgikit\s+name\s*=\s*(.*?)\s*>(.*)<\/cgikit>/im	#/
			name = _extract_name($1)
			body = $2

			return name, body
		elsif str =~ /\A<cgikit\s+name\s*=\s*(.*?)\s*\/>/im	#/
			name = _extract_name($1)
			
			return name, ''
		else
			raise CKHTMLParseError, "can't extract name and body: " << str.inspect
		end
	end
end


# The super class of HTTP Request-Response classes.
class CKMessage
	# HTML content.
	attr_accessor :content

	# Hash of HTTP headers.
	attr_accessor :headers

	# HTTP version. The default value is "1.1".
	attr_accessor :http_version

	# The encoding used for the content.
	attr_accessor :encoding

	# Array of CKCookie objects.
	attr_reader   :cookies

	EOL = "\r\n"

	def initialize( headers = nil )
		@headers      = headers || {}
		@http_version = "1.1"
		@cookies      = []
	end

	# Adds a cookie object.
	def add_cookie( cookie )
		@cookies << cookie
	end

	# Removes the specified cookie in the cookies.
	def remove_cookie( cookie )
		@cookies.delete cookie
	end

	# Returns HTTP version.
	def http_version_line
		"HTTP/#@http_version"
	end
end


# CKRequest represents requests of HTTP headers.
#
# CKRequest returns form parameters and cookies.
# But, in many cases, this class isn't used directly to get parameters 
# because CGIKit sets these parameters to components 
# automatically in the process of instanciating components.
#
# == Getting a request object
# You can get a request object by CKApplication#request or CKComponent#request.
#
# == Parameters
# CKRequest has some methods to access parameters.
#
# <b>form_values[key]</b>::       Returns an array of parameters.
#                                 If key is missing, returns an empty array.
# <b>form_value(key), [key]</b>:: This method returns a first object of
#                                 an array of parameters.
#                                 If key is missing, returns nil.
#
# == Cookies
# Cookies is an array which includes some CKCookie objects.
# See CKCookie section about details of cookie.
#
# <b>cookie(key)</b>::        Returns a CKCookie object whose key is the 
#                             same as the argument.
# <b>cookies</b>::            Returns an array of CKCookie objects.
# <b>cookie_value(key)</b>::  Returns the value of CKCookie object
#                             whose key is the the same as the argument.
# <b>cookie_values(key)</b>:: If the argument is nil (by default, argument is
#                             nil.), this method returns an array which has
#                             all the values of cookies. Otherwise, it returns
#                             an array which has the values of cookies
#                             specified by the argument.
#
# == HTTP Headers
# Some of HTTP headers are defined by instance methods.
# Call CKRequest#headers to get other CGI environment variables.
#
# <b>headers[key]</b>::      Returns a value of HTTP header.
# <b>accept</b>::            HTTP_ACCEPT
# <b>accept_charset</b>::    HTTP_ACCEPT_CHARSET 
# <b>accept_language</b>::   HTTP_ACCEPT_LANGUAGE
# <b>auth_type</b>::         AUTH_TYPE
# <b>content_length</b>::    CONTENT_LENGTH
# <b>content_type</b>::      CONTENT_TYPE
# <b>from</b>::              HTTP_FROM    
# <b>gateway_interface</b>:: GATEWAY_INTERFACE
# <b>path_info</b>::         PATH_INFO
# <b>path_translated</b>::   PATH_TRANSLATED
# <b>query_string</b>::      QUERY_STRING
# <b>raw_cookie</b>::        HTTP_COOKIE
# <b>referer</b>::           HTTP_REFERER
# <b>remote_addr</b>::       REMOTE_ADDR
# <b>remote_host</b>::       HTTP_HOST
# <b>remote_ident</b>::      REMOTE_IDENT
# <b>remote_user</b>::       REMOTE_USER
# <b>request_method</b>::    REQUEST_METHOD
# <b>script_name</b>::       SCRIPT_NAME
# <b>server_name</b>::       SERVER_NAME
# <b>server_port</b>::       SERVER_PORT
# <b>server_protocol</b>::   SERVER_PROTOCOL
# <b>server_software</b>::   SERVER_SOFTWARE
# <b>uri</b>, <b>url</b>::   REQUEST_URI
# <b>user_agent</b>::        HTTP_USER_AGENT
class CKRequest < CKMessage
	# Array of parameters.
	attr_accessor :form_values

	class << self
		# Parse query string and return a hash of parameters.
		def parse_query_string( query )
			params = Hash.new([])
			query.split(/[&;]/n).each do |pairs|
				key, value = pairs.split('=',2).collect{|v| CKUtilities.unescape_url(v) }
				if params.has_key?(key)
					params[key].push(value)
				else
					params[key] = [value]
				end
			end
			params
		end
	end

	def initialize( headers = nil, form_values = nil )
		super headers
		@cookies     = CKCookie.parse_raw_cookie @headers['HTTP_COOKIE']
		@form_values = form_values || Hash.new([])
	end

	# Returns a first object of an array of parameters. If key is missing, returns nil.
	def form_value( key )
		if @form_values.key? key
			@form_values[key].first
		else
			nil
		end
	end

	alias [] form_value

	# Returns a CKCookie object whose key is the same as the argument.
	def cookie( key )
		@cookies.each { | cookie |
			if cookie.name == key
				return cookie
			end
		}
		nil
	end

	# Returns the value of CKCookie object whose key is the the same as the argument.
	def cookie_value( key )
		@cookies.each { | cookie |
			if cookie.name == key
				return cookie.value
			end
		}
		nil
	end

	# If the argument is nil (by default, argument is nil.),
	# this method returns an array which has all the values of cookies. Otherwise,
	# it returns an array which has the values of cookies specified by the argument.
	def cookie_values( key = nil )
		if key then
			_cookie_values_for_key( @cookies, key )
		else
			_cookie_values @cookies
		end
	end

	# Returns an array of languages for localization.
	# The order is the preferred order of languages.
	def languages
		langs   = []
		quality = {}

		if accept_language then
			accept_language.scan(/\s*([^,;]*)\s*;\s*q\s*=\s*([^,]*),|([^,]*),/) do |match|
				lang = match.pop  || match.shift
				q    = (match.pop || 1).to_f

				if lang =~ /([^-]*)-(.*)/ then lang = $1 end

				if quality[lang] then
					if q > quality[lang] then
						quality[lang] = q
					end
				else
					quality[lang] = q
				end
			end

			sorted = quality.sort do |a,b|
				if    a.last <  b.last then  1
				elsif a.last == b.last then  0
				elsif a.last >  b.last then -1
				end
			end

			sorted.each do |lang|
				langs << lang.first
			end
		end

		langs
	end

	def accept;            self.headers['HTTP_ACCEPT']          end
	def accept_charset;    self.headers['HTTP_ACCEPT_CHARSET']  end
	def accept_language;   self.headers['HTTP_ACCEPT_LANGUAGE'] end
	def auth_type;         self.headers['AUTH_TYPE']            end
	def content_length;    self.headers['CONTENT_LENGTH']       end
	def content_type;      self.headers['CONTENT_TYPE']         end
	def from;              self.headers['HTTP_FROM']            end
	def gateway_interface; self.headers['GATEWAY_INTERFACE']    end
	def path_info;         self.headers['PATH_INFO']            end
	def path_translated;   self.headers['PATH_TRANSLATED']      end
	def query_string;      self.headers['QUERY_STRING']         end
	def raw_cookie;        self.headers['HTTP_COOKIE']          end
	def referer;           self.headers['HTTP_REFERER']         end
	def remote_addr;       self.headers['REMOTE_ADDR']          end
	def remote_host;       self.headers['HTTP_HOST']            end
	def remote_ident;      self.headers['REMOTE_IDENT']         end
	def remote_user;       self.headers['REMOTE_USER']          end
	def request_method;    self.headers['REQUEST_METHOD']       end
	def script_name;       self.headers['SCRIPT_NAME']          end
	def server_name;       self.headers['SERVER_NAME']          end
	def server_port;       self.headers['SERVER_PORT']          end
	def server_protocol;   self.headers['SERVER_PROTOCOL']      end
	def server_software;   self.headers['SERVER_SOFTWARE']      end
	def uri;               self.headers['REQUEST_URI']          end
	def user_agent;        self.headers['HTTP_USER_AGENT']      end

	alias url uri

	private

	def _cookie_values( cookies )
		values = {}
		cookies.each { | cookie |
			values[ cookie.name ] = cookie.value
		}
		values
	end

	def _cookie_values_for_key( cookies, key )
		values = []
		cookies.each { | cookie |
			if cookie.name == key
				values << cookie.value
			end
		}
		values
	end
end


# A response object that is sent to a browser by a CKAdapter object.
#
# == Getting a response object
# You can get a response object by CKApplication#response or CKComponent#response.
#
# == Setting headers for a response object
# To send HTTP response headers, append a pair of key and value to headers.
# For example,
#
#  application.response.headers['Content-Type'] = 'text/html'
#
class CKResponse < CKMessage
	# Status code in HTTP. Default status is 200 ( OK ).
	attr_accessor :status

	STATUS = { 
		100 => 'Continue',
		101 => 'Switching Protocols',
		200 => 'OK',
		201 => 'Created',
		202 => 'Accepted',
		203 => 'Non-Authoritative Information',
		204 => 'No Content',
		205 => 'Reset Content',
		206 => 'Partial Content',
		300 => 'Multiple Choices',
		301 => 'Moved Permanently',
		302 => 'Found',
		303 => 'See Other',
		304 => 'Not Modified',
		305 => 'Use Proxy',
		307 => 'Temporary Redirect',
		400 => 'Bad Request',
		401 => 'Unauthorized',
		402 => 'Payment Required',
		403 => 'Forbidden',
		404 => 'Not Found',
		405 => 'Method Not Allowed',
		406 => 'Not Acceptable',
		407 => 'Proxy Authentication Required',
		408 => 'Request Timeout',
		409 => 'Conflict',
		410 => 'Gone',
		411 => 'Length Required',
		412 => 'Precondition Failed',
		413 => 'Request Entity Too Large',
		414 => 'Request-URI Too Long',
		415 => 'Unsupported Media Type',
		416 => 'Requested Range Not Satisfiable',
		417 => 'Expectation Failed',
		500 => 'Internal Server Error',
		501 => 'Not Implemented',
		502 => 'Bad Gateway',
		503 => 'Service Unavailable',
		504 => 'Gateway Timeout',
		505 => 'HTTP Version Not Supported'
	}

	def initialize( headers = nil )
		super
		@status = 200
		@headers['Content-Type'] = 'text/html'
	end

	def to_s
		response = ''
		response << ( __cookie || '' )
		response << ( _general_header  || '' )
		response << ( _response_header || '' )
		response << _entity_header
		response << EOL
		response << ( self.content     || '' ) unless redirect?
		response
	end

	private

	def _general_header
		header = ''
		header << ( __header('Cache-Control') || '' )
		header << ( __header('Connection') || '' )
		header << ( __header('Date') || '' )
		header << ( __header('Pragma') || '' )
		header << ( __header('Trailer') || '' )
		header << ( __header('Transfet-Encoding') || '' )
		header << ( __header('Upgrade') || '' )
		header << ( __header('Via') || '' )
		header << ( __header('Warning') || '' )
	end

	def _response_header
		header = ''
		header << ( __header('Accept-Ranges') || '' )
		header << ( __header('Age') || '' )
		header << ( __header('ETag') || '' )
		header << ( __header('Location') || '' )
		header << ( __header('Proxy-Authenticate') || '' )
		header << ( __header('Retry-After') || '' )
		header << ( __header('Server') || '' )
		header << ( __header('Vary') || '' )
		header << ( __header('WWW-Authenticate') || '' )
	end

	def _entity_header
		header = ''
		header << ( __header('Allow') || '' )
		header << ( __header('Content-Encoding') || '' )
		header << ( __header('Content-Language') || '' )
		header << ( __header('Content-Length') || '' )
		header << ( __header('Content-Location') || '' )
		header << ( __header('Content-MD5') || '' )
		header << ( __header('Content-Range') || '' )
		header << __content_type
		header << ( __header('Content-Disposition') || '' )
		header << ( __header('Expires') || '' )
		header << ( __header('Last-Modified') || '' )
	end

	def __header( header )
		"#{header}: #{self.headers[header]}#{EOL}" if self.headers[header]
	end

 	def __content_type
		header = "Content-Type: #{self.headers['Content-Type']}"
		header << "; charset=#{self.encoding}" if self.encoding
		header << EOL
	end

	def __cookie
		return if @cookies.empty?

		header = ''
		@cookies.each { | cookie |
			header << "Set-Cookie: #{cookie.to_s}"
			header << EOL
		}
		header
	end

	public

	def status_line
		"#{http_version_line} #@status #{STATUS[@status]}#{EOL}"
	end

	# Sends a temporary redirect response to the client using the specified URL.
	def set_redirect( url )
		@status = 302
		@headers['Location'] = url
	end

	# Returns true if the response is setted redirect.
	def redirect?
		if ( @status == 302 ) or ( @status == 307 ) then
			true
		else
			false
		end
	end
end


# CKCookie is a class for cookie.
# To send cookies to a browser needs to create cookie objects 
# and set them to a response object. Instead of creating cookie objects,
# you can also get cookie objects from a request object.
#
# CKCookie objects have a pair of a cookie name and value.
# If you make the objects have multiple values for one name, 
# you must write code by yourself. 
# 
# == Controlling cookie objects
#
# === Creating cookies
# Give arguments of initialize() a name or a pair of name/value.
# The value of cookie is omittable.
#
#  cookie = CKCookie.new( name, value )
#
# === Getting cookies from a request object
# CKRequest has some methods for getting cookies.
# The methods are cookie(key), cookies, cookie_value(key), cookie_values(key).
# See also CKRequest.
#
# === Setting cookies to a response object
# CKResponse has methods for setting cookies. These methods are 
# defined in CKMessage, the superclass of CKResponse.
# Use add_cookie(cookie) and remove_cookie(cookie).
class CKCookie
	# Name of the cookie.
	attr_accessor :name

	# Value of the cookie.
	attr_accessor :value

	# Restricts the cookie in the site.
	attr_accessor :path

	# Domain that can receive the cookie.
	attr_accessor :domain

	# Expiry date. You set Time object to the cookie object.
	# The value is formatted when the cookie is returned.
	attr_accessor :expires

	# Decides whether the cookie is encrypted or not.
	attr_accessor :secure

	class << self
		# Parse raw cookie string and return an array of cookies.
 		def parse_raw_cookie( raw_cookie )
			cookies = []
			return cookies unless raw_cookie

			raw_cookie.split('; ').each do |pairs|
				name, value = pairs.split('=',2)
				name  = CKUtilities.unescape_url name
				value = CKUtilities.unescape_url value

				cookies << CKCookie.new( name, value )
			end

			cookies
		end
	end

	def initialize( name, value = nil, domain = nil, path = nil, secure = false )
		@name   = name
		@value  = value
		@domain = domain
		@path   = path
		@secure = secure
	end

	def to_s
		buf = "#@name="
		if @value   then buf << CKUtilities.escape_url(@value.to_s) end
		if @domain  then buf << "; domain=#@domain" end
		if @path	   then buf << "; path=#@path" end
		if @expires then buf << "; expires=#{CKUtilities.date(@expires)}" end
		if @secure == true then buf << '; secure' end
		buf
	end
end


# CKUtilitis is a module wihch collects utility methods based on cgi.rb.
module CKUtilities
	CR            = "\015"
	LF            = "\012"
	EOL           = CR + LF
	RFC822_DAYS   = %w[ Sun Mon Tue Wed Thu Fri Sat ]
	RFC822_MONTHS = %w[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ]

	# Returns an encoded string for URL.
	def escape_url( string )
		string.to_s.gsub( /([^ a-zA-Z0-9_.-]+)/n ) do
			'%' + $1.unpack( 'H2' * $1.size ).join( '%' ).upcase
		end.tr( ' ', '+' )
	end

	# Returns a string decoded from URL.
	def unescape_url( string )
		string.to_s.tr( '+', ' ' ).gsub( /((?:%[0-9a-fA-F]{2})+)/n ) do
			[ $1.delete( '%' ) ].pack( 'H*' )
		end
	end

	# Escapes HTML control characters.
	def escape_html( string )
		string.to_s.gsub(/&/n, '&amp;').
			gsub(/\"/n, '&quot;').
			gsub(/>/n, '&gt;').
			gsub(/</n, '&lt;').
			gsub(/'/n, '&#39;')
	end

	# Unescapes HTML control characters.
	def unescape_html( string )
  		string.to_s.gsub(/&(.*?);/n) do
	    	match = $1.dup
	    	case match
    		when /\Aamp\z/ni           then '&'
			when /\Aquot\z/ni          then '"'
			when /\Agt\z/ni            then '>'
			when /\Alt\z/ni            then '<'
			when /\A#0*(\d+)\z/n       then
				if Integer($1) < 256
					Integer($1).chr
				else
					if Integer($1) < 65536 and \
						($KCODE[0] == ?u or $KCODE[0] == ?U)
						[Integer($1)].pack("U")
					else
						"&##{$1};"
					end
				end
			when /\A#x([0-9a-f]+)\z/ni then
				if $1.hex < 256
					$1.hex.chr
				else
					if $1.hex < 65536 and \
						($KCODE[0] == ?u or $KCODE[0] == ?U)
						[$1.hex].pack("U")
					else
						"&#x#{$1};"
					end
				end
			else
				"&#{match};"
			end
		end
	end

	# Formats Time object in RFC1123.
	# For example, "Sat, 1 Jan 2000 00:00:00 GMT".
	def date( time )
		t = time.clone.gmtime
		return format("%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
			RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
			t.hour, t.min, t.sec)
	end

	module_function :escape_url
	module_function :unescape_url
	module_function :escape_html
	module_function :unescape_html
	module_function :date
end


# CKLog is a simple logging class.
#
# == Debug level
# DEBUG < INFO < WARN < ERROR < FATAL
class CKLog

	DEBUG          = 1
	INFO           = 2
	WARN           = 3
	ERROR          = 4
	FATAL          = 5
	DEFAULT_LEVEL  = DEBUG

	# Log level. The default level is DEBUG.
	attr_accessor :level

	# Outputter. The default outputter is standard error.
	attr_accessor :out

	# Program name. The default name is "CGIKit" and the version.
	attr_accessor :name

	# Max file size to log. If size of file to output is over the max file size,
	# exception FileSizeError is raised.
	attr_accessor :max_file_size

	class FileSizeError < CKError; end #:nodoc:

	class << self
		# Outputs a debug string to STDERR.
		# A backtrace is sent if "exception" is given an error object.
		def debug( message, exception = nil )
			message = "[#{Time.now}] DEBUG - #{CKApplication.version}: #{message}\n"
			if exception then
				message << exception.backtrace.join << "\n"
			end
			$stderr.write message
		end
	end

	def initialize( options = {} )
		@level         = options['level']  || DEFAULT_LEVEL
		@name          = options['name']   || CKApplication.version
		@max_file_size = options['max_file_size']
		@file          = options['file']
		@out           = options['out'] || $stderr
	end

	def add( level, message = nil, &block )
		if message.nil? and block_given? then
			message = yield
		end

		message = _format_message( level, @name, message )

		if @file then
			if _max_file_size? message then
				raise FileSizeError, "#@file: Can't write log. The file size is limit."
			else
				CKFileLock.exclusive_lock(@file, 'a') do |f|
					f.write message
				end
			end
		else
			@out.write message
		end
	end

	private
	def _format_message( level, name, message )
		case level
		when DEBUG then level_s = 'DEBUG'
		when INFO  then level_s = ' INFO'
		when WARN  then level_s = ' WARN'
		when ERROR then level_s = 'ERROR'
		when FATAL then level_s = 'FATAL'
		end

		"[#{Time.now}] #{level_s} - #{name}: #{message}\n"
	end

	def _max_file_size?( message )
		if @max_file_size and FileTest.exist?(@file) then
			@max_file_size <= (File.size(@file) + message.size)
		end
	end

	public
	def close
		@out.close
	end

	def debug( message = nil, &block )
		add( DEBUG, message, &block ) if debug?
	end

	def info( message = nil, &block )
		add( INFO, message, &block ) if info?
	end

	def warn( message = nil, &block )
		add( WARN, message, &block ) if warn?
	end

	def error( message = nil, &block )
		add( ERROR, message, &block ) if error?
	end

	def fatal( message = nil, &block )
		add( FATAL, message, &block ) if fatal?
	end

	def debug?
		@level <= DEBUG
	end

	def info?
		@level <= INFO
	end

	def warn?
		@level <= WARN
	end

	def error?
		@level <= ERROR
	end

	def fatal?
		@level <= FATAL
	end

end


# CKFileLock is for locking files.
class CKFileLock
	# Creates a shared file lock on a file.
	def self.shared_lock( filename, mode = 'r' )
		File.open( filename, mode ) do | io |
			io.flock File::LOCK_SH
			yield io
			io.flock File::LOCK_UN
		end
	end

	# Creates a exclusive file lock on a file.
	def self.exclusive_lock( filename, mode = 'w' )
		File.open( filename, mode ) do | io |
			io.flock File::LOCK_EX
			yield io
			io.flock File::LOCK_UN
		end
	end
end


# CKByteData objects manage bytes.
class CKByteData
	# Path of a file saving the bytes.
	attr_accessor :path

	# Content type of the bytes.
	attr_accessor :content_type

	class << self
		# Create an instance from specified IO object.
		# If you give this a File object, sets "path" attribute of the instance.
		def new_with_io( io, offset = nil, count = nil )
			io.pos = offset if offset
			bytes  = new io.read(count)
			if io.respond_to? 'path' then
				bytes.path = io.path
			end
			bytes
		end

		# Create an instance from specified file.
		def new_with_file( filename )
			f          = File.open filename
			bytes      = new f.read
			bytes.path = f.path
			f.close
			bytes
		end
	end

	public

	def initialize( string = nil )
		@bytes = string || ''
	end

	# Returns bytes with spcified length or whole length if you omit it.
	def bytes( length = nil )
		if length then
			@bytes.slice( 0, length )
		else
			@bytes.to_s
		end
	end

	# Executes the block for every byte.
	def each
		byte = nil
		@bytes.each_byte do | byte |
			yield byte
		end
	end

	# Returns true if the bytes of each objects are equal.
	def ==( bytes )
		if @bytes == bytes.bytes then
			true
		else
			false
		end
	end

	# Returns length of the bytes.
	def length
		@bytes.size
	end

	# Appends bytes to the bytes.
	def <<( bytes )
		if bytes.is_a? CKByteData then
			@bytes << bytes.bytes
		else
			@bytes << bytes
		end
	end

	# Writes the bytes to a specified file.
	def write_to_file( filename, lock = true )
		if lock then
			CKFileLock.exclusive_lock(filename, 'w+') do |file|
				file.write to_s
			end
		else
			File.open(filename, 'w+') do |file|
				file.write to_s
			end
		end			
	end

	# Returns the object as a string.
	def to_s
		@bytes.to_s
	end
end


# CKResourceManager class manages resources of an application.
class CKResourceManager
	MIME = {
		'ez'      => 'application/andrew-inset',
		'hqx'     => 'application/mac-binhex40',
		'cpt'     => 'application/mac-compactpro',
		'doc'     => 'application/msword',
		'bin'     => 'application/octet-stream',
		'dms'     => 'application/octet-stream',
		'lha'     => 'application/octet-stream',
		'lzh'     => 'application/octet-stream',
		'exe'     => 'application/octet-stream',
		'class'   => 'application/octet-stream',
		'so'      => 'application/octet-stream',
		'dll'     => 'application/octet-stream',
		'oda'     => 'application/oda',
		'pdf'     => 'application/pdf',
		'ai'      => 'application/postscript',
		'eps'     => 'application/postscript',
		'ps'      => 'application/postscript',
		'smi'     => 'application/smil',
		'smil'    => 'application/smil',
		'mif'     => 'application/vnd.mif',
		'xls'     => 'application/vnd.ms-excel',
		'ppt'     => 'application/vnd.ms-powerpoint',
		'wbxml'   => 'application/vnd.wap.wbxml',
		'wmlc'    => 'application/vnd.wap.wmlc',
		'wmlsc'   => 'application/vnd.wap.wmlscriptc',
		'bcpio'   => 'application/x-bcpio',
		'vcd'     => 'application/x-cdlink',
		'pgn'     => 'application/x-chess-pgn',
		'cpio'    => 'application/x-cpio',
		'csh'     => 'application/x-csh',
		'dcr'     => 'application/x-director',
		'dir'     => 'application/x-director',
		'dxr'     => 'application/x-director',
		'dvi'     => 'application/x-dvi',
		'spl'     => 'application/x-futuresplash',
		'gtar'    => 'application/x-gtar',
		'hdf'     => 'application/x-hdf',
		'js'      => 'application/x-javascript',
		'skp'     => 'application/x-koan',
		'skd'     => 'application/x-koan',
		'skt'     => 'application/x-koan',
		'skm'     => 'application/x-koan',
		'latex'   => 'application/x-latex',
		'nc'      => 'application/x-netcdf',
		'cdf'     => 'application/x-netcdf',
		'sh'      => 'application/x-sh',
		'shar'    => 'application/x-shar',
		'swf'     => 'application/x-shockwave-flash',
		'sit'     => 'application/x-stuffit',
		'sv4cpio' => 'application/x-sv4cpio',
		'sv4crc'  => 'application/x-sv4crc',
		'tar'     => 'application/x-tar',
		'tcl'     => 'application/x-tcl',
		'tex'     => 'application/x-tex',
		'texinfo' => 'application/x-texinfo',
		'texi'    => 'application/x-texinfo',
		't'       => 'application/x-troff',
		'tr'      => 'application/x-troff',
		'roff'    => 'application/x-troff',
		'man'     => 'application/x-troff-man',
		'me'      => 'application/x-troff-me',
		'ms'      => 'application/x-troff-ms',
		'ustar'   => 'application/x-ustar',
		'src'     => 'application/x-wais-source',
		'xhtml'   => 'application/xhtml+xml',
		'xht'     => 'application/xhtml+xml',
		'zip'     => 'application/zip',
		'au'      => 'audio/basic',
		'snd'     => 'audio/basic',
		'mid'     => 'audio/midi',
		'midi'    => 'audio/midi',
		'kar'     => 'audio/midi',
		'mpga'    => 'audio/mpeg',
		'mp2'     => 'audio/mpeg',
		'mp3'     => 'audio/mpeg',
		'aif'     => 'audio/x-aiff',
		'aiff'    => 'audio/x-aiff',
		'aifc'    => 'audio/x-aiff',
		'm3u'     => 'audio/x-mpegurl',
		'ram'     => 'audio/x-pn-realaudio',
		'rm'      => 'audio/x-pn-realaudio',
		'rpm'     => 'audio/x-pn-realaudio-plugin',
		'ra'      => 'audio/x-realaudio',
		'wav'     => 'audio/x-wav',
		'pdb'     => 'chemical/x-pdb',
		'xyz'     => 'chemical/x-xyz',
		'bmp'     => 'image/bmp',
		'gif'     => 'image/gif',
		'ief'     => 'image/ief',
		'jpeg'    => 'image/jpeg',
		'jpg'     => 'image/jpeg',
		'jpe'     => 'image/jpeg',
		'png'     => 'image/png',
		'tiff'    => 'image/tiff',
		'tif'     => 'image/tiff',
		'djvu'    => 'image/vnd.djvu',
		'djv'     => 'image/vnd.djvu',
		'wbmp'    => 'image/vnd.wap.wbmp',
		'ras'     => 'image/x-cmu-raster',
		'pnm'     => 'image/x-portable-anymap',
		'pbm'     => 'image/x-portable-bitmap',
		'pgm'     => 'image/x-portable-graymap',
		'ppm'     => 'image/x-portable-pixmap',
		'rgb'     => 'image/x-rgb',
		'xbm'     => 'image/x-xbitmap',
		'xpm'     => 'image/x-xpixmap',
		'xwd'     => 'image/x-xwindowdump',
		'igs'     => 'model/iges',
		'iges'    => 'model/iges',
		'msh'     => 'model/mesh',
		'mesh'    => 'model/mesh',
		'silo'    => 'model/mesh',
		'wrl'     => 'model/vrml',
		'vrml'    => 'model/vrml',
		'css'     => 'text/css',
		'html'    => 'text/html',
		'htm'     => 'text/html',
		'asc'     => 'text/plain',
		'txt'     => 'text/plain',
		'rtx'     => 'text/richtext',
		'rtf'     => 'text/rtf',
		'sgml'    => 'text/sgml',
		'sgm'     => 'text/sgml',
		'tsv'     => 'text/tab-separated-values',
		'wml'     => 'text/vnd.wap.wml',
		'wmls'    => 'text/vnd.wap.wmlscript',
		'etx'     => 'text/x-setext',
		'xml'     => 'text/xml',
		'xsl'     => 'text/xml',
		'mpeg'    => 'video/mpeg',
		'mpg'     => 'video/mpeg',
		'mpe'     => 'video/mpeg',
		'qt'      => 'video/quicktime',
		'mov'     => 'video/quicktime',
		'mxu'     => 'video/vnd.mpegurl',
		'avi'     => 'video/x-msvideo',
		'movie'   => 'video/x-sgi-movie',
		'ice'     => 'x-conference/x-cooltalk'
	}

	def initialize( application )
		@application          = application
		@resources            = @application.resources
		@web_server_resources = @application.web_server_resources
		@document_root        = @application.document_root
	end

	# Returns the public URL for the specified resource
	# when it is under the web server resources directory.
	# Otherwise returns nil.
	def url( name )
		if filepath = path(name) then
			absolute = File.expand_path @web_server_resources
			if filepath =~ /^#{absolute}/ then
				url = filepath.sub(@document_root, '')
				return url
			end
		end

		nil
	end

	# Returns the file path of the specified resource.
	def path( name )
		resourcedir = [@resources, @web_server_resources]

		resourcedir.each do |dir|
			filepath = File.join(dir, name).untaint
			if FileTest.exist? filepath then
				return File.expand_path(filepath).untaint
			end
		end
		nil
	end

	# Returns a CKByteData object for the specified resource.
	def bytedata( name )
		if filepath = path(name) then
			data = CKByteData.new_with_file filepath
			data.content_type = content_type filepath
			data
		else
			nil
		end
	end

	# Finds the content type for extension of the specified path.
	# If the path don't have extension, returns nil.
	def content_type( path )
		# for ruby 1.6
		base = File.basename path
		type = nil
		if base =~ /\.([^\.]+)$/ then
			type = MIME[$1]
		end
		type
	end
end


require 'set'

class CKQualifierParser
	class ParseError < StandardError; end #:nodoc:

	attr_reader :qualifier

	def initialize( format )
		tokens     = _analyze format.dup
		@qualifier = _parse tokens
	end

	private

	def _analyze( format )
		format    = "(#{format})"
		scanner   = StringScanner.new format
		qualifier = nil
		tokens    = []
		op_reg    = /\A(==|!=|>=|<=|>|<|=~)/im

		until scanner.eos? do
			scanner.skip /\A[\s]+/

			if str = scanner.scan(/\A(\(|\))/) then
				tokens << str
			elsif str = scanner.scan(op_reg) then
				tokens << CKQualifier.operator_symbol(str)
			elsif str = scanner.scan(/\A\d+\.\d+/) then
				tokens << str.to_f
			elsif str = scanner.scan(/\A\d+/) then
				tokens << str.to_i
			elsif scanner.match?(/\Atrue\W/) then
				scanner.scan /\Atrue/
				tokens << true
			elsif scanner.match?(/\Afalse\W/) then
				scanner.scan /\Afalse/
				tokens << false
			elsif scanner.match?(/\Anil\W/) then
				scanner.scan /\Anil/
				tokens << nil
			elsif scanner.scan(/\A\//) then
				tokens << _parse_regexp(scanner)
			elsif str = scanner.scan(/\A'(([^'\\]|\\.)*)'/) then
				tokens << scanner[0]
			elsif str = scanner.scan(/\A"(([^"\\]|\\.)*)"/) then
				tokens << scanner[0]
			else
				str = scanner.scan /\A[^\s\(\)]+/
				tokens << str
			end
		end

		tokens
	end

	def _parse_regexp( scanner )
		regstr = ''
		char = nil
		pre_char = nil
		option = nil

		until scanner.eos? do
			char = scanner.getch

			if (pre_char != '\\') and (char == '/') then
				option = scanner.scan(/\A\w+/)
				break
			end

			regstr << char
			pre_char = char
		end

		Regexp.new(regstr, option)
	end

	def _parse( tokens )
		op_stack  = []
		out_stack = []
		op = left = right = q = nil

		reg_and = /\Aand\Z/mi
		reg_or  = /\Aor\Z/mi
		reg_not = /\Anot\Z/mi

		tokens.each do |token|
			case token
			when '('
				op_stack << token
			when ')'
				until op_stack.last == '(' do
					op    = op_stack.pop
					right = out_stack.pop
					left  = out_stack.pop

					case op
					when Symbol
						if Regexp === right then
							q = CKKeyValueQualifier.new(left, op, right)
						elsif right =~ /\A'(([^'\\]|\\.)*)'/ then
							q = CKKeyValueQualifier.new(left, op, $1)
						elsif right =~ /\A"(([^"\\]|\\.)*)"/ then
							q = CKKeyValueQualifier.new(left, op, $1)
						elsif (Numeric === right) or (right == true) or \
							(right == false) or right.nil? then
							q = CKKeyValueQualifier.new(left, op, right)
						else
							q = CKKeyComparisonQualifier.new(left, op, right)
						end
					when reg_and
						if CKAndQualifier === right then
							right.qualifiers.unshift left
							q = right
						else
							q = CKAndQualifier.new [left, right]
						end
					when reg_or
						if CKOrQualifier === right then
							right.qualifiers.unshift left
							q = right
						else
							q = CKOrQualifier.new [left, right]
						end
					when reg_not
						q = CKNotQualifier.new right
					end
					out_stack << q
				end
				op_stack.pop		
			when reg_and
				op_stack << token
			when reg_or
				op_stack << token
			when reg_not
				op_stack << token
			when Symbol
				op_stack << token
			else
				out_stack << token
			end
		end

		result = out_stack.pop
		unless out_stack.empty? and op_stack.empty? then
			raise ParseError, 'parse error'
		end

		result
	end
end


class CKQualifier
	module ComparisonSupport
		def coerce( left, right )
			coerced = left

			case right
			when String
				coerced = left.to_s
			when Integer
				coerced = left.to_i
			when Float
				coerced = left.to_f
			when NilClass
				if left == '' then
					coerced = nil
				end
			when Regexp
				coerced = left.to_s
			end

			coerced
		end

		def compare( left, right, symbol )
			coerced = coerce(left, right)
			__send__(symbol, coerced, right)
		end

		def equal?( left, right )
			left == right
		end

		def not_equal?( left, right )
			left != right
		end

		def greater?( left, right )
			left > right
		end

		def greater_or_equal?( left, right )
			left >= right
		end

		def less?( left, right )
			left < right
		end

		def less_or_equal?( left, right )
			left <= right
		end

		def match?( left, right )
			if left =~ right then
				true
			else
				false
			end
		end
	end

	class UnknownKeyError < StandardError; end #:nodoc:

	extend ComparisonSupport

	EQUAL            = :'equal?'
	NOT_EQUAL        = :'not_equal?'
	GREATER          = :'greater?'
	GREATER_OR_EQUAL = :'greater_or_equal?'
	LESS             = :'less?'
	LESS_OR_EQUAL    = :'less_or_equal?'
	MATCH            = :'match?'

	class << self
		OPERATORS   = ['==', '!=', '<', '<=', '>', '>=', '=~']

		def new_with_format( format )
			parser = CKQualifierParser.new(format)
			parser.qualifier
		end

		alias format new_with_format

		def operator_symbol( string )
			case string.upcase
			when '==' then EQUAL
			when '!=' then NOT_EQUAL
			when '>'  then GREATER
			when '>=' then GREATER_OR_EQUAL
			when '<'  then LESS
			when '<=' then LESS_OR_EQUAL
			when '=~' then MATCH
			end
		end

		def operator_string( operator )
			case operator
			when EQUAL            then '=='
			when NOT_EQUAL        then '!='
			when GREATER          then '>'
			when GREATER_OR_EQUAL then '>='
			when LESS             then '<'
			when LESS_OR_EQUAL    then '<='
			when MATCH            then '=~'
			end
		end

		def operators
			OPERATORS
		end

	end

	def qualifier_keys
		set = Set.new
		add_qualifier_keys set
		set
	end

	# abstract - subclasses must override it
	def add_qualifier_keys( set ); end

	alias inspect to_s
end


class CKKeyValueQualifier < CKQualifier
	attr_reader :key, :value, :symbol

	def initialize( key, symbol, value )
		super()
		@key = key
		@symbol = symbol
		@value = value
	end

	def add_qualifier_keys( set )
		set << @key
	end

	def ==( other )
		bool = false
		if CKKeyValueQualifier === other then
			if (@key == other.key) and (@symbol == other.symbol) and \
				(@value == other.value) then
				bool = true
			end
		end

		bool
	end

	def eval?( object )
		CKQualifier.compare(object[@key], @value, @symbol)
	end

	public

	def to_s
		op = CKQualifier.operator_string @symbol
		if String === @value then
			value_s = "'#@value'"
		else
			value_s = @value
		end

		"(#@key #{op} #{value_s})"
	end
end


class CKKeyComparisonQualifier < CKQualifier
	attr_reader :left, :symbol, :right

	def initialize( left, symbol, right )
		super()
		@left   = left
		@symbol = symbol
		@right  = right
	end

	def add_qualifier_keys( set )
		set << @left
	end

	def ==( other )
		bool = false
		if CKKeyComparisonQualifier === other then
			if (@left == other.left) and (@symbol == other.symbol) and \
				(@right == other.right) then
				bool = true
			end
		end

		bool
	end

	def eval?( object )
		CKQualifier.compare(object[@left], object[@right], @symbol)
	end

	def to_s
		op = CKQualifier.operator_string @symbol
		"(#@left #{op} #@right)"
	end
end


class CKAndQualifier < CKQualifier
	attr_reader :qualifiers

	def initialize( qualifiers )
		super()
		@qualifiers = qualifiers
	end

	def each
		qualifiers.each do |qualifier|
			yield qualifier
		end
	end

	def add_qualifier_keys( set )
		@qualifiers.each do |qualifier|
			qualifier.add_qualifier_keys set
		end
	end

	def ==( other )
		bool = false
		if CKAndQualifier === other then
			if @qualifiers == other.qualifiers then
				bool = true
			end
		end

		bool
	end

	def eval?( object )
		@qualifiers.each do |qualifier|
			unless qualifier.eval? object then
				return false
			end
		end
		true
	end

	def to_s
		str = '('
		@qualifiers.each do |q|
			str << q.to_s
			unless @qualifiers.last == q then
				str << " AND "
			end
		end
		str << ')'
		str
	end
end


class CKOrQualifier < CKQualifier
	attr_reader :qualifiers

	def initialize( qualifiers )
		super()
		@qualifiers = qualifiers
	end

	def each
		qualifiers.each do |qualifier|
			yield qualifier
		end
	end

	def add_qualifier_keys( set )
		@qualifiers.each do |qualifier|
			qualifier.add_qualifier_keys set
		end
	end

	def ==( other )
		bool = false
		if CKOrQualifier === other then
			if @qualifiers == other.qualifiers then
				bool = true
			end
		end

		bool
	end

	def eval?( object )
		@qualifiers.each do |qualifier|
			if qualifier.eval? object then
				return true
			end
		end
		false
	end

	def to_s
		str = '('
		@qualifiers.each do |q|
			str << q.to_s
			unless @qualifiers.last == q then
				str << " OR "
			end
		end
		str << ')'
		str
	end
end


class CKNotQualifier < CKQualifier
	attr_reader :qualifier

	def initialize( qualifier )
		super()
		@qualifier = qualifier
	end

	def add_qualifier_keys( set )
		qualifier.add_qualifier_keys set
	end

	def ==( other )
		bool = false
		if CKNotQualifier === other then
			if @qualifier == other.qualifier then
				bool = true
			end
		end

		bool
	end

	def eval?( object )
		unless @qualifier.eval? object then
			true
		else
			false
		end
	end

	def to_s
		"(NOT #{qualifier})"
	end
end