validator/httpd/cgi-bin check,1.432.2.15,1.432.2.16

Update of /sources/public/validator/httpd/cgi-bin
In directory hutz:/tmp/cvs-serv10602/httpd/cgi-bin

Modified Files:
      Tag: validator-0_7-branch
	check 
Log Message:
* moving all outputs to templates instead of inline code
* adding ucn and soap12 outputs as experimental additions
  ( will allow us to plug unicorn to main validator, and also let people 
  actually develop libraries for these outputs)
* deprecating homegrown xml format (in documentation, needs to be in the changelog and announce)

These involve some heavy porting of code from CVS HEAD to 0.7 branch, needs some testing.



Index: check
===================================================================
RCS file: /sources/public/validator/httpd/cgi-bin/check,v
retrieving revision 1.432.2.15
retrieving revision 1.432.2.16
diff -u -d -r1.432.2.15 -r1.432.2.16
--- check	12 Oct 2006 04:20:10 -0000	1.432.2.15
+++ check	13 Oct 2006 07:16:20 -0000	1.432.2.16
@@ -301,7 +301,54 @@
   %cache_opts,
 );
 
+# templates for alternate output formats
+my $XMLT = HTML::Template->new(
+  filename          => File::Spec->catfile($CFG->{Paths}->{Templates},
+                                          $lang, 'xml_output.tmpl'),
+  die_on_bad_params => FALSE,
+  loop_context_vars => TRUE,
+  cache             => TRUE,
+);
+my $SOAPT = HTML::Template->new(
+  filename          => File::Spec->catfile($CFG->{Paths}->{Templates},
+                                          $lang, 'soap_output.tmpl'),
+  die_on_bad_params => FALSE,
+  loop_context_vars => TRUE,
+  cache             => TRUE,
+);
+my $UCNT = HTML::Template->new(
+  filename          => File::Spec->catfile($CFG->{Paths}->{Templates},
+                                          $lang, 'ucn_output.tmpl'),
+  die_on_bad_params => FALSE,
+  loop_context_vars => TRUE,
+  cache             => TRUE,
+);
+my $SOAPFT = HTML::Template->new(
+  filename          => File::Spec->catfile($CFG->{Paths}->{Templates},
+                                          $lang, 'soap_fault.tmpl'),
+  die_on_bad_params => FALSE,
+  loop_context_vars => TRUE,
+  cache             => TRUE,
+);
+my $EARLT = HTML::Template->new(
+  filename          => File::Spec->catfile($CFG->{Paths}->{Templates},
+                                          $lang, 'earl_xml.tmpl'),
+  die_on_bad_params => FALSE,
+  loop_context_vars => TRUE,
+  cache             => TRUE,
+  global_vars       => TRUE,
+);
+my $N3T = HTML::Template->new(
+  filename          => File::Spec->catfile($CFG->{Paths}->{Templates},
+                                          $lang, 'earl_n3.tmpl'),
+  die_on_bad_params => FALSE,
+  loop_context_vars => TRUE,
+  cache             => TRUE,
+  global_vars       => TRUE,
+);
+
 $File->{T} = $T;
+$File->{S} = $SOAPT;
 $File->{E} = $E;
 $File->{H} = $H;
 
@@ -323,6 +370,7 @@
 $RSRC = \%rsrc;
 
 $T->param(cfg_home_page => $CFG->{'Home Page'});
+$SOAPT->param(cfg_home_page => $CFG->{'Home Page'});
 
 undef $lang;
 undef %cache_opts;
@@ -771,61 +819,40 @@
   }
 }
 
