[tools] testbot/cgi: Introduce ValueFormatter to handle value formatting.

Francois Gouget fgouget at codeweavers.com
Mon Apr 18 03:32:36 CDT 2022


This allows sharing code between CollectionBlock and FormPage.

Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
---
 .../lib/ObjectModel/CGI/CollectionBlock.pm    | 38 +++-------
 testbot/lib/ObjectModel/CGI/FormPage.pm       | 11 +--
 testbot/lib/ObjectModel/CGI/ValueFormatter.pm | 69 +++++++++++++++++++
 3 files changed, 82 insertions(+), 36 deletions(-)
 create mode 100644 testbot/lib/ObjectModel/CGI/ValueFormatter.pm

diff --git a/testbot/lib/ObjectModel/CGI/CollectionBlock.pm b/testbot/lib/ObjectModel/CGI/CollectionBlock.pm
index 435c44e7f..36f1b0188 100644
--- a/testbot/lib/ObjectModel/CGI/CollectionBlock.pm
+++ b/testbot/lib/ObjectModel/CGI/CollectionBlock.pm
@@ -47,6 +47,8 @@ our @EXPORT = qw(new);
 use POSIX qw(strftime);
 use URI::Escape;
 
+use ObjectModel::CGI::ValueFormatter;
+
 
 =pod
 =over 12
@@ -280,35 +282,15 @@ sub GenerateDataView($$$)
   my $PropertyName = $PropertyDescriptor->GetName();
   my $Value = $Row->{Item}->$PropertyName;
 
-  if ($PropertyDescriptor->GetClass() eq "Itemref" and defined $Value)
+  if ($PropertyDescriptor->GetClass() eq "Basic" and
+      $PropertyDescriptor->GetType() eq "DT" and defined $Value)
   {
-    # Note: This only supports single-key Itemrefs
-    foreach my $ValuePD (@{$Value->GetPropertyDescriptors()})
-    {
-      if ($ValuePD->GetIsKey())
-      {
-        $PropertyName = $ValuePD->GetName();
-        print $self->escapeHTML($Value->$PropertyName);
-        return;
-      }
-    }
-  }
-  elsif ($PropertyDescriptor->GetClass() eq "Basic")
-  {
-    if ($PropertyDescriptor->GetType() eq "B")
-    {
-      print $Value ? "Yes" : "No";
-      return;
-    }
-    if ($PropertyDescriptor->GetType() eq "DT" and defined $Value)
-    {
-      print "<script type='text/javascript'><!--\n",
-            "ShowDateTime($Value);\n",
-            "//--></script><noscript><div>",
-            strftime("%Y-%m-%d %H:%M:%S", localtime($Value)),
-            "</div></noscript>\n";
-      return;
-    }
+    print "<script type='text/javascript'><!--\n",
+          "ShowDateTime($Value);\n",
+          "//--></script><noscript><div>",
+          strftime("%Y-%m-%d %H:%M:%S", localtime($Value)),
+          "</div></noscript>\n";
+    return;
   }
   print defined $Value ? $self->escapeHTML($Value) : " ";
 }
diff --git a/testbot/lib/ObjectModel/CGI/FormPage.pm b/testbot/lib/ObjectModel/CGI/FormPage.pm
index 4f29efec7..7fd2f561e 100644
--- a/testbot/lib/ObjectModel/CGI/FormPage.pm
+++ b/testbot/lib/ObjectModel/CGI/FormPage.pm
@@ -57,6 +57,8 @@ $MyFormPage->GeneratePage();
 use ObjectModel::CGI::Page;
 our @ISA = qw(ObjectModel::CGI::Page);
 
+use ObjectModel::CGI::ValueFormatter;
+
 
 =pod
 =over 12
@@ -280,14 +282,7 @@ sub GenerateValueView($$$)
 {
   my ($self, $PropertyDescriptor, $Value) = @_;
 
-  if ($PropertyDescriptor->GetClass() eq "Basic")
-  {
-    if ($PropertyDescriptor->GetType() eq "B")
-    {
-      print $Value ? "Yes" : "No";
-    }
-  }
-  print defined $Value ? $self->escapeHTML($Value) : " ";
+  GenerateValueHTML($self, $PropertyDescriptor, $Value);
 }
 
 sub GetInputType($$)
diff --git a/testbot/lib/ObjectModel/CGI/ValueFormatter.pm b/testbot/lib/ObjectModel/CGI/ValueFormatter.pm
new file mode 100644
index 000000000..cad6eee1b
--- /dev/null
+++ b/testbot/lib/ObjectModel/CGI/ValueFormatter.pm
@@ -0,0 +1,69 @@
+# -*- Mode: Perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+# Generates HTML providing a user-readable representation of a given value
+#
+# Copyright 2022 Francois Gouget
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+
+use strict;
+
+package ObjectModel::CGI::ValueFormatter;
+
+use Exporter 'import';
+our @EXPORT = qw(GenerateValueHTML);
+
+
+=pod
+=over 12
+
+=item C<GenerateValueHTML()>
+
+Generates an HTML snippet representing the value in a user-readable form.
+
+The Parent parameter must point to a class that implements the escapeHTML()
+method.
+
+=back
+=cut
+
+sub GenerateValueHTML($$$);
+
+sub GenerateValueHTML($$$)
+{
+  my ($Parent, $PropertyDescriptor, $Value) = @_;
+
+  if ($PropertyDescriptor->GetClass() eq "Basic" and
+      $PropertyDescriptor->GetType() eq "B")
+  {
+    print $Value ? "Yes" : "No";
+    return;
+  }
+  if ($PropertyDescriptor->GetClass() eq "Itemref" and defined $Value)
+  {
+    # Note: This only supports single-key Itemrefs
+    foreach my $ValuePD (@{$Value->GetPropertyDescriptors()})
+    {
+      if ($ValuePD->GetIsKey())
+      {
+        my $PropertyName = $ValuePD->GetName();
+        GenerateValueHTML($Parent, $ValuePD, $Value->$PropertyName);
+        return;
+      }
+    }
+  }
+  print defined $Value ? $Parent->escapeHTML($Value) : " ";
+}
+
+1;
-- 
2.30.2



More information about the wine-devel mailing list