1
0
mirror of synced 2024-11-21 21:43:49 +00:00

Fixed: support for nillable elements

This commit is contained in:
Mark van Renswoude 2008-04-21 15:24:33 +00:00
parent 0d63424104
commit 727a5dca46
3 changed files with 72 additions and 15 deletions

View File

@ -447,11 +447,15 @@ begin
end; end;
end; end;
if hasItem and (ASection = dxsInterface) then if ASection = dxsInterface then
begin begin
// #ToDo3 (MvR) 9-3-2008: namespace support?
AStream.WriteLn('const'); AStream.WriteLn('const');
AStream.WriteLn(' XMLSchemaInstanceURI = ''http://www.w3.org/2001/XMLSchema-instance'';');
if hasItem then
// #ToDo3 (MvR) 9-3-2008: namespace support?
AStream.WriteLn(' TargetNamespace = '''';'); AStream.WriteLn(' TargetNamespace = '''';');
AStream.WriteLn(); AStream.WriteLn();
AStream.WriteLn(); AStream.WriteLn();
end; end;
@ -1063,6 +1067,7 @@ function TDelphiXMLDataBindingGenerator.WriteSchemaInterfaceProperty(AStream: TS
var var
sourceCode: TNamedFormatStringList; sourceCode: TNamedFormatStringList;
writeOptional: Boolean; writeOptional: Boolean;
writeNil: Boolean;
writeTextProp: Boolean; writeTextProp: Boolean;
propertyItem: TXMLDataBindingItem; propertyItem: TXMLDataBindingItem;
dataTypeName: String; dataTypeName: String;
@ -1079,9 +1084,15 @@ begin
{ If the property has a collection, it's Count property will be enough { If the property has a collection, it's Count property will be enough
to check if an item is present, no need to write a HasX method. } to check if an item is present, no need to write a HasX method. }
// #ToDo3 (MvR) 14-4-2008: move first check to XMLDataBindingGenerator ? // #ToDo3 (MvR) 14-4-2008: move first check to XMLDataBindingGenerator ?
writeOptional := False;
writeNil := False;
if AMember in [dxmPropertyGet, dxmPropertyDeclaration] then
begin
writeOptional := not Assigned(AProperty.Collection) and writeOptional := not Assigned(AProperty.Collection) and
AProperty.IsOptional and AProperty.IsOptional;
(AMember in [dxmPropertyGet, dxmPropertyDeclaration]); writeNil := AProperty.IsNillable;
end;
dataTypeName := ''; dataTypeName := '';
@ -1139,6 +1150,9 @@ begin
if writeOptional then if writeOptional then
sourceCode.Add(PropertyIntfMethodGetOptional); sourceCode.Add(PropertyIntfMethodGetOptional);
if writeNil then
sourceCode.Add(PropertyIntfMethodGetNil);
if writeTextProp then if writeTextProp then
sourceCode.Add(PropertyIntfMethodGetText); sourceCode.Add(PropertyIntfMethodGetText);
@ -1164,6 +1178,9 @@ begin
if writeOptional then if writeOptional then
sourceCode.Add(PropertyInterfaceOptional); sourceCode.Add(PropertyInterfaceOptional);
if writeNil then
sourceCode.Add(PropertyInterfaceNil);
if AProperty.IsReadOnly then if AProperty.IsReadOnly then
begin begin
if writeTextProp then if writeTextProp then
@ -1188,9 +1205,13 @@ begin
begin begin
WriteNewLine; WriteNewLine;
// #ToDo1 (MvR) 21-4-2008: optional attributes!
if writeOptional then if writeOptional then
sourceCode.Add(PropertyImplMethodGetOptional); sourceCode.Add(PropertyImplMethodGetOptional);
if writeNil then
sourceCode.Add(PropertyImplMethodGetNil);
if writeTextProp then if writeTextProp then
sourceCode.Add(PropertyImplMethodGetText); sourceCode.Add(PropertyImplMethodGetText);

View File

