From a30ef97eb3316633074aa2e2c0645f946f3c7456 Mon Sep 17 00:00:00 2001 From: Bert Herngreen Date: Wed, 6 Sep 2017 17:12:15 +0200 Subject: [PATCH] Make generation of 'Def' getter method configurable.\nSkip generation of 'Def' getter method for Variants --- Forms/MainFrm.dfm | 34 +++++++++++++--------- Forms/MainFrm.pas | 4 +++ Units/DataBindingSettingsXML.pas | 38 ++++++++++++++++++++----- Units/DelphiXMLDataBindingGenerator.pas | 10 +++++-- XSD/DataBindingSettings.settings.xml | 2 +- XSD/DataBindingSettings.xsd | 1 + 6 files changed, 64 insertions(+), 25 deletions(-) diff --git a/Forms/MainFrm.dfm b/Forms/MainFrm.dfm index ef2dd30..f3b5e40 100644 --- a/Forms/MainFrm.dfm +++ b/Forms/MainFrm.dfm @@ -4,7 +4,7 @@ object MainForm: TMainForm BorderIcons = [biSystemMenu, biMinimize] BorderStyle = bsDialog Caption = 'X'#178'Software XML Data Binding for Delphi' - ClientHeight = 291 + ClientHeight = 312 ClientWidth = 438 Color = clBtnFace Font.Charset = DEFAULT_CHARSET @@ -17,7 +17,7 @@ object MainForm: TMainForm OnCreate = FormCreate DesignSize = ( 438 - 291) + 312) PixelsPerInch = 96 TextHeight = 13 object lblSchema: TLabel @@ -44,14 +44,14 @@ object MainForm: TMainForm Left = 8 Top = 43 Width = 422 - Height = 204 + Height = 225 Anchors = [akLeft, akTop, akBottom] Caption = ' Output ' TabOrder = 1 - ExplicitHeight = 167 + ExplicitHeight = 204 DesignSize = ( 422 - 204) + 225) object rbFile: TRadioButton Left = 11 Top = 21 @@ -83,7 +83,6 @@ object MainForm: TMainForm TabOrder = 2 object spFile: TTabSheet TabVisible = False - ExplicitHeight = 85 object lblFile: TLabel Left = 4 Top = 7 @@ -105,8 +104,6 @@ object MainForm: TMainForm end object spFolder: TTabSheet TabVisible = False - ExplicitLeft = 8 - ExplicitHeight = 195 DesignSize = ( 408 83) @@ -171,10 +168,19 @@ object MainForm: TMainForm 'perties' TabOrder = 3 end + object cbGenerateGetOptionalOrDefault: TCheckBox + Left = 11 + Top = 198 + Width = 401 + Height = 17 + Hint = ' Generate a "Def" method for optional elements' + Caption = ' Generate a "Def" method for optional elements' + TabOrder = 4 + end end object btnGenerate: TButton Left = 274 - Top = 258 + Top = 279 Width = 75 Height = 25 Anchors = [akRight, akBottom] @@ -182,11 +188,11 @@ object MainForm: TMainForm Default = True TabOrder = 3 OnClick = btnGenerateClick - ExplicitTop = 221 + ExplicitTop = 258 end object btnClose: TButton Left = 355 - Top = 258 + Top = 279 Width = 75 Height = 25 Anchors = [akRight, akBottom] @@ -194,11 +200,11 @@ object MainForm: TMainForm Caption = '&Close' TabOrder = 4 OnClick = btnCloseClick - ExplicitTop = 221 + ExplicitTop = 258 end object btnHints: TButton Left = 7 - Top = 258 + Top = 279 Width = 142 Height = 25 Anchors = [akRight, akBottom] @@ -206,7 +212,7 @@ object MainForm: TMainForm Caption = 'Generate blank &Hints file' TabOrder = 2 OnClick = btnHintsClick - ExplicitTop = 221 + ExplicitTop = 258 end object DefaultEditStyle: TcxDefaultEditStyleController Style.HotTrack = False diff --git a/Forms/MainFrm.pas b/Forms/MainFrm.pas index 9f97609..df30b54 100644 --- a/Forms/MainFrm.pas +++ b/Forms/MainFrm.pas @@ -52,6 +52,7 @@ type spFile: TTabSheet; spFolder: TTabSheet; cbHasChecksEmpty: TCheckBox; + cbGenerateGetOptionalOrDefault: TCheckBox; procedure btnCloseClick(Sender: TObject); procedure btnGenerateClick(Sender: TObject); @@ -183,6 +184,7 @@ begin end; generator.HasChecksEmpty := cbHasChecksEmpty.Checked; + generator.HasGenerateGetOptionalOrDefault := cbGenerateGetOptionalOrDefault.Checked; generator.OnGetFileName := GetFileName; generator.Execute(feSchema.Text); @@ -283,6 +285,7 @@ begin end; cbHasChecksEmpty.Checked := settings.Output.HasHasChecksEmpty and settings.Output.HasChecksEmpty; + cbGenerateGetOptionalOrDefault.Checked := settings.Output.HasGenerateGetOptionalOrDefault and settings.Output.GenerateGetOptionalOrDefault; end; end; end; @@ -319,6 +322,7 @@ begin end; settings.Output.HasChecksEmpty := cbHasChecksEmpty.Checked; + settings.Output.GetOptionalOrDefault := cbGenerateGetOptionalOrDefault.Checked; settings.OwnerDocument.SaveToFile(fileName); end; diff --git a/Units/DataBindingSettingsXML.pas b/Units/DataBindingSettingsXML.pas index 97f43d3..c60fb1d 100644 --- a/Units/DataBindingSettingsXML.pas +++ b/Units/DataBindingSettingsXML.pas @@ -1,7 +1,7 @@ { X2Software XML Data Binding - Generated on: 21-7-2017 9:19:46 + Generated on: 6-9-2017 16:58:58 Generated from: P:\x2xmldatabinding\XSD\DataBindingSettings.xsd } unit DataBindingSettingsXML; @@ -28,7 +28,7 @@ type Contains the settings and hints for the Delphi XML Data Binding. } IXMLDataBindingSettings = interface(IXMLNode) - ['{0407CBCB-B49B-4ED3-A2F6-CCDDFE46F334}'] + ['{90C0CA10-A7AD-4418-98B6-D03469DB8913}'] procedure XSDValidateDocument(AStrict: Boolean = False); function GetHasOutput: Boolean; function GetOutput: IXMLDataBindingOutput; @@ -41,7 +41,7 @@ type Contains the user-defined output settings last used } IXMLDataBindingOutput = interface(IXMLNode) - ['{21821EFA-C7D8-4299-BF44-8122FBF2BC2E}'] + ['{166F0975-2D2F-4CDB-B911-005163A0761F}'] procedure XSDValidate; procedure XSDValidateStrict(AResult: IXSDValidateStrictResult); @@ -53,10 +53,13 @@ type function GetOutputMultiple: IXMLOutputMultiple; function GetHasHasChecksEmpty: Boolean; function GetHasChecksEmpty: Boolean; + function GetHasGenerateGetOptionalOrDefault: Boolean; + function GetGenerateGetOptionalOrDefault: Boolean; procedure SetOutputTypeText(const Value: WideString); procedure SetOutputType(const Value: TXMLDataBindingOutputType); procedure SetHasChecksEmpty(const Value: Boolean); + procedure SetGenerateGetOptionalOrDefault(const Value: Boolean); property OutputTypeText: WideString read GetOutputTypeText write SetOutputTypeText; property OutputType: TXMLDataBindingOutputType read GetOutputType write SetOutputType; @@ -66,10 +69,12 @@ type property OutputMultiple: IXMLOutputMultiple read GetOutputMultiple; property HasHasChecksEmpty: Boolean read GetHasHasChecksEmpty; property HasChecksEmpty: Boolean read GetHasChecksEmpty write SetHasChecksEmpty; + property HasGenerateGetOptionalOrDefault: Boolean read GetHasGenerateGetOptionalOrDefault; + property GenerateGetOptionalOrDefault: Boolean read GetGenerateGetOptionalOrDefault write SetGenerateGetOptionalOrDefault; end; IXMLOutputSingle = interface(IXMLNode) - ['{ABB2D62A-0B4D-4E4B-8835-ED8AFE7564EA}'] + ['{77AE2C19-333F-4335-872E-659AE17C4701}'] procedure XSDValidate; procedure XSDValidateStrict(AResult: IXSDValidateStrictResult); @@ -81,7 +86,7 @@ type end; IXMLOutputMultiple = interface(IXMLNode) - ['{2F4918D6-EE9C-4986-9052-285A8B4D58C5}'] + ['{8C1E7817-DBF1-4125-B29A-F7279006C7FB}'] procedure XSDValidate; procedure XSDValidateStrict(AResult: IXSDValidateStrictResult); @@ -124,10 +129,13 @@ type function GetOutputMultiple: IXMLOutputMultiple; function GetHasHasChecksEmpty: Boolean; function GetHasChecksEmpty: Boolean; + function GetHasGenerateGetOptionalOrDefault: Boolean; + function GetGenerateGetOptionalOrDefault: Boolean; procedure SetOutputTypeText(const Value: WideString); procedure SetOutputType(const Value: TXMLDataBindingOutputType); procedure SetHasChecksEmpty(const Value: Boolean); + procedure SetGenerateGetOptionalOrDefault(const Value: Boolean); end; TXMLOutputSingle = class(TX2XMLNode, IXSDValidate, IXSDValidateStrict, IXMLOutputSingle) @@ -279,13 +287,13 @@ end; procedure TXMLDataBindingOutput.XSDValidate; begin GetOutputType; - SortChildNodes(Self, ['OutputType', 'OutputSingle', 'OutputMultiple', 'HasChecksEmpty']); + SortChildNodes(Self, ['OutputType', 'OutputSingle', 'OutputMultiple', 'HasChecksEmpty', 'GenerateGetOptionalOrDefault']); end; procedure TXMLDataBindingOutput.XSDValidateStrict(AResult: IXSDValidateStrictResult); begin GetOutputType; - SortChildNodes(Self, ['OutputType', 'OutputSingle', 'OutputMultiple', 'HasChecksEmpty']); + SortChildNodes(Self, ['OutputType', 'OutputSingle', 'OutputMultiple', 'HasChecksEmpty', 'GenerateGetOptionalOrDefault']); end; function TXMLDataBindingOutput.GetOutputTypeText: WideString; @@ -332,6 +340,17 @@ begin Result := ChildNodes['HasChecksEmpty'].NodeValue; end; +function TXMLDataBindingOutput.GetHasGenerateGetOptionalOrDefault: Boolean; +begin + Result := Assigned(ChildNodes.FindNode('GenerateGetOptionalOrDefault')); +end; + + +function TXMLDataBindingOutput.GetGenerateGetOptionalOrDefault: Boolean; +begin + Result := ChildNodes['GenerateGetOptionalOrDefault'].NodeValue; +end; + procedure TXMLDataBindingOutput.SetOutputTypeText(const Value: WideString); begin ChildNodes['OutputType'].NodeValue := Value; @@ -348,6 +367,11 @@ begin ChildNodes['HasChecksEmpty'].NodeValue := BoolToXML(Value); end; +procedure TXMLDataBindingOutput.SetGenerateGetOptionalOrDefault(const Value: Boolean); +begin + ChildNodes['GenerateGetOptionalOrDefault'].NodeValue := BoolToXML(Value); +end; + procedure TXMLOutputSingle.XSDValidate; begin CreateRequiredElements(Self, ['FileName']); diff --git a/Units/DelphiXMLDataBindingGenerator.pas b/Units/DelphiXMLDataBindingGenerator.pas index c02bbb0..c570eae 100644 --- a/Units/DelphiXMLDataBindingGenerator.pas +++ b/Units/DelphiXMLDataBindingGenerator.pas @@ -23,6 +23,7 @@ type FHasChecksEmpty: Boolean; FOnGetFileName: TGetFileNameEvent; + FHasGenerateGetOptionalOrDefault: Boolean; protected procedure GenerateDataBinding; override; procedure GenerateOutputFile(ASchemaList: TXMLSchemaList; const ASourceFileName, AUnitName: String); @@ -72,6 +73,7 @@ type property UnitNames: TDictionary read FUnitNames; public property HasChecksEmpty: Boolean read FHasChecksEmpty write FHasChecksEmpty; + property HasGenerateGetOptionalOrDefault: Boolean read FHasGenerateGetOptionalOrDefault write FHasGenerateGetOptionalOrDefault; property OnGetFileName: TGetFileNameEvent read FOnGetFileName write FOnGetFileName; end; @@ -86,6 +88,8 @@ uses X2Log.Global; +const + VariantText = 'Variant'; { TDelphiXMLDataBindingGenerator } @@ -314,7 +318,7 @@ var typeMapping: TTypeMapping; begin - Result := 'Variant'; + Result := VariantText; if GetDataTypeMapping(ADataType, typeMapping) then Result := typeMapping.DelphiName; end; @@ -1225,7 +1229,7 @@ begin if writeOptional then begin sourceCode.Add(PropertyIntfMethodGetOptional); - if AProperty.PropertyType = ptSimple then + if HasGenerateGetOptionalOrDefault and (AProperty.PropertyType = ptSimple) and (dataTypeName <> VariantText) then sourceCode.Add(PropertyIntfMethodGetOptionalOrDefault); end; @@ -1316,7 +1320,7 @@ begin sourceCode.Add(PropertyImplMethodGetOptional[GetDelphiElementType(nodeType)]); end; - if AProperty.PropertyType = ptSimple then + if HasGenerateGetOptionalOrDefault and (AProperty.PropertyType = ptSimple) and (dataTypeName <> VariantText) then sourceCode.Add(PropertyImplMethodGetOptionalOrDefault); end; diff --git a/XSD/DataBindingSettings.settings.xml b/XSD/DataBindingSettings.settings.xml index 8223c72..1d09f4c 100644 --- a/XSD/DataBindingSettings.settings.xml +++ b/XSD/DataBindingSettings.settings.xml @@ -1,2 +1,2 @@ -Single..\Units\DataBindingSettingsXML.pas +Single..\Units\DataBindingSettingsXML.pasfalsefalse diff --git a/XSD/DataBindingSettings.xsd b/XSD/DataBindingSettings.xsd index df6b65c..83a556e 100644 --- a/XSD/DataBindingSettings.xsd +++ b/XSD/DataBindingSettings.xsd @@ -35,6 +35,7 @@ +