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
|
unit idehtml2class;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, sax, sax_html, fpjson, pascodegen;
Type
TSpecialMethod = (smConstructor,smBindElements,smBindElementEvents);
TSpecialMethods = Set of TSpecialMethod;
TFormOption = (foEvents,foFormFile,foBindInConstructor);
TFormOptions = Set of TFormOption;
{ THTML2ClassOptions }
THTML2ClassOptions = Class (TPersistent)
Private
FExcludeElements: TStrings;
FFormOptions: TFormOptions;
FMethods : Array[1..3] of TSpecialMethods;
FBools : Array[1..2] of Boolean;
FStrings : Array[1..10] of String;
function GetB(AIndex: Integer): Boolean;
function GetMethods(AIndex: Integer): TSpecialMethods;
function GetS(AIndex: Integer): String;
procedure SetB(AIndex: Integer; AValue: Boolean);
procedure SetExcludeElements(AValue: TStrings);
procedure SetMethods(AIndex: Integer; AValue: TSpecialMethods);
procedure SetS(AIndex: Integer; AValue: String);
Public
Constructor Create;
Destructor Destroy; override;
Procedure Reset; virtual;
Procedure toJSON(aObject : TJSONObject);
Procedure FromJSON(aJSON : String);
Procedure FromJSON(aObject : TJSONObject);
Function asJSON(Formatted : Boolean) : String;
Property OverrideMethods : TSpecialMethods index 1 Read GetMethods Write SetMethods;
Property AddMethods : TSpecialMethods index 2 Read GetMethods Write SetMethods;
Property VirtualMethods : TSpecialMethods index 3 Read GetMethods Write SetMethods;
Property FormOptions : TFormOptions Read FFormOptions Write FFormOptions;
Property ParentClassName : String Index 1 Read GetS Write SetS;
Property GetElementFunction : String Index 2 Read GetS Write SetS;
Property EventSignature : String Index 3 Read GetS Write SetS;
Property EventModifiers : String Index 4 Read GetS Write SetS;
Property ConstructorArgs : String Index 5 Read GetS Write SetS;
Property BelowID : String Index 6 Read GetS Write SetS;
Property HTMLFileName : String Index 7 Read GetS Write SetS;
Property TagMapFileName : String Index 8 Read GetS Write SetS;
Property FormClassname : String Index 9 Read GetS Write SetS;
Property ExtraUnits: String index 10 Read GetS Write SetS;
Property UseDefaultElements : Boolean Index 1 Read GetB Write SetB;
Property AddHTMLToProject : Boolean Index 2 Read GetB Write SetB;
Property ExcludeElements : TStrings Read FExcludeElements Write SetExcludeElements;
end;
Type
TLogEvent = Procedure (Sender : TObject; Const Msg : String) of object;
{ TFormElement }
TFormElement = Class(TCollectionItem)
private
FHTMLID: String;
FName: String;
FType: String;
FEvents : TStrings;
function GetEvents: TStrings;
function getName: String;
procedure SetEvents(AValue: TStrings);
Public
Destructor Destroy; override;
Function HasEvents : Boolean;
Procedure Assign(Source : TPersistent); override;
Published
Property Name : String Read getName Write FName;
Property HTMLID : String Read FHTMLID Write FHTMLID;
Property ElementType : String Read FType Write FType;
Property Events : TStrings Read GetEvents Write SetEvents;
end;
{ TFormElementList }
TFormElementList = CLass(TCollection)
private
function GetEl(aIndex : Integer): TFormElement;
Public
Function Add(Const aName : string) : TFormElement;
Function IndexOf(Const aName : string) : Integer;
Function Find(Const aName : string) : TFormElement;
Property Elements[aIndex : Integer] : TFormElement Read GetEl; default;
end;
TAttributeOperation = (aoNotPresent,aoPresent,aoEqual,aoNotEqual,aoContains);
{ TAttributeCondition }
TAttributeCondition = Class(TCollectionItem)
private
FAttribute: String;
FOperation: TAttributeOperation;
FValue: String;
Public
Procedure LoadFromJSON(aName : String; aValue: TJSONData);
function IsMatch(aValue: String): Boolean;
Property Attribute : String Read FAttribute Write FAttribute;
Property Operation : TAttributeOperation Read FOperation Write FOperation;
Property Value : String Read FValue Write FValue;
end;
{ TAttributeConditionList }
TAttributeConditionList = Class(TCollection)
private
function GetC(aIndex : Integer): TAttributeCondition;
Public
Procedure LoadFromJSON(aJSON : TJSONObject);
Function IsMatch(Attrs: TSAXAttributes): Boolean;
Property Conditions[aIndex : Integer] : TAttributeCondition Read GetC; default;
end;
(* // Structure of accepted JSON
[
{
"class" : "TWebComboBox",
"tag" : "input",
"attrs" : {
name0 : null, // name0 Not present
name1 : "value", // name1 equals value
name2 ; "-value", // name2 does not equal value
name3 : "~value" // name3 contains value
}
}
]
*)
{ THTMLElementMap }
THTMLElementMap = Class(TCollectionItem)
private
FConditionList : TAttributeConditionList;
FControlClass: String;
FTag: String;
function GetAttrConditionList: TAttributeConditionList;
Protected
Function CreateConditionList : TAttributeConditionList; virtual;
Public
Destructor Destroy; override;
Procedure LoadFromJSON(aJSON : TJSONObject);
Function HasConditions : Boolean;
Function IsMatch(aTag: SAXString; Attrs: TSAXAttributes): Boolean;
Property Tag : String Read FTag Write FTag;
Property ControlClass : String Read FControlClass Write FControlClass;
Property Attributes : TAttributeConditionList Read GetAttrConditionList;
end;
{ THTMLElementMapList }
THTMLElementMapList = Class(TCollection)
private
function GetM(aIndex : Integer): THTMLElementMap;
Public
Procedure LoadFromFile(Const aFileName : String);
Procedure LoadFromStream(aStream : TStream); virtual;
Procedure LoadFromJSON(aJSON : TJSONArray); virtual;
Function IndexOfMap(aTag: SAXString; Attrs: TSAXAttributes): Integer;
Function FindMap(aTag: SAXString; Attrs: TSAXAttributes): THTMLElementMap;
Property Maps[aIndex : Integer] : THTMLElementMap Read GetM; default;
end;
{ THTMLToFormElements }
THTMLToFormElements = class(TComponent)
private
FBelowID: String;
FDefaultElements: Boolean;
FExcludeIDS: TStrings;
FFormElements: TFormElementList;
FLevel : Integer;
FMap: THTMLElementMapList;
FOnLog: TLogEvent;
function MakeValidName(aID: string): string;
procedure SetExcludeIDS(AValue: TStrings);
procedure SetFormElements(AValue: TFormElementList);
protected
Procedure DoLog(Const Msg : String);
Procedure DoLog(Const Fmt : String; Args : Array of const);
function CreateHTMLElementMapList: THTMLElementMapList; virtual;
procedure GetEvents(aEl: TFormElement; Atts: TSAXAttributes); virtual;
procedure DoEndElement(Sender: TObject; const {%H-}NamespaceURI, {%H-}LocalName,
{%H-}QName: SAXString); virtual;
procedure DoStartElement(Sender: TObject; const {%H-}NamespaceURI, LocalName,
{%H-}QName: SAXString; Atts: TSAXAttributes); virtual;
function Maptype(aTag: SAXString; Atts: TSAXAttributes): String; virtual;
Class Function CreateElementList : TFormElementList; virtual;
Property Level : Integer Read FLevel Write FLevel;
Public
Constructor Create(aOwner : TComponent); override;
Destructor Destroy; override;
Procedure Clear;
Procedure LoadFromStream(aInput : TStream);
Procedure LoadFromFile(Const aFileName : String);
Procedure LoadOptions(aOptions : THTML2ClassOptions);
Property FormElements : TFormElementList Read FFormElements Write SetFormElements;
Property BelowID : String Read FBelowID Write FBelowID;
Property ExcludeIDS : TStrings Read FExcludeIDS Write SetExcludeIDS;
Property Map : THTMLElementMapList Read FMap;
Property DefaultElements : Boolean Read FDefaultElements Write FDefaultElements;
Property OnLog : TLogEvent Read FOnLog Write FOnLog;
end;
{ THTMLExtractIDS }
TExtractOption = (eoExtraInfo // Add info object with Tag. In stringarray, emit ID=Info.ToString
);
{ TTagInfo - attached to string in objects }
TTagInfo = Class(TObject)
private
FInputName: String;
FInputType: String;
FTag: String;
Public
Constructor Create(Const aTag,aType,aName : String);
Function ToString : String; override;
Property TagName : String Read FTag Write FTag;
Property InputType : String Read FInputType Write FInputType;
Property InputName : String Read FInputName Write FInputName;
end;
{ TTagInfoItem }
TTagInfoItem = Class(TCollectionItem)
private
FElementID: UTF8String;
FInputName: UTF8String;
FInputType: UTF8String;
FTagName: UTF8String;
Public
Procedure Assign(aSource : TPersistent); override;
Function ToString : String; override;
Property ElementID : UTF8String Read FElementID Write FElementID;
Property TagName : UTF8String Read FTagName Write FTagName;
Property InputType : UTF8String Read FInputType Write FInputType;
Property InputName : UTF8String Read FInputName Write FInputName;
end;
{ TTagInfoList }
TTagInfoList = class(TCollection)
private
function GetTag(aIndex : Integer): TTagInfoItem;
procedure SetTag(aIndex : Integer; AValue: TTagInfoItem);
Public
function AddTagItem(const aElementID, aTag, aType, aName: String): TTagInfoItem;
Function IndexOfID(const aElementID : String) : Integer;
Function FindByID(const aElementID : String) : TTagInfoItem;
Property Tags [aIndex : Integer] : TTagInfoItem Read GetTag Write SetTag; default;
end;
TExtractOptions = set of TExtractOption;
THTMLExtractIDS = Class(TComponent)
Private
FBelowID: String;
FLevel: Integer;
FList: TTagInfoList;
FOptions: TExtractOptions;
Protected
procedure DoStartElement(Sender: TObject; const {%H-}NamespaceURI, {%H-}LocalName,
{%H-}QName: SAXString; Atts: TSAXAttributes); virtual;
procedure DoEndElement(Sender: TObject; const {%H-}NamespaceURI, {%H-}LocalName,
{%H-}QName: SAXString); virtual;
Property List : TTagInfoList Read FList;
Property Level : Integer Read FLevel Write FLevel;
Public
Procedure ExtractIDS(aInput : TStream; aList : TTagInfoList); overload;
Procedure ExtractIDS(aInput : TStream; aList : TStrings); overload;
Function ExtractIDS(aInput : TStream) : TStringArray; overload;
Procedure ExtractIDS(Const aFileName : String; aList : TTagInfoList); overload;
Procedure ExtractIDS(Const aFileName : String; aList : TStrings); overload;
function ExtractIDS(const aFileName: String): TStringArray; overload;
Property BelowID : String Read FBelowID Write FBelowID;
Property Options : TExtractOptions Read FOptions Write FOptions;
end;
{ TFormCodeGen }
{ TFormFileCodeGen }
TFormFileCodeGen = Class(TPascalCodeGenerator)
private
FElementHeight: Word;
FElementHSpacing: Word;
FElementVSpacing: Word;
FElementWidth: Word;
FDoEvents: Boolean;
FFormClassName: String;
FFormElements: TFormElementList;
FIDProperty: String;
FLeft: Word;
FMaxHeight: Word;
FMaxWidth: Word;
FTop: Word;
Protected
function GetFormName(const aClassName: string): String; virtual;
procedure GenerateElements; virtual;
procedure EmitElementEvents(El: TFormElement); virtual;
procedure EmitElementProps(El: TFormElement); virtual;
procedure NextPosition; virtual;
Property ELeft : Word Read FLeft Write FLeft;
Property ETop : Word Read FTop Write FTop;
Public
Constructor Create(aOwner : TComponent);override;
Procedure Execute;
Property FormElements: TFormElementList read FFormElements write FFormElements;
Property FormClassName : String read FFormClassName write FFormClassName;
Property DoEvents : Boolean read FDoEvents write FDoEvents;
Property IDProperty : String Read FIDProperty Write FIDProperty;
Property ElementHeight : Word Read FElementHeight Write FElementHeight;
Property ElementWidth : Word Read FElementWidth Write FElementWidth;
Property MaxWidth : Word Read FMaxWidth Write FMaxWidth;
Property MaxHeight : Word Read FMaxHeight Write FMaxHeight;
Property ElementHSpacing : Word Read FElementHSpacing Write FElementHSpacing;
Property ElementVSpacing : Word Read FElementVSpacing Write FElementVSpacing;
end;
TFormCodeGen = Class(TPascalCodeGenerator)
private
FAddMethods: TSpecialMethods;
FConstructorArgs: String;
FEventModifiers: String;
FEventSignature: string;
FFormClassName: string;
FFormElements: TFormElementList;
FFormFileGenerator: TFormFileCodeGen;
FFormSource: Tstrings;
FGetElementFunction: string;
FOptions: TFormOptions;
FOverrideMethods: TSpecialMethods;
FParentClassName: string;
FVirtualMethods: TSpecialMethods;
procedure SetFormElements(AValue: TFormElementList);
Protected
function BaseUnits : String; override;
Function CreateHTMLToFormELements: THTMLToFormElements; virtual;
Class Function CreateElementList : TFormElementList; virtual;
procedure EmitFormFile; virtual;
function CreateFormFileGen : TFormFileCodeGen; virtual;
procedure EmitFormElement(aEL: TFormElement); virtual;
procedure EmitFormEvents(aEL: TFormElement);virtual;
procedure EmitImplementation; virtual;
procedure EmitPublicSection; virtual;
procedure EmitPublishedSection; virtual;
procedure EmitFormBindElements; virtual;
procedure EmitFormBindEvents; virtual;
procedure EmitFormConstructor; virtual;
function VirtualOverride(M: TSpecialMethod; const Decl: String): string; virtual;
Public
Constructor Create(aOwner : TComponent); override;
Destructor Destroy; override;
class function Pretty(const S: String): string; virtual;
class procedure GetEventNameAndHandler(const S,aFieldName: String; out aName, aHandler: string);
Procedure Execute;
Procedure LoadOptions(aOptions : THTML2ClassOptions);
Property FormFileGenerator : TFormFileCodeGen Read fFormFileGenerator Write FFormFileGenerator;
Property FormElements : TFormElementList Read FFormElements Write SetFormElements;
Property FormClassName : string Read FFormClassName Write FFormClassName;
Property ParentClassName : string Read FParentClassName Write FParentClassName;
Property GetElementFunction : string Read FGetElementFunction Write FGetElementFunction;
Property EventSignature: string Read FEventSignature Write FEventSignature;
Property EventModifiers : String Read FEventModifiers Write FEventModifiers;
Property ConstructorArgs : String Read FConstructorArgs Write FConstructorArgs;
Property Options : TFormOptions Read FOptions Write FOptions;
Property AddMethods : TSpecialMethods Read FAddMethods Write FAddMethods;
Property OverrideMethods : TSpecialMethods Read FOverrideMethods Write FOverrideMethods;
Property VirtualMethods : TSpecialMethods Read FVirtualMethods Write FVirtualMethods;
Property FormSource : Tstrings Read FFormSource;
end;
implementation
uses TypInfo, bufstream;
{ TTagInfoList }
function TTagInfoList.GetTag(aIndex : Integer): TTagInfoItem;
begin
Result:=Items[aIndex] as TTagInfoItem;
end;
procedure TTagInfoList.SetTag(aIndex : Integer; AValue: TTagInfoItem);
begin
Items[aIndex]:=aValue;
end;
function TTagInfoList.AddTagItem(const aElementID, aTag, aType, aName: String
): TTagInfoItem;
begin
Result:=TTagInfoItem(Add);
Result.ElementID:=aElementID;
Result.TagName:=aTag;
Result.InputType:=aType;
Result.InputName:=aName;
end;
function TTagInfoList.IndexOfID(const aElementID: String): Integer;
begin
Result:=Count-1;
While (Result>=0) and Not (aElementID=Tags[Result].ElementID) do
Dec(Result);
end;
function TTagInfoList.FindByID(const aElementID: String): TTagInfoItem;
var
I : integer;
begin
Result:=Nil;
I:=IndexOfID(aElementID);
if I<>-1 then
Result:=Tags[i];
end;
{ TTagInfoItem }
procedure TTagInfoItem.Assign(aSource: TPersistent);
Var
Src : TTagInfoItem absolute aSource;
begin
if aSource is TTagInfoItem then
begin
ElementID:=Src.ElementID;
InputName:=Src.InputName;
InputType:=Src.InputType;
TagName:=Src.TagName;
end
else
inherited Assign(aSource);
end;
function TTagInfoItem.ToString: String;
begin
Result:=ElementID;
if (TagName<>'') or (InputType<>'') or (InputName<>'') then
Result:=Result+'=';
Result:=Result+TagName;
if InputType<>'' then
Result:=Result+'['+InputType+']';
if InputName<>'' then
Result:=Result+'('+InputName+')';
end;
{ TTagInfo }
constructor TTagInfo.Create(const aTag, aType, aName: String);
begin
FTag:=aTag;
FInputType:=aType;
FInputName:=aName;
end;
function TTagInfo.ToString: String;
begin
Result:=FTag;
if FInputType<>'' then
Result:=Result+'['+FInputType+']'
end;
{ THTMLExtractIDS }
procedure THTMLExtractIDS.DoStartElement(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString; Atts: TSAXAttributes);
function GetIndex(const aName: SAXString): Integer;
begin
Result := Atts.Length-1;
while (Result>=0) and not SameText(UTF8Encode(Atts.LocalNames[Result]),UTF8Encode(aName)) do
Dec(Result);
end;
Var
aID,aTag,aType,aName: UTF8String;
Idx : Integer;
begin
aTag:='';
aType:='';
aName:='';
if Not Assigned(atts) then exit;
aID:=UTF8Encode(Atts.GetValue('','id'));
if (aID<>'') then
begin
if (Level=0) and (BelowID=aID) then
begin
Level:=1;
exit;
end
else if (BelowID<>'') and (Level<=0) then
Exit;
if eoExtraInfo in FOptions then
begin
aTag:=LowerCase(UTF8Encode(LocalName));
if SameText(aTag,'input') then
begin
idx:=GetIndex('type');
if Idx=-1 then
aType:='text'
else
aType:=LowerCase(Utf8Encode(Atts.LocalNames[Idx]));
end;
end;
FList.AddTagItem(aID,aTag,aType,aName);
end;
end;
procedure THTMLExtractIDS.DoEndElement(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString);
begin
if Level>0 then
Dec(FLevel);
end;
procedure THTMLExtractIDS.ExtractIDS(aInput: TStream; aList: TStrings);
Var
aCol : TTagInfoList;
aItm : TTagInfoItem;
obj : TTagInfo;
I : Integer;
begin
Obj:=nil;
aCol:=TTagInfoList.Create(TTagInfoItem);
try
ExtractIDS(aInput,aCol);
For I:=0 to aCol.Count-1 do
begin
aItm:=aCol[i];
if eoExtraInfo in FOptions then
Obj:=TTagInfo.Create(aItm.TagName,aItm.InputType,aItm.InputName);
aList.AddObject(aItm.ElementID,Obj);
end;
finally
aCol.Free;
end;
end;
procedure THTMLExtractIDS.ExtractIDS(aInput: TStream; aList: TTagInfoList);
var
MyReader : THTMLReader;
begin
FList:=aList;
MyReader:=THTMLReader.Create;
Try
MyReader.OnStartElement:=@DoStartElement;
MyReader.OnEndElement:=@DoEndElement;
MyReader.ParseStream(aInput);
finally
FreeAndNil(MyReader);
end;
end;
function THTMLExtractIDS.ExtractIDS(aInput: TStream): TStringArray;
Var
L : TStringList;
S : String;
I : Integer;
begin
L:=TStringList.Create;
try
L.OwnsObjects:=True;
ExtractIDS(aInput,L);
L.Sort;
Setlength(Result{%H-},L.Count);
For I:=0 to L.Count-1 do
begin
S:=L[i];
if Assigned(L.Objects[i]) then
S:=S+TTagInfo(L.Objects[i]).ToString;
Result[I]:=S;
end;
finally
L.Free;
end;
end;
procedure THTMLExtractIDS.ExtractIDS(const aFileName: String;
aList: TTagInfoList);
Var
F : TFileStream;
B : TBufStream;
begin
F:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
try
B:=TReadBufStream.Create(F,4096);
B.SourceOwner:=True;
ExtractIDS(B,aList);
finally
B.Free;
end;
end;
procedure THTMLExtractIDS.ExtractIDS(const aFileName: String; aList: TStrings);
Var
F : TFileStream;
B : TBufStream;
begin
F:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
try
B:=TReadBufStream.Create(F,4096);
B.SourceOwner:=True;
ExtractIDS(B,aList);
finally
B.Free;
end;
end;
function THTMLExtractIDS.ExtractIDS(const aFileName : String): TStringArray;
Var
L : TStringList;
I : Integer;
S : String;
begin
L:=TStringList.Create;
try
ExtractIDS(aFileName,L);
L.Sort;
Setlength(Result{%H-},L.Count);
For I:=0 to L.Count-1 do
begin
S:=L[i];
if Assigned(L.Objects[i]) then
S:=S+TTagInfo(L.Objects[i]).ToString;
Result[I]:=S;
end;
finally
L.Free;
end;
end;
{ ----------------------------------------------------------------------
----------------------------------------------------------------------}
{ THTML2ClassOptions }
function THTML2ClassOptions.GetB(AIndex: Integer): Boolean;
begin
Result:=FBools[aindex];
end;
function THTML2ClassOptions.GetMethods(AIndex: Integer): TSpecialMethods;
begin
Result:=FMethods[aindex];
end;
function THTML2ClassOptions.GetS(AIndex: Integer): String;
begin
Result:=FStrings[aindex];
end;
procedure THTML2ClassOptions.SetB(AIndex: Integer; AValue: Boolean);
begin
FBools[aIndex]:=aValue;
end;
procedure THTML2ClassOptions.SetExcludeElements(AValue: TStrings);
begin
if FExcludeElements=AValue then Exit;
FExcludeElements.Assign(AValue);
end;
procedure THTML2ClassOptions.SetMethods(AIndex: Integer; AValue: TSpecialMethods);
begin
FMethods[aIndex]:=aValue;
end;
procedure THTML2ClassOptions.SetS(AIndex: Integer; AValue: String);
begin
FStrings[aIndex]:=aValue;
end;
constructor THTML2ClassOptions.Create;
begin
FExcludeElements:=TStringList.Create;
Reset;
end;
destructor THTML2ClassOptions.Destroy;
begin
FreeAndNil(FExcludeElements);
inherited Destroy;
end;
procedure THTML2ClassOptions.Reset;
begin
// Assume class is TComponent descendant
ConstructorArgs:='aOwner : TComponent';
FormClassName:='TMyForm';
ParentClassName:='TComponent';
EventSignature:='Event : TJSEvent';
EventModifiers:='virtual; abstract;';
GetElementFunction:='document.getelementByID';
AddMethods:=[smConstructor,smBindElements,smBindElementEvents];
VirtualMethods:=[smBindElementEvents,smBindElements];
OverrideMethods:=[smConstructor];
FormOptions:=[foBindInConstructor];
FExcludeElements.Clear;
ExtraUnits:='Classes'
end;
procedure THTML2ClassOptions.toJSON(aObject: TJSONObject);
Function GenToArray(aMethods : TSpecialMethods) : TJSONArray;
Var
M : TSpecialMethod;
begin
Result:=TJSONArray.Create;
For M in TSpecialMethods do
If M in aMethods then
Result.Add(GetEnumName(TypeInfo(TSpecialMethod),Ord(M)));
end;
Function OptionsToArray(aOptions : TFormOptions) : TJSONArray;
Var
F : TFormOption;
begin
Result:=TJSONArray.Create;
For F in TFormOptions do
If F in aOptions then
Result.Add(GetEnumName(TypeInfo(TFormOptions),Ord(F)));
end;
Var
arr : TJSONArray;
S : String;
begin
With aObject do
begin
Add('OverrideMethods',GenToArray(OverrideMethods));
Add('AddMethods',GenToArray(AddMethods));
Add('VirtualMethods',GenToArray(VirtualMethods));
Add('FormOptions',OptionsToArray(FormOptions));
Add('GetElementFunction',GetElementFunction);
Add('EventSignature',EventSignature);
Add('EventModifiers',EventModifiers);
Add('ConstructorArgs',ConstructorArgs);
Add('BelowID',BelowID);
Add('HTMLFileName',HTMLFileName);
Add('FormClassname',FormClassname);
Add('UseDefaultElements',UseDefaultElements);
Add('AddHTMLToProject',AddHTMLToProject);
arr:=TJSONArray.Create;
Add('ExcludeElements',Arr);
For S in ExcludeElements do
arr.Add(S);
end;
end;
procedure THTML2ClassOptions.FromJSON(aJSON: String);
Var
D : TJSONData;
J : TJSONObject Absolute D;
begin
D:=GetJSON(aJSON);
try
if D is TJSONObject then
FromJSON(J);
finally
D.Free;
end;
end;
procedure THTML2ClassOptions.FromJSON(aObject: TJSONObject);
Function GenFromArray(Arr : TJSONArray) : TSpecialMethods;
Var
I,Idx : integer;
begin
Result:=[];
if Assigned(Arr) then
For I:=0 to Arr.Count-1 do
if (Arr.types[I]=jtString) then
begin
Idx:=GetEnumValue(TypeInfo(TSpecialMethod),Arr.Strings[I]);
If Idx<>-1 then
include(Result,TSpecialMethod(Idx));
end;
end;
Function OptionsFromArray(arr : TJSONArray) : TFormOptions;
Var
I,Idx : integer;
begin
Result:=[];
if Assigned(Arr) then
For I:=0 to Arr.Count-1 do
if (Arr.types[I]=jtString) then
begin
Idx:=GetEnumValue(TypeInfo(TFormOption),Arr.Strings[I]);
If Idx<>-1 then
include(Result,TFormOption(Idx));
end;
end;
Var
arr : TJSONArray;
I : integer;
begin
With aObject do
begin
OverrideMethods:=GenFromArray(Get('OverrideMethods',TJSONArray(Nil)));
AddMethods:=GenFromArray(Get('AddMethods',TJSONArray(Nil)));
VirtualMethods:=GenFromArray(Get('VirtualMethods',TJSONArray(Nil)));
FormOptions:=OptionsFromArray(Get('FormOptions',TJSONArray(Nil)));
GetElementFunction:=Get('GetElementFunction','');
EventSignature:=Get('EventSignature','');
EventModifiers:=Get('EventModifiers','');
ConstructorArgs:=Get('ConstructorArgs','');
BelowID:=Get('BelowID','');
HTMLFileName:=Get('HTMLFileName','');
FormClassname:=Get('FormClassname','');
UseDefaultElements:=Get('UseDefaultElements',False);
AddHTMLToProject:=Get('AddHTMLToProject',False);
ExcludeElements.Clear;
Arr:=Get('ExcludeElements',TJSONArray(Nil));
if Assigned(Arr) then
For I:=0 to Arr.Count-1 do
if (Arr.types[I]=jtString) then
ExcludeElements.Add(Arr.Strings[I]);
end;
end;
function THTML2ClassOptions.asJSON(Formatted: Boolean): String;
Var
J : TJSONObject;
begin
J:=TJSONObject.Create;
try
ToJSON(J);
if Formatted then
Result:=J.FormatJSON()
else
Result:=J.asJSON;
finally
J.Free;
end;
end;
{ TFormFileCodeGen }
function TFormFileCodeGen.GetFormName(const aClassName: string): String;
begin
Result:=aClassName;
if SameText(Copy(Result,1,1),'T') then
Delete(Result,1,1);
end;
(*
procedure TFormFileCodeGen.LoadFromStream(const AStream: TStream);
begin
if aStream=Nil then exit;
end;
*)
constructor TFormFileCodeGen.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
IDProperty:='ElementID';
ElementHeight:=24;
ElementWidth:=72;
ElementVSpacing:=8;
ElementHSpacing:=16;
MaxWidth:=800;
MaxHeight:=600;
end;
procedure TFormFileCodeGen.NextPosition;
begin
ELeft:=ELeft+ElementWidth+ElementHSpacing;
if ELeft+ElementWidth>=MaxWidth then
begin
ELeft:=8;
ETop:=ETop+ElementHeight+ElementVSpacing;
end;
end;
procedure TFormFileCodeGen.EmitElementProps(El : TFormElement);
begin
AddLn('Top = %d',[ETop]);
AddLn('Left = %d',[ELeft]);
Addln('Width = %d',[ElementWidth]);
Addln('Height = %d',[ElementHeight]);
addLn('%s = ''%s''',[IDProperty,El.Name]);
end;
procedure TFormFileCodeGen.EmitElementEvents(El : TFormElement);
Var
S,EN,EH : String;
begin
For S in El.Events do
begin
TFormCodeGen.GetEventNameAndHandler(S,El.Name,EN,EH);
Addln('%s = %s',[EN,EH]);
end;
end;
procedure TFormFileCodeGen.GenerateElements;
Var
I : Integer;
El : TFormElement;
begin
For I:=0 to FormElements.Count-1 do
begin
el:=FormElements[i];
With El do
begin
Addln('object %s: %s',[Name,ElementType]);
Indent;
EmitElementProps(EL);
if DoEvents then
EmitElementEvents(El);
Undent;
AddLn('end');
NextPosition;
end;
end;
end;
procedure TFormFileCodeGen.Execute;
begin
ETop:=8;
ELeft:=8;
AddLn('object %s : %s',[GetFormName(FormClassName),FormClassName]);
Indent;
AddLn('Width = %d',[MaxWidth]);
AddLn('Height = %d',[MaxHeight]);
GenerateElements;
Undent;
AddLn('end');
end;
{ THTMLElementMapList }
function THTMLElementMapList.GetM(aIndex : Integer): THTMLElementMap;
begin
Result:=Items[aIndex] as THTMLElementMap;
end;
procedure THTMLElementMapList.LoadFromFile(const aFileName: String);
Var
F : TFileStream;
begin
F:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(F);
finally
F.Free;
end;
end;
procedure THTMLElementMapList.LoadFromStream(aStream: TStream);
Var
D : TJSONData;
begin
D:=GetJSON(aStream);
try
if D is TJSONArray then
LoadFromJSON(D as TJSONArray);
finally
D.Free;
end;
end;
procedure THTMLElementMapList.LoadFromJSON(aJSON: TJSONArray);
Var
E : TJSONEnum;
begin
For E in aJSON do
if E.Value is TJSONObject then
(Add as THTMLElementMap).LoadFromJSON(e.Value as TJSONObject);
end;
function THTMLElementMapList.IndexOfMap(aTag: SAXString; Attrs: TSAXAttributes
): Integer;
begin
Result:=0;
While (Result<Count) and Not GetM(Result).IsMatch(aTag,Attrs) do
Inc(Result);
if Result=Count then
Result:=-1;
end;
function THTMLElementMapList.FindMap(aTag: SAXString; Attrs: TSAXAttributes
): THTMLElementMap;
Var
Idx : Integer;
begin
Idx:=IndexOfMap(aTag,Attrs);
If Idx=-1 then
Result:=Nil
else
Result:=GetM(Idx);
end;
{ THTMLElementMap }
function THTMLElementMap.GetAttrConditionList: TAttributeConditionList;
begin
If FConditionList=Nil then
FConditionList:=CreateConditionList;
Result:=FConditionList
end;
function THTMLElementMap.CreateConditionList: TAttributeConditionList;
begin
Result:=TAttributeConditionList.Create(TAttributeCondition);
end;
destructor THTMLElementMap.Destroy;
begin
FreeAndNil(FConditionList);
inherited Destroy;
end;
procedure THTMLElementMap.LoadFromJSON(aJSON: TJSONObject);
Var
A : TJSONObject;
begin
FTag:=aJSON.Get('tag','');
ControlClass:=aJSON.Get('class','');
A:=aJSON.Get('attrs',TJSONObject(Nil));
If Assigned(A) then
Attributes.LoadFromJSON(A);
end;
function THTMLElementMap.HasConditions: Boolean;
begin
Result:=Assigned(FConditionList) and (FConditionList.Count>0);
end;
function THTMLElementMap.IsMatch(aTag: SAXString; Attrs: TSAXAttributes): Boolean;
begin
Result:=SameText(UTF8Encode(aTag),FTag);
if Result and HasConditions then
Result:=Attributes.IsMatch(Attrs);
end;
{ TAttributeConditionList }
function TAttributeConditionList.GetC(aIndex : Integer): TAttributeCondition;
begin
Result:=TAttributeCondition(Items[aIndex]);
end;
procedure TAttributeConditionList.LoadFromJSON(aJSON: TJSONObject);
Var
E : TJSONEnum;
A : TAttributeCondition;
begin
For E in aJSON do
begin
A:=Add as TAttributeCondition;
A.LoadFromJSON(E.Key,E.Value);
end;
end;
function TAttributeConditionList.IsMatch(Attrs: TSAXAttributes): Boolean;
function GetIndex(const aName: SAXString): Integer;
begin
Result := Attrs.Length-1;
while (Result>=0) and not SameText(UTF8Encode(Attrs.LocalNames[Result]),UTF8Encode(aName)) do
Dec(Result);
end;
Var
I,Idx : Integer;
A : TAttributeCondition;
begin
Result:=True;
I:=0;
While Result and (I<Count) do
begin
A:=GetC(I);
Idx:=GetIndex(UTF8Decode(A.Attribute));
if A.Operation=aoNotPresent then
Result:=Idx<0
else
Result:=A.IsMatch(UTF8Encode(Attrs.GetValue(Idx)));
Inc(I);
end;
end;
{ TAttributeCondition }
procedure TAttributeCondition.LoadFromJSON(aName: String; aValue: TJSONData);
Var
S : TJSONStringType;
C : Char;
begin
Attribute:=aName;
if aValue.JSONType=jtNull then
Operation:=aoNotPresent
else if aValue.JSONType=jtBoolean then
begin
if aValue.AsBoolean then
Operation:=aoPresent
else
Operation:=aoNotPresent
end
else
begin
S:=aValue.AsString;
If S<>'' then
C:=S[1]
else
C:=#0;
Case C of
'-' : Operation:=aoNotEqual;
'~' : Operation:=aoContains;
else
Operation:=aoEqual;
Value:=S;
end;
if Operation in [aoNotEqual,aoContains] then
Value:=Copy(S,2,Length(S)-1);
end;
end;
function TAttributeCondition.IsMatch(aValue: String): Boolean;
begin
Case Operation of
aoPresent : Result:=True;
aoNotEqual : Result:=Not SameText(aValue,Value);
aoEqual : Result:=SameText(aValue,Value);
aoContains : Result:=Pos(LowerCase(Value),LowerCase(aValue))>0;
end;
end;
{ THTMLToFormElements }
procedure THTMLToFormElements.SetFormElements(AValue: TFormElementList);
begin
if FFormElements=AValue then Exit;
FFormElements:=AValue;
end;
procedure THTMLToFormElements.DoLog(const Msg: String);
begin
if Assigned(FOnLog) then
FOnLog(Self,Msg);
end;
procedure THTMLToFormElements.DoLog(const Fmt: String; Args: array of const);
begin
DoLog(Format(Fmt,Args));
end;
function THTMLToFormElements.Maptype(aTag: SAXString; Atts: TSAXAttributes): String;
var
t : string;
m : THTMLElementMap;
begin
Result:='';
if Map.Count>0 then
begin
M:=Map.FindMap(aTag,Atts);
if Assigned(m) then
Exit(M.ControlClass)
else if not DefaultElements then
begin
DoLog('Could not map tag %s',[aTag]);
Exit;
end;
end;
t:=lowercase(Utf8Encode(aTag));
case t of
'input' : Result:='TJSHTMLInputElement';
'button' : Result:='TJSHTMLButtonElement';
'select' : Result:='TJSHTMLSelectElement';
'textarea' : Result:='TJSHTMLTextAreaElement';
'option' : Result:='';
else
Result:='TJSHTMLElement';
end;
end;
function THTMLToFormElements.MakeValidName(aID: string): string;
Var
C : Char;
begin
Result:='';
for C in aID do
if C in ['_','a'..'z','A'..'Z','0'..'9'] then
Result:=Result+C
else
Result:=Result+'_';
end;
procedure THTMLToFormElements.SetExcludeIDS(AValue: TStrings);
begin
if FExcludeIDS=AValue then Exit;
FExcludeIDs.AddStrings(AValue,True);
end;
procedure THTMLToFormElements.DoStartElement(Sender: TObject;
const NamespaceURI, LocalName, QName: SAXString; Atts: TSAXAttributes);
Var
aID,aType : String;
El : TFormElement;
begin
if Not Assigned(atts) then exit;
aID:=UTF8Encode(Atts.GetValue('','id'));
if (aID='') or (FExcludeIDS.IndexOf(aID)>=0) then
exit;
if (Level=0) and (BelowID=aID) then
Level:=1
else if (BelowID<>'') and (Level<=0) then
Exit;
aType:=MapType(LocalName,Atts);
if aType='' then
DoLog('Ignoring tag %s with id %s',[LocalName,aID])
else
begin
El:=FormElements.Add(MakeValidName(aID));
EL.ElementType:=aType;
EL.HTMLID:=aId;
GetEvents(El,Atts);
end
end;
procedure THTMLToFormElements.GetEvents(aEl : TFormElement; Atts : TSAXAttributes);
Var
I,aLen : Integer;
aName : string;
begin
for I:=0 to Atts.Length-1 do
begin
aName:=UTF8Encode(Atts.GetLocalName(i));
aLen:=Length(aName);
if (aLen>3) and (Copy(aName,1,1)='_') and (Copy(aName,aLen,1)='_') then
aEl.Events.Add(Copy(aName,2,aLen-2)+'='+UTF8Encode(Atts.GetValue(i)));
end;
end;
procedure THTMLToFormElements.DoEndElement(Sender: TObject; const NamespaceURI,
LocalName, QName: SAXString);
begin
if Level>0 then
Dec(FLevel);
end;
class function THTMLToFormElements.CreateElementList: TFormElementList;
begin
Result:=TFormElementList.Create(TFormElement);
end;
function THTMLToFormElements.CreateHTMLElementMapList: THTMLElementMapList;
begin
Result:=THTMLElementMapList.Create(THTMLElementMap);
end;
constructor THTMLToFormElements.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FMap:=CreateHTMLElementMapList;
FFormElements:=CreateElementList;
FExcludeIDS:=TStringList.Create;
TStringList(FExcludeIDS).Sorted:=True;
end;
destructor THTMLToFormElements.Destroy;
begin
FreeAndNil(FMap);
FreeAndNil(FExcludeIDS);
FreeAndNil(FFormElements);
inherited Destroy;
end;
procedure THTMLToFormElements.Clear;
begin
FFormElements.Clear;
end;
procedure THTMLToFormElements.LoadFromStream(aInput: TStream);
var
MyReader : THTMLReader;
begin
MyReader:=THTMLReader.Create;
Try
MyReader.OnStartElement:=@DoStartElement;
MyReader.OnEndElement:=@DoEndElement;
MyReader.ParseStream(aInput);
finally
FreeAndNil(MyReader);
end;
end;
procedure THTMLToFormElements.LoadFromFile(const aFileName: String);
var
F : TFileStream;
begin
F:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(F);
finally
F.Free;
end;
end;
procedure THTMLToFormElements.LoadOptions(aOptions: THTML2ClassOptions);
begin
BelowID:=aoptions.BelowID;
ExcludeIDS:=aOptions.ExcludeElements;
DefaultElements:=aOptions.UseDefaultElements;
if (aOptions.TagMapFileName<>'') and FileExists(aOptions.TagMapFileName) then
Map.LoadFromFile(aOptions.TagMapFileName);
end;
{ TFormCodeGen }
procedure TFormCodeGen.SetFormElements(AValue: TFormElementList);
begin
if FFormElements=AValue then Exit;
FFormElements.Assign(AValue);
end;
function TFormCodeGen.BaseUnits: String;
begin
Result:='js, web';
end;
class function TFormCodeGen.CreateElementList: TFormElementList;
begin
Result:=TFormElementList.Create(TFormElement);
end;
constructor TFormCodeGen.Create(aOwner: TComponent);
Var
Defs : THTML2ClassOptions;
begin
inherited Create(aOwner);
FFormElements:=CreateElementList;
FFormFileGenerator:=CreateFormFileGen;
FFormSource:=TStringList.Create;
// Defaults must be set in
Defs:=THTML2ClassOptions.Create;
try
Defs.Reset;
Loadoptions(Defs);
finally
Defs.Free;
end;
end;
destructor TFormCodeGen.Destroy;
begin
FreeAndNil(FFormSource);
FreeAndNil(fFormFileGenerator) ;
FreeAndNil(FFormElements);
inherited Destroy;
end;
procedure TFormCodeGen.EmitFormElement(aEL : TFormElement);
begin
With aEl do
AddLn('%s : %s;',[Name,ElementType]) ;
end;
procedure TFormCodeGen.EmitFormEvents(aEL : TFormElement);
Var
S,EN,EH : String;
begin
if not aEl.HasEvents then
exit;
For S in aEl.Events do
begin
GetEventNameAndHandler(S,aEl.Name,EN,EH);
Addln('Procedure %s(%s); %s',[EH, EventSignature,EventModifiers]);
end;
end;
procedure TFormCodeGen.EmitPublishedSection;
var
I : Integer;
begin
For I:=0 to FormElements.Count-1 do
EmitFormElement(FormElements[i]);
if foEvents in Options then
For I:=0 to FormElements.Count-1 do
EmitFormEvents(FormElements[i]);
end;
function TFormCodeGen.VirtualOverride(M: TSpecialMethod; const Decl: String): string;
begin
Result:=Decl;
if M in OverrideMethods then
Result:=Result+' override;'
else if M in VirtualMethods then
Result:=Result+' virtual;'
end;
procedure TFormCodeGen.EmitPublicSection;
begin
if smConstructor in AddMethods then
Addln(VirtualOverride(smConstructor,'Constructor create('+ConstructorArgs+');'));
if smBindElements in AddMethods then
Addln(VirtualOverride(smBindElements, 'Procedure BindElements;'));
if (smBindElementEvents in AddMethods) and (foEvents in Options) then
Addln(VirtualOverride(smBindElementEvents,'Procedure BindElementEvents;'));
end;
procedure TFormCodeGen.Execute;
begin
Source.Clear;
Addln('unit %s;',[OutputUnitName]);
CreateHeader;
Addln('Type');
Indent;
ClassHeader(FormClassName);
AddLn('%s = class(%s) ',[FormClassName,ParentClassName]);
Addln('Published');
Indent;
EmitPublishedSection;
Undent;
Addln('Public');
Indent;
EmitPublicSection;
Undent;
Addln('end;');
Undent;
Addln('');
Addln('implementation');
AddLn('');
if (foFormFile in Options) then
begin
EmitFormFile;
AddLn('');
AddLn('{$R *.dfm}');
AddLn('');
end;
ClassHeader(FormClassName);
EmitImplementation;
AddLn('');
AddLn('end.');
end;
procedure TFormCodeGen.LoadOptions(aOptions: THTML2ClassOptions);
begin
ExtraUnits:=aOptions.ExtraUnits;
FormClassName:=aOptions.FormClassname;
ParentClassName:=aOptions.ParentClassName;
GetElementFunction:=aOptions.GetElementFunction;
EventSignature:=aOptions.EventSignature;
EventModifiers:=aOptions.EventModifiers;
ConstructorArgs:=aOptions.ConstructorArgs;
Options:=aOptions.FormOptions;
AddMethods:=aOptions.AddMethods;
OverrideMethods:=aOptions.OverrideMethods;
VirtualMethods:=aOptions.VirtualMethods;
end;
procedure TFormCodeGen.EmitFormFile;
begin
FormFileGenerator.FormElements:=Self.FormElements;
FormFileGenerator.DoEvents:=foEvents in Options;
FormFileGenerator.FormClassName:=Self.FormClassName;
FormFileGenerator.Execute;
FormSource.Assign(FormFileGenerator.Source);
end;
function TFormCodeGen.CreateFormFileGen: TFormFileCodeGen;
begin
Result:=TFormFileCodeGen.Create(Nil);
end;
function TFormCodeGen.CreateHTMLToFormELements: THTMLToFormElements;
begin
Result:=THTMLToFormElements.Create(Self);
end;
procedure TFormCodeGen.EmitFormConstructor;
begin
Addln('');
Addln('Constructor %s.create(%s);',[FormClassName,ConstructorArgs]);
if not (foBindInConstructor in Options) then
SimpleMethodBody(['Inherited;'])
else
begin
if foEvents in Options then
SimpleMethodBody(['Inherited;','BindElements;','BindElementEvents;'])
else
SimpleMethodBody(['Inherited;','BindElements;']);
end;
Addln('');
end;
procedure TFormCodeGen.EmitImplementation;
begin
if smConstructor in AddMethods then
EmitFormConstructor;
if (smBindElements in AddMethods) then
EmitFormBindElements;
if (foEvents in Options) and Not (foFormFile in Options) and (smBindElementEvents in AddMethods) then
EmitFormBindEvents;
end;
procedure TFormCodeGen.EmitFormBindElements;
var
I : integer;
El : TFormElement;
begin
Addln('');
Addln('Procedure %s.BindElements;',[FormClassName]);
Addln('');
AddLn('begin');
Indent;
if smBindElements in OverrideMethods then
AddLn('inherited;');
For I:=0 to FormElements.Count-1 do
begin
el:=FormElements[i];
With El do
Addln('%s:=%s(%s(''%s''));',[Name,ElementType,GetElementFunction,HTMLID]);
end;
Undent;
Addln('end;');
Addln('');
end;
class function TFormCodeGen.Pretty(const S: String): string;
begin
Result:=UpperCase(Copy(S,1,1))+LowerCase(Copy(S,2,Length(S)-1));
end;
class procedure TFormCodeGen.GetEventNameAndHandler(const S,
aFieldName: String; out aName, aHandler: string);
Var
P : Integer;
begin
P:=Pos('=',S);
if (P=0) then
P:=Length(S)+1;
aName:=Copy(S,1,P-1);
aHandler:=Copy(S,P+1,Length(S)-P);
if AHandler='' then
aHandler:=aFieldName+Pretty(aName);
// Writeln(aFieldName,': ',S,' -> ',aName,' & ',aHandler);
end;
procedure TFormCodeGen.EmitFormBindEvents;
var
I : integer;
El : TFormElement;
S,EN,EH : String;
begin
Addln('Procedure %s.BindElementEvents;',[FormClassName]);
Addln('');
AddLn('begin');
Indent;
if smBindElementEvents in OverrideMethods then
AddLn('inherited;');
For I:=0 to FormElements.Count-1 do
begin
el:=FormElements[i];
With El do
if HasEvents then
For S in El.Events do
begin
GetEventNameAndHandler(S,Name,EN,EH);
Addln('%s.AddEventListener(''%s'',@%s);',[Name,EN,EH]);
end;
end;
Undent;
Addln('end;');
end;
{ TFormElementList }
function TFormElementList.GetEl(aIndex : Integer): TFormElement;
begin
Result:=Items[aIndex] as TFormElement;
end;
function TFormElementList.Add(const aName: string): TFormElement;
begin
if IndexOf(aName)<>-1 then
Raise Exception.CreateFmt('Duplicate name : %s' ,[aName]);
Result:=(Inherited Add) as TFormElement;
Result.Name:=aName;
end;
function TFormElementList.IndexOf(const aName: string): Integer;
begin
Result:=Count-1;
While (Result>=0) and Not SameText(aName,GetEl(Result).Name) do
Dec(Result);
end;
function TFormElementList.Find(const aName: string): TFormElement;
var
Idx : Integer;
begin
Idx:=IndexOf(aName);
if Idx>=0 then
Result:=GetEl(Idx)
else
Result:=Nil;
end;
{ TFormElement }
function TFormElement.GetEvents: TStrings;
begin
If (FEvents=Nil) then
FEvents:=TStringList.Create;
Result:=FEvents;
end;
function TFormElement.getName: String;
begin
Result:=FName;
if Result='' then
Result:=HTMLID;
end;
procedure TFormElement.SetEvents(AValue: TStrings);
begin
If AValue=FEVents then exit;
Events.Assign(aValue);
end;
destructor TFormElement.Destroy;
begin
FreeAndNil(FEvents);
inherited Destroy;
end;
function TFormElement.HasEvents: Boolean;
begin
Result:=Assigned(FEvents) and (FEvents.Count>0);
end;
procedure TFormElement.Assign(Source: TPersistent);
Var
FE : TFormElement absolute Source;
begin
if Source is TFormElement then
begin
FHTMLID:=FE.HTMLID;
FName:=FE.FName;
FType:=FE.FType;
if FE.HasEvents then
Events:=FE.Events;
end
else
inherited Assign(Source);
end;
end.
|