/[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

Contents of /trunk/lib/WebPAC/Normalize/XML.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 38 - (show annotations)
Sat Nov 12 21:21:50 2005 UTC (18 years, 4 months ago) by dpavlin
File size: 2285 byte(s)
added ForceContent so that tags without attributes work, added strict checking

1 package WebPAC::Normalize::XML;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common WebPAC::Normalize/;
7 use XML::Simple;
8 use Data::Dumper;
9 use Text::Iconv;
10
11 =head1 NAME
12
13 WebPAC::Normalize::XML - apply XML normalisaton rules
14
15 =head1 VERSION
16
17 Version 0.01
18
19 =cut
20
21 our $VERSION = '0.01';
22
23 =head1 SYNOPSIS
24
25 This module uses C<conf/normalize/*.xml> files to perform normalisation
26 from input records
27
28 =cut
29
30 =head1 FUNCTIONS
31
32 =head2 open
33
34 Read normalisation rules defined using XML from C<conf/normalize/*.xml> and
35 parse it.
36
37 my $n = new WebPAC::Normalize::XML;
38 $n->open(
39 tag => 'isis',
40 xml_file => '/path/to/conf/normalize/isis.xml',
41 );
42
43 C<tag> defines tag to use within C<xml_file>
44
45 C<xml_file> defines path to normalize XML.
46
47 =cut
48
49 sub open {
50 my $self = shift;
51
52 my $arg = {@_};
53
54 my $log = $self->_get_logger();
55
56 foreach my $req (qw/tag xml_file/) {
57 $log->logconfess("need argument $req") unless $arg->{$req};
58 }
59
60 $self->{'tag'} = $arg->{'tag'};
61 my $xml_file = $arg->{'xml_file'};
62
63 $log->info("using $xml_file tag <",$self->{'tag'},">");
64
65 $log->logdie("normalisation xml file '$xml_file' doesn't exist!") if (! -e $xml_file);
66
67 $self->{'import_xml_file'} = $xml_file;
68
69 $self->{'import_xml'} = XMLin($xml_file,
70 ForceArray => [ $self->{'tag'}, $self->{'tags'}, 'config', 'format' ],
71 ForceContent => 1,
72 );
73
74 $log->debug("import xml is ",sub { Dumper($self->{'import_xml'}) });
75
76 return $self;
77 }
78
79
80 =head2 _x
81
82 Convert string from XML UTF-8 encoding to code page defined in C<xml_file>.
83
84 my $text = $n->_x('utf8 text');
85
86 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
89 =cut
90
91 sub _x {
92 my $self = shift;
93 my $utf8 = shift || return;
94
95 # create UTF-8 convertor for import_xml files
96 $self->{'utf2cp'} ||= Text::Iconv->new('UTF-8' ,$self->{'code_page'} || 'ISO-8859-2');
97
98 return $self->{'utf2cp'}->convert($utf8) ||
99 $self->_get_logger()->logwarn("can't convert '$utf8'");
100 }
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 1; # End of WebPAC::Normalize::XML

  ViewVC Help
Powered by ViewVC 1.1.26