YAX/Parser.pm

filter: Statementstmt 69.2% 126 / 182 Branchbran 42.8% 24 / 56 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 72.7% 16 / 22 Podpod 50.0% 5 / 10 totaltotal 57.0% 171 / 300 CC 37 SCAR 51.4
CC
37
Cov
59%
stream
462.0
parse
9.0
read_file
6.0
CRAP 169.9
↑ Summary YAX/Query.pm »
1 package YAX::Parser;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use YAX::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use YAX::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use YAX::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use YAX::Fragment;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use YAX::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use YAX::Constants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #========================================================================
13 # These regular expressions have been gratefully borrowed from:
14 #
15 # REX/Perl 1.0
16 # Robert D. Cameron "REX: XML Shallow Parsing with Regular Expressions",
17 # Technical Report TR 1998-17, School of Computing Science, Simon Fraser
18 # University, November, 1998.
19 # Copyright (c) 1998, Robert D. Cameron.
20 # The following code may be freely used and distributed provided that
21 # this copyright and citation notice remains intact and that modifications
22 # or additions are clearly identified.
23  
24 3 our $TextSE = "[^<]+";
25 3 our $UntilHyphen = "[^-]*-";
26 3 our $Until2Hyphens = "$UntilHyphen(?:[^-]$UntilHyphen)*-";
27 3 our $CommentCE = "$Until2Hyphens>?";
28 3 our $UntilRSBs = "[^\\]]*](?:[^\\]]+])*]+";
29 3 our $CDATA_CE = "$UntilRSBs(?:[^\\]>]$UntilRSBs)*>";
30 3 our $S = "[ \\n\\t\\r]+";
31 3 our $NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]";
32 3 our $NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]";
33 3 our $Name = "(?:$NameStrt)(?:$NameChar)*";
34 3 our $QuoteSE = "\"[^\"]*\"|'[^']*'";
35 3 our $DT_IdentSE = "$S$Name(?:$S(?:$Name|$QuoteSE))*";
36 3 our $MarkupDeclCE = "(?:[^\\]\"'><]+|$QuoteSE)*>";
37 3 our $S1 = "[\\n\\r\\t ]";
38 3 our $UntilQMs = "[^?]*\\?+";
39 3 our $PI_Tail = "\\?>|$S1$UntilQMs(?:[^>?]$UntilQMs)*>";
40 3 our $DT_ItemSE = "<(?:!(?:--$Until2Hyphens>|[^-]$MarkupDeclCE)|\\?$Name(?:$PI_Tail))|%$Name;|$S";
41 3 our $DocTypeCE = "$DT_IdentSE(?:$S)?(?:\\[(?:$DT_ItemSE)*](?:$S)?)?>?";
42 3 our $DeclCE = "--(?:$CommentCE)?|\\[CDATA\\[(?:$CDATA_CE)?|DOCTYPE(?:$DocTypeCE)?";
43 3 our $PI_CE = "$Name(?:$PI_Tail)?";
44 3 our $EndTagCE = "$Name(?:$S)?>?";
45 3 our $AttValSE = "\"[^<\"]*\"|'[^<']*'";
46 3 our $ElemTagCE = "$Name(?:$S$Name(?:$S)?=(?:$S)?(?:$AttValSE))*(?:$S)?/?>?";
47 3 our $ElementCE = "/(?:$EndTagCE)?|(?:$ElemTagCE)?";
48 3 our $MarkupSPE = "<(?:!(?:$DeclCE)?|\\?(?:$PI_CE)?|(?:$ElementCE)?)";
49 3 our $XML_SPE = "$TextSE|$MarkupSPE";
50  
51 #========================================================================
52  
53 # these have captures for parsing attributes
54 3 our $AttValSE2 = "\"([^<\"]*)\"|'([^<']*)'";
55 3 our $ElemTagCE2 = "(?:($Name)(?:$S)?=(?:$S)?(?:$AttValSE2))+(?:$S)?/?>?";
56  
57 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
58 1 my ( $class ) = @_;
59 1 my $self = bless { }, $class;
60 1 return $self;
61 }
62  
63 2 ▶sub parse {
Subroutine parse: called CC=9 SCAR=22.0
Pod documented
64 2 my ( $self, $xstr ) = ( shift, shift );
65 2 ▶ return unless $xstr;
Branch: 1 branch
BranchTrueFalseTotal
unless $xstr 0 2 2
66 2 my @nodes = $self->tokenize( $xstr );
67  
68 2 my $xdoc = YAX::Document->new();
69 2 my @stack = ( $xdoc );
70 2 my ( $spec, $elmt );
71 2 foreach my $node ( $self->tokenize( $xstr ) ) {
72 102 $spec = substr( $node, 0, 2 );
73 102 ▶ if ( index( $spec, '<' ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($spec, "<") != 0) 48 54 102
74 48 $self->_mk_text( $node, $stack[-1] );
75 48 next;
76 }
77 54 ▶ if ( $spec eq '</' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq "</") 22 32 54
78 22 pop @stack;
79 22 next;
80 }
81 32 ▶ if ( $spec eq '<!' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq "<!") 3 29 32
82 3 $self->_mk_decl( $node, $stack[-1] );
83 3 next;
84 }
85 29 ▶ if ( $spec eq '<?' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq "<?") 2 27 29
86 2 $self->_mk_proc( $node, $stack[-1] );
87 2 next;
88 }
89  
90 27 $elmt = $self->_mk_elmt( $node, $stack[-1] );
91 27 ▶ push( @stack, $elmt ) unless ( $node =~ m{/>$} );
Branch: 1 branch
BranchTrueFalseTotal
unless $node =~ m[/>$] 22 5 27
92 $xdoc->set( $elmt->{id} => $elmt ) if $elmt->{id}
93 27 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
if $elmt->{"id"} 7 20 27
94  
95 2 return $xdoc;
96 }
97  
98 0 ▶sub stream {
Subroutine stream: not called CC=21 SCAR=61.4
Pod documented
99 0 my ( $self, $xstr, $state ) = ( shift, shift, shift );
100 0 my %subs;
101 0 ▶ if ( @_ == 1 and ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
102 0 %subs = %{$_[0]};
103 } else {
104 0 %subs = @_;
105 }
106  
107 0 ▶ my $text = delete $subs{text} || $subs{pass};
Condition0%0/3
delete $subs{'text'} || $subs{'pass'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $subs{'text'} || $subs{'pass'}
delete $subs{'text'} $subs{'pass'}
Truth table0%0/3
A || B
A= delete $subs{'text'}B= $subs{'pass'}
ABresult
1X1
011
000
108 0 ▶ my $decl = delete $subs{decl} || $subs{pass};
Condition0%0/3
delete $subs{'decl'} || $subs{'pass'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $subs{'decl'} || $subs{'pass'}
delete $subs{'decl'} $subs{'pass'}
Truth table0%0/3
A || B
A= delete $subs{'decl'}B= $subs{'pass'}
ABresult
1X1
011
000
109 0 ▶ my $proc = delete $subs{proc} || $subs{pass};
Condition0%0/3
delete $subs{'proc'} || $subs{'pass'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $subs{'proc'} || $subs{'pass'}
delete $subs{'proc'} $subs{'pass'}
Truth table0%0/3
A || B
A= delete $subs{'proc'}B= $subs{'pass'}
ABresult
1X1
011
000
110 0 ▶ my $elmt = delete $subs{elmt} || $subs{pass};
Condition0%0/3
delete $subs{'elmt'} || $subs{'pass'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $subs{'elmt'} || $subs{'pass'}
delete $subs{'elmt'} $subs{'pass'}
Truth table0%0/3
A || B
A= delete $subs{'elmt'}B= $subs{'pass'}
ABresult
1X1
011
000
111 0 ▶ my $elcl = delete $subs{elcl} || $subs{pass};
Condition0%0/3
delete $subs{'elcl'} || $subs{'pass'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $subs{'elcl'} || $subs{'pass'}
delete $subs{'elcl'} $subs{'pass'}
Truth table0%0/3
A || B
A= delete $subs{'elcl'}B= $subs{'pass'}
ABresult
1X1
011
000
112  
113 0 my ( $spec, $name, $copy, $atts, %atts );
114 0 foreach my $node ( $self->tokenize( $xstr ) ) {
115 0 $spec = substr( $node, 0, 2 );
116 0 ▶ if ( index( $spec, '<' ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($spec, "<") != 0) 0 0 0
117 0 ▶ $text && $text->( $state, $node );
Branch: 1 branch
BranchTrueFalseTotal
$text and &$text($state, $node) 0 0 0
118 0 next;
119 }
120 0 ▶ if ( $spec eq '</' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq "</") 0 0 0
121 0 ▶ $elcl && $elcl->( $state, substr( $node, 2, -1 ) );
Branch: 1 branch
BranchTrueFalseTotal
$elcl and &$elcl($state, substr($node, 2, -1)) 0 0 0
122 0 next;
123 }
124 0 ▶ if ( $spec eq '<!' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq "<!") 0 0 0
125 0 ▶ $decl && $decl->( $state, $node );
Branch: 1 branch
BranchTrueFalseTotal
$decl and &$decl($state, $node) 0 0 0
126 0 next;
127 }
128 0 ▶ if ( $spec eq '<?' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq "<?") 0 0 0
129 0 ▶ $proc && $proc->( $state, $node );
Branch: 1 branch
BranchTrueFalseTotal
$proc and &$proc($state, $node) 0 0 0
130 0 next;
131 }
132  
133 0 ▶ $elmt && do {
Branch: 1 branch
BranchTrueFalseTotal
$elmt and do { $copy = substr($node, 1, -1); ($name, $atts) = split(/\s+/, $copy, 2); $name =~ s[/$][]; %atts = $atts ? $self->parse_attributes($atts) : (); &$elmt($state, $name, %atts) } 0 0 0
134 0 $copy = substr( $node, 1, -1 );
135 0 ( $name, $atts ) = split( /\s+/, $copy, 2 );
136 0 $name =~ s{/$}{};
137 0 ▶ %atts = $atts ? $self->parse_attributes( $atts ) : ( );
Branch: 1 branch
BranchTrueFalseTotal
$atts ? : 0 0 0
138 0 $elmt->( $state, $name, %atts );
139 };
140  
141 0 ▶ if ( substr( $node, -2 ) eq '/>' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($node, -2) eq "/>") 0 0 0
142 0 ▶ $elcl && $elcl->( $state, $name );
Branch: 1 branch
BranchTrueFalseTotal
$elcl and &$elcl($state, $name) 0 0 0
143 }
144 }
145 }
146  
147 0 ▶sub read_file {
Subroutine read_file: not called CC=2 SCAR=17.9
Pod undocumented
148 0 my ( $self, $file ) = @_;
149 0 my $xstr;
150 {
151 0 ▶ open FH, $file or return;
Branch: 1 branch
BranchTrueFalseTotal
open FH, $file or return 0 0 0
152 0 local $/ = undef;
153 0 $xstr = <FH>;
154 0 close FH;
155 }
156 0 return $xstr;
157 }
158  
159 0 ▶sub parse_file {
Subroutine parse_file: not called CC=1 SCAR=6.9
Pod documented
160 0 my ( $self, $file ) = @_;
161 0 return $self->parse( $self->read_file( $file ) );
162 }
163  
164 0 ▶sub stream_file {
Subroutine stream_file: not called CC=1 SCAR=6.9
Pod documented
165 0 my ( $self, $file, $state, %subs ) = @_;
166 0 return $self->stream( $self->read_file( $file ), $state, %subs );
167 }
168  
169 0 ▶sub parse_as_fragment {
Subroutine parse_as_fragment: not called CC=2 SCAR=17.9
Pod undocumented
170 0 my ( $self, $xstr ) = @_;
171 0 my $xdoc = $self->parse( '<yax:frag>'.$xstr.'</yax:frag>' );
172 0 my $root = $xdoc->root;
173 0 my $frag = YAX::Fragment->new;
174 0 ▶ $frag->append( $root->[0] ) while @$root;
Branch: 1 branch
BranchTrueFalseTotal
@$root and do { $frag->append($root->[0]) } 0 0 0
175 0 return $frag;
176 }
177  
178 0 ▶sub parse_file_as_fragment {
Subroutine parse_file_as_fragment: not called CC=1 SCAR=6.9
Pod undocumented
179 0 my ( $self, $file ) = @_;
180 0 my $xstr = $self->read_file( $file );
181 0 my $frag = $self->parse_as_fragment( $xstr );
182 0 return $frag;
183 }
184  
185 4 ▶sub tokenize {
Subroutine tokenize: called CC=1 SCAR=0.0
Pod documented
186 4 my ( $self, $xstr ) = @_;
187 4 return $xstr =~ /$XML_SPE/g;
188 }
189  
190 3 ▶sub _mk_decl {
Subroutine _mk_decl: called CC=4 SCAR=13.9
191 3 my ( $self, $decl, $parent ) = @_;
192 3 my ( $type, $name );
193 3 my $offset = 1;
194 3 my $length = length( $decl );
195  
196 3 ▶ substr( $decl, 0, 4 ) eq '<!--' && do {
Branch: 1 branch
BranchTrueFalseTotal
substr($decl, 0, 4) eq "<!--" and do { $offset = 4; $length = $length - $offset - 3; $type = 8; $name = "#comment" } 1 2 3
197 1 $offset = 4;
198 1 $length = $length - $offset - 3;
199 1 $type = COMMENT_NODE;
200 1 $name = '#comment';
201 };
202 3 ▶ substr( $decl, 0, 9 ) eq '<![CDATA[' && do {
Branch: 1 branch
BranchTrueFalseTotal
substr($decl, 0, 9) eq "<![CDATA[" and do { $offset = 9; $length = $length - $offset - 3; $type = 4; $name = "#cdata" } 1 2 3
203 1 $offset = 9;
204 1 $length = $length - $offset - 3;
205 1 $type = CDATA_SECTION_NODE;
206 1 $name = '#cdata';
207 };
208 3 ▶ substr( $decl, 0, 9 ) eq '<!DOCTYPE' && do {
Branch: 1 branch
BranchTrueFalseTotal
substr($decl, 0, 9) eq "<!DOCTYPE" and do { $offset = 10; $length = $length - $offset - 3; $type = 10; $name = "#document-type" } 1 2 3
209 1 $offset = 10;
210 1 $length = $length - $offset - 3;
211 1 $type = DOCUMENT_TYPE_NODE;
212 1 $name = "#document-type";
213 };
214 3 return $self->_mk_node(
215 $name, $type, substr( $decl, $offset, $length ), $parent
216 );
217 }
218  
219 2 ▶sub _mk_proc {
Subroutine _mk_proc: called CC=1 SCAR=0.0
220 2 my ( $self, $text, $parent ) = @_;
221 2 my ( $name, $data ) = ( $text =~ /^<\?([a-zA-Z0-9_-]+?)\s+(.*?)\s*\?>/ );
222 2 return $self->_mk_node(
223 $name, PROCESSING_INSTRUCTION_NODE, $data, $parent
224 );
225 }
226  
227 5 ▶sub _mk_node {
Subroutine _mk_node: called CC=1 SCAR=0.0
228 5 my ( $self, $name, $type, $data, $parent ) = @_;
229  
230 5 my $node = YAX::Node->new( $name, $type, $data );
231 5 push @$parent, $node;
232 5 $node->parent( $parent );
233  
234 5 return $node;
235 }
236  
237 48 ▶sub _mk_text {
Subroutine _mk_text: called CC=1 SCAR=0.0
238 48 my ( $self, $text, $parent ) = @_;
239  
240 48 my $node = YAX::Text->new( $text );
241 48 push @$parent, $node;
242 48 $node->parent( $parent );
243  
244 48 return $node;
245 }
246  
247 27 ▶sub _mk_elmt {
Subroutine _mk_elmt: called CC=2 SCAR=6.9
248 27 my ( $self, $elmt, $parent ) = @_;
249 27 my $copy = substr( $elmt, 1, -1 );
250 27 my ( $name, $atts ) = split(/\s+/, $copy, 2);
251  
252 27 $name =~ s/\/$//;
253  
254 27 ▶ my %atts = $atts ? $self->parse_attributes( $atts ) : ( );
Branch: 1 branch
BranchTrueFalseTotal
$atts ? : 20 7 27
255 27 my $node = YAX::Element->new( $name, %atts );
256  
257 27 push @$parent, $node;
258 27 $node->parent( $parent );
259  
260 27 return $node;
261 }
262  
263 20 ▶sub parse_attributes {
Subroutine parse_attributes: called CC=3 SCAR=11.0
Pod undocumented
264 20 my ( $self, $atts ) = @_;
265 20 my %atts = ( );
266 20 ▶ while ( $atts =~ /$ElemTagCE2/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atts =~ /$YAX::Parser::ElemTagCE2/g) 27 20 47
267 27 ▶ $atts{ $1 } = defined $2 ? $2 : $3;
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 27 0 27
268 }
269 20 return %atts;
270 }
271  
272 3 1;
273  
274 __END__
↑ Summary YAX/Query.pm »