From 56013f783a72a03b5448cd20062372f38a9eb604 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Fri, 18 Sep 2009 07:49:04 +0000 Subject: [PATCH] Better collection support Support for optional attributes --- Forms/MainFrm.dfm | Bin 3022 -> 5162 bytes Units/DelphiXMLDataBindingGenerator.pas | 76 +++++++++++++++--------- Units/DelphiXMLDataBindingResources.pas | 7 +++ Units/XMLDataBindingGenerator.pas | 2 +- 4 files changed, 55 insertions(+), 30 deletions(-) diff --git a/Forms/MainFrm.dfm b/Forms/MainFrm.dfm index a1e1733abd9e900f263c3c789c395087c565c8a9..d5e4a09cffb6161ced3f341044cb9ab14e9f56ae 100644 GIT binary patch literal 5162 zcmcIo+in{-5PdJue;`o6PK?xuuC{}wfGt~bQ9Cj$wFwXu0e6>@SW)C+NhNZTkM9g` z%e7?3YMK{YJG{-AIdeF>D*Kz|hFnuFhpJk>B9rj*#~%o}VpBt2lXkajPLR-gfl;%Dt zr5fiIV{!mUS%t@rq+5jxAYJYO2hN%1GdYHk!_YIdkN}^McCY&!O*eeagua2i$sFTz z(nl*oibz@=G%3dlk%@6WXG;pSxS|;oZePy|8{5DN7XU&+=zD(j-o}lTyS>Sj3--#! ziOC{4eahnD`TA^SjFJ}xx6zNxJUMYLd1|_cQ$VLG%S*aIXI~JyfEoDj7WrZ`0O6=q z$tuH{K6KmTl3&~&gm9p9_)W;~kbM}<78gSBQga=Kz~^7x?_|4?bQ=x?&lj*PK&huB z<6Bk(FXap~xw+5{xiu4-1^T>nmJt3<#}cv`&{;NGnQ~tVz@nIN|(r=z?jEKZAAptq`_`Ng2Wr`^(1VTgu_=+Or1Lw5(jg}bH=>Bp?6^U zlWU&YYct*tI1*QbLVMCD`s`*ji`vMna44EXmq|dj<4l&n_%Oj`?Bq94~n-Nt|i<(C&z3GIYOx?Y{tDD)pki z?c%OK|116$EVAHROZTgC>RSW^@wLv(&1v>$&i4NmdVv1jNt5Gk z3wqutUN4djW+dX%pfYX>*WCVkDLy8NubJP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ErVKiN~M3wbc>GoTy;bxb5H51E1;VDQx*Ju&AN*UHL{VjSuuqk-Az4q z3q5oYrAR)z_SQhd6eeUnjd9Q84H8cH!L?0=^_De7>NU#MjjZ~u{3qIJ74ODZdq&t8w5{Yuz;;uVZMwbZ6us-~oX=YgnO9;xrc?7Yaa6;qSCsSd*|oNQBUT+e^xuUFiG zer*bWmrssK45xtO0i)-8e3DIq{!+;9iU_Hey9}#aKt0LSjf+frTc%)sP(|*CBm4ji vyB)>O47m)WVEB=FtOkNF)5zS|nn;30Mn(O3NrFQxXa7L(%no?; diff --git a/Units/DelphiXMLDataBindingGenerator.pas b/Units/DelphiXMLDataBindingGenerator.pas index 3f3c52c..723d0b3 100644 --- a/Units/DelphiXMLDataBindingGenerator.pas +++ b/Units/DelphiXMLDataBindingGenerator.pas @@ -289,7 +289,14 @@ var begin case AProperty.PropertyType of ptSimple: - Result := TranslateDataType(TXMLDataBindingSimpleProperty(AProperty).DataType); + if Assigned(AProperty.Collection) then + begin + if AInterfaceName then + Result := ItemInterface + else + Result := ItemClass; + end else + Result := TranslateDataType(TXMLDataBindingSimpleProperty(AProperty).DataType); ptItem: begin item := TXMLDataBindingItemProperty(AProperty).Item; @@ -785,30 +792,33 @@ begin begin propertyItem := AItem.Properties[propertyIndex]; + if (not AItem.IsCollection) and Assigned(propertyItem.Collection) then + begin + WritePrototype; + + { Inline collection } + if ASection = dxsImplementation then + begin + if propertyItem.PropertyType = ptItem then + AStream.WriteLnNamedFmt(' RegisterChildNode(''%:s'', %:s);', + ['ItemSourceName', propertyItem.Name, + 'ItemClass', GetDataTypeName(propertyItem, False)]); + + AStream.WriteLnNamedFmt(' %:s := CreateCollection(%:s, %:s, ''%:s'') as %:s;', + ['FieldName', PrefixField + propertyItem.TranslatedName, + 'CollectionClass', PrefixClass + propertyItem.Collection.TranslatedName, + 'CollectionInterface', PrefixInterface + propertyItem.Collection.TranslatedName, + 'ItemInterface', GetDataTypeName(propertyItem, True), + 'ItemSourceName', propertyItem.Name]); + end; + end; + if propertyItem.PropertyType = ptItem then begin itemProperty := TXMLDataBindingItemProperty(propertyItem); - if (not AItem.IsCollection) and Assigned(propertyItem.Collection) then - begin - WritePrototype; - - { Inline collection } - if ASection = dxsImplementation then - begin - AStream.WriteLnNamedFmt(' RegisterChildNode(''%:s'', %:s);', - ['ItemSourceName', propertyItem.Name, - 'ItemClass', PrefixClass + itemProperty.Item.TranslatedName]); - - AStream.WriteLnNamedFmt(' %:s := CreateCollection(%:s, %:s, ''%:s'') as %:s;', - ['FieldName', PrefixField + propertyItem.TranslatedName, - 'CollectionClass', PrefixClass + propertyItem.Collection.TranslatedName, - 'CollectionInterface', PrefixInterface + propertyItem.Collection.TranslatedName, - 'ItemInterface', PrefixInterface + itemProperty.Item.TranslatedName, - 'ItemSourceName', propertyItem.Name]); - end; - end else if ((not AItem.IsCollection) or - (propertyItem <> AItem.CollectionItem)) then + if (not AItem.IsCollection) or + (propertyItem <> AItem.CollectionItem) then begin { Item property } if Assigned(itemProperty.Item) and @@ -945,8 +955,8 @@ begin ptSimple: begin dataTypeName := TranslateDataType(TXMLDataBindingSimpleProperty(AItem.CollectionItem).DataType); - dataClassName := 'TXMLNode'; - dataIntfName := 'IXMLNode'; + dataClassName := ItemClass; + dataIntfName := ItemInterface; end; ptItem: begin @@ -1215,9 +1225,11 @@ begin begin WriteNewLine; - // #ToDo1 (MvR) 21-4-2008: optional attributes! if writeOptional then - sourceCode.Add(PropertyImplMethodGetOptional); + if AProperty.IsAttribute then + sourceCode.Add(PropertyImplMethodGetOptionalAttr) + else + sourceCode.Add(PropertyImplMethodGetOptional); if writeNil then sourceCode.Add(PropertyImplMethodGetNil); @@ -1229,10 +1241,16 @@ begin case AProperty.PropertyType of ptSimple: - sourceCode.Add(XMLToNativeDataType('Result', - '%:s', - TXMLDataBindingSimpleProperty(AProperty).DataType, - GetDelphiNodeType(AProperty))); + if Assigned(AProperty.Collection) then + begin + sourceCode.Add('begin'); + sourceCode.Add(' Result := %:s;'); + sourceCode.Add('end;'); + end else + sourceCode.Add(XMLToNativeDataType('Result', + '%:s', + TXMLDataBindingSimpleProperty(AProperty).DataType, + GetDelphiNodeType(AProperty))); ptItem: begin diff --git a/Units/DelphiXMLDataBindingResources.pas b/Units/DelphiXMLDataBindingResources.pas index 449ba86..8a69e2f 100644 --- a/Units/DelphiXMLDataBindingResources.pas +++ b/Units/DelphiXMLDataBindingResources.pas @@ -92,11 +92,18 @@ const 'end;' + CrLf + '' + CrLf; + PropertyImplMethodGetOptionalAttr = 'function TXML%:s.GetHas%:s: Boolean;' + CrLf + + 'begin' + CrLf + + ' Result := Assigned(AttributeNodes.FindNode(''%:s''));' + CrLf + + 'end;' + CrLf + + '' + CrLf; + PropertyImplMethodGetNil = 'function TXML%:s.Get%:sIsNil: Boolean;' + CrLf + 'begin' + CrLf + ' Result := GetNodeIsNil(ChildNodes[''%:s'']);' + CrLf + 'end;' + CrLf + '' + CrLf; + PropertyImplMethodSetNil = 'procedure TXML%:s.Set%:sIsNil(const Value: Boolean);' + CrLf + 'begin' + CrLf + ' SetNodeIsNil(ChildNodes[''%:s''], Value);' + CrLf + diff --git a/Units/XMLDataBindingGenerator.pas b/Units/XMLDataBindingGenerator.pas index 4ebd319..8f14e2f 100644 --- a/Units/XMLDataBindingGenerator.pas +++ b/Units/XMLDataBindingGenerator.pas @@ -1272,7 +1272,7 @@ begin ASchema.InsertItem(collectionItem, interfaceItem); end; - propertyItem.Collection := collectionItem; + propertyItem.Collection := collectionItem; end; end; end;