1 |
#!/usr/bin/perl -d:DProf |
2 |
use strict; |
3 |
|
4 |
use lib qw[/home/httpd/html/www/markup/html/whatpm |
5 |
/home/wakaba/work/manakai/lib |
6 |
/home/wakaba/public_html/-temp/wiki/lib]; |
7 |
use CGI::Carp qw[fatalsToBrowser]; |
8 |
use Time::HiRes qw/time/; |
9 |
|
10 |
use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module |
11 |
|
12 |
my $http = SuikaWiki::Input::HTTP->new; |
13 |
|
14 |
## TODO: _charset_ |
15 |
|
16 |
my @mode = split m#/#, scalar $http->meta_variable ('PATH_INFO'), -1; |
17 |
shift @mode if @mode and $mode[0] == ''; |
18 |
## TODO: decode unreserved characters |
19 |
|
20 |
my $s = $http->parameter ('s'); |
21 |
if (length $s > 1000_000) { |
22 |
print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long"; |
23 |
exit; |
24 |
} |
25 |
my $char_length = length $s; |
26 |
my %time; |
27 |
my $time1; |
28 |
my $time2; |
29 |
|
30 |
require Message::DOM::DOMImplementation; |
31 |
my $dom = Message::DOM::DOMImplementation->____new; |
32 |
# $| = 1; |
33 |
my $doc; |
34 |
my $el; |
35 |
|
36 |
if (@mode == 3 and $mode[0] eq 'html' and |
37 |
($mode[2] eq 'html' or $mode[2] eq 'test')) { |
38 |
print STDOUT "Content-Type: text/plain; charset=utf-8\n\n"; |
39 |
|
40 |
require Encode; |
41 |
require Whatpm::HTML; |
42 |
|
43 |
$time1 = time; |
44 |
$s = Encode::decode ('utf-8', $s); |
45 |
$time2 = time; |
46 |
$time{decode} = $time2 - $time1; |
47 |
|
48 |
|
49 |
print STDOUT "#errors\n"; |
50 |
|
51 |
my $onerror = sub { |
52 |
my (%opt) = @_; |
53 |
print STDOUT "$opt{line},$opt{column},$opt{type}\n"; |
54 |
}; |
55 |
|
56 |
$doc = $dom->create_document; |
57 |
$time1 = time; |
58 |
if (length $mode[1]) { |
59 |
$el = $doc->create_element_ns |
60 |
('http://www.w3.org/1999/xhtml', [undef, $mode[1]]); |
61 |
Whatpm::HTML->set_inner_html ($el, $s, $onerror); |
62 |
} else { |
63 |
Whatpm::HTML->parse_string ($s => $doc, $onerror); |
64 |
} |
65 |
$time2 = time; |
66 |
$time{parse} = $time2 - $time1; |
67 |
|
68 |
print "#document\n"; |
69 |
|
70 |
my $out; |
71 |
if ($mode[2] eq 'html') { |
72 |
$time1 = time; |
73 |
$out = Whatpm::HTML->get_inner_html ($el || $doc); |
74 |
$time2 = time; |
75 |
$time{serialize_html} = $time2 - $time1; |
76 |
} else { # test |
77 |
$time1 = time; |
78 |
$out = test_serialize ($el || $doc); |
79 |
$time2 = time; |
80 |
$time{serialize_test} = $time2 - $time1; |
81 |
} |
82 |
print STDOUT Encode::encode ('utf-8', $$out); |
83 |
print STDOUT "\n"; |
84 |
} elsif (@mode == 3 and $mode[0] eq 'xhtml' and |
85 |
($mode[2] eq 'html' or $mode[2] eq 'test')) { |
86 |
print STDOUT "Content-Type: text/plain; charset=utf-8\n\n"; |
87 |
|
88 |
require Message::DOM::XMLParserTemp; |
89 |
print STDOUT "#errors\n"; |
90 |
|
91 |
my $onerror = sub { |
92 |
my $err = shift; |
93 |
print STDOUT $err->location->line_number, ","; |
94 |
print STDOUT $err->location->column_number, ","; |
95 |
print STDOUT $err->text, "\n"; |
96 |
return 1; |
97 |
}; |
98 |
|
99 |
open my $fh, '<', \$s; |
100 |
my $time1 = time; |
101 |
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
102 |
($fh => $dom, $onerror, charset => 'utf-8'); |
103 |
my $time2 = time; |
104 |
$time{parse_xml} = $time2 - $time1; |
105 |
|
106 |
print "#document\n"; |
107 |
|
108 |
my $out; |
109 |
if ($mode[2] eq 'html') { |
110 |
## TODO: Use XHTML serializer |
111 |
#$out = Whatpm::HTML->get_inner_html ($doc); |
112 |
} else { # test |
113 |
$time1 = time; |
114 |
$out = test_serialize ($doc); |
115 |
$time2 = time; |
116 |
$time{serialize_test} = $time2 - $time1; |
117 |
} |
118 |
print STDOUT Encode::encode ('utf-8', $$out); |
119 |
print STDOUT "\n"; |
120 |
} else { |
121 |
print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n404"; |
122 |
exit; |
123 |
} |
124 |
|
125 |
if ($http->parameter ('dom5')) { |
126 |
require Whatpm::ContentChecker; |
127 |
my $onerror = sub { |
128 |
my %opt = @_; |
129 |
print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n"; |
130 |
}; |
131 |
print STDOUT "#domerrors\n"; |
132 |
$time1 = time; |
133 |
if ($el) { |
134 |
Whatpm::ContentChecker->check_element ($el, $onerror); |
135 |
} else { |
136 |
Whatpm::ContentChecker->check_document ($doc, $onerror); |
137 |
} |
138 |
$time2 = time; |
139 |
$time{check} = $time2 - $time1; |
140 |
} |
141 |
|
142 |
print STDOUT "#log\n"; |
143 |
for (qw/decode parse parse_xml serialize_html serialize_xml serialize_test |
144 |
check/) { |
145 |
next unless defined $time{$_}; |
146 |
print STDOUT { |
147 |
decode => 'bytes->chars', |
148 |
parse => 'html5(chars)->dom5', |
149 |
parse_xml => 'xml1(chars)->dom5', |
150 |
serialize_html => 'dom5->html5(char)', |
151 |
serialize_xml => 'dom5->xml1(char)', |
152 |
serialize_test => 'dom5->test(char)', |
153 |
check => 'dom5 check', |
154 |
}->{$_}; |
155 |
print STDOUT "\t", $time{$_}, "s\n"; |
156 |
open my $file, '>>', ".manakai-$_.txt" or die ".manakai-$_.txt: $!"; |
157 |
print $file $char_length, "\t", $time{$_}, "\n"; |
158 |
} |
159 |
|
160 |
exit; |
161 |
|
162 |
sub test_serialize ($) { |
163 |
my $node = shift; |
164 |
my $r = ''; |
165 |
|
166 |
my @node = map { [$_, ''] } @{$node->child_nodes}; |
167 |
while (@node) { |
168 |
my $child = shift @node; |
169 |
my $nt = $child->[0]->node_type; |
170 |
if ($nt == $child->[0]->ELEMENT_NODE) { |
171 |
$r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case? |
172 |
|
173 |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] } |
174 |
@{$child->[0]->attributes}) { |
175 |
$r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? |
176 |
$r .= $attr->[1] . '"' . "\x0A"; |
177 |
} |
178 |
|
179 |
unshift @node, |
180 |
map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes}; |
181 |
} elsif ($nt == $child->[0]->TEXT_NODE) { |
182 |
$r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A"; |
183 |
} elsif ($nt == $child->[0]->CDATA_SECTION_NODE) { |
184 |
$r .= '| ' . $child->[1] . '<![CDATA[' . $child->[0]->data . "]]>\x0A"; |
185 |
} elsif ($nt == $child->[0]->COMMENT_NODE) { |
186 |
$r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A"; |
187 |
} elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) { |
188 |
$r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A"; |
189 |
} elsif ($nt == $child->[0]->PROCESSING_INSTRUCTION_NODE) { |
190 |
$r .= '| ' . $child->[1] . '<?' . $child->[0]->target . ' ' . |
191 |
$child->[0]->data . "?>\x0A"; |
192 |
} else { |
193 |
$r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error |
194 |
} |
195 |
} |
196 |
|
197 |
return \$r; |
198 |
} # test_serialize |
199 |
|
200 |
sub get_node_path ($) { |
201 |
my $node = shift; |
202 |
my @r; |
203 |
while (defined $node) { |
204 |
my $rs; |
205 |
if ($node->node_type == 1) { |
206 |
$rs = $node->manakai_local_name; |
207 |
$node = $node->parent_node; |
208 |
} elsif ($node->node_type == 2) { |
209 |
$rs = '@' . $node->manakai_local_name; |
210 |
$node = $node->owner_element; |
211 |
} elsif ($node->node_type == 3) { |
212 |
$rs = '"' . $node->data . '"'; |
213 |
$node = $node->parent_node; |
214 |
} elsif ($node->node_type == 9) { |
215 |
$rs = ''; |
216 |
$node = $node->parent_node; |
217 |
} else { |
218 |
$rs = '#' . $node->node_type; |
219 |
$node = $node->parent_node; |
220 |
} |
221 |
unshift @r, $rs; |
222 |
} |
223 |
return join '/', @r; |
224 |
} # get_node_path |
225 |
|
226 |
=head1 AUTHOR |
227 |
|
228 |
Wakaba <w@suika.fam.cx>. |
229 |
|
230 |
=head1 LICENSE |
231 |
|
232 |
Copyright 2007 Wakaba <w@suika.fam.cx> |
233 |
|
234 |
This library is free software; you can redistribute it |
235 |
and/or modify it under the same terms as Perl itself. |
236 |
|
237 |
=cut |
238 |
|
239 |
## $Date: 2007/06/21 14:54:14 $ |