@ -73,12 +73,14 @@ const
PropertyIntfMethodGetOptional = ' function GetHas%<PropertyName>:s: Boolean;'; PropertyIntfMethodGetOptional = ' function GetHas%<PropertyName>:s: Boolean;';
PropertyIntfMethodGetNil = ' function GetIs%<PropertyName>:sNil: Boolean;';
PropertyIntfMethodGetText = ' function Get%<PropertyName>:sText: WideString;'; PropertyIntfMethodGetText = ' function Get%<PropertyName>:sText: WideString;';
PropertyIntfMethodGet = ' function Get%<PropertyName>:s: %<DataType>:s;'; PropertyIntfMethodGet = ' function Get%<PropertyName>:s: %<DataType>:s;';
PropertyIntfMethodSetText = ' procedure Set%<PropertyName>:sText(const Value: WideString);'; PropertyIntfMethodSetText = ' procedure Set%<PropertyName>:sText(const Value: WideString);';
PropertyIntfMethodSet = ' procedure Set%<PropertyName>:s(const Value: %<DataType>:s);'; PropertyIntfMethodSet = ' procedure Set%<PropertyName>:s(const Value: %<DataType>:s);';
PropertyInterfaceOptional = ' property Has%<PropertyName>:s: Boolean read GetHas%<PropertyName>:s;'; PropertyInterfaceOptional = ' property Has%<PropertyName>:s: Boolean read GetHas%<PropertyName>:s;';
PropertyInterfaceNil = ' property Is%<PropertyName>:sNil: Boolean read GetIs%<PropertyName>:sNil;';
PropertyInterfaceTextReadOnly = ' property %<PropertyName>:sText: WideString read Get%<PropertyName>:sText;'; PropertyInterfaceTextReadOnly = ' property %<PropertyName>:sText: WideString read Get%<PropertyName>:sText;';
PropertyInterfaceReadOnly = ' property %<PropertyName>:s: %<DataType>:s read Get%<PropertyName>:s;'; PropertyInterfaceReadOnly = ' property %<PropertyName>:s: %<DataType>:s read Get%<PropertyName>:s;';
PropertyInterfaceText = ' property %<PropertyName>:sText: WideString read Get%<PropertyName>:sText write Set%<PropertyName>:sText;'; PropertyInterfaceText = ' property %<PropertyName>:sText: WideString read Get%<PropertyName>:sText write Set%<PropertyName>:sText;';
@ -90,6 +92,17 @@ const
'end;' + CrLf + 'end;' + CrLf +
'' + CrLf; '' + CrLf;
PropertyImplMethodGetNil = 'function TXML%<Name>:s.GetIs%<PropertyName>:sNil: Boolean;' + CrLf +
'var' + CrLf +
' childNode: IXMLNode;' + CrLf +
'' + CrLf +
'begin' + CrLf +
' childNode := ChildNodes[''%<PropertySourceName>:s''];' + CrLf +
' Result := childNode.HasAttribute(''nil'', XMLSchemaInstanceURI) and' + CrLf +
' StrToBoolDef(childNode.GetAttributeNS(''nil'', XMLSchemaInstanceURI), False);' + CrLf +
'end;' + CrLf +
'' + CrLf;
PropertyImplMethodGetText = 'function TXML%<Name>:s.Get%<PropertyName>:sText: WideString;' + CrLf + PropertyImplMethodGetText = 'function TXML%<Name>:s.Get%<PropertyName>:sText: WideString;' + CrLf +
'begin' + CrLf + 'begin' + CrLf +
' Result := ChildNodes[''%<PropertySourceName>:s''].NodeValue;' + CrLf + ' Result := ChildNodes[''%<PropertySourceName>:s''].NodeValue;' + CrLf +

View File

