<commit_before>unit ScriptValidatorResult;
interface
uses
  SysUtils, VerySimpleXML;


type
  TScriptValidatorIssue = record
    Line: Integer;
    Column: Integer;
    Module: string;
    Param: string;
    Msg: string;
  end;
  TScriptValidatorIssueArray = array of TScriptValidatorIssue;

  TScriptValidatorResult = class(TObject)
  strict private
    fHints: TScriptValidatorIssueArray;
    fWarnings: TScriptValidatorIssueArray;
    fErrors: TScriptValidatorIssueArray;
    procedure Add(aLine, aColumn: Integer; const aParam, aMessage: string; var aDest: TScriptValidatorIssueArray); inline;
    procedure ArrayToXML(aSrc: TScriptValidatorIssueArray; var aDest: TXmlNode);
    procedure XMLToArray(aSrc: TXmlNode; var aDest: TScriptValidatorIssueArray);
    function FixText(const aTest: string): string;
  public
    procedure AddHint(aLine, aColumn: Integer; const aParam, aMessage: string);
    procedure AddWarning(aLine, aColumn: Integer; const aParam, aMessage: string);
    procedure AddError(aLine, aColumn: Integer; const aParam, aMessage: string);
    function ToXML: string;
    procedure FromXML(const aXml: string);
    property Hints: TScriptValidatorIssueArray read fHints write fHints;
    property Warnings: TScriptValidatorIssueArray read fWarnings write fWarnings;
    property Errors: TScriptValidatorIssueArray read fErrors write fErrors;
  end;

implementation
uses
  Classes;

{ TScriptValidatorResult }
procedure TScriptValidatorResult.Add(aLine, aColumn: Integer; const aParam, aMessage: string; var aDest: TScriptValidatorIssueArray);
var
  I: Integer;
  Issue: TScriptValidatorIssue;
begin
  I := Length(aDest);
  SetLength(aDest, I + 1);
  Issue.Line   := aLine;
  Issue.Column := aColumn;
  Issue.Param  := aParam;
  Issue.Msg    := aMessage;
  aDest[I]     := Issue;
end;


procedure TScriptValidatorResult.AddHint(aLine, aColumn: Integer; const aParam, aMessage: string);
begin
  Add(aLine, aColumn, aParam, aMessage, fHints);
end;


procedure TScriptValidatorResult.AddWarning(aLine, aColumn: Integer; const aParam, aMessage: string);
begin
  Add(aLine, aColumn, aParam, aMessage, fWarnings);
end;


procedure TScriptValidatorResult.AddError(aLine, aColumn: Integer; const aParam, aMessage: string);
begin
  Add(aLine, aColumn, aParam, aMessage, fErrors);
end;


procedure TScriptValidatorResult.ArrayToXML(aSrc: TScriptValidatorIssueArray; var aDest: TXmlNode);
var
  Node:  TXmlNode;
  Issue: TScriptValidatorIssue;
begin
  for Issue in aSrc do
  begin
    Node := aDest.AddChild('Issue');
    Node.SetAttribute('Line', IntToStr(Issue.Line));
    Node.SetAttribute('Column', IntToStr(Issue.Column));
    Node.SetAttribute('Module', Issue.Module);
    Node.SetAttribute('Param', Issue.Param);
    Node.SetAttribute('Msg', Issue.Msg);
  end;
end;


procedure TScriptValidatorResult.XMLToArray(aSrc: TXmlNode; var aDest: TScriptValidatorIssueArray);
var
  I: Integer;
  Node: TXmlNode;
  Issue: TScriptValidatorIssue;
  Len: Integer;
begin
  for I := 0 to aSrc.ChildNodes.Count - 1 do
  begin
    Node := aSrc.ChildNodes[I];

    Len := Length(aDest);
    SetLength(aDest, Len + 1);
    Issue.Line   := StrToInt(Node.Attribute['Line']);
    Issue.Column := StrToInt(Node.Attribute['Column']);
    Issue.Module := FixText(Node.Attribute['Module']);
    Issue.Param  := FixText(Node.Attribute['Param']);
    Issue.Msg    := FixText(Node.Attribute['Msg']);
    aDest[Len]   := Issue;
  end;
end;


function TScriptValidatorResult.FixText(const aTest: string): string;
begin
  Result := StringReplace(aTest, '&#39;', '"', [rfReplaceAll, rfIgnoreCase]);
end;


function TScriptValidatorResult.ToXML: string;
var
  nHint, nWarning, nError: TXmlNode;
begin
  with TXmlVerySimple.Create do
  begin
    try
      Root.NodeName := 'ScriptValidatorResult';
      nHint := Root.AddChild('Hints');
      nWarning := Root.AddChild('Warnings');
      nError := Root.AddChild('Errors');

      ArrayToXML(fHints, nHint);
      ArrayToXML(fWarnings, nWarning);
      ArrayToXML(fErrors, nError);

      Result := Text;
    finally
      Free;
    end;
  end;
end;


procedure TScriptValidatorResult.FromXML(const aXml: string);
begin
  with TXmlVerySimple.Create do
    try
      Text := aXml;
      XMLToArray(Root.Find('Hints'), fHints);
      XMLToArray(Root.Find('Warnings'), fWarnings);
      XMLToArray(Root.Find('Errors'), fErrors);
    finally
      Free;
    end;
end;

end.<commit_msg>Update ScriptValidatorResult to use KM_IoXML instead of VerySimpleXML, so we could compile it on lazarus too<commit_after>unit ScriptValidatorResult;
interface
uses
  SysUtils, KM_IoXML;


