#!/usr/bin/perl
use strict;
use utf8;
use lib qw[/home/httpd/html/www/markup/html/whatpm
           /home/wakaba/work/manakai2/lib];
use CGI::Carp qw[fatalsToBrowser];
use Scalar::Util qw[refaddr];
use Time::HiRes qw/time/;
sub htescape ($) {
  my $s = $_[0];
  $s =~ s/&/&/g;
  $s =~ s/</g;
  $s =~ s/>/>/g;
  $s =~ s/"/"/g;
  $s =~ s{([\x00-\x09\x0B-\x1F\x7F-\xA0\x{FEFF}\x{FFFC}-\x{FFFF}])}{
    sprintf 'U+%04X ', ord $1;
  }ge;
  return $s;
} # htescape
  my @nav;
  my %time;
  require Message::DOM::DOMImplementation;
  my $dom = Message::DOM::DOMImplementation->new;
{
  use Message::CGI::HTTP;
  my $http = Message::CGI::HTTP->new;
  if ($http->get_meta_variable ('PATH_INFO') ne '/') {
    print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400";
    exit;
  }
  binmode STDOUT, ':utf8';
  $| = 1;
  load_text_catalog ('en'); ## TODO: conneg
  print STDOUT qq[Content-Type: text/html; charset=utf-8
Web Document Conformance Checker (BETA) 
Request URI 
    <@{[htescape $input->{request_uri}]} >Document URI 
    <@{[htescape $input->{uri}]} >
      yet
  push @nav, ['#document-info' => 'Information'];
if (defined $input->{s}) {
  $char_length = length $input->{s};
  print STDOUT qq[
Base URI 
    <@{[htescape $input->{base_uri}]} >Internet Media Type 
    @{[htescape $input->{media_type}]}
    @{[$input->{media_type_overridden} ? '(overridden) ' : defined $input->{official_type} ? $input->{media_type} eq $input->{official_type} ? '' : '(sniffed; official type is: '.htescape ($input->{official_type}).')' : '(sniffed) ']} Character Encoding 
    @{[defined $input->{charset} ? ''.htescape ($input->{charset}).'' : '(none)']}
    @{[$input->{charset_overridden} ? '(overridden) ' : '']} 
Length 
    $char_length byte@{[$char_length == 1 ? '' : 's']} 
 
];
  $input->{id_prefix} = '';
  #$input->{nested} = 0;
  my $result = {conforming_min => 1, conforming_max => 1};
  check_and_print ($input => $result);
  print_result_section ($result);
} else {
  print STDOUT qq[];
  print_result_input_error_section ($input);
}
  print STDOUT qq[
];
  for (@nav) {
    print STDOUT qq[$_->[1]  
];
  for (qw/decode parse parse_html parse_xml parse_manifest
          check check_manifest/) {
    next unless defined $time{$_};
    open my $file, '>>', ".cc-$_.txt" or die ".cc-$_.txt: $!";
    print $file $char_length, "\t", $time{$_}, "\n";
  }
exit;
}
sub add_error ($$$) {
  my ($layer, $err, $result) = @_;
  if (defined $err->{level}) {
    if ($err->{level} eq 's') {
      $result->{$layer}->{should}++;
      $result->{$layer}->{score_min} -= 2;
      $result->{conforming_min} = 0;
    } elsif ($err->{level} eq 'w' or $err->{level} eq 'g') {
      $result->{$layer}->{warning}++;
    } elsif ($err->{level} eq 'u' or $err->{level} eq 'unsupported') {
      $result->{$layer}->{unsupported}++;
      $result->{unsupported} = 1;
    } elsif ($err->{level} eq 'i') {
      #
    } else {
      $result->{$layer}->{must}++;
      $result->{$layer}->{score_max} -= 2;
      $result->{$layer}->{score_min} -= 2;
      $result->{conforming_min} = 0;
      $result->{conforming_max} = 0;
    }
  } else {
    $result->{$layer}->{must}++;
    $result->{$layer}->{score_max} -= 2;
    $result->{$layer}->{score_min} -= 2;
    $result->{conforming_min} = 0;
    $result->{conforming_max} = 0;
  }
} # add_error
sub check_and_print ($$) {
  my ($input, $result) = @_;
  print_http_header_section ($input, $result);
  my $doc;
  my $el;
  my $cssom;
  my $manifest;
  my @subdoc;
  if ($input->{media_type} eq 'text/html') {
    ($doc, $el) = print_syntax_error_html_section ($input, $result);
    print_source_string_section
        ($input,
         \($input->{s}),
         $input->{charset} || $doc->input_encoding);
  } elsif ({
            'text/xml' => 1,
            'application/atom+xml' => 1,
            'application/rss+xml' => 1,
            'application/svg+xml' => 1,
            'application/xhtml+xml' => 1,
            'application/xml' => 1,
           }->{$input->{media_type}}) {
    ($doc, $el) = print_syntax_error_xml_section ($input, $result);
    print_source_string_section ($input,
                                 \($input->{s}),
                                 $doc->input_encoding);
  } elsif ($input->{media_type} eq 'text/css') {
    $cssom = print_syntax_error_css_section ($input, $result);
    print_source_string_section
        ($input, \($input->{s}),
         $cssom->manakai_input_encoding);
  } elsif ($input->{media_type} eq 'text/cache-manifest') {
## TODO: MUST be text/cache-manifest
    $manifest = print_syntax_error_manifest_section ($input, $result);
    print_source_string_section ($input, \($input->{s}),
                                 'utf-8');
  } else {
    ## TODO: Change HTTP status code??
    print_result_unknown_type_section ($input, $result);
  }
  if (defined $doc or defined $el) {
    $doc->document_uri ($input->{uri});
    $doc->manakai_entity_base_uri ($input->{base_uri});
    print_structure_dump_dom_section ($input, $doc, $el);
    my $elements = print_structure_error_dom_section
        ($input, $doc, $el, $result, sub {
          push @subdoc, shift;
        });
    print_table_section ($input, $elements->{table}) if @{$elements->{table}};
    print_listing_section ({
      id => 'identifiers', label => 'IDs', heading => 'Identifiers',
    }, $input, $elements->{id}) if keys %{$elements->{id}};
    print_listing_section ({
      id => 'terms', label => 'Terms', heading => 'Terms',
    }, $input, $elements->{term}) if keys %{$elements->{term}};
    print_listing_section ({
      id => 'classes', label => 'Classes', heading => 'Classes',
    }, $input, $elements->{class}) if keys %{$elements->{class}};
  } elsif (defined $cssom) {
    print_structure_dump_cssom_section ($input, $cssom);
    ## TODO: CSSOM validation
    add_error ('structure', {level => 'u'} => $result);
  } elsif (defined $manifest) {
    print_structure_dump_manifest_section ($input, $manifest);
    print_structure_error_manifest_section ($input, $manifest, $result);
  }
  my $id_prefix = 0;
  for my $subinput (@subdoc) {
    $subinput->{id_prefix} = 'subdoc-' . ++$id_prefix;
    $subinput->{nested} = 1;
    $subinput->{base_uri} = $subinput->{container_node}->base_uri
        unless defined $subinput->{base_uri};
    my $ebaseuri = htescape ($subinput->{base_uri});
    push @nav, ['#' . $subinput->{id_prefix} => 'Sub #' . $id_prefix];
    print STDOUT qq[];
  }
} # check_and_print
sub print_http_header_section ($$) {
  my ($input, $result) = @_;
  return unless defined $input->{header_status_code} or
      defined $input->{header_status_text} or
      @{$input->{header_field} or []};
  
  push @nav, ['#source-header' => 'HTTP Header'] unless $input->{nested};
  print STDOUT qq[];
} # print_http_header_section
sub print_syntax_error_html_section ($$) {
  my ($input, $result) = @_;
  
  require Encode;
  require Whatpm::HTML;
  
  print STDOUT qq[
];
  return ($doc, $el);
} # print_syntax_error_html_section
sub print_syntax_error_xml_section ($$) {
  my ($input, $result) = @_;
  
  require Message::DOM::XMLParserTemp;
  
  print STDOUT qq[
];
  return ($doc, undef);
} # print_syntax_error_xml_section
sub get_css_parser () {
  our $CSSParser;
  return $CSSParser if $CSSParser;
  require Whatpm::CSS::Parser;
  my $p = Whatpm::CSS::Parser->new;
  $p->{prop}->{$_} = 1 for qw/
    alignment-baseline
    background background-attachment background-color background-image
    background-position background-position-x background-position-y
    background-repeat border border-bottom border-bottom-color
    border-bottom-style border-bottom-width border-collapse border-color
    border-left border-left-color
    border-left-style border-left-width border-right border-right-color
    border-right-style border-right-width
    border-spacing -manakai-border-spacing-x -manakai-border-spacing-y
    border-style border-top border-top-color border-top-style border-top-width
    border-width bottom
    caption-side clear clip color content counter-increment counter-reset
    cursor direction display dominant-baseline empty-cells float font
    font-family font-size font-size-adjust font-stretch
    font-style font-variant font-weight height left
    letter-spacing line-height
    list-style list-style-image list-style-position list-style-type
    margin margin-bottom margin-left margin-right margin-top marker-offset
    marks max-height max-width min-height min-width opacity -moz-opacity
    orphans outline outline-color outline-style outline-width overflow
    overflow-x overflow-y
    padding padding-bottom padding-left padding-right padding-top
    page page-break-after page-break-before page-break-inside
    position quotes right size table-layout
    text-align text-anchor text-decoration text-indent text-transform
    top unicode-bidi vertical-align visibility white-space width widows
    word-spacing writing-mode z-index
  /;
  $p->{prop_value}->{display}->{$_} = 1 for qw/
    block clip inline inline-block inline-table list-item none
    table table-caption table-cell table-column table-column-group
    table-header-group table-footer-group table-row table-row-group
    compact marker
  /;
  $p->{prop_value}->{position}->{$_} = 1 for qw/
    absolute fixed relative static
  /;
  $p->{prop_value}->{float}->{$_} = 1 for qw/
    left right none
  /;
  $p->{prop_value}->{clear}->{$_} = 1 for qw/
    left right none both
  /;
  $p->{prop_value}->{direction}->{ltr} = 1;
  $p->{prop_value}->{direction}->{rtl} = 1;
  $p->{prop_value}->{marks}->{crop} = 1;
  $p->{prop_value}->{marks}->{cross} = 1;
  $p->{prop_value}->{'unicode-bidi'}->{$_} = 1 for qw/
    normal bidi-override embed
  /;
  for my $prop_name (qw/overflow overflow-x overflow-y/) {
    $p->{prop_value}->{$prop_name}->{$_} = 1 for qw/
      visible hidden scroll auto -webkit-marquee -moz-hidden-unscrollable
    /;
  }
  $p->{prop_value}->{visibility}->{$_} = 1 for qw/
    visible hidden collapse
  /;
  $p->{prop_value}->{'list-style-type'}->{$_} = 1 for qw/
    disc circle square decimal decimal-leading-zero
    lower-roman upper-roman lower-greek lower-latin
    upper-latin armenian georgian lower-alpha upper-alpha none
    hebrew cjk-ideographic hiragana katakana hiragana-iroha
    katakana-iroha
  /;
  $p->{prop_value}->{'list-style-position'}->{outside} = 1;
  $p->{prop_value}->{'list-style-position'}->{inside} = 1;
  $p->{prop_value}->{'page-break-before'}->{$_} = 1 for qw/
    auto always avoid left right
  /;
  $p->{prop_value}->{'page-break-after'}->{$_} = 1 for qw/
    auto always avoid left right
  /;
  $p->{prop_value}->{'page-break-inside'}->{auto} = 1;
  $p->{prop_value}->{'page-break-inside'}->{avoid} = 1;
  $p->{prop_value}->{'background-repeat'}->{$_} = 1 for qw/
    repeat repeat-x repeat-y no-repeat
  /;
  $p->{prop_value}->{'background-attachment'}->{scroll} = 1;
  $p->{prop_value}->{'background-attachment'}->{fixed} = 1;
  $p->{prop_value}->{'font-size'}->{$_} = 1 for qw/
    xx-small x-small small medium large x-large xx-large
    -manakai-xxx-large -webkit-xxx-large
    larger smaller
  /;
  $p->{prop_value}->{'font-style'}->{normal} = 1;
  $p->{prop_value}->{'font-style'}->{italic} = 1;
  $p->{prop_value}->{'font-style'}->{oblique} = 1;
  $p->{prop_value}->{'font-variant'}->{normal} = 1;
  $p->{prop_value}->{'font-variant'}->{'small-caps'} = 1;
  $p->{prop_value}->{'font-stretch'}->{$_} = 1 for
      qw/normal wider narrower ultra-condensed extra-condensed
        condensed semi-condensed semi-expanded expanded
        extra-expanded ultra-expanded/;
  $p->{prop_value}->{'text-align'}->{$_} = 1 for qw/
    left right center justify begin end
  /;
  $p->{prop_value}->{'text-transform'}->{$_} = 1 for qw/
    capitalize uppercase lowercase none
  /;
  $p->{prop_value}->{'white-space'}->{$_} = 1 for qw/
    normal pre nowrap pre-line pre-wrap -moz-pre-wrap
  /;
  $p->{prop_value}->{'writing-mode'}->{$_} = 1 for qw/
    lr rl tb lr-tb rl-tb tb-rl
  /;
  $p->{prop_value}->{'text-anchor'}->{$_} = 1 for qw/
    start middle end
  /;
  $p->{prop_value}->{'dominant-baseline'}->{$_} = 1 for qw/
    auto use-script no-change reset-size ideographic alphabetic
    hanging mathematical central middle text-after-edge text-before-edge
  /;
  $p->{prop_value}->{'alignment-baseline'}->{$_} = 1 for qw/
    auto baseline before-edge text-before-edge middle central
    after-edge text-after-edge ideographic alphabetic hanging
    mathematical
  /;
  $p->{prop_value}->{'text-decoration'}->{$_} = 1 for qw/
    none blink underline overline line-through
  /;
  $p->{prop_value}->{'caption-side'}->{$_} = 1 for qw/
    top bottom left right
  /;
  $p->{prop_value}->{'table-layout'}->{auto} = 1;
  $p->{prop_value}->{'table-layout'}->{fixed} = 1;
  $p->{prop_value}->{'border-collapse'}->{collapse} = 1;
  $p->{prop_value}->{'border-collapse'}->{separate} = 1;
  $p->{prop_value}->{'empty-cells'}->{show} = 1;
  $p->{prop_value}->{'empty-cells'}->{hide} = 1;
  $p->{prop_value}->{cursor}->{$_} = 1 for qw/
    auto crosshair default pointer move e-resize ne-resize nw-resize n-resize
    se-resize sw-resize s-resize w-resize text wait help progress
  /;
  for my $prop (qw/border-top-style border-left-style
                   border-bottom-style border-right-style outline-style/) {
    $p->{prop_value}->{$prop}->{$_} = 1 for qw/
      none hidden dotted dashed solid double groove ridge inset outset
    /;
  }
  for my $prop (qw/color background-color
                   border-bottom-color border-left-color border-right-color
                   border-top-color border-color/) {
    $p->{prop_value}->{$prop}->{transparent} = 1;
    $p->{prop_value}->{$prop}->{flavor} = 1;
    $p->{prop_value}->{$prop}->{'-manakai-default'} = 1;
  }
  $p->{prop_value}->{'outline-color'}->{invert} = 1;
  $p->{prop_value}->{'outline-color'}->{'-manakai-invert-or-currentcolor'} = 1;
  $p->{pseudo_class}->{$_} = 1 for qw/
    active checked disabled empty enabled first-child first-of-type
    focus hover indeterminate last-child last-of-type link only-child
    only-of-type root target visited
    lang nth-child nth-last-child nth-of-type nth-last-of-type not
    -manakai-contains -manakai-current
  /;
  $p->{pseudo_element}->{$_} = 1 for qw/
    after before first-letter first-line
  /;
  return $CSSParser = $p;
} # get_css_parser
sub print_syntax_error_css_section ($$) {
  my ($input, $result) = @_;
  print STDOUT qq[
];
  return $cssom;
} # print_syntax_error_css_section
sub print_syntax_error_manifest_section ($$) {
  my ($input, $result) = @_;
  require Whatpm::CacheManifest;
  print STDOUT qq[
];
  return $manifest;
} # print_syntax_error_manifest_section
sub print_source_string_section ($$$) {
  my $input = shift;
  my $s;
  unless ($input->{is_char_string}) {
    require Encode;
    my $enc = Encode::find_encoding ($_[1]); ## TODO: charset name -> Perl name
    return unless $enc;
    $s = \($enc->decode (${$_[0]}));
  } else {
    $s = $_[0];
  }
  my $i = 1;                             
  push @nav, ['#source-string' => 'Source'] unless $input->{nested};
  print STDOUT qq[
";
} # print_input_string_section
sub print_document_tree ($$) {
  my ($input, $node) = @_;
  my $r = '';
  my @node = ($node);
  while (@node) {
    my $child = shift @node;
    unless (ref $child) {
      $r .= $child;
      next;
    }
    my $node_id = $input->{id_prefix} . 'node-'.refaddr $child;
    my $nt = $child->node_type;
    if ($nt == $child->ELEMENT_NODE) {
      my $child_nsuri = $child->namespace_uri;
      $r .= qq[] . htescape ($child->tag_name) .
          ''; ## ISSUE: case
      if ($child->has_attributes) {
        $r .= '';
        for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] }
                      @{$child->attributes}) {
          $r .= qq[] . htescape ($attr->[0]) . ' = '; ## ISSUE: case?
          $r .= '' . htescape ($attr->[1]) . '  ';
      }
      if ($child->has_child_nodes) {
        $r .= '';
        unshift @node, @{$child->child_nodes}, ' ' . htescape ($child->data) . ' <[CDATA[' . htescape ($child->data) . ' ]]>Document';
      $r .= qq[];
      my $cp = $child->manakai_charset;
      if (defined $cp) {
        $r .= qq[charset parameter = ];
        $r .= htescape ($cp) . qq[inputEncoding = ];
      my $ie = $child->input_encoding;
      if (defined $ie) {
        $r .= qq[@{[htescape ($ie)]}];
        if ($child->manakai_has_bom) {
          $r .= qq[ (with BOM null)];
      }
      $r .= qq[@{[scalar get_text ('manakaiIsHTML:'.($child->manakai_is_html?1:0))]} ];
      $r .= qq[@{[scalar get_text ('manakaiCompatMode:'.$child->manakai_compat_mode)]} ];
      unless ($child->manakai_is_html) {
        $r .= qq[XML version = @{[htescape ($child->xml_version)]} ];
        if (defined $child->xml_encoding) {
          $r .= qq[XML encoding = @{[htescape ($child->xml_encoding)]} ];
        } else {
          $r .= qq[XML encoding = (null) ];
        }
        $r .= qq[XML standalone = @{[$child->xml_standalone ? 'true' : 'false']} ];
      }
      $r .= qq[ ];
      if ($child->has_child_nodes) {
        $r .= '';
        unshift @node, @{$child->child_nodes}, '  ';
      }
    } elsif ($nt == $child->DOCUMENT_TYPE_NODE) {
      $r .= qq'<!DOCTYPE>';
      $r .= qq[Name = @{[htescape ($child->name)]}  ];
      $r .= qq[Public identifier = @{[htescape ($child->public_id)]}  ];
      $r .= qq[System identifier = @{[htescape ($child->system_id)]}  ];
      $r .= ' <?@{[htescape ($child->target)]} @{[htescape ($child->data)]} ?>@{[$child->node_type]} @{[htescape ($child->node_name)]} '; # error
    }
  }
  $r .= ' ';
  print STDOUT $r;
} # print_document_tree
sub print_structure_dump_dom_section ($$$) {
  my ($input, $doc, $el) = @_;
  print STDOUT qq[
Document Tree 
];
  push @nav, [qq[#$input->{id_prefix}document-tree] => 'Tree']
      unless $input->{nested};
  print_document_tree ($input, $el || $doc);
  print STDOUT qq[];
} # print_structure_dump_dom_section
sub print_structure_dump_cssom_section ($$) {
  my ($input, $cssom) = @_;
  print STDOUT qq[
];
} # print_structure_dump_cssom_section
sub print_structure_dump_manifest_section ($$) {
  my ($input, $manifest) = @_;
  print STDOUT qq[
];
} # print_structure_dump_manifest_section
sub print_structure_error_dom_section ($$$$$) {
  my ($input, $doc, $el, $result, $onsubdoc) = @_;
  print STDOUT qq[];
  return $elements;
} # print_structure_error_dom_section
sub print_structure_error_manifest_section ($$$) {
  my ($input, $manifest, $result) = @_;
  print STDOUT qq[];
} # print_structure_error_manifest_section
sub print_table_section ($$) {
  my ($input, $tables) = @_;
  
  push @nav, [qq[#$input->{id_prefix}tables] => 'Tables']
      unless $input->{nested};
  print STDOUT qq[
];
} # print_table_section
sub print_listing_section ($$$) {
  my ($opt, $input, $ids) = @_;
  
  push @nav, ['#' . $input->{id_prefix} . $opt->{id} => $opt->{label}]
      unless $input->{nested};
  print STDOUT qq[
];
} # print_listing_section
sub print_result_section ($) {
  my $result = shift;
  print STDOUT qq[
Result ];
  if ($result->{unsupported} and $result->{conforming_max}) {  
    print STDOUT qq[
The conformance
        checker cannot decide whether the document is conforming or
        not, since the document contains one or more unsupported
        features.  The document might or might not be conforming.
];
  } elsif ($result->{conforming_min}) {
    print STDOUT qq[
No conformance-error is
        found in this document.
];
  } elsif ($result->{conforming_max}) {
    print STDOUT qq[
This document
        is likely non -conforming , but in rare case
        it might be conforming.
];
  } else {
    print STDOUT qq[
This document is 
        non -conforming
];
  }
  print STDOUT qq[
MUST ‐level
ErrorsSHOULD ‐level
ErrorsWarnings Score  ];
  my $must_error = 0;
  my $should_error = 0;
  my $warning = 0;
  my $score_min = 0;
  my $score_max = 0;
  my $score_base = 20;
  my $score_unit = $score_base / 100;
  for (
    [Transfer => 'transfer', ''],
    [Character => 'char', ''],
    [Syntax => 'syntax', '#parse-errors'],
    [Structure => 'structure', '#document-errors'],
  ) {
    $must_error += ($result->{$_->[1]}->{must} += 0);
    $should_error += ($result->{$_->[1]}->{should} += 0);
    $warning += ($result->{$_->[1]}->{warning} += 0);
    $score_min += (($result->{$_->[1]}->{score_min} *= $score_unit) += $score_base);
    $score_max += (($result->{$_->[1]}->{score_max} *= $score_unit) += $score_base);
    my $uncertain = $result->{$_->[1]}->{unsupported} ? '?' : '';
    my $label = $_->[0];
    if ($result->{$_->[1]}->{must} or
        $result->{$_->[1]}->{should} or
        $result->{$_->[1]}->{warning} or
        $result->{$_->[1]}->{unsupported}) {
      $label = qq[$label ];
    }
    print STDOUT qq[$label $result->{$_->[1]}->{must}$uncertain $result->{$_->[1]}->{should}$uncertain $result->{$_->[1]}->{warning}$uncertain ];
    if ($uncertain) {
      print qq[−∞..$result->{$_->[1]}->{score_max} ];
    } elsif ($result->{$_->[1]}->{score_min} != $result->{$_->[1]}->{score_max}) {
      print qq[$result->{$_->[1]}->{score_min}..$result->{$_->[1]}->{score_max} $result->{$_->[1]}->{score_min} ];
    }
  }
  $score_max += $score_base;
  print STDOUT qq[
Semantics 0? 0? 0? −∞..$score_base  
Total 
$must_error? 
$should_error? 
$warning? 
−∞..$score_max 
Important : This conformance checking service
is under development .  The result above might be wrong .
 ];
  push @nav, ['#result-summary' => 'Result'];
} # print_result_section
sub print_result_unknown_type_section ($$) {
  my ($input, $result) = @_;
  my $euri = htescape ($input->{uri});
  print STDOUT qq[
];
  push @nav, [qq[#$input->{id_prefix}parse-errors] => 'Errors']
      unless $input->{nested};
  add_error (char => {level => 'u'} => $result);
  add_error (syntax => {level => 'u'} => $result);
  add_error (structure => {level => 'u'} => $result);
} # print_result_unknown_type_section
sub print_result_input_error_section ($) {
  my $input = shift;
  print STDOUT qq[
Input Error : @{[htescape ($input->{error_status_text})]}
 ];
  push @nav, ['#result-summary' => 'Result'];
} # print_result_input_error_section
sub get_error_label ($$) {
  my ($input, $err) = @_;
  my $r = '';
  my $line;
  my $column;
    
  if (defined $err->{node}) {
    $line = $err->{node}->get_user_data ('manakai_source_line');
    if (defined $line) {
      $column = $err->{node}->get_user_data ('manakai_source_column');
    } else {
      if ($err->{node}->node_type == $err->{node}->ATTRIBUTE_NODE) {
        my $owner = $err->{node}->owner_element;
        $line = $owner->get_user_data ('manakai_source_line');
        $column = $owner->get_user_data ('manakai_source_column');
      } else {
        my $parent = $err->{node}->parent_node;
        if ($parent) {
          $line = $parent->get_user_data ('manakai_source_line');
          $column = $parent->get_user_data ('manakai_source_column');
        }
      }
    }
  }
  unless (defined $line) {
    if (defined $err->{token} and defined $err->{token}->{line}) {
      $line = $err->{token}->{line};
      $column = $err->{token}->{column};
    } elsif (defined $err->{line}) {
      $line = $err->{line};
      $column = $err->{column};
    }
  }
 
  if (defined $line) {
    if (defined $column and $column > 0) {
      $r = qq[Line $line  column $column];
    } else {
      $line = $line - 1 || 1;
      $r = qq[Line $line ];
    }
  }
  if (defined $err->{node}) {
    $r .= ' ' if length $r;
    $r .= get_node_link ($input, $err->{node});
  }
  if (defined $err->{index}) {
    if (length $r) {
      $r .= ', Index ' . (0+$err->{index});
    } else {
      $r .= "Index "
          . (0+$err->{index}) . ' ';
    }
  }
  if (defined $err->{value}) {
    $r .= ' ' if length $r;
    $r .= '' . htescape ($err->{value}) . 'MUST ‐level
        errorSHOULD ‐level
        errorWarning Not
        supported Information $elevel ] .
      htescape (get_node_path ($_[1])) . qq[ ];
} # get_node_link
{
  my $Msg = {};
sub load_text_catalog ($) {
  my $lang = shift; # MUST be a canonical lang name
  open my $file, '<:utf8', "cc-msg.$lang.txt"
      or die "$0: cc-msg.$lang.txt: $!";
  while (<$file>) {
    if (s/^([^;]+);([^;]*);//) {
      my ($type, $cls, $msg) = ($1, $2, $_);
      $msg =~ tr/\x0D\x0A//d;
      $Msg->{$type} = [$cls, $msg];
    }
  }
} # load_text_catalog
sub get_text ($) {
  my ($type, $level, $node) = @_;
  $type = $level . ':' . $type if defined $level;
  $level = 'm' unless defined $level;
  my @arg;
  {
    if (defined $Msg->{$type}) {
      my $msg = $Msg->{$type}->[1];
      $msg =~ s{\$([0-9]+) }{
        defined $arg[$1] ? htescape ($arg[$1]) : '(undef)';
      }ge;
      $msg =~ s{{\@([A-Za-z0-9:_.-]+)} }{
        UNIVERSAL::can ($node, 'get_attribute_ns')
            ? htescape ($node->get_attribute_ns (undef, $1)) : ''
      }ge;
      $msg =~ s{{\@} }{
        UNIVERSAL::can ($node, 'value') ? htescape ($node->value) : ''
      }ge;
      $msg =~ s{{local-name} }{
        UNIVERSAL::can ($node, 'manakai_local_name')
          ? htescape ($node->manakai_local_name) : ''
      }ge;
      $msg =~ s{{element-local-name} }{
        (UNIVERSAL::can ($node, 'owner_element') and
         $node->owner_element)
          ? htescape ($node->owner_element->manakai_local_name)
          : ''
      }ge;
      return ($type, 'level-' . $level . ' ' . $Msg->{$type}->[0], $msg);
    } elsif ($type =~ s/:([^:]*)$//) {
      unshift @arg, $1;
      redo;
    }
  }
  return ($type, 'level-'.$level, htescape ($_[0]));
} # get_text
}
sub get_input_document ($$) {
  my ($http, $dom) = @_;
  my $request_uri = $http->get_parameter ('uri');
  my $r = {};
  if (defined $request_uri and length $request_uri) {
    my $uri = $dom->create_uri_reference ($request_uri);
    unless ({
             http => 1,
            }->{lc $uri->uri_scheme}) {
      return {uri => $request_uri, request_uri => $request_uri,
              error_status_text => 'URI scheme not allowed'};
    }
    require Message::Util::HostPermit;
    my $host_permit = new Message::Util::HostPermit;
    $host_permit->add_rule (<check ($uri->uri_host, $uri->uri_port || 80)) {
      return {uri => $request_uri, request_uri => $request_uri,
              error_status_text => 'Connection to the host is forbidden'};
    }
    require LWP::UserAgent;
    my $ua = WDCC::LWPUA->new;
    $ua->{wdcc_dom} = $dom;
    $ua->{wdcc_host_permit} = $host_permit;
    $ua->agent ('Mozilla'); ## TODO: for now.
    $ua->parse_head (0);
    $ua->protocols_allowed ([qw/http/]);
    $ua->max_size (1000_000);
    my $req = HTTP::Request->new (GET => $request_uri);
    $req->header ('Accept-Encoding' => 'identity, *; q=0');
    my $res = $ua->request ($req);
    ## TODO: 401 sets |is_success| true.
    if ($res->is_success or $http->get_parameter ('error-page')) {
      $r->{base_uri} = $res->base; ## NOTE: It does check |Content-Base|, |Content-Location|, and .
=head1 LICENSE
Copyright 2007-2008 Wakaba 
This library is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
=cut
## $Date: 2008/03/17 13:52:48 $