Revision 0bdaae6a
Von Johannes Grassler vor mehr als 1 Jahr hinzugefügt
SL/XMLInvoice.pm | ||
---|---|---|
3 | 3 |
use strict; |
4 | 4 |
use warnings; |
5 | 5 |
|
6 |
use Module::Load; |
|
7 |
|
|
6 | 8 |
use SL::Locale::String qw(t8); |
7 | 9 |
use XML::LibXML; |
8 | 10 |
|
... | ... | |
166 | 168 |
die "Children of $self must implement a metadata() method returning the bill's metadata as a hash."; |
167 | 169 |
} |
168 | 170 |
|
171 |
=item check_signature($dom) |
|
172 |
|
|
173 |
This static method takes a DOM object and returns 1 if this DOM object can be |
|
174 |
parsed by the child class in question, 0 otherwise. C<SL::XMLInvoice> uses this |
|
175 |
method to determine which child class to instantiate for a given document. All |
|
176 |
child classes must implement this method. |
|
177 |
|
|
178 |
=cut |
|
179 |
|
|
180 |
sub check_signature { |
|
181 |
my $self = shift; |
|
182 |
die "Children of $self must implement a check_signature() method returning 1 for supported XML, 0 for unsupported XML."; |
|
183 |
} |
|
184 |
|
|
185 |
=item supported() |
|
186 |
|
|
187 |
This static method returns an array of free-form strings describing XML invoice |
|
188 |
types parseable by the child class. C<SL::XMLInvoice> uses this method to |
|
189 |
output a list of supported XML invoice types if its constructor fails to find |
|
190 |
to find an appropriate child class to parse the given document with. All child |
|
191 |
classes must implement this method. |
|
192 |
|
|
193 |
=cut |
|
194 |
|
|
195 |
sub supported { |
|
196 |
my $self = shift; |
|
197 |
die "Children of $self must implement a supported() method returning a list of supported XML invoice types."; |
|
198 |
} |
|
199 |
|
|
200 |
|
|
169 | 201 |
=item items() |
170 | 202 |
|
171 | 203 |
This method returns an array of hashes containing line item metadata, such as |
... | ... | |
181 | 213 |
die "Children of $self must implement a item() method returning the bill's items as a hash."; |
182 | 214 |
} |
183 | 215 |
|
216 |
|
|
184 | 217 |
=item parse_xml() |
185 | 218 |
|
186 | 219 |
This method is only implemented in child classes of C<SL::XMLInvoice> and is |
... | ... | |
206 | 239 |
|
207 | 240 |
=over 4 |
208 | 241 |
|
209 |
=item _document_nodenames()
|
|
242 |
=item _document_modules()
|
|
210 | 243 |
|
211 |
This method is implemented in C<SL::XMLInvoice> only and returns a hash mapping
|
|
212 |
XML document root node name to a child class implementing a parser for it. If
|
|
213 |
you add any child classes for new XML document types you need to add them to
|
|
214 |
this hash and add a use statement to make it available from C<SL::XMLInvoice>.
|
|
244 |
This method is implemented in C<SL::XMLInvoice> only and returns a list of
|
|
245 |
child classes, each implementing an XML invoice parser. If you add any child
|
|
246 |
classes for new XML document types you need to add them to this list to make it
|
|
247 |
available from C<SL::XMLInvoice>. |
|
215 | 248 |
|
216 | 249 |
=cut |
217 | 250 |
|
218 |
sub _document_nodenames {
|
|
219 |
return {
|
|
220 |
'rsm:CrossIndustryInvoice' => 'SL::XMLInvoice::CrossIndustryInvoice',
|
|
221 |
'ubl:Invoice' => 'SL::XMLInvoice::UBL',
|
|
222 |
};
|
|
251 |
sub _document_modules {
|
|
252 |
return (
|
|
253 |
'SL::XMLInvoice::CrossIndustryInvoice', |
|
254 |
'SL::XMLInvoice::UBL', |
|
255 |
);
|
|
223 | 256 |
} |
224 | 257 |
|
225 | 258 |
=item _data_keys() |
... | ... | |
260 | 293 |
sub new { |
261 | 294 |
my ($self, $xml_data) = @_; |
262 | 295 |
my $type = undef; |
296 |
|
|
263 | 297 |
$self = {}; |
264 | 298 |
|
265 | 299 |
bless $self; |
... | ... | |
274 | 308 |
} |
275 | 309 |
|
276 | 310 |
# Determine parser class to use |
277 |
my $document_nodename = $self->{dom}->documentElement->nodeName; |
|
278 |
if ( ${$self->_document_nodenames}{$document_nodename} ) { |
|
279 |
$type = ${$self->_document_nodenames}{$document_nodename} |
|
280 |
} |
|
311 |
foreach my $module ( $self->_document_modules ) |
|
312 |
{ |
|
313 |
load $module; |
|
314 |
if ( $module->check_signature($self->{dom}) ) { |
|
315 |
$type = $module; |
|
316 |
last; |
|
317 |
} |
|
318 |
} |
|
281 | 319 |
|
282 | 320 |
unless ( $type ) { |
283 | 321 |
$self->{result} = RES_UNKNOWN_ROOT_NODE_TYPE; |
284 |
my $node_types = join(",", keys %{ $self->_document_nodenames }); |
|
285 |
$self->{message} = t8("Could not parse XML Invoice: unknown root node name (#1) (supported: (#2))", |
|
286 |
$document_nodename, |
|
287 |
$node_types, |
|
322 |
my @supported = (); |
|
323 |
|
|
324 |
foreach my $module ( $self->_document_modules ) { |
|
325 |
my @module_list = $module->supported(); |
|
326 |
push @supported, @module_list; |
|
327 |
} |
|
328 |
|
|
329 |
my $supported_types = join(",\n", @supported); |
|
330 |
$self->{message} = t8("Could not parse XML Invoice: unknown XML invoice type\nsupported: #1", |
|
331 |
$supported_types, |
|
288 | 332 |
); |
289 | 333 |
return $self; |
290 | 334 |
} |
291 | 335 |
|
292 |
eval {require $type}; # Load the parser class |
|
293 | 336 |
bless $self, $type; |
294 | 337 |
|
295 | 338 |
# Implementation sanity check for child classes: make sure they are aware of |
Auch abrufbar als: Unified diff
XMLInvoice: Formaterkennung umgebaut