htdump.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. {
  2. This program takes an W3 IDL XML file with interface definitions and
  3. dumps a interface definition.
  4. }
  5. {$MODE objfpc}
  6. {$H+}
  7. program htdump;
  8. uses sysutils, DOM, xmlread;
  9. Var DoImplementation : Boolean;
  10. procedure DumpNode(node: TDOMNode; spc: String);forward;
  11. procedure DumpMethodNode(node: TDOMElement; spc: String);
  12. var N,NN : TDOMNode;
  13. rettype : DOMString;
  14. firstparam : boolean;
  15. i : longint;
  16. begin
  17. N:=Node.FindNode('returns');
  18. If N<>Nil then
  19. rettype:=TDomElement(N).GetAttribute('type');
  20. If Not DoImplementation then
  21. Write (spc);
  22. If RetType='void' then
  23. Write ('Procedure ')
  24. else
  25. Write ('Function ');
  26. If DoImplementation then
  27. Write(TDomElement(Node.ParentNode).GetAttribute('name'),'.');
  28. Write (Node.GetAttribute('name'));
  29. N:=Node.FindNode('params');
  30. If N<>Nil then
  31. begin
  32. FirstParam:=True;
  33. for I:=1 to N.ChildNodes.Count-1 do
  34. begin
  35. NN:=N.ChildNodes.Item[i];
  36. If NN.NodeName<>'param' then
  37. begin
  38. If Firstparam then
  39. begin
  40. Write('(');
  41. FirstParam:=False
  42. end
  43. else
  44. Write(';');
  45. writeln (spc,NN.NodeName,' : ',TDOMElement(NN).GetAttribute('type'));
  46. end;
  47. end;
  48. If Not FirstParam then
  49. Write (')');
  50. end;
  51. If RetType <>'void' then
  52. Write (' : ',Rettype);
  53. Writeln(';');
  54. If DoImplementation then
  55. begin
  56. Writeln;
  57. Writeln('Begin');
  58. Writeln('End;');
  59. Writeln;
  60. Writeln;
  61. end;
  62. end;
  63. procedure DumpAttributeNode(Doprivate: Boolean;node: TDOMElement; spc: String);
  64. Var PropName : DOMString;
  65. begin
  66. PropName:=Node.GetAttribute('name');
  67. If DOPrivate then
  68. Write (spc,'F')
  69. else
  70. Write (spc,'Property ');
  71. Write (PropName,' : ',Node.getAttribute('type'));
  72. If Not DoPrivate then
  73. begin
  74. Write (' Read F',PropName);
  75. If not(Node.getAttribute('readonly')='yes') then
  76. Write (' Write F',PropName)
  77. end;
  78. Writeln(';');
  79. end;
  80. Procedure DumpInterfaceNode (node : TDomElement; spc : String);
  81. Var N : TDOMNode;
  82. C : TDOMNodeList;
  83. I : longint;
  84. begin
  85. If not DoImplementation then
  86. begin
  87. Write(spc,Node.GetAttribute('name'),' = Class');
  88. N:=Node.Attributes.GetNamedItem('inherits');
  89. If N<>Nil then
  90. Write('(',N.NodeValue,')');
  91. Writeln;
  92. // Dump Property fields
  93. Writeln (Spc+' Private');
  94. N:=Node.FirstChild;
  95. While N<>Nil do
  96. begin
  97. If N.NodeName='attribute' then
  98. DumpAttributeNode(True,TDOMElement(N), spc + ' ');
  99. N:=N.NextSibling;
  100. end;
  101. Writeln (Spc,' Public');
  102. end;
  103. N:=Node.FirstChild;
  104. While N<>Nil do
  105. begin
  106. If N.NodeName='method' then
  107. DumpMethodNode(TDomElement(N), spc + ' ');
  108. N:=N.NextSibling;
  109. end;
  110. If Not DoImplementation then
  111. begin
  112. N:=Node.FirstChild;
  113. While N<>Nil do
  114. begin
  115. If N.NodeName='attribute' then
  116. DumpAttributeNode(False,TDomElement(N), spc + ' ');
  117. N:=N.NextSibling;
  118. end;
  119. writeln (spc,'End;')
  120. end;
  121. end;
  122. procedure DumpNode(node: TDOMNode; spc: String);
  123. var
  124. i: Integer;
  125. attr: TDOMNode;
  126. begin
  127. If Node.NodeName='interface' then
  128. DumpInterfaceNode(TDOMElement(Node),Spc)
  129. else if Node.NodeName='method' then
  130. DumpMethodNode(TDOMELEMENt(Node),Spc)
  131. else if Node.NodeName='attribute' then
  132. DumpAttributeNode(True,TDomelement(node),spc)
  133. else
  134. if node.FirstChild <> nil then
  135. DumpNode(node.FirstChild, spc + ' ');
  136. if node.NextSibling <> nil then
  137. DumpNode(node.NextSibling, spc);
  138. end;
  139. var
  140. i : longint;
  141. xml: TXMLDocument;
  142. begin
  143. if (ParamCount <1) or (paramcount>2) then begin
  144. WriteLn('htdump -m <xml>');
  145. exit;
  146. end;
  147. I:=1;
  148. If paramstr(1)='-m' then
  149. begin
  150. I:=2;
  151. DoImplementation:=True;
  152. end;
  153. ReadXMLFile(xml, ParamStr(i));
  154. WriteLn ('// Created From file ',paramstr(I));
  155. DumpNode(xml, '');
  156. end.