blib/lib/PAR/WebStart/PNLP.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/t/config.t ↑ Summary lib/t/basic.t »
1 package PAR::WebStart::PNLP;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use XML::SAX::ExpatXS;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use XML::SAX;
6 use File::Basename;
7 use base qw(XML::SAX::Base);
8 our $VERSION = '0.20';
9
10 our %wantarray = map {$_ => 1} qw(par argument description module);
11
12 sub new {
13 my ($class, %args) = @_;
14 my $file = $args{file};
15 die "Please supply a valid pnlp file" unless ($file and -e $file);
16 bless {file => $file, cfg => undef, ERROR => ''}, $class;
17 }
18
19 sub parse {
20 my $self = shift;
21 my $file = $self->{file};
22 XML::SAX->add_parser(q(XML::SAX::ExpatXS));
23 my $factory = XML::SAX::ParserFactory->new();
24 my $handler = PNLPHandler->new();
25 my $parser = $factory->parser( Handler => $handler);
26
27 eval { $parser->parse_uri($file); };
28 if ($@) {
29 $self->{ERROR} = qq{Error in parsing $file};
30 return;
31 }
32 my $cfg = $handler->{cfg};
33 fix_args($cfg);
34 fix_par($cfg);
35 return $cfg;
36 }
37
38 sub fix_args {
39 my $cfg = shift;
40 my $args = [];
41 foreach my $arg(@{$cfg->{argument}}) {
42 my $value = $arg->{value};
43 foreach my $entry(split ' ', $value) {
44 push @$args, {value => $entry};
45 }
46 }
47 $cfg->{argument} = $args;
48 }
49
50 sub fix_par {
51 my $cfg = shift;
52 my $pars = $cfg->{par};
53 return if (scalar(@$pars) == 1);
54 my $main = $cfg->{'application-desc'}->{'main-par'};
55 return unless $main;
56 my $par_main = $main . '.par';
57 my $par_tmp = [];
58 my $i = 1;
59 foreach my $par (@$pars) {
60 my $par_file = basename($par->{href}, qr{\.par});
61 if ($par_file eq $par_main) {
62 $par_tmp->[0] = {%$par};
63 }
64 else {
65 $par_tmp->[$i] = {%$par};
66 $i++;
67 }
68 }
69 $cfg->{par} = $par_tmp;
70 }
71
72 # begin the in-line package
73 package PNLPHandler;
74 use strict;
75 use warnings;
76
77 my $curr_el = '';
78 my %array_count = map {$_ => 0} keys %wantarray;
79
80 sub new {
81 my $type = shift;
82 return bless {text => '', cfg => {}}, $type;
83 }
84
85 sub start_document {
86 my ($self) = @_;
87 # print "Starting document\n";
88 $self->{text} = '';
89 }
90
91 sub start_element {
92 my ($self, $element) = @_;
93 # print "Starting $element->{Name}\n";
94 my $cfg = $self->{cfg};
95 $curr_el = $element->{Name};
96 $cfg->{$curr_el}->{seen} = 1 unless $wantarray{$curr_el};
97 $self->display_text();
98 foreach my $ak (keys %{ $element->{Attributes} } ) {
99 my $at = $element->{Attributes}->{$ak};
100 my $name = $at->{Name};
101 my $value = $at->{Value};
102 if ($wantarray{$curr_el}) {
103 $cfg->{$curr_el}->[$array_count{$curr_el}]->{$name} = $value;
104 }
105 else {
106 $cfg->{$curr_el}->{$name} = $value;
107 }
108 # print qq(Attribute $at->{Name} = "$at->{Value}"\n);
109 }
110 }
111
112 sub characters {
113 my ($self, $characters) = @_;
114 my $text = $characters->{Data};
115 $text =~ s/^\s*//;
116 $text =~ s/\s*$//;
117 $self->{text} .= $text;
118 }
119
120 sub end_element {
121 my ($self, $element) = @_;
122 $self->display_text();
123 # print "Ending $element->{Name}\n";
124 $array_count{$curr_el}++;
125 }
126
127 sub display_text {
128 my $self = shift;
129 my $cfg = $self->{cfg};
130 if ( defined( $self->{text} ) && $self->{text} ne "" ) {
131 if ($wantarray{$curr_el}) {
132 $cfg->{$curr_el}->[$array_count{$curr_el}]->{value} = $self->{text};
133 }
134 else {
135 $cfg->{$curr_el}->{value} = $self->{text};
136 }
137 # print " text: [$self->{text}]\n";
138 $self->{text} = '';
139 }
140 }
141
142 sub end_document {
143 my ($self) = @_;
144 # print "Document finished\n";
145 }
146
147 1; #Ye Olde 'Return True' for the in-line package..
148
149
150 __END__
« lib/t/config.t ↑ Summary lib/t/basic.t »