validator/httpd/cgi-bin check,1.583,1.584

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

Modified Files:
	check 
Log Message:

* Renaming the W3C::Validator::SAXHandler (misleading name) module
as what it is, some hybrid event handler.

* Giving W3C::Validator::EventHandler a proper namespace and packacge
structure. 

* subclassing W3C::Validator::EventHandler with extra handling of char/data
for the creation of the document outline => W3C::Validator::EventHandler::Outliner

We could move the various packages out of the check script and into their own cpan'd 
modules, I guess... 




Index: check
===================================================================
RCS file: /sources/public/validator/httpd/cgi-bin/check,v
retrieving revision 1.583
retrieving revision 1.584
diff -u -d -r1.583 -r1.584
--- check	13 Feb 2008 05:57:24 -0000	1.583
+++ check	23 Apr 2008 04:23:31 -0000	1.584
@@ -651,7 +651,7 @@
 # preparse with XML parser if necessary
 # we should really be using a SAX ErrorHandler, but I can't find
 # a way to make it work with XML::LibXML::SAX::Parser... ** FIXME **
-# ditto, we should try using W3C::Validator::SAXHandler,
+# ditto, we should try using W3C::Validator::EventHandler,
 # but it's badly linked to opensp at the moment
 if (&is_xml($File)) {
 
@@ -847,7 +847,13 @@
              ],
             );
 
-  my $h = W3C::Validator::SAXHandler->new($opensp, $File);
+  my $h; # event handler
+  if ($File->{Opt}->{'Outline'}) {
+      $h = W3C::Validator::EventHandler::Outliner->new($opensp, $File, $CFG);
+  }
+  else {
+      $h = W3C::Validator::EventHandler->new($opensp, $File, $CFG);      
+  }
 
   $opensp->handler($h);
   $opensp->parse_string(join"\n",@{$File->{Content}});
@@ -2521,47 +2527,39 @@
 
 #####
 
-sub W3C::Validator::SAXHandler::new
+package W3C::Validator::EventHandler;
+#
+# Define global constants
+use constant TRUE  => 1;
+use constant FALSE => 0;
+
+#
+# Tentative Validation Severities.
+use constant T_WARN  =>  4; # 0000 0100
+use constant T_ERROR =>  8; # 0000 1000
+
+#
+# Output flags for error processing
+use constant O_SOURCE  => 1; # 0000 0001
+use constant O_CHARSET => 2; # 0000 0010
+use constant O_DOCTYPE => 4; # 0000 0100
+use constant O_NONE    => 8; # 0000 1000
+
+
+sub new
 {
   my $class = shift;
   my $parser = shift;
   my $File = shift;
-
-  my $self = { _file => $File, _parser => $parser,
-    current_heading_level => 0, am_in_heading => 0  };
-
+  my $CFG = shift;
+  my $self = { _file => $File, CFG => $CFG, _parser => $parser };
   bless $self, $class;
 }
 
-sub W3C::Validator::SAXHandler::characters
-{
-  my ($self, $chars) = @_;
-  if ($self->{am_in_heading} == 1) {
-    my $data = $chars->{Data};
-    $data =~  s/[\r|\n]/ /g;
-    $self->{_file}->{heading_outline} .= $data;
-  }
-}
-
-sub W3C::Validator::SAXHandler::data
-{
-  my ($self, $chars) = @_;
-  if ($self->{am_in_heading} == 1) {
-    my $data = $chars->{Data};
-    $data =~  s/[\r|\n]/ /g;
-    $self->{_file}->{heading_outline} .= $data;
-  }
-}
 
