]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Conf.pm
3 ################################################################################
5 # File: shared/Conf.pm #
7 # Authors: André Malo <nd@o3media.de> #
9 # Description: read and parse configuration files #
11 ################################################################################
20 ################################################################################
28 sub VERSION
{(q
$Revision$ =~ /([\d.]+)\s*$/)[0] or '0.0'}
30 ################################################################################
34 use base
qw(Exporter);
35 @EXPORT = qw(read_script_conf);
37 ### add_limit () ###############################################################
41 # Params: $node - element node
42 # $conf - hashref of config hash (will be modified)
43 # $Script - scriptname (first name)
48 my ($node, $conf, $Script) = @_;
50 my %apps = map {($_ -> getFirstChild
-> getData
=> 1)}
51 $node -> getElementsByTagName
('Application',0) -> item
(0)
52 -> getElementsByTagName
('Script',0);
54 if ($apps {$Script}) {
55 foreach ($node -> getElementsByTagName
('Constant', 0)) {add_data
($_, $conf)}
56 foreach ($node -> getElementsByTagName
('Property', 0)) {add_prop
($_, $conf)}}
61 ### add_prop () ################################################################
63 # add a property (recursive if necessary)
65 # Params: $node - element node
66 # $conf - hashref of config hash (will be modified)
71 my ($node, $conf) = @_;
73 my $name = $node -> getAttribute
('name');
75 die "element 'Property' requires attribute 'name' - aborted" unless (length ($name));
77 my @props = $node -> getElementsByTagName
('Property', 0);
78 my @vars = $node -> getElementsByTagName
('Variable', 0);
79 my @lists = $node -> getElementsByTagName
('List', 0);
85 my $hash = (defined $conf -> {$name})?
$conf -> {$name}:{};
87 die "name '$name' is defined for 'Property' and 'Variable' - aborted" unless (ref $hash eq 'HASH');
89 &add_prop
($_, $hash);
90 $conf -> {$name} = $hash;}}
96 my $lname = $_ -> getAttribute
('name');
98 die "element 'List' requires attribute 'name' - aborted" unless (length ($lname) and defined ($lname));
99 die "double defined name '$lname' - aborted" if ( exists ( $conf -> {$name} -> {$lname} ) );
101 $conf -> {$name} -> {$lname} = [map {($_ -> hasChildNodes
)?
$_ -> getFirstChild
-> getData
:undef} $_ -> getElementsByTagName
('ListItem', 0)];}}
107 my $vname = $_ -> getAttribute
('name');
109 die "element 'Variable' requires attribute 'name' - aborted" unless (length ($vname) and defined ($vname));
110 die "double defined name '$vname' - aborted" if ( exists ( $conf -> {$name} -> {$vname} ) );
112 $conf -> {$name} -> {$vname} = ($_ -> hasChildNodes
)?
$_ -> getFirstChild
-> getData
:undef;}}
117 ### add_data () ################################################################
119 # add a real value (Constant or Variable)
121 # Params: $node - Element node
122 # $conf - hashref of config hash (will be modified)
127 my ($node, $conf) = @_;
128 my $name = $node -> getAttribute
('name');
130 die q
"element '".$node -> getNodeName
.q
"' requires attribute 'name' - aborted" unless (length ($name) and defined ($name));
131 die q
"double defined name '$name' - aborted" if ( exists ( $conf -> {$name} ) );
133 $conf -> {$name} = ($node -> hasChildNodes
)?
$node -> getFirstChild
-> getData
:undef;
138 ### parse_script_conf () #######################################################
140 # parse a config file
142 # Params: $filename - filename
143 # $conf - hashref of config hash (hash will be modified)
144 # $Script - scriptname
148 sub parse_script_conf
($\
%$) {
149 my ($filename, $conf, $Script) = @_;
152 my $xml = new XML
::DOM
::Parser
-> parsefile
($filename);
153 my $config = $xml -> getElementsByTagName
('Config', 0) -> item
(0);
155 add_data
$_, $conf for ($config -> getElementsByTagName
('Constant', 0));
156 add_prop
$_, $conf for ($config -> getElementsByTagName
('Property', 0));
157 add_limit
$_, $conf, $Script for ($config -> getElementsByTagName
('Limit', 0));
163 ### read_script_conf () ########################################################
165 # read and parse whole script config.
167 # Params: $Config - /path/to/config-dir # NO trailing slash please
168 # $Shared - /path/to/shared-dir # -- " --
169 # $Script - scriptname
171 sub read_script_conf
($$$) {
172 my ($Config, $Shared, $Script) = @_;
174 $Script =~ s/^(.*)\..*$/$1/; # extract script's 'first name'
175 my $common = "$Shared/common.xml"; # shared config file
176 my $group = "$Config/common.xml"; # group config file
177 my $special = "$Config/$Script.xml"; # special script config file
178 my %conf=(); # config hash
180 parse_script_conf
($common , %conf, $Script);
181 parse_script_conf
($group, %conf, $Script);
182 parse_script_conf
($special, %conf, $Script);
189 # keep 'require' happy
194 ### end of Conf ################################################################
196 ### show() #####################################################################
200 # Usage: Conf::Test::show (hashref)
202 package Conf
::Test
;sub show
{print"Content-type: text/plain\n\n";&hash
($_[
203 0],'')}sub hash
{my($ref,$string)=@_;foreach(sort keys%$ref){my$val=$ref->
204 {$_};unless(ref($val)){print$string,$_,' = ',$val,"\n";next;}else{if(ref(
205 $val)eq 'HASH'){&hash
($val,"$string$_ -> ");}else{if(ref($val)eq'ARRAY'){
206 &array
($val,"$string$_ -> ");}}}}}sub array
{my($ref,$string)=@_;my $i=0;
207 foreach (@
$ref){unless(ref($_)){print$string,"[$i] = ", $_,"\n";}else{if(
208 ref($_)eq 'HASH'){&hash
($_,"$string\[$i] -> ")}else{if(ref($_)eq'ARRAY'){
209 &array
($_,"$string\[$i] -> ");}}}$i++;}}# n.d.p./2001-01-05/lm:2001-01-19
210 # FUNCTION: printing the configuration, USAGE: &Conf::Test::show ($conf);
214 ### *real* end of Conf ;-) #####################################################
patrick-canterino.de