/[cwmp]/google/rules.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /google/rules.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (hide annotations)
Sun Jun 17 23:02:08 2007 UTC (16 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 2666 byte(s)
added general, name space removing, case insesitive _tag helper
and implemented parser for GetRPCMethodsResponse
1 dpavlin 23 #!/usr/bin/perl
2    
3     use warnings;
4     use strict;
5    
6     use XML::Rules;
7     use File::Slurp;
8     use Data::Dump qw/dump/;
9 dpavlin 26 use Carp qw/confess cluck/;
10 dpavlin 23
11     my $path = $ARGV[0] || 'protocol/inform.xml';
12    
13     my $state;
14    
15 dpavlin 26 # get tag name, case insensitive, namespace ignoring
16     sub _tag {
17     my ( $tag_hash, $name, $sub_key ) = @_;
18     confess "need hash as first argument" unless ( ref $tag_hash eq 'HASH' );
19     $name = (grep { m/$name/i } keys %$tag_hash )[0];
20     # $name =~ s/^\w+://;
21     if ( defined $tag_hash->{$name} ) {
22     if ( ! defined $sub_key ) {
23     return $tag_hash->{$name};
24     } elsif ( defined $tag_hash->{$name}->{$sub_key} ) {
25     return $tag_hash->{$name}->{$sub_key};
26     } else {
27     return if ( $name =~ m/^value$/i );
28     warn "can't find '$name/$sub_key' in ", dump( $tag_hash );
29     return;
30     }
31     } else {
32     warn "can't find '$name' in ", dump( $tag_hash );
33     return;
34     }
35     }
36    
37 dpavlin 23 my $parser = XML::Rules->new(
38     # start_rules => [
39     # '^division_name,fax' => 'skip',
40     # ],
41     namespaces => {
42 dpavlin 24 # 'http://schemas.xmlsoap.org/soap/envelope/' => 'soapenv',
43     # 'http://schemas.xmlsoap.org/soap/encoding/' => 'soap',
44     'urn:dslforum-org:cwmp-1-0' => '',
45 dpavlin 23 },
46     rules => [
47     #_default => 'content trim',
48     x_default => sub {
49     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
50     warn dump( $tag_name, $tag_hash, $context );
51     },
52 dpavlin 26 #
53     # Inform
54     #
55 dpavlin 23 qr/DeviceId/ => sub {
56     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
57 dpavlin 24 foreach my $name ( keys %$tag_hash ) {
58     next if $name eq '_content';
59     my $key = $name;
60     $key =~ s/^\w+://; # stip namespace
61 dpavlin 26 $state->{DeviceID}->{ $key } = _tag( $tag_hash, $name, '_content' );
62 dpavlin 24 }
63 dpavlin 23 },
64 dpavlin 24 qr/EventStruct/ => sub {
65 dpavlin 23 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
66 dpavlin 24 push @{ $state->{EventStruct} }, $tag_hash->{EventCode}->{_content};
67 dpavlin 23 },
68 dpavlin 25 qr/(MaxEnvelopes|CurrentTime|RetryCount)/ => sub {
69 dpavlin 23 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
70 dpavlin 25 $state->{$tag_name} = $tag_hash->{_content};
71     },
72     qr/ParameterValueStruct/ => sub {
73     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
74     # Name/Value tags must be case insnesitive
75     my $value = (grep( /value/i, keys %$tag_hash ))[0];
76 dpavlin 26 $state->{Parameter}->{ _tag($tag_hash, 'Name', '_content') } = _tag($tag_hash, 'Value', '_content' );
77 dpavlin 25 },
78 dpavlin 26 #
79     # GetRPCMethodsResponse
80     #
81     qr/^(?:^\w+:)*string$/ => 'content array',
82     qr/MethodList/ => sub {
83     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
84     $state->{MethodList} = _tag( $tag_hash, 'string' );
85     },
86 dpavlin 23 ]
87     );
88    
89     my $xml = read_file( $path );
90     print "## $1 ##\n" if ( $xml =~ s/^Name:\s+(.*?)$//sm );
91     $xml =~ s/^.*Response:\s+//sm;
92 dpavlin 24 #warn $xml;
93 dpavlin 23 $parser->parsestring( $xml );
94    
95     print "state = ", dump( $state );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26