/[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 62 - (hide annotations)
Wed Jun 20 21:37:52 2007 UTC (16 years, 11 months ago) by dpavlin
Original Path: google/lib/CWMP/Request.pm
File size: 3515 byte(s)
added namespaces to shut up XML::Rules
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     my $state;
48    
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     $state->{_dispatch} = 'Inform'; # what reponse to call
75     },
76 dpavlin 31 qr/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     qr/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     qr/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     qr/MethodList/ => sub {
104     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
105     $state->{MethodList} = _tag( $tag_hash, 'string' );
106     },
107 dpavlin 50 #
108     # Fault
109     #
110     qr/^Fault$/ => sub {
111     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
112     $state->{Fault} = {
113     FaultCode => _tag( $tag_hash, 'FaultCode', '_content' ),
114     FaultString => _tag( $tag_hash, 'FaultString', '_content' ),
115     };
116     warn "FAULT: ", $state->{Fault}->{FaultCode}, " ", $state->{Fault}->{FaultString}, "\n";
117     }
118 dpavlin 31 ]
119     );
120    
121     =head2 parse
122    
123     my $state = CWMP::Request->parse( "<soap>request</soap>" );
124    
125     =cut
126    
127     sub parse {
128     my $self = shift;
129    
130     my $xml = shift || confess "no xml?";
131    
132     $state = {};
133     $parser->parsestring( $xml );
134     return $state;
135     }
136 dpavlin 32
137     1;

  ViewVC Help
Powered by ViewVC 1.1.26