source: branches/DirectWeb/UXMLClasses.pas

Last change on this file was 91, checked in by george, 14 years ago
  • Upraveno: Třída TWebServer oddělena do samostatné jednotky.
File size: 4.4 KB
Line 
1unit UXMLClasses;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses Classes, SysUtils, Contnrs, StrUtils;
8
9type
10 TStringListEx = class(TStringList)
11 public
12 procedure AddNameValue(Name, Value: string);
13 end;
14
15 TXmlElement = class
16 private
17 function GetAsString: string; virtual;
18 public
19 property AsString: string read GetAsString;
20 end;
21
22 TXmlString = class(TXmlElement)
23 private
24 function GetAsString: string; override;
25 public
26 Text: string;
27 end;
28
29 TXmlTag = class(TXmlElement)
30 private
31 function GetAsString: string; override;
32 public
33 EndTagSymbol: Char;
34 TagName: string;
35 Attributes: TStringListEx;
36 SubElements: TList; // of TXmlElement;
37 constructor Create;
38 destructor Destroy; override;
39 end;
40
41 TXmlDocument = class
42 private
43 function FormatStructure(Text: string): string;
44 function GetAsString: string;
45 public
46 Formated: Boolean;
47 MainTag: TXmlTag;
48 Content: TXmlTag;
49 XmlVersion: string;
50 Encoding: string;
51 constructor Create;
52 destructor Destroy; override;
53 property AsString: string read GetAsString;
54 end;
55
56implementation
57
58{ THtmlElement }
59
60constructor TXmlTag.Create;
61begin
62 Attributes := TStringListEx.Create;
63 Attributes.NameValueSeparator := '=';
64 SubElements := TList.Create;
65 EndTagSymbol := '/';
66end;
67
68destructor TXmlTag.Destroy;
69var
70 I: Integer;
71begin
72 Attributes.Destroy;
73 for I := 0 to SubElements.Count - 1 do
74 TXmlElement(SubElements[I]).Destroy;
75 SubElements.Destroy;
76 inherited;
77end;
78
79function TXmlTag.GetAsString: string;
80var
81 AttributesText: string;
82 I: Integer;
83 Content: string;
84begin
85 Content := '';
86 for I := 0 to SubElements.Count - 1 do
87 Content := Content + TXmlElement(SubElements[I]).AsString;
88
89 AttributesText := '';
90 for I := 0 to Attributes.Count - 1 do
91 AttributesText := AttributesText + ' ' + Attributes.Names[I] + '="' + Attributes.ValueFromIndex[I] + '"';
92
93 if TagName <> '' then begin
94 if Content <> '' then
95 Result := '<' + TagName + AttributesText + '>' + Content + '<' + EndTagSymbol + TagName + '>'
96 else Result := '<' + TagName + AttributesText + EndTagSymbol + '>';
97 end else Result := Content;
98end;
99
100{ TXmlString }
101
102function TXmlString.GetAsString: string;
103begin
104 Result := Text;
105end;
106
107{ TXmlElement }
108
109function TXmlElement.GetAsString: string;
110begin
111 Result := ''; // dodelat
112end;
113
114{ TXmlDocument }
115
116constructor TXmlDocument.Create;
117begin
118 inherited;
119 Encoding := 'utf-8';
120 XmlVersion := '1.0';
121 MainTag := TXmlTag.Create;
122 with MainTag do begin
123 TagName := '?xml';
124 EndTagSymbol := '?';
125 Attributes.Add('version=1.0');
126 Attributes.Add('encoding=utf-8');
127 end;
128 Content := TXmlTag.Create;
129end;
130
131destructor TXmlDocument.Destroy;
132begin
133 Content.Destroy;
134 MainTag.Destroy;
135 inherited;
136end;
137
138function TXmlDocument.FormatStructure(Text: string): string;
139const
140 NewLine = #13#10;
141var
142 IndentCount: Integer;
143 I: Integer;
144 LastPos: Integer;
145 Content: string;
146begin
147 IndentCount := 0;
148 Result := '';
149 LastPos := 1;
150 I := 1;
151 while I < Length(Text) do begin
152 if Text[I] = '<' then begin
153 Content := Trim(Copy(Text, LastPos, I - LastPos));
154 if Length(Content) > 0 then
155 Result := Result + DupeString(' ', IndentCount) + Content + NewLine;
156 LastPos := I;
157 end;
158 if Text[I] = '>' then begin
159 if Text[LastPos + 1] = '/' then Dec(IndentCount);
160 Result := Result + DupeString(' ', IndentCount) + Copy(Text, LastPos, I - LastPos + 1)
161 + NewLine;
162 if (Text[LastPos + 1] <> '/') and (Text[I - 1] <> '/') and
163 (Text[I - 1] <> '?') and (Text[LastPos + 1] <> '!') then Inc(IndentCount);
164 LastPos := I + 1;
165 end;
166 Inc(I);
167 end;
168 if Text[LastPos + 1] = '/' then Dec(IndentCount);
169 Result := Result + DupeString(' ', IndentCount) + Copy(Text, LastPos, I - LastPos + 1)
170 + NewLine;
171end;
172
173function TXmlDocument.GetAsString: string;
174begin
175 Result := MainTag.AsString +
176 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
177 + Content.AsString;
178 if Formated then Result := FormatStructure(Result);
179end;
180
181{ TStringListEx }
182
183procedure TStringListEx.AddNameValue(Name, Value: string);
184begin
185 Add(Name + NameValueSeparator + Value);
186end;
187
188end.
Note: See TracBrowser for help on using the repository browser.