1
0
mirror of synced 2024-11-21 05:33:50 +00:00

Enumerator support for node collections

This commit is contained in:
Mark van Renswoude 2013-06-21 11:05:00 +00:00
parent cc62e18e4e
commit 1e12269490
3 changed files with 136 additions and 4 deletions

View File

@ -46,6 +46,21 @@ type
end;
TXMLNodeCollectionEnumerator = class(TInterfacedObject)
private
FNodeCollection: IXMLNodeCollection;
FIndex: Integer;
public
constructor Create(ANodeCollection: IXMLNodeCollection);
function GetCurrent: IXMLNode;
function MoveNext: Boolean; virtual;
property Current: IXMLNode read GetCurrent;
end;
const
AllTimeFragments = [Low(TXMLTimeFragment)..High(TXMLTimeFragment)];
@ -160,6 +175,33 @@ end;
{ TXMLNodeCollectionEnumerator }
constructor TXMLNodeCollectionEnumerator.Create(ANodeCollection: IXMLNodeCollection);
begin
inherited Create;
FNodeCollection := ANodeCollection;
FIndex := -1;
end;
function TXMLNodeCollectionEnumerator.GetCurrent: IXMLNode;
begin
if (FIndex >= 0) and (FIndex < FNodeCollection.Count) then
Result := FNodeCollection.Nodes[FIndex]
else
Result := nil;
end;
function TXMLNodeCollectionEnumerator.MoveNext: Boolean;
begin
Inc(FIndex);
Result := (FIndex < FNodeCollection.Count);
end;
function DateTimeToXML(ADate: TDateTime; AFormat: TXMLDateTimeFormat; ATimeFragments: TXMLTimeFragments): string;
var
formatSettings: TFormatSettings;

View File

@ -69,6 +69,8 @@ type
procedure WriteSchemaEnumerationArray(AStream: TStreamHelper; AItem: TXMLDataBindingEnumeration);
procedure WriteValidate(AStream: TStreamHelper; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
procedure WriteEnumeratorMethod(AStream: TStreamHelper; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
procedure WriteEnumerator(AStream: TStreamHelper; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
function GetDelphiNodeType(AProperty: TXMLDataBindingProperty): TDelphiNodeType;
function GetDelphiElementType(AProperty: TXMLDataBindingProperty): TDelphiElementType;
@ -695,8 +697,10 @@ begin
if Assigned(AItem.BaseItem) then
parent := PrefixInterface + AItem.BaseItem.TranslatedName
else if AItem.IsCollection then
parent := CollectionInterface
else
begin
parent := CollectionInterface;
WriteEnumerator(AStream, AItem, ASection);
end else
parent := ItemInterface;
@ -717,8 +721,10 @@ begin
if Assigned(AItem.BaseItem) then
parent := PrefixClass + AItem.BaseItem.TranslatedName
else if AItem.IsCollection then
parent := CollectionClass
else
begin
parent := CollectionClass;
WriteEnumerator(AStream, AItem, ASection);
end else
parent := ItemClass;
@ -738,6 +744,7 @@ begin
dxsImplementation:
begin
WriteEnumerator(AStream, AItem, ASection);
WriteSchemaInterfaceProperties(AStream, AItem, ASection);
end;
end;
@ -937,6 +944,7 @@ begin
AStream.WriteLn(' protected');
WriteValidate(AStream, AItem, ASection);
WriteEnumeratorMethod(AStream, AItem, ASection);
hasMembers := WriteSchemaInterfaceCollectionProperties(AStream, AItem, ASection);
for member := Low(TDelphiXMLMember) to High(TDelphiXMLMember) do
@ -1594,6 +1602,63 @@ begin
end;
procedure TDelphiXMLDataBindingGenerator.WriteEnumeratorMethod(AStream: TStreamHelper; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
begin
if not AItem.IsCollection then
Exit;
case ASection of
dxsInterface,
dxsClass:
begin
AStream.WriteLnNamedFmt(EnumeratorMethodInterface,
['Name', AItem.TranslatedName]);
AStream.WriteLn('');
end;
dxsImplementation:
begin
AStream.WriteLnNamedFmt(EnumeratorMethodImplementation,
['Name', AItem.TranslatedName]);
end;
end;
end;
procedure TDelphiXMLDataBindingGenerator.WriteEnumerator(AStream: TStreamHelper; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
begin
if not AItem.IsCollection then
Exit;
case ASection of
dxsInterface:
begin
AStream.WriteLnNamedFmt(EnumeratorInterface,
['Name', AItem.TranslatedName,
'ItemName', AItem.CollectionItem.TranslatedName,
'GUID', CreateNewGUID]);
AStream.WriteLn('');
end;
dxsClass:
begin
AStream.WriteLnNamedFmt(EnumeratorClass,
['Name', AItem.TranslatedName,
'ItemName', AItem.CollectionItem.TranslatedName]);
AStream.WriteLn('');
end;
dxsImplementation:
begin
AStream.WriteLnNamedFmt(EnumeratorImplementation,
['Name', AItem.TranslatedName,
'ItemName', AItem.CollectionItem.TranslatedName]);
end;
end;
end;
function TDelphiXMLDataBindingGenerator.GetDelphiNodeType(AProperty: TXMLDataBindingProperty): TDelphiNodeType;
begin
if AProperty.IsAttribute then

View File

@ -98,6 +98,31 @@ const
XSDValidateMethodImplementationEnd = 'end;' + CrLf;
EnumeratorMethodInterface = ' function GetEnumerator: IXML%<Name>:sEnumerator;';
EnumeratorMethodImplementation = 'function TXML%<Name>:s.GetEnumerator: IXML%<Name>:sEnumerator;' + CrLf +
'begin' + CrLf +
' Result := TXML%<Name>:sEnumerator.Create(Self);' + CrLf +
'end;' + CrLf;
EnumeratorInterface = ' IXML%<Name>:sEnumerator = interface' + CrLf +
' %<GUID>:s' + CrLf +
' function GetCurrent: IXML%<ItemName>:s;' + CrLf +
' function MoveNext: Boolean;' + CrLf +
' property Current: IXML%<ItemName>:s read GetCurrent;' + CrLf +
' end;' + CrLf;
EnumeratorClass = ' TXML%<Name>:sEnumerator = class(TXMLNodeCollectionEnumerator, IXML%<Name>:sEnumerator)' + CrLf +
' protected' + CrLf +
' function GetCurrent: IXML%<ItemName>:s;' + CrLf +
' end;' + CrLf;
EnumeratorImplementation = 'function TXML%<Name>:sEnumerator.GetCurrent: IXML%<ItemName>:s;' + CrLf +
'begin' + CrLf +
' Result := (inherited GetCurrent as IXML%<ItemName>:s);' + CrLf +
'end;' + CrLf;
PropertyIntfMethodGetOptional = ' function GetHas%<PropertyName>:s: Boolean;';
PropertyIntfMethodGetNil = ' function Get%<PropertyName>:sIsNil: Boolean;';
PropertyIntfMethodGetText = ' function Get%<PropertyName>:sText: WideString;';