+my $template;
 
 if ($File->{Opt}->{Output} eq 'xml') {
-  &report_xml($File);
+  $template = $XMLT;
 } elsif ($File->{Opt}->{Output} eq 'earl') {
-  &report_earl($File);
+  $template = $EARLT;
 } elsif ($File->{Opt}->{Output} eq 'n3') {
-  &report_n3($File);
-} else {
-  &prep_template($File, $T);
-
-  if (! $File->{Doctype} and ($File->{Version} eq 'unknown' or $File->{Version} eq 'SGML' or (!$File->{Version}))) {
-    # @@TODO@@ we should try falling back on other version info, such as the ones stored in Version_ESIS
-    $T->param(file_version => '(no Doctype found)');
-  }
-  else {
-    $T->param(file_version => $File->{Version});
-  }
-  my $num_errors = scalar @{$File->{Errors}};
-  if ($num_errors >0)
-  {
-    $T->param(has_errors => 1);
-  }
-  $T->param(valid_errors_num => $num_errors);
-  my ($number_of_errors, $reported_errors) = &report_errors($File);
-  if ($number_of_errors > 1) {
-    $number_of_errors = "$number_of_errors errors"
-  }
-  else {
-    $number_of_errors = "$number_of_errors error"
-  }
-  $T->param(file_errors => $reported_errors);
-   $T->param(number_of_errors => $number_of_errors);
-  if ($File->{'Is Valid'}) {
-    $T->param(VALID => TRUE);
-    $T->param(valid_status => 'Valid');
-    &report_valid($File, $T);
+  $template = $N3T;
+} elsif ($File->{Opt}->{Output} eq 'ucn') {
+  $template = $UCNT;
+} elsif ($File->{Opt}->{Output} eq 'soap12') {
+  if ($File->{'Error Flagged'}) {
+    $template = $SOAPFT;
   } else {
-    $T->param(VALID => FALSE);
-    $T->param(valid_status => 'Invalid');
+    $template = $SOAPT;
   }
+} else {
+    $template = $T;
+}
 
-  $T->param(file_warnings  => $File->{Warnings});
-  $T->param(file_outline   => &outline($File))
-    if $T->param('opt_show_outline');
-  $T->param(file_source    => &source($File))
-    if $T->param('opt_show_source');
-  $T->param(file_parsetree => &parsetree($File))
-    if $T->param('opt_show_parsetree');
-  $T->param('opt_show_esis' => TRUE)   if $File->{Opt}->{'Show ESIS'};
-  $T->param('file_esis' => &show_esis($File))   if $T->param('opt_show_esis');
-  $T->param('opt_show_raw_errors' => TRUE) if $File->{Opt}->{'Show Errors'};
-  $T->param('file_raw_errors' =>    &show_errors($File)) if  $T->param('opt_show_raw_errors');
+&prep_template($File, $template);
+fin_template($File, $template);
 
-  print $T->output;
-}
+$template->param(file_warnings => $File->{Warnings});
+$template->param(file_source => &source($File))
+  if $template->param('opt_show_source');
+$template->param('opt_show_esis' => TRUE)
+  if $File->{Opt}->{'Show ESIS'};
+$template->param('opt_show_raw_errors' => TRUE)
+  if $File->{Opt}->{'Show Errors'};
+$template->param('file_raw_errors' => &show_errors($File))
+  if $template->param('opt_show_raw_errors');
+
+print $template->output;
 
 #
 # Get rid of $File object and exit.
@@ -837,7 +864,6 @@
 #############################################################################
 
 #
-# Generate HTML report.
 sub prep_template ($$) {
   my $File = shift;
   my $T    = shift;
@@ -866,6 +892,7 @@
   $T->param(file_size        => $File->{Size});
   $T->param(file_contenttype => $File->{ContentType});
   $T->param(file_charset     => $File->{Charset}->{Use});
+  $T->param(file_doctype     => $File->{DOCTYPE});
 
   #
   # Output options...
@@ -906,6 +933,45 @@
        $T->param($over_charset_param => TRUE);
   }
 }
