1
0
mirror of synced 2024-11-14 19:13:50 +00:00

Added support for strict(er) validation

Fixed setter being generated for repeating simple types (and in some cases, causing syntax errors)
This commit is contained in:
Mark van Renswoude 2017-07-19 13:58:31 +02:00
parent 79d87acb61
commit c16eb103da
3 changed files with 126 additions and 104 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ Units/__history/
*.identcache *.identcache
bin/ bin/
lib/ lib/
__history/

View File

@ -57,6 +57,7 @@ type
procedure WriteSchemaEnumerationArray(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingEnumeration); procedure WriteSchemaEnumerationArray(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingEnumeration);
procedure WriteValidate(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection); procedure WriteValidate(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
procedure WriteValidateImplementation(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingInterface; AStrict: Boolean);
procedure WriteEnumeratorMethod(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection); procedure WriteEnumeratorMethod(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
procedure WriteEnumerator(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection); procedure WriteEnumerator(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
@ -75,6 +76,7 @@ type
implementation implementation
uses uses
StrUtils,
SysUtils, SysUtils,
X2UtNamedFormat, X2UtNamedFormat,
@ -1113,6 +1115,9 @@ function TDelphiXMLDataBindingGenerator.WriteSchemaInterfaceProperty(AWriter: TN
typeMapping: TTypeMapping; typeMapping: TTypeMapping;
begin begin
if Assigned(AProperty.Collection) then
exit(True);
Result := AProperty.IsReadOnly; Result := AProperty.IsReadOnly;
if (not Result) and (AProperty.PropertyType = ptSimple) then if (not Result) and (AProperty.PropertyType = ptSimple) then
@ -1493,16 +1498,6 @@ end;
procedure TDelphiXMLDataBindingGenerator.WriteValidate(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection); procedure TDelphiXMLDataBindingGenerator.WriteValidate(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
var
propertyIndex: Integer;
propertyItem: TXMLDataBindingProperty;
elementSortOrder: string;
elementSortCount: Integer;
elementRequired: string;
elementRequiredCount: Integer;
attributeRequired: string;
attributeRequiredCount: Integer;
begin begin
if AItem.DocumentElement then if AItem.DocumentElement then
begin begin
@ -1529,18 +1524,49 @@ begin
dxsImplementation: dxsImplementation:
begin begin
AWriter.WriteLineNamedFmt(XSDValidateMethodImplementationBegin, WriteValidateImplementation(AWriter, AItem, False);
WriteValidateImplementation(AWriter, AItem, True);
end;
end;
end;
end;
procedure TDelphiXMLDataBindingGenerator.WriteValidateImplementation(AWriter: TNamedFormatWriter; AItem: TXMLDataBindingInterface; AStrict: Boolean);
procedure AddArrayElement(var AOutput: string; var ACount: Integer; const AValue: string);
begin
AOutput := AOutput + ', ';
{ Prevent "Line too long" on large elements }
if (ACount > 0) and (ACount mod 5 = 0) then
AOutput := AOutput + XSDValidateMethodImplementationArrayBreak;
AOutput := AOutput + AValue;
Inc(ACount);
end;
var
propertyIndex: Integer;
propertyItem: TXMLDataBindingProperty;
elementSortCount: Integer;
elementSortOrder: string;
elementRequired: string;
elementRequiredCount: Integer;
attributeRequired: string;
attributeRequiredCount: Integer;
begin
AWriter.WriteLineNamedFmt(IfThen(AStrict, XSDValidateStrictMethodImplementationBegin, XSDValidateMethodImplementationBegin),
['Name', AItem.TranslatedName]); ['Name', AItem.TranslatedName]);
elementSortOrder := '';
elementSortCount := 0; elementSortCount := 0;
elementSortOrder := '';
elementRequired := '';
elementRequiredCount := 0; elementRequiredCount := 0;
elementRequired := '';
attributeRequired := '';
attributeRequiredCount := 0; attributeRequiredCount := 0;
attributeRequired := '';
for propertyIndex := 0 to Pred(AItem.PropertyCount) do for propertyIndex := 0 to Pred(AItem.PropertyCount) do
begin begin
@ -1549,29 +1575,16 @@ begin
if propertyItem.IsAttribute then if propertyItem.IsAttribute then
begin begin
if not propertyItem.IsOptional then if not propertyItem.IsOptional then
begin AddArrayElement(attributeRequired, attributeRequiredCount, QuotedStr(propertyItem.Name));
attributeRequired := attributeRequired + ', ' + QuotedStr(propertyItem.Name);
Inc(attributeRequiredCount);
end;
end else if not propertyItem.IsNodeValue then end else if not propertyItem.IsNodeValue then
begin begin
elementSortOrder := elementSortOrder + ', '; AddArrayElement(elementSortOrder, elementSortCount, QuotedStr(propertyItem.Name));
{ Prevent "Line too long" on large elements }
if (elementSortCount > 0) and (elementSortCount mod 5 = 0) then
elementSortOrder := elementSortOrder + XSDValidateMethodImplementationSortNewLine;
elementSortOrder := elementSortOrder + QuotedStr(propertyItem.Name);
Inc(elementSortCount);
if (not propertyItem.IsOptional) and (not propertyItem.IsRepeating) then if (not propertyItem.IsOptional) and (not propertyItem.IsRepeating) then
begin begin
case propertyItem.PropertyType of case propertyItem.PropertyType of
ptSimple: ptSimple:
begin AddArrayElement(elementRequired, elementRequiredCount, QuotedStr(propertyItem.Name));
elementRequired := elementRequired + ', ' + QuotedStr(propertyItem.Name);
Inc(elementRequiredCount);
end;
ptItem: ptItem:
{ For Item properties, we call our getter property. This ensures the child element exists, { For Item properties, we call our getter property. This ensures the child element exists,
@ -1588,7 +1601,7 @@ begin
if elementRequiredCount > 0 then if elementRequiredCount > 0 then
begin begin
Delete(elementRequired, 1, 2); Delete(elementRequired, 1, 2);
AWriter.WriteLineNamedFmt(XSDValidateMethodImplementationRequired, AWriter.WriteLineNamedFmt(IfThen(AStrict, XSDValidateStrictMethodImplementationRequired, XSDValidateMethodImplementationRequired),
['RequiredElements', elementRequired]); ['RequiredElements', elementRequired]);
end; end;
@ -1596,7 +1609,7 @@ begin
if attributeRequiredCount > 0 then if attributeRequiredCount > 0 then
begin begin
Delete(attributeRequired, 1, 2); Delete(attributeRequired, 1, 2);
AWriter.WriteLineNamedFmt(XSDValidateMethodImplementationAttrib, AWriter.WriteLineNamedFmt(IfThen(AStrict, XSDValidateStrictMethodImplementationAttrib, XSDValidateMethodImplementationAttrib),
['RequiredAttributes', attributeRequired]); ['RequiredAttributes', attributeRequired]);
end; end;
@ -1607,10 +1620,7 @@ begin
['SortOrder', elementSortOrder]); ['SortOrder', elementSortOrder]);
end; end;
AWriter.WriteLine(XSDValidateMethodImplementationEnd); AWriter.WriteLine(IfThen(AStrict, XSDValidateStrictMethodImplementationEnd, XSDValidateMethodImplementationEnd));
end;
end;
end;
end; end;

View File

@ -95,17 +95,21 @@ const
XSDValidateInterface = 'IXSDValidate'; XSDValidateInterface = 'IXSDValidate, IXSDValidateStrict';
XSDValidateDocumentMethodInterface = ' procedure XSDValidateDocument;'; XSDValidateDocumentMethodInterface = ' procedure XSDValidateDocument(AStrict: Boolean = False);';
XSDValidateDocumentMethodImplementation = 'procedure TXML%<Name>:s.XSDValidateDocument;' + CrLf + XSDValidateDocumentMethodImplementation = 'procedure TXML%<Name>:s.XSDValidateDocument(AStrict: Boolean);' + CrLf +
'begin' + CrLf + 'begin' + CrLf +
' if AStrict then' + CrLf +
' XMLDataBindingUtils.XSDValidateStrict(Self)' + CrLf +
' else' + CrLf +
' XMLDataBindingUtils.XSDValidate(Self);' + CrLf + ' XMLDataBindingUtils.XSDValidate(Self);' + CrLf +
'end;' + CrLf; 'end;' + CrLf;
XSDValidateMethodInterface = ' procedure XSDValidate;'; XSDValidateMethodInterface = ' procedure XSDValidate;' + CrLf +
' procedure XSDValidateStrict(AResult: IXSDValidateStrictResult);';
XSDValidateMethodImplementationBegin = 'procedure TXML%<Name>:s.XSDValidate;' + CrLf + XSDValidateMethodImplementationBegin = 'procedure TXML%<Name>:s.XSDValidate;' + CrLf +
'begin'; 'begin';
@ -114,11 +118,18 @@ const
XSDValidateMethodImplementationComplex = ' Get%<Name>:s;'; XSDValidateMethodImplementationComplex = ' Get%<Name>:s;';
XSDValidateMethodImplementationAttrib = ' CreateRequiredAttributes(Self, [%<RequiredAttributes>:s]);'; XSDValidateMethodImplementationAttrib = ' CreateRequiredAttributes(Self, [%<RequiredAttributes>:s]);';
XSDValidateMethodImplementationSort = ' SortChildNodes(Self, [%<SortOrder>:s]);'; XSDValidateMethodImplementationSort = ' SortChildNodes(Self, [%<SortOrder>:s]);';
XSDValidateMethodImplementationSortNewLine = #13#10 + ' '; XSDValidateMethodImplementationArrayBreak = #13#10 + ' ';
XSDValidateMethodImplementationEnd = 'end;' + CrLf; XSDValidateMethodImplementationEnd = 'end;' + CrLf;
XSDValidateStrictMethodImplementationBegin = 'procedure TXML%<Name>:s.XSDValidateStrict(AResult: IXSDValidateStrictResult);' + CrLf +
'begin';
XSDValidateStrictMethodImplementationRequired = ' ValidateRequiredElements(AResult, Self, [%<RequiredElements>:s]);';
XSDValidateStrictMethodImplementationAttrib = ' ValidateRequiredAttributes(AResult, Self, [%<RequiredAttributes>:s]);';
XSDValidateStrictMethodImplementationEnd = 'end;' + CrLf;
EnumeratorMethodInterface = ' function GetEnumerator: IXML%<Name>:sEnumerator;'; EnumeratorMethodInterface = ' function GetEnumerator: IXML%<Name>:sEnumerator;';
EnumeratorMethodImplementation = 'function TXML%<Name>:s.GetEnumerator: IXML%<Name>:sEnumerator;' + CrLf + EnumeratorMethodImplementation = 'function TXML%<Name>:s.GetEnumerator: IXML%<Name>:sEnumerator;' + CrLf +
'begin' + CrLf + 'begin' + CrLf +