-sub W3C::Validator::SAXHandler::start_element
+sub start_element
 {
   my ($self, $element) = @_;
-  if ($element->{Name} =~ /^h([1-6])$/i) {
-    $self->{_file}->{heading_outline} ||= "";
-    $self->{_file}->{heading_outline} .=
-      "    " x int($1) . "[$element->{Name}] ";
-    $self->{am_in_heading} = 1;
-  }
 
   my $has_xmlns = FALSE;
   my $xmlns_value = undef;
@@ -2594,12 +2592,12 @@
 
   my $doctype = $self->{_file}->{DOCTYPE};
 
-  if (!defined($CFG->{Types}->{$doctype}->{Name}) ||
-      $element->{Name} ne $CFG->{Types}->{$doctype}->{Name}) {
+  if (!defined($self->{CFG}->{Types}->{$doctype}->{Name}) ||
+      $element->{Name} ne $self->{CFG}->{Types}->{$doctype}->{Name}) {
     # add to list of non-root namespaces
     push(@{$self->{_file}->{Namespaces}}, $xmlns_value) if $has_xmlns;
   }
-  elsif (!$has_xmlns and $CFG->{Types}->{$doctype}->{"Namespace Required"}) {
+  elsif (!$has_xmlns and $self->{CFG}->{Types}->{$doctype}->{"Namespace Required"}) {
     # whine if the root xmlns attribute is noted as required by spec,
     # but not present
     my $err;
@@ -2610,15 +2608,15 @@
     $err->{num}  = "no-xmlns";
     $err->{type} = "E";
     $err->{msg}  = "Missing xmlns attribute for element ".$element->{Name} . ". 
-    The value should be: $CFG->{Types}->{$doctype}->{Namespace}";
+    The value should be: $self->{CFG}->{Types}->{$doctype}->{Namespace}";
     
 
     # ...
     $self->{_file}->{'Is Valid'} = FALSE;
     push @{$self->{_file}->{Errors}}, $err;
   }
-  elsif ($has_xmlns and (defined $CFG->{Types}->{$doctype}->{Namespace})
-  and ($xmlns_value ne $CFG->{Types}->{$doctype}->{Namespace}) ) {
+  elsif ($has_xmlns and (defined $self->{CFG}->{Types}->{$doctype}->{Namespace})
+  and ($xmlns_value ne $self->{CFG}->{Types}->{$doctype}->{Namespace}) ) {
     # whine if root xmlns element is not the one specificed by the spec
     my $err;
     my $location = $self->{_parser}->get_location();
@@ -2628,7 +2626,7 @@
     $err->{num}  = "wrong-xmlns";
     $err->{type} = "E";
     $err->{msg}  = "Wrong xmlns attribute for element $element->{Name}. ".
-      "The value should be: $CFG->{Types}->{$doctype}->{Namespace}";
+      "The value should be: $self->{CFG}->{Types}->{$doctype}->{Namespace}";
 
     # ...
     $self->{_file}->{'Is Valid'} = FALSE;
@@ -2637,17 +2635,7 @@
 }
 
 
-sub W3C::Validator::SAXHandler::end_element
-{
-  my ($self, $element) = @_;
-  if ($element->{Name} =~ /^h[1-6]$/i) {
-    $self->{_file}->{heading_outline} .= "\n";
-    $self->{am_in_heading} = 0;
-  }
-
-}
-
-sub W3C::Validator::SAXHandler::error
+sub error
 {
   my $self = shift;
   my $error = shift;
@@ -2766,6 +2754,84 @@
   }
 }
 
+package W3C::Validator::EventHandler::Outliner;
+#
+# Define global constants
+use constant TRUE  => 1;
+use constant FALSE => 0;
+
+#
+# Tentative Validation Severities.
+use constant T_WARN  =>  4; # 0000 0100
+use constant T_ERROR =>  8; # 0000 1000
+
+#
+# Output flags for error processing
+use constant O_SOURCE  => 1; # 0000 0001
+use constant O_CHARSET => 2; # 0000 0010
+use constant O_DOCTYPE => 4; # 0000 0100
+use constant O_NONE    => 8; # 0000 1000
+
+use base qw(W3C::Validator::EventHandler);
+
+sub new
+{
+  my $class = shift;
+  my $parser = shift;
+  my $File = shift;
+  my $CFG = shift;
+  my $self = $class->SUPER::new($parser, $File, $CFG);
+  $self->{current_heading_level}= 0;
+  $self->{am_in_heading} = 0;
+  bless $self, $class;
+}
+
+sub characters
+{
+  my ($self, $chars) = @_;
+  if ($self->{am_in_heading} == 1) {
+    my $data = $chars->{Data};
+    $data =~  s/[\r|\n]/ /g;
+    $self->{_file}->{heading_outline} .= $data;
+  }
+}
+
+sub data
+{
+  my ($self, $chars) = @_;
+  if ($self->{am_in_heading} == 1) {
+    my $data = $chars->{Data};
+    $data =~  s/[\r|\n]/ /g;
+    $self->{_file}->{heading_outline} .= $data;
+  }
+}
+
+sub start_element
+{
+  my ($self, $element) = @_;
+  if ($element->{Name} =~ /^h([1-6])$/i) {
+    $self->{_file}->{heading_outline} ||= "";
+    $self->{_file}->{heading_outline} .=
+      "    " x int($1) . "[$element->{Name}] ";
+    $self->{am_in_heading} = 1;
+  }
+
+return $self->SUPER::start_element($element)
+  
+}
+
+
+sub end_element
+{
+  my ($self, $element) = @_;
+  if ($element->{Name} =~ /^h[1-6]$/i) {
+    $self->{_file}->{heading_outline} .= "\n";
+    $self->{am_in_heading} = 0;
+  }
+
+}
+
+
 #####
 
 package W3C::Validator::UserAgent;

Received on Wednesday, 23 April 2008 04:24:06 UTC