| 1 | ########################################################################### | ||
| 2 | # | ||
| 3 | # $Id: JarReader.pm,v 1.2 2006/04/09 15:42:19 pkaluski Exp $ | ||
| 4 | # $Name: Stable_0_16 $ | ||
| 5 | # | ||
| 6 | # A module for parsing jar record format file. | ||
| 7 | # | ||
| 8 | # $Log: JarReader.pm,v $ | ||
| 9 | # Revision 1.2 2006/04/09 15:42:19 pkaluski | ||
| 10 | # Small code clean-up. Each module has comprehensive POD | ||
| 11 | # | ||
| 12 | # Revision 1.1 2006/01/01 13:13:50 pkaluski | ||
| 13 | # Initial revision. Works. | ||
| 14 | # | ||
| 15 | # | ||
| 16 | ############################################################################ | ||
| 17 | |||
| 18 | package LRpt::JarReader; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | LRpt::JarReader - A module for reading jar record format files. | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This class is a part of L<C<LRpt>|LRpt> library. | ||
| 28 | Object of this class provides an easy interface to jar record format file. | ||
| 29 | |||
| 30 | =head1 METHODS | ||
| 31 | |||
| 32 | In this sections you will find a more or less complete listing of all | ||
| 33 | methods provided by the package. Note that the package itself is not | ||
| 34 | public so none of those methods are guaranteed to be maintained in future | ||
| 35 | (including the package itself). | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | ############################################################################ | ||
| 40 | |||
| 41 | =head2 C<new> | ||
| 42 | |||
| 43 | my $jr = LRpt::JarReader->new( rules => $rules, | ||
| 44 | filename => $filename, | ||
| 45 | filehandle => $filehandle ); | ||
| 46 | |||
| 47 | Constructor. | ||
| 48 | Three parameters are allowed: | ||
| 49 | |||
| 50 | =over 4 | ||
| 51 | |||
| 52 | =item rules | ||
| 53 | |||
| 54 | A reference to a list defining the expected layout of the | ||
| 55 | input file. Each entry of the list is a reference to a hash | ||
| 56 | describing rules for one entry. Each hash may have the | ||
| 57 | following keys: | ||
| 58 | |||
| 59 | B<name> => Name of en entry in a section. This parameter is mandatory | ||
| 60 | |||
| 61 | B<mandatory> => Specifies if an entry is mandatory. The default is '1' | ||
| 62 | which means 'mandatory'. If an entry is mandatory, it is an error not | ||
| 63 | to specify it. | ||
| 64 | |||
| 65 | B<trim> => Specifies if traling spaces of an entry value should be | ||
| 66 | trimmed. Default is 1, i.e. trim | ||
| 67 | |||
| 68 | =item filename | ||
| 69 | |||
| 70 | Name of a file, which should be read as input | ||
| 71 | |||
| 72 | =item filehandle | ||
| 73 | |||
| 74 | Filehandle to a file, which should be read as input | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | ############################################################################ | ||
| 81 | sub new | ||
| 82 | { | ||
| 83 | my $proto = shift; | ||
| 84 | my %params = @_; | ||
| 85 | |||
| 86 | my $class = ref( $proto ) || $proto; | ||
| 87 | |||
| 88 | my $self = {}; | ||
| 89 | bless( $self, $class ); | ||
| 90 | $self->{ 'rules' } = $params{ 'rules' }; | ||
| 91 | if( $params{ 'filename' } ){ | ||
| 92 | die "filehandle must not be given when a file name is given" | ||
| 93 | if $params{ 'filehandle' }; | ||
| 94 | open( FH, "< $params{ 'filename' } " ) or | ||
| 95 | die "Cannot open $params{ 'filename' }"; | ||
| 96 | $self->{ 'filename' } = $params{ 'filename' }; | ||
| 97 | $self->{ 'filehandle' } = *FH; | ||
| 98 | }else{ | ||
| 99 | $self->{ 'filehandle' } = $params{ 'filehandle' }; | ||
| 100 | } | ||
| 101 | foreach my $rule ( @{ $self->{ 'rules' } } ){ | ||
| 102 | if( not exists $rule->{ 'mandatory' } ){ | ||
| 103 | $rule->{ 'mandatory' } = 1; | ||
| 104 | } | ||
| 105 | if( not exists $rule->{ 'trim' } ){ | ||
| 106 | $rule->{ 'trim' } = 1; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | ############################################################################ | ||
| 114 | |||
| 115 | =head2 C<read_all> | ||
| 116 | |||
| 117 | $jr->read_all(); | ||
| 118 | |||
| 119 | Parses the input file. Creates a list containing values for all sections. | ||
| 120 | Each item from the list is a reference to a section. Each section is | ||
| 121 | a hash, where key is an entry name and value is an entry value. | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | ############################################################################ | ||
| 126 | sub read_all | ||
| 127 | { | ||
| 128 | my $self = shift; | ||
| 129 | my $curr_entry = ""; | ||
| 130 | my $is_first = 1; | ||
| 131 | my $curr_section = {}; | ||
| 132 | my $key = ""; | ||
| 133 | my @sections = (); | ||
| 134 | my $fh = $self->{ 'filehandle' }; | ||
| 135 | while( 1 ){ | ||
| 136 | $_ = <$fh>; | ||
| 137 | if( ! defined $_ ){ | ||
| 138 | if( $is_first ){ | ||
| 139 | last; | ||
| 140 | } | ||
| 141 | $curr_section->{ $key } = $curr_entry; | ||
| 142 | $self->check_mandatory( $curr_section ); | ||
| 143 | push( @sections, $curr_section ); | ||
| 144 | $curr_section = {}; | ||
| 145 | $is_first = 1; | ||
| 146 | last; | ||
| 147 | } | ||
| 148 | if( /^#/ or /^\s+$/ ){ | ||
| 149 | next; | ||
| 150 | } | ||
| 151 | if( /^%%\s*$/ ){ | ||
| 152 | $curr_section->{ $key } = $curr_entry; | ||
| 153 | $self->check_mandatory( $curr_section ); | ||
| 154 | push( @sections, $curr_section ); | ||
| 155 | $curr_section = {}; | ||
| 156 | $is_first = 1; | ||
| 157 | next; | ||
| 158 | } | ||
| 159 | if( /^\w+:/ ){ | ||
| 160 | if( $is_first ){ # The first entry in a section | ||
| 161 | $is_first = 0; | ||
| 162 | }else{ | ||
| 163 | $curr_section->{ $key } = $curr_entry; | ||
| 164 | } | ||
| 165 | ( $key, $curr_entry ) = ( /^(\w+):\s+(.*)/ ); | ||
| 166 | }else{ | ||
| 167 | $curr_entry = $curr_entry . $_; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | $self->{ 'sections' } = \@sections; | ||
| 171 | } | ||
| 172 | |||
| 173 | ############################################################################ | ||
| 174 | |||
| 175 | =head2 C<check_mandatory> | ||
| 176 | |||
| 177 | $jr->check_mandatory( $section ); | ||
| 178 | |||
| 179 | Checks if the current section contains all mandatory entries (and | ||
| 180 | does necessary trimming). | ||
| 181 | |||
| 182 | =cut | ||
| 183 | |||
| 184 | ############################################################################ | ||
| 185 | sub check_mandatory | ||
| 186 | { | ||
| 187 | my $self = shift; | ||
| 188 | my $section = shift; | ||
| 189 | |||
| 190 | my @read_entries = keys %{ $section }; | ||
| 191 | my %def_names = (); | ||
| 192 | foreach my $def ( @{ $self->{ 'rules' } } ){ | ||
| 193 | $def_names{ $def->{ 'name' } } = 1; | ||
| 194 | if( ! exists $section->{ $def->{ 'name' } } ){ | ||
| 195 | if( $def->{ 'mandatory' } ){ | ||
| 196 | die "Mandatory entry '$def->{ 'name' }' not given"; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | if( $def->{ 'trim' } ){ | ||
| 200 | $section->{ $def->{ 'name' } } =~ s/\s*$//; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | foreach my $re ( @read_entries ){ | ||
| 204 | if( not exists $def_names{ $re } ){ | ||
| 205 | die "Unexpected entry $re"; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | ############################################################################ | ||
| 211 | |||
| 212 | =head2 C<get_section_with> | ||
| 213 | |||
| 214 | $jr->get_section_with( %entry ); | ||
| 215 | |||
| 216 | Returns a reference to a section containing an entry of a given name, having | ||
| 217 | a given value. | ||
| 218 | |||
| 219 | =cut | ||
| 220 | |||
| 221 | ############################################################################ | ||
| 222 | sub get_section_with | ||
| 223 | { | ||
| 224 | my $self = shift; | ||
| 225 | my %entry = @_; | ||
| 226 | |||
| 227 | my $name = ( keys %entry )[ 0 ]; | ||
| 228 | my $value = $entry{ $name }; | ||
| 229 | |||
| 230 | foreach my $sect ( @{ $self->{ 'sections' } } ){ | ||
| 231 | if( exists $sect->{ $name } ){ | ||
| 232 | if( $sect->{ $name } eq $value ){ | ||
| 233 | return $sect; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | return undef; | ||
| 238 | } | ||
| 239 | |||
| 240 | ############################################################################ | ||
| 241 | |||
| 242 | =head2 C<get_all_values_of> | ||
| 243 | |||
| 244 | $jr->get_all_values_of( $name ) | ||
| 245 | |||
| 246 | Returnes a list of all values of a given entry in all sections. | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | ############################################################################ | ||
| 251 | sub get_all_values_of | ||
| 252 | { | ||
| 253 | my $self = shift; | ||
| 254 | my $name = shift; | ||
| 255 | my @values = (); | ||
| 256 | foreach my $sect ( @{ $self->{ 'sections' } } ){ | ||
| 257 | push( @values, $sect->{ $name } ); | ||
| 258 | } | ||
| 259 | return @values; | ||
| 260 | } | ||
| 261 | |||
| 262 | 1; | ||
| 263 | |||
| 264 | =head1 SEE ALSO | ||
| 265 | |||
| 266 | The project is maintained on Source Forge L<http://lreport.sourceforge.net>. | ||
| 267 | You can find there links to some helpful documentation like tutorial. | ||
| 268 | |||
| 269 | =head1 AUTHORS | ||
| 270 | |||
| 271 | Piotr Kaluski E<lt>pkaluski@piotrkaluski.comE<gt> | ||
| 272 | |||
| 273 | =head1 COPYRIGHT | ||
| 274 | |||
| 275 | Copyright (c) 2004-2006 Piotr Kaluski. Poland. All rights reserved. | ||
| 276 | |||
| 277 | You may distribute under the terms of either the GNU General Public License | ||
| 278 | or the Artistic License, as specified in the Perl README file. | ||
| 279 | |||
| 280 | =cut | ||
| 281 |