1 | unit GeneratorXml;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Source, SourceNode, Generator;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
22 | implementation
|
---|
23 |
|
---|
24 | resourcestring
|
---|
25 | SUnsupportedNodeType = 'Unsupported node type';
|
---|
26 |
|
---|
27 | { TGeneratorXml }
|
---|
28 |
|
---|
29 | procedure TGeneratorXml.GenerateNodes(SourceNodes: TSourceNodeList<TSourceNode>;
|
---|
30 | NodeName: string);
|
---|
31 | var
|
---|
32 | I: Integer;
|
---|
33 | begin
|
---|
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;
|
---|
39 | end;
|
---|
40 |
|
---|
41 | procedure TGeneratorXml.GenerateNode(SourceNode: TSourceNode; NodeName: string);
|
---|
42 | var
|
---|
43 | I: Integer;
|
---|
44 | Field: TField;
|
---|
45 | Obj: TObject;
|
---|
46 | List: TSourceNodeList<TSourceNode>;
|
---|
47 | begin
|
---|
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);
|
---|
77 | end;
|
---|
78 |
|
---|
79 | procedure TGeneratorXml.Generate;
|
---|
80 | begin
|
---|
81 | AddTextLine('<?xml version="1.0" encoding="UTF-8"?>');
|
---|
82 | GenerateNode(Prog, 'Program');
|
---|
83 | end;
|
---|
84 |
|
---|
85 | constructor TGeneratorXml.Create;
|
---|
86 | begin
|
---|
87 | inherited;
|
---|
88 | Name := 'XML';
|
---|
89 | FileExt := '.xml';
|
---|
90 | end;
|
---|
91 |
|
---|
92 | end.
|
---|
93 |
|
---|