| 1 | ########################################################################### | ||
| 2 | # | ||
| 3 | # $Id: KeySubst.pm,v 1.4 2006/09/17 19:22:37 pkaluski Exp $ | ||
| 4 | # $Name: Stable_0_16 $ | ||
| 5 | # | ||
| 6 | # Module, which encapsulates logic of substituting where key entries | ||
| 7 | # with the actual values. | ||
| 8 | # | ||
| 9 | # $Log: KeySubst.pm,v $ | ||
| 10 | # Revision 1.4 2006/09/17 19:22:37 pkaluski | ||
| 11 | # Cosmetic changes in help screen | ||
| 12 | # | ||
| 13 | # Revision 1.3 2006/09/10 18:31:53 pkaluski | ||
| 14 | # Selects are taken from files or STDIN (like with diamond operator). | ||
| 15 | # | ||
| 16 | # Revision 1.2 2006/04/09 15:42:19 pkaluski | ||
| 17 | # Small code clean-up. Each module has comprehensive POD | ||
| 18 | # | ||
| 19 | # Revision 1.1 2006/01/01 13:13:48 pkaluski | ||
| 20 | # Initial revision. Works. | ||
| 21 | # | ||
| 22 | # | ||
| 23 | ########################################################################### | ||
| 24 | package LRpt::KeySubst; | ||
| 25 | use strict; | ||
| 26 | use Getopt::Long; | ||
| 27 | use LRpt::JarReader; | ||
| 28 | require Exporter; | ||
| 29 | |||
| 30 | =head1 NAME | ||
| 31 | |||
| 32 | LRpt::KeySubst - A module for substituting where keys placeholders in | ||
| 33 | select templates | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | lks.pl --keys=keys.txt selects.txt | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | This module is a part of L<C<LRpt>|LRpt> (B<LReport>) library. | ||
| 42 | It is used for replacing I<where keys> placeholders with the actual value | ||
| 43 | in selects templates. | ||
| 44 | You should not use C<LRpt::KeySubst> module directly in your code. | ||
| 45 | Instead you should use B<lks.pl> tool, which is a simple wrapper | ||
| 46 | around the module. B<lks.pl> looks like this: | ||
| 47 | |||
| 48 | use strict; | ||
| 49 | use LRpt::KeySubst; | ||
| 50 | |||
| 51 | wkey_subst( @ARGV ); | ||
| 52 | |||
| 53 | |||
| 54 | =head1 COMMAND LINE OPTIONS | ||
| 55 | |||
| 56 | =over 4 | ||
| 57 | |||
| 58 | =item --keys=name | ||
| 59 | |||
| 60 | Name of a file containing actual values for where keys to be put in | ||
| 61 | select templates. | ||
| 62 | |||
| 63 | =item --help | ||
| 64 | |||
| 65 | Prints help screen. | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | |||
| 72 | use vars qw( @EXPORT @ISA ); | ||
| 73 | @ISA = qw(Exporter); | ||
| 74 | @EXPORT = qw(wkey_subst); | ||
| 75 | |||
| 76 | |||
| 77 | # | ||
| 78 | # For JarReader | ||
| 79 | # | ||
| 80 | our @keys_rules = ( { 'name' => 'name' }, | ||
| 81 | { 'name' => 'key' } ); | ||
| 82 | |||
| 83 | our @sel_rules = ( { 'name' => 'name' }, | ||
| 84 | { 'name' => 'select' } ); | ||
| 85 | |||
| 86 | =head1 METHODS | ||
| 87 | |||
| 88 | In this sections you will find a more or less complete listing of all | ||
| 89 | methods provided by the package. Note that the package itself is not | ||
| 90 | public so none of those methods are guaranteed to be maintained in future | ||
| 91 | (including the package itself). | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | ############################################################################ | ||
| 96 | |||
| 97 | =head2 C<wkey_subst> | ||
| 98 | |||
| 99 | wkey_subst( @ARGV ); | ||
| 100 | |||
| 101 | Main function. @ARGV is processes by standard Getopt::Long module. Meaning | ||
| 102 | of each switch is given in L<SYNOPSIS|"SYNOPSIS">. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | ############################################################################ | ||
| 107 | sub wkey_subst | ||
| 108 | { | ||
| 109 | local( @ARGV ) = @_; | ||
| 110 | my $keys_file = ""; | ||
| 111 | my $help = ""; | ||
| 112 | |||
| 113 | GetOptions( "keys=s" => \$keys_file, | ||
| 114 | "help" => \$help ); | ||
| 115 | if( !$keys_file or $help ){ | ||
| 116 | print_usage(); | ||
| 117 | exit( 1 ); | ||
| 118 | } | ||
| 119 | |||
| 120 | my $key_jr = LRpt::JarReader->new( 'rules' => \@keys_rules, | ||
| 121 | 'filename' => $keys_file ); | ||
| 122 | |||
| 123 | $key_jr->read_all(); | ||
| 124 | unshift( @ARGV, "-" ) unless @ARGV; | ||
| 125 | while( my $file = shift( @ARGV ) ){ | ||
| 126 | open( SEL_FILE, "< $file" ) or die "Cannot open $file for reading: $!"; | ||
| 127 | my $select_jr = LRpt::JarReader->new( 'rules' => \@sel_rules, | ||
| 128 | 'filehandle' => *SEL_FILE ); | ||
| 129 | |||
| 130 | $select_jr->read_all(); | ||
| 131 | |||
| 132 | my @sel_names = $select_jr->get_all_values_of( 'name' ); | ||
| 133 | foreach my $sel ( @sel_names ){ | ||
| 134 | my $section = $select_jr->get_section_with( 'name' => $sel ); | ||
| 135 | my $select = $section->{ 'select' }; | ||
| 136 | print "name: $sel\n"; | ||
| 137 | while( $select =~ /--(\w+)--/ ){ | ||
| 138 | my $key_name = $1; | ||
| 139 | my $sect = $key_jr->get_section_with( 'name' => $key_name ); | ||
| 140 | if( !$sect ){ | ||
| 141 | die "Key $key_name not defined or empty value given"; | ||
| 142 | }else{ | ||
| 143 | my $value = $sect->{ 'key' }; | ||
| 144 | $select =~ s/--$key_name--/$value/g; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | print "select: $select\n"; | ||
| 148 | print "%%\n"; | ||
| 149 | } | ||
| 150 | close( SEL_FILE ) or die "Cannot close $file : $!"; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | ########################################################################### | ||
| 155 | |||
| 156 | =head2 C<print_usage> | ||
| 157 | |||
| 158 | print_usage(); | ||
| 159 | |||
| 160 | Prints usage text | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | ########################################################################### | ||
| 165 | sub print_usage | ||
| 166 | { | ||
| 167 | print "Usage: $0 --help --keys=<name> filenames\n"; | ||
| 168 | print "\n"; | ||
| 169 | print " --help - prints this help screen\n"; | ||
| 170 | print " --keys - name of a file containing where key"; | ||
| 171 | print " definitions\n"; | ||
| 172 | print " filenames - names of files containing selects to be\n"; | ||
| 173 | print " parsed\n"; | ||
| 174 | exit( 0 ); | ||
| 175 | } | ||
| 176 | 1; | ||
| 177 | |||
| 178 | |||
| 179 | =head1 SEE ALSO | ||
| 180 | |||
| 181 | The project is maintained on Source Forge L<http://lreport.sourceforge.net>. | ||
| 182 | You can find there links to some helpfull documentation like tutorial. | ||
| 183 | |||
| 184 | =head1 AUTHORS | ||
| 185 | |||
| 186 | Piotr Kaluski E<lt>pkaluski@piotrkaluski.comE<gt> | ||
| 187 | |||
| 188 | =head1 COPYRIGHT | ||
| 189 | |||
| 190 | Copyright (c) 2004-2006 Piotr Kaluski. Poland. All rights reserved. | ||
| 191 | |||
| 192 | You may distribute under the terms of either the GNU General Public License | ||
| 193 | or the Artistic License, as specified in the Perl README file. | ||
| 194 | |||
| 195 | =cut | ||
| 196 |