| 1 | unit UXmlClasses;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses Classes, SysUtils, Contnrs, StrUtils;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 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 |
|
|---|
| 56 | implementation
|
|---|
| 57 |
|
|---|
| 58 | { THtmlElement }
|
|---|
| 59 |
|
|---|
| 60 | constructor TXmlTag.Create;
|
|---|
| 61 | begin
|
|---|
| 62 | Attributes := TStringListEx.Create;
|
|---|
| 63 | Attributes.NameValueSeparator := '=';
|
|---|
| 64 | SubElements := TList.Create;
|
|---|
| 65 | EndTagSymbol := '/';
|
|---|
| 66 | end;
|
|---|
| 67 |
|
|---|
| 68 | destructor TXmlTag.Destroy;
|
|---|
| 69 | var
|
|---|
| 70 | I: Integer;
|
|---|
| 71 | begin
|
|---|
| 72 | Attributes.Free;
|
|---|
| 73 | for I := 0 to SubElements.Count - 1 do TXmlElement(SubElements[I]).Free;
|
|---|
| 74 | SubElements.Free;
|
|---|
| 75 | inherited;
|
|---|
| 76 | end;
|
|---|
| 77 |
|
|---|
| 78 | function TXmlTag.GetAsString: string;
|
|---|
| 79 | var
|
|---|
| 80 | AttributesText: string;
|
|---|
| 81 | I: Integer;
|
|---|
| 82 | Content: string;
|
|---|
| 83 | begin
|
|---|
| 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;
|
|---|
| 97 | end;
|
|---|
| 98 |
|
|---|
| 99 | { TXmlString }
|
|---|
| 100 |
|
|---|
| 101 | function TXmlString.GetAsString: string;
|
|---|
| 102 | begin
|
|---|
| 103 | Result := Text;
|
|---|
| 104 | end;
|
|---|
| 105 |
|
|---|
| 106 | { TXmlElement }
|
|---|
| 107 |
|
|---|
| 108 | function TXmlElement.GetAsString: string;
|
|---|
| 109 | begin
|
|---|
| 110 | Result := ''; // dodelat
|
|---|
| 111 | end;
|
|---|
| 112 |
|
|---|
| 113 | { TXmlDocument }
|
|---|
| 114 |
|
|---|
| 115 | constructor TXmlDocument.Create;
|
|---|
| 116 | begin
|
|---|
| 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;
|
|---|
| 128 | end;
|
|---|
| 129 |
|
|---|
| 130 | destructor TXmlDocument.Destroy;
|
|---|
| 131 | begin
|
|---|
| 132 | Content.Free;
|
|---|
| 133 | MainTag.Free;
|
|---|
| 134 | inherited;
|
|---|
| 135 | end;
|
|---|
| 136 |
|
|---|
| 137 | function TXmlDocument.FormatStructure(Text: string): string;
|
|---|
| 138 | const
|
|---|
| 139 | NewLine = #13#10;
|
|---|
| 140 | var
|
|---|
| 141 | IndentCount: Integer;
|
|---|
| 142 | I: Integer;
|
|---|
| 143 | LastPos: Integer;
|
|---|
| 144 | Content: string;
|
|---|
| 145 | begin
|
|---|
| 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;
|
|---|
| 170 | end;
|
|---|
| 171 |
|
|---|
| 172 | function TXmlDocument.GetAsString: string;
|
|---|
| 173 | begin
|
|---|
| 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);
|
|---|
| 178 | end;
|
|---|
| 179 |
|
|---|
| 180 | { TStringListEx }
|
|---|
| 181 |
|
|---|
| 182 | procedure TStringListEx.AddNameValue(Name, Value: string);
|
|---|
| 183 | begin
|
|---|
| 184 | Add(Name + NameValueSeparator + Value);
|
|---|
| 185 | end;
|
|---|
| 186 |
|
|---|
| 187 | end.
|
|---|