+sub fin_template ($$) {
+  my $File = shift;
+  my $T    = shift;
+
+  if (! $File->{Doctype} and ($File->{Version} eq 'unknown' or $File->{Version} eq 'SGML' or (!$File->{Version}))) {
+
+    # @@TODO@@ we should try falling back on other version
+    # info, such as the ones stored in Version_ESIS
+    $T->param(file_version => '(no Doctype found)');
+  }
+  else {
+    $T->param(file_version => $File->{Version});
+  }
+  my ($num_errors,$num_warnings, $num_info, $reported_errors) = &report_errors($File);
+  if ($num_errors+$num_warnings > 0)
+  {
+    $T->param(has_errors => 1);
+  }
+  $T->param(valid_errors_num => $num_errors);
+  $num_warnings += scalar @{$File->{Warnings}};
+  $T->param(valid_warnings_num => $num_warnings);
+  my $number_of_errors = ""; # textual form of $num_errors
+  if ($num_errors > 1) {
+    $number_of_errors = "$num_errors errors"
+  }
+  else {
+    $number_of_errors = "$num_errors error"
+  }
+  $T->param(file_errors => $reported_errors);
+   $T->param(number_of_errors => $number_of_errors);
+  if ($File->{'Is Valid'}) {
+    $T->param(VALID => TRUE);
+    $T->param(valid_status => 'Valid');
+    &report_valid($File, $T);
+  } else {
+    $T->param(VALID => FALSE);
+    $T->param(valid_status => 'Invalid');
+  }
+}
 
 #
 # Output "This page is Valid" report.
@@ -947,6 +1013,9 @@
   $File->{T}->param(have_warnings => TRUE);
   $File->{E}->param($WID => TRUE, %{$params});
   $File->{E}->param(have_warnings => TRUE);
+  $File->{S}->param($WID => TRUE, %{$params});
+  $File->{S}->param(have_warnings => TRUE);
+
 }
 
 #
@@ -1426,6 +1495,8 @@
   my $File = shift;
   my $Errors = [];
   my $number_of_errors = 0;
+  my $number_of_warnings = 0;
+  my $number_of_info = 0;
 
   # Hash to keep track of how many of each error is reported.
   my %Msgs; # Used to generate a UID for explanations.
@@ -1438,7 +1509,7 @@
 
       my $explanation;
       if ($err->{num}) {
-        my (undef, $num) = split /\./, $err->{num};
+        my $num = $err->{num};
         if (exists $Msgs{$num}) { # We've already seen this message...
           if ($File->{Opt}->{Verbose}) { # ...so only repeat it in Verbose mode.
             $explanation = qq(\n    <div class="hidden mid-$num"></div>\n);
@@ -1452,14 +1523,14 @@
         my $_msg = $RSRC->{msg}->{nomsg}->{verbose};
         $_msg =~ s/<!--MID-->/$num/g;
         if ($File->{'Is Upload'})
-	{
-		$_msg =~ s/<!--URI-->//g
-	}
-	else
-	{
-		my $escaped_uri = uri_escape($File->{URI});
-        	$_msg =~ s/<!--URI-->/$escaped_uri/g;
-	}
+        {
+          $_msg =~ s/<!--URI-->//g
+        }
+        else
+        {
+          my $escaped_uri = uri_escape($File->{URI});
+          $_msg =~ s/<!--URI-->/$escaped_uri/g;
+        }
         $explanation .= "    $_msg\n"; # The send feedback plea.
       }
 
@@ -1469,27 +1540,36 @@
       if ($err->{type} eq 'I')
       {
         $err->{class} = 'msg_info';
+        $err->{err_type_err} = 0;
+        $err->{err_type_warn} = 0;
         $err->{err_type_info} = 1;
+        $number_of_info += 1;
       }
       elsif ($err->{type} eq 'E')
       {
         $err->{class} = 'msg_err';
         $err->{err_type_err} = 1;
+        $err->{err_type_warn} = 0;
+        $err->{err_type_info} = 0;
         $number_of_errors += 1;
       }
       elsif (($err->{type} eq 'W') or ($err->{type} eq 'X') )
       {
         $err->{class} = 'msg_warn';
+        $err->{err_type_err} = 0;
         $err->{err_type_warn} = 1;
+        $err->{err_type_info} = 0;
+        $number_of_warnings += 1;
       }
       # TODO other classes for "X" etc? FIXME find all types of message.
 
       push @{$Errors}, $err;
     }
   }
