| 1 | # Copyrights 2009-2013 by [Mark Overmeer]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.01. | ||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | package XML::Compile::RPC; | ||
| 9 | use vars '$VERSION'; | ||
| 10 | $VERSION = '0.17'; | ||
| 11 | |||
| 12 | use base 'XML::Compile::Cache'; | ||
| 13 | |||
| 14 | use Log::Report 'xml-compile-rpc', syntax => 'SHORT'; | ||
| 15 | |||
| 16 | |||
| 17 | sub init($) | ||
| 18 | { my ($self, $args) = @_; | ||
| 19 | |||
| 20 | push @{$args->{opts_rw}}, sloppy_floats => 1, sloppy_integers => 1 | ||
| 21 | , mixed_elements => 'STRUCTURAL'; | ||
| 22 | |||
| 23 | push @{$args->{opts_readers}} | ||
| 24 | , hooks => [ {type => 'ValueType', replace => \&_rewrite_string} | ||
| 25 | , {type => 'ISO8601', replace => \&_reader_rewrite_date} ]; | ||
| 26 | |||
| 27 | push @{$args->{opts_writers}} | ||
| 28 | , hook => {type => 'ISO8601', before => \&_writer_rewrite_date}; | ||
| 29 | |||
| 30 | $self->SUPER::init($args); | ||
| 31 | |||
| 32 | (my $xsd = __FILE__) =~ s,\.pm$,/xml-rpc.xsd,; | ||
| 33 | $self->importDefinitions($xsd); | ||
| 34 | |||
| 35 | # only declared methods are accepted by the Cache | ||
| 36 | $self->declare(WRITER => 'methodCall'); | ||
| 37 | $self->declare(READER => 'methodResponse'); | ||
| 38 | $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _rewrite_string($$$$$) | ||
| 42 | { my ($element, $reader, $path, $type, $replaced) = @_; | ||
| 43 | # panic $element->childNodes; | ||
| 44 | |||
| 45 | (grep $_->isa('XML::LibXML::Element'), $element->childNodes) | ||
| 46 | ? $replaced->($element) | ||
| 47 | : (value => {string => $element->textContent}); | ||
| 48 | } | ||
| 49 | |||
| 50 | # xsd:dateTime requires - and : between the components | ||
| 51 | sub _iso8601_to_dateTime($) | ||
| 52 | { my $s = shift; | ||
| 53 | $s =~ s/^([12][0-9][0-9][0-9])-?([01][0-9])-?([0-3][0-9])T/$1-$2-$3T/; | ||
| 54 | $s =~ s/T([012][0-9]):?([0-5][0-9]):?([0-6][0-9])/T$1:$2:$3/; | ||
| 55 | $s; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _writer_rewrite_date | ||
| 59 | { my ($doc, $string, $path) = @_; | ||
| 60 | _iso8601_to_dateTime $string; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _reader_rewrite_date | ||
| 64 | { my ($element, $reader, $path, $type, $replaced) = @_; | ||
| 65 | my $schema_time = _iso8601_to_dateTime $element->textContent; | ||
| 66 | # $schema_time should get validated... | ||
| 67 | ('dateTime.iso8601' => $schema_time); | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; |