File Coverage

blib/lib/Locale/Maketext/Extract/Plugin/XSL.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Locale::Maketext::Extract::Plugin::XSL;
2              
3 2     2   51458 use warnings;
  2         5  
  2         64  
4 2     2   11 use strict;
  2         3  
  2         108  
5              
6 2     2   14 use base qw(Locale::Maketext::Extract::Plugin::Base);
  2         8  
  2         2151  
7 2     2   1331 use Carp qw(croak);
  2         4  
  2         90  
8 2     2   2118 use Regexp::Common;
  2         31130  
  2         10  
9 2     2   129948 use XML::LibXML;
  0            
  0            
10             use XML::LibXML::XPathContext;
11              
12             =head1 NAME
13              
14             Locale::Maketext::Extract::Plugin::XSL - XSL file parser
15              
16             =head1 VERSION
17              
18             Version 0.4
19              
20             =cut
21              
22             our $VERSION = '0.4';
23              
24             =head1 SYNOPSIS
25              
26             my $ext = Locale::Maketext::Extract->new(
27             plugins => {'Locale::Maketext::Extract::Plugin::XSL' => '*'} );
28             $ext->extract_file('test.xsl');
29             $ext->compile();
30              
31             or perhaps more convenient:
32              
33             xgettext.pl -P Locale::Maketext::Extract::Plugin::XSL
34              
35              
36             =head1 DESCRIPTION
37              
38             Extracts strings to localise from XSL stylesheet files.
39              
40             Using Perl, custom localisation functions may be registered using
41             L->register_function().
42              
43             =head1 KNOWN FILE TYPES
44              
45             =over 4
46              
47             =item .xsl
48              
49             =item .xslt
50              
51             =back
52              
53             =head1 VALID FORMATS
54              
55             This plugin will check for localisation functions in all attribute values of the XSL document.
56             Valid localisation function names are:
57              
58             =over 4
59              
60             =item loc
61              
62             =item locfrag
63              
64             =item l
65              
66             =item lfrag
67              
68             =back
69              
70             Note that only the local-name for the function will be checked for. Namespace prefixes
71             will be ignored. I.e. and
72             will be treated the same.
73              
74             =head1 FUNCTIONS
75              
76             =head2 file_types
77              
78             File types this plugin should handle
79              
80             =cut
81             sub file_types {
82             return qw( xsl xslt );
83             }
84              
85              
86             =head2 extract
87              
88             Extraction function. Parses XSL document and adds localisation entries
89              
90             =cut
91             sub extract {
92             my ($self,$filecontent) = @_;
93              
94             my $parser = XML::LibXML->new();
95             $parser->load_ext_dtd(0);
96              
97             my $doc;
98             eval {
99             $doc = $parser->parse_string( $filecontent );
100             };
101             if ( $@ ) {
102             croak( "Could not parse input string: $@\n" );
103             }
104              
105             my $xc = XML::LibXML::XPathContext->new($doc);
106             $xc->registerNs('xsl', 'http://www.w3.org/1999/XSL/Transform');
107             my @nodes = $xc->findnodes('//@*[contains(.,":loc(") or contains(.,":l(") or contains(.,":locfrag(") or contains(.,":lfrag(")]');
108             foreach my $node (@nodes) {
109             $self->_parse_expression( $node->value() );
110             }
111              
112             return;
113             }
114              
115             =head2 _parse_expression
116              
117             Extract loc functions from XPATH expressions
118              
119             =cut
120             sub _parse_expression {
121             my ($self, $value) = @_;
122              
123             while (
124             $value =~ /$RE{balanced}{-begin => ':loc|:locfrag|:l|:lfrag'}{-keep}/gx
125             ) {
126             my $str = substr($1,1,length($1)-2); # remove wrapping parens
127              
128             # $Re{balanced} will match the outermost parens only, -begin does not work
129             # as (I) expected, so...
130             # If the expression includes more than one loc function, recurse
131             if (
132             $str =~ /\:l(?:oc)?(?:frag)?\(/x
133             ) {
134             $self->_parse_expression( $str );
135             }
136             else {
137             my @data;
138             while ($str =~ /$RE{quoted}{-keep}/gx ) {
139             push @data, substr($1,1,length($1)-2); # remove wrapping quotes
140             }
141             $self->add_entry(shift @data, undef, join(',', @data));
142             }
143             }
144              
145             return 1;
146             }
147              
148             =head1 SEE ALSO
149              
150             =over 4
151              
152             =item L
153              
154             for extracting translatable strings from common template
155             systems and perl source files.
156              
157             =item L
158              
159             =item L
160              
161             =item L
162              
163             =item L
164              
165             =item L
166              
167             =item L
168              
169             =item L
170              
171             =item L
172              
173             =item L
174              
175             =item L
176              
177             =back
178              
179              
180             =head1 AUTHOR
181              
182             Michael Kroell, C<< >>
183              
184             =head1 BUGS
185              
186             Please report any bugs or feature requests to C, or through
187             the web interface at L. I will be notified, and then you'll
188             automatically be notified of progress on your bug as I make changes.
189              
190              
191             =head1 SUPPORT
192              
193             You can find documentation for this module with the perldoc command.
194              
195             perldoc Locale::Maketext::Extract::Plugin::XSL
196              
197              
198             You can also look for information at:
199              
200             =over 4
201              
202             =item * RT: CPAN's request tracker
203              
204             L
205              
206             =item * AnnoCPAN: Annotated CPAN documentation
207              
208             L
209              
210             =item * CPAN Ratings
211              
212             L
213              
214             =item * Search CPAN
215              
216             L
217              
218             =back
219              
220              
221             =head1 ACKNOWLEDGEMENTS
222              
223              
224             =head1 COPYRIGHT
225              
226             Copyright 2008-2011 Michael Kroell, all rights reserved.
227              
228             =head1 LICENSE
229              
230             This program is free software; you can redistribute it and/or modify it
231             under the same terms as Perl itself.
232              
233              
234             =cut
235              
236             1; # End of Locale::Maketext::Extract::Plugin::XSL