[ale] Issue with sub name and XML::Parser
Christopher Fowler
cfowler at outpostsentinel.com
Wed Dec 1 10:29:20 EST 2004
On Wed, 2004-12-01 at 09:45, Fletch wrote:
> >>>>> "Christopher" == Christopher Fowler <cfowler at outpostsentinel.com> writes:
>
> [...]
>
>
> Christopher> Perl does not like 'sub param-name'
>
> Nope, gotta be _?[A-z0-9_]+. You might can hack it by assigning a
> coderef to a glob:
>
> *{"param-name"} = sub { ... }
>
>
> Or use the Stream style instead. Or look at XML::SAX.
>
> http://search.cpan.org/~msergeant/XML-SAX-0.12/SAX/Intro.pod
I think I got it working somewhat.
Here is code:
#!/opt/SAM/perl/bin/perl -I/opt/SAM/perl-lib
use XML::Parser;
my %PARAMS = ();
sub add_param {
my($name, $value) = @_;
$PARAMS{$name} = $value;
return;
}
{
package web_xml;
my $p_name = undef;
my $p_value = undef;
*{"param-name"} = sub {
$p_name = 1;
};
*{"_param-name"} = sub {
$p_name = undef;
};
*{"param-value"} = sub {
};
*{"_param-value"} = sub {
$p_value = undef;
};
sub char_handler {
my ($v1, $v2) = @_;
return unless $p_name or $p_value;
if($p_name == 1) {
$p_name = $v2;
$p_value = 2;
} elsif ($p_value == 2) {
return if $v2 =~ m/^\s/;
#print "Paramater Name: [$p_name]\n";
#print "Parameter Value: [$v2]\n";
::add_param($p_name, $v2);
$p_value = undef;
$p_name = 0;
}
}
}
sub get_data {
my $data = "";
open(F, "</usr/local/tomcat/webapps/AlarmCenter/WEB-INF/web.xml");
while(<F>) {
chomp;
$data .= $_;
}
close(F);
return $data;
}
sub main {
my $data = get_data();
my $parser = new XML::Parser(Style => 'Subs', Pkg => 'web_xml');
$parser->setHandlers(Char => \&web_xml::char_handler);
$parser->parse($data);
foreach (keys %PARAMS) {
print "$_ = $PARAMS{$_}\n";
}
return;
}
main;
# vi: set ts=2 sw=2: #
Here is output:
[tomcat at sam-devel Reports]$ /tmp/test.pl | more
db-driver = org.gjt.mm.mysql.Driver
xslt = /WEB-INF/xml/main.xsl
cmsdb-service-name = cmsdb
web-version = Version 1.1.0 (Build 2944)
cc-proxy-timeout = 2
jcd-ssl = true
db-user = cms
console-log-path = /u01/cms/logs/
regex-validator = /opt/SAM/Utils/bin/regex_test.pl -t
db-url = jdbc:mysql://127.0.0.1/ALARMCENTER
notify-script = /opt/SAM/ensbins/notify.pl
cc-proxy-port = 4000
db-pass = cms
cc-ens-port = 782
restart-script = /opt/SAM/Utils/bin/restart.pl
jcd-port = 785
Here is XML piece:
<!-- GENERAL APPLICATION CONFIGURATION -->^M
<context-param><param-name>db-driver</param-name><param-value>org.gjt.mm.mysql.Driver</param-value></context-param>^M
<context-param><param-name>db-url</param-name><param-value>jdbc:mysql://127.0.0.1/ALARMCENTER</param-value></context-param>^M
<context-param><param-name>db-user</param-name><param-value>cms</param-value></context-param>^M
<context-param><param-name>db-pass</param-name><param-value>cms</param-value></context-param>^M
<context-param><param-name>console-log-path</param-name><param-value>/u01/cms/logs/</param-value></context-param>^M
<context-param><param-name>default-organization</param-name><param-value/></context-param>^M
<context-param>^M
<param-name>cc-proxy-timeout</param-name>^M
<param-value>2</param-value>^M
</context-param>^M
<context-param><param-name>cc-proxy-port</param-name><param-value>4000</param-value></context-param>^M
<context-param>^M
<param-name>cc-ens-port</param-name>^M
<param-value>782</param-value>^M
</context-param>^M
<context-param>^M
<param-name>jcd-port</param-name>^M
<param-value>785</param-value>^M
</context-param>^M
<context-param>^M
<param-name>jcd-ssl</param-name>^M
<param-value>true</param-value>^M
</context-param>^M
<context-param><param-name>restart-script</param-name><param-value>/opt/SAM/Utils/bin/restart.pl</param-value></context-param>^M
<context-param>^M
<param-name>cmsdb-service-name</param-name>^M
<param-value>cmsdb</param-value>^M
</context-param>^M
The _<sub names> do not seem to be working. I'msure there are some
gotchas that will break this code but the server is in a controlled
environment. The users do not even have tty access to the Linux box.
Web only.
More information about the Ale
mailing list