| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Format::XmlRpc.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: Datum parser: XML-RPC using RPC::XML | ||
| 6 | |||
| 7 | package DTA::CAB::Format::XmlRpc; | ||
| 8 | use DTA::CAB::Format::XmlCommon; | ||
| 9 | use DTA::CAB::Datum ':all'; | ||
| 10 | use RPC::XML; | ||
| 11 | use RPC::XML::Parser; | ||
| 12 | use IO::File; | ||
| 13 | use Carp; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | ##============================================================================== | ||
| 17 | ## Globals | ||
| 18 | ##============================================================================== | ||
| 19 | |||
| 20 | our @ISA = qw(DTA::CAB::Format::XmlCommon); | ||
| 21 | |||
| 22 | BEGIN { | ||
| 23 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, filenameRegex=>qr/\.(?i:xml(?:\-?)rpc|rpc(?:[\-\.]?)xml)$/); | ||
| 24 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>$_) foreach (qw(xml-rpc xml_rpc)); | ||
| 25 | } | ||
| 26 | |||
| 27 | ##============================================================================== | ||
| 28 | ## Constructors etc. | ||
| 29 | ##============================================================================== | ||
| 30 | |||
| 31 | ## $fmt = CLASS_OR_OBJ->new(%args) | ||
| 32 | ## + object structure: | ||
| 33 | ## ( | ||
| 34 | ## ##-- input | ||
| 35 | ## rxprs => $rpc_parser, ##-- RPC::XML::Parser object | ||
| 36 | ## rxdata => $rpc_data, ##-- structured data as decoded by RPC::XML::Parser | ||
| 37 | ## | ||
| 38 | ## ##-- output | ||
| 39 | ## docbuf => $doc, ##-- DTA::CAB::Document output buffer | ||
| 40 | ## xprs => $xml_parser, ##-- XML::LibXML parser object | ||
| 41 | ## level => $formatLevel, ##-- format level | ||
| 42 | ## encoding => $encoding, ##-- output encoding | ||
| 43 | ## | ||
| 44 | ## ##-- common | ||
| 45 | ## ) | ||
| 46 | sub new { | ||
| 47 | my $that = shift; | ||
| 48 | my $fmt = bless({ | ||
| 49 | ##-- input | ||
| 50 | rxprs => RPC::XML::Parser->new(), | ||
| 51 | #rxdata => undef, | ||
| 52 | |||
| 53 | ##-- output | ||
| 54 | docbuf => DTA::CAB::Document->new(), | ||
| 55 | #encoding => 'UTF-8', | ||
| 56 | level => 0, | ||
| 57 | #xprs => XML::LibXML->new, (inherited from XmlCommon) | ||
| 58 | #xdoc => undef, (inherited from XmlCommon) | ||
| 59 | |||
| 60 | ##-- common | ||
| 61 | #(none) | ||
| 62 | |||
| 63 | ##-- user args | ||
| 64 | @_ | ||
| 65 | }, ref($that)||$that); | ||
| 66 | return $fmt; | ||
| 67 | } | ||
| 68 | |||
| 69 | ##============================================================================== | ||
| 70 | ## Methods: Persistence | ||
| 71 | ##============================================================================== | ||
| 72 | |||
| 73 | ## @keys = $class_or_obj->noSaveKeys() | ||
| 74 | ## + returns list of keys not to be saved | ||
| 75 | ## + default just returns empty list | ||
| 76 | sub noSaveKeys { | ||
| 77 | return ($_[0]->SUPER::noSaveKeys, qw(rxprs rxdata docbuf xprs)); | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | ##============================================================================== | ||
| 82 | ## Methods: Input | ||
| 83 | ##============================================================================== | ||
| 84 | |||
| 85 | ##-------------------------------------------------------------- | ||
| 86 | ## Methods: Input: Input selection | ||
| 87 | |||
| 88 | ## $fmt = $fmt->close() | ||
| 89 | ## + close current input source, if any | ||
| 90 | sub close { | ||
| 91 | delete($_[0]{rxdata}); | ||
| 92 | return $_[0]; | ||
| 93 | } | ||
| 94 | |||
| 95 | ## $fmt = $fmt->fromString(\$string) | ||
| 96 | sub fromString { | ||
| 97 | my $fmt = shift; | ||
| 98 | $fmt->close; | ||
| 99 | $fmt->{rxdata} = $fmt->{rxprs}->parse(ref($_[0]) ? ${$_[0]} : $_[0]); | ||
| 100 | return $fmt->checkData(); | ||
| 101 | } | ||
| 102 | |||
| 103 | ## $fmt = $fmt->fromFile($filename_or_handle) | ||
| 104 | sub fromFile { | ||
| 105 | return $_[0]->DTA::CAB::Format::fromFile(@_[1..$#_]); | ||
| 106 | } | ||
| 107 | |||
| 108 | ## $fmt = $fmt->fromFh($handle) | ||
| 109 | sub fromFh { | ||
| 110 | my ($fmt,$fh) = @_; | ||
| 111 | $fmt->close; | ||
| 112 | $fmt->{rxdata} = $fmt->{rxprs}->parse($fh); | ||
| 113 | return $fmt->checkData(); | ||
| 114 | } | ||
| 115 | |||
| 116 | ## $fmt_or_undef = $fmt->checkData() | ||
| 117 | ## + check for valid parse | ||
| 118 | sub checkData { | ||
| 119 | my $fmt = shift; | ||
| 120 | if (!defined($fmt->{rxdata})) { | ||
| 121 | $fmt->logwarn("checkData(): no parse data {rxdata} defined"); | ||
| 122 | return undef; | ||
| 123 | } | ||
| 124 | elsif (!ref($fmt->{rxdata})) { | ||
| 125 | $fmt->logwarn("checkData(): parse error: $fmt->{rxdata}"); | ||
| 126 | return undef; | ||
| 127 | } | ||
| 128 | elsif ($fmt->{rxdata}->is_fault) { | ||
| 129 | $fmt->logwarn("checkData(): fault: (".$fmt->{rxdata}->code.") ".$fmt->{rxdata}->string); | ||
| 130 | return undef; | ||
| 131 | } | ||
| 132 | return $fmt; | ||
| 133 | } | ||
| 134 | |||
| 135 | ##-------------------------------------------------------------- | ||
| 136 | ## Methods: Input: Generic API | ||
| 137 | |||
| 138 | ## $doc = $fmt->parseDocument(); | ||
| 139 | ## + Override: parse document from currently selected input source. | ||
| 140 | sub parseDocument { | ||
| 141 | my $fmt = shift; | ||
| 142 | if (!defined($fmt->{rxdata})) { | ||
| 143 | $fmt->logconfess("parseDocument(): no source data {rxdata} defined!"); | ||
| 144 | return undef; | ||
| 145 | } | ||
| 146 | my $doc = $fmt->forceDocument($fmt->{rxdata}->value); | ||
| 147 | |||
| 148 | ##-- force doc refs, deep | ||
| 149 | if (UNIVERSAL::isa($doc,'DTA::CAB::Document')) { | ||
| 150 | @{$doc->{body}} = map { toSentence($_) } @{$doc->{body}}; | ||
| 151 | foreach (@{$doc->{body}}) { | ||
| 152 | @{$_->{tokens}} = map { toToken($_) } @{$_->{tokens}}; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | return $doc; | ||
| 157 | } | ||
| 158 | |||
| 159 | ##============================================================================== | ||
| 160 | ## Methods: Output | ||
| 161 | ##============================================================================== | ||
| 162 | |||
| 163 | |||
| 164 | ##-------------------------------------------------------------- | ||
| 165 | ## Methods: Output: output selection | ||
| 166 | |||
| 167 | ## $fmt = $fmt->flush() | ||
| 168 | ## + flush accumulated output | ||
| 169 | sub flush { | ||
| 170 | my $fmt = shift; | ||
| 171 | if (defined($fmt->{docbuf}) && defined($fmt->{output})) { | ||
| 172 | my $rpcobj = RPC::XML::smart_encode( $fmt->{docbuf} ); | ||
| 173 | $fmt->{xdoc} = $fmt->rpcXmlDocument($rpcobj); | ||
| 174 | $fmt->{docbuf} = DTA::CAB::Document->new(); | ||
| 175 | } | ||
| 176 | return $fmt->SUPER::flush(); | ||
| 177 | } | ||
| 178 | |||
| 179 | ##-------------------------------------------------------------- | ||
| 180 | ## Methods: Output: Local | ||
| 181 | |||
| 182 | ## $xmldoc = $fmt->rpcXmlDocument($rpcobj_or_string) | ||
| 183 | sub rpcXmlDocument { | ||
| 184 | return $_[0]->xmlparser->parse_string( ref($_[1]) ? $_[1]->as_string : $_[1] ); | ||
| 185 | } | ||
| 186 | |||
| 187 | ##-------------------------------------------------------------- | ||
| 188 | ## Methods: Output: Recommended API | ||
| 189 | |||
| 190 | ## $fmt = $fmt->putToken($tok) | ||
| 191 | sub putToken { $_[0]->putTokenRaw(Storable::dclone($_[1])); } | ||
| 192 | sub putTokenRaw { | ||
| 193 | my ($fmt,$tok) = @_; | ||
| 194 | my $buf = $fmt->{docbuf}; | ||
| 195 | if (@{$buf->{body}}) { | ||
| 196 | push(@{$buf->{body}[$#{$buf->{body}}]}, $tok); | ||
| 197 | } else { | ||
| 198 | push(@{$buf->{body}}, toSentence([$tok])); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | ## $fmt = $fmt->putSentence($sent) | ||
| 203 | sub putSentence { $_[0]->putSentenceRaw(Storable::dclone($_[1])); } | ||
| 204 | sub putSentenceRaw { | ||
| 205 | my ($fmt,$sent) = @_; | ||
| 206 | push(@{$fmt->{docbuf}{body}}, $sent); | ||
| 207 | return $fmt; | ||
| 208 | } | ||
| 209 | |||
| 210 | ##-------------------------------------------------------------- | ||
| 211 | ## Methods: Output: Required API | ||
| 212 | |||
| 213 | ## $fmt = $fmt->putDocument($doc) | ||
| 214 | our %IGNORE_DOC_ATTRS = (textbufr=>1,teibufr=>1); | ||
| 215 | sub putDocument { $_[0]->putDocumentRaw(Storable::dclone($_[1])); } | ||
| 216 | sub putDocumentRaw { | ||
| 217 | my ($fmt,$doc) = @_; | ||
| 218 | my $buf = $fmt->{docbuf}; | ||
| 219 | if (scalar(keys(%$buf))==1 && !@{$buf->{body}}) { | ||
| 220 | ##-- steal $doc | ||
| 221 | $fmt->{docbuf} = bless({ | ||
| 222 | map {($_=>$doc->{$_})} | ||
| 223 | grep {!exists $IGNORE_DOC_ATTRS{$_}} ##... but ignore some attributes | ||
| 224 | keys %$doc | ||
| 225 | }, ref($doc)||$doc); | ||
| 226 | } else { | ||
| 227 | ##-- append $doc->{body} onto $buf->{body} | ||
| 228 | push(@{$buf->{body}}, @{$doc->{body}}); | ||
| 229 | foreach (grep {$_ ne 'body' && !exists $IGNORE_DOC_ATTRS{$_}} keys(%$doc)) { | ||
| 230 | $buf->{$_} = $doc->{$_}; ##-- clobber existing keys | ||
| 231 | } | ||
| 232 | } | ||
| 233 | return $fmt; | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | 1; ##-- be happy | ||
| 238 | |||
| 239 | __END__ |