File Coverage

blib/lib/Data/Kanji/Tomoe.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Data::Kanji::Tomoe;
2              
3             our $VERSION = 0.04;
4              
5             # Read in the data file from Tomoe
6              
7 1     1   35330 use warnings;
  1         4  
  1         35  
8 1     1   6 use strict;
  1         3  
  1         38  
9 1     1   723 use XML::Parser;
  0            
  0            
10             use utf8;
11             use Carp;
12              
13             sub point()
14             {
15             my ($tomoe) = @_;
16             my $stroke_points = $$tomoe{stroke_points};
17             my $attrval = $$tomoe{attrval};
18             my $x = $$attrval{x};
19             my $y = $$attrval{y};
20             # print $x, $y;
21             push @$stroke_points, [$x, $y];
22             }
23              
24             sub stroke()
25             {
26             my ($tomoe) = @_;
27             $$tomoe{stroke_points} = [];
28             $$tomoe{stroke_count}++;
29             }
30              
31             sub strokes()
32             {
33             my ($tomoe) = @_;
34             $$tomoe{stroke_count} = 0;
35             }
36              
37             sub character()
38             {
39             my ($tomoe) = @_;
40             # Kill old character.
41             $tomoe->{character} = {};
42             }
43              
44             sub utf8()
45             {
46             my ($tomoe) = @_;
47             }
48              
49             sub dictionary()
50             {
51             my ($tomoe) = @_;
52              
53             }
54              
55             sub point_end()
56             {
57             my ($tomoe) = @_;
58             }
59              
60             sub stroke_end()
61             {
62             my ($tomoe) = @_;
63             my $stroke_points = $$tomoe{stroke_points};
64             my $character = $tomoe->{character};
65             push @{$character->{strokes}}, $stroke_points;
66             # print "Number of points is ", scalar @$stroke_points, "\n";
67             # my $stroke_data = StrokeRecognition::identify($stroke_points);
68             # print $stroke_data;
69             }
70              
71             sub strokes_end()
72             {
73             my ($tomoe) = @_;
74             # print " ", $$tomoe{stroke_count}, "\n";
75             }
76              
77             sub character_end()
78             {
79             my ($tomoe) = @_;
80             my $character_callback = $tomoe->{character_callback};
81             if ($character_callback) {
82             &{$character_callback} ($tomoe, $tomoe->{character});
83             }
84             }
85              
86             sub utf8_end()
87             {
88             my ($tomoe, $string) = @_;
89             $tomoe->{character}->{utf8} = $string;
90             # print $string, " ";
91             }
92              
93             sub dictionary_end()
94             {
95             my ($tomoe) = @_;
96              
97             }
98              
99             # Subroutines which handle the various tags in the Tomoe data.
100              
101             my %start_handlers = (
102             point => \& point,
103             stroke => \& stroke,
104             strokes => \& strokes,
105             character => \& character,
106             utf8 => \& utf8,
107             dictionary => \& dictionary,
108             );
109              
110             # Subroutines which handle the various tags in the Tomoe data.
111              
112             my %end_handlers = (
113             point => \& point_end,
114             stroke => \& stroke_end,
115             strokes => \& strokes_end,
116             character => \& character_end,
117             utf8 => \& utf8_end,
118             dictionary => \& dictionary_end,
119             );
120              
121             # Handle an XML starting tag
122              
123             sub handle_start
124             {
125             my ($tomoe, $parsexml, $element, %attrval) = @_;
126             $$tomoe{element} = $element;
127             $$tomoe{attrval} = \%attrval;
128             if ($start_handlers{$element}) {
129             &{$start_handlers{$element}}($tomoe);
130             } else {
131             print STDERR "No element handler for element `$element' at line $.\n";
132             }
133             }
134              
135             # Handle an XML ending tag
136              
137             sub handle_end
138             {
139             my ($tomoe, $parsexml, $element) = @_;
140             my $values = $$tomoe{values};
141             my $string = $$values{$element};
142             $string =~ s/^\s+|\s+$//g if $string;
143             if ($end_handlers{$element}) {
144             &{$end_handlers{$element}}($tomoe, $string)
145             }
146             else {
147             print STDERR "No end handler for element `$element' at line $.\n";
148             }
149             undef $$values{$element};
150             }
151              
152             # Handle XML character data
153              
154             sub handle_char
155             {
156             my ($tomoe, $parsexml, $string) = @_;
157             my $values = $$tomoe{values};
158             $$values{$$tomoe{element}} .= $string;
159             }
160              
161             sub parse
162             {
163             my ($parser) = @_;
164             $$parser{xmlparser}->parse($$parser{file});
165             }
166              
167             sub parsefile
168             {
169             my ($parser, $tomoe_data_file) = @_;
170             open_file ($parser, $tomoe_data_file);
171             $parser->parse ($parser->{file});
172             close $parser->{file} or croak $!;
173             }
174              
175             sub open_file
176             {
177             my ($parser, $tomoe_data_file) = @_;
178             open my $tomoe_data, "<:encoding(utf8)", $tomoe_data_file
179             or die "Can't open $tomoe_data_file: $!";
180             $$parser{file_name} = $tomoe_data_file;
181             $$parser{file} = $tomoe_data;
182             }
183              
184             sub new
185             {
186             my ($package, %inputs) = @_;
187             my $parser = \%inputs;
188             $parser->{xmlparser} = new XML::Parser(
189             Handlers => {
190             Start => sub { handle_start ($parser, @_); },
191             End => sub { handle_end ($parser, @_); },
192             Char => sub { handle_char ($parser, @_); },
193             },
194             );
195             $$parser{values} = {};
196             bless $parser;
197             if ($inputs{tomoe_data_file}) {
198             open_file ($parser, $inputs{tomoe_data_file});
199             }
200             return $parser;
201             }
202              
203             1;