| 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.Destroy;
|
|---|
| 73 | for I := 0 to SubElements.Count - 1 do
|
|---|
| 74 | TXmlElement(SubElements[I]).Destroy;
|
|---|
| 75 | SubElements.Destroy;
|
|---|
| 76 | inherited;
|
|---|
| 77 | end;
|
|---|
| 78 |
|
|---|
| 79 | function TXmlTag.GetAsString: string;
|
|---|
| 80 | var
|
|---|
| 81 | AttributesText: string;
|
|---|
| 82 | I: Integer;
|
|---|
| 83 | Content: string;
|
|---|
| 84 | begin
|
|---|
| 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;
|
|---|
| 98 | end;
|
|---|
| 99 |
|
|---|
| 100 | { TXmlString }
|
|---|
| 101 |
|
|---|
| 102 | function TXmlString.GetAsString: string;
|
|---|
| 103 | begin
|
|---|
| 104 | Result := Text;
|
|---|
| 105 | end;
|
|---|
| 106 |
|
|---|
| 107 | { TXmlElement }
|
|---|
| 108 |
|
|---|
| 109 | function TXmlElement.GetAsString: string;
|
|---|
| 110 | begin
|
|---|
| 111 | Result := ''; // dodelat
|
|---|
| 112 | end;
|
|---|
| 113 |
|
|---|
| 114 | { TXmlDocument }
|
|---|
| 115 |
|
|---|
| 116 | constructor TXmlDocument.Create;
|
|---|
| 117 | begin
|
|---|
| 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;
|
|---|
| 129 | end;
|
|---|
| 130 |
|
|---|
| 131 | destructor TXmlDocument.Destroy;
|
|---|
| 132 | begin
|
|---|
| 133 | Content.Destroy;
|
|---|
| 134 | MainTag.Destroy;
|
|---|
| 135 | inherited;
|
|---|
| 136 | end;
|
|---|
| 137 |
|
|---|
| 138 | function TXmlDocument.FormatStructure(Text: string): string;
|
|---|
| 139 | const
|
|---|
| 140 | NewLine = #13#10;
|
|---|
| 141 | var
|
|---|
| 142 | IndentCount: Integer;
|
|---|
| 143 | I: Integer;
|
|---|
| 144 | LastPos: Integer;
|
|---|
| 145 | Content: string;
|
|---|
| 146 | begin
|
|---|
| 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;
|
|---|
| 171 | end;
|
|---|
| 172 |
|
|---|
| 173 | function TXmlDocument.GetAsString: string;
|
|---|
| 174 | begin
|
|---|
| 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);
|
|---|
| 179 | end;
|
|---|
| 180 |
|
|---|
| 181 | { TStringListEx }
|
|---|
| 182 |
|
|---|
| 183 | procedure TStringListEx.AddNameValue(Name, Value: string);
|
|---|
| 184 | begin
|
|---|
| 185 | Add(Name + NameValueSeparator + Value);
|
|---|
| 186 | end;
|
|---|
| 187 |
|
|---|
| 188 | end.
|
|---|