188 |
|
|
189 |
push @{$self->{'data'}->{$mfn}->{$k}}, $val; |
push @{$self->{'data'}->{$mfn}->{$k}}, $val; |
190 |
} |
} |
191 |
|
} else { |
192 |
|
push @{$self->{'data'}->{$mfn}->{'000'}}, $mfn; |
193 |
} |
} |
194 |
|
|
195 |
} |
} |
254 |
|
|
255 |
$self->{'tag'} = $type2tag{$type_base}; |
$self->{'tag'} = $type2tag{$type_base}; |
256 |
|
|
257 |
$log->debug("using type '",$self->{'type'},"' tag <",$self->{'tag'},">") if ($self->{'debug'}); |
$log->info("using type '",$self->{'type'},"' tag <",$self->{'tag'},">"); |
258 |
|
|
259 |
my $f = "./import_xml/".$self->{'type'}.".xml"; |
my $f = "./import_xml/".$self->{'type'}.".xml"; |
260 |
$log->logconfess("import_xml file '$f' doesn't exist!") if (! -e "$f"); |
$log->logconfess("import_xml file '$f' doesn't exist!") if (! -e "$f"); |
261 |
|
|
262 |
$log->debug("reading '$f'") if ($self->{'debug'}); |
$log->info("reading '$f'"); |
263 |
|
|
264 |
|
$self->{'import_xml_file'} = $f; |
265 |
|
|
266 |
$self->{'import_xml'} = XMLin($f, |
$self->{'import_xml'} = XMLin($f, |
267 |
ForceArray => [ $self->{'tag'}, 'config', 'format' ], |
ForceArray => [ $self->{'tag'}, 'config', 'format' ], |
268 |
); |
); |
269 |
|
|
270 |
|
$log->debug("import xml is ",sub { Dumper($self->{'import_xml'}) }); |
271 |
|
|
272 |
} |
} |
273 |
|
|
274 |
=head2 create_lookup |
=head2 create_lookup |
487 |
|
|
488 |
$i = 0 if (! $i); |
$i = 0 if (! $i); |
489 |
|
|
490 |
my $format = $self->{'utf2cp'}->convert($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); |
my $format = $self->_x($format_utf8) || $log->logconfess("can't convert '$format_utf8' from UTF-8 to ",$self->{'code_page'}); |
491 |
|
|
492 |
my @out; |
my @out; |
493 |
|
|
608 |
|
|
609 |
my @ds = $webpac->data_structure($rec); |
my @ds = $webpac->data_structure($rec); |
610 |
|
|
611 |
|
This method will also set C<$webpac->{'currnet_filename'}> if there is |
612 |
|
<filename> tag in C<import_xml>. |
613 |
|
|
614 |
=cut |
=cut |
615 |
|
|
616 |
sub data_structure { |
sub data_structure { |
621 |
my $rec = shift; |
my $rec = shift; |
622 |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
623 |
|
|
624 |
|
undef $self->{'currnet_filename'}; |
625 |
|
|
626 |
my @sorted_tags; |
my @sorted_tags; |
627 |
if ($self->{tags_by_order}) { |
if ($self->{tags_by_order}) { |
628 |
@sorted_tags = @{$self->{tags_by_order}}; |
@sorted_tags = @{$self->{tags_by_order}}; |
654 |
} |
} |
655 |
next if (! @v); |
next if (! @v); |
656 |
|
|
657 |
|
# use format? |
658 |
|
if ($tag->{'format_name'}) { |
659 |
|
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
660 |
|
} |
661 |
|
|
662 |
# does tag have type? |
# does tag have type? |
663 |
if ($tag->{'type'}) { |
if ($tag->{'type'}) { |
664 |
push @{$row->{$tag->{'type'}}}, @v; |
push @{$row->{$tag->{'type'}}}, @v; |
667 |
push @{$row->{'swish'}}, @v; |
push @{$row->{'swish'}}, @v; |
668 |
} |
} |
669 |
|
|
670 |
|
if ($field eq 'filename') { |
671 |
|
$self->{'current_filename'} = join('',@v); |
672 |
|
$log->debug("filename: ",$self->{'current_filename'}); |
673 |
|
} |
674 |
|
|
675 |
} |
} |
676 |
|
|
677 |
if ($row) { |
if ($row) { |
678 |
$row->{'tag'} = $field; |
$row->{'tag'} = $field; |
679 |
|
|
680 |
|
# TODO: name_sigular, name_plural |
681 |
|
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
682 |
|
$row->{'name'} = $name ? $self->_x($name) : $field; |
683 |
|
|
684 |
push @ds, $row; |
push @ds, $row; |
685 |
|
|
686 |
$log->debug("row $field: ",sub { Dumper($row) }); |
$log->debug("row $field: ",sub { Dumper($row) }); |
687 |
} |
} |
688 |
|
|
721 |
return $out; |
return $out; |
722 |
} |
} |
723 |
|
|
724 |
|
=head2 apply_format |
725 |
|
|
726 |
|
Apply format specified in tag with C<format_name="name"> and |
727 |
|
C<format_delimiter=";;">. |
728 |
|
|
729 |
|
my $text = $webpac->apply_format($format_name,$format_delimiter,$data); |
730 |
|
|
731 |
|
Formats can contain C<lookup{...}> if you need them. |
732 |
|
|
733 |
|
=cut |
734 |
|
|
735 |
|
sub apply_format { |
736 |
|
my $self = shift; |
737 |
|
|
738 |
|
my ($name,$delimiter,$data) = @_; |
739 |
|
|
740 |
|
my $log = $self->_get_logger(); |
741 |
|
|
742 |
|
if (! $self->{'import_xml'}->{'format'}->{$name}) { |
743 |
|
$log->warn("<format name=\"$name\"> is not defined in ",$self->{'import_xml_file'}); |
744 |
|
return $data; |
745 |
|
} |
746 |
|
|
747 |
|
$log->warn("no delimiter for format $name") if (! $delimiter); |
748 |
|
|
749 |
|
my $format = $self->_x($self->{'import_xml'}->{'format'}->{$name}->{'content'}) || $log->logdie("can't find format '$name'"); |
750 |
|
|
751 |
|
my @data = split(/\Q$delimiter\E/, $data); |
752 |
|
|
753 |
|
my $out = sprintf($format, @data); |
754 |
|
$log->debug("using format $name [$format] on $data to produce: $out"); |
755 |
|
|
756 |
|
if ($out =~ m/$LOOKUP_REGEX/o) { |
757 |
|
return $self->lookup($out); |
758 |
|
} else { |
759 |
|
return $out; |
760 |
|
} |
761 |
|
|
762 |
|
} |
763 |
|
|
764 |
|
|
765 |
# |
# |
766 |
# |
# |
767 |
# |
# |
814 |
return $va <=> $vb; |
return $va <=> $vb; |
815 |
} |
} |
816 |
|
|
817 |
|
=head2 _get_logger |
818 |
|
|
819 |
|
Get C<Log::Log4perl> object with a twist: domains are defined for each |
820 |
|
method |
821 |
|
|
822 |
|
my $log = $webpac->_get_logger(); |
823 |
|
|
824 |
|
=cut |
825 |
|
|
826 |
sub _get_logger { |
sub _get_logger { |
827 |
my $self = shift; |
my $self = shift; |
828 |
|
|
829 |
my @c = caller(1); |
my $name = (caller(1))[3] || caller; |
830 |
return get_logger($c[3]); |
return get_logger($name); |
831 |
|
} |
832 |
|
|
833 |
|
=head2 _x |
834 |
|
|
835 |
|
Convert string from UTF-8 to code page defined in C<import_xml>. |
836 |
|
|
837 |
|
my $text = $webpac->_x('utf8 text'); |
838 |
|
|
839 |
|
=cut |
840 |
|
|
841 |
|
sub _x { |
842 |
|
my $self = shift; |
843 |
|
my $utf8 = shift || return; |
844 |
|
|
845 |
|
return $self->{'utf2cp'}->convert($utf8) || |
846 |
|
$self->_get_logger()->logwarn("can't convert '$utf8'"); |
847 |
} |
} |
848 |
|
|
849 |
# |
# |