source: trunk/Packages/CoolWeb/Common/UXmlClasses.pas

Last change on this file was 5, checked in by chronos, 12 years ago
  • Added: Required packages.
File size: 4.0 KB
Line 
1unit UXmlClasses;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses Classes, SysUtils, StrUtils, SpecializedList,
8 SpecializedDictionary;
9
10type
11 TXmlElement = class
12 private
13 function GetAsString: string; virtual;
14 public
15 property AsString: string read GetAsString;
16 end;
17
18 TXmlString = class(TXmlElement)
19 private
20 function GetAsString: string; override;
21 public
22 Text: string;
23 end;
24
25 TXmlTag = class(TXmlElement)
26 private
27 function GetAsString: string; override;
28 public
29 EndTagSymbol: string;
30 ShringEmpty: Boolean;
31 Name: string;
32 Attributes: TDictionaryStringString;
33 SubElements: TListObject; // TListObject<TXmlElement>;
34 constructor Create;
35 destructor Destroy; override;
36 end;
37
38 TXmlDocument = class
39 private
40 MainTag: TXmlTag;
41 function FormatStructure(Text: string): string;
42 function GetAsString: string;
43 public
44 Formated: Boolean;
45 Content: TXmlTag;
46 XmlVersion: string;
47 Encoding: string;
48 constructor Create;
49 destructor Destroy; override;
50 property AsString: string read GetAsString;
51 end;
52
53implementation
54
55{ THtmlElement }
56
57constructor TXmlTag.Create;
58begin
59 ShringEmpty := True;
60 Attributes := TDictionaryStringString.Create;
61 SubElements := TListObject.Create;
62 EndTagSymbol := '/';
63end;
64
65destructor TXmlTag.Destroy;
66begin
67 Attributes.Free;
68 SubElements.Free;
69 inherited;
70end;
71
72function TXmlTag.GetAsString: string;
73var
74 AttributesText: string;
75 I: Integer;
76 Content: string;
77begin
78 Content := '';
79 for I := 0 to SubElements.Count - 1 do
80 Content := Content + TXmlElement(SubElements[I]).AsString;
81
82 AttributesText := '';
83 for I := 0 to Attributes.Count - 1 do
84 AttributesText := AttributesText + ' ' + Attributes.Keys[I] + '="' + Attributes[I].Value + '"';
85
86 if Name <> '' then begin
87 if (Content <> '') or not ShringEmpty then
88 Result := '<' + Name + AttributesText + '>' + Content + '<' + EndTagSymbol + Name + '>'
89 else Result := '<' + Name + AttributesText + EndTagSymbol + '>';
90 end else Result := Content;
91end;
92
93{ TXmlString }
94
95function TXmlString.GetAsString: string;
96begin
97 Result := Text;
98end;
99
100{ TXmlElement }
101
102function TXmlElement.GetAsString: string;
103begin
104 Result := ''; // dodelat
105end;
106
107{ TXmlDocument }
108
109constructor TXmlDocument.Create;
110begin
111 inherited;
112 Encoding := 'utf-8';
113 XmlVersion := '1.0';
114 MainTag := TXmlTag.Create;
115 with MainTag do begin
116 Name := '?xml';
117 EndTagSymbol := '?';
118 Attributes.Add('version', '1.0');
119 Attributes.Add('encoding', 'utf-8');
120 end;
121 Content := TXmlTag.Create;
122end;
123
124destructor TXmlDocument.Destroy;
125begin
126 Content.Free;
127 MainTag.Free;
128 inherited;
129end;
130
131function TXmlDocument.FormatStructure(Text: string): string;
132const
133 NewLine = #13#10;
134var
135 IndentCount: Integer;
136 I: Integer;
137 LastPos: Integer;
138 Content: string;
139begin
140 IndentCount := 0;
141 Result := '';
142 LastPos := 1;
143 I := 1;
144 while I < Length(Text) do begin
145 if Text[I] = '<' then begin
146 Content := Trim(Copy(Text, LastPos, I - LastPos));
147 if Length(Content) > 0 then
148 Result := Result + DupeString(' ', IndentCount) + Content + NewLine;
149 LastPos := I;
150 end;
151 if Text[I] = '>' then begin
152 if Text[LastPos + 1] = '/' then Dec(IndentCount);
153 Result := Result + DupeString(' ', IndentCount) + Copy(Text, LastPos, I - LastPos + 1)
154 + NewLine;
155 if (Text[LastPos + 1] <> '/') and (Text[I - 1] <> '/') and
156 (Text[I - 1] <> '?') and (Text[LastPos + 1] <> '!') then Inc(IndentCount);
157 LastPos := I + 1;
158 end;
159 Inc(I);
160 end;
161 if Text[LastPos + 1] = '/' then Dec(IndentCount);
162 Result := Result + DupeString(' ', IndentCount) + Copy(Text, LastPos, I - LastPos + 1)
163 + NewLine;
164end;
165
166function TXmlDocument.GetAsString: string;
167begin
168 Result := MainTag.AsString + Content.AsString;
169 if Formated then Result := FormatStructure(Result);
170end;
171
172end.
Note: See TracBrowser for help on using the repository browser.