2
0

ppuxml.pp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. procedure Init; override;
  35. end;
  36. implementation
  37. { TPpuXmlOutput }
  38. function TPpuXmlOutput.XmlStr(const s: string): string;
  39. var
  40. ws: widestring;
  41. ps: PWideChar;
  42. pd: PAnsiChar;
  43. slen, dlen, dpos: integer;
  44. procedure _AddChar(c: ansichar);
  45. begin
  46. if dpos = dlen then begin
  47. dlen:=dlen*2;
  48. SetLength(Result, dlen);
  49. pd:=PAnsiChar(Result) + dpos;
  50. end;
  51. pd^:=c;
  52. Inc(pd);
  53. Inc(dpos);
  54. end;
  55. procedure _AddStr(const s: string);
  56. var
  57. p: PAnsiChar;
  58. i: integer;
  59. begin
  60. p:=PAnsiChar(s);
  61. for i:=1 to Length(s) do begin
  62. _AddChar(p^);
  63. Inc(p);
  64. end;
  65. end;
  66. var
  67. c: widechar;
  68. begin
  69. ws:=UTF8Decode(s);
  70. ps:=PWideChar(ws);
  71. slen:=Length(ws);
  72. dlen:=slen + 2;
  73. SetLength(Result, dlen);
  74. pd:=PAnsiChar(Result);
  75. dpos:=0;
  76. while slen > 0 do begin
  77. c:=ps^;
  78. case c of
  79. '<': _AddStr('&lt;');
  80. '>': _AddStr('&gt;');
  81. '&': _AddStr('&amp;');
  82. '''': _AddStr('&apos;');
  83. '"': _AddStr('&quot;');
  84. '\': _AddStr('\\');
  85. else
  86. if (c > #127) or (byte(c) in [9, 10, 13]) then
  87. _AddStr('&#x' + hexStr(word(c), 4) + ';')
  88. else
  89. if c < #32 then
  90. _AddStr('\x' + hexStr(byte(c), 2))
  91. else
  92. _AddChar(c);
  93. end;
  94. Inc(ps);
  95. Dec(slen);
  96. end;
  97. SetLength(Result, dpos);
  98. end;
  99. function TPpuXmlOutput.GetTagName(const n, def: string): string;
  100. begin
  101. Result:=LowerCase(n);
  102. if Result = '' then
  103. Result:=def;
  104. end;
  105. procedure TPpuXmlOutput.WriteStr(const AName, AValue: string);
  106. begin
  107. if AName = 'Type' then
  108. exit;
  109. WriteLn(Format('<%s>%s</%0:s>', [GetTagName(AName, 'value'), XmlStr(AValue)]));
  110. end;
  111. procedure TPpuXmlOutput.WriteArrayStart(const AName: string);
  112. begin
  113. if (AName = '') and (Indent = 0) then
  114. exit;
  115. WriteLn(Format('<%s>', [GetTagName(AName, 'array')]));
  116. inherited;
  117. end;
  118. procedure TPpuXmlOutput.WriteArrayEnd(const AName: string);
  119. begin
  120. if (AName = '') and (Indent = 0) then
  121. exit;
  122. inherited;
  123. WriteLn(Format('</%s>', [GetTagName(AName, 'array')]));
  124. end;
  125. procedure TPpuXmlOutput.WriteObjectStart(const AName: string; Def: TPpuDef);
  126. begin
  127. if Def = nil then
  128. WriteLn(Format('<%s>', [GetTagName(AName, 'object')]))
  129. else
  130. WriteLn(Format('<%s>', [GetTagName(Def.DefTypeName, 'object')]));
  131. inherited;
  132. end;
  133. procedure TPpuXmlOutput.WriteObjectEnd(const AName: string; Def: TPpuDef);
  134. begin
  135. inherited;
  136. if Def = nil then
  137. WriteLn(Format('</%s>', [GetTagName(AName, 'object')]))
  138. else
  139. WriteLn(Format('</%s>', [GetTagName(Def.DefTypeName, 'object')]));
  140. end;
  141. procedure TPpuXmlOutput.Init;
  142. begin
  143. inherited Init;
  144. WriteLn('<?xml version="1.0" encoding="utf-8"?>');
  145. end;
  146. end.