lib/MKDoc/XML/Token.pm

filter: Statementstmt 100% 113 / 113 Branchbran 100% 36 / 36 Conditioncond 93.3% 28 / 30 MC/DCmcdc 85.7% 12 / 14 Subroutinesub 100% 15 / 15 Podpod 100% 12 / 12 totaltotal 98.1% 216 / 220 CC 30 SCAR 13.8
CC
30
Cov
99%
leaf
6.0
tag_open
6.0
tag_self_close
5.0
CRAP 4.0
« lib/MKDoc/XML/Tagger/Preserve.pm ↑ Summary lib/MKDoc/XML/Tagger.pm »
1 =head1 NAME
2  
3 MKDoc::XML::Token - XML Token Object
4  
5 =cut
6 package MKDoc::XML::Token;
7 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 =head1 SYNOPSIS
12  
13 my $tokens = MKDoc::XML::Tokenizer->process_data ($some_xml);
14 foreach my $token (@{$tokens})
15 {
16 print "'" . $token->as_string() . "' is text\n" if (defined $token->text());
17 print "'" . $token->as_string() . "' is a self closing tag\n" if (defined $token->tag_self_close());
18 print "'" . $token->as_string() . "' is an opening tag\n" if (defined $token->tag_open());
19 print "'" . $token->as_string() . "' is a closing tag\n" if (defined $token->tag_close());
20 print "'" . $token->as_string() . "' is a processing instruction\n" if (defined $token->pi());
21 print "'" . $token->as_string() . "' is a declaration\n" if (defined $token->declaration());
22 print "'" . $token->as_string() . "' is a comment\n" if (defined $token->comment());
23 print "'" . $token->as_string() . "' is a tag\n" if (defined $token->tag());
24 print "'" . $token->as_string() . "' is a pseudo-tag (NOT text and NOT tag)\n" if (defined $token->pseudotag());
25 print "'" . $token->as_string() . "' is a leaf token (NOT opening tag)\n" if (defined $token->leaf());
26 }
27  
28  
29 =head1 SUMMARY
30  
31 L<MKDoc::XML::Token> is an object representing an XML token produced by L<MKDoc::XML::Tokenizer>.
32  
33 It has a set of methods to identify the type of token it is, as well as to help building a parsed
34 tree as in L<MKDoc::XML::TreeBuilder>.
35  
36 =head1 API
37  
38 =head2 my $token = new MKDoc::XML::Token ($string_token);
39  
40 Constructs a new MKDoc::XML::Token object.
41  
42 =cut
43 sub new
44 29 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
45 29 my $class = shift;
46 29 my $token = shift;
47 29 return bless \$token, $class;
48 }
49  
50  
51 =head2 my $string_token = $token->as_string();
52  
53 Returns the string representation of this token so that:
54  
55 MKDoc::XML::Token->new ($token)->as_string eq $token
56  
57 is a tautology.
58  
59 =cut
60 sub as_string
61 19 ▶{
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
62 19 my $self = shift;
63 19 return $$self;
64 }
65  
66  
67 =head2 my $node = $token->leaf();
68  
69 If this token is not an opening tag, this method will return its corresponding
70 node structure as returned by $token->text(), $token->tag_self_close(),
71 etc.
72  
73 Returns undef otherwise.
74  
75 =cut
76 sub leaf
77 8061 ▶{
Subroutine leaf: called CC=6 SCAR=17.9
Pod documented
78 8061 my $self = shift;
79 8061 my $res = undef;
80
81 8061 $res = $self->comment();
82 8061 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 4 8057 8061
83
84 8057 $res = $self->declaration();
85 8057 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 2 8055 8057
86
87 8055 $res = $self->pi();
88 8055 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 4 8051 8055
89
90 8051 $res = $self->tag_self_close();
91 8051 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 7 8044 8051
92
93 8044 $res = $self->text();
94 8044 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 4106 3938 8044
95  
96 3938 return;
97 }
98  
99  
100 =head2 my $node = $token->pseudotag();
101  
102 If this token is a comment, declaration or processing instruction,
103 this method will return $token->tag_comment(), $token_declaration()
104 or $token->pi() resp.
105  
106 Returns undef otherwise.
107  
108 =cut
109 sub pseudotag
110 8 ▶{
Subroutine pseudotag: called CC=4 SCAR=13.9
Pod documented
111 8 my $self = shift;
112 8 my $res = undef;
113
114 8 $res = $self->comment();
115 8 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 1 7 8
116
117 7 $res = $self->declaration();
118 7 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 1 6 7
119
120 6 $res = $self->pi();
121 6 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 1 5 6
122
123 5 return;
124 }
125  
126  
127 =head2 my $node = $token->tag();
128  
129 If this token is an opening, closing, or self closing tag, this
130 method will return $token->tag_open(), $token->tag_close()
131 or $token->tag_self_close() resp.
132  
133 Returns undef otherwise.
134  
135 =cut
136 sub tag
137 27 ▶{
Subroutine tag: called CC=4 SCAR=13.9
Pod documented
138 27 my $self = shift;
139 27 my $res = undef;
140  
141 27 $res = $self->tag_open();
142 27 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 10 17 27
143
144 17 $res = $self->tag_close();
145 17 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 4 13 17
146
147 13 $res = $self->tag_self_close();
148 13 ▶ defined $res and return $res;
Branch: 1 branch
BranchTrueFalseTotal
defined $res and return $res 4 9 13
149
150 9 return $res;
151 }
152  
153  
154 =head2 my $node = $token->comment();
155  
156 If this token object represents a declaration, the following structure
157 is returned:
158  
159 # this is <!-- I like Pie. Pie is good -->
160 {
161 _tag => '~comment',
162 text => ' I like Pie. Pie is good ',
163 }
164
165 Returns undef otherwise.
166  
167 =cut
168 sub comment
169 8077 ▶{
Subroutine comment: called CC=2 SCAR=6.9
Pod documented
170 8077 my $self = shift;
171 8077 my $node = undef;
172 8077 ▶ $$self =~ /^<\!--/ and do {
Branch: 1 branch
BranchTrueFalseTotal
$$self =~ /^<\!--/ and do { $node = {"_tag", "~comment", "text", $$self}; $node->{"text"} =~ s/^<\!--//; $node->{"text"} =~ s/-->$// } 6 8071 8077
173 6 $node = {
174 _tag => '~comment',
175 text => $$self,
176 };
177 6 $node->{text} =~ s/^<\!--//;
178 6 $node->{text} =~ s/-->$//;
179 };
180 8077 $node;
181 }
182  
183  
184 =head2 my $node = $token->declaration();
185  
186 If this token object represents a declaration, the following structure
187 is returned:
188  
189 # this is <!DOCTYPE foo>
190 {
191 _tag => '~declaration',
192 text => 'DOCTYPE foo',
193 }
194  
195 Returns undef otherwise.
196  
197 =cut
198 sub declaration
199 8072 ▶{
Subroutine declaration: called CC=3 SCAR=11.0
Pod documented
200 8072 my $self = shift;
201 8072 my $node = undef;
202 8072 ▶ $$self !~ /^<\!--/ and $$self =~ /^<!/ and do {
Branch: 1 branch
BranchTrueFalseTotal
not $$self =~ /^<\!--/ and $$self =~ /^<!/ and do { $node = {"_tag", "~declaration", "text", $$self}; $node->{"text"} =~ s/^<!//; $node->{"text"} =~ s/>$// } 4 8068 8072
Condition100%3/3
not $$self =~ /^<\!--/ and $$self =~ /^<!/
!ll&&!rl&&r
180674
MC/DC100%2/2
not $$self =~ /^<\!--/ and $$self =~ /^<!/
not $$self =~ /^<\!--/ $$self =~ /^<!/
Truth table100%3/3
not(A) and B
A= not $$self =~ /^<\!--/B= $$self =~ /^<!/
ABresult
0X0
100
111
203 4 $node = {
204 _tag => '~declaration',
205 text => $$self,
206 };
207 4 $node->{text} =~ s/^<!//;
208 4 $node->{text} =~ s/>$//;
209 };
210 8072 $node;
211 }
212  
213  
214 =head2 my $node = $token->pi();
215  
216 If this token object represents a processing instruction, the following structure
217 is returned:
218  
219 # this is <?xml version="1.0" charset="UTF-8"?>
220 {
221 _tag => '~pi',
222 text => 'xml version="1.0" charset="UTF-8"',
223 }
224  
225 Returns undef otherwise.
226  
227 =cut
228 sub pi
229 8069 ▶{
Subroutine pi: called CC=2 SCAR=6.9
Pod documented
230 8069 my $self = shift;
231 8069 my $node = undef;
232 8069 ▶ $$self =~ /^<\?/ and do {
Branch: 1 branch
BranchTrueFalseTotal
$$self =~ /^<\?/ and do { $node = {"_tag", "~pi", "text", $$self}; $node->{"text"} =~ s/^<\?//; $node->{"text"} =~ s/\>$//; $node->{"text"} =~ s/\?$// } 6 8063 8069
233 6 $node = {
234 _tag => '~pi',
235 text => $$self,
236 };
237 6 $node->{text} =~ s/^<\?//;
238 6 $node->{text} =~ s/\>$//;
239 6 $node->{text} =~ s/\?$//;
240 };
241 8069 $node;
242 }
243  
244  
245 =head2 my $node = $token->tag_open();
246  
247 If this token object represents an opening tag, the following structure
248 is returned:
249  
250 # this is <aTag foo="bar" baz="buz">
251 {
252 _tag => 'aTag',
253 _open => 1,
254 _close => 0,
255 foo => 'bar',
256 baz => 'buz',
257 }
258  
259 Returns undef otherwise.
260  
261 =cut
262 sub tag_open
263 3976 ▶{
Subroutine tag_open: called CC=6 SCAR=17.9
Pod documented
264 3976 my $self = shift;
265 3976 my $node = undef;
266 $$self !~ /^<\!/ and
267 $$self !~ /^<\// and
268 $$self !~ /\/>$/ and
269 $$self !~ /^<\?/ and
270 3976 ▶ $$self =~ /^</ and do {
Branch: 1 branch
BranchTrueFalseTotal
not $$self =~ /^<\!/ || $$self =~ m[^</] || $$self =~ m[/>$] || $$self =~ /^<\?/ and $$self =~ /^</ and do { my(%node) = _extract_attributes($$self); ($node{"_tag"}) = $$self =~ /.*?([A-Za-z0-9][A-Za-z0-9_:-]*)/; $node{"_open"} = 1; $node{"_close"} = 0; $node = \%node } 1985 1991 3976
Condition100%12/12
not $$self =~ /^<\!/ || $$self =~ m[^</] || $$self =~ m[/>$] || $$self =~ /^<\?/ and $$self =~ /^</
!ll&&!rl&&r
198471985
$$self =~ /^<\!/ || $$self =~ m[^</] || $$self =~ m[/>$] || $$self =~ /^<\?/
l!l&&r!l&&!r
198221992
$$self =~ /^<\!/ or $$self =~ m[^</] or $$self =~ m[/>$]
l!l&&r!l&&!r
197751994
$$self =~ /^<\!/ or $$self =~ m[^</]
l!l&&r!l&&!r
419731999
MC/DC100%5/5
not $$self =~ /^<\!/ || $$self =~ m[^</] || $$self =~ m[/>$] || $$self =~ /^<\?/ and $$self =~ /^</
$$self =~ /^<\!/ $$self =~ m[^</] $$self =~ m[/>$] $$self =~ /^<\?/ $$self =~ /^</
Truth table100%6/6
not(A or B or C || D) and E
A= $$self =~ /^<\!/B= $$self =~ m[^</]C= $$self =~ m[/>$]D= $$self =~ /^<\?/E= $$self =~ /^</
ABCDEresult
1XXXX0
01XXX0
001XX0
0001X0
000000
000011
271 1985 my %node = _extract_attributes ($$self);
272 1985 ($node{_tag}) = $$self =~ /.*?([A-Za-z0-9][A-Za-z0-9_:-]*)/;
273 1985 $node{_open} = 1;
274 1985 $node{_close} = 0;
275 1985 $node = \%node;
276 };
277
278 3976 $node;
279 }
280  
281  
282 =head2 my $node = $token->tag_close();
283  
284 If this token object represents a closing tag, the following structure
285 is returned:
286  
287 # this is </aTag>
288 {
289 _tag => 'aTag',
290 _open => 0,
291 _close => 1,
292 }
293  
294 Returns undef otherwise.
295  
296 =cut
297 sub tag_close
298 1994 ▶{
Subroutine tag_close: called CC=4 SCAR=13.9
Pod documented
299 1994 my $self = shift;
300 1994 my $node = undef;
301 $$self !~ /^<\!/ and
302 $$self =~ /^<\// and
303 1994 ▶ $$self !~ /\/>$/ and do {
Branch: 1 branch
BranchTrueFalseTotal
not $$self =~ /^<\!/ and $$self =~ m[^</] and not $$self =~ m[/>$] and do { my(%node) = (); ($node{"_tag"}) = $$self =~ /.*?([A-Za-z0-9][A-Za-z0-9_:-]*)/; $node{"_open"} = 0; $node{"_close"} = 1; $node = \%node } 1974 20 1994
Condition83%5/6
not $$self =~ /^<\!/ and $$self =~ m[^</] and not $$self =~ m[/>$]
!ll&&!rl&&r
2001974
not $$self =~ /^<\!/ and $$self =~ m[^</]
!ll&&!rl&&r
4161974
MC/DC66%2/3
not $$self =~ /^<\!/ and $$self =~ m[^</] and not $$self =~ m[/>$]
not $$self =~ /^<\!/ $$self =~ m[^</] not $$self =~ m[/>$]
Truth table75%3/4
not(A) and B and not(C)
A= not $$self =~ /^<\!/B= $$self =~ m[^</]C= not $$self =~ m[/>$]
ABCresult
0XX0
10X0
1100
1111
304 1974 my %node = ();
305 1974 ($node{_tag}) = $$self =~ /.*?([A-Za-z0-9][A-Za-z0-9_:-]*)/;
306 1974 $node{_open} = 0;
307 1974 $node{_close} = 1;
308 1974 $node = \%node;
309 };
310
311 1994 $node;
312 }
313  
314  
315 =head2 my $node = $token->tag_self_close();
316  
317 If this token object represents a self-closing tag, the following structure
318 is returned:
319  
320 # this is <aTag foo="bar" baz="buz" />
321 {
322 _tag => 'aTag',
323 _open => 1,
324 _close => 1,
325 foo => 'bar',
326 baz => 'buz',
327 }
328  
329 Returns undef otherwise.
330  
331 =cut
332 sub tag_self_close
333 8074 ▶{
Subroutine tag_self_close: called CC=5 SCAR=16.1
Pod documented
334 8074 my $self = shift;
335 8074 my $node = undef;
336 $$self !~ /^<\!/ and
337 $$self !~ /^<\// and
338 $$self =~ /\/>$/ and
339 # ((?:\w|:|-)+)\s*=\s*\"(.*?)\"/gs;
340 8074 ▶ $$self =~ /^</ and do {
Branch: 1 branch
BranchTrueFalseTotal
not $$self =~ /^<\!/ || $$self =~ m[^</] and $$self =~ m[/>$] and $$self =~ /^</ and do { my(%node) = _extract_attributes($$self); ($node{"_tag"}) = $$self =~ /.*?([A-Za-z0-9][A-Za-z0-9_:-]*)/; $node{"_open"} = 1; $node{"_close"} = 1; $node = \%node } 14 8060 8074
Condition88%8/9
not $$self =~ /^<\!/ || $$self =~ m[^</] and $$self =~ m[/>$] and $$self =~ /^</
!ll&&!rl&&r
8060014
not $$self =~ /^<\!/ || $$self =~ m[^</] and $$self =~ m[/>$]
!ll&&!rl&&r
1974608614
$$self =~ /^<\!/ || $$self =~ m[^</]
l!l&&r!l&&!r
419706100
MC/DC75%3/4
not $$self =~ /^<\!/ || $$self =~ m[^</] and $$self =~ m[/>$] and $$self =~ /^</
$$self =~ /^<\!/ $$self =~ m[^</] $$self =~ m[/>$] $$self =~ /^</
Truth table80%4/5
not(A || B) and C and D
A= $$self =~ /^<\!/B= $$self =~ m[^</]C= $$self =~ m[/>$]D= $$self =~ /^</
ABCDresult
1XXX0
01XX0
000X0
00100
00111
341 14 my %node = _extract_attributes ($$self);
342 14 ($node{_tag}) = $$self =~ /.*?([A-Za-z0-9][A-Za-z0-9_:-]*)/;
343 14 $node{_open} = 1;
344 14 $node{_close} = 1;
345 14 $node = \%node;
346 };
347
348 8074 $node;
349 }
350  
351  
352 =head2 my $node = $token->text();
353  
354 If this token object represents a piece of text, then this text is returned.
355 Returns undef otherwise. TRAP! $token->text() returns a false value if this
356 text happens to be '0' or ''. So really you should use:
357  
358 if (defined $token->text()) {
359 ... do stuff...
360 }
361  
362 =cut
363 sub text
364 8052 ▶{
Subroutine text: called CC=2 SCAR=6.9
Pod documented
365 8052 my $self = shift;
366 8052 ▶ return ($$self !~ /^</) ? $$self : undef;
Branch: 1 branch
BranchTrueFalseTotal
!($$self =~ /^</) ? : 4107 3945 8052
367 }
368  
369 23 our $S = "[ \\n\\t\\r]+";
370 23 our $NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]";
371 23 our $NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]";
372 23 our $Name = "(?:$NameStrt)(?:$NameChar)*";
373 23 our $EndTagCE = "$Name(?:$S)?>?";
374 23 our $AttValSE = "\"[^<\"]*\"|'[^<']*'";
375 23 our $ElemTagCE = "$Name((?:$S$Name(?:$S)?=(?:$S)?(?:$AttValSE))*)(?:$S)?/?>?";
376 23 our $ElemTagCE_Mod = "$S($Name)(?:$S)?=(?:$S)?($AttValSE)";
377  
378 23 our $RE_1 = qr /$ElemTagCE/;
379 23 our $RE_2 = qr /$ElemTagCE_Mod/;
380  
381  
382 sub _extract_attributes
383 1999 ▶{
Subroutine _extract_attributes: called CC=2 SCAR=6.9
384 1999 my $tag = shift;
385 1999 my ($tags) = $tag =~ /$RE_1/g;
386 1999 my %attr = $tag =~ /$RE_2/g;
387 1999 foreach my $key (keys %attr)
388 {
389 1818 my $val = $attr{$key};
390 1818 $val =~ s/^(\"|\')//;
391 1818 $val =~ s/(\"|\')$//;
392 1818 $attr{$key} = $val;
393 }
394
395 1999 %attr;
396 }
397  
398  
399 23 1;
400  
401  
402 __END__
« lib/MKDoc/XML/Tagger/Preserve.pm ↑ Summary lib/MKDoc/XML/Tagger.pm »