Skip to content

Commit 66690fa

Browse files
committed
Update TActiveTextValidator and private TErrorInfo
Validator was updated to check that any non-empty active text is topped and tailed by ekDocument elements. Rewrite TActiveTextValidator.ValidateDocumentStructure to call into IActiveText.IsValidActiveTextDocument to do the heavy lifting. Removed unused Element field from TErrorInfo private advanced record of TActiveTextValidator. Modified constructor and calls to it accordingly.
1 parent c34fb83 commit 66690fa

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

Src/ActiveText.UValidator.pas

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,11 @@ TErrorInfo = record
3636
public
3737
/// <summary>Error code.</summary>
3838
Code: TErrorCode;
39-
/// <summary>Reference to element causing problem.</summary>
40-
/// <remarks>May be nil if error doesn't relate to an element.
41-
/// </remarks>
42-
Element: IActiveTextElem;
4339
/// <summary>Description of error.</summary>
4440
Description: string;
4541
/// <summary>Constructs a record. Sets fields from parameter values.
4642
/// </summary>
47-
constructor Create(const ACode: TErrorCode; AElement: IActiveTextElem;
48-
const ADescription: string); overload;
43+
constructor Create(const ACode: TErrorCode; const ADescription: string);
4944
end;
5045
strict private
5146
/// <summary>Validates given link element.</summary>
@@ -56,6 +51,14 @@ TErrorInfo = record
5651
/// <returns>Boolean. True on success or False on failure.</returns>
5752
class function ValidateLink(LinkElem: IActiveTextActionElem;
5853
out ErrInfo: TErrorInfo): Boolean; static;
54+
/// <summary>Validates document structure.</summary>
55+
/// <param name="ActiveText">IActiveText [in] Active text to be validated.
56+
/// </param>
57+
/// <param name="ErrInfo">TErrorInfo [out] Contains error information if
58+
/// validation fails. Undefined if validation succeeds.</param>
59+
/// <returns>Boolean. True on success or False on failure.</returns>
60+
class function ValidateDocumentStructure(ActiveText: IActiveText;
61+
out ErrInfo: TErrorInfo): Boolean; static;
5962
public
6063
/// <summary>Validates given active text.</summary>
6164
/// <param name="ActiveText">IActiveText [in] Active text to be validated.
@@ -92,6 +95,9 @@ class function TActiveTextValidator.Validate(ActiveText: IActiveText;
9295
begin
9396
if ActiveText.IsEmpty then
9497
Exit(True);
98+
// Validate document structure
99+
if not ValidateDocumentStructure(ActiveText, ErrInfo) then
100+
Exit(False);
95101
// Validate elements
96102
for Elem in ActiveText do
97103
begin
@@ -115,6 +121,16 @@ class function TActiveTextValidator.Validate(ActiveText: IActiveText): Boolean;
115121
Result := Validate(ActiveText, Dummy);
116122
end;
117123

124+
class function TActiveTextValidator.ValidateDocumentStructure(
125+
ActiveText: IActiveText; out ErrInfo: TErrorInfo): Boolean;
126+
resourcestring
127+
sNoDocTags = 'Document must start and end with document tags';
128+
begin
129+
Result := ActiveText.IsValidActiveTextDocument;
130+
if not Result then
131+
ErrInfo := TErrorInfo.Create(errBadStructure, sNoDocTags);
132+
end;
133+
118134
class function TActiveTextValidator.ValidateLink(
119135
LinkElem: IActiveTextActionElem; out ErrInfo: TErrorInfo): Boolean;
120136
resourcestring
@@ -150,7 +166,7 @@ TProtocolInfo = record
150166
< Length(PI.Protocol) + PI.MinURLLength then
151167
begin
152168
ErrInfo := TErrorInfo.Create(
153-
errBadLinkURL, LinkElem, Format(sURLLengthErr, [URL])
169+
errBadLinkURL, Format(sURLLengthErr, [URL])
154170
);
155171
Exit(False);
156172
end;
@@ -160,17 +176,16 @@ TProtocolInfo = record
160176
// No supported protocol
161177
Result := False;
162178
ErrInfo := TErrorInfo.Create(
163-
errBadLinkProtocol, LinkElem, Format(sURLProtocolErr, [URL])
179+
errBadLinkProtocol, Format(sURLProtocolErr, [URL])
164180
);
165181
end;
166182

167183
{ TActiveTextValidator.TErrorInfo }
168184

169185
constructor TActiveTextValidator.TErrorInfo.Create(const ACode: TErrorCode;
170-
AElement: IActiveTextElem; const ADescription: string);
186+
const ADescription: string);
171187
begin
172188
Code := ACode;
173-
Element := AElement;
174189
Description := ADescription;
175190
end;
176191

0 commit comments

Comments
 (0)