forked from jmpessoa/lazandroidmodulewizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroidXMLResString.pas
189 lines (157 loc) · 3.97 KB
/
AndroidXMLResString.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
unit AndroidXMLResString;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, laz2_XMLWrite, laz2_XMLRead, laz2_Dom;
Type
{
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello!</string>
<string name="Toto">toto!</string>
</resources>
}
TXMLResString = class
private
FSaved: Boolean;
function GetCount: integer;
function GetValueFromIndex(AIndex: integer): string;
procedure SetValueFromIndex(AIndex: integer; AValue: string);
function GetNameFromIndex(AIndex: integer): string;
procedure SetNameFromIndex(AIndex: integer; AValue: string);
protected
FFileName: string;
FStringRes: TStringList;
public
constructor Create(AFileName: string = '');
destructor Destroy; override;
procedure Open; overload;
procedure Open(AFileName: string); overload;
procedure Save;
function Add(AName, AValue: string): integer;
function FindByName(AName: string): integer;
procedure Delete(AIndex: integer);
property Count: integer read GetCount;
property Saved: boolean read FSaved;
property ValueFromIndex[AIndex: Integer]: string read GetValueFromIndex write SetValueFromIndex;
property NameFromIndex[Index: Integer]: string read GetNameFromIndex write SetNameFromIndex;
end;
implementation
{ TXMLResString }
procedure TXMLResString.Delete(AIndex: integer);
begin
FStringRes.Delete(AIndex);
end;
function TXMLResString.FindByName(AName: string): integer;
Var
i: integer;
begin
Result:= -1;
for i:= 0 to FStringRes.Count -1 do
begin
if FStringRes.Names[i] = AName then
begin
Result:= i;
Break;
end;
end;
end;
function TXMLResString.Add(AName, AValue: string): integer;
begin
Result:= -1;
if FindByName(AName) > -1 then
Exit
else
Result:= FStringRes.Add(AName + '=' + AValue);
FSaved:= False;
end;
procedure TXMLResString.SetNameFromIndex(AIndex: integer; AValue: string);
begin
FStringRes.Strings[AIndex]:= AValue + '=' + FStringRes.ValueFromIndex[AIndex];
FSaved:= False;
end;
function TXMLResString.GetNameFromIndex(AIndex: integer): string;
begin
Result:= FStringRes.Names[AIndex];
end;
procedure TXMLResString.SetValueFromIndex(AIndex: integer; AValue: string);
begin
FStringRes.ValueFromIndex[AIndex]:= AValue;
FSaved:= False;
end;
function TXMLResString.GetValueFromIndex(AIndex: integer): string;
begin
Result:= FStringRes.ValueFromIndex[AIndex];
end;
function TXMLResString.GetCount: integer;
begin
Result:= FStringRes.Count;
end;
procedure TXMLResString.Open(AFileName: string);
begin
FFileName:= AFileName;
Open;
end;
procedure TXMLResString.Open;
Var
Res: TDOMNode;
XmlRes: TXMLDocument;
begin
if Pos('strings.xml', FFileName) > 0 then
begin
ReadXMLFile(XmlRes, FFileName);
try
FStringRes.Clear;
Res:= XmlRes.DocumentElement.FirstChild;
while Assigned(Res) do
begin
FStringRes.Add(Res.Attributes.Item[0].NodeValue + '=' + Res.ChildNodes.Item[0].NodeValue);
Res:= Res.NextSibling;
end;
FSaved:= True;
finally
XmlRes.Free
end;
end;
//TODO:
(*
if Pos('AndroidManifest.xml', FFileName) > 0 then
begin
end;
if Pos('build.xml', FFileName) > 0 then
begin
end;
*)
end;
procedure TXMLResString.Save;
Var
NewXml: TXMLDocument;
Child: TDOMNode;
NewRes: TDOMElement;
i: integer;
begin
NewXml:= TXMLDocument.Create;
Child:= NewXml.CreateElement('resources');
NewXml.AppendChild(Child);
for i:= 0 to FStringRes.Count -1 do
begin
NewRes:= NewXml.CreateElement('string');
NewRes.SetAttribute('name', FStringRes.Names[i]);
NewRes.AppendChild(NewXml.CreateTextNode(FStringRes.ValueFromIndex[i]));
Child.AppendChild(NewRes);
end;
WriteXMLFile(NewXml, FFileName);
NewXml.Free;
FSaved:= True;
end;
constructor TXMLResString.Create(AFileName: string = '');
begin
FStringRes:= TStringList.Create;
if AFileName <> '' then
FFileName:= AFileName;
end;
destructor TXMLResString.Destroy;
begin
FStringRes.Free;
end;
end.