-  return $number_of_errors, $Errors;
+  return $number_of_errors, $number_of_warnings, $number_of_info, $Errors;
 }
 
+
 #
 # Chop the source line into 3 pieces; the character at which the error
 # was detected, and everything to the left and right of that position.
@@ -2095,284 +2175,6 @@
 }
 
 #
-# Return an XML report for the page.
-sub report_xml {
-  my $File = shift;
-
-  my $valid = ($File->{'Is Valid'} ? 'Valid' : 'Invalid');
-  my $errs  = ($File->{'Is Valid'} ? '0'     : scalar @{$File->{Errors}});
-  if ($File->{E}->param('fatal_http_error')) {
-    $valid = 'Could not validate';
-  }
-
-  print <<".EOF.";
-Content-Type: application/xml; charset=UTF-8
-X-W3C-Validator-Status: $valid
-X-W3C-Validator-Errors: $errs
-
-<?xml version="1.0" encoding="UTF-8"?>
-<?xml-stylesheet type="text/xsl" href="xml-results.xsl"?>
-<!DOCTYPE result [
-  <!ELEMENT result (meta, warnings?, messages?)>
-  <!ATTLIST result
-    version CDATA #FIXED '0.9.1'
-  >
-
-  <!ELEMENT meta (uri, modified, server, size, encoding, doctype, errors)>
-  <!ELEMENT uri      (#PCDATA)>
-  <!ELEMENT modified (#PCDATA)>
-  <!ELEMENT server   (#PCDATA)>
-  <!ELEMENT size     (#PCDATA)>
-  <!ELEMENT encoding (#PCDATA)>
-  <!ELEMENT doctype  (#PCDATA)>
-  <!ELEMENT errors   (#PCDATA)>
-
-  <!ELEMENT warnings (warning)+>
-  <!ELEMENT warning  (#PCDATA)>
-
-  <!ELEMENT messages (msg)*>
-  <!ELEMENT msg      (#PCDATA)>
-  <!ATTLIST msg
-    line   CDATA #IMPLIED
-    col    CDATA #IMPLIED
-    offset CDATA #IMPLIED
-  >
-]>
-.EOF.
-
-  print qq(
-<result>
-  <meta>
-    <uri>), &ent($File->{URI}), qq(</uri>
-    <modified>), &ent($File->{Modified}), qq(</modified>
-    <server>), &ent($File->{Server}), qq(</server>
-    <size>), &ent($File->{Size}), qq(</size>
-    <encoding>), &ent($File->{Charset}->{Use}), qq(</encoding>
-    <doctype>), &ent($File->{DOCTYPE}), qq(</doctype>
-    <errors>), $errs, qq(</errors>
-  </meta>
-);
-
-  #@@FIXME: This is borked after templatification.
-  # &add_warning($File, 'note', 'Note:', <<".EOF.");
-  #    This interface is highly experimental and the output *will* change
-  #    -- probably even several times -- before finished. Do *not* rely on it!
-  #    See http://validator.w3.org/docs/users.html#api-warning
-  # .EOF.
-  #@@FIXME;
-
-  if (defined $File->{Warnings} and scalar @{$File->{Warnings}}) {
-    print qq(  <warnings>\n);
-    printf qq(    <warning>%s</warning>\n),
-      &ent($_->{Message}) for @{$File->{Warnings}};
-    print qq(  </warnings>\n);
-  }
-
-  if (defined $File->{Errors} and scalar @{$File->{Errors}}) {
-    print qq(  <messages>\n);
-
-    foreach my $err (@{$File->{Errors}}) {
-      chomp $err->{msg};
-
-      # Find index into the %frag hash for the "explanation..." links.
-      $err->{idx} = $err->{msg};
-      $err->{idx} =~ s/"[^\"]*"/FOO/g;
-      $err->{idx} =~ s/[^A-Za-z ]//g;
-      $err->{idx} =~ s/\s+/ /g;        # Collapse spaces
-      $err->{idx} =~ s/(^\s|\s$)//g;   # Remove leading and trailing spaces.
-      $err->{idx} =~ s/(FOO )+/FOO /g; # Collapse FOOs.
-      $err->{idx} =~ s/FOO FOO/FOO/g;  # Collapse FOOs.
-
-      my $offset = $File->{Offsets}->[$err->{line} - 1]->[1] + $err->{char};
-      printf <<".EOF.", &ent($err->{msg});
-    <msg line="$err->{line}" col="$err->{char}" offset="$offset">%s</msg>
-.EOF.
-    }
-    print qq(  </messages>\n);
-  }
-  print qq(</result>\n);
-}
-
-#
-# Return an EARL report for the page.
-sub report_earl {
-  my $File = shift;
-
-  my $valid = ($File->{'Is Valid'} ? 'Valid' : 'Invalid');
-  my $errs  = ($File->{'Is Valid'} ? '0'     : scalar @{$File->{Errors}});
-  if ($File->{E}->param('fatal_http_error')) {
-    $valid = 'Could not validate';
-  }
-
-  print <<".EOF.";
-Content-Type: application/rdf+xml; charset=UTF-8
-X-W3C-Validator-Status: $valid
-X-W3C-Validator-Errors: $errs
-
-<?xml version="1.0" encoding="UTF-8"?>
-<rdf:RDF
-  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
-  xmlns="http://www.w3.org/2001/03/earl/1.0-test#"
-  xmlns:val="http://validator.w3.org/this_will_change/do_not_rely_on_it!">
-
-  <Assertor rdf:about="http://validator.w3.org/">
-    <name>W3 Validator</name>
-    <contactInfo rdf:resource="http://validator.w3.org/about.html"/>
-    <testMode rdf:resource="http://www.w3.org/2001/03/earl/1.00#Auto" />
-
-.EOF.
-
-  unless ($File->{'Is Valid'}) {
-    printf <<".EOF.", &ent($File->{URI});
-  <asserts>
-   <Assertion rdf:ID="result">
-    <subject rdf:resource="%s" />
-    <result rdf:resource="http://www.w3.org/2001/03/earl/1.00#fails" />
-    <testCase rdf:resource="http://www.w3.org/MarkUp/" />
-    <note>Invalid!</note>
-   </Assertion>
-  </asserts>
-.EOF.
-
-    my $errnum = 0 ;
-    foreach my $err (@{$File->{Errors}}) {
-      ++$errnum ;
-      chomp $err->{msg};
-
-      # Find index into the %frag hash for the "explanation..." links.
-      $err->{idx} =  $err->{msg};
-      $err->{idx} =~ s/"[^\"]*"/FOO/g;
-      $err->{idx} =~ s/[^A-Za-z ]//g;
-      $err->{idx} =~ s/\s+/ /g;        # Collapse spaces
-      $err->{idx} =~ s/(^\s|\s\Z)//g;  # Remove leading and trailing spaces.
-      $err->{idx} =~ s/(FOO )+/FOO /g; # Collapse FOOs.
-      $err->{idx} =~ s/FOO FOO/FOO/g;  # Collapse FOOs.
-
-      my @offsets = (
-                     $File->{Offsets}->[$err->{line}    ]->[0],
-                     $File->{Offsets}->[$err->{line} - 1]->[1],
-                     $File->{Offsets}->[$err->{line} - 1]->[1] + $err->{char}
-                    );
-      printf <<".EOF.", &ent($File->{URI}), &ent($err->{msg});
-    <asserts>
-      <Assertion rdf:ID="err$errnum">
-        <subject rdf:parseType="Resource">
-          <reprOf rdf:resource="%s"/>
-          <val:line>$err->{line}</val:line>
-          <val:column>$err->{char}</val:column>
-          <val:offset>@offsets</val:offset>
-        </subject>
-        <result rdf:resource="http://www.w3.org/2003/03/earl/1.00#fails" />
-        <testCase rdf:resource="http://www.w3.org/Markup/" />
-        <note>%s</note>
-      </Assertion>
-    </asserts>
-.EOF.
-    }
-  } else {
-    printf <<".EOF.", &ent($File->{URI});
-  <asserts>
-   <Assertion>
-    <subject rdf:resource="%s" />
-    <result rdf:resource="http://www.w3.org/2001/03/earl/1.00#passes" />
-    <testCase rdf:resource="http://www.w3.org/MarkUp/" />
-    <note>Valid!</note>
-   </Assertion>
-  </asserts>
-.EOF.
-  }
-
-  print <<".EOF.";
-  </Assertor>
-</rdf:RDF>
-.EOF.
-}
-
-#
-# Return a Notation3 EARL report for the page.
-#
-# @@TODO: escape output;
-sub report_n3 {
-  my $File = shift;
-
-  my $valid = ($File->{'Is Valid'} ? 'Valid' : 'Invalid');
-  my $errs  = ($File->{'Is Valid'} ? '0' : scalar @{$File->{Errors}});
-  if ($File->{E}->param('fatal_http_error')) {
-    $valid = 'Could not validate';
-  }
-
-  print <<".EOF.";
-Content-Type: text/plain; charset=UTF-8
-X-W3C-Validator-Status: $valid
-X-W3C-Validator-Errors: $errs
-
-\@prefix earl: <http://www.w3.org/2001/03/earl/1.0-test#> .
-\@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
-\@prefix val: <http://validator.w3.org/this_will_change/do_not_rely_on_it!> .
-
-<http://validator.w3.org/> a earl:Assertor;
-  earl:name "W3 Validator";
-  earl:asserts
-.EOF.
-
-  unless ($File->{'Is Valid'}) {
-    for (my $i = 0; $i <= scalar @{$File->{Errors}}; $i++) {
-      my $err = $File->{Errors}->[$i];
-      chomp $err->{msg};
-
-      # Find index into the %frag hash for the "explanation..." links.
-      $err->{idx} =  $err->{msg};
-      $err->{idx} =~ s/"[^\"]*"/FOO/g;
-      $err->{idx} =~ s/[^A-Za-z ]//g;
-      $err->{idx} =~ s/\s+/ /g;        # Collapse spaces
-      $err->{idx} =~ s/(^\s|\s\Z)//g;  # Remove leading and trailing spaces.
-      $err->{idx} =~ s/(FOO )+/FOO /g; # Collapse FOOs.
-      $err->{idx} =~ s/FOO FOO/FOO/g;  # Collapse FOOs.
-
-      my @offsets = (
-                     $File->{Offsets}->[$err->{line}    ]->[0],
-                     $File->{Offsets}->[$err->{line} - 1]->[1],
-                     $File->{Offsets}->[$err->{line} - 1]->[1] + $err->{char}
-                    );
-      print <<".EOF.";
-    [
-      earl:testMode earl:Auto;
-      rdf:predicate earl:fails;
-      rdf:subject [
-                    val:column "$err->{char}";
-                    val:line   "$err->{line}";
-                    val:offset "@offsets";
-                    earl:testSubject <$File->{URI}>
-                  ];
-      rdf:object [
-                   earl:id <http://www.w3.org/HTML/>;
-                   earl:note """$err->{msg} """
-                 ]
-.EOF.
-
-     if ($i == scalar @{$File->{Errors}}) {
-       print "    ]\n";
-     } else {
-       print "    ],\n";
-     }
-    }
-  } else {
-    print <<".EOF.";
-    [
-      earl:testMode earl:Auto;
-      rdf:predicate earl:passes;
-      rdf:subject   [earl:testSubject <$File->{URI}>];
-      rdf:object    [
-                      earl:id <http://www.w3.org/HTML/>;
-                      earl:note "Valid"
-                    ]
-    ]
-.EOF.
-  }
-  print " .\n";
-}
-
-#
 # Autodetection as in Appendix F of the XML 1.0 Recommendation.
 # <http://www.w3.org/TR/2000/REC-xml-20001006#sec-guessing>
 #

Received on Friday, 13 October 2006 07:16:35 UTC