ppuxml.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. {
  2. Copyright (c) 2013 by Yury Sidorov and the FPC Development Team
  3. XML output of a PPU File
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. unit ppuxml;
  17. {$mode objfpc}{$H+}
  18. interface
  19. uses
  20. SysUtils, Classes, ppuout;
  21. type
  22. { TPpuXmlOutput }
  23. TPpuXmlOutput = class(TPpuOutput)
  24. private
  25. function XmlStr(const s: string): string;
  26. function GetTagName(const n, def: string): string;
  27. protected
  28. procedure WriteObjectStart(const AName: string; Def: TPpuDef); override;
  29. procedure WriteObjectEnd(const AName: string; Def: TPpuDef); override;
  30. procedure WriteArrayStart(const AName: string); override;
  31. procedure WriteArrayEnd(const AName: string); override;
  32. procedure WriteStr(const AName, AValue: string); override;
  33. public
  34. constructor Create(var OutFile: Text); override;
  35. procedure Init; override;
  36. end;
  37. implementation
  38. { TPpuXmlOutput }
  39. function TPpuXmlOutput.XmlStr(const s: string): string;
  40. var
  41. ws: widestring;
  42. ps: PWideChar;
  43. pd: PAnsiChar;
  44. slen, dlen, dpos: integer;
  45. procedure _AddChar(c: ansichar);
  46. begin
  47. if dpos = dlen then begin
  48. dlen:=dlen*2;
  49. SetLength(Result, dlen);
  50. pd:=PAnsiChar(Result) + dpos;
  51. end;
  52. pd^:=c;
  53. Inc(pd);
  54. Inc(dpos);
  55. end;
  56. procedure _AddStr(const s: string);
  57. var
  58. p: PAnsiChar;
  59. i: integer;
  60. begin
  61. p:=PAnsiChar(s);
  62. for i:=1 to Length(s) do begin
  63. _AddChar(p^);
  64. Inc(p);
  65. end;
  66. end;
  67. var
  68. c: widechar;
  69. begin
  70. ws:=UTF8Decode(s);
  71. ps:=PWideChar(ws);
  72. slen:=Length(ws);
  73. dlen:=slen + 2;
  74. SetLength(Result, dlen);
  75. pd:=PAnsiChar(Result);
  76. dpos:=0;
  77. while slen > 0 do begin
  78. c:=ps^;
  79. case c of
  80. '<': _AddStr('&lt;');
  81. '>': _AddStr('&gt;');
  82. '&': _AddStr('&amp;');
  83. '''': _AddStr('&apos;');
  84. '"': _AddStr('&quot;');
  85. '\': _AddStr('\\');
  86. else
  87. if (c > #127) or (byte(c) in [9, 10, 13]) then
  88. _AddStr('&#x' + hexStr(word(c), 4) + ';')
  89. else
  90. if c < #32 then
  91. _AddStr('\x' + hexStr(byte(c), 2))
  92. else
  93. _AddChar(c);
  94. end;
  95. Inc(ps);
  96. Dec(slen);
  97. end;
  98. SetLength(Result, dpos);
  99. end;
  100. function TPpuXmlOutput.GetTagName(const n, def: string): string;
  101. begin
  102. Result:=LowerCase(n);
  103. if Result = '' then
  104. Result:=def;
  105. end;
  106. procedure TPpuXmlOutput.WriteStr(const AName, AValue: string);
  107. begin
  108. if AName = 'Type' then
  109. exit;
  110. WriteLn(Format('<%s>%s</%0:s>', [GetTagName(AName, 'value'), XmlStr(AValue)]));
  111. end;
  112. procedure TPpuXmlOutput.WriteArrayStart(const AName: string);
  113. begin
  114. if (AName = '') and (Indent = 0) then
  115. exit;
  116. WriteLn(Format('<%s>', [GetTagName(AName, 'array')]));
  117. inherited;
  118. end;
  119. procedure TPpuXmlOutput.WriteArrayEnd(const AName: string);
  120. begin
  121. if (AName = '') and (Indent = 0) then
  122. exit;
  123. inherited;
  124. WriteLn(Format('</%s>', [GetTagName(AName, 'array')]));
  125. end;
  126. procedure TPpuXmlOutput.WriteObjectStart(const AName: string; Def: TPpuDef);
  127. begin
  128. if Def = nil then
  129. WriteLn(Format('<%s>', [GetTagName(AName, 'object')]))
  130. else
  131. WriteLn(Format('<%s>', [GetTagName(Def.DefTypeName, 'object')]));
  132. inherited;
  133. end;
  134. procedure TPpuXmlOutput.WriteObjectEnd(const AName: string; Def: TPpuDef);
  135. begin
  136. inherited;
  137. if Def = nil then
  138. WriteLn(Format('</%s>', [GetTagName(AName, 'object')]))
  139. else
  140. WriteLn(Format('</%s>', [GetTagName(Def.DefTypeName, 'object')]));
  141. end;
  142. constructor TPpuXmlOutput.Create(var OutFile: Text);
  143. begin
  144. inherited Create(OutFile);
  145. end;
  146. procedure TPpuXmlOutput.Init;
  147. begin
  148. inherited Init;
  149. WriteLn('<?xml version="1.0" encoding="utf-8"?>');
  150. end;
  151. end.