| 1 | unit UXmlClasses;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses Classes, SysUtils, StrUtils, SpecializedList,
|
|---|
| 8 | SpecializedDictionary;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 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 |
|
|---|
| 53 | implementation
|
|---|
| 54 |
|
|---|
| 55 | { THtmlElement }
|
|---|
| 56 |
|
|---|
| 57 | constructor TXmlTag.Create;
|
|---|
| 58 | begin
|
|---|
| 59 | ShringEmpty := True;
|
|---|
| 60 | Attributes := TDictionaryStringString.Create;
|
|---|
| 61 | SubElements := TListObject.Create;
|
|---|
| 62 | EndTagSymbol := '/';
|
|---|
| 63 | end;
|
|---|
| 64 |
|
|---|
| 65 | destructor TXmlTag.Destroy;
|
|---|
| 66 | begin
|
|---|
| 67 | Attributes.Free;
|
|---|
| 68 | SubElements.Free;
|
|---|
| 69 | inherited;
|
|---|
| 70 | end;
|
|---|
| 71 |
|
|---|
| 72 | function TXmlTag.GetAsString: string;
|
|---|
| 73 | var
|
|---|
| 74 | AttributesText: string;
|
|---|
| 75 | I: Integer;
|
|---|
| 76 | Content: string;
|
|---|
| 77 | begin
|
|---|
| 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;
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | { TXmlString }
|
|---|
| 94 |
|
|---|
| 95 | function TXmlString.GetAsString: string;
|
|---|
| 96 | begin
|
|---|
| 97 | Result := Text;
|
|---|
| 98 | end;
|
|---|
| 99 |
|
|---|
| 100 | { TXmlElement }
|
|---|
| 101 |
|
|---|
| 102 | function TXmlElement.GetAsString: string;
|
|---|
| 103 | begin
|
|---|
| 104 | Result := ''; // dodelat
|
|---|
| 105 | end;
|
|---|
| 106 |
|
|---|
| 107 | { TXmlDocument }
|
|---|
| 108 |
|
|---|
| 109 | constructor TXmlDocument.Create;
|
|---|
| 110 | begin
|
|---|
| 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;
|
|---|
| 122 | end;
|
|---|
| 123 |
|
|---|
| 124 | destructor TXmlDocument.Destroy;
|
|---|
| 125 | begin
|
|---|
| 126 | Content.Free;
|
|---|
| 127 | MainTag.Free;
|
|---|
| 128 | inherited;
|
|---|
| 129 | end;
|
|---|
| 130 |
|
|---|
| 131 | function TXmlDocument.FormatStructure(Text: string): string;
|
|---|
| 132 | const
|
|---|
| 133 | NewLine = #13#10;
|
|---|
| 134 | var
|
|---|
| 135 | IndentCount: Integer;
|
|---|
| 136 | I: Integer;
|
|---|
| 137 | LastPos: Integer;
|
|---|
| 138 | Content: string;
|
|---|
| 139 | begin
|
|---|
| 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;
|
|---|
| 164 | end;
|
|---|
| 165 |
|
|---|
| 166 | function TXmlDocument.GetAsString: string;
|
|---|
| 167 | begin
|
|---|
| 168 | Result := MainTag.AsString + Content.AsString;
|
|---|
| 169 | if Formated then Result := FormatStructure(Result);
|
|---|
| 170 | end;
|
|---|
| 171 |
|
|---|
| 172 | end.
|
|---|