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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
{
Copyright 2014-2021 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Services (on Android and iOS) definitions (parameters) in CastleEngineManifest. }
unit ToolServices;
interface
uses SysUtils, Generics.Collections, DOM,
CastleUtils, CastleStringUtils;
type
TService = class
private
FParameters: TStringStringMap;
FName: string;
public
constructor Create(const Name: string = '');
destructor Destroy; override;
procedure ReadCastleEngineManifest(const Element: TDOMElement);
property Name: string read FName;
property Parameters: TStringStringMap read FParameters;
end;
TServiceList = class({$ifdef FPC}specialize{$endif} TObjectList<TService>)
public
procedure ReadCastleEngineManifest(const Element: TDOMElement);
function HasService(const Name: string): boolean;
procedure AddService(const Name: string);
{ Find service by name.
Returns @nil if not found.
You can check HasService earlier, if HasService('xxx') = @true then Service('xxx')
will never be @nil. }
function Service(const Name: string): TService;
end;
{ Find in Element all children called <parameter>,
read them and add to Parameters list,
expecting this format:
<parameter key="my_key" value="my_value" />
or
<parameter key="my_key">my_value</parameter>
or
<parameter key="my_key"><![CDATA[my_value]]></parameter>
Note that keys are converted to lowercase.
Parameter keys, just like macro names, are not case-sensitive.
All the keys in RequiredKeys are guaranteed to have a value set.
If they are not specified in Element (or Element is @nil),
they will have an empty value.
This is necessary if you want to use this macro in template *even*
when user doesn't specify it.
}
procedure ReadParameters(const Element: TDOMElement; const Parameters: TStringStringMap;
const RequiredKeys: array of String);
implementation
uses Classes, XMLRead, XMLWrite,
CastleXmlUtils, CastleUriUtils, CastleFilesUtils;
{ internal utils ------------------------------------------------------------- }
function GetCData(const Element: TDOMElement): String;
var
I: TXMLCDataIterator;
begin
Result := '';
I := TXMLCDataIterator.Create(Element);
try
while I.GetNext do
Result := Result + I.Current;
finally FreeAndNil(I) end;
end;
procedure ReadParameters(const Element: TDOMElement; const Parameters: TStringStringMap;
const RequiredKeys: array of String);
var
ChildElements: TXMLElementIterator;
ChildElement: TDOMElement;
Key, Value, KeyLower: string;
begin
if Element <> nil then
begin
ChildElements := Element.ChildrenIterator('parameter');
try
while ChildElements.GetNext do
begin
ChildElement := ChildElements.Current;
Key := LowerCase(ChildElement.AttributeString('key'));
if Key = '' then
raise Exception.Create('Key for <parameter> is empty in CastleEngineManifest.xml');
if ChildElement.HasAttribute('value') then
Value := ChildElement.AttributeString('value')
else
begin
Value := ChildElement.TextData;
if Value = '' then
Value := GetCData(ChildElement);
{ value cannot be empty in this case }
if Value = '' then
raise Exception.CreateFmt('No value for key "%s" specified in CastleEngineManifest.xml', [Key]);
end;
Parameters.Add(Key, Value);
end;
finally FreeAndNil(ChildElements) end;
end;
for Key in RequiredKeys do
begin
KeyLower := LowerCase(Key);
if not Parameters.ContainsKey(KeyLower) then
Parameters.Add(KeyLower, '');
end;
end;
{ TService ------------------------------------------------------------------- }
constructor TService.Create(const Name: string);
begin
inherited Create;
FParameters := TStringStringMap.Create;
FName := Name;
end;
destructor TService.Destroy;
begin
FreeAndNil(FParameters);
inherited;
end;
procedure TService.ReadCastleEngineManifest(const Element: TDOMElement);
begin
FName := Element.AttributeString('name');
ReadParameters(Element, Parameters, []);
end;
{ TServiceList ------------------------------------------------------ }
procedure TServiceList.ReadCastleEngineManifest(const Element: TDOMElement);
var
ChildElements: TXMLElementIterator;
ChildElement: TDOMElement;
NewService: TService;
begin
ChildElements := Element.ChildrenIterator('component'); // parse deprecated name 'component'
try
while ChildElements.GetNext do
begin
ChildElement := ChildElements.Current;
NewService := TService.Create;
Add(NewService);
NewService.ReadCastleEngineManifest(ChildElement);
end;
finally FreeAndNil(ChildElements) end;
ChildElements := Element.ChildrenIterator('service');
try
while ChildElements.GetNext do
begin
ChildElement := ChildElements.Current;
NewService := TService.Create;
Add(NewService);
NewService.ReadCastleEngineManifest(ChildElement);
end;
finally FreeAndNil(ChildElements) end;
end;
function TServiceList.HasService(const Name: string): boolean;
var
I: Integer;
begin
for I := 0 to Count - 1 do
if Items[I].Name = Name then
Exit(true);
Result := false;
end;
function TServiceList.Service(const Name: string): TService;
var
I: Integer;
begin
for I := 0 to Count - 1 do
if Items[I].Name = Name then
Exit(Items[I]);
Result := nil;
end;
procedure TServiceList.AddService(const Name: string);
var
NewService: TService;
begin
if not HasService(Name) then
begin
NewService := TService.Create(Name);
Add(NewService);
end;
end;
end.
|