]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Conf.pm
c0920d019804317519f9d8a6e7d44ca9686106d8
3 ################################################################################
5 # File: shared/Conf.pm #
7 # Authors: Andre Malo <nd@o3media.de>, 2001-06-16 #
9 # Description: read and parse configuration files #
11 ################################################################################
21 ################################################################################
24 $VERSION = do { my @r =(q
$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x
$#r, @r };
26 ################################################################################
30 use base
qw(Exporter);
31 @EXPORT = qw(read_script_conf);
33 ### add_limit () ###############################################################
37 # Params: $node - element node
38 # $conf - hashref of config hash (will be modified)
39 # $Script - scriptname (first name)
44 my ($node, $conf, $Script) = @_;
46 my %apps = map {($_ -> getFirstChild
-> getData
=> 1)}
47 $node -> getElementsByTagName
('Application',0) -> item
(0)
48 -> getElementsByTagName
('Script',0);
50 if ($apps {$Script}) {
51 foreach ($node -> getElementsByTagName
('Constant', 0)) {add_data
($_, $conf)}
52 foreach ($node -> getElementsByTagName
('Property', 0)) {add_prop
($_, $conf)}}
57 ### add_prop () ################################################################
59 # add a property (recursive if necessary)
61 # Params: $node - element node
62 # $conf - hashref of config hash (will be modified)
67 my ($node, $conf) = @_;
69 my $name = $node -> getAttribute
('name');
71 die "element 'Property' requires attribute 'name' - aborted" unless (length ($name));
73 my @props = $node -> getElementsByTagName
('Property', 0);
74 my @vars = $node -> getElementsByTagName
('Variable', 0);
75 my @lists = $node -> getElementsByTagName
('List', 0);
81 my $hash = (defined $conf -> {$name})?
$conf -> {$name}:{};
83 die "name '$name' is defined for 'Property' and 'Variable' - aborted" unless (ref $hash eq 'HASH');
85 &add_prop
($_, $hash);
86 $conf -> {$name} = $hash;}}
92 my $lname = $_ -> getAttribute
('name');
94 die "element 'List' requires attribute 'name' - aborted" unless (length ($lname) and defined ($lname));
95 die "double defined name '$lname' - aborted" if ( exists ( $conf -> {$name} -> {$lname} ) );
97 $conf -> {$name} -> {$lname} = [map {($_ -> hasChildNodes
)?
$_ -> getFirstChild
-> getData
:undef} $_ -> getElementsByTagName
('ListItem', 0)];}}
103 my $vname = $_ -> getAttribute
('name');
105 die "element 'Variable' requires attribute 'name' - aborted" unless (length ($vname) and defined ($vname));
106 die "double defined name '$vname' - aborted" if ( exists ( $conf -> {$name} -> {$vname} ) );
108 $conf -> {$name} -> {$vname} = ($_ -> hasChildNodes
)?
$_ -> getFirstChild
-> getData
:undef;}}
113 ### add_data () ################################################################
115 # add a real value (Constant or Variable)
117 # Params: $node - Element node
118 # $conf - hashref of config hash (will be modified)
123 my ($node, $conf) = @_;
124 my $name = $node -> getAttribute
('name');
126 die q
"element '".$node -> getNodeName
.q
"' requires attribute 'name' - aborted" unless (length ($name) and defined ($name));
127 die q
"double defined name '$name' - aborted" if ( exists ( $conf -> {$name} ) );
129 $conf -> {$name} = ($node -> hasChildNodes
)?
$node -> getFirstChild
-> getData
:undef;
134 ### parse_script_conf () #######################################################
136 # parse a config file
138 # Params: $filename - filename
139 # $conf - hashref of config hash (hash will be modified)
140 # $Script - scriptname
144 sub parse_script_conf
($\
%$) {
145 my ($filename, $conf, $Script) = @_;
148 my $xml = new XML
::DOM
::Parser
-> parsefile
($filename);
149 my $config = $xml -> getElementsByTagName
('Config', 0) -> item
(0);
151 add_data
$_, $conf for ($config -> getElementsByTagName
('Constant', 0));
152 add_prop
$_, $conf for ($config -> getElementsByTagName
('Property', 0));
153 add_limit
$_, $conf, $Script for ($config -> getElementsByTagName
('Limit', 0));
159 ### read_script_conf () ########################################################
161 # read and parse whole script config.
163 # Params: $Config - /path/to/config-dir # NO trailing slash please
164 # $Shared - /path/to/shared-dir # -- " --
165 # $Script - scriptname
167 # Return: hashref of config hash
169 sub read_script_conf
($$$) {
170 my ($Config, $Shared, $Script) = @_;
172 $Script =~ s/^(.*)\..*$/$1/; # extract script's 'first name'
173 my $common = "$Shared/common.xml"; # shared config file
174 my $group = "$Config/common.xml"; # group config file
175 my $special = "$Config/$Script.xml"; # special script config file
176 my %conf=(); # config hash
178 parse_script_conf
($common , %conf, $Script);
179 parse_script_conf
($group, %conf, $Script);
180 parse_script_conf
($special, %conf, $Script);
187 # keep 'require' happy
192 ### end of Conf ################################################################
194 ### show() #####################################################################
198 # Usage: Conf::Test::show (hashref)
200 package Conf
::Test
;sub show
{print"Content-type: text/plain\n\n";&hash
($_[
201 0],'')}sub hash
{my($ref,$string)=@_;foreach(sort keys%$ref){my$val=$ref->
202 {$_};unless(ref($val)){print$string,$_,' = ',$val,"\n";next;}else{if(ref(
203 $val)eq 'HASH'){&hash
($val,"$string$_ -> ");}else{if(ref($val)eq'ARRAY'){
204 &array
($val,"$string$_ -> ");}}}}}sub array
{my($ref,$string)=@_;my $i=0;
205 foreach (@
$ref){unless(ref($_)){print$string,"[$i] = ", $_,"\n";}else{if(
206 ref($_)eq 'HASH'){&hash
($_,"$string\[$i] -> ")}else{if(ref($_)eq'ARRAY'){
207 &array
($_,"$string\[$i] -> ");}}}$i++;}}# n.d.p./2001-01-05/lm:2001-01-19
208 # FUNCTION: printing the configuration, USAGE: &Conf::Test::show ($conf);
212 ### *real* end of Conf ;-) #####################################################
patrick-canterino.de