type
  TScriptValidatorIssue = record
    Line: Integer;
    Column: Integer;
    Module: string;
    Param: string;
    Msg: string;
  end;
  TScriptValidatorIssueArray = array of TScriptValidatorIssue;

  TScriptValidatorResult = class(TObject)
  strict private
    fHints: TScriptValidatorIssueArray;
    fWarnings: TScriptValidatorIssueArray;
    fErrors: TScriptValidatorIssueArray;
    procedure Add(aLine, aColumn: Integer; const aParam, aMessage: string; var aDest: TScriptValidatorIssueArray); inline;
    procedure ArrayToXML(aSrc: TScriptValidatorIssueArray; var aDest: TKMXmlNode);
    procedure XMLToArray(aSrc: TKMXmlNode; var aDest: TScriptValidatorIssueArray);
    function FixText(const aTest: string): string;
  public
    procedure AddHint(aLine, aColumn: Integer; const aParam, aMessage: string);
    procedure AddWarning(aLine, aColumn: Integer; const aParam, aMessage: string);
    procedure AddError(aLine, aColumn: Integer; const aParam, aMessage: string);
    function ToXML: string;
    procedure FromXML(const aXml: string);
    property Hints: TScriptValidatorIssueArray read fHints write fHints;
    property Warnings: TScriptValidatorIssueArray read fWarnings write fWarnings;
    property Errors: TScriptValidatorIssueArray read fErrors write fErrors;
  end;

implementation
uses
  Classes;

{ TScriptValidatorResult }
procedure TScriptValidatorResult.Add(aLine, aColumn: Integer; const aParam, aMessage: string; var aDest: TScriptValidatorIssueArray);
var
  I: Integer;
  Issue: TScriptValidatorIssue;
begin
  I := Length(aDest);
  SetLength(aDest, I + 1);
  Issue.Line   := aLine;
  Issue.Column := aColumn;
  Issue.Param  := aParam;
  Issue.Msg    := aMessage;
  aDest[I]     := Issue;
end;


procedure TScriptValidatorResult.AddHint(aLine, aColumn: Integer; const aParam, aMessage: string);
begin
  Add(aLine, aColumn, aParam, aMessage, fHints);
end;


procedure TScriptValidatorResult.AddWarning(aLine, aColumn: Integer; const aParam, aMessage: string);
begin
  Add(aLine, aColumn, aParam, aMessage, fWarnings);
end;


procedure TScriptValidatorResult.AddError(aLine, aColumn: Integer; const aParam, aMessage: string);
begin
  Add(aLine, aColumn, aParam, aMessage, fErrors);
end;


procedure TScriptValidatorResult.ArrayToXML(aSrc: TScriptValidatorIssueArray; var aDest: TKMXmlNode);
var
  Node:  TKMXmlNode;
  Issue: TScriptValidatorIssue;
begin
  for Issue in aSrc do
  begin
    Node := aDest.AddChild('Issue');
    Node.Attributes['Line'] := Issue.Line;
    Node.Attributes['Column'] := Issue.Column;
    Node.Attributes['Module'] := Issue.Module;
    Node.Attributes['Param'] := Issue.Param;
    Node.Attributes['Msg'] := Issue.Msg;
  end;
end;


procedure TScriptValidatorResult.XMLToArray(aSrc: TKMXmlNode; var aDest: TScriptValidatorIssueArray);
var
  I: Integer;
  Node: TKMXmlNode;
  Issue: TScriptValidatorIssue;
  Len: Integer;
begin
  for I := 0 to aSrc.ChildsCount - 1 do
  begin
    Node := aSrc.Childs[I];

    Len := Length(aDest);
    SetLength(aDest, Len + 1);
    Issue.Line   := Node.Attributes['Line'].AsInteger;
    Issue.Column := Node.Attributes['Column'].AsInteger;
    Issue.Module := FixText(Node.Attributes['Module'].AsString);
    Issue.Param  := FixText(Node.Attributes['Param'].AsString);
    Issue.Msg    := FixText(Node.Attributes['Msg'].AsString);
    aDest[Len]   := Issue;
  end;
end;


function TScriptValidatorResult.FixText(const aTest: string): string;
begin
  Result := StringReplace(aTest, '&#39;', '"', [rfReplaceAll, rfIgnoreCase]);
end;


function TScriptValidatorResult.ToXML: string;
var
  xmlDoc: TKMXmlDocument;
  nHint, nWarning, nError: TKMXmlNode;
begin
  xmlDoc := TKMXmlDocument.Create('ScriptValidatorResult');
  try
    nHint := xmlDoc.Root.AddChild('Hints');
    nWarning := xmlDoc.Root.AddChild('Warnings');
    nError := xmlDoc.Root.AddChild('Errors');

    ArrayToXML(fHints, nHint);
    ArrayToXML(fWarnings, nWarning);
    ArrayToXML(fErrors, nError);

    Result := xmlDoc.Xml;
  finally
    xmlDoc.Free;
  end;
end;


procedure TScriptValidatorResult.FromXML(const aXml: string);
var
  xmlDoc: TKMXmlDocument;
begin
  xmlDoc := TKMXmlDocument.Create();
  try
    xmlDoc.Xml := aXml;
    XMLToArray(xmlDoc.Root.FindNode('Hints'), fHints);
    XMLToArray(xmlDoc.Root.FindNode('Warnings'), fWarnings);
    XMLToArray(xmlDoc.Root.FindNode('Errors'), fErrors);
  finally
    xmlDoc.Free;
  end;
end;

end.
{'fraction_non_alphanumeric': 0.10916157690172126, 'fraction_numerical': 0.0013325930038867297, 'mean_word_length': 4.7072243346007605, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7466460', 'n_tokens_mistral': 2925, 'n_tokens_neox': 2830, 'n_words': 703}