###
### Copyright 2000-2007 University of Illinois Board of Trustees
### All rights reserved.
###
### AppConfig - psgconf action class for AppConfig files
###
### Campus Information Technologies and Educational Services
### University of Illinois at Urbana-Champaign
###
package PSGConf::Action::GenerateFile::AppConfig;
use strict;
use PSGConf::Action::GenerateFile;
our @ISA = qw(PSGConf::Action::GenerateFile);
###############################################################################
### generate method
###############################################################################
sub _print_entry
{
my ($self, $fh, $key, $value) = @_;
$value = " "
if ( ! defined ($value) || ! length ($value) );
### Backslash any quotes in the value itself.
$value =~ s/"/\\"/g
if ( $value =~ m/"/o );
# quote any values as well.
$value = '"' . $value . '"'
if ( $value =~ m/[\s\t\n]/o );
### Backslash any carriage returns in the value.
$value =~ s/\n/\\
/g
if ( $value =~ m/\n/o );
print $fh "$key = $value\n";
}
sub _deref_entry
{
my ($self, $fh, $key, $value) = @_;
my ($ref, $c);
$ref = ref($value);
### If we are pointing to an ARRAY, get each element.
if ( $ref eq 'ARRAY' ) {
for ( $c = 0; $c < scalar @$value; $c++ ) {
$self->_deref_entry ($fh, $key, $value->[$c]);
}
### If we are pointing to A HASH, get both the key and value.
} elsif ( $ref eq 'HASH' ) {
print $fh "[$key]\n"
if ( exists $self->{stanza} );
foreach $c (sort keys %{$value}) {
$self->_deref_entry (
$fh,
(exists $self->{stanza})? $c: "$key $c",
$value->{$c}
);
}
print $fh "\n"
if ( exists $self->{stanza} );
### Base case, print out the entry...
} else {
$self->_print_entry ($fh, $key, $value);
}
}
sub generate
{
my ($self, $fh, $psgconf) = @_;
my ($var);
foreach $var (sort keys %{$self->{vars}})
{
$self->_deref_entry ($fh, $var, $self->{vars}->{$var});
}
return 1;
}
###############################################################################
### documentation
###############################################################################
1;
__END__
=head1 NAME
PSGConf::Action::GenerateFile::AppConfig - generate environment files
=head1 SYNOPSIS
use PSGConf::Action::GenerateFile::AppConfig;
$psgconf->register_actions(
PSGConf::Action::GenerateFile::AppConfig->new(
'name' => '/path/to/file',
'stanza' => 1,
'vars' => {
'var1' => 'value1',
...
},
...
),
...
);
=head1 DESCRIPTION
The B<PSGConf::Action::GenerateFile::AppConfig>
module provides a B<PSGConf> action class for generating files
in the format that AppConfig perl module can use.
The B<PSGConf::Action::GenerateFile::AppConfig> class
is derived from the B<PSGConf::Action::GenerateFile> class, but it
defines/overrides the following methods:
=over 4
=item generate()
Generates the AppConfig formatted file.
=back
In addition to the attributes supported by
the B<PSGConf::Action::GenerateFile> class, the
B<PSGConf::Action::GenerateFile::AppConfig> class supports the following
attributes:
=over 4
=item I<stanza>
If it exists, it uses the key from a hash as a starting stanza,
i.e. I<[key]> format.
=item I<vars>
An anonymous hash containing the AppConfig variable definitions.
=back
=head1 SEE ALSO
L<perl>
L<AppConfig>
L<PSGConf>
L<PSGConf::Action::GenerateFile>
=cut
syntax highlighted by Code2HTML, v. 0.9.1