/[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 27 - (hide annotations)
Sun Jun 17 23:05:52 2007 UTC (16 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 2798 byte(s)
added ID
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 dpavlin 27 $name = (grep { m/^(?:\w+:)*$name$/i } keys %$tag_hash )[0];
20 dpavlin 26 # $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 27 'ID' => sub {
53     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
54     $state->{ID} = $tag_hash->{_content};
55     },
56 dpavlin 26 #
57     # Inform
58     #
59 dpavlin 23 qr/DeviceId/ => sub {
60     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
61 dpavlin 24 foreach my $name ( keys %$tag_hash ) {
62     next if $name eq '_content';
63     my $key = $name;
64     $key =~ s/^\w+://; # stip namespace
65 dpavlin 26 $state->{DeviceID}->{ $key } = _tag( $tag_hash, $name, '_content' );
66 dpavlin 24 }
67 dpavlin 23 },
68 dpavlin 24 qr/EventStruct/ => sub {
69 dpavlin 23 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
70 dpavlin 24 push @{ $state->{EventStruct} }, $tag_hash->{EventCode}->{_content};
71 dpavlin 23 },
72 dpavlin 25 qr/(MaxEnvelopes|CurrentTime|RetryCount)/ => sub {
73 dpavlin 23 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
74 dpavlin 25 $state->{$tag_name} = $tag_hash->{_content};
75     },
76     qr/ParameterValueStruct/ => sub {
77     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
78     # Name/Value tags must be case insnesitive
79     my $value = (grep( /value/i, keys %$tag_hash ))[0];
80 dpavlin 26 $state->{Parameter}->{ _tag($tag_hash, 'Name', '_content') } = _tag($tag_hash, 'Value', '_content' );
81 dpavlin 25 },
82 dpavlin 26 #
83     # GetRPCMethodsResponse
84     #
85     qr/^(?:^\w+:)*string$/ => 'content array',
86     qr/MethodList/ => sub {
87     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
88     $state->{MethodList} = _tag( $tag_hash, 'string' );
89     },
90 dpavlin 23 ]
91     );
92    
93     my $xml = read_file( $path );
94     print "## $1 ##\n" if ( $xml =~ s/^Name:\s+(.*?)$//sm );
95     $xml =~ s/^.*Response:\s+//sm;
96 dpavlin 24 #warn $xml;
97 dpavlin 23 $parser->parsestring( $xml );
98    
99     print "state = ", dump( $state );

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26