/[webpac2]/trunk/lib/WebPAC/Normalize/XML.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 /trunk/lib/WebPAC/Normalize/XML.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (hide annotations)
Sat Jul 16 23:56:14 2005 UTC (18 years, 9 months ago) by dpavlin
File size: 2267 byte(s)
data_source seems to work

1 dpavlin 12 package WebPAC::Normalize::XML;
2 dpavlin 8
3     use warnings;
4     use strict;
5    
6 dpavlin 13 use base qw/WebPAC::Common WebPAC::Normalize/;
7 dpavlin 10 use Storable;
8 dpavlin 12 use XML::Simple;
9     use Data::Dumper;
10 dpavlin 13 use Text::Iconv;
11 dpavlin 8
12     =head1 NAME
13    
14 dpavlin 12 WebPAC::Normalize::XML - apply XML normalisaton rules
15 dpavlin 8
16     =head1 VERSION
17    
18     Version 0.01
19    
20     =cut
21    
22     our $VERSION = '0.01';
23    
24     =head1 SYNOPSIS
25    
26     This module uses C<conf/normalize/*.xml> files to perform normalisation
27     from input records
28    
29     =cut
30    
31     =head1 FUNCTIONS
32    
33 dpavlin 13 =head2 open
34 dpavlin 8
35 dpavlin 12 Read normalisation rules defined using XML from C<conf/normalize/*.xml> and
36     parse it.
37 dpavlin 8
38 dpavlin 13 my $n = new WebPAC::Normalize::XML;
39     $n->open(
40 dpavlin 12 tag => 'isis',
41     xml_file => '/path/to/conf/normalize/isis.xml',
42 dpavlin 13 );
43 dpavlin 8
44 dpavlin 12 C<tag> defines tag to use within C<xml_file>
45    
46     C<xml_file> defines path to normalize XML.
47    
48 dpavlin 8 =cut
49    
50 dpavlin 13 sub open {
51     my $self = shift;
52 dpavlin 8
53 dpavlin 13 my $arg = {@_};
54 dpavlin 8
55     my $log = $self->_get_logger();
56    
57 dpavlin 12 foreach my $req (qw/tag xml_file/) {
58 dpavlin 13 $log->logconfess("need argument $req") unless $arg->{$req};
59 dpavlin 12 }
60 dpavlin 8
61 dpavlin 13 $self->{'tag'} = $arg->{'tag'};
62     my $xml_file = $arg->{'xml_file'};
63 dpavlin 8
64 dpavlin 12 $log->info("using $xml_file tag <",$self->{'tag'},">");
65 dpavlin 8
66 dpavlin 12 $log->logdie("normalisation xml file '$xml_file' doesn't exist!") if (! -e $xml_file);
67 dpavlin 8
68 dpavlin 12 $self->{'import_xml_file'} = $xml_file;
69 dpavlin 8
70 dpavlin 13 $self->{'import_xml'} = XMLin($xml_file,
71 dpavlin 12 ForceArray => [ $self->{'tag'}, $self->{'tags'}, 'config', 'format' ],
72 dpavlin 8 );
73    
74     $log->debug("import xml is ",sub { Dumper($self->{'import_xml'}) });
75    
76 dpavlin 12 return $self;
77 dpavlin 8 }
78    
79    
80 dpavlin 13 =head2 _x
81 dpavlin 8
82 dpavlin 13 Convert string from UTF-8 to code page defined in C<import_xml>.
83 dpavlin 8
84 dpavlin 13 my $text = $n->_x('utf8 text');
85 dpavlin 8
86 dpavlin 13 Default application code page is C<ISO-8859-2>. You will probably want to
87     change that when creating new instance of object based on this one.
88 dpavlin 8
89     =cut
90    
91 dpavlin 13 sub _x {
92 dpavlin 8 my $self = shift;
93 dpavlin 13 my $utf8 = shift || return;
94 dpavlin 8
95 dpavlin 13 # create UTF-8 convertor for import_xml files
96     $self->{'utf2cp'} ||= Text::Iconv->new('UTF-8' ,$self->{'code_page'} || 'ISO-8859-2');
97 dpavlin 8
98 dpavlin 13 return $self->{'utf2cp'}->convert($utf8) ||
99     $self->_get_logger()->logwarn("can't convert '$utf8'");
100 dpavlin 8 }
101    
102    
103     =head1 AUTHOR
104    
105     Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
106    
107     =head1 COPYRIGHT & LICENSE
108    
109     Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
110    
111     This program is free software; you can redistribute it and/or modify it
112     under the same terms as Perl itself.
113    
114     =cut
115    
116 dpavlin 12 1; # End of WebPAC::Normalize::XML

  ViewVC Help
Powered by ViewVC 1.1.26