/[cwmp]/google/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

Contents of /google/lib/CWMP/Request.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 92 - (show annotations)
Sat Jun 23 09:15:19 2007 UTC (17 years ago) by dpavlin
File size: 4437 byte(s)
new correct rewriter
1 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 our $state; # FIXME check this!
48
49 my $parser = XML::Rules->new(
50 # start_rules => [
51 # '^division_name,fax' => 'skip',
52 # ],
53 namespaces => {
54 '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 '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 'Inform' => sub {
74 $state->{_dispatch} = 'InformResponse'; # what reponse to call
75 },
76 'DeviceId' => sub {
77 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 'EventStruct' => sub {
86 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 'ParameterValueStruct' => sub {
94 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 'MethodList' => sub {
104 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
105 $state->{MethodList} = _tag( $tag_hash, 'string' );
106 },
107 #
108 # GetParameterNamesResponse
109 #
110 'ParameterInfoStruct' => sub {
111 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
112 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 warn "===> $name\n";
121 $s =~ s/^([^\.]+)/\$state->{ParameterInfo}->{'$1'}/;
122 warn "---> $s\n";
123
124 my $stat;
125 while ( $s =~ s/\.(\d+)/->[$1]/ ) {
126 $stat->{array}++;
127 warn "-\@-> $s\n";
128 }
129 while ( $s =~ s/\.([a-zA-Z0-9_]+)/->{'$1'}/ ) {
130 $stat->{scalar}++;
131 warn "-\$-> $s\n";
132 };
133 $s .= "->{'writable'} = $writable;";
134
135 warn "## $name\n## tree: $s\n## stat: ",dump( $stat ), "\n";
136
137 eval "$s";
138 confess "can't eval $s : $@" if ($@);
139
140 #warn "## state = dump( $state ), "\n";
141 },
142 #
143 # Fault
144 #
145 'Fault' => sub {
146 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
147 $state->{Fault} = {
148 FaultCode => _tag( $tag_hash, 'FaultCode', '_content' ),
149 FaultString => _tag( $tag_hash, 'FaultString', '_content' ),
150 };
151 warn "FAULT: ", $state->{Fault}->{FaultCode}, " ", $state->{Fault}->{FaultString}, "\n";
152 }
153 ]
154 );
155
156 =head2 parse
157
158 my $state = CWMP::Request->parse( "<soap>request</soap>" );
159
160 =cut
161
162 sub parse {
163 my $self = shift;
164
165 my $xml = shift || confess "no xml?";
166
167 $state = {};
168 $parser->parsestring( $xml );
169 return $state;
170 }
171
172 1;

  ViewVC Help
Powered by ViewVC 1.1.26