@ -14,6 +14,7 @@ type
TXMLDataBindingEnumerationMember = class; TXMLDataBindingEnumerationMember = class;
TXMLDataBindingEnumeration = class; TXMLDataBindingEnumeration = class;
TXMLDataBindingProperty = class; TXMLDataBindingProperty = class;
TXMLDataBindingItemProperty = class;
TXMLDataBindingUnresolvedItem = class; TXMLDataBindingUnresolvedItem = class;
@ -243,6 +244,7 @@ type
private private
FIsAttribute: Boolean; FIsAttribute: Boolean;
FIsOptional: Boolean; FIsOptional: Boolean;
FIsNillable: Boolean;
FIsRepeating: Boolean; FIsRepeating: Boolean;
FCollection: TXMLDataBindingInterface; FCollection: TXMLDataBindingInterface;
protected protected
@ -253,6 +255,7 @@ type
public public
property IsAttribute: Boolean read FIsAttribute write FIsAttribute; property IsAttribute: Boolean read FIsAttribute write FIsAttribute;
property IsOptional: Boolean read FIsOptional write FIsOptional; property IsOptional: Boolean read FIsOptional write FIsOptional;
property IsNillable: Boolean read FIsNillable write FIsNillable;
property IsReadOnly: Boolean read GetIsReadOnly; property IsReadOnly: Boolean read GetIsReadOnly;
property IsRepeating: Boolean read FIsRepeating write FIsRepeating; property IsRepeating: Boolean read FIsRepeating write FIsRepeating;
property PropertyType: TXMLDataBindingPropertyType read GetPropertyType; property PropertyType: TXMLDataBindingPropertyType read GetPropertyType;
@ -269,6 +272,7 @@ type
function GetPropertyType(): TXMLDataBindingPropertyType; override; function GetPropertyType(): TXMLDataBindingPropertyType; override;
public public
constructor Create(AOwner: TXMLDataBindingGenerator; ASchemaItem: IXMLSchemaItem; const AName: String; ADataType: IXMLTypeDef); constructor Create(AOwner: TXMLDataBindingGenerator; ASchemaItem: IXMLSchemaItem; const AName: String; ADataType: IXMLTypeDef);
constructor CreateFromAlias(AOwner: TXMLDataBindingGenerator; AProperty: TXMLDataBindingItemProperty; ADataType: IXMLTypeDef);
property DataType: IXMLTypeDef read FDataType; property DataType: IXMLTypeDef read FDataType;
end; end;
@ -338,6 +342,7 @@ const
MaxOccursUnbounded = 'unbounded'; MaxOccursUnbounded = 'unbounded';
UseOptional = 'optional'; UseOptional = 'optional';
CollectionPostfix = 'List'; CollectionPostfix = 'List';
AttributeNillable = 'nillable';
@ -624,8 +629,7 @@ function TXMLDataBindingGenerator.CheckElementOccurance(AElement: IXMLElementDef
if Supports(ANode, IXMLElementCompositor, compositor) then if Supports(ANode, IXMLElementCompositor, compositor) then
begin begin
case AOccurance of case AOccurance of
boMinOccurs: Result := (compositor.MinOccurs = 0) or boMinOccurs: Result := (compositor.MinOccurs = 0);
(compositor.CompositorType = ctChoice);
boMaxOccurs: Result := (compositor.MaxOccurs = MaxOccursUnbounded) or boMaxOccurs: Result := (compositor.MaxOccurs = MaxOccursUnbounded) or
(compositor.MaxOccurs > 1); (compositor.MaxOccurs > 1);
end; end;
@ -669,7 +673,7 @@ var
begin begin
Result := False; Result := False;
if Supports(AElement, IXMLElementCompositor, compositor) then if Supports(AElement.ParentNode, IXMLElementCompositor, compositor) then
Result := (compositor.CompositorType = ctChoice) and Result := (compositor.CompositorType = ctChoice) and
(compositor.ElementDefs.Count > 1); (compositor.ElementDefs.Count > 1);
end; end;
@ -766,6 +770,7 @@ end;
procedure TXMLDataBindingGenerator.ProcessChildElement(ASchema: TXMLDataBindingSchema; AElement: IXMLElementDef; AInterface: TXMLDataBindingInterface); procedure TXMLDataBindingGenerator.ProcessChildElement(ASchema: TXMLDataBindingSchema; AElement: IXMLElementDef; AInterface: TXMLDataBindingInterface);
var var
actualElement: IXMLElementDef;
propertyType: TXMLDataBindingItem; propertyType: TXMLDataBindingItem;
propertyItem: TXMLDataBindingProperty; propertyItem: TXMLDataBindingProperty;
@ -787,6 +792,16 @@ begin
IsChoice(AElement); IsChoice(AElement);
propertyItem.IsRepeating := IsElementRepeating(AElement); propertyItem.IsRepeating := IsElementRepeating(AElement);
actualElement := AElement;
while Assigned(actualElement) and Assigned(actualElement.Ref) do
actualElement := actualElement.Ref;
if AElement.HasAttribute(AttributeNillable) then
propertyItem.IsNillable := StrToBoolDef(AElement.Attributes[AttributeNillable], False)
else if actualElement.HasAttribute(AttributeNillable) then
propertyItem.IsNillable := StrToBoolDef(actualElement.Attributes[AttributeNillable], False);
AInterface.AddProperty(propertyItem); AInterface.AddProperty(propertyItem);
end; end;
end; end;
@ -1440,10 +1455,7 @@ begin
if itemProperty.Item = AOldItem then if itemProperty.Item = AOldItem then
begin begin
{ Replace item property with simple property } { Replace item property with simple property }
simpleProperty := TXMLDataBindingSimpleProperty.Create(Owner, simpleProperty := TXMLDataBindingSimpleProperty.CreateFromAlias(Owner, itemProperty, TXMLDataBindingSimpleTypeAliasItem(AOldItem).DataType);
itemProperty.SchemaItem,
itemProperty.Name,
TXMLDataBindingSimpleTypeAliasItem(AOldItem).DataType);
{ FProperties owns itemProperty and will free it } { FProperties owns itemProperty and will free it }
FProperties[propertyIndex] := simpleProperty; FProperties[propertyIndex] := simpleProperty;
@ -1553,6 +1565,17 @@ begin
end; end;
constructor TXMLDataBindingSimpleProperty.CreateFromAlias(AOwner: TXMLDataBindingGenerator; AProperty: TXMLDataBindingItemProperty; ADataType: IXMLTypeDef);
begin
Create(AOwner, AProperty.SchemaItem, AProperty.Name, ADataType);
IsAttribute := AProperty.IsAttribute;
IsOptional := AProperty.IsOptional;
IsNillable := AProperty.IsNillable;
IsRepeating := AProperty.IsRepeating;
end;
function TXMLDataBindingSimpleProperty.GetIsReadOnly(): Boolean; function TXMLDataBindingSimpleProperty.GetIsReadOnly(): Boolean;
begin begin
Result := False; Result := False;