source: branches/lazarus/UXmlClasses.pas

Last change on this file was 61, checked in by george, 15 years ago
  • Přidáno: Další chybějící soubory s vývojové větve ve Free Pascalu.
  • Property svn:executable set to *
File size: 4.3 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.Free;
73 for I := 0 to SubElements.Count - 1 do TXmlElement(SubElements[I]).Free;
74 SubElements.Free;
75 inherited;
76end;
77
78function TXmlTag.GetAsString: string;
79var
80 AttributesText: string;
81 I: Integer;
82 Content: string;
83begin
84 Content := '';
85 for I := 0 to SubElements.Count - 1 do
86 Content := Content + TXmlElement(SubElements[I]).AsString;
87
88 AttributesText := '';
89 for I := 0 to Attributes.Count - 1 do
90 AttributesText := AttributesText + ' ' + Attributes.Names[I] + '="' + Attributes.ValueFromIndex[I] + '"';
91
92 if TagName <> '' then begin
93 if Content <> '' then
94 Result := '<' + TagName + AttributesText + '>' + Content + '<' + EndTagSymbol + TagName + '>'
95 else Result := '<' + TagName + AttributesText + EndTagSymbol + '>';
96 end else Result := Content;
97end;
98
99{ TXmlString }
100
101function TXmlString.GetAsString: string;
102begin
103 Result := Text;
104end;
105
106{ TXmlElement }
107
108function TXmlElement.GetAsString: string;
109begin
110 Result := ''; // dodelat
111end;
112
113{ TXmlDocument }
114
115constructor TXmlDocument.Create;
116begin
117 inherited;
118 Encoding := 'windows-1250';
119 XmlVersion := '1.0';
120 MainTag := TXmlTag.Create;
121 with MainTag do begin
122 TagName := '?xml';
123 EndTagSymbol := '?';
124 Attributes.Add('version=1.0');
125 Attributes.Add('encoding=windows-1250');
126 end;
127 Content := TXmlTag.Create;
128end;
129
130destructor TXmlDocument.Destroy;
131begin
132 Content.Free;
133 MainTag.Free;
134 inherited;
135end;
136
137function TXmlDocument.FormatStructure(Text: string): string;
138const
139 NewLine = #13#10;
140var
141 IndentCount: Integer;
142 I: Integer;
143 LastPos: Integer;
144 Content: string;
145begin
146 IndentCount := 0;
147 Result := '';
148 LastPos := 1;
149 I := 1;
150 while I < Length(Text) do begin
151 if Text[I] = '<' then begin
152 Content := Trim(Copy(Text, LastPos, I - LastPos));
153 if Length(Content) > 0 then
154 Result := Result + DupeString(' ', IndentCount) + Content + NewLine;
155 LastPos := I;
156 end;
157 if Text[I] = '>' then begin
158 if Text[LastPos + 1] = '/' then Dec(IndentCount);
159 Result := Result + DupeString(' ', IndentCount) + Copy(Text, LastPos, I - LastPos + 1)
160 + NewLine;
161 if (Text[LastPos + 1] <> '/') and (Text[I - 1] <> '/') and
162 (Text[I - 1] <> '?') and (Text[LastPos + 1] <> '!') then Inc(IndentCount);
163 LastPos := I + 1;
164 end;
165 Inc(I);
166 end;
167 if Text[LastPos + 1] = '/' then Dec(IndentCount);
168 Result := Result + DupeString(' ', IndentCount) + Copy(Text, LastPos, I - LastPos + 1)
169 + NewLine;
170end;
171
172function TXmlDocument.GetAsString: string;
173begin
174 Result := MainTag.AsString +
175 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
176 + Content.AsString;
177 if Formated then Result := FormatStructure(Result);
178end;
179
180{ TStringListEx }
181
182procedure TStringListEx.AddNameValue(Name, Value: string);
183begin
184 Add(Name + NameValueSeparator + Value);
185end;
186
187end.
Note: See TracBrowser for help on using the repository browser.