API3/Compat/API2/Basic/TTF/XMLparse.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 89 CC 11 SCAR 45.8
CC
11
Cov
0%
read_xml
132.0
BEGIN
2.0
BEGIN
2.0
CRAP 97.3
1 #=======================================================================
2 # ____ ____ _____ _ ____ ___ ____
3 # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \
4 # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) |
5 # | __/| |_| | _| _ _ / ___ \| __/| | / __/
6 # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____|
7 #
8 # A Perl Module Chain to faciliate the Creation and Modification
9 # of High-Quality "Portable Document Format (PDF)" Files.
10 #
11 #=======================================================================
12 #
13 # THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW:
14 #
15 #
16 # Copyright Martin Hosken <Martin_Hosken@sil.org>
17 #
18 # No warranty or expression of effectiveness, least of all regarding
19 # anyone's safety, is implied in this software or documentation.
20 #
21 # This specific module is licensed under the Perl Artistic License.
22 #
23 #
24 # $Id: XMLparse.pm,v 2.0 2005/11/16 02:16:00 areibens Exp $
25 #
26 #=======================================================================
27 package PDF::API3::Compat::API2::Basic::TTF::XMLparse;
28
29 =head1 NAME
30
31 PDF::API3::Compat::API2::Basic::TTF::XMLparse - provides support for XML parsing. Requires Expat module XML::Parser::Expat
32
33 =head1 SYNOPSIS
34
35 use PDF::API3::Compat::API2::Basic::TTF::Font;
36 use PDF::API3::Compat::API2::Basic::TTF::XMLparse;
37
38 $f = PDF::API3::Compat::API2::Basic::TTF::Font->new;
39 read_xml($f, $ARGV[0]);
40 $f->out($ARGV[1]);
41
42 =head1 DESCRIPTION
43
44 This module contains the support routines for parsing XML and generating the
45 Truetype font structures as a result. The module has been separated from the rest
46 of the package in order to reduce the dependency that this would bring, of the
47 whole package on XML::Parser. This way, people without the XML::Parser can still
48 use the rest of the package.
49
50 The package interacts with another package through the use of a context containing
51 and element 'receiver' which is an object which can possibly receive one of the
52 following messages:
53
54 =over 4
55
56 =item XML_start
57
58 This message is called when an open tag occurs. It is called with the context,
59 tag name and the attributes. The return value has no meaning.
60
61 =item XML_end
62
63 This messages is called when a close tag occurs. It is called with the context,
64 tag name and attributes (held over from when the tag was opened). There are 3
65 possible return values from such a message:
66
67 =over 8
68
69 =item undef
70
71 This is the default return value indicating that default processing should
72 occur in which either the current element on the tree, or the text of this element
73 should be stored in the parent object.
74
75 =item $context
76
77 This magic value marks that the element should be deleted from the parent.
78 Nothing is stored in the parent. (This rather than '' is used to allow 0 returns.)
79
80 =item anything
81
82 Anything else is taken as the element content to be stored in the parent.
83
84 =back 4
85
86 =back 4
87
88 In addition, the context hash passed to these messages contains the following
89 keys:
90
91 =over 4
92
93 =item xml
94
95 This is the expat xml object. The context is also available as
96 $context->{'xml'}{' mycontext'}. But that is a long winded way of not saying much!
97
98 =item font
99
100 This is the base object that was passed in for XML parsing.
101
102 =item receiver
103
104 This holds the current receiver of parsing events. It may be set in associated
105 application to adjust which objects should receive messages when. It is also stored
106 in the parsing stack to ensure that where an object changes it during XML_start, that
107 that same object that received XML_start will receive the corresponding XML_end
108
109 =item stack
110
111 This is the parsing stack, used internally to hold the current receiver and attributes
112 for each element open, as a complete hierarchy back to the root element.
113
114 =item tree
115
116 This element contains the storage tree corresponding to the parent of each element
117 in the stack. The default action is to push undef onto this stack during XML_start
118 and then to resolve this, either in the associated application (by changing
119 $context->{'tree'}[-1]) or during XML_end of a child element, by which time we know
120 whether we are dealing with an array or a hash or what.
121
122 =item text
123
124 Character processing is to insert all the characters into the text element of the
125 context for available use later.
126
127 =back 4
128
129 =head1 METHODS
130
131 =cut
132
133 use XML::Parser::Expat;
134 use Exporter;
135
136 use strict;
137 use vars qw(@ISA @EXPORT);
138
139 @ISA = qw(Exporter);
140 @EXPORT = qw(read_xml);
141
142 sub read_xml
143 {
144 my ($font, $fname) = @_;
145
146 my ($xml) = XML::Parser::Expat->new;
147 my ($context) = {'xml' => $xml, 'font' => $font};
148
149 $xml->setHandlers('Start' => sub {
150 my ($x, $tag, %attrs) = @_;
151 my ($context) = $x->{' mycontext'};
152 my ($fn) = $context->{'receiver'}->can('XML_start');
153
154 push(@{$context->{'tree'}}, undef);
155 push(@{$context->{'stack'}}, [$context->{'receiver'}, {%attrs}]);
156 &{$fn}($context->{'receiver'}, $context, $tag, %attrs) if defined $fn;
157 },
158 'End' => sub {
159 my ($x, $tag) = @_;
160 my ($context) = $x->{' mycontext'};
161 my ($fn) = $context->{'receiver'}->can('XML_end');
162 my ($stackinfo) = pop(@{$context->{'stack'}});
163 my ($current, $res);
164
165 $context->{'receiver'} = $stackinfo->[0];
166 $context->{'text'} =~ s/^\s*(.*?)\s*$/$1/o;
167 $res = &{$fn}($context->{'receiver'}, $context, $tag, %{$stackinfo->[1]}) if defined $fn;
168 $current = pop(@{$context->{'tree'}});
169 $current = $context->{'text'} unless (defined $current);
170 $context->{'text'} = '';
171
172 if (defined $res)
173 {
174 return if ($res eq $context);
175 $current = $res;
176 }
177 return unless $#{$context->{'tree'}} >= 0;
178 if ($tag eq 'elem')
179 {
180 $context->{'tree'}[-1] = [] unless defined $context->{'tree'}[-1];
181 push (@{$context->{'tree'}[-1]}, $current);
182 } else
183 {
184 $context->{'tree'}[-1] = {} unless defined $context->{'tree'}[-1];
185 $context->{'tree'}[-1]{$tag} = $current;
186 }
187 },
188 'Char' => sub {
189 my ($x, $str) = @_;
190 $x->{' mycontext'}{'text'} .= $str;
191 });
192
193 $xml->{' mycontext'} = $context;
194
195 $context->{'receiver'} = $font;
196 if (ref $fname)
197 { return $xml->parse($fname); }
198 else
199 { return $xml->parsefile($fname); }
200 }
201
202