FastForward.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« FastForward/Spreadsheet.pm ↑ Summary
1 package Apache::FastForward;
2 # Copyright 2006 Jerzy Wachowiak
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Encode qw( resolve_alias encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶use Apache;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 @Apache::FastForward::ISA = qw( Apache );
12  
13 $VERSION = '1.1';
14  
15 # PUBLIC METHODS (convention: capital first letter)
16  
17 sub new {
18  
19 # Contract:
20 # [1] Input: $r for mod_perl subclassing ('r' or '_r' magic name)
21  
22 my $class = shift;
23 my $r = shift;
24
25 my $self = {};
26 $self->{VERSION} = $VERSION;
27 $self->{r} = $r;
28 $self->{csv} = Text::CSV_XS->new( {
29 'quote_char' => '"',
30 'escape_char' => '"',
31 'sep_char' => ';',
32 'binary' => 1 } ); # Needed by encoding of non ASCI characters!
33 $self->{tables} = undef;
34 bless ( $self, $class );
35 return $self
36 }
37  
38 sub ParseBody {
39  
40 # Contract:
41 # [1] Input: Encoding (if nothing defaults to UTF-8)
42 # [2] Output: returning success (1) or failure(0)
43  
44 my $self = shift;
45 my $document_charset = shift;
46  
47 $document_charset = 'UTF-8' unless defined( $document_charset );
48 resolve_alias( $document_charset ) or return 0;
49
50 $self->read( my $content, $self->header_in( 'Content-length' ) );
51 $content = decode( $document_charset, $content );
52
53 my ( @variable_names, $header_key );
54 my @lines = split( /\n/, $content );
55 for my $line ( @lines ){
56
57 chomp( $line );
58 next unless length( $line );
59
60 if ( $self->{csv}->parse( $line ) ){
61 my ( $steering_cell, @cells ) = $self->{csv}->fields();
62 $steering_cell = lc( trim( $steering_cell ) );
63
64 if ( $steering_cell eq 'post'){
65
66 undef( @variable_names );
67 for my $cell ( @cells ){
68 $cell = trim( $cell )
69 }
70 next if length( join( '', @cells ) ) == 0;
71
72 @variable_names = @cells;
73 undef( $header_key );
74 $header_key = build_header_key( @variable_names );
75 unless ( exists( $self->{tables}->{$header_key} ) ){
76 $self->{tables}->{$header_key} = undef
77 }
78 }
79 elsif ( $steering_cell eq 'value'){
80
81 @variable_names or next;
82
83 my %variable_value;
84 my $index;
85 for ( $index = 0; $index < scalar( @variable_names ); $index++ ){
86 my $value = $cells[$index];
87 if ( defined( $value ) ){
88 $value = trim( $value );
89 $variable_value{$variable_names[$index]} = $value
90 }
91 else {
92 $variable_value{$variable_names[$index]} = undef
93 }
94 }
95 push( @{$self->{tables}->{$header_key}}, \%variable_value );
96 }
97 else {
98 next
99 } #if ( $steering_cell eq 'post'){
100 }
101 else {
102 return 0
103 } #if ( $self->{csv}->parse( $line ) ){
104 } #for my $line ( @lines ){
105 return 1
106 }
107  
108 sub GetTable {
109  
110 # Contract:
111 # [1] Input: column names, order does not matter
112 # [2] Output: array of hashes [ {col1=> var11, col2=>var12}, {col1=> var21, col2=>var22} ]
113 # or undef if nothing exists
114
115 my ( $self, @column_headers ) = @_;
116  
117 my $header_key = build_header_key( @column_headers );
118 if ( defined( $self->{tables}->{$header_key} ) ){
119 return @{$self->{tables}->{$header_key}}
120 }
121 else {
122 return undef
123 }
124 }
125  
126 sub IsBodyEmpty {
127  
128 # Contract:
129 # [1] Input->no (parsed body is passed via the class instance)
130 # [2] Output: check success (1) or failure(0)
131
132 my $self = shift;
133
134 return not defined( $self->{tables} )
135 }
136  
137 sub IsDefinedTable {
138  
139 # Contract:
140 # [1] Input: column names, order does not matter
141 # [2] Output: check success (1) or failure(0)
142  
143 my ( $self, @column_headers ) = @_;
144  
145 my $header_key = build_header_key( @column_headers );
146
147 return defined( $self->{tables}->{$header_key} )
148  
149 }
150  
151 sub IsDefinedHeader {
152  
153 # Contract:
154 # [1] Input: column names, order does not matter
155 # [2] Output: check success (1) or failure(0)
156  
157 my ( $self, @column_headers ) = @_;
158  
159 my $header_key = build_header_key( @column_headers );
160
161 return exists( $self->{tables}->{$header_key} )
162 }
163  
164 sub ListHeaders {
165  
166 # Contract:
167 # [1] Input: last parsed body private via class instance
168 # [2] Output: array of headers or undef if nothing
169  
170 my $self = shift;
171  
172 return keys( %{$self->{tables}} )
173 }
174  
175 sub ListTables {
176  
177 # Contract:
178 # [1] Input: last parsed body private via class instance
179 # [2] Output: array of headers or undef if nothing
180  
181 my $self = shift;
182  
183 my @tables;
184 my @headers = keys( %{$self->{tables}} );
185 foreach my $header ( @headers ){
186 push( @tables, $header ) if $self->IsDefinedTable( $header )
187 }
188
189 return @tables
190 }
191  
192  
193 # PRIVATE METHODS (convention: small_letters)
194  
195 sub build_header_key{
196
197 my @column_headers = @_;
198
199 @column_headers = sort( @column_headers );
200 my $header_key = join( '|', @column_headers );
201
202 return $header_key
203 }
204  
205 sub trim {
206
207 my $string = shift;
208
209 $string =~ s/^\s+//;
210 $string =~s/\s+$//;
211
212 return $string
213 }
214 1
215 __END__
« FastForward/Spreadsheet.pm ↑ Summary