source: branches/xpascal/Generators/GeneratorXml.pas

Last change on this file was 233, checked in by chronos, 17 months ago
  • Added: Support for procedures.
  • Added: Project pascal file can be opened from main menu. Last file name is remembered.
  • Modified: Improved XML output of source structure.
File size: 2.2 KB
Line 
1unit GeneratorXml;
2
3interface
4
5uses
6 Classes, SysUtils, Source, SourceNode, Generator;
7
8type
9 { TGeneratorXml }
10
11 TGeneratorXml = class(TGenerator)
12 private
13 procedure GenerateNodes(SourceNodes: TSourceNodeList<TSourceNode>;
14 NodeName: string);
15 procedure GenerateNode(SourceNode: TSourceNode; NodeName: string);
16 public
17 procedure Generate; override;
18 constructor Create; override;
19 end;
20
21
22implementation
23
24resourcestring
25 SUnsupportedNodeType = 'Unsupported node type';
26
27{ TGeneratorXml }
28
29procedure TGeneratorXml.GenerateNodes(SourceNodes: TSourceNodeList<TSourceNode>;
30 NodeName: string);
31var
32 I: Integer;
33begin
34 for I := 0 to SourceNodes.Count - 1 do begin
35 if SourceNodes[I] is TSourceNode then begin
36 GenerateNode(TSourceNode(SourceNodes[I]), NodeName);
37 end else raise Exception.Create(SUnsupportedNodeType);
38 end;
39end;
40
41procedure TGeneratorXml.GenerateNode(SourceNode: TSourceNode; NodeName: string);
42var
43 I: Integer;
44 Field: TField;
45 Obj: TObject;
46 List: TSourceNodeList<TSourceNode>;
47begin
48 if SourceNode is TSourceNode then begin
49 AddTextLine('<' + NodeName + '>');
50 Indent := Indent + 1;
51 for I := 0 to SourceNode.FieldsCount - 1 do begin
52 Field := SourceNode.GetField(I);
53 try
54 if Field.DataType = dtObject then begin
55 Obj := SourceNode.GetValueObject(I);
56 if Obj is TSourceNode then
57 GenerateNode(TSourceNode(Obj), Field.Name);
58 end else
59 if Field.DataType = dtList then begin
60 SourceNode.GetValue(I, List);
61 if List.Count > 0 then
62 GenerateNodes(List, Field.Name);
63 end else
64 if Field.DataType = dtString then begin
65 if SourceNode.GetValueString(I) <> '' then
66 AddTextLine('<' + Field.Name + '>' + SourceNode.GetValueString(I) +
67 '</' + Field.Name + '>');
68 end;
69 finally
70 Field.Free;
71 end;
72 end;
73 Indent := Indent - 1;
74 AddTextLine('</' + NodeName + '>');
75 end else
76 raise Exception.Create(SUnsupportedNodeType);
77end;
78
79procedure TGeneratorXml.Generate;
80begin
81 AddTextLine('<?xml version="1.0" encoding="UTF-8"?>');
82 GenerateNode(Prog, 'Program');
83end;
84
85constructor TGeneratorXml.Create;
86begin
87 inherited;
88 Name := 'XML';
89 FileExt := '.xml';
90end;
91
92end.
93
Note: See TracBrowser for help on using the repository browser.