/[cwmp]/google/trunk/lib/CWMP/Request.pm
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/trunk/lib/CWMP/Request.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 90 - (hide annotations)
Sat Jun 23 08:14:45 2007 UTC (16 years, 11 months ago) by dpavlin
Original Path: google/lib/CWMP/Request.pm
File size: 4234 byte(s)
parse ParameterInfoStruct into tree structure by rewriting name into perl
(hack, this *is* dynamic language) and evaling that.
1 dpavlin 31 package CWMP::Request;
2    
3     use warnings;
4     use strict;
5    
6     use XML::Rules;
7     use Data::Dump qw/dump/;
8     use Carp qw/confess cluck/;
9    
10     =head1 NAME
11    
12     CWMP::Request - parse SOAP request
13    
14     =head1 METHODS
15    
16     =head2 _tag
17    
18     Get value of tag. Tag name is case insensitive (don't ask why),
19     we ignore namespaces and can take optional C<sub_key>
20     (usually C<_content>).
21    
22     _tag( $tag_hash, $name, $sub_key )
23    
24     =cut
25    
26     sub _tag {
27     my ( $tag_hash, $name, $sub_key ) = @_;
28     confess "need hash as first argument" unless ( ref $tag_hash eq 'HASH' );
29     $name = (grep { m/^(?:\w+:)*$name$/i } keys %$tag_hash )[0];
30     # $name =~ s/^\w+://;
31     if ( defined $tag_hash->{$name} ) {
32     if ( ! defined $sub_key ) {
33     return $tag_hash->{$name};
34     } elsif ( defined $tag_hash->{$name}->{$sub_key} ) {
35     return $tag_hash->{$name}->{$sub_key};
36     } else {
37     return if ( $name =~ m/^value$/i );
38     warn "can't find '$name/$sub_key' in ", dump( $tag_hash );
39     return;
40     }
41     } else {
42     warn "can't find '$name' in ", dump( $tag_hash );
43     return;
44     }
45     }
46    
47 dpavlin 90 our $state; # FIXME check this!
48 dpavlin 31
49     my $parser = XML::Rules->new(
50     # start_rules => [
51     # '^division_name,fax' => 'skip',
52     # ],
53     namespaces => {
54 dpavlin 62 'http://schemas.xmlsoap.org/soap/envelope/' => 'soapenv',
55     'http://schemas.xmlsoap.org/soap/encoding/' => 'soap',
56     'http://www.w3.org/2001/XMLSchema' => 'xsd',
57     'http://www.w3.org/2001/XMLSchema-instance' => 'xsi',
58 dpavlin 31 'urn:dslforum-org:cwmp-1-0' => '',
59     },
60     rules => [
61     #_default => 'content trim',
62     x_default => sub {
63     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
64     warn dump( $tag_name, $tag_hash, $context );
65     },
66     'ID' => sub {
67     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
68     $state->{ID} = $tag_hash->{_content};
69     },
70     #
71     # Inform
72     #
73 dpavlin 36 'Inform' => sub {
74 dpavlin 71 $state->{_dispatch} = 'InformResponse'; # what reponse to call
75 dpavlin 36 },
76 dpavlin 65 'DeviceId' => sub {
77 dpavlin 31 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
78     foreach my $name ( keys %$tag_hash ) {
79     next if $name eq '_content';
80     my $key = $name;
81     $key =~ s/^\w+://; # stip namespace
82     $state->{DeviceID}->{ $key } = _tag( $tag_hash, $name, '_content' );
83     }
84     },
85 dpavlin 65 'EventStruct' => sub {
86 dpavlin 31 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
87     push @{ $state->{EventStruct} }, $tag_hash->{EventCode}->{_content};
88     },
89     qr/(MaxEnvelopes|CurrentTime|RetryCount)/ => sub {
90     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
91     $state->{$tag_name} = $tag_hash->{_content};
92     },
93 dpavlin 65 'ParameterValueStruct' => sub {
94 dpavlin 31 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
95     # Name/Value tags must be case insnesitive
96     my $value = (grep( /value/i, keys %$tag_hash ))[0];
97     $state->{Parameter}->{ _tag($tag_hash, 'Name', '_content') } = _tag($tag_hash, 'Value', '_content' );
98     },
99     #
100     # GetRPCMethodsResponse
101     #
102     qr/^(?:^\w+:)*string$/ => 'content array',
103 dpavlin 65 'MethodList' => sub {
104 dpavlin 31 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
105     $state->{MethodList} = _tag( $tag_hash, 'string' );
106 dpavlin 65 },
107 dpavlin 50 #
108 dpavlin 65 # GetParameterNamesResponse
109     #
110     'ParameterInfoStruct' => sub {
111     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
112 dpavlin 90 my $name = _tag($tag_hash, 'Name', '_content');
113     my $writable = _tag($tag_hash, 'Writable', '_content' );
114    
115     confess "need state" unless ( $state ); # don't remove!
116    
117     # XXX dragons ahead: convert name to tree rewriting it into perl
118    
119     my $s = $name;
120     $s =~ s#^#\$state->{ParameterInfo}->{'$name'}->{'#;
121     $s =~ s#(\w+)\.(\d+)\.(\w+)#$1'}->[$2]->{'$3#ig;
122     $s =~ s#(\w+)\.(\w+)#$1'}->{'$2#g;
123     $s =~ s#(\w+)$#$1'}->{'writable'} = $writable;#; #fix-vim
124     eval "$s";
125     confess "can't eval $s : $@" if ($@);
126    
127     #warn "## tree: $s ", dump( $state ), "\n";
128 dpavlin 65 },
129     #
130 dpavlin 50 # Fault
131     #
132 dpavlin 65 'Fault' => sub {
133 dpavlin 50 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
134     $state->{Fault} = {
135     FaultCode => _tag( $tag_hash, 'FaultCode', '_content' ),
136     FaultString => _tag( $tag_hash, 'FaultString', '_content' ),
137     };
138     warn "FAULT: ", $state->{Fault}->{FaultCode}, " ", $state->{Fault}->{FaultString}, "\n";
139     }
140 dpavlin 31 ]
141     );
142    
143     =head2 parse
144    
145     my $state = CWMP::Request->parse( "<soap>request</soap>" );
146    
147     =cut
148    
149     sub parse {
150     my $self = shift;
151    
152     my $xml = shift || confess "no xml?";
153    
154     $state = {};
155     $parser->parsestring( $xml );
156     return $state;
157     }
158 dpavlin 32
159     1;

  ViewVC Help
Powered by ViewVC 1.1.26