blib/lib/XML/Twig.pm

filter: Statementstmt 89.4% 34 / 38 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 12 / 15 Podpod n/a totaltotal 86.7% 46 / 53 CC 1 SCAR 1.4
CC
1
Cov
89%
PCDATA
2.0
CDATA
2.0
BEGIN
1.3
CRAP 1.2
« blib/lib/XML/speedup.pl ↑ Summary blib/lib/XML/Twig/XPath.pm »
1 164 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 164 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
3  
4  
5 # This is created in the caller's space
6 # I realize (now!) that it's not clean, but it's been there for 10+ years...
7 BEGIN
8 0 ▶{ sub ::PCDATA { '#PCDATA' } ## no critic (Subroutines::ProhibitNestedSubs);
Subroutine PCDATA: not called CC=1 SCAR=6.9
9 0 ▶ sub ::CDATA { '#CDATA' } ## no critic (Subroutines::ProhibitNestedSubs);
Subroutine CDATA: not called CC=1 SCAR=6.9
10 }
11  
12 164 ▶use UNIVERSAL();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 ## if a sub returns a scalar, it better not bloody disappear in list context
15 ## no critic (Subroutines::ProhibitExplicitReturnUndef);
16  
17 my $perl_version;
18 my $parser_version;
19  
20 ######################################################################
21 package XML::Twig;
22 ######################################################################
23  
24 require 5.010;
25  
26 164 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 164 ▶use Scalar::Util qw(weaken looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 164 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 164 ▶use vars qw($VERSION @ISA %valid_option);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 164 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 164 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 164 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 164 ▶use Config; # to get perl's path name in case we need to know if perlio is available
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 *isa= *UNIVERSAL::isa;
39  
40 # flag set to true if the version of expat seems to be 1.95.2, which has annoying bugs
41 # wrt doctype handling. This is global for performance reasons.
42 my $expat_1_95_2=0;
43  
44 # a slight non-xml mod: # is allowed as a first character
45 my $REG_TAG_FIRST_LETTER;
46 $REG_TAG_FIRST_LETTER= q{(?:[[:alpha:]:#_])};
47  
48 my $REG_TAG_LETTER= q{(?:[\w_.-]*)};
49  
50 # a simple name (no colon)
51 my $REG_NAME_TOKEN= qq{(?:$REG_TAG_FIRST_LETTER$REG_TAG_LETTER*)};
52  
53 # a tag name, possibly including namespace
54 my $REG_NAME= qq{(?:(?:$REG_NAME_TOKEN:)?$REG_NAME_TOKEN)};
55  
56 # tag name (leading # allowed)
57 my $REG_TAG_NAME=$REG_NAME;
58  
59 # name or wildcard (* or '') (leading # allowed)
60 my $REG_NAME_W = qq{(?:$REG_NAME|[*])};
61  
62 # class and ids are deliberately permissive
63 my $REG_NTOKEN_FIRST_LETTER;
64 $REG_NTOKEN_FIRST_LETTER= q{(?:[[:alpha:]:_])};
65  
66 my $REG_NTOKEN_LETTER= q{(?:[\w_:.-]*)};
67  
68 my $REG_NTOKEN= qq{(?:$REG_NTOKEN_FIRST_LETTER$REG_NTOKEN_LETTER*)};
69 my $REG_CLASS = $REG_NTOKEN;
70 my $REG_ID = $REG_NTOKEN;
71  
72 # allow <tag> #<tag> (private elt) * <tag>.<class> *.<class> <tag>#<id> *#<id>
73 my $REG_TAG_PART= qq{(?:$REG_NAME_W(?:(?:[.]$REG_CLASS)+|[#]$REG_ID)?|(?:[.]$REG_CLASS)+)};
74  
75 my $REG_REGEXP = q{(?:/(?:[^\\/]|\\.)*/[eimsox]*)}; # regexp
76 my $REG_MATCH = q{[!=]~}; # match (or not)
77 my $REG_STRING = q{(?:"(?:[^\\"]|\\.)*"|'(?:[^\\']|\\.)*')}; # string (simple or double quoted)
78 my $REG_NUMBER = q{(?:\d+(?:\.\d*)?|\.\d+)}; # number
79 my $REG_VALUE = qq{(?:$REG_STRING|$REG_NUMBER)}; # value
80 my $REG_OP = q{==|!=|>|<|>=|<=|eq|ne|lt|gt|le|ge|=}; # op
81 my $REG_FUNCTION = q{(?:string|text)\(\s*\)};
82 my $REG_STRING_ARG = qq{(?:string|text)\\(\\s*$REG_NAME_W\\s*\\)};
83 my $REG_COMP = q{(?:>=|<=|!=|<|>|=)};
84  
85 my $REG_TAG_IN_PREDICATE= $REG_NAME_W . q{(?=\s*(?i:and\b|or\b|\]|$))};
86  
87 # keys in the context stack, chosen not to interfere with att names, even private (#-prefixed) ones
88 my $ST_TAG = '##tag';
89 my $ST_ELT = '##elt';
90 my $ST_NS = '##ns' ;
91  
92 # used in the handler trigger code
93 my $REG_NAKED_PREDICATE= qq{((?:"[^"]*"|'[^']*'|$REG_STRING_ARG|$REG_FUNCTION|\@$REG_NAME_W|$REG_MATCH\\s*$REG_REGEXP|[\\s\\d><=!()+.-]|(?i:and)|(?i:or)|$REG_TAG_IN_PREDICATE)*)};
94 my $REG_PREDICATE= qq{\\[$REG_NAKED_PREDICATE\\]};
95  
96 # not all axis, only supported ones (in get_xpath)
97 my @supported_axis= ( 'ancestor', 'ancestor-or-self', 'child', 'descendant', 'descendant-or-self',
98 'following', 'following-sibling', 'parent', 'preceding', 'preceding-sibling', 'self'
99 );
100 my $REG_AXIS = "(?:" . join( '|', @supported_axis) .")";
101  
102 # only used in the "xpath"engine (for get_xpath/findnodes) for now
103 my $REG_PREDICATE_ALT = qr{\[(?:(?:string\(\s*\)|\@$REG_TAG_NAME)\s*$REG_MATCH\s*$REG_REGEXP\s*|[^\]]*)\]};
104  
105 # used to convert XPath tests on strings to the perl equivalent
106 my %PERL_ALPHA_TEST= ( '=' => ' eq ', '!=' => ' ne ', '>' => ' gt ', '>=' => ' ge ', '<' => ' lt ', '<=' => ' le ');
107  
108 my( $FB_HTMLCREF, $FB_XMLCREF); # Encode flags
109  
110 my $NO_WARNINGS= 'no warnings';
111  
112 # default namespaces, both ways
113 my %DEFAULT_NS= ( xml => "http://www.w3.org/XML/1998/namespace",
114 xmlns => "http://www.w3.org/2000/xmlns/",
115 );
116 my %DEFAULT_URI2NS= map { $DEFAULT_NS{$_} => $_ } keys %DEFAULT_NS;
117  
118 # constants
119 my( $PCDATA, $CDATA, $PI, $COMMENT, $ENT, $ELT, $NOTATION, $TEXT, $ASIS, $EMPTY, $BUFSIZE);
120  
121 # used when an HTML doc only has a PUBLIC declaration, to generate the SYSTEM one
122 # this should really be done by HTML::TreeBuilder, but as of HTML::TreeBuilder 4.2 it isn't
123 # the various declarations are taken from http://en.wikipedia.org/wiki/Document_Type_Declaration
124 my %HTML_DECL= ( "-//W3C//DTD HTML 4.0 Transitional//EN" => "http://www.w3.org/TR/REC-html40/loose.dtd",
125 "-//W3C//DTD HTML 4.01//EN" => "http://www.w3.org/TR/html4/strict.dtd",
126 "-//W3C//DTD HTML 4.01 Transitional//EN" => "http://www.w3.org/TR/html4/loose.dtd",
127 "-//W3C//DTD HTML 4.01 Frameset//EN" => "http://www.w3.org/TR/html4/frameset.dtd",
128 "-//W3C//DTD XHTML 1.0 Strict//EN" => "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd",
129 "-//W3C//DTD XHTML 1.0 Transitional//EN" => "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
130 "-//W3C//DTD XHTML 1.0 Frameset//EN" => "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd",
131 "-//W3C//DTD XHTML 1.1//EN" => "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd",
132 "-//W3C//DTD XHTML Basic 1.0//EN" => "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd",
133 "-//W3C//DTD XHTML Basic 1.1//EN" => "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd",
134 "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" => "http://www.wapforum.org/DTD/xhtml-mobile10.dtd",
135 "-//WAPFORUM//DTD XHTML Mobile 1.1//EN" => "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile11.dtd",
136 "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" => "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd",
137 "-//W3C//DTD XHTML+RDFa 1.0//EN" => "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd",
138 );
139  
140 my $DEFAULT_HTML_TYPE= "-//W3C//DTD HTML 4.0 Transitional//EN";
141  
142 my $SEP= qr/\s*(?:$|\|)/;
143  
144 my $DEBUG_HANDLER;
145 BEGIN
146 {
147 $VERSION = '3.55';
148  
149 0 ▶use XML::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
150 my $needVersion = '2.23';
151 ($parser_version= $XML::Parser::VERSION)=~ s{_\d+}{}; # remove _<n> from version so numeric tests do not warn
152 croak "need at least XML::Parser version $needVersion" unless $parser_version >= $needVersion;
153  
154 ($perl_version= $])=~ s{_\d+}{};
155  
156 eval "use Encode qw( :all)"; ## no critic ProhibitStringyEval
157 $FB_XMLCREF = 0x0400; # Encode::FB_XMLCREF;
158 $FB_HTMLCREF = 0x0200; # Encode::FB_HTMLCREF;
159  
160 # used to store the gi's
161 # should be set for each twig really, at least when there are several
162 # the init ensures that special gi's are always the same
163  
164 # constants: element types
165 $PCDATA = '#PCDATA';
166 $CDATA = '#CDATA';
167 $PI = '#PI';
168 $COMMENT = '#COMMENT';
169 $ENT = '#ENT';
170 $NOTATION = '#NOTATION';
171  
172 # element classes
173 $ELT = '#ELT';
174 $TEXT = '#TEXT';
175  
176 # element properties
177 $ASIS = '#ASIS';
178 $EMPTY = '#EMPTY';
179  
180 # used in parseurl to set the buffer size to the same size as in XML::Parser::Expat
181 $BUFSIZE = 32768;
182  
183 # gi => index
184 %XML::Twig::gi2index=( '', 0, $PCDATA => 1, $CDATA => 2, $PI => 3, $COMMENT => 4, $ENT => 5);
185 # list of gi's
186 @XML::Twig::index2gi=( '', $PCDATA, $CDATA, $PI, $COMMENT, $ENT);
187  
188 # gi's under this value are special
189 $XML::Twig::SPECIAL_GI= @XML::Twig::index2gi;
190  
191 %XML::Twig::base_ent= ( '>' => '&gt;', '<' => '&lt;', '&' => '&amp;', "'" => '&apos;', '"' => '&quot;',);
192 foreach my $c ( "\n", "\r", "\t") { $XML::Twig::base_ent{$c}= sprintf( "&#x%02x;", ord( $c)); }
193  
194 # now set some aliases
195 *find_nodes = *get_xpath; # same as XML::XPath
196 *findnodes = *get_xpath; # same as XML::LibXML
197 *getElementsByTagName = *descendants;
198 *descendants_or_self = *descendants; # valid in XML::Twig, not in XML::Twig::Elt
199 *find_by_tag_name = *descendants;
200 *getElementById = *elt_id;
201 *getEltById = *elt_id;
202 *toString = *sprint;
203 *create_accessors = *att_accessors;
204  
205 }
206  
207 @ISA = qw(XML::Parser);
208  
209 # fake gi's used in twig_handlers and start_tag_handlers
210 my $ALL = '_all_'; # the associated function is always called
211 my $DEFAULT= '_default_'; # the function is called if no other handler has been
212  
213 # some defaults
214 my $COMMENTS_DEFAULT= 'keep';
215 my $PI_DEFAULT = 'keep';
216  
217 # handlers used in regular mode
218 my %twig_handlers=( Start => \&_twig_start,
219 End => \&_twig_end,
220 Char => \&_twig_char,
221 Entity => \&_twig_entity,
222 Notation => \&_twig_notation,
223 XMLDecl => \&_twig_xmldecl,
224 Doctype => \&_twig_doctype,
225 Element => \&_twig_element,
226 Attlist => \&_twig_attlist,
227 CdataStart => \&_twig_cdatastart,
228 CdataEnd => \&_twig_cdataend,
229 Proc => \&_twig_pi,
230 Comment => \&_twig_comment,
231 Default => \&_twig_default,
232 ExternEnt => \&_twig_extern_ent,
233 );
234  
235 # handlers used when twig_roots is used and we are outside of the roots
236 my %twig_handlers_roots=
237 ( Start => \&_twig_start_check_roots,
238 End => \&_twig_end_check_roots,
239 Doctype => \&_twig_doctype,
240 Char => undef, Entity => undef, XMLDecl => \&_twig_xmldecl,
241 Element => undef, Attlist => undef, CdataStart => undef,
242 CdataEnd => undef, Proc => undef, Comment => undef,
243 Proc => \&_twig_pi_check_roots,
244 Default => sub {}, # hack needed for XML::Parser 2.27
245 ExternEnt => \&_twig_extern_ent,
246 );
247  
248 # handlers used when twig_roots and print_outside_roots are used and we are
249 # outside of the roots
250 my %twig_handlers_roots_print_2_30=
251 ( Start => \&_twig_start_check_roots,
252 End => \&_twig_end_check_roots,
253 Char => \&_twig_print,
254 Entity => \&_twig_print_entity,
255 ExternEnt => \&_twig_print_entity,
256 DoctypeFin => \&_twig_doctype_fin_print,
257 XMLDecl => sub { _twig_xmldecl( @_); _twig_print( @_); },
258 Doctype => \&_twig_print_doctype, # because recognized_string is broken here
259 # Element => \&_twig_print, Attlist => \&_twig_print,
260 CdataStart => \&_twig_print, CdataEnd => \&_twig_print,
261 Proc => \&_twig_pi_check_roots, Comment => \&_twig_print,
262 Default => \&_twig_print_check_doctype,
263 ExternEnt => \&_twig_extern_ent,
264 );
265  
266 # handlers used when twig_roots, print_outside_roots and keep_encoding are used
267 # and we are outside of the roots
268 my %twig_handlers_roots_print_original_2_30=
269 ( Start => \&_twig_start_check_roots,
270 End => \&_twig_end_check_roots,
271 Char => \&_twig_print_original,
272 # I have no idea why I should not be using this handler!
273 Entity => \&_twig_print_entity,
274 ExternEnt => \&_twig_print_entity,
275 DoctypeFin => \&_twig_doctype_fin_print,
276 XMLDecl => sub { _twig_xmldecl( @_); _twig_print_original( @_) },
277 Doctype => \&_twig_print_original_doctype, # because original_string is broken here
278 Element => \&_twig_print_original, Attlist => \&_twig_print_original,
279 CdataStart => \&_twig_print_original, CdataEnd => \&_twig_print_original,
280 Proc => \&_twig_pi_check_roots, Comment => \&_twig_print_original,
281 Default => \&_twig_print_original_check_doctype,
282 );
283  
284 # handlers used when twig_roots and print_outside_roots are used and we are
285 # outside of the roots
286 my %twig_handlers_roots_print_2_27=
287 ( Start => \&_twig_start_check_roots,
288 End => \&_twig_end_check_roots,
289 Char => \&_twig_print,
290 # if the Entity handler is set then it prints the entity declaration
291 # before the entire internal subset (including the declaration!) is output
292 Entity => sub {},
293 XMLDecl => \&_twig_print, Doctype => \&_twig_print,
294 CdataStart => \&_twig_print, CdataEnd => \&_twig_print,
295 Proc => \&_twig_pi_check_roots, Comment => \&_twig_print,
296 Default => \&_twig_print,
297 ExternEnt => \&_twig_extern_ent,
298 );
299  
300 # handlers used when twig_roots, print_outside_roots and keep_encoding are used
301 # and we are outside of the roots
302 my %twig_handlers_roots_print_original_2_27=
303 ( Start => \&_twig_start_check_roots,
304 End => \&_twig_end_check_roots,
305 Char => \&_twig_print_original,
306 # for some reason original_string is wrong here
307 # this can be a problem if the doctype includes non ascii characters
308 XMLDecl => \&_twig_print, Doctype => \&_twig_print,
309 # if the Entity handler is set then it prints the entity declaration
310 # before the entire internal subset (including the declaration!) is output
311 Entity => sub {},
312 #Element => undef, Attlist => undef,
313 CdataStart => \&_twig_print_original, CdataEnd => \&_twig_print_original,
314 Proc => \&_twig_pi_check_roots, Comment => \&_twig_print_original,
315 Default => \&_twig_print, # _twig_print_original does not work
316 ExternEnt => \&_twig_extern_ent,
317 );
318  
319 my %twig_handlers_roots_print= $parser_version > 2.27
320 ? %twig_handlers_roots_print_2_30
321 : %twig_handlers_roots_print_2_27;
322 my %twig_handlers_roots_print_original= $parser_version > 2.27
323 ? %twig_handlers_roots_print_original_2_30
324 : %twig_handlers_roots_print_original_2_27;
325  
326 # handlers used when the finish_print method has been called
327 my %twig_handlers_finish_print=
328 ( Start => \&_twig_print,
329 End => \&_twig_print, Char => \&_twig_print,
330 Entity => \&_twig_print, XMLDecl => \&_twig_print,
331 Doctype => \&_twig_print, Element => \&_twig_print,
332 Attlist => \&_twig_print, CdataStart => \&_twig_print,
333 CdataEnd => \&_twig_print, Proc => \&_twig_print,
334 Comment => \&_twig_print, Default => \&_twig_print,
335 ExternEnt => \&_twig_extern_ent,
336 );
337  
338 # handlers used when the finish_print method has been called and the keep_encoding
339 # option is used
340 my %twig_handlers_finish_print_original=
341 ( Start => \&_twig_print_original, End => \&_twig_print_end_original,
342 Char => \&_twig_print_original, Entity => \&_twig_print_original,
343 XMLDecl => \&_twig_print_original, Doctype => \&_twig_print_original,
344 Element => \&_twig_print_original, Attlist => \&_twig_print_original,
345 CdataStart => \&_twig_print_original, CdataEnd => \&_twig_print_original,
346 Proc => \&_twig_print_original, Comment => \&_twig_print_original,
347 Default => \&_twig_print_original,
348 );
349  
350 # handlers used within ignored elements
351 my %twig_handlers_ignore=
352 ( Start => \&_twig_ignore_start,
353 End => \&_twig_ignore_end,
354 Char => undef, Entity => undef, XMLDecl => undef,
355 Doctype => undef, Element => undef, Attlist => undef,
356 CdataStart => undef, CdataEnd => undef, Proc => undef,
357 Comment => undef, Default => undef,
358 ExternEnt => undef,
359 );
360  
361 # those handlers are only used if the entities are NOT to be expanded
362 my %twig_noexpand_handlers= ( ExternEnt => undef, Default => \&_twig_default );
363  
364 my @saved_default_handler;
365  
366 my $ID= 'id'; # default value, set by the Id argument
367 my $css_sel; # set through the css_sel option to allow .class selectors in triggers
368  
369 # all allowed options
370 %valid_option=
371 ( # XML::Twig options
372 TwigHandlers => 1, Id => 1,
373 TwigRoots => 1, TwigPrintOutsideRoots => 1,
374 StartTagHandlers => 1, EndTagHandlers => 1,
375 ForceEndTagHandlersUsage => 1,
376 DoNotChainHandlers => 1,
377 IgnoreElts => 1,
378 Index => 1,
379 AttAccessors => 1,
380 EltAccessors => 1,
381 FieldAccessors => 1,
382 CharHandler => 1,
383 TopDownHandlers => 1,
384 KeepEncoding => 1, DoNotEscapeAmpInAtts => 1,
385 ParseStartTag => 1, KeepAttsOrder => 1,
386 LoadDTD => 1, DTDHandler => 1, DTDBase => 1, NoXxe => 1,
387 DoNotOutputDTD => 1, NoProlog => 1,
388 ExpandExternalEnts => 1,
389 DiscardSpaces => 1, KeepSpaces => 1, DiscardAllSpaces => 1,
390 DiscardSpacesIn => 1, KeepSpacesIn => 1,
391 PrettyPrint => 1, EmptyTags => 1,
392 EscapeGt => 1,
393 Quote => 1,
394 Comments => 1, Pi => 1,
395 OutputFilter => 1, InputFilter => 1,
396 OutputTextFilter => 1,
397 OutputEncoding => 1,
398 RemoveCdata => 1,
399 EltClass => 1,
400 MapXmlns => 1, KeepOriginalPrefix => 1,
401 SkipMissingEnts => 1,
402 # XML::Parser options
403 ErrorContext => 1, ProtocolEncoding => 1,
404 Namespaces => 1, NoExpand => 1,
405 Stream_Delimiter => 1, ParseParamEnt => 1,
406 NoLWP => 1, Non_Expat_Options => 1,
407 Xmlns => 1, CssSel => 1,
408 UseTidy => 1, TidyOptions => 1,
409 OutputHtmlDoctype => 1,
410 );
411  
412 my $active_twig; # last active twig,for XML::Twig::s
413  
414 # predefined input and output filters
415 use vars qw( %filter);
416 %filter= ( html => \&html_encode,
417 safe => \&safe_encode,
418 safe_hex => \&safe_encode_hex,
419 );
420  
421 # trigger types (used to sort them)
422 my ($LEVEL_TRIGGER, $REGEXP_TRIGGER, $XPATH_TRIGGER)=(1..3);
423  
424 sub new
425 { my ($class, %args) = @_;
426 my $handlers;
427  
428 # change all nice_perlish_names into nicePerlishNames
429 %args= _normalize_args( %args);
430  
431 # check options
432 unless( $args{MoreOptions})
433 { foreach my $arg (keys %args)
434 { carp "invalid option $arg" unless $valid_option{$arg}; }
435 }
436  
437 # a twig is really an XML::Parser
438 # my $self= XML::Parser->new(%args);
439 my $self;
440 $self= XML::Parser->new(%args);
441  
442 bless $self, $class;
443  
444 $self->{_twig_context_stack}= [];
445  
446 # allow tag.class selectors in handler triggers
447 $css_sel= $args{CssSel};
448  
449 if( exists $args{TwigHandlers})
450 { $handlers= $args{TwigHandlers};
451 $self->setTwigHandlers( $handlers);
452 delete $args{TwigHandlers};
453 }
454  
455 # take care of twig-specific arguments
456 if( exists $args{StartTagHandlers})
457 { $self->setStartTagHandlers( $args{StartTagHandlers});
458 delete $args{StartTagHandlers};
459 }
460  
461 if( exists $args{DoNotChainHandlers})
462 { $self->{twig_do_not_chain_handlers}= $args{DoNotChainHandlers}; }
463  
464 if( exists $args{IgnoreElts})
465 { # change array to hash so you can write ignore_elts => [ qw(foo bar baz)]
466 if( isa( $args{IgnoreElts}, 'ARRAY')) { $args{IgnoreElts}= { map { $_ => 1 } @{$args{IgnoreElts}} }; }
467 $self->setIgnoreEltsHandlers( $args{IgnoreElts});
468 delete $args{IgnoreElts};
469 }
470  
471 if( exists $args{Index})
472 { my $index= $args{Index};
473 # we really want a hash name => path, we turn an array into a hash if necessary
474 if( ref( $index) eq 'ARRAY')
475 { my %index= map { $_ => $_ } @$index;
476 $index= \%index;
477 }
478 while( my( $name, $exp)= each %$index)
479 { $self->setTwigHandler( $exp, sub { push @{$_[0]->{_twig_index}->{$name}}, $_; 1; }); }
480 }
481  
482 $self->{twig_elt_class}= $args{EltClass} || 'XML::Twig::Elt';
483 if( defined( $args{EltClass}) && $args{EltClass} ne 'XML::Twig::Elt') { $self->{twig_alt_elt_class}=1; }
484 if( exists( $args{EltClass})) { delete $args{EltClass}; }
485  
486 if( exists( $args{MapXmlns}))
487 { $self->{twig_map_xmlns}= $args{MapXmlns};
488 $self->{Namespaces}=1;
489 delete $args{MapXmlns};
490 }
491  
492 if( exists( $args{KeepOriginalPrefix}))
493 { $self->{twig_keep_original_prefix}= $args{KeepOriginalPrefix};
494 delete $args{KeepOriginalPrefix};
495 }
496  
497 $self->{twig_dtd_handler}= $args{DTDHandler};
498 delete $args{DTDHandler};
499  
500 if( $args{ExpandExternalEnts})
501 { $self->set_expand_external_entities( 1);
502 $self->{twig_expand_external_ents}= $args{ExpandExternalEnts};
503 $self->{twig_read_external_dtd}= 1; # implied by ExpandExternalEnts
504 if( $args{ExpandExternalEnts} == -1)
505 { $self->{twig_extern_ent_nofail}= 1;
506 $self->setHandlers( ExternEnt => \&_twig_extern_ent_nofail);
507 }
508 delete $args{LoadDTD};
509 delete $args{ExpandExternalEnts};
510 }
511 else
512 { $self->set_expand_external_entities( 0); }
513  
514 if( !$args{NoLWP} && ! _use( 'URI') && ! _use( 'URI::File') && ! _use( 'LWP'))
515 { $self->{twig_ext_ent_handler}= \&XML::Parser::initial_ext_ent_handler }
516 elsif( $args{NoXxe})
517 { $self->{twig_ext_ent_handler}=
518 sub { my($xp, $base, $path) = @_; $xp->{ErrorMessage}.= "cannot use entities in document when the no_xxe option is on"; return undef; };
519 }
520 else
521 { $self->{twig_ext_ent_handler}= \&XML::Parser::file_ext_ent_handler }
522  
523 if( $args{DoNotEscapeAmpInAtts})
524 { $self->set_do_not_escape_amp_in_atts( 1);
525 $self->{twig_do_not_escape_amp_in_atts}=1;
526 }
527 else
528 { $self->set_do_not_escape_amp_in_atts( 0);
529 $self->{twig_do_not_escape_amp_in_atts}=0;
530 }
531  
532 # deal with TwigRoots argument, a hash of elements for which
533 # subtrees will be built (and associated handlers)
534  
535 if( $args{TwigRoots})
536 { $self->setTwigRoots( $args{TwigRoots});
537 delete $args{TwigRoots};
538 }
539  
540 if( $args{EndTagHandlers})
541 { unless ($self->{twig_roots} || $args{ForceEndTagHandlersUsage})
542 { croak "you should not use EndTagHandlers without TwigRoots\n",
543 "if you want to use it anyway, normally because you have ",
544 "a start_tag_handlers that calls 'ignore' and you want to ",
545 "call an ent_tag_handlers at the end of the element, then ",
546 "pass 'force_end_tag_handlers_usage => 1' as an argument ",
547 "to new";
548 }
549  
550 $self->setEndTagHandlers( $args{EndTagHandlers});
551 delete $args{EndTagHandlers};
552 }
553  
554 if( $args{TwigPrintOutsideRoots})
555 { croak "cannot use twig_print_outside_roots without twig_roots"
556 unless( $self->{twig_roots});
557 # if the arg is a filehandle then store it
558 if( _is_fh( $args{TwigPrintOutsideRoots}) )
559 { $self->{twig_output_fh}= $args{TwigPrintOutsideRoots}; }
560 $self->{twig_default_print}= $args{TwigPrintOutsideRoots};
561 }
562  
563 # space policy
564 if( $args{KeepSpaces})
565 { croak "cannot use both keep_spaces and discard_spaces" if( $args{DiscardSpaces});
566 croak "cannot use both keep_spaces and discard_all_spaces" if( $args{DiscardAllSpaces});
567 croak "cannot use both keep_spaces and keep_spaces_in" if( $args{KeepSpacesIn});
568 $self->{twig_keep_spaces}=1;
569 delete $args{KeepSpaces};
570 }
571 if( $args{DiscardSpaces})
572 {
573 croak "cannot use both discard_spaces and keep_spaces_in" if( $args{KeepSpacesIn});
574 croak "cannot use both discard_spaces and discard_all_spaces" if( $args{DiscardAllSpaces});
575 croak "cannot use both discard_spaces and discard_spaces_in" if( $args{DiscardSpacesIn});
576 $self->{twig_discard_spaces}=1;
577 delete $args{DiscardSpaces};
578 }
579 if( $args{KeepSpacesIn})
580 { croak "cannot use both keep_spaces_in and discard_spaces_in" if( $args{DiscardSpacesIn});
581 croak "cannot use both keep_spaces_in and discard_all_spaces" if( $args{DiscardAllSpaces});
582 $self->{twig_discard_spaces}=1;
583 $self->{twig_keep_spaces_in}={};
584 my @tags= @{$args{KeepSpacesIn}};
585 foreach my $tag (@tags) { $self->{twig_keep_spaces_in}->{$tag}=1; }
586 delete $args{KeepSpacesIn};
587 }
588  
589 if( $args{DiscardAllSpaces})
590 {
591 croak "cannot use both discard_all_spaces and discard_spaces_in" if( $args{DiscardSpacesIn});
592 $self->{twig_discard_all_spaces}=1;
593 delete $args{DiscardAllSpaces};
594 }
595  
596 if( $args{DiscardSpacesIn})
597 { $self->{twig_keep_spaces}=1;
598 $self->{twig_discard_spaces_in}={};
599 my @tags= @{$args{DiscardSpacesIn}};
600 foreach my $tag (@tags) { $self->{twig_discard_spaces_in}->{$tag}=1; }
601 delete $args{DiscardSpacesIn};
602 }
603 # discard spaces by default
604 $self->{twig_discard_spaces}= 1 unless( $self->{twig_keep_spaces});
605  
606 $args{Comments}||= $COMMENTS_DEFAULT;
607 if( $args{Comments} eq 'drop') { $self->{twig_keep_comments}= 0; }
608 elsif( $args{Comments} eq 'keep') { $self->{twig_keep_comments}= 1; }
609 elsif( $args{Comments} eq 'process') { $self->{twig_process_comments}= 1; }
610 else { croak "wrong value for comments argument: '$args{Comments}' (should be 'drop', 'keep' or 'process')"; }
611 delete $args{Comments};
612  
613 $args{Pi}||= $PI_DEFAULT;
614 if( $args{Pi} eq 'drop') { $self->{twig_keep_pi}= 0; }
615 elsif( $args{Pi} eq 'keep') { $self->{twig_keep_pi}= 1; }
616 elsif( $args{Pi} eq 'process') { $self->{twig_process_pi}= 1; }
617 else { croak "wrong value for pi argument: '$args{Pi}' (should be 'drop', 'keep' or 'process')"; }
618 delete $args{Pi};
619  
620 if( $args{KeepEncoding})
621 {
622 # set it in XML::Twig::Elt so print functions know what to do
623 $self->set_keep_encoding( 1);
624 $self->{parse_start_tag}= $args{ParseStartTag} || \&_parse_start_tag;
625 delete $args{ParseStartTag} if defined( $args{ParseStartTag}) ;
626 delete $args{KeepEncoding};
627 }
628 else
629 { $self->set_keep_encoding( 0);
630 if( $args{ParseStartTag})
631 { $self->{parse_start_tag}= $args{ParseStartTag}; }
632 else
633 { delete $self->{parse_start_tag}; }
634 delete $args{ParseStartTag};
635 }
636  
637 if( $args{OutputFilter})
638 { $self->set_output_filter( $args{OutputFilter});
639 delete $args{OutputFilter};
640 }
641 else
642 { $self->set_output_filter( 0); }
643  
644 if( $args{RemoveCdata})
645 { $self->set_remove_cdata( $args{RemoveCdata});
646 delete $args{RemoveCdata};
647 }
648 else
649 { $self->set_remove_cdata( 0); }
650  
651 if( $args{OutputTextFilter})
652 { $self->set_output_text_filter( $args{OutputTextFilter});
653 delete $args{OutputTextFilter};
654 }
655 else
656 { $self->set_output_text_filter( 0); }
657  
658 if( $args{KeepAttsOrder})
659 { $self->{keep_atts_order}= $args{KeepAttsOrder};
660 if( _use( 'Tie::IxHash'))
661 { $self->set_keep_atts_order( $self->{keep_atts_order}); }
662 else
663 { croak "Tie::IxHash not available, option keep_atts_order not allowed"; }
664 }
665 else
666 { $self->set_keep_atts_order( 0); }
667  
668 if( $args{PrettyPrint}) { $self->set_pretty_print( $args{PrettyPrint}); }
669 if( $args{EscapeGt}) { $self->escape_gt( $args{EscapeGt}); }
670 if( $args{EmptyTags}) { $self->set_empty_tag_style( $args{EmptyTags}) }
671  
672 if( exists $args{Id}) { $ID= $args{Id}; delete $args{ID}; }
673 if( $args{NoProlog}) { $self->{no_prolog}= 1; delete $args{NoProlog}; }
674 if( $args{DoNotOutputDTD}) { $self->{no_dtd_output}= 1; delete $args{DoNotOutputDTD}; }
675 if( $args{LoadDTD}) { $self->{twig_read_external_dtd}= 1; delete $args{LoadDTD}; }
676 if( $args{CharHandler}) { $self->setCharHandler( $args{CharHandler}); delete $args{CharHandler}; }
677  
678 if( $args{InputFilter}) { $self->set_input_filter( $args{InputFilter}); delete $args{InputFilter}; }
679 if( $args{NoExpand}) { $self->setHandlers( %twig_noexpand_handlers); $self->{twig_no_expand}=1; }
680 if( my $output_encoding= $args{OutputEncoding}) { $self->set_output_encoding( $output_encoding); delete $args{OutputFilter}; }
681  
682 if( my $tdh= $args{TopDownHandlers}) { $self->{twig_tdh}=1; delete $args{TopDownHandlers}; }
683  
684 if( my $acc_a= $args{AttAccessors}) { $self->att_accessors( @$acc_a); }
685 if( my $acc_e= $args{EltAccessors}) { $self->elt_accessors( isa( $acc_e, 'ARRAY') ? @$acc_e : $acc_e); }
686 if( my $acc_f= $args{FieldAccessors}) { $self->field_accessors( isa( $acc_f, 'ARRAY') ? @$acc_f : $acc_f); }
687  
688 if( $args{UseTidy}) { $self->{use_tidy}= 1; }
689 $self->{tidy_options}= $args{TidyOptions} || {};
690  
691 if( $args{OutputHtmlDoctype}) { $self->{html_doctype}= 1; }
692  
693 $self->set_quote( $args{Quote} || 'double');
694  
695 # set handlers
696 if( $self->{twig_roots})
697 { if( $self->{twig_default_print})
698 { if( $self->{twig_keep_encoding})
699 { $self->setHandlers( %twig_handlers_roots_print_original); }
700 else
701 { $self->setHandlers( %twig_handlers_roots_print); }
702 }
703 else
704 { $self->setHandlers( %twig_handlers_roots); }
705 }
706 else
707 { $self->setHandlers( %twig_handlers); }
708  
709 # XML::Parser::Expat does not like these handler to be set. So in order to
710 # use the various sets of handlers on XML::Parser or XML::Parser::Expat
711 # objects when needed, these ones have to be set only once, here, at
712 # XML::Parser level
713 $self->setHandlers( Init => \&_twig_init, Final => \&_twig_final);
714  
715 $self->{twig_entity_list}= XML::Twig::Entity_list->new;
716 $self->{twig_notation_list}= XML::Twig::Notation_list->new;
717  
718 $self->{twig_id}= $ID;
719 $self->{twig_stored_spaces}='';
720  
721 $self->{twig_autoflush}= 1; # auto flush by default
722  
723 $self->{twig}= $self;
724 weaken( $self->{twig});
725  
726 return $self;
727 }
728  
729 sub parse
730 {
731 my $t= shift;
732 # if called as a class method, calls nparse, which creates the twig then parses it
733 if( !ref( $t) || !isa( $t, 'XML::Twig')) { return $t->nparse( @_); }
734  
735 # trap underlying bug in IO::Handle (see RT #17500)
736 # croak if -CD (or PERL_UNICODE set to D) and parsing a pipe
737 if( ${^UNICODE} && (${^UNICODE} & 24) && isa( $_[0], 'GLOB') && -p $_[0] )
738 { croak "cannot parse the output of a pipe when perl is set to use the UTF8 perlIO layer\n"
739 . "set the environment variable PERL_UNICODE or use the -C option (see perldoc perlrun)\n"
740 . "not to include 'D'";
741 }
742 $t= eval { $t->SUPER::parse( @_); };
743  
744 if( !$t
745 && $@=~m{(syntax error at line 1, column 0, byte 0|not well-formed \(invalid token\) at line 1, column 1, byte 1)}
746 && -f $_[0]
747 && ( ! ref( $_[0]) || ref( $_[0])) ne 'GLOB' # -f works on a filehandle, so this make sure $_[0] is a real file
748 )
749 { croak "you seem to have used the parse method on a filename ($_[0]), you probably want parsefile instead"; }
750 return _checked_parse_result( $t, $@);
751 }
752  
753 sub parsefile
754 { my $t= shift;
755 if( -f $_[0] && ! -s $_[0]) { return _checked_parse_result( undef, "empty file '$_[0]'"); }
756 $t= eval { $t->SUPER::parsefile( @_); };
757 return _checked_parse_result( $t, $@);
758 }
759  
760 sub _checked_parse_result
761 { my( $t, $returned)= @_;
762 if( !$t)
763 { if( isa( $returned, 'XML::Twig') && $returned->{twig_finish_now})
764 { $t= $returned;
765 delete $t->{twig_finish_now};
766 return $t->_twig_final;
767 }
768 else
769 { _croak( $returned, 0); }
770 }
771  
772 $active_twig= $t;
773 return $t;
774 }
775  
776 sub active_twig { return $active_twig; }
777  
778 sub finish_now
779 { my $t= shift;
780 $t->{twig_finish_now}=1;
781 # XML::Parser 2.43 changed xpcroak in a way that caused test failures for XML::Twig
782 # the change was reverted in 2.44, but this is here to ensure that tests pass with 2.43
783 if( $XML::Parser::VERSION == 2.43)
784 { no warnings;
785 $t->parser->{twig_error}= $t;
786 *XML::Parser::Expat::xpcroak= sub { die $_[0]->{twig_error}; };
787 die $t;
788 }
789 else
790 { die $t; }
791 }
792  
793 sub parsefile_inplace { shift->_parse_inplace( parsefile => @_); }
794 sub parsefile_html_inplace { shift->_parse_inplace( parsefile_html => @_); }
795  
796 sub _parse_inplace
797 { my( $t, $method, $file, $suffix)= @_;
798 _use( 'File::Temp') || croak "need File::Temp to use inplace methods\n";
799 _use( 'File::Basename');
800  
801 my $tmpdir= dirname( $file);
802 my( $tmpfh, $tmpfile)= File::Temp::tempfile( DIR => $tmpdir);
803 my $original_fh= select $tmpfh;
804  
805 # we can only use binmode :utf8 if perl was compiled with useperlio
806 # might be a problem if keep_encoding used but the file is already in utf8
807 if( !$t->{twig_keep_encoding} && _use_perlio()) { binmode( $tmpfh, ":utf8" ); } ## no critic (RequireEncodingWithUTF8Layer)
808  
809 $t->$method( $file);
810  
811 select $original_fh;
812 close $tmpfh;
813 my $mode= (stat( $file))[2] & oct(7777);
814 chmod $mode, $tmpfile or croak "cannot change temp file mode to $mode: $!";
815  
816 if( $suffix)
817 { my $backup;
818 if( $suffix=~ m{\*}) { ($backup = $suffix) =~ s/\*/$file/g; }
819 else { $backup= $file . $suffix; }
820  
821 rename( $file, $backup) or croak "cannot backup initial file ($file) to $backup: $!";
822 }
823 rename( $tmpfile, $file) or croak "cannot rename temp file ($tmpfile) to initial file ($file): $!";
824  
825 return $t;
826 }
827  
828 sub parseurl
829 { my $t= shift;
830 $t->_parseurl( 0, @_);
831 }
832  
833 sub safe_parseurl
834 { my $t= shift;
835 $t->_parseurl( 1, @_);
836 }
837  
838 sub safe_parsefile_html
839 { my $t= shift;
840 eval { $t->parsefile_html( @_); };
841 return $@ ? $t->_reset_twig_after_error : $t;
842 }
843  
844 sub safe_parseurl_html
845 { my $t= shift;
846 _use( 'LWP::Simple') or croak "missing LWP::Simple";
847 eval { $t->parse_html( LWP::Simple::get( shift()), @_); } ;
848 return $@ ? $t->_reset_twig_after_error : $t;
849 }
850  
851 sub parseurl_html
852 { my $t= shift;
853 _use( 'LWP::Simple') or croak "missing LWP::Simple";
854 $t->parse_html( LWP::Simple::get( shift()), @_);
855 }
856  
857 # uses eval to catch the parser's death
858 sub safe_parse_html
859 { my $t= shift;
860 eval { $t->parse_html( @_); } ;
861 return $@ ? $t->_reset_twig_after_error : $t;
862 }
863  
864 sub parsefile_html
865 { my $t= shift;
866 my $file= shift;
867 my $indent= $t->{ErrorContext} ? 1 : 0;
868 $t->set_empty_tag_style( 'html');
869 my $html2xml= $t->{use_tidy} ? \&_tidy_html : \&_html2xml;
870 my $options= $t->{use_tidy} ? $t->{tidy_options} || {} : { indent => $indent, html_doctype => $t->{html_doctype} };
871 $t->parse( $html2xml->( _slurp( $file), $options), @_);
872 return $t;
873 }
874  
875 sub parse_html
876 { my $t= shift;
877 my $options= ref $_[0] && ref $_[0] eq 'HASH' ? shift() : {};
878 my $use_tidy= exists $options->{use_tidy} ? $options->{use_tidy} : $t->{use_tidy};
879 my $content= shift;
880 my $indent= $t->{ErrorContext} ? 1 : 0;
881 $t->set_empty_tag_style( 'html');
882 my $html2xml= $use_tidy ? \&_tidy_html : \&_html2xml;
883 my $conv_options= $use_tidy ? $t->{tidy_options} || {} : { indent => $indent, html_doctype => $t->{html_doctype} };
884 $t->parse( $html2xml->( isa( $content, 'GLOB') ? _slurp_fh( $content) : $content, $conv_options), @_);
885 return $t;
886 }
887  
888 sub xparse
889 { my $t= shift;
890 my $to_parse= $_[0];
891 if( isa( $to_parse, 'GLOB')) { $t->parse( @_); }
892 elsif( $to_parse=~ m{^\s*<}) { $to_parse=~ m{<html}i ? $t->_parse_as_xml_or_html( @_)
893 : $t->parse( @_);
894 }
895 elsif( $to_parse=~ m{^\w+://.*\.html?$}) { _use( 'LWP::Simple') or croak "missing LWP::Simple";
896 $t->_parse_as_xml_or_html( LWP::Simple::get( shift()), @_);
897 }
898 elsif( $to_parse=~ m{^\w+://}) { _use( 'LWP::Simple') or croak "missing LWP::Simple";
899 my $doc= LWP::Simple::get( shift);
900 if( ! defined $doc) { $doc=''; }
901 my $xml_parse_ok= $t->safe_parse( $doc, @_);
902 if( $xml_parse_ok)
903 { return $xml_parse_ok; }
904 else
905 { my $diag= $@;
906 if( $doc=~ m{<html}i)
907 { $t->parse_html( $doc, @_); }
908 else
909 { croak $diag; }
910 }
911 }
912 elsif( $to_parse=~ m{\.html?$}) { my $content= _slurp( shift);
913 $t->_parse_as_xml_or_html( $content, @_);
914 }
915 else { $t->parsefile( @_); }
916 }
917  
918 sub _parse_as_xml_or_html
919 { my $t= shift;
920 if( _is_well_formed_xml( $_[0]))
921 { $t->parse( @_) }
922 else
923 { my $html2xml= $t->{use_tidy} ? \&_tidy_html : \&_html2xml;
924 my $options= $t->{use_tidy} ? $t->{tidy_options} || {} : { indent => 0, html_doctype => $t->{html_doctype} };
925 my $html= $html2xml->( $_[0], $options, @_);
926 if( _is_well_formed_xml( $html))
927 { $t->parse( $html); }
928 else
929 { croak $@; } # can't really test this because HTML::Parser or HTML::Tidy may change how they deal with bas HTML between versions
930 }
931 }
932  
933 { my $parser;
934 sub _is_well_formed_xml
935 { $parser ||= XML::Parser->new;
936 eval { $parser->parse( $_[0]); };
937 return $@ ? 0 : 1;
938 }
939 }
940  
941 sub nparse
942 { my $class= shift;
943 my $to_parse= pop;
944 $class->new( @_)->xparse( $to_parse);
945 }
946  
947 sub nparse_pp { shift()->nparse( pretty_print => 'indented', @_); }
948 sub nparse_e { shift()->nparse( error_context => 1, @_); }
949 sub nparse_ppe { shift()->nparse( pretty_print => 'indented', error_context => 1, @_); }
950  
951 sub _html2xml
952 { my( $html, $options)= @_;
953 _use( 'HTML::TreeBuilder', '3.13') or croak "cannot parse HTML: missing HTML::TreeBuilder v >= 3.13\n";
954 my $tree= HTML::TreeBuilder->new;
955 $tree->ignore_ignorable_whitespace( 0);
956 $tree->ignore_unknown( 0);
957 $tree->no_space_compacting( 1);
958 $tree->store_comments( 1);
959 $tree->store_pis(1);
960 $tree->parse( $html);
961 $tree->eof;
962  
963 my $xml='';
964 if( $options->{html_doctype} && exists $tree->{_decl} )
965 { my $decl= $tree->{_decl}->as_XML;
966  
967 # first try to fix declarations that are missing the SYSTEM part
968 $decl =~ s{^\s*<!DOCTYPE \s+ ((?i)html) \s+ PUBLIC \s+ "([^"]*)" \s* >}
969 { my $system= $HTML_DECL{$2} || $HTML_DECL{$DEFAULT_HTML_TYPE};
970 qq{<!DOCTYPE $1 PUBLIC "$2" "$system">}
971  
972 }xe;
973  
974 # then check that the declaration looks OK (so it parses), if not remove it,
975 # better to parse without the declaration than to die stupidly
976 if( $decl =~ m{<!DOCTYPE \s+ (?i:HTML) (\s+ PUBLIC \s+ "[^"]*" \s+ (SYSTEM \s+)? "[^"]*")? \s*>}x # PUBLIC then SYSTEM
977 || $decl =~ m{<!DOCTYPE \s+ (?i:HTML) \s+ SYSTEM \s+ "[^"]*" \s*>}x # just SYSTEM
978 )
979 { $xml= $decl; }
980 }
981  
982 $xml.= _as_XML( $tree);
983  
984 _fix_xml( $tree, \$xml);
985  
986 if( $options->{indent}) { _indent_xhtml( \$xml); }
987 $tree->delete;
988 $xml=~ s{\s+$}{}s; # trim end
989 return $xml;
990 }
991  
992 sub _tidy_html
993 { my( $html, $options)= @_;
994 _use( 'HTML::Tidy') or croak "cannot cleanup HTML using HTML::Tidy (required by the use_tidy option): $@\n"; ;
995 my $TIDY_DEFAULTS= { output_xhtml => 1, # duh!
996 tidy_mark => 0, # do not add the "generated by tidy" comment
997 numeric_entities => 1,
998 char_encoding => 'utf8',
999 bare => 1,
1000 clean => 1,
1001 doctype => 'transitional',
1002 fix_backslash => 1,
1003 merge_divs => 0,
1004 merge_spans => 0,
1005 sort_attributes => 'alpha',
1006 indent => 0,
1007 wrap => 0,
1008 break_before_br => 0,
1009 };
1010 $options ||= {};
1011 my $tidy_options= { %$TIDY_DEFAULTS, %$options};
1012 my $tidy = HTML::Tidy->new( $tidy_options);
1013 $tidy->ignore( type => 1, type => 2 ); # 1 is TIDY_WARNING, 2 is TIDY_ERROR, not clean
1014 my $xml= $tidy->clean( $html );
1015 return $xml;
1016 }
1017  
1018 { my %xml_parser_encoding;
1019 sub _fix_xml
1020 { my( $tree, $xml)= @_; # $xml is a ref to the xml string
1021  
1022 my $max_tries=5;
1023 my $add_decl;
1024  
1025 while( ! _check_xml( $xml) && $max_tries--)
1026 {
1027 # a couple of fixes for weird HTML::TreeBuilder errors
1028 if( $@=~ m{^\s*xml (or text )?declaration not at start of (external )?entity}i)
1029 { $$xml=~ s{<\?xml.*?\?>}{}g;
1030 #warn " fixed xml declaration in the wrong place\n";
1031 }
1032 elsif( $@=~ m{undefined entity})
1033 { $$xml=~ s{&(amp;)?Amp;}{&amp;}g if $HTML::TreeBuilder::VERSION < 4.00;
1034 if( _use( 'HTML::Entities::Numbered')) { $$xml=name2hex_xml( $$xml); }
1035 $$xml=~ s{&(\w+);}{ my $ent= $1; if( $ent !~ m{^(amp|lt|gt|apos|quote)$}) { "&amp;$ent;" } }eg;
1036 }
1037 elsif( $@=~ m{&Amp; used in html})
1038 # if $Amp; is used instead of &amp; then HTML::TreeBuilder's as_xml is tripped (old version)
1039 { $$xml=~ s{&(amp;)?Amp;}{&amp;}g if $HTML::TreeBuilder::VERSION < 4.00;
1040 }
1041 elsif( $@=~ m{^\s*not well-formed \(invalid token\)})
1042 { if( $HTML::TreeBuilder::VERSION < 4.00)
1043 { $$xml=~ s{&(amp;)?Amp;}{&amp;}g;
1044 $$xml=~ s{(<[^>]* )(\d+=)"}{$1a$2"}g; # <table 1> comes out as <table 1="1">, "fix the attribute
1045 }
1046 my $q= '<img "="&#34;" '; # extracted so vim doesn't get confused
1047 if( _use( 'HTML::Entities::Numbered')) { $$xml=name2hex_xml( $$xml); }
1048 if( $$xml=~ m{$q})
1049 { $$xml=~ s{$q}{<img }g; # happens with <img src="foo.png"" ...
1050 }
1051 else
1052 { my $encoding= _encoding_from_meta( $tree);
1053 unless( keys %xml_parser_encoding) { %xml_parser_encoding= _xml_parser_encodings(); }
1054  
1055 if( ! $add_decl)
1056 { if( $xml_parser_encoding{$encoding})
1057 { $add_decl=1; }
1058 elsif( $encoding eq 'euc-jp' && $xml_parser_encoding{'x-euc-jp-jisx0221'})
1059 { $encoding="x-euc-jp-jisx0221"; $add_decl=1;}
1060 elsif( $encoding eq 'shift-jis' && $xml_parser_encoding{'x-sjis-jisx0221'})
1061 { $encoding="x-sjis-jisx0221"; $add_decl=1;}
1062  
1063 if( $add_decl)
1064 { $$xml=~ s{^(<\?xml.*?\?>)?}{<?xml version="1.0" encoding="$encoding"?>}s;
1065 #warn " added decl (encoding $encoding)\n";
1066 }
1067 else
1068 { $$xml=~ s{^(<\?xml.*?\?>)?}{}s;
1069 #warn " converting to utf8 from $encoding\n";
1070 $$xml= _to_utf8( $encoding, $$xml);
1071 }
1072 }
1073 else
1074 { $$xml=~ s{^(<\?xml.*?\?>)?}{}s;
1075 #warn " converting to utf8 from $encoding\n";
1076 $$xml= _to_utf8( $encoding, $$xml);
1077 }
1078 }
1079 }
1080 }
1081  
1082 # some versions of HTML::TreeBuilder escape CDATA sections
1083 $$xml=~ s{(&lt;!\[CDATA\[.*?\]\]&gt;)}{_unescape_cdata( $1)}eg;
1084 }
1085  
1086 sub _xml_parser_encodings
1087 { my @encodings=( 'iso-8859-1'); # this one is included by default, there is no map for it in @INC
1088 foreach my $inc (@INC)
1089 { push @encodings, map { basename( $_, '.enc') } glob( File::Spec->catdir( $inc => XML => Parser => Encodings => '*.enc')); }
1090 return map { $_ => 1 } @encodings;
1091 }
1092 }
1093  
1094 sub _unescape_cdata
1095 { my( $cdata)= @_;
1096 $cdata=~s{&lt;}{<}g;
1097 $cdata=~s{&gt;}{>}g;
1098 $cdata=~s{&amp;}{&}g;
1099 return $cdata;
1100 }
1101  
1102 sub _as_XML {
1103  
1104 # fork of HTML::Element::as_XML, which is a little too buggy and inconsistent between versions for my liking
1105 my ($elt) = @_;
1106 my $xml= '';
1107 my $empty_element_map = $elt->_empty_element_map;
1108  
1109 my ( $tag, $node, $start ); # per-iteration scratch
1110 $elt->traverse(
1111 sub {
1112 ( $node, $start ) = @_;
1113 if ( ref $node )
1114 { # it's an element
1115 $tag = $node->{'_tag'};
1116 if ($start)
1117 { # on the way in
1118 foreach my $att ( grep { ! m{^(_|/$)} } keys %$node )
1119 { # fix attribute names instead of dying
1120 my $new_att= $att;
1121 if( $att=~ m{^\d}) { $new_att= "a$att"; }
1122 $new_att=~ s{[^\w\d:_-]}{}g;
1123 $new_att ||= 'a';
1124 if( $new_att ne $att) { $node->{$new_att}= delete $node->{$att}; }
1125 }
1126  
1127 if ( $empty_element_map->{$tag} && (!@{ $node->{'_content'} || []}) )
1128 { $xml.= $node->starttag_XML( undef, 1 ); }
1129 else
1130 { $xml.= $node->starttag_XML(undef); }
1131 }
1132 else
1133 { # on the way out
1134 unless ( $empty_element_map->{$tag} and !@{ $node->{'_content'} || [] } )
1135 { $xml.= $node->endtag_XML();
1136 } # otherwise it will have been an <... /> tag.
1137 }
1138 }
1139 elsif( $node=~ /<!\[CDATA\[/) # the content includes CDATA
1140 { foreach my $chunk (split /(<!\[CDATA\[.*?\]\]>)/s, $node) # chunks are CDATA sections or normal text
1141 { $xml.= $chunk =~ m{<!\[CDATA\[} ? $chunk : _xml_escape( $chunk); }
1142 }
1143 else # it's just text
1144 { $xml .= _xml_escape($node); }
1145 1; # keep traversing
1146 }
1147 );
1148 return $xml;
1149 }
1150  
1151 sub _xml_escape
1152 { my( $html)= @_;
1153 $html =~ s{&(?! # An ampersand that isn't followed by...
1154 ( \#[0-9]+; | # A hash mark, digits and semicolon, or
1155 \#x[0-9a-fA-F]+; | # A hash mark, "x", hex digits and semicolon, or
1156 [\w]+; # A valid unicode entity name and semicolon
1157 )
1158 )
1159 }
1160 {&amp;}gx if 0; # Needs to be escaped to amp
1161  
1162 $html=~ s{&}{&amp;}g;
1163  
1164 # in old versions of HTML::TreeBuilder &amp; can come out as &Amp;
1165 if( $HTML::TreeBuilder::VERSION && $HTML::TreeBuilder::VERSION <= 3.23) { $html=~ s{&Amp;}{&amp;}g; }
1166  
1167 # simple character escapes
1168 $html =~ s/</&lt;/g;
1169 $html =~ s/>/&gt;/g;
1170 $html =~ s/"/&quot;/g;
1171 $html =~ s/'/&apos;/g;
1172  
1173 return $html;
1174 }
1175  
1176 sub _check_xml
1177 { my( $xml)= @_; # $xml is a ref to the xml string
1178 my $ok= eval { XML::Parser->new->parse( $$xml); };
1179 return $ok;
1180 }
1181  
1182 sub _encoding_from_meta
1183 { my( $tree)= @_;
1184 my $enc="iso-8859-1";
1185 my @meta= $tree->find( 'meta');
1186 foreach my $meta (@meta)
1187 { if( $meta->{'http-equiv'} && ($meta->{'http-equiv'} =~ m{^\s*content-type\s*}i)
1188 && $meta->{content} && ($meta->{content} =~ m{^\s*text/html\s*;\s*charset\s*=\s*(\S*)\s*}i)
1189 )
1190 { $enc= lc $1;
1191 #warn " encoding from meta tag is '$enc'\n";
1192 last;
1193 }
1194 }
1195 return $enc;
1196 }
1197  
1198 { sub _to_utf8
1199 { my( $encoding, $string)= @_;
1200 local $SIG{__DIE__};
1201 if( _use( 'Encode'))
1202 { Encode::from_to( $string, $encoding => 'utf8', 0x0400); } # 0x0400 is Encode::FB_XMLCREF
1203 elsif( _use( 'Text::Iconv'))
1204 { my $converter = eval { Text::Iconv->new( $encoding => "utf8") };
1205 if( $converter) { $string= $converter->convert( $string); }
1206 }
1207 elsif( _use( 'Unicode::Map8') && _use( 'Unicode::String'))
1208 { my $map= Unicode::Map8->new( $encoding);
1209 $string= $map->tou( $string)->utf8;
1210 }
1211 $string=~ s{[\x00-\x08\x0B\x0C\x0E-\x1F]}{}g; # get rid of control chars
1212 return $string;
1213 }
1214 }
1215  
1216 sub _indent_xhtml
1217 { my( $xhtml)= @_; # $xhtml is a ref
1218 my %block_tag= map { $_ => 1 } qw( html
1219 head
1220 meta title link script base
1221 body
1222 h1 h2 h3 h4 h5 h6
1223 p br address blockquote pre
1224 ol ul li dd dl dt
1225 table tr td th tbody tfoot thead col colgroup caption
1226 div frame frameset hr
1227 );
1228  
1229 my $level=0;
1230 $$xhtml=~ s{( (?:<!(?:--.*?-->|[CDATA[.*?]]>)) # ignore comments and CDATA sections
1231 | <(\w+)((?:\s+\w+\s*=\s*(?:"[^"]*"|'[^']*'))*\s*/>) # empty tag
1232 | <(\w+) # start tag
1233 |</(\w+) # end tag
1234 )
1235 }
1236 { if( $2 && $block_tag{$2}) { my $indent= " " x $level;
1237 "\n$indent<$2$3";
1238 }
1239 elsif( $4 && $block_tag{$4}) { my $indent= " " x $level;
1240 $level++ unless( $4=~ m{/>});
1241 my $nl= $4 eq 'html' ? '' : "\n";
1242 "$nl$indent<$4";
1243 }
1244 elsif( $5 && $block_tag{$5}) { $level--; "</$5"; }
1245 else { $1; }
1246 }xesg;
1247 }
1248  
1249 sub add_stylesheet
1250 { my( $t, $type, $href)= @_;
1251 my %text_type= map { $_ => 1 } qw( xsl css);
1252 my $ss= $t->{twig_elt_class}->new( $PI);
1253 if( $text_type{$type})
1254 { $ss->_set_pi( 'xml-stylesheet', qq{type="text/$type" href="$href"}); }
1255 else
1256 { croak "unsupported style sheet type '$type'"; }
1257  
1258 $t->_add_cpi_outside_of_root( leading_cpi => $ss);
1259 return $t;
1260 }
1261  
1262 { my %used; # module => 1 if require ok, 0 otherwise
1263 my %disallowed; # for testing, refuses to _use modules in this hash
1264  
1265 sub _disallow_use ## no critic (Subroutines::ProhibitNestedSubs);
1266 { my( @modules)= @_;
1267 $disallowed{$_}= 1 foreach (@modules);
1268 }
1269  
1270 sub _allow_use ## no critic (Subroutines::ProhibitNestedSubs);
1271 { my( @modules)= @_;
1272 $disallowed{$_}= 0 foreach (@modules);
1273 }
1274  
1275 sub _use ## no critic (Subroutines::ProhibitNestedSubs);
1276 { my( $module, $version)= @_;
1277 $version ||= 0;
1278 if( $disallowed{$module}) { return 0; }
1279 if( $used{$module}) { return 1; }
1280 if( eval "require $module") { import $module; $used{$module}= 1; ## no critic ProhibitStringyEval
1281 if( $version)
1282 {
1283 ## no critic (TestingAndDebugging::ProhibitNoStrict);
1284 no strict 'refs';
1285 if( ${"${module}::VERSION"} >= $version ) { return 1; }
1286 else { return 0; }
1287 }
1288 else
1289 { return 1; }
1290 }
1291 else { $used{$module}= 0; return 0; }
1292 }
1293 }
1294  
1295 # used to solve the [n] predicates while avoiding getting the entire list
1296 # needs a prototype to accept passing bare blocks
1297 sub _first_n(&$@) ## no critic (Subroutines::ProhibitSubroutinePrototypes);
1298 { my $coderef= shift;
1299 my $n= shift;
1300 my $i=0;
1301 if( $n > 0)
1302 { foreach (@_) { if( &$coderef) { $i++; return $_ if( $i == $n); } } }
1303 elsif( $n < 0)
1304 { foreach (reverse @_) { if( &$coderef) { $i--; return $_ if( $i == $n); } } }
1305 else
1306 { croak "illegal position number 0"; }
1307 return undef;
1308 }
1309  
1310 sub _slurp_uri
1311 { my( $uri, $base)= @_;
1312 if( $uri=~ m{^\w+://}) { _use( 'LWP::Simple'); return LWP::Simple::get( $uri); }
1313 else { return _slurp( _based_filename( $uri, $base)); }
1314 }
1315  
1316 sub _based_filename
1317 { my( $filename, $base)= @_;
1318 # cf. XML/Parser.pm's file_ext_ent_handler
1319 if (defined($base) and not ($filename =~ m{^(?:[\\/]|\w+:)}))
1320 { my $newpath = $base;
1321 $newpath =~ s{[^\\/:]*$}{$filename};
1322 $filename = $newpath;
1323 }
1324 return $filename;
1325 }
1326  
1327 sub _slurp
1328 { my( $filename)= @_;
1329 my $to_slurp;
1330 open( $to_slurp, "<$filename") or croak "cannot open '$filename': $!";
1331 local $/= undef;
1332 my $content= <$to_slurp>;
1333 close $to_slurp;
1334 return $content;
1335 }
1336  
1337 sub _slurp_fh
1338 { my( $fh)= @_;
1339 local $/= undef;
1340 my $content= <$fh>;
1341 return $content;
1342 }
1343  
1344 # I should really add extra options to allow better configuration of the
1345 # LWP::UserAgent object
1346 # this method forks (except on VMS!)
1347 # - the child gets the data and copies it to the pipe,
1348 # - the parent reads the stream and sends it to XML::Parser
1349 # the data is cut it chunks the size of the XML::Parser::Expat buffer
1350 # the method returns the twig and the status
1351 sub _parseurl
1352 { my( $t, $safe, $url, $agent)= @_;
1353 _use( 'LWP') || croak "LWP not available, needed to use parseurl methods";
1354 if( $^O ne 'VMS')
1355 { pipe( README, WRITEME) or croak "cannot create connected pipes: $!";
1356 if( my $pid= fork)
1357 { # parent code: parse the incoming file
1358 close WRITEME; # no need to write
1359 my $result= $safe ? $t->safe_parse( \*README) : $t->parse( \*README);
1360 close README;
1361 return $@ ? 0 : $t;
1362 }
1363 else
1364 { # child
1365 close README; # no need to read
1366 local $|=1;
1367 $agent ||= LWP::UserAgent->new;
1368 my $request = HTTP::Request->new( GET => $url);
1369 # _pass_url_content is called with chunks of data the same size as
1370 # the XML::Parser buffer
1371 my $response = $agent->request( $request,
1372 sub { _pass_url_content( \*WRITEME, @_); }, $BUFSIZE);
1373 $response->is_success or croak "$url ", $response->message;
1374 close WRITEME;
1375 CORE::exit(); # CORE is there for mod_perl (which redefines exit)
1376 }
1377 }
1378 else
1379 { # VMS branch (hard to test!)
1380 local $|=1;
1381 $agent ||= LWP::UserAgent->new;
1382 my $request = HTTP::Request->new( GET => $url);
1383 my $response = $agent->request( $request);
1384 $response->is_success or croak "$url ", $response->message;
1385 my $result= $safe ? $t->safe_parse($response->content) : $t->parse($response->content);
1386 return $@ ? 0 : $t;
1387 }
1388  
1389 }
1390  
1391 # get the (hopefully!) XML data from the URL and
1392 sub _pass_url_content
1393 { my( $fh, $data, $response, $protocol)= @_;
1394 print {$fh} $data;
1395 }
1396  
1397 sub add_options
1398 { my %args= map { $_, 1 } @_;
1399 %args= _normalize_args( %args);
1400 foreach (keys %args) { $valid_option{$_}++; }
1401 }
1402  
1403 sub _pretty_print_styles { return XML::Twig::Elt::_pretty_print_styles(); }
1404  
1405 sub _twig_store_internal_dtd
1406 {
1407 # warn " in _twig_store_internal_dtd...\n"; # DEBUG handler
1408 my( $p, $string)= @_;
1409 my $t= $p->{twig};
1410 if( $t->{twig_keep_encoding}) { $string= $p->original_string(); }
1411 $t->{twig_doctype}->{internal} .= $string;
1412 return;
1413 }
1414  
1415 sub _twig_stop_storing_internal_dtd
1416 { # warn " in _twig_stop_storing_internal_dtd...\n"; # DEBUG handler
1417 my $p= shift;
1418 if( @saved_default_handler && defined $saved_default_handler[1])
1419 { $p->setHandlers( @saved_default_handler); }
1420 else
1421 {
1422 $p->setHandlers( Default => undef);
1423 }
1424 $p->{twig}->{twig_doctype}->{internal}=~ s{^\s*\[}{};
1425 $p->{twig}->{twig_doctype}->{internal}=~ s{\]\s*$}{};
1426 return;
1427 }
1428  
1429 sub _twig_doctype_fin_print
1430 { # warn " in _twig_doctype_fin_print...\n"; # DEBUG handler
1431 my( $p)= shift;
1432 if( $p->{twig}->{twig_doctype}->{has_internal} && !$expat_1_95_2) { print ' ]>'; }
1433 return;
1434 }
1435  
1436 sub _normalize_args
1437 { my %normalized_args;
1438 while( my $key= shift )
1439 { $key= join '', map { ucfirst } split /_/, $key;
1440 #$key= "Twig".$key unless( substr( $key, 0, 4) eq 'Twig');
1441 $normalized_args{$key}= shift ;
1442 }
1443 return %normalized_args;
1444 }
1445  
1446 sub _is_fh { return unless $_[0]; return $_[0] if( isa( $_[0], 'GLOB') || isa( $_[0], 'IO::Scalar')); }
1447  
1448 sub _set_handler
1449 { my( $handlers, $whole_path, $handler)= @_;
1450  
1451 my $H_SPECIAL = qr{($ALL|$DEFAULT|$COMMENT|$TEXT)};
1452 my $H_PI = qr{(\?|$PI)\s*(([^\s]*)\s*)};
1453 my $H_LEVEL = qr{level \s* \( \s* ([0-9]+) \s* \)}x;
1454 my $H_REGEXP = qr{\(\?([\^xism]*)(-[\^xism]*)?:(.*)\)}x;
1455 my $H_XPATH = qr{(/?/?$REG_TAG_PART? \s* ($REG_PREDICATE\s*)?)+}x;
1456  
1457 my $prev_handler;
1458  
1459 my $cpath= $whole_path;
1460 #warn "\$cpath: '$cpath\n";
1461 while( $cpath && $cpath=~ s{^\s*($H_SPECIAL|$H_PI|$H_LEVEL|$H_REGEXP|$H_XPATH)\s*($|\|)}{})
1462 { my $path= $1;
1463 #warn "\$cpath: '$cpath' - $path: '$path'\n";
1464 $prev_handler ||= $handlers->{handlers}->{string}->{$path} || undef; # $prev_handler gets the first found handler
1465  
1466 _set_special_handler ( $handlers, $path, $handler, $prev_handler)
1467 || _set_pi_handler ( $handlers, $path, $handler, $prev_handler)
1468 || _set_level_handler ( $handlers, $path, $handler, $prev_handler)
1469 || _set_regexp_handler ( $handlers, $path, $handler, $prev_handler)
1470 || _set_xpath_handler ( $handlers, $path, $handler, $prev_handler)
1471 || croak "unrecognized expression in handler: '$whole_path'";
1472  
1473 # this both takes care of the simple (gi) handlers and store
1474 # the handler code reference for other handlers
1475 $handlers->{handlers}->{string}->{$path}= $handler;
1476 }
1477  
1478 if( $cpath) { croak "unrecognized expression in handler: '$whole_path'"; }
1479  
1480 return $prev_handler;
1481 }
1482  
1483 sub _set_special_handler
1484 { my( $handlers, $path, $handler, $prev_handler)= @_;
1485 if( $path =~ m{^\s*($ALL|$DEFAULT|$COMMENT|$TEXT)\s*$}io )
1486 { $handlers->{handlers}->{$1}= $handler;
1487 return 1;
1488 }
1489 else
1490 { return 0; }
1491 }
1492  
1493 sub _set_xpath_handler
1494 { my( $handlers, $path, $handler, $prev_handler)= @_;
1495 if( my $handler_data= _parse_xpath_handler( $path, $handler))
1496 { _add_handler( $handlers, $handler_data, $path, $prev_handler);
1497 return 1;
1498 }
1499 else
1500 { return 0; }
1501 }
1502  
1503 sub _add_handler
1504 { my( $handlers, $handler_data, $path, $prev_handler)= @_;
1505  
1506 my $tag= $handler_data->{tag};
1507 my @handlers= $handlers->{xpath_handler}->{$tag} ? @{$handlers->{xpath_handler}->{$tag}} : ();
1508  
1509 if( $prev_handler) { @handlers= grep { $_->{path} ne $path } @handlers; }
1510  
1511 push @handlers, $handler_data if( $handler_data->{handler});
1512  
1513 if( @handlers > 1)
1514 { @handlers= sort { (($b->{score}->{type} || 0) <=> ($a->{score}->{type} || 0))
1515 || (($b->{score}->{anchored} || 0) <=> ($a->{score}->{anchored} || 0))
1516 || (($b->{score}->{steps} || 0) <=> ($a->{score}->{steps} || 0))
1517 || (($b->{score}->{predicates} || 0) <=> ($a->{score}->{predicates} || 0))
1518 || (($b->{score}->{tests} || 0) <=> ($a->{score}->{tests} || 0))
1519 || ($a->{path} cmp $b->{path})
1520 } @handlers;
1521 }
1522  
1523 $handlers->{xpath_handler}->{$tag}= \@handlers;
1524 }
1525  
1526 sub _set_pi_handler
1527 { my( $handlers, $path, $handler, $prev_handler)= @_;
1528 # PI conditions ( '?target' => \&handler or '?' => \&handler
1529 # or '#PItarget' => \&handler or '#PI' => \&handler)
1530 if( $path=~ /^\s*(?:\?|$PI)\s*(?:([^\s]*)\s*)$/)
1531 { my $target= $1 || '';
1532 # update the path_handlers count, knowing that
1533 # either the previous or the new handler can be undef
1534 $handlers->{pi_handlers}->{$1}= $handler;
1535 return 1;
1536 }
1537 else
1538 { return 0;
1539 }
1540 }
1541  
1542 sub _set_level_handler
1543 { my( $handlers, $path, $handler, $prev_handler)= @_;
1544 if( $path =~ m{^ \s* level \s* \( \s* ([0-9]+) \s* \) \s* $}ox )
1545 { my $level= $1;
1546 my $sub= sub { my( $stack)= @_; return( ($stack->[-1]->{$ST_TAG} !~ m{^#}) && (scalar @$stack == $level + 1) ) };
1547 my $handler_data= { tag=> '*', score => { type => $LEVEL_TRIGGER}, trigger => $sub,
1548 path => $path, handler => $handler, test_on_text => 0
1549 };
1550 _add_handler( $handlers, $handler_data, $path, $prev_handler);
1551 return 1;
1552 }
1553 else
1554 { return 0; }
1555 }
1556  
1557 sub _set_regexp_handler
1558 { my( $handlers, $path, $handler, $prev_handler)= @_;
1559 # if the expression was a regexp it is now a string (it was stringified when it became a hash key)
1560 if( $path=~ m{^\(\?([\^xism]*)(?:-[\^xism]*)?:(.*)\)$})
1561 { my $regexp= qr/(?$1:$2)/; # convert it back into a regexp
1562 my $sub= sub { my( $stack)= @_; return( $stack->[-1]->{$ST_TAG} =~ $regexp ) };
1563 my $handler_data= { tag=> '*', score => { type => $REGEXP_TRIGGER} , trigger => $sub,
1564 path => $path, handler => $handler, test_on_text => 0
1565 };
1566 _add_handler( $handlers, $handler_data, $path, $prev_handler);
1567 return 1;
1568 }
1569 else
1570 { return 0; }
1571 }
1572  
1573 $DEBUG_HANDLER= 0; # 0 or 1 (output the handler checking code) or 2 (super verbose)
1574 my $handler_string; # store the handler itself
1575 sub _set_debug_handler { $DEBUG_HANDLER= shift; }
1576 sub _warn_debug_handler { if( $DEBUG_HANDLER < 3) { warn @_; } else { $handler_string .= join( '', @_); } }
1577 sub _return_debug_handler { my $string= $handler_string; $handler_string=''; return $string; }
1578  
1579 sub _parse_xpath_handler
1580 { my( $xpath, $handler)= @_;
1581 my $xpath_original= $xpath;
1582  
1583 if( $DEBUG_HANDLER >=1) { _warn_debug_handler( "\n\nparsing path '$xpath'\n"); }
1584  
1585 my $path_to_check= $xpath;
1586 $path_to_check=~ s{/?/?$REG_TAG_PART?\s*(?:$REG_PREDICATE\s*)?}{}g;
1587 if( $DEBUG_HANDLER && $path_to_check=~ /\S/) { _warn_debug_handler( "left: $path_to_check\n"); }
1588 return if( $path_to_check=~ /\S/);
1589  
1590 (my $xpath_to_display= $xpath)=~ s{(["{}'\[\]\@\$])}{\\$1}g;
1591  
1592 my @xpath_steps;
1593 my $last_token_is_sep;
1594  
1595 while( $xpath=~ s{^\s*
1596 ( (//?) # separator
1597 | (?:$REG_TAG_PART\s*(?:$REG_PREDICATE\s*)?) # tag name and optional predicate
1598 | (?:$REG_PREDICATE) # just a predicate
1599 )
1600 }
1601 {}x
1602 )
1603 { # check that we have alternating separators and steps
1604 if( $2) # found a separator
1605 { if( $last_token_is_sep) { return 0; } # 2 separators in a row
1606 $last_token_is_sep= 1;
1607 }
1608 else
1609 { if( defined( $last_token_is_sep) && !$last_token_is_sep) { return 0; } # 2 steps in a row
1610 $last_token_is_sep= 0;
1611 }
1612  
1613 push @xpath_steps, $1;
1614 }
1615 if( $last_token_is_sep) { return 0; } # expression cannot end with a separator
1616  
1617 my $i=-1;
1618  
1619 my $perlfunc= _join_n( $NO_WARNINGS . ';',
1620 q|my( $stack)= @_; |,
1621 q|my @current_elts= (scalar @$stack); |,
1622 q|my @new_current_elts; |,
1623 q|my $elt; |,
1624 ($DEBUG_HANDLER >= 1) && (qq#warn q{checking path '$xpath_to_display'\n};#),
1625 );
1626  
1627 my $last_tag='';
1628 my $anchored= $xpath_original=~ m{^\s*/(?!/)} ? 1 : 0;
1629 my $score={ type => $XPATH_TRIGGER, anchored => $anchored };
1630 my $flag= { test_on_text => 0 };
1631 my $sep='/'; # '/' or '//'
1632 while( my $xpath_step= pop @xpath_steps)
1633 { my( $tag, $predicate)= $xpath_step =~ m{^($REG_TAG_PART)?(?:\[(.*)\])?\s*$};
1634 $score->{steps}++;
1635 $tag||='*';
1636  
1637 my $warn_empty_stack= $DEBUG_HANDLER >= 2 ? qq{warn "return with empty stack\\n";} : '';
1638  
1639 if( $predicate)
1640 { if( $DEBUG_HANDLER >= 2) { _warn_debug_handler( "predicate is: '$predicate'\n"); }
1641 # changes $predicate (from an XPath expression to a Perl one)
1642 if( $predicate=~ m{^\s*$REG_NUMBER\s*$}) { croak "position selector [$predicate] not supported on twig_handlers"; }
1643 _parse_predicate_in_handler( $predicate, $flag, $score);
1644 if( $DEBUG_HANDLER >= 2) { _warn_debug_handler( "predicate becomes: '$predicate'\n"); }
1645 }
1646  
1647 my $tag_cond= _tag_cond( $tag);
1648 my $cond= join( " && ", grep { $_ } $tag_cond, $predicate) || 1;
1649  
1650 if( $css_sel && $tag=~ m{\.}) { $tag=~s{\..*$}{}; $tag ||='*'; }
1651 $tag=~ s{(.)#.+$}{$1};
1652  
1653 $last_tag ||= $tag;
1654  
1655 if( $sep eq '/')
1656 {
1657 $perlfunc .= sprintf( _join_n( q#foreach my $current_elt (@current_elts) #,
1658 q# { next if( !$current_elt); #,
1659 q# $current_elt--; #,
1660 q# $elt= $stack->[$current_elt]; #,
1661 q# if( %s) { push @new_current_elts, $current_elt;} #,
1662 q# } #,
1663 ),
1664 $cond
1665 );
1666 }
1667 elsif( $sep eq '//')
1668 {
1669 $perlfunc .= sprintf( _join_n( q#foreach my $current_elt (@current_elts) #,
1670 q# { next if( !$current_elt); #,
1671 q# $current_elt--; #,
1672 q# my $candidate= $current_elt; #,
1673 q# while( $candidate >=0) #,
1674 q# { $elt= $stack->[$candidate]; #,
1675 q# if( %s) { push @new_current_elts, $candidate;} #,
1676 q# $candidate--; #,
1677 q# } #,
1678 q# } #,
1679 ),
1680 $cond
1681 );
1682 }
1683 my $warn= $DEBUG_HANDLER >= 2 ? _join_n( qq#warn qq%fail at cond '$cond'%;#) : '';
1684 $perlfunc .= sprintf( _join_n( q#unless( @new_current_elts) { %s return 0; } #,
1685 q#@current_elts= @new_current_elts; #,
1686 q#@new_current_elts=(); #,
1687 ),
1688 $warn
1689 );
1690  
1691 $sep= pop @xpath_steps;
1692 }
1693  
1694 if( $anchored) # there should be a better way, but this works
1695 {
1696 my $warn= $DEBUG_HANDLER >= 2 ? _join_n( qq#warn qq{fail, stack not empty};#) : '';
1697 $perlfunc .= sprintf( _join_n( q#if( ! grep { $_ == 0 } @current_elts) { %s return 0;}#), $warn);
1698 }
1699  
1700 $perlfunc.= qq{warn "handler for '$xpath_to_display' triggered\\n";\n} if( $DEBUG_HANDLER >=2);
1701 $perlfunc.= qq{return q{$xpath_original};\n};
1702 _warn_debug_handler( "\nperlfunc:\n$perlfunc\n") if( $DEBUG_HANDLER>=1);
1703 my $s= eval "sub { $perlfunc }"; ## no critic (ProhibitStringyEval)
1704 if( $@)
1705 { croak "wrong handler condition '$xpath' ($@);" }
1706  
1707 _warn_debug_handler( "last tag: '$last_tag', test_on_text: '$flag->{test_on_text}'\n") if( $DEBUG_HANDLER >=1);
1708 _warn_debug_handler( "score: ", join( ' ', map { "$_: $score->{$_}" } sort keys %$score), "\n") if( $DEBUG_HANDLER >=1);
1709 return { tag=> $last_tag, score => $score, trigger => $s, path => $xpath_original, handler => $handler, test_on_text => $flag->{test_on_text} };
1710 }
1711  
1712 sub _join_n { return join( "\n", @_, ''); }
1713  
1714 # the "tag" part can be <tag>, <tag>.<class> or <tag>#<id> (where tag can be *, or start with # for hidden tags)
1715 sub _tag_cond
1716 { my( $full_tag)= @_;
1717  
1718 my( $tag, $class, $id);
1719 if( $full_tag=~ m{^(.+)#(.+)$})
1720 { ($tag, $id)= ($1, $2); } # <tag>#<id>
1721 else
1722 { ( $tag, $class)= $css_sel ? $full_tag=~ m{^(.*?)(?:\.(.*))?$} : ($full_tag, undef); }
1723  
1724 my $tag_cond = $tag && $tag ne '*' ? qq#(\$elt->{'$ST_TAG'} eq "$tag")# : '';
1725 my $id_cond = defined $id ? qq#(\$elt->{id} eq "$id")# : '';
1726  
1727 my $class_cond;
1728 if( defined $class ) {
1729 my @classes = split /\./, $class;
1730 $class_cond = '(' . join( ' && ', map { qq#(\$elt->{class}=~ m{(^| )$_( |\$)})# } @classes) . ')';
1731 }
1732  
1733 my $full_cond= join( ' && ', grep { $_ } ( $tag_cond, $class_cond, $id_cond));
1734  
1735 # warn "full_cond: [$full_cond]\n";
1736  
1737 return $full_cond;
1738 }
1739  
1740 # input: the predicate ($_[0]) which will be changed in place
1741 # flags, a hashref with various flags (like test_on_text)
1742 # the score
1743 sub _parse_predicate_in_handler
1744 { my( $flag, $score)= @_[1..2];
1745 $_[0]=~ s{(?<token>
1746 (?<tstr>$REG_STRING) # quoted string
1747 |(?<tregexp>$REG_REGEXP) # regexp
1748 |(?<tatt_re>\@(?<att>$REG_TAG_NAME) \s* (?<op>$REG_MATCH) \s* (?<re>$REG_REGEXP)) # @att and regexp
1749 |(?<tatt_num>\@(?<att>$REG_TAG_NAME) \s* (?<op>$REG_COMP) \s* (?<nb>$REG_NUMBER)) # @att then comparison operator then number
1750 |(?<tatt_op>\@(?<att>$REG_TAG_NAME)(?=\s*(?:[><=!]))) # @att followed by a comparison operator
1751 |(?<tbare_att>\@(?<att>$REG_TAG_NAME)) # @att (not followed by a comparison operator)
1752 |=~|!~ # matching operators
1753 |(?<tbare_num_test>[><]=?|=|!=)(?=\s*[\d+-]) # test before a number
1754 |(?<tbare_test>[><]=?|=|!=) # test, other cases
1755 # this bit is a mess, but it is the only solution with this half-baked parser
1756 |(?<tstr_re>string\(\s*(?<tag>$REG_NAME?)\s*\)\s*(?<op>$REG_MATCH)\s*(?<re>$REG_REGEXP)) # string( child) =~ /regexp/ or string()=~ /regexp/
1757 |(?<tstr_str>string\(\s*(?<tag>$REG_NAME?)\s*\)\s*(?<op>$REG_COMP)\s*(?<string>$REG_STRING)) # string( child) = "value" (or other test)
1758 |(?<tstr_num>string\(\s*(?<tag>$REG_NAME?)\s*\)\s*(?<op>$REG_COMP)\s*(?<nb>$REG_NUMBER)) # string( child) = nb (or other test)
1759 |(?<tfunc>$REG_FUNCTION) # no arg functions
1760 |(?<tand_or>\band\b|\bor\b)
1761 |(?<tntag>$REG_TAG_IN_PREDICATE) # nested tag name (needs to be after all other unquoted strings)
1762  
1763 )}
1764 {
1765 $score->{predicates}++;
1766 my %m = %+; # save the matches, so further regexps don't overwrite it
1767  
1768 # store tests on text (they are not always allowed)
1769 if( $m{tfunc} || $m{tstr_re} || $m{tstr_num} || $m{tstr_str} ) { $flag->{test_on_text}= 1; }
1770  
1771 my $token = $m{token};
1772 if( defined $m{tstr} ) { _safe_string( $token) }
1773 elsif( defined $m{tregexp} ){ _safe_regexp( $token) }
1774 elsif( $m{tntag} ) { qq{(\$elt->{'$ST_ELT'} && \$elt->{'$ST_ELT'}->has_child( '$m{tntag}'))} }
1775 elsif( $m{tatt_op} ) { _att_is_private($m{att})
1776 ? qq{ (\$elt->{'$ST_ELT'} && \$elt->{'$ST_ELT'}->{att}->{'$m{att}'})}
1777 : qq{\$elt->{'$m{att}'}}
1778 }
1779 elsif( $m{tatt_re} ) { _att_is_private($m{att})
1780 ? qq{ (\$elt->{'$ST_ELT'} && \$elt->{'$ST_ELT'}->{att}->{'$m{att}'} $m{op} $m{re})}
1781 : qq{\$elt->{'$m{att}'} $m{op} $m{re}}
1782 }
1783 elsif( $m{tatt_num} ) { my $op = $m{op} eq '=' ? '==' : $m{op};
1784 # the context stack stores private atts, set in an other handler
1785 my $context_stack = qq{\$elt\->{'$ST_ELT'}};
1786 # look for private atts in the context stack, other atts in the plain parse result
1787 my $att_location = _att_is_private($m{att}) ? qq{$context_stack\->{att}} : qq{\$elt};
1788 my $att= ${att_location} . qq{->{'$m{att}'}};
1789 my @tests;
1790 # for private atts, check existence of the context stack so we don't autovivify it
1791 push @tests, $context_stack if _att_is_private($m{att});
1792 # the base test
1793 push @tests, qq{$att $op $m{nb}};
1794 join ' && ', @tests;
1795 }
1796 # for some reason Devel::Cover flags the following lines as not tested. They are though.
1797 elsif( $m{tbare_att} ) { _att_is_private($m{att})
1798 ? qq{(\$elt->{'$ST_ELT'} && defined(\$elt->{'$ST_ELT'}->{att}->{'$m{att}'}))}
1799 : qq{defined( \$elt->{'$m{att}'})}
1800 }
1801 elsif( $m{tbare_num_test }&& ($m{tbare_num_test} eq '=') )
1802 { "==" } # others tests are unchanged
1803 elsif( $m{tbare_test} ) { $PERL_ALPHA_TEST{$m{tbare_test}} }
1804 elsif( $m{tfunc}&& $m{tfunc}=~ m{^string} )
1805 { "\$elt->{'$ST_ELT'}->text"; }
1806 elsif( $m{tstr_re} ) { my $tag = $m{tag} // '';
1807 my $op = $m{op};
1808 my( $regexp)= _safe_regexp( $m{re});
1809 if( $m{tag} ) { "defined( first { \$_->text $m{op} $regexp } \$elt->{'$ST_ELT'}->_children( '$m{tag}'))" }
1810 else { "\$elt->{'$ST_ELT'}->text $m{op} $regexp" }
1811 }
1812 elsif( $m{tstr_str} ) { my $tag = $m{tag};
1813 my $op= $PERL_ALPHA_TEST{$m{op}};
1814 my $str= substr( $m{string}, 1, -1 ); # remove quotes
1815 $str= _safe_string($str);
1816 $str=~ s{(?<=.)'(?=.)}{\\'}g; # escape a quote within the string
1817 $str= qq{'$str'};
1818 if ( $tag ) { "defined( first { \$_->text $op $str } \$elt->{'$ST_ELT'}->children( '$tag'))"; }
1819 else { "\$elt->{'$ST_ELT'}->text $op $str"; }
1820 }
1821 elsif( $m{tstr_num } ) { $m{tag }//= '';
1822 my $op = $m{op} eq '=' ? '==' : $m{op};
1823 if ( $m{tag} )
1824 { "defined( first { (\$_->text $op $m{nb}) } \$elt->{'$ST_ELT'}->children( '$m{tag}'))"; }
1825 else
1826 { "\$elt->{'$ST_ELT'}->text $op $m{nb}" }
1827 }
1828 elsif( $m{tand_or} ) { $score->{tests}++; $m{tand_or} eq 'and' ? '&&' : '||' ; }
1829 else { $token; }
1830 }gexs;
1831 }
1832  
1833 sub _safe_string
1834 { my( $string)= @_;
1835 $string=~ s{([\@\$]\s*\{\s*\[)}{\\$1}g;
1836 return $string;
1837 }
1838  
1839 sub _safe_regexp
1840 { my( $regexp)= @_;
1841 if( $regexp=~ m{\(\s*\?\s*\{}) { s{(\([()])}{\\$1}g; } # if '(?{' (code in a regexp) then escape all '(' and ')'
1842 return $regexp;
1843 }
1844  
1845 # no regexp in there so we can still use %+ in the following code
1846 sub _att_is_private {
1847 my ($att) = @_;
1848 return substr( $att, 0, 1 ) eq '#';
1849 }
1850  
1851 sub setCharHandler
1852 { my( $t, $handler)= @_;
1853 $t->{twig_char_handler}= $handler;
1854 }
1855  
1856 sub _reset_handlers
1857 { my $handlers= shift;
1858 delete $handlers->{handlers};
1859 delete $handlers->{path_handlers};
1860 delete $handlers->{subpath_handlers};
1861 $handlers->{attcond_handlers_exp}=[] if( $handlers->{attcond_handlers});
1862 delete $handlers->{attcond_handlers};
1863 }
1864  
1865 sub _set_handlers
1866 { my $handlers= shift || return;
1867 my $set_handlers= {};
1868 foreach my $path (keys %{$handlers})
1869 { _set_handler( $set_handlers, $path, $handlers->{$path}); }
1870  
1871 return $set_handlers;
1872 }
1873  
1874 sub setTwigHandler
1875 { my( $t, $path, $handler)= @_;
1876 $t->{twig_handlers} ||={};
1877 return _set_handler( $t->{twig_handlers}, $path, $handler);
1878 }
1879  
1880 sub setTwigHandlers
1881 { my( $t, $handlers)= @_;
1882 my $previous_handlers= $t->{twig_handlers} || undef;
1883 _reset_handlers( $t->{twig_handlers});
1884 $t->{twig_handlers}= _set_handlers( $handlers);
1885 return $previous_handlers;
1886 }
1887  
1888 sub setStartTagHandler
1889 { my( $t, $path, $handler)= @_;
1890 $t->{twig_starttag_handlers}||={};
1891 return _set_handler( $t->{twig_starttag_handlers}, $path, $handler);
1892 }
1893  
1894 sub setStartTagHandlers
1895 { my( $t, $handlers)= @_;
1896 my $previous_handlers= $t->{twig_starttag_handlers} || undef;
1897 _reset_handlers( $t->{twig_starttag_handlers});
1898 $t->{twig_starttag_handlers}= _set_handlers( $handlers);
1899 return $previous_handlers;
1900 }
1901  
1902 sub setIgnoreEltsHandler
1903 { my( $t, $path, $action)= @_;
1904 $t->{twig_ignore_elts_handlers}||={};
1905 return _set_handler( $t->{twig_ignore_elts_handlers}, $path, $action );
1906 }
1907  
1908 sub setIgnoreEltsHandlers
1909 { my( $t, $handlers)= @_;
1910 my $previous_handlers= $t->{twig_ignore_elts_handlers};
1911 _reset_handlers( $t->{twig_ignore_elts_handlers});
1912 $t->{twig_ignore_elts_handlers}= _set_handlers( $handlers);
1913 return $previous_handlers;
1914 }
1915  
1916 sub setEndTagHandler
1917 { my( $t, $path, $handler)= @_;
1918 $t->{twig_endtag_handlers}||={};
1919 return _set_handler( $t->{twig_endtag_handlers}, $path,$handler);
1920 }
1921  
1922 sub setEndTagHandlers
1923 { my( $t, $handlers)= @_;
1924 my $previous_handlers= $t->{twig_endtag_handlers};
1925 _reset_handlers( $t->{twig_endtag_handlers});
1926 $t->{twig_endtag_handlers}= _set_handlers( $handlers);
1927 return $previous_handlers;
1928 }
1929  
1930 # a little more complex: set the twig_handlers only if a code ref is given
1931 sub setTwigRoots
1932 { my( $t, $handlers)= @_;
1933 my $previous_roots= $t->{twig_roots};
1934 _reset_handlers($t->{twig_roots});
1935 $t->{twig_roots}= _set_handlers( $handlers);
1936  
1937 _check_illegal_twig_roots_handlers( $t->{twig_roots});
1938  
1939 foreach my $path (keys %{$handlers})
1940 { $t->{twig_handlers}||= {};
1941 _set_handler( $t->{twig_handlers}, $path, $handlers->{$path})
1942 if( ref($handlers->{$path}) && isa( $handlers->{$path}, 'CODE'));
1943 }
1944 return $previous_roots;
1945 }
1946  
1947 sub _check_illegal_twig_roots_handlers
1948 { my( $handlers)= @_;
1949 foreach my $tag_handlers (values %{$handlers->{xpath_handler}})
1950 { foreach my $handler_data (@$tag_handlers)
1951 { if( my $type= $handler_data->{test_on_text})
1952 { croak "string() condition not supported on twig_roots option"; }
1953 }
1954 }
1955 return;
1956 }
1957  
1958 # just store the reference to the expat object in the twig
1959 sub _twig_init
1960 { # warn " in _twig_init...\n"; # DEBUG handler
1961  
1962 my $p= shift;
1963 my $t=$p->{twig};
1964  
1965 if( $t->{twig_parsing} ) { croak "cannot reuse a twig that is already parsing"; }
1966 $t->{twig_parsing}=1;
1967  
1968 $t->{twig_parser}= $p;
1969 weaken( $t->{twig_parser});
1970  
1971 # in case they had been created by a previous parse
1972 delete $t->{twig_dtd};
1973 delete $t->{twig_doctype};
1974 delete $t->{twig_xmldecl};
1975 delete $t->{twig_root};
1976  
1977 # if needed set the output filehandle
1978 $t->_set_fh_to_twig_output_fh();
1979 return;
1980 }
1981  
1982 # uses eval to catch the parser's death
1983 sub safe_parse
1984 { my $t= shift;
1985 eval { $t->parse( @_); } ;
1986 return $@ ? $t->_reset_twig_after_error : $t;
1987 }
1988  
1989 sub safe_parsefile
1990 { my $t= shift;
1991 eval { $t->parsefile( @_); } ;
1992 return $@ ? $t->_reset_twig_after_error : $t;
1993 }
1994  
1995 # restore a twig in a proper state so it can be reused for a new parse
1996 sub _reset_twig
1997 { my $t= shift;
1998 $t->{twig_parsing}= 0;
1999 delete $t->{twig_current};
2000 delete $t->{extra_data};
2001 delete $t->{twig_dtd};
2002 delete $t->{twig_in_pcdata};
2003 delete $t->{twig_in_cdata};
2004 delete $t->{twig_stored_space};
2005 delete $t->{twig_entity_list};
2006 $t->root->delete if( $t->root);
2007 delete $t->{twig_root};
2008 return $t;
2009 }
2010  
2011 sub _reset_twig_after_error
2012 { my $t= shift;
2013 $t->_reset_twig;
2014 return undef;
2015 }
2016  
2017 sub _add_or_discard_stored_spaces
2018 { my $t= shift;
2019 $t->{twig_right_after_root}=0; #XX
2020  
2021 my $current= $t->{twig_current} or return; # ugly hack, with ignore on, twig_current can disappear
2022 return unless length $t->{twig_stored_spaces};
2023 my $current_gi= $XML::Twig::index2gi[$current->{'gi'}];
2024  
2025 if( ! $t->{twig_discard_all_spaces})
2026 { if( ! defined( $t->{twig_space_policy}->{$current_gi}))
2027 { $t->{twig_space_policy}->{$current_gi}= _space_policy( $t, $current_gi); }
2028 if( $t->{twig_space_policy}->{$current_gi} || ($t->{twig_stored_spaces}!~ m{\n}) || $t->{twig_preserve_space})
2029 { _insert_pcdata( $t, $t->{twig_stored_spaces} ); }
2030 }
2031  
2032 $t->{twig_stored_spaces}='';
2033  
2034 return;
2035 }
2036  
2037 # the default twig handlers, which build the tree
2038 sub _twig_start
2039 { # warn " in _twig_start...\n"; # DEBUG handler
2040  
2041 #foreach my $s (@_) { next if ref $s; warn "$s: ", is_utf8( $s) ? "has flag" : "FLAG NOT SET"; } # YYY
2042  
2043 my ($p, $gi, @att)= @_;
2044 my $t=$p->{twig};
2045  
2046 # empty the stored pcdata (space stored in case they are really part of
2047 # a pcdata element) or stored it if the space policy dictates so
2048 # create a pcdata element with the spaces if need be
2049 _add_or_discard_stored_spaces( $t);
2050 my $parent= $t->{twig_current};
2051  
2052 # if we were parsing PCDATA then we exit the pcdata
2053 if( $t->{twig_in_pcdata})
2054 { $t->{twig_in_pcdata}= 0;
2055 $t->_trigger_text_handler();
2056  
2057 delete $parent->{'twig_current'};
2058 $parent= $parent->{parent};
2059 }
2060  
2061 # if we choose to keep the encoding then we need to parse the tag
2062 if( my $func = $t->{parse_start_tag})
2063 { ($gi, @att)= &$func($p->original_string); }
2064 elsif( $t->{twig_entities_in_attribute})
2065 {
2066 ($gi,@att)= _parse_start_tag( $p->recognized_string);
2067 $t->{twig_entities_in_attribute}=0;
2068 }
2069  
2070 # if we are using an external DTD, we need to fill the default attributes
2071 if( $t->{twig_read_external_dtd}) { _fill_default_atts( $t, $gi, \@att); }
2072  
2073 # filter the input data if need be
2074 if( my $filter= $t->{twig_input_filter})
2075 { $gi= $filter->( $gi);
2076 foreach my $att (@att) { $att= $filter->($att); }
2077 }
2078  
2079 my $ns_decl;
2080 if( $t->{twig_map_xmlns})
2081 { $ns_decl= _replace_ns( $t, \$gi, \@att); }
2082  
2083 my $elt= $t->{twig_elt_class}->new( $gi);
2084 $elt->set_atts( @att);
2085  
2086 # now we can store the tag and atts
2087 my $context= { $ST_TAG => $gi, $ST_ELT => $elt, @att};
2088 $context->{$ST_NS}= $ns_decl if $ns_decl;
2089 weaken( $context->{$ST_ELT});
2090 push @{$t->{_twig_context_stack}}, $context;
2091  
2092 delete $parent->{'twig_current'} if( $parent);
2093 $t->{twig_current}= $elt;
2094 $elt->{'twig_current'}=1;
2095  
2096 if( $parent)
2097 { my $prev_sibling= $parent->{last_child};
2098 if( $prev_sibling)
2099 { $prev_sibling->{next_sibling}= $elt;
2100 $elt->{prev_sibling}=$prev_sibling; weaken( $elt->{prev_sibling}); ;
2101 }
2102  
2103 $elt->{parent}=$parent; weaken( $elt->{parent}); ;
2104 unless( $parent->{first_child}) { $parent->{first_child}= $elt; }
2105 delete $parent->{empty}; $parent->{last_child}=$elt; weaken( $parent->{last_child}); ;
2106 }
2107 else
2108 { # processing root
2109 $t->set_root( $elt);
2110 # call dtd handler if need be
2111 $t->{twig_dtd_handler}->($t, $t->{twig_dtd})
2112 if( defined $t->{twig_dtd_handler});
2113  
2114 # set this so we can catch external entities
2115 # (the handler was modified during DTD processing)
2116 if( $t->{twig_default_print})
2117 { $p->setHandlers( Default => \&_twig_print); }
2118 elsif( $t->{twig_roots})
2119 { $p->setHandlers( Default => sub { return }); }
2120 else
2121 { $p->setHandlers( Default => \&_twig_default); }
2122 }
2123  
2124 $elt->{empty}= $p->recognized_string=~ m{/\s*>$}s ? 1 : 0;
2125  
2126 $elt->{extra_data}= $t->{extra_data} if( $t->{extra_data});
2127 $t->{extra_data}='';
2128  
2129 # if the element is ID-ed then store that info
2130 my $id= $elt->{'att'}->{$ID};
2131 if( defined $id)
2132 { $t->{twig_id_list}->{$id}= $elt;
2133 weaken( $t->{twig_id_list}->{$id});
2134 }
2135  
2136 # call user handler if need be
2137 if( $t->{twig_starttag_handlers})
2138 { # call all appropriate handlers
2139 my @handlers= _handler( $t, $t->{twig_starttag_handlers}, $gi);
2140  
2141 local $_= $elt;
2142  
2143 foreach my $handler ( @handlers)
2144 { $handler->($t, $elt) || last; }
2145 # call _all_ handler if needed
2146 if( my $all= $t->{twig_starttag_handlers}->{handlers}->{$ALL})
2147 { $all->($t, $elt); }
2148 }
2149  
2150 # check if the tag is in the list of tags to be ignored
2151 if( $t->{twig_ignore_elts_handlers})
2152 { my @handlers= _handler( $t, $t->{twig_ignore_elts_handlers}, $gi);
2153 # only the first handler counts, it contains the action (discard/print/string)
2154 if( @handlers) { my $action= shift @handlers; $t->ignore( $elt, $action); }
2155 }
2156  
2157 if( $elt->{'att'}->{'xml:space'} && ( $elt->{'att'}->{'xml:space'} eq 'preserve')) { $t->{twig_preserve_space}++; }
2158  
2159 return;
2160 }
2161  
2162 sub _replace_ns
2163 { my( $t, $gi, $atts)= @_;
2164 my $decls;
2165 foreach my $new_prefix ( $t->parser->new_ns_prefixes)
2166 { my $uri= $t->parser->expand_ns_prefix( $new_prefix);
2167 # replace the prefix if it is mapped
2168 $decls->{$new_prefix}= $uri;
2169 if( !$t->{twig_keep_original_prefix} && (my $mapped_prefix= $t->{twig_map_xmlns}->{$uri}))
2170 { $new_prefix= $mapped_prefix; }
2171 # now put the namespace declaration back in the element
2172 if( $new_prefix eq '#default')
2173 { push @$atts, "xmlns" => $uri; }
2174 else
2175 { push @$atts, "xmlns:$new_prefix" => $uri; }
2176 }
2177  
2178 if( $t->{twig_keep_original_prefix})
2179 { # things become more complex: we need to find the original prefix
2180 # and store both prefixes
2181 my $ns_info= $t->_ns_info( $$gi);
2182 my $map_att;
2183 if( $ns_info->{mapped_prefix})
2184 { $$gi= "$ns_info->{mapped_prefix}:$$gi";
2185 $map_att->{$ns_info->{mapped_prefix}}= $ns_info->{prefix};
2186 }
2187 my $att_name=1;
2188 foreach( @$atts)
2189 { if( $att_name)
2190 {
2191 my $ns_info= $t->_ns_info( $_);
2192 if( $ns_info->{mapped_prefix})
2193 { $_= "$ns_info->{mapped_prefix}:$_";
2194 $map_att->{$ns_info->{mapped_prefix}}= $ns_info->{prefix};
2195 }
2196 $att_name=0;
2197 }
2198 else
2199 { $att_name=1; }
2200 }
2201 push @$atts, '#original_gi', $map_att if( $map_att);
2202 }
2203 else
2204 { $$gi= $t->_replace_prefix( $$gi);
2205 my $att_name=1;
2206 foreach( @$atts)
2207 { if( $att_name) { $_= $t->_replace_prefix( $_); $att_name=0; }
2208 else { $att_name=1; }
2209 }
2210 }
2211 return $decls;
2212 }
2213  
2214 # extract prefix, local_name, uri, mapped_prefix from a name
2215 # will only work if called from a start or end tag handler
2216 sub _ns_info
2217 { my( $t, $name)= @_;
2218 my $ns_info={};
2219 my $p= $t->parser;
2220 $ns_info->{uri}= $p->namespace( $name);
2221 return $ns_info unless( $ns_info->{uri});
2222  
2223 $ns_info->{prefix}= _a_proper_ns_prefix( $p, $ns_info->{uri});
2224 $ns_info->{mapped_prefix}= $t->{twig_map_xmlns}->{$ns_info->{uri}} || $ns_info->{prefix};
2225  
2226 return $ns_info;
2227 }
2228  
2229 sub _a_proper_ns_prefix
2230 { my( $p, $uri)= @_;
2231 foreach my $prefix ($p->current_ns_prefixes)
2232 { if( $p->expand_ns_prefix( $prefix) eq $uri)
2233 { return $prefix; }
2234 }
2235 return;
2236 }
2237  
2238 # returns the uri bound to a prefix in the original document
2239 # only works in a handler
2240 # can be used to deal with xsi:type attributes
2241 sub original_uri
2242 { my( $t, $prefix)= @_;
2243 my $ST_NS = '##ns' ;
2244 foreach my $ns (map { $_->{$ST_NS} if $_->{$ST_NS} } reverse @{$t->{_twig_context_stack}})
2245 { return $ns->{$prefix} || next; }
2246 return;
2247 }
2248  
2249 sub _fill_default_atts
2250 { my( $t, $gi, $atts)= @_;
2251 my $dtd= $t->{twig_dtd};
2252 my $attlist= $dtd->{att}->{$gi};
2253 my %value= @$atts;
2254 foreach my $att (keys %$attlist)
2255 { if( !exists( $value{$att})
2256 && exists( $attlist->{$att}->{default})
2257 && ( $attlist->{$att}->{default} ne '#IMPLIED')
2258 )
2259 { # the quotes are included in the default, so we need to remove them
2260 my $default_value= substr( $attlist->{$att}->{default}, 1, -1);
2261 push @$atts, $att, $default_value;
2262 }
2263 }
2264 return;
2265 }
2266  
2267 # the default function to parse a start tag (in keep_encoding mode)
2268 # can be overridden with the parse_start_tag method
2269 # only works for 1-byte character sets
2270 sub _parse_start_tag
2271 { my $string= shift;
2272 my( $gi, @atts);
2273  
2274 # get the gi (between < and the first space, / or > character)
2275 if( $string=~ s{^<\s*($REG_TAG_NAME)\s*[\s>/]}{}s)
2276 { $gi= $1; }
2277 else
2278 { croak "error parsing tag '$string'"; }
2279 while( $string=~ s{^([^\s=]*)\s*=\s*(["'])(.*?)\2\s*}{}s)
2280 { push @atts, $1, $3; }
2281 return $gi, @atts;
2282 }
2283  
2284 sub set_root
2285 { my( $t, $elt)= @_;
2286 $t->{twig_root}= $elt;
2287 if( $elt)
2288 { $elt->{twig}= $t;
2289 weaken( $elt->{twig});
2290 }
2291 return $t;
2292 }
2293  
2294 sub _twig_end
2295 { # warn " in _twig_end...\n"; # DEBUG handler
2296 my ($p, $gi) = @_;
2297  
2298 my $t=$p->{twig};
2299  
2300 if( $t->{twig_in_pcdata} )
2301 { $t->_trigger_text_handler(); }
2302  
2303 if( $t->{twig_map_xmlns}) { $gi= $t->_replace_prefix( $gi); }
2304  
2305 _add_or_discard_stored_spaces( $t);
2306  
2307 # the new twig_current is the parent
2308 my $elt= $t->{twig_current};
2309 delete $elt->{'twig_current'};
2310  
2311 # if we were parsing PCDATA then we exit the pcdata too
2312 if( $t->{twig_in_pcdata})
2313 {
2314 $t->{twig_in_pcdata}= 0;
2315 $elt= $elt->{parent} if($elt->{parent});
2316 delete $elt->{'twig_current'};
2317 }
2318  
2319 # parent is the new current element
2320 my $parent= $elt->{parent};
2321 $t->{twig_current}= $parent;
2322  
2323 if( $parent)
2324 { $parent->{'twig_current'}=1;
2325 # twig_to_be_normalized
2326 if( $parent->{twig_to_be_normalized}) { $parent->normalize; $parent->{twig_to_be_normalized}=0; }
2327 }
2328  
2329 if( $t->{extra_data})
2330 { $elt->_set_extra_data_before_end_tag( $t->{extra_data});
2331 $t->{extra_data}='';
2332 }
2333  
2334 if( $t->{twig_handlers})
2335 { # look for handlers
2336 my @handlers= _handler( $t, $t->{twig_handlers}, $gi);
2337  
2338 if( $t->{twig_tdh})
2339 { if( @handlers) { push @{$t->{twig_handlers_to_trigger}}, [ $elt, \@handlers ]; }
2340 if( my $all= $t->{twig_handlers}->{handlers}->{$ALL})
2341 { push @{$t->{twig_handlers_to_trigger}}, [ $elt, [$all] ]; }
2342 }
2343 else
2344 {
2345 local $_= $elt; # so we can use $_ in the handlers
2346  
2347 foreach my $handler ( @handlers)
2348 { $handler->($t, $elt) || last; }
2349 # call _all_ handler if needed
2350 my $all= $t->{twig_handlers}->{handlers}->{$ALL};
2351 if( $all)
2352 { $all->($t, $elt); }
2353 if( @handlers || $all) { $t->{twig_right_after_root}=0; }
2354 }
2355 }
2356  
2357 # if twig_roots is set for the element then set appropriate handler
2358 if( $t->{twig_root_depth} and ($p->depth == $t->{twig_root_depth}) )
2359 { if( $t->{twig_default_print})
2360 { # select the proper fh (and store the currently selected one)
2361 $t->_set_fh_to_twig_output_fh();
2362 if( !($p->depth==1)) { $t->{twig_right_after_root}=1; } #XX
2363 if( $t->{twig_keep_encoding})
2364 { $p->setHandlers( %twig_handlers_roots_print_original); }
2365 else
2366 { $p->setHandlers( %twig_handlers_roots_print); }
2367 }
2368 else
2369 { $p->setHandlers( %twig_handlers_roots); }
2370 }
2371  
2372 if( $elt->{'att'}->{'xml:space'} && ( $elt->{'att'}->{'xml:space'} eq 'preserve')) { $t->{twig_preserve_space}--; }
2373  
2374 pop @{$t->{_twig_context_stack}};
2375 return;
2376 }
2377  
2378 sub _trigger_tdh
2379 { my( $t)= @_;
2380  
2381 if( @{$t->{twig_handlers_to_trigger}})
2382 { my @handlers_to_trigger_now= sort { $a->[0]->cmp( $b->[0]) } @{$t->{twig_handlers_to_trigger}};
2383 foreach my $elt_handlers (@handlers_to_trigger_now)
2384 { my( $handled_elt, $handlers_to_trigger)= @$elt_handlers;
2385 foreach my $handler ( @$handlers_to_trigger)
2386 { local $_= $handled_elt; $handler->($t, $handled_elt) || last; }
2387 }
2388 }
2389 return;
2390 }
2391  
2392 # return the list of handler that can be activated for an element
2393 # (either of CODE ref's or 1's for twig_roots)
2394  
2395 sub _handler
2396 { my( $t, $handlers, $gi)= @_;
2397  
2398 my @found_handlers=();
2399 my $found_handler;
2400  
2401 foreach my $handler ( map { @$_ } grep { $_ } $handlers->{xpath_handler}->{$gi}, $handlers->{xpath_handler}->{'*'})
2402 { my $trigger= $handler->{trigger};
2403 if( my $found_path= $trigger->( $t->{_twig_context_stack}))
2404 { my $found_handler= $handler->{handler};
2405 push @found_handlers, $found_handler;
2406 }
2407 }
2408  
2409 # if no handler found call default handler if defined
2410 if( !@found_handlers && defined $handlers->{handlers}->{$DEFAULT})
2411 { push @found_handlers, $handlers->{handlers}->{$DEFAULT}; }
2412  
2413 if( @found_handlers and $t->{twig_do_not_chain_handlers})
2414 { @found_handlers= ($found_handlers[0]); }
2415  
2416 return @found_handlers; # empty if no handler found
2417  
2418 }
2419  
2420 sub _replace_prefix
2421 { my( $t, $name)= @_;
2422 my $p= $t->parser;
2423 my $uri= $p->namespace( $name);
2424 # try to get the namespace from default if none is found (for attributes)
2425 # this should probably be an option
2426 if( !$uri and( $name!~/^xml/)) { $uri= $p->expand_ns_prefix( '#default'); }
2427 if( $uri)
2428 { if (my $mapped_prefix= $t->{twig_map_xmlns}->{$uri} || $DEFAULT_URI2NS{$uri})
2429 { return "$mapped_prefix:$name"; }
2430 else
2431 { my $prefix= _a_proper_ns_prefix( $p, $uri);
2432 if( $prefix eq '#default') { $prefix=''; }
2433 return $prefix ? "$prefix:$name" : $name;
2434 }
2435 }
2436 else
2437 { return $name; }
2438 }
2439  
2440 sub _twig_char
2441 { # warn " in _twig_char...\n"; # DEBUG handler
2442  
2443 my ($p, $string)= @_;
2444 my $t=$p->{twig};
2445  
2446 if( $t->{twig_keep_encoding})
2447 { if( !$t->{twig_in_cdata})
2448 { $string= $p->original_string(); }
2449 else
2450 {
2451 use bytes;
2452 if( length( $string) < 1024)
2453 { $string= $p->original_string(); }
2454 else
2455 { #warn "dodgy case";
2456 # TODO original_string does not hold the entire string, but $string is wrong
2457 # I believe due to a bug in XML::Parser
2458 # for now, we use the original string, even if it means that it's been converted to utf8
2459 }
2460 }
2461 }
2462  
2463 if( $t->{twig_input_filter}) { $string= $t->{twig_input_filter}->( $string); }
2464 if( $t->{twig_char_handler}) { $string= $t->{twig_char_handler}->( $string); }
2465  
2466 my $elt= $t->{twig_current};
2467  
2468 if( $t->{twig_in_cdata})
2469 { # text is the continuation of a previously created cdata
2470 $elt->{cdata}.= $t->{twig_stored_spaces} . $string;
2471 }
2472 elsif( $t->{twig_in_pcdata})
2473 { # text is the continuation of a previously created pcdata
2474 if( $t->{extra_data})
2475 { $elt->_push_extra_data_in_pcdata( $t->{extra_data}, length( $elt->{pcdata}));
2476 $t->{extra_data}='';
2477 }
2478 $elt->{pcdata}.= $string;
2479 }
2480 else
2481 {
2482 # text is just space, which might be discarded later
2483 if( $string=~/\A\s*\Z/s)
2484 {
2485 if( $t->{extra_data})
2486 { # we got extra data (comment, pi), lets add the spaces to it
2487 $t->{extra_data} .= $string;
2488 }
2489 else
2490 { # no extra data, just store the spaces
2491 $t->{twig_stored_spaces}.= $string;
2492 }
2493 }
2494 else
2495 { my $new_elt= _insert_pcdata( $t, $t->{twig_stored_spaces}.$string);
2496 delete $elt->{'twig_current'};
2497 $new_elt->{'twig_current'}=1;
2498 $t->{twig_current}= $new_elt;
2499 $t->{twig_in_pcdata}=1;
2500 if( $t->{extra_data})
2501 { $new_elt->_push_extra_data_in_pcdata( $t->{extra_data}, 0);
2502 $t->{extra_data}='';
2503 }
2504 }
2505 }
2506 return;
2507 }
2508  
2509 sub _twig_cdatastart
2510 { # warn " in _twig_cdatastart...\n"; # DEBUG handler
2511  
2512 my $p= shift;
2513 my $t=$p->{twig};
2514  
2515 $t->{twig_in_cdata}=1;
2516 my $cdata= $t->{twig_elt_class}->new( $CDATA);
2517 my $twig_current= $t->{twig_current};
2518  
2519 if( $t->{twig_in_pcdata})
2520 { # create the node as a sibling of the PCDATA
2521 $cdata->{prev_sibling}=$twig_current; weaken( $cdata->{prev_sibling}); ;
2522 $twig_current->{next_sibling}= $cdata;
2523 my $parent= $twig_current->{parent};
2524 $cdata->{parent}=$parent; weaken( $cdata->{parent}); ;
2525 delete $parent->{empty}; $parent->{last_child}=$cdata; weaken( $parent->{last_child}); ;
2526 $t->{twig_in_pcdata}=0;
2527 }
2528 else
2529 { # we have to create a PCDATA element if we need to store spaces
2530 if( $t->_space_policy($XML::Twig::index2gi[$twig_current->{'gi'}]) && $t->{twig_stored_spaces})
2531 { _insert_pcdata( $t, $t->{twig_stored_spaces}); }
2532 $t->{twig_stored_spaces}='';
2533  
2534 # create the node as a child of the current element
2535 $cdata->{parent}=$twig_current; weaken( $cdata->{parent}); ;
2536 if( my $prev_sibling= $twig_current->{last_child})
2537 { $cdata->{prev_sibling}=$prev_sibling; weaken( $cdata->{prev_sibling}); ;
2538 $prev_sibling->{next_sibling}= $cdata;
2539 }
2540 else
2541 { $twig_current->{first_child}= $cdata; }
2542 delete $twig_current->{empty}; $twig_current->{last_child}=$cdata; weaken( $twig_current->{last_child}); ;
2543 }
2544  
2545 delete $twig_current->{'twig_current'};
2546 $t->{twig_current}= $cdata;
2547 $cdata->{'twig_current'}=1;
2548 if( $t->{extra_data}) { $cdata->set_extra_data( $t->{extra_data}); $t->{extra_data}='' };
2549 return;
2550 }
2551  
2552 sub _twig_cdataend
2553 { # warn " in _twig_cdataend...\n"; # DEBUG handler
2554  
2555 my $p= shift;
2556 my $t=$p->{twig};
2557  
2558 $t->{twig_in_cdata}=0;
2559  
2560 my $elt= $t->{twig_current};
2561 delete $elt->{'twig_current'};
2562 my $cdata= $elt->{cdata};
2563 $elt->{cdata}= $cdata;
2564  
2565 push @{$t->{_twig_context_stack}}, { $ST_TAG => $CDATA };
2566  
2567 if( $t->{twig_handlers})
2568 { # look for handlers
2569 my @handlers= _handler( $t, $t->{twig_handlers}, $CDATA);
2570 local $_= $elt; # so we can use $_ in the handlers
2571 foreach my $handler ( @handlers) { $handler->($t, $elt) || last; }
2572 }
2573  
2574 pop @{$t->{_twig_context_stack}};
2575  
2576 $elt= $elt->{parent};
2577 $t->{twig_current}= $elt;
2578 $elt->{'twig_current'}=1;
2579  
2580 $t->{twig_long_cdata}=0;
2581 return;
2582 }
2583  
2584 sub _pi_elt_handlers
2585 { my( $t, $pi)= @_;
2586 my $pi_handlers= $t->{twig_handlers}->{pi_handlers} || return;
2587 foreach my $handler ( $pi_handlers->{$pi->{target}}, $pi_handlers->{''})
2588 { if( $handler) { local $_= $pi; $handler->( $t, $pi) || last; } }
2589 }
2590  
2591 sub _pi_text_handler
2592 { my( $t, $target, $data)= @_;
2593 if( my $handler= $t->{twig_handlers}->{pi_handlers}->{$target})
2594 { return $handler->( $t, $target, $data); }
2595 if( my $handler= $t->{twig_handlers}->{pi_handlers}->{''})
2596 { return $handler->( $t, $target, $data); }
2597 return defined( $data) && $data ne '' ? "<?$target $data?>" : "<?$target?>" ;
2598 }
2599  
2600 sub _comment_elt_handler
2601 { my( $t, $comment)= @_;
2602 if( my $handler= $t->{twig_handlers}->{handlers}->{$COMMENT})
2603 { local $_= $comment; $handler->($t, $comment); }
2604 }
2605  
2606 sub _comment_text_handler
2607 { my( $t, $comment)= @_;
2608 if( my $handler= $t->{twig_handlers}->{handlers}->{$COMMENT})
2609 { $comment= $handler->($t, $comment);
2610 if( !defined $comment || $comment eq '') { return ''; }
2611 }
2612 return "<!--$comment-->";
2613 }
2614  
2615 sub _twig_comment
2616 { # warn " in _twig_comment...\n"; # DEBUG handler
2617 my( $p, $comment_text)= @_;
2618 my $t=$p->{twig};
2619  
2620 if( $t->{twig_keep_encoding}) { $comment_text= substr( $p->original_string(), 4, -3); }
2621  
2622 $t->_twig_pi_comment( $p, $COMMENT, $t->{twig_keep_comments}, $t->{twig_process_comments},
2623 '_set_comment', '_comment_elt_handler', '_comment_text_handler', $comment_text
2624 );
2625 return;
2626 }
2627  
2628 sub _twig_pi
2629 { # warn " in _twig_pi...\n"; # DEBUG handler
2630  
2631 my( $p, $target, $data)= @_;
2632 my $t=$p->{twig};
2633  
2634 if( $t->{twig_keep_encoding})
2635 { my $pi_text= substr( $p->original_string(), 2, -2);
2636 ($target, $data)= split( /\s+/, $pi_text, 2);
2637 }
2638  
2639 $t->_twig_pi_comment( $p, $PI, $t->{twig_keep_pi}, $t->{twig_process_pi},
2640 '_set_pi', '_pi_elt_handlers', '_pi_text_handler', $target, $data
2641 );
2642 return;
2643 }
2644  
2645 sub _twig_pi_comment
2646 { my( $t, $p, $type, $keep, $process, $set, $elt_handler, $text_handler, @parser_args)= @_;
2647  
2648 if( $t->{twig_input_filter})
2649 { foreach my $arg (@parser_args) { $arg= $t->{twig_input_filter}->( $arg); } }
2650  
2651 # if pi/comments are to be kept then we piggyback them to the current element
2652 if( $keep)
2653 { # first add spaces
2654 if( $t->{twig_stored_spaces})
2655 { $t->{extra_data}.= $t->{twig_stored_spaces};
2656 $t->{twig_stored_spaces}= '';
2657 }
2658  
2659 my $extra_data= $t->$text_handler( @parser_args);
2660 $t->{extra_data}.= $extra_data;
2661  
2662 }
2663 elsif( $process)
2664 {
2665 my $twig_current= $t->{twig_current}; # defined unless we are outside of the root
2666  
2667 my $elt= $t->{twig_elt_class}->new( $type);
2668 $elt->$set( @parser_args);
2669 if( $t->{extra_data})
2670 { $elt->set_extra_data( $t->{extra_data});
2671 $t->{extra_data}='';
2672 }
2673  
2674 if( ! $t->root)
2675 { $t->_add_cpi_outside_of_root( leading_cpi => $elt);
2676 }
2677 elsif( $t->{twig_in_pcdata})
2678 { $t->_trigger_text_handler();
2679 # create the node as a sibling of the PCDATA
2680 $elt->paste_after( $twig_current);
2681 $t->{twig_in_pcdata}=0;
2682 }
2683 elsif( $twig_current)
2684 { # we have to create a PCDATA element if we need to store spaces
2685 if( $t->_space_policy($XML::Twig::index2gi[$twig_current->{'gi'}]) && $t->{twig_stored_spaces})
2686 { _insert_pcdata( $t, $t->{twig_stored_spaces}); }
2687 $t->{twig_stored_spaces}='';
2688 # create the node as a child of the current element
2689 $elt->paste_last_child( $twig_current);
2690 }
2691 else
2692 { $t->_add_cpi_outside_of_root( trailing_cpi => $elt); }
2693  
2694 if( $twig_current)
2695 { delete $twig_current->{'twig_current'};
2696 my $parent= $elt->{parent};
2697 $t->{twig_current}= $parent;
2698 $parent->{'twig_current'}=1;
2699 }
2700  
2701 $t->$elt_handler( $elt);
2702 }
2703  
2704 }
2705  
2706 sub _trigger_text_handler
2707 { my ($t) = @_;
2708 if( my $text_handler= $t->{twig_handlers}->{handlers}->{$TEXT} )
2709 { local $_= $t->{twig_current};
2710 $text_handler->( $t, $_) if $_;
2711 }
2712 }
2713  
2714 # add a comment or pi before the first element
2715 sub _add_cpi_outside_of_root
2716 { my($t, $type, $elt)= @_; # $type is 'leading_cpi' or 'trailing_cpi'
2717 $t->{$type} ||= $t->{twig_elt_class}->new( '#CPI');
2718 # create the node as a child of the current element
2719 $elt->paste_last_child( $t->{$type});
2720 return $t;
2721 }
2722  
2723 sub _twig_final
2724 { # warn " in _twig_final...\n"; # DEBUG handler
2725  
2726 my $p= shift;
2727 my $t= $p->isa( 'XML::Twig') ? $p : $p->{twig};
2728  
2729 # store trailing data
2730 if( $t->{extra_data}) { $t->{trailing_cpi_text} = $t->{extra_data}; $t->{extra_data}=''; }
2731 $t->{trailing_spaces}= $t->{twig_stored_spaces} || '';
2732 my $s= $t->{twig_stored_spaces}; $s=~s{\n}{\\n}g;
2733 if( $t->{twig_stored_spaces}) { my $s= $t->{twig_stored_spaces}; }
2734  
2735 # restore the selected filehandle if needed
2736 $t->_set_fh_to_selected_fh();
2737  
2738 $t->_trigger_tdh if( $t->{twig_tdh});
2739  
2740 select $t->{twig_original_selected_fh} if($t->{twig_original_selected_fh}); # probably dodgy
2741  
2742 if( exists $t->{twig_autoflush_data})
2743 { my @args;
2744 push @args, $t->{twig_autoflush_data}->{fh} if( $t->{twig_autoflush_data}->{fh});
2745 push @args, @{$t->{twig_autoflush_data}->{args}} if( $t->{twig_autoflush_data}->{args});
2746 $t->flush( @args);
2747 delete $t->{twig_autoflush_data};
2748 $t->root->delete if $t->root;
2749 }
2750  
2751 # tries to clean-up (probably not very well at the moment)
2752 #undef $p->{twig};
2753 undef $t->{twig_parser};
2754 delete $t->{twig_parsing};
2755 @{$t}{ qw( twig_parser twig_parsing _twig_context_stack twig_current) }=();
2756  
2757 return $t;
2758 }
2759  
2760 sub _insert_pcdata
2761 { my( $t, $string)= @_;
2762 # create a new PCDATA element
2763 my $parent= $t->{twig_current}; # always defined
2764 my $elt;
2765 if( exists $t->{twig_alt_elt_class})
2766 { $elt= $t->{twig_elt_class}->new( $PCDATA);
2767 $elt->{pcdata}= $string;
2768 }
2769 else
2770 { $elt= bless( { gi => $XML::Twig::gi2index{$PCDATA}, pcdata => $string }, 'XML::Twig::Elt'); }
2771  
2772 my $prev_sibling= $parent->{last_child};
2773 if( $prev_sibling)
2774 { $prev_sibling->{next_sibling}= $elt;
2775 $elt->{prev_sibling}=$prev_sibling; weaken( $elt->{prev_sibling}); ;
2776 }
2777 else
2778 { $parent->{first_child}= $elt; }
2779  
2780 $elt->{parent}=$parent; weaken( $elt->{parent}); ;
2781 delete $parent->{empty}; $parent->{last_child}=$elt; weaken( $parent->{last_child}); ;
2782 $t->{twig_stored_spaces}='';
2783 return $elt;
2784 }
2785  
2786 sub _space_policy
2787 { my( $t, $gi)= @_;
2788 my $policy;
2789 $policy=0 if( $t->{twig_discard_spaces});
2790 $policy=1 if( $t->{twig_keep_spaces});
2791 $policy=1 if( $t->{twig_keep_spaces_in}
2792 && $t->{twig_keep_spaces_in}->{$gi});
2793 $policy=0 if( $t->{twig_discard_spaces_in}
2794 && $t->{twig_discard_spaces_in}->{$gi});
2795 return $policy;
2796 }
2797  
2798 sub _twig_entity
2799 { # warn " in _twig_entity...\n"; # DEBUG handler
2800 my( $p, $name, $val, $sysid, $pubid, $ndata, $param)= @_;
2801 my $t=$p->{twig};
2802  
2803 #{ no warnings; my $base= $p->base; warn "_twig_entity called: expand: '$t->{twig_expand_external_ents}', base: '$base', name: '$name', val: '$val', sysid: '$sysid', pubid: '$pubid', ndata: '$ndata', param: '$param'\n";}
2804  
2805 my $missing_entity=0;
2806  
2807 if( $sysid)
2808 { if($ndata)
2809 { if( ! -f _based_filename( $sysid, $p->base)) { $missing_entity= 1; }
2810 }
2811 else
2812 { if( $t->{twig_expand_external_ents})
2813 { $val= eval { _slurp_uri( $sysid, $p->base) };
2814 if( ! defined $val)
2815 { if( $t->{twig_extern_ent_nofail})
2816 { $missing_entity= 1; }
2817 else
2818 { _croak( "cannot load SYSTEM entity '$name' from '$sysid': $@", 3); }
2819 }
2820 }
2821 }
2822 }
2823  
2824 my $ent=XML::Twig::Entity->new( $name, $val, $sysid, $pubid, $ndata, $param);
2825 if( $missing_entity) { $t->{twig_missing_system_entities}->{$name}= $ent; }
2826  
2827 my $entity_list= $t->entity_list;
2828 if( $entity_list) { $entity_list->add( $ent); }
2829  
2830 if( $parser_version > 2.27)
2831 { # this is really ugly, but with some versions of XML::Parser the value
2832 # of the entity is not properly returned by the default handler
2833 my $ent_decl= $ent->text;
2834 if( $t->{twig_keep_encoding})
2835 { if( defined $ent->{val} && ($ent_decl !~ /["']/))
2836 { my $val= $ent->{val};
2837 $ent_decl .= $val =~ /"/ ? qq{'$val' } : qq{"$val" };
2838 }
2839 # for my solaris box (perl 5.6.1, XML::Parser 2.31, expat?)
2840 $t->{twig_doctype}->{internal}=~ s{<!ENTITY\s+$name\s+$}{substr( $ent_decl, 0, -1)}e;
2841 }
2842 $t->{twig_doctype}->{internal} .= $ent_decl
2843 unless( $t->{twig_doctype}->{internal}=~ m{<!ENTITY\s+$name\s+});
2844 }
2845  
2846 return;
2847 }
2848  
2849 sub _twig_notation
2850 { my( $p, $name, $base, $sysid, $pubid ) = @_;
2851 my $t = $p->{twig};
2852  
2853 my $notation = XML::Twig::Notation->new( $name, $base, $sysid, $pubid );
2854 my $notation_list = $t->notation_list();
2855 if( $notation_list ) { $notation_list->add( $notation ); }
2856  
2857 # internal should get the recognized_string, but XML::Parser does not provide it
2858 # so we need to re-create it ( $notation->text) and stick it there.
2859 $t->{twig_doctype}->{internal} .= $notation->text;
2860  
2861 return;
2862 }
2863  
2864 sub _twig_extern_ent
2865 { # warn " in _twig_extern_ent...I (", $_[0]->original_string, ")\n"; # DEBUG handler
2866 my( $p, $base, $sysid, $pubid)= @_;
2867 my $t= $p->{twig};
2868 if( $t->{twig_no_expand})
2869 { my $ent_name= $t->{twig_keep_encoding} ? $p->original_string : $p->recognized_string;
2870 _twig_insert_ent( $t, $ent_name);
2871 return '';
2872 }
2873 my $ent_content= eval { $t->{twig_ext_ent_handler}->( $p, $base, $sysid) };
2874 if( ! defined $ent_content)
2875 {
2876 my $ent_name = $p->recognized_string;
2877 my $file = _based_filename( $sysid, $base);
2878 my $error_message= "cannot expand $ent_name - cannot load '$file'";
2879 if( $t->{twig_extern_ent_nofail}) { return "<!-- $error_message -->"; }
2880 else { _croak( $error_message); }
2881 }
2882 return $ent_content;
2883 }
2884  
2885 # I use this so I can change the $Carp::CarpLevel (which determines how many call frames to skip when reporting an error)
2886 sub _croak
2887 { my( $message, $level)= @_;
2888 $Carp::CarpLevel= $level || 0;
2889 croak $message;
2890 }
2891  
2892 sub _twig_xmldecl
2893 { # warn " in _twig_xmldecl...\n"; # DEBUG handler
2894  
2895 my $p= shift;
2896 my $t=$p->{twig};
2897 $t->{twig_xmldecl}||={}; # could have been set by set_output_encoding
2898 $t->{twig_xmldecl}->{version}= shift;
2899 $t->{twig_xmldecl}->{encoding}= shift;
2900 $t->{twig_xmldecl}->{standalone}= shift;
2901 return;
2902 }
2903  
2904 sub _twig_doctype
2905 { # warn " in _twig_doctype...\n"; # DEBUG handler
2906 my( $p, $name, $sysid, $pub, $internal)= @_;
2907 my $t=$p->{twig};
2908 $t->{twig_doctype}||= {}; # create
2909 $t->{twig_doctype}->{name}= $name; # always there
2910 $t->{twig_doctype}->{sysid}= $sysid; #
2911 $t->{twig_doctype}->{pub}= $pub; #
2912  
2913 # now let's try to cope with XML::Parser 2.28 and above
2914 if( $parser_version > 2.27)
2915 { @saved_default_handler= $p->setHandlers( Default => \&_twig_store_internal_dtd,
2916 Entity => \&_twig_entity,
2917 );
2918 $p->setHandlers( DoctypeFin => \&_twig_stop_storing_internal_dtd);
2919 $t->{twig_doctype}->{internal}='';
2920 }
2921 else
2922 # for XML::Parser before 2.28
2923 { $internal||='';
2924 $internal=~ s{^\s*\[}{};
2925 $internal=~ s{]\s*$}{};
2926 $t->{twig_doctype}->{internal}=$internal;
2927 }
2928  
2929 # now check if we want to get the DTD info
2930 if( $t->{twig_read_external_dtd} && $sysid)
2931 { # let's build a fake document with an internal DTD
2932 if( $t->{DTDBase})
2933 { _use( 'File::Spec');
2934 $sysid=File::Spec->catfile($t->{DTDBase}, $sysid);
2935 }
2936 my $dtd= _slurp_uri( $sysid);
2937 # if the DTD includes an XML declaration, it needs to be moved before the DOCTYPE bit
2938 if( $dtd=~ s{^(\s*<\?xml(\s+\w+\s*=\s*("[^"]*"|'[^']*'))*\s*\?>)}{})
2939 { $dtd= "$1<!DOCTYPE $name [$dtd]><$name/>"; }
2940 else
2941 { $dtd= "<!DOCTYPE $name [$dtd]><$name/>"; }
2942  
2943 $t->save_global_state(); # save the globals (they will be reset by the following new)
2944 my $t_dtd= XML::Twig->new( load_DTD => 1, ParseParamEnt => 1, error_context => $t->{ErrorContext} || 0); # create a temp twig
2945 $t_dtd->parse( $dtd); # parse it
2946 $t->{twig_dtd}= $t_dtd->{twig_dtd}; # grab the dtd info
2947 #$t->{twig_dtd_is_external}=1;
2948 $t->entity_list->_add_list( $t_dtd->entity_list) if( $t_dtd->entity_list); # grab the entity info
2949 $t->notation_list->_add_list( $t_dtd->notation_list) if( $t_dtd->notation_list); # grab the notation info
2950 $t->restore_global_state();
2951 }
2952 return;
2953 }
2954  
2955 sub _twig_element
2956 { # warn " in _twig_element...\n"; # DEBUG handler
2957  
2958 my( $p, $name, $model)= @_;
2959 my $t=$p->{twig};
2960 $t->{twig_dtd}||= {}; # may create the dtd
2961 $t->{twig_dtd}->{model}||= {}; # may create the model hash
2962 $t->{twig_dtd}->{elt_list}||= []; # ordered list of elements
2963 push @{$t->{twig_dtd}->{elt_list}}, $name; # store the elt
2964 $t->{twig_dtd}->{model}->{$name}= $model; # store the model
2965 if( ($parser_version > 2.27) && ($t->{twig_doctype}->{internal}=~ m{(^|>)\s*$}) )
2966 { my $text= $XML::Twig::Elt::keep_encoding ? $p->original_string : $p->recognized_string;
2967 unless( $text)
2968 { # this version of XML::Parser does not return the text in the *_string method
2969 # we need to rebuild it
2970 $text= "<!ELEMENT $name $model>";
2971 }
2972 $t->{twig_doctype}->{internal} .= $text;
2973 }
2974 return;
2975 }
2976  
2977 sub _twig_attlist
2978 { # warn " in _twig_attlist...\n"; # DEBUG handler
2979  
2980 my( $p, $gi, $att, $type, $default, $fixed)= @_;
2981 #warn "in attlist: gi: '$gi', att: '$att', type: '$type', default: '$default', fixed: '$fixed'\n";
2982 my $t=$p->{twig};
2983 $t->{twig_dtd}||= {}; # create dtd if need be
2984 $t->{twig_dtd}->{$gi}||= {}; # create elt if need be
2985 #$t->{twig_dtd}->{$gi}->{att}||= {}; # create att if need be
2986 if( ($parser_version > 2.27) && ($t->{twig_doctype}->{internal}=~ m{(^|>)\s*$}) )
2987 { my $text= $XML::Twig::Elt::keep_encoding ? $p->original_string : $p->recognized_string;
2988 unless( $text)
2989 { # this version of XML::Parser does not return the text in the *_string method
2990 # we need to rebuild it
2991 my $att_decl="$att $type";
2992 $att_decl .= " #FIXED" if( $fixed);
2993 $att_decl .= " $default" if( defined $default);
2994 # 2 cases: there is already an attlist on that element or not
2995 if( $t->{twig_dtd}->{att}->{$gi})
2996 { # there is already an attlist, add to it
2997 $t->{twig_doctype}->{internal}=~ s{(<!ATTLIST\s*$gi )(.*?)\n?>}
2998 { "$1$2\n" . ' ' x length( $1) . "$att_decl\n>"}es;
2999 }
3000 else
3001 { # create the attlist
3002 $t->{twig_doctype}->{internal}.= "<!ATTLIST $gi $att_decl>"
3003 }
3004 }
3005 }
3006 $t->{twig_dtd}->{att}->{$gi}->{$att}= {} ;
3007 $t->{twig_dtd}->{att}->{$gi}->{$att}->{type}= $type;
3008 $t->{twig_dtd}->{att}->{$gi}->{$att}->{default}= $default if( defined $default);
3009 $t->{twig_dtd}->{att}->{$gi}->{$att}->{fixed}= $fixed;
3010 return;
3011 }
3012  
3013 sub _twig_default
3014 { # warn " in _twig_default...\n"; # DEBUG handler
3015  
3016 my( $p, $string)= @_;
3017  
3018 my $t= $p->{twig};
3019  
3020 # we need to process the data in 2 cases: entity, or spaces after the closing tag
3021  
3022 # after the closing tag (no twig_current and root has been created)
3023 if( ! $t->{twig_current} && $t->{twig_root} && $string=~ m{^\s+$}m) { $t->{twig_stored_spaces} .= $string; }
3024  
3025 # process only if we have an entity
3026 if( $string=~ m{^&([^;]*);$})
3027 { # the entity has to be pure pcdata, or we have a problem
3028 if( ($p->original_string=~ m{^<}) && ($p->original_string=~ m{>$}) )
3029 { # string is a tag, entity is in an attribute
3030 $t->{twig_entities_in_attribute}=1 if( $t->{twig_do_not_escape_amp_in_atts});
3031 }
3032 else
3033 { my $ent;
3034 if( $t->{twig_keep_encoding})
3035 { _twig_char( $p, $string);
3036 $ent= substr( $string, 1, -1);
3037 }
3038 else
3039 { $ent= _twig_insert_ent( $t, $string);
3040 }
3041  
3042 return $ent;
3043 }
3044 }
3045 }
3046  
3047 sub _twig_insert_ent
3048 {
3049 my( $t, $string)=@_;
3050  
3051 my $twig_current= $t->{twig_current};
3052  
3053 my $ent= $t->{twig_elt_class}->new( $ENT);
3054 $ent->{ent}= $string;
3055  
3056 _add_or_discard_stored_spaces( $t);
3057  
3058 if( $t->{twig_in_pcdata})
3059 { # create the node as a sibling of the #PCDATA
3060  
3061 $ent->{prev_sibling}=$twig_current; weaken( $ent->{prev_sibling}); ;
3062 $twig_current->{next_sibling}= $ent;
3063 my $parent= $twig_current->{parent};
3064 $ent->{parent}=$parent; weaken( $ent->{parent}); ;
3065 delete $parent->{empty}; $parent->{last_child}=$ent; weaken( $parent->{last_child}); ;
3066 # the twig_current is now the parent
3067 delete $twig_current->{'twig_current'};
3068 $t->{twig_current}= $parent;
3069 # we left pcdata
3070 $t->{twig_in_pcdata}=0;
3071 }
3072 else
3073 { # create the node as a child of the current element
3074 $ent->{parent}=$twig_current; weaken( $ent->{parent}); ;
3075 if( my $prev_sibling= $twig_current->{last_child})
3076 { $ent->{prev_sibling}=$prev_sibling; weaken( $ent->{prev_sibling}); ;
3077 $prev_sibling->{next_sibling}= $ent;
3078 }
3079 else
3080 { if( $twig_current) { $twig_current->{first_child}= $ent; } }
3081 if( $twig_current) { delete $twig_current->{empty}; $twig_current->{last_child}=$ent; weaken( $twig_current->{last_child}); ; }
3082 }
3083  
3084 return $ent;
3085 }
3086  
3087 sub parser
3088 { return $_[0]->{twig_parser}; }
3089  
3090 # returns the declaration text (or a default one)
3091 sub xmldecl
3092 { my $t= shift;
3093 return '' unless( $t->{twig_xmldecl} || $t->{output_encoding});
3094 my $decl_string;
3095 my $decl= $t->{twig_xmldecl};
3096 if( $decl)
3097 { my $version= $decl->{version};
3098 $decl_string= q{<?xml};
3099 $decl_string .= qq{ version="$version"};
3100  
3101 # encoding can either have been set (in $decl->{output_encoding})
3102 # or come from the document (in $decl->{encoding})
3103 if( $t->{output_encoding})
3104 { my $encoding= $t->{output_encoding};
3105 $decl_string .= qq{ encoding="$encoding"};
3106 }
3107 elsif( $decl->{encoding})
3108 { my $encoding= $decl->{encoding};
3109 $decl_string .= qq{ encoding="$encoding"};
3110 }
3111  
3112 if( defined( $decl->{standalone}))
3113 { $decl_string .= q{ standalone="};
3114 $decl_string .= $decl->{standalone} ? "yes" : "no";
3115 $decl_string .= q{"};
3116 }
3117  
3118 $decl_string .= "?>\n";
3119 }
3120 else
3121 { my $encoding= $t->{output_encoding};
3122 $decl_string= qq{<?xml version="1.0" encoding="$encoding"?>};
3123 }
3124  
3125 my $output_filter= XML::Twig::Elt::output_filter();
3126 return $output_filter ? $output_filter->( $decl_string) : $decl_string;
3127 }
3128  
3129 sub set_doctype
3130 { my( $t, $name, $system, $public, $internal)= @_;
3131 $t->{twig_doctype}= {} unless defined $t->{twig_doctype};
3132 my $doctype= $t->{twig_doctype};
3133 $doctype->{name} = $name if( defined $name);
3134 $doctype->{sysid} = $system if( defined $system);
3135 $doctype->{pub} = $public if( defined $public);
3136 $doctype->{internal} = $internal if( defined $internal);
3137 }
3138  
3139 sub doctype_name
3140 { my $t= shift;
3141 my $doctype= $t->{twig_doctype} or return '';
3142 return $doctype->{name} || '';
3143 }
3144  
3145 sub system_id
3146 { my $t= shift;
3147 my $doctype= $t->{twig_doctype} or return '';
3148 return $doctype->{sysid} || '';
3149 }
3150  
3151 sub public_id
3152 { my $t= shift;
3153 my $doctype= $t->{twig_doctype} or return '';
3154 return $doctype->{pub} || '';
3155 }
3156  
3157 sub internal_subset
3158 { my $t= shift;
3159 my $doctype= $t->{twig_doctype} or return '';
3160 return $doctype->{internal} || '';
3161 }
3162  
3163 # return the dtd object
3164 sub dtd
3165 { my $t= shift;
3166 return $t->{twig_dtd};
3167 }
3168  
3169 # return an element model, or the list of element models
3170 sub model
3171 { my $t= shift;
3172 my $elt= shift;
3173 return $t->dtd->{model}->{$elt} if( $elt);
3174 return (sort keys %{$t->dtd->{model}});
3175 }
3176  
3177 # return the entity_list object
3178 sub entity_list
3179 { my $t= shift;
3180 return $t->{twig_entity_list};
3181 }
3182  
3183 # return the list of entity names
3184 sub entity_names
3185 { my $t= shift;
3186 return $t->entity_list->entity_names;
3187 }
3188  
3189 # return the entity object
3190 sub entity
3191 { my $t= shift;
3192 my $entity_name= shift;
3193 return $t->entity_list->ent( $entity_name);
3194 }
3195  
3196 # return the notation_list object
3197 sub notation_list
3198 { my $t= shift;
3199 return $t->{twig_notation_list};
3200 }
3201  
3202 # return the list of notation names
3203 sub notation_names
3204 { my $t= shift;
3205 return $t->notation_list->notation_names;
3206 }
3207  
3208 # return the notation object
3209 sub notation
3210 { my $t= shift;
3211 my $notation_name= shift;
3212 return $t->notation_list->notation( $notation_name);
3213 }
3214  
3215 sub print_prolog
3216 { my $t= shift;
3217 my $fh= isa( $_[0], 'GLOB') || isa( $_[0], 'IO::Scalar') ? shift : $t->{twig_output_fh} || select() || \*STDOUT;
3218 ## no critic (TestingAndDebugging::ProhibitNoStrict);
3219 no strict 'refs';
3220 print {$fh} $t->prolog( @_);
3221 }
3222  
3223 sub prolog
3224 { my $t= shift;
3225 if( $t->{no_prolog}){ return ''; }
3226  
3227 return $t->{no_prolog} ? ''
3228 : defined $t->{no_dtd_output} ? $t->xmldecl
3229 : $t->xmldecl . $t->doctype( @_);
3230 }
3231  
3232 sub doctype
3233 { my $t= shift;
3234 my %args= _normalize_args( @_);
3235 my $update_dtd = $args{UpdateDTD} || '';
3236 my $doctype_text='';
3237  
3238 my $doctype= $t->{twig_doctype};
3239  
3240 if( $doctype)
3241 { $doctype_text .= qq{<!DOCTYPE $doctype->{name}} if( $doctype->{name});
3242 $doctype_text .= qq{ PUBLIC "$doctype->{pub}"} if( $doctype->{pub});
3243 $doctype_text .= qq{ SYSTEM} if( $doctype->{sysid} && !$doctype->{pub});
3244 $doctype_text .= qq{ "$doctype->{sysid}"} if( $doctype->{sysid});
3245 }
3246  
3247 if( $update_dtd)
3248 { if( $doctype)
3249 { my $internal=$doctype->{internal};
3250 # awful hack, but at least it works a little better that what was there before
3251 if( $internal)
3252 { # remove entity and notation declarations (they will be re-generated from the updated entity list)
3253 $internal=~ s{<! \s* ENTITY \s+ $REG_TAG_NAME \s+ ( ("[^"]*"|'[^']*') \s* | SYSTEM [^>]*) >\s*}{}xg;
3254 $internal=~ s{<! \s* NOTATION .*? >\s*}{}sxg;
3255 $internal=~ s{^\n}{};
3256 }
3257 $internal .= $t->entity_list->text ||'' if( $t->entity_list);
3258 $internal .= $t->notation_list->text ||'' if( $t->notation_list);
3259 if( $internal) { $doctype_text .= "[\n$internal]>\n"; }
3260 }
3261 elsif( !$t->{'twig_dtd'} && ( keys %{$t->entity_list} || keys %{$t->notation_list} ) )
3262 { $doctype_text .= "<!DOCTYPE " . $t->root->gi . " [\n" . $t->entity_list->text . $t->notation_list->text . "\n]>";}
3263 else
3264 { $doctype_text= $t->{twig_dtd};
3265 $doctype_text .= $t->dtd_text;
3266 }
3267 }
3268 elsif( $doctype)
3269 { if( my $internal= $doctype->{internal})
3270 { # add opening and closing brackets if not already there
3271 # plus some spaces and newlines for a nice formating
3272 # I test it here because I can't remember which version of
3273 # XML::Parser need it or not, nor guess which one will in the
3274 # future, so this about the best I can do
3275 $internal=~ s{^\s*(\[\s*)?}{ [\n};
3276 $internal=~ s{\s*(\]\s*(>\s*)?)?\s*$}{\n]>\n};
3277  
3278 $doctype_text .= $internal;
3279 }
3280 }
3281  
3282 if( $doctype_text)
3283 {
3284 # terrible hack, as I can't figure out in which case the darn prolog
3285 # should get an extra > (depends on XML::Parser and expat versions)
3286 $doctype_text=~ s/(>\s*)*$/>\n/;
3287  
3288 my $output_filter= XML::Twig::Elt::output_filter();
3289 return $output_filter ? $output_filter->( $doctype_text) : $doctype_text;
3290 }
3291 else
3292 { return $doctype_text; }
3293 }
3294  
3295 sub _leading_cpi
3296 { my $t= shift;
3297 my $leading_cpi= $t->{leading_cpi} || return '';
3298 return $leading_cpi->sprint( 1);
3299 }
3300  
3301 sub _trailing_cpi
3302 { my $t= shift;
3303 my $trailing_cpi= $t->{trailing_cpi} || return '';
3304 return $trailing_cpi->sprint( 1);
3305 }
3306  
3307 sub _trailing_cpi_text
3308 { my $t= shift;
3309 return $t->{trailing_cpi_text} || '';
3310 }
3311  
3312 sub print_to_file
3313 { my( $t, $filename)= (shift, shift);
3314 my $out_fh;
3315 my $mode= $t->{twig_keep_encoding} && ! _use_perlio() ? '>' : '>:utf8';
3316 open( $out_fh, $mode, $filename) or _croak( "cannot create file $filename: $!");
3317 $t->print( $out_fh, @_);
3318 close $out_fh;
3319 return $t;
3320 }
3321  
3322 # probably only works on *nix (at least the chmod bit)
3323 # first print to a temporary file, then rename that file to the desired file name, then change permissions
3324 # to the original file permissions (or to the current umask)
3325 sub safe_print_to_file
3326 { my( $t, $filename)= (shift, shift);
3327 my $perm= -f $filename ? (stat $filename)[2] & 07777 : ~umask() ; ## no critic (ProhibitLeadingZeros)
3328 XML::Twig::_use( 'File::Temp') || croak "need File::Temp to use safe_print_to_file\n";
3329 my $tmpdir= dirname( $filename);
3330 my( $fh, $tmpfilename) = File::Temp::tempfile( DIR => $tmpdir);
3331 $t->print_to_file( $tmpfilename, @_);
3332 rename( $tmpfilename, $filename) or unlink $tmpfilename && _croak( "cannot move temporary file to $filename: $!");
3333 chmod $perm, $filename;
3334 return $t;
3335 }
3336  
3337 sub print
3338 { my $t= shift;
3339 my $fh= isa( $_[0], 'GLOB') || isa( $_[0], 'IO::Scalar') ? shift : undef;
3340 my %args= _normalize_args( @_);
3341  
3342 my $old_select = defined $fh ? select $fh : undef;
3343 my $old_pretty = defined ($args{PrettyPrint}) ? $t->set_pretty_print( $args{PrettyPrint}) : undef;
3344 my $old_empty_tag = defined ($args{EmptyTags}) ? $t->set_empty_tag_style( $args{EmptyTags}) : undef;
3345  
3346 ## no critic (RequireEncodingWithUTF8Layer)
3347 if( ! $t->{twig_keep_encoding} && _use_perlio() ) { binmode( $fh || \*STDOUT, ":utf8" ); }
3348  
3349 print $t->prolog( %args) . $t->_leading_cpi( %args);
3350 $t->{twig_root}->print;
3351 print $t->_trailing_cpi # trailing comments and pi's (elements, in 'process' mode)
3352 . $t->_trailing_cpi_text # trailing comments and pi's (in 'keep' mode)
3353 . ( ($t->{twig_keep_spaces}||'') && ($t->{trailing_spaces} || ''))
3354 ;
3355  
3356 $t->set_pretty_print( $old_pretty) if( defined $old_pretty);
3357 $t->set_empty_tag_style( $old_empty_tag) if( defined $old_empty_tag);
3358 if( $fh) { select $old_select; }
3359  
3360 return $t;
3361 }
3362  
3363 sub flush
3364 { my $t= shift;
3365  
3366 $t->_trigger_tdh if $t->{twig_tdh};
3367  
3368 return if( $t->{twig_completely_flushed});
3369  
3370 my $fh= isa( $_[0], 'GLOB') || isa( $_[0], 'IO::Scalar') ? shift : undef;
3371 my $old_select= defined $fh ? select $fh : undef;
3372 my $up_to= ref $_[0] ? shift : undef;
3373 my %args= _normalize_args( @_);
3374  
3375 my $old_pretty;
3376 if( defined $args{PrettyPrint})
3377 { $old_pretty= $t->set_pretty_print( $args{PrettyPrint});
3378 delete $args{PrettyPrint};
3379 }
3380  
3381 my $old_empty_tag_style;
3382 if( $args{EmptyTags})
3383 { $old_empty_tag_style= $t->set_empty_tag_style( $args{EmptyTags});
3384 delete $args{EmptyTags};
3385 }
3386  
3387 # the "real" last element processed, as _twig_end has closed it
3388 my $last_elt;
3389 my $flush_trailing_data=0;
3390 if( $up_to)
3391 { $last_elt= $up_to; }
3392 elsif( $t->{twig_current})
3393 { $last_elt= $t->{twig_current}->{last_child}; }
3394 else
3395 { $last_elt= $t->{twig_root};
3396 $flush_trailing_data=1;
3397 $t->{twig_completely_flushed}=1;
3398 }
3399  
3400 # flush the DTD unless it has ready flushed (ie root has been flushed)
3401 my $elt= $t->{twig_root};
3402 unless( $elt->{'flushed'})
3403 { # store flush info so we can auto-flush later
3404 if( $t->{twig_autoflush})
3405 { $t->{twig_autoflush_data}={};
3406 $t->{twig_autoflush_data}->{fh} = $fh if( $fh);
3407 $t->{twig_autoflush_data}->{args} = \@_ if( @_);
3408 }
3409 $t->print_prolog( %args);
3410 print $t->_leading_cpi;
3411 }
3412  
3413 while( $elt)
3414 { my $next_elt;
3415 if( $last_elt && $last_elt->in( $elt))
3416 {
3417 unless( $elt->{'flushed'})
3418 { # just output the front tag
3419 print $elt->start_tag();
3420 $elt->{'flushed'}=1;
3421 }
3422 $next_elt= $elt->{first_child};
3423 }
3424 else
3425 { # an element before the last one or the last one,
3426 $next_elt= $elt->{next_sibling};
3427 $elt->_flush();
3428 $elt->delete;
3429 last if( $last_elt && ($elt == $last_elt));
3430 }
3431 $elt= $next_elt;
3432 }
3433  
3434 if( $flush_trailing_data)
3435 { print $t->_trailing_cpi # trailing comments and pi's (elements, in 'process' mode)
3436 , $t->_trailing_cpi_text # trailing comments and pi's (in 'keep' mode)
3437 }
3438  
3439 select $old_select if( defined $old_select);
3440 $t->set_pretty_print( $old_pretty) if( defined $old_pretty);
3441 $t->set_empty_tag_style( $old_empty_tag_style) if( defined $old_empty_tag_style);
3442  
3443 if( my $ids= $t->{twig_id_list})
3444 { while( my ($id, $elt)= each %$ids)
3445 { if( ! defined $elt)
3446 { delete $t->{twig_id_list}->{$id} }
3447 }
3448 }
3449  
3450 return $t;
3451 }
3452  
3453 # flushes up to an element
3454 # this method just reorders the arguments and calls flush
3455 sub flush_up_to
3456 { my $t= shift;
3457 my $up_to= shift;
3458 if( isa( $_[0], 'GLOB') || isa( $_[0], 'IO::Scalar'))
3459 { my $fh= shift;
3460 $t->flush( $fh, $up_to, @_);
3461 }
3462 else
3463 { $t->flush( $up_to, @_); }
3464  
3465 return $t;
3466 }
3467  
3468 # same as print except the entire document text is returned as a string
3469 sub sprint
3470 { my $t= shift;
3471 my %args= _normalize_args( @_);
3472  
3473 my $old_pretty;
3474 if( defined $args{PrettyPrint})
3475 { $old_pretty= $t->set_pretty_print( $args{PrettyPrint});
3476 delete $args{PrettyPrint};
3477 }
3478  
3479 my $old_empty_tag_style;
3480 if( defined $args{EmptyTags})
3481 { $old_empty_tag_style= $t->set_empty_tag_style( $args{EmptyTags});
3482 delete $args{EmptyTags};
3483 }
3484  
3485 my $string= $t->prolog( %args) # xml declaration and doctype
3486 . $t->_leading_cpi( %args) # leading comments and pi's in 'process' mode
3487 . ( ($t->{twig_root} && $t->{twig_root}->sprint) || '')
3488 . $t->_trailing_cpi # trailing comments and pi's (elements, in 'process' mode)
3489 . $t->_trailing_cpi_text # trailing comments and pi's (in 'keep' mode)
3490 ;
3491 if( $t->{twig_keep_spaces} && $t->{trailing_spaces}) { $string .= $t->{trailing_spaces}; }
3492  
3493 $t->set_pretty_print( $old_pretty) if( defined $old_pretty);
3494 $t->set_empty_tag_style( $old_empty_tag_style) if( defined $old_empty_tag_style);
3495  
3496 return $string;
3497 }
3498  
3499 # this method discards useless elements in a tree
3500 # it does the same thing as a flush except it does not print it
3501 # the second argument is an element, the last purged element
3502 # (this argument is usually set through the purge_up_to method)
3503 sub purge
3504 { my $t= shift;
3505 my $up_to= shift;
3506  
3507 $t->_trigger_tdh if $t->{twig_tdh};
3508  
3509 # the "real" last element processed, as _twig_end has closed it
3510 my $last_elt;
3511 if( $up_to)
3512 { $last_elt= $up_to; }
3513 elsif( $t->{twig_current})
3514 { $last_elt= $t->{twig_current}->{last_child}; }
3515 else
3516 { $last_elt= $t->{twig_root}; }
3517  
3518 my $elt= $t->{twig_root};
3519  
3520 while( $elt)
3521 { my $next_elt;
3522 if( $last_elt && $last_elt->in( $elt))
3523 { $elt->{'flushed'}=1;
3524 $next_elt= $elt->{first_child};
3525 }
3526 else
3527 { # an element before the last one or the last one,
3528 $next_elt= $elt->{next_sibling};
3529 $elt->delete;
3530 last if( $last_elt && ($elt == $last_elt) );
3531 }
3532 $elt= $next_elt;
3533 }
3534  
3535 if( my $ids= $t->{twig_id_list})
3536 { while( my ($id, $elt)= each %$ids) { if( ! defined $elt) { delete $t->{twig_id_list}->{$id} } } }
3537  
3538 return $t;
3539 }
3540  
3541 # flushes up to an element. This method just calls purge
3542 sub purge_up_to
3543 { my $t= shift;
3544 return $t->purge( @_);
3545 }
3546  
3547 sub root
3548 { return $_[0]->{twig_root}; }
3549  
3550 sub normalize
3551 { return $_[0]->root->normalize; }
3552  
3553 # create accessor methods on attribute names
3554 { my %accessor; # memorize accessor names so re-creating them won't trigger an error
3555 sub att_accessors
3556 {
3557 my $twig_or_class= shift;
3558 my $elt_class= ref $twig_or_class ? $twig_or_class->{twig_elt_class}
3559 : 'XML::Twig::Elt'
3560 ;
3561 ## no critic (TestingAndDebugging::ProhibitNoStrict);
3562 no strict 'refs';
3563 foreach my $att (@_)
3564 { _croak( "attempt to redefine existing method $att using att_accessors")
3565 if( $elt_class->can( $att) && !$accessor{$att});
3566  
3567 if( !$accessor{$att})
3568 { *{"$elt_class\::$att"}=
3569 sub :lvalue
3570 { my $elt= shift;
3571 if( @_) { $elt->{att}->{$att}= $_[0]; }
3572 $elt->{att}->{$att};
3573 };
3574 $accessor{$att}=1;
3575 }
3576 }
3577 return $twig_or_class;
3578 }
3579 }
3580  
3581 { my %accessor; # memorize accessor names so re-creating them won't trigger an error
3582 sub elt_accessors
3583 {
3584 my $twig_or_class= shift;
3585 my $elt_class= ref $twig_or_class ? $twig_or_class->{twig_elt_class}
3586 : 'XML::Twig::Elt'
3587 ;
3588  
3589 # if arg is a hash ref, it's exp => name, otherwise it's a list of tags
3590 my %exp_to_alias= ref( $_[0]) && isa( $_[0], 'HASH') ? %{$_[0]}
3591 : map { $_ => $_ } @_;
3592 ## no critic (TestingAndDebugging::ProhibitNoStrict);
3593 no strict 'refs';
3594 while( my( $alias, $exp)= each %exp_to_alias )
3595 { if( $elt_class->can( $alias) && !$accessor{$alias})
3596 { _croak( "attempt to redefine existing method $alias using elt_accessors"); }
3597  
3598 if( !$accessor{$alias})
3599 { *{"$elt_class\::$alias"}=
3600 sub
3601 { my $elt= shift;
3602 return wantarray ? $elt->children( $exp) : $elt->first_child( $exp);
3603 };
3604 $accessor{$alias}=1;
3605 }
3606 }
3607 return $twig_or_class;
3608 }
3609 }
3610  
3611 { my %accessor; # memorize accessor names so re-creating them won't trigger an error
3612 sub field_accessors
3613 {
3614 my $twig_or_class= shift;
3615 my $elt_class= ref $twig_or_class ? $twig_or_class->{twig_elt_class}
3616 : 'XML::Twig::Elt'
3617 ;
3618 my %exp_to_alias= ref( $_[0]) && isa( $_[0], 'HASH') ? %{$_[0]}
3619 : map { $_ => $_ } @_;
3620  
3621 ## no critic (TestingAndDebugging::ProhibitNoStrict);
3622 no strict 'refs';
3623 while( my( $alias, $exp)= each %exp_to_alias )
3624 { if( $elt_class->can( $alias) && !$accessor{$alias})
3625 { _croak( "attempt to redefine existing method $exp using field_accessors"); }
3626 if( !$accessor{$alias})
3627 { *{"$elt_class\::$alias"}=
3628 sub
3629 { my $elt= shift;
3630 $elt->field( $exp)
3631 };
3632 $accessor{$alias}=1;
3633 }
3634 }
3635 return $twig_or_class;
3636 }
3637 }
3638  
3639 sub first_elt
3640 { my( $t, $cond)= @_;
3641 my $root= $t->root || return undef;
3642 return $root if( $root->passes( $cond));
3643 return $root->next_elt( $cond);
3644 }
3645  
3646 sub last_elt
3647 { my( $t, $cond)= @_;
3648 my $root= $t->root || return undef;
3649 return $root->last_descendant( $cond);
3650 }
3651  
3652 sub next_n_elt
3653 { my( $t, $offset, $cond)= @_;
3654 $offset -- if( $t->root->matches( $cond) );
3655 return $t->root->next_n_elt( $offset, $cond);
3656 }
3657  
3658 sub get_xpath
3659 { my $twig= shift;
3660 if( isa( $_[0], 'ARRAY'))
3661 { my $elt_array= shift;
3662 return _unique_elts( map { $_->get_xpath( @_) } @$elt_array);
3663 }
3664 else
3665 { return $twig->root->get_xpath( @_); }
3666 }
3667  
3668 # get a list of elts and return a sorted list of unique elts
3669 sub _unique_elts
3670 { my @sorted= sort { $a ->cmp( $b) } @_;
3671 my @unique;
3672 while( my $current= shift @sorted)
3673 { push @unique, $current unless( @unique && ($unique[-1] == $current)); }
3674 return @unique;
3675 }
3676  
3677 sub findvalue
3678 { my $twig= shift;
3679 if( isa( $_[0], 'ARRAY'))
3680 { my $elt_array= shift;
3681 return join( '', map { $_->findvalue( @_) } @$elt_array);
3682 }
3683 else
3684 { return $twig->root->findvalue( @_); }
3685 }
3686  
3687 sub findvalues
3688 { my $twig= shift;
3689 if( isa( $_[0], 'ARRAY'))
3690 { my $elt_array= shift;
3691 return map { $_->findvalues( @_) } @$elt_array;
3692 }
3693 else
3694 { return $twig->root->findvalues( @_); }
3695 }
3696  
3697 sub set_id_seed
3698 { my $t= shift;
3699 XML::Twig::Elt->set_id_seed( @_);
3700 return $t;
3701 }
3702  
3703 # return an array ref to an index, or undef
3704 sub index
3705 { my( $twig, $name, $index)= @_;
3706 return defined( $index) ? $twig->{_twig_index}->{$name}->[$index] : $twig->{_twig_index}->{$name};
3707 }
3708  
3709 # return a list with just the root
3710 # if a condition is given then return an empty list unless the root matches
3711 sub children
3712 { my( $t, $cond)= @_;
3713 my $root= $t->root;
3714 unless( $cond && !($root->passes( $cond)) )
3715 { return ($root); }
3716 else
3717 { return (); }
3718 }
3719  
3720 sub _children { return ($_[0]->root); }
3721  
3722 # weird, but here for completude
3723 # used to solve (non-sensical) /doc[1] XPath queries
3724 sub child
3725 { my $t= shift;
3726 my $nb= shift;
3727 return ($t->children( @_))[$nb];
3728 }
3729  
3730 sub descendants
3731 { my( $t, $cond)= @_;
3732 my $root= $t->root;
3733 if( $root->passes( $cond) )
3734 { return ($root, $root->descendants( $cond)); }
3735 else
3736 { return ( $root->descendants( $cond)); }
3737 }
3738  
3739 sub simplify { my $t= shift; $t->root->simplify( @_); }
3740 sub subs_text { my $t= shift; $t->root->subs_text( @_); }
3741 sub trim { my $t= shift; $t->root->trim( @_); $t->root->trim; return $t; }
3742  
3743 sub set_keep_encoding
3744 { my( $t, $keep)= @_;
3745 $t->{twig_keep_encoding}= $keep;
3746 $t->{NoExpand}= $keep;
3747 return XML::Twig::Elt::set_keep_encoding( $keep);
3748 }
3749  
3750 sub set_expand_external_entities
3751 { return XML::Twig::Elt::set_expand_external_entities( @_); }
3752  
3753 sub escape_gt
3754 { my $t= shift; $t->{twig_escape_gt}= 1; return XML::Twig::Elt::escape_gt( @_); }
3755  
3756 sub do_not_escape_gt
3757 { my $t= shift; $t->{twig_escape_gt}= 0; return XML::Twig::Elt::do_not_escape_gt( @_); }
3758  
3759 sub elt_id
3760 { return $_[0]->{twig_id_list}->{$_[1]}; }
3761  
3762 # add a gi to the gi table, returns its index
3763 # if the gi already exists, just return it's index
3764 sub _add_gi
3765 { my( $gi )= @_;
3766 if ( ! exists $XML::Twig::gi2index{$gi} )
3767 { push @XML::Twig::index2gi, $gi;
3768 $XML::Twig::gi2index{$gi}= $#XML::Twig::index2gi;
3769 }
3770 return $XML::Twig::gi2index{$gi};
3771 }
3772  
3773 # change it in ALL twigs at the moment
3774 sub change_gi
3775 { my( $twig, $old_gi, $new_gi)= @_;
3776 my $index;
3777 return unless($index= $XML::Twig::gi2index{$old_gi});
3778 $XML::Twig::index2gi[$index]= $new_gi;
3779 delete $XML::Twig::gi2index{$old_gi};
3780 $XML::Twig::gi2index{$new_gi}= $index;
3781 return $twig;
3782 }
3783  
3784 # builds the DTD from the stored (possibly updated) data
3785 sub dtd_text
3786 { my $t= shift;
3787 my $dtd= $t->{twig_dtd};
3788 my $doctype= $t->{twig_doctype} or return '';
3789 my $string= "<!DOCTYPE ".$doctype->{name};
3790  
3791 $string .= " [\n";
3792  
3793 foreach my $gi (@{$dtd->{elt_list}})
3794 { $string.= "<!ELEMENT $gi ".$dtd->{model}->{$gi}.">\n" ;
3795 if( $dtd->{att}->{$gi})
3796 { my $attlist= $dtd->{att}->{$gi};
3797 $string.= "<!ATTLIST $gi\n";
3798 foreach my $att ( sort keys %{$attlist})
3799 {
3800 if( $attlist->{$att}->{fixed})
3801 { $string.= " $att $attlist->{$att}->{type} #FIXED $attlist->{$att}->{default}"; }
3802 else
3803 { $string.= " $att $attlist->{$att}->{type} $attlist->{$att}->{default}"; }
3804 $string.= "\n";
3805 }
3806 $string.= ">\n";
3807 }
3808 }
3809 $string.= $t->entity_list->text if( $t->entity_list);
3810 $string.= "\n]>\n";
3811 return $string;
3812 }
3813  
3814 # prints the DTD from the stored (possibly updated) data
3815 sub dtd_print
3816 { my $t= shift;
3817 my $fh= isa( $_[0], 'GLOB') || isa( $_[0], 'IO::Scalar') ? shift : undef;
3818 if( $fh) { print $fh $t->dtd_text; }
3819 else { print $t->dtd_text; }
3820 return $t;
3821 }
3822  
3823 # build the subs that call directly expat
3824 BEGIN
3825 { my @expat_methods= qw( depth in_element within_element context
3826 current_line current_column current_byte
3827 recognized_string original_string
3828 xpcroak xpcarp
3829 base current_element element_index
3830 xml_escape
3831 position_in_context);
3832 foreach my $method (@expat_methods)
3833 {
3834 ## no critic (TestingAndDebugging::ProhibitNoStrict);
3835 no strict 'refs';
3836 *{$method}= sub { my $t= shift;
3837 _croak( "calling $method after parsing is finished") unless( $t->{twig_parsing});
3838 return $t->{twig_parser}->$method(@_);
3839 };
3840 }
3841 }
3842  
3843 sub path
3844 { my( $t, $gi)= @_;
3845 if( $t->{twig_map_xmlns})
3846 { return "/" . join( "/", map { $t->_replace_prefix( $_)} ($t->{twig_parser}->context, $gi)); }
3847 else
3848 { return "/" . join( "/", ($t->{twig_parser}->context, $gi)); }
3849 }
3850  
3851 sub finish
3852 { my $t= shift;
3853 return $t->{twig_parser}->finish;
3854 }
3855  
3856 # just finish the parse by printing the rest of the document
3857 sub finish_print
3858 { my( $t, $fh)= @_;
3859 my $old_fh;
3860 unless( defined $fh)
3861 { $t->_set_fh_to_twig_output_fh(); }
3862 elsif( defined $fh)
3863 { $old_fh= select $fh;
3864 $t->{twig_original_selected_fh}= $old_fh if( $old_fh);
3865 }
3866  
3867 my $p=$t->{twig_parser};
3868 if( $t->{twig_keep_encoding})
3869 { $p->setHandlers( %twig_handlers_finish_print); }
3870 else
3871 { $p->setHandlers( %twig_handlers_finish_print_original); }
3872 return $t;
3873 }
3874  
3875 sub set_remove_cdata { return XML::Twig::Elt::set_remove_cdata( @_); }
3876  
3877 sub output_filter { return XML::Twig::Elt::output_filter( @_); }
3878 sub set_output_filter { return XML::Twig::Elt::set_output_filter( @_); }
3879  
3880 sub output_text_filter { return XML::Twig::Elt::output_text_filter( @_); }
3881 sub set_output_text_filter { return XML::Twig::Elt::set_output_text_filter( @_); }
3882  
3883 sub set_input_filter
3884 { my( $t, $input_filter)= @_;
3885 my $old_filter= $t->{twig_input_filter};
3886 if( !$input_filter || isa( $input_filter, 'CODE') )
3887 { $t->{twig_input_filter}= $input_filter; }
3888 elsif( $input_filter eq 'latin1')
3889 { $t->{twig_input_filter}= latin1(); }
3890 elsif( $filter{$input_filter})
3891 { $t->{twig_input_filter}= $filter{$input_filter}; }
3892 else
3893 { _croak( "invalid input filter: $input_filter"); }
3894  
3895 return $old_filter;
3896 }
3897  
3898 sub set_empty_tag_style
3899 { return XML::Twig::Elt::set_empty_tag_style( @_); }
3900  
3901 sub set_pretty_print
3902 { return XML::Twig::Elt::set_pretty_print( @_); }
3903  
3904 sub set_quote
3905 { return XML::Twig::Elt::set_quote( @_); }
3906  
3907 sub set_indent
3908 { return XML::Twig::Elt::set_indent( @_); }
3909  
3910 sub set_keep_atts_order
3911 { shift; return XML::Twig::Elt::set_keep_atts_order( @_); }
3912  
3913 sub keep_atts_order
3914 { return XML::Twig::Elt::keep_atts_order( @_); }
3915  
3916 sub set_do_not_escape_amp_in_atts
3917 { return XML::Twig::Elt::set_do_not_escape_amp_in_atts( @_); }
3918  
3919 # save and restore package globals (the ones in XML::Twig::Elt)
3920 # should probably return the XML::Twig object itself, but instead
3921 # returns the state (as a hashref) for backward compatibility
3922 sub save_global_state
3923 { my $t= shift;
3924 return $t->{twig_saved_state}= XML::Twig::Elt::global_state();
3925 }
3926  
3927 sub restore_global_state
3928 { my $t= shift;
3929 XML::Twig::Elt::set_global_state( $t->{twig_saved_state});
3930 }
3931  
3932 sub global_state
3933 { return XML::Twig::Elt::global_state(); }
3934  
3935 sub set_global_state
3936 { return XML::Twig::Elt::set_global_state( $_[1]); }
3937  
3938 sub dispose
3939 { my $t= shift;
3940 $t->DESTROY;
3941 return;
3942 }
3943  
3944 sub DESTROY
3945 { my $t= shift;
3946 if( $t->{twig_root} && isa( $t->{twig_root}, 'XML::Twig::Elt'))
3947 { $t->{twig_root}->delete }
3948  
3949 # added to break circular references
3950 undef $t->{twig};
3951 undef $t->{twig_root}->{twig} if( $t->{twig_root});
3952 undef $t->{twig_parser};
3953  
3954 undef %$t;# prevents memory leaks (especially when using mod_perl)
3955 undef $t;
3956 }
3957  
3958 # return true if perl was compiled using perlio
3959 # if perl is not available return true, these days perlio should be used
3960 sub _use_perlio
3961 { return $Config{useperlio} eq 'define'; }
3962  
3963  
3964 # returns the parth to the perl executable (if available)
3965 sub _this_perl
3966 { # straight from perlvar
3967 my $secure_perl_path= $Config{perlpath};
3968 if ($^O ne 'VMS')
3969 { $secure_perl_path .= $Config{_exe} unless $secure_perl_path =~ m/$Config{_exe}$/i; }
3970 if( ! -f $secure_perl_path) { $secure_perl_path= ''; } # when perl is not available (PDK)
3971 return $secure_perl_path;
3972 }
3973  
3974 #
3975 # non standard handlers
3976 #
3977  
3978 # kludge: expat 1.95.2 calls both Default AND Doctype handlers
3979 # so if the default handler finds '<!DOCTYPE' then it must
3980 # unset itself (_twig_print_doctype will reset it)
3981 sub _twig_print_check_doctype
3982 { # warn " in _twig_print_check_doctype...\n"; # DEBUG handler
3983  
3984 my $p= shift;
3985 my $string= $p->recognized_string();
3986 if( $string eq '<!DOCTYPE')
3987 {
3988 $p->setHandlers( Default => undef);
3989 $p->setHandlers( Entity => undef);
3990 $expat_1_95_2=1;
3991 }
3992 else
3993 { print $string; }
3994  
3995 return;
3996 }
3997  
3998 sub _twig_print
3999 { # warn " in _twig_print...\n"; # DEBUG handler
4000 my $p= shift;
4001 if( $expat_1_95_2 && ($p->recognized_string eq '[') && !$p->{twig}->{expat_1_95_2_seen_bracket})
4002 { # otherwise the opening square bracket of the doctype gets printed twice
4003 $p->{twig}->{expat_1_95_2_seen_bracket}=1;
4004 }
4005 else
4006 { if( $p->{twig}->{twig_right_after_root})
4007 { my $s= $p->recognized_string(); print $s if $s=~ m{\S}; }
4008 else
4009 { print $p->recognized_string(); }
4010 }
4011 return;
4012 }
4013 # recognized_string does not seem to work for entities, go figure!
4014 # so this handler is used to print them anyway
4015 sub _twig_print_entity
4016 { # warn " in _twig_print_entity...\n"; # DEBUG handler
4017 my $p= shift;
4018 XML::Twig::Entity->new( @_)->print;
4019 }
4020  
4021 # kludge: expat 1.95.2 calls both Default AND Doctype handlers
4022 # so if the default handler finds '<!DOCTYPE' then it must
4023 # unset itself (_twig_print_doctype will reset it)
4024 sub _twig_print_original_check_doctype
4025 { # warn " in _twig_print_original_check_doctype...\n"; # DEBUG handler
4026  
4027 my $p= shift;
4028 my $string= $p->original_string();
4029 if( $string eq '<!DOCTYPE')
4030 { $p->setHandlers( Default => undef);
4031 $p->setHandlers( Entity => undef);
4032 $expat_1_95_2=1;
4033 }
4034 else
4035 { print $string; }
4036  
4037 return;
4038 }
4039  
4040 sub _twig_print_original
4041 { # warn " in _twig_print_original...\n"; # DEBUG handler
4042 my $p= shift;
4043 print $p->original_string();
4044 return;
4045 }
4046  
4047 sub _twig_print_original_doctype
4048 { # warn " in _twig_print_original_doctype...\n"; # DEBUG handler
4049 my( $p, $name, $sysid, $pubid, $internal)= @_;
4050 _print_doctype(@_);
4051 $p->setHandlers( Default => \&_twig_print_original);
4052 return;
4053 }
4054  
4055 sub _twig_print_doctype
4056 { # warn " in _twig_print_doctype...\n"; # DEBUG handler
4057 my( $p, $name, $sysid, $pubid, $internal)= @_;
4058 _print_doctype(@_);
4059 $p->setHandlers( Default => \&_twig_print);
4060 return;
4061 }
4062  
4063 sub _print_doctype
4064 { my( $p, $name, $sysid, $pubid, $internal)= @_;
4065 if( $name)
4066 { # with recent versions of XML::Parser original_string does not work,
4067 # hence we need to rebuild the doctype declaration
4068 my $doctype='';
4069 $doctype .= qq{<!DOCTYPE $name} if( $name);
4070 $doctype .= qq{ PUBLIC "$pubid"} if( $pubid);
4071 $doctype .= qq{ SYSTEM} if( $sysid && !$pubid);
4072 $doctype .= qq{ "$sysid"} if( $sysid);
4073 $doctype .= ' [' if( $internal) ;
4074 $doctype .= qq{>} unless( $internal || $expat_1_95_2);
4075 $p->{twig}->{twig_doctype}->{has_internal}=$internal;
4076 print $doctype;
4077 }
4078 return;
4079 }
4080  
4081 sub _twig_print_original_default
4082 { # warn " in _twig_print_original_default...\n"; # DEBUG handler
4083 my $p= shift;
4084 print $p->original_string();
4085 return;
4086 }
4087  
4088 # account for the case where the element is empty
4089 sub _twig_print_end_original
4090 { # warn " in _twig_print_end_original...\n"; # DEBUG handler
4091 my $p= shift;
4092 print $p->original_string();
4093 return;
4094 }
4095  
4096 sub _twig_start_check_roots
4097 { # warn " in _twig_start_check_roots...\n"; # DEBUG handler
4098 my $p= shift;
4099 my $gi= shift;
4100  
4101 my $t= $p->{twig};
4102  
4103 my $fh= $t->{twig_output_fh} || select() || \*STDOUT;
4104  
4105 my $ns_decl;
4106 unless( $p->depth == 0)
4107 { if( $t->{twig_map_xmlns}) { $ns_decl= _replace_ns( $t, \$gi, \@_); }
4108 }
4109  
4110 my $context= { $ST_TAG => $gi, @_};
4111 $context->{$ST_NS}= $ns_decl if $ns_decl;
4112 push @{$t->{_twig_context_stack}}, $context;
4113 my %att= @_;
4114  
4115 if( _handler( $t, $t->{twig_roots}, $gi))
4116 { $p->setHandlers( %twig_handlers); # restore regular handlers
4117 $t->{twig_root_depth}= $p->depth;
4118 pop @{$t->{_twig_context_stack}}; # will be pushed back in _twig_start
4119 _twig_start( $p, $gi, @_);
4120 return;
4121 }
4122  
4123 # $tag will always be true if it needs to be printed (the tag string is never empty)
4124 my $tag= $t->{twig_default_print} ? $t->{twig_keep_encoding} ? $p->original_string
4125 : $p->recognized_string
4126 : '';
4127  
4128 if( $p->depth == 0)
4129 {
4130 ## no critic (TestingAndDebugging::ProhibitNoStrict);
4131 no strict 'refs';
4132 print {$fh} $tag if( $tag);
4133 pop @{$t->{_twig_context_stack}}; # will be pushed back in _twig_start
4134 _twig_start( $p, $gi, @_);
4135 $t->root->{'flushed'}=1; # or the root start tag gets output the first time we flush
4136 }
4137 elsif( $t->{twig_starttag_handlers})
4138 { # look for start tag handlers
4139  
4140 my @handlers= _handler( $t, $t->{twig_starttag_handlers}, $gi);
4141 my $last_handler_res;
4142 foreach my $handler ( @handlers)
4143 { $last_handler_res= $handler->($t, $gi, %att);
4144 last unless $last_handler_res;
4145 }
4146 ## no critic (TestingAndDebugging::ProhibitNoStrict);
4147 no strict 'refs';
4148 print {$fh} $tag if( $tag && (!@handlers || $last_handler_res));
4149 }
4150 else
4151 {
4152 ## no critic (TestingAndDebugging::ProhibitNoStrict);
4153 no strict 'refs';
4154 print {$fh} $tag if( $tag);
4155 }
4156 return;
4157 }
4158  
4159 sub _twig_end_check_roots
4160 { # warn " in _twig_end_check_roots...\n"; # DEBUG handler
4161  
4162 my( $p, $gi, %att)= @_;
4163 my $t= $p->{twig};
4164 # $tag can be empty (<elt/>), hence the undef and the tests for defined
4165 my $tag= $t->{twig_default_print} ? $t->{twig_keep_encoding} ? $p->original_string
4166 : $p->recognized_string
4167 : undef;
4168 my $fh= $t->{twig_output_fh} || select() || \*STDOUT;
4169  
4170 if( $t->{twig_endtag_handlers})
4171 { # look for end tag handlers
4172 my @handlers= _handler( $t, $t->{twig_endtag_handlers}, $gi);
4173 my $last_handler_res=1;
4174 foreach my $handler ( @handlers)
4175 { $last_handler_res= $handler->($t, $gi) || last; }
4176 }
4177 {
4178 ## no critic (TestingAndDebugging::ProhibitNoStrict);
4179 no strict 'refs';
4180 print {$fh} $tag if( defined $tag);
4181 }
4182 if( $p->depth == 0)
4183 {
4184 _twig_end( $p, $gi);
4185 $t->root->{end_tag_flushed}=1;
4186 }
4187  
4188 pop @{$t->{_twig_context_stack}};
4189 return;
4190 }
4191  
4192 sub _twig_pi_check_roots
4193 { # warn " in _twig_pi_check_roots...\n"; # DEBUG handler
4194 my( $p, $target, $data)= @_;
4195 my $t= $p->{twig};
4196 my $pi= $t->{twig_default_print} ? $t->{twig_keep_encoding} ? $p->original_string
4197 : $p->recognized_string
4198 : undef;
4199 my $fh= $t->{twig_output_fh} || select() || \*STDOUT;
4200  
4201 if( my $handler= $t->{twig_handlers}->{pi_handlers}->{$target}
4202 || $t->{twig_handlers}->{pi_handlers}->{''}
4203 )
4204 { # if handler is called on pi, then it needs to be processed as a regular node
4205 my @flags= qw( twig_process_pi twig_keep_pi);
4206 my @save= @{$t}{@flags}; # save pi related flags
4207 @{$t}{@flags}= (1, 0); # override them, pi needs to be processed
4208 _twig_pi( @_); # call handler on the pi
4209 @{$t}{@flags}= @save;; # restore flag
4210 }
4211 else
4212 {
4213 ## no critic (TestingAndDebugging::ProhibitNoStrict);
4214 no strict 'refs';
4215 print {$fh} $pi if( defined( $pi));
4216 }
4217 return;
4218 }
4219  
4220 sub _output_ignored
4221 { my( $t, $p)= @_;
4222 my $action= $t->{twig_ignore_action};
4223  
4224 my $get_string= $t->{twig_keep_encoding} ? 'original_string' : 'recognized_string';
4225  
4226 if( $action eq 'print' ) { print $p->$get_string; }
4227 else
4228 { my $string_ref;
4229 if( $action eq 'string')
4230 { $string_ref= \$t->{twig_buffered_string}; }
4231 elsif( ref( $action) && ref( $action) eq 'SCALAR')
4232 { $string_ref= $action; }
4233 else
4234 { _croak( "wrong ignore action: $action"); }
4235  
4236 $$string_ref .= $p->$get_string;
4237 }
4238 }
4239  
4240 sub _twig_ignore_start
4241 { # warn " in _twig_ignore_start...\n"; # DEBUG handler
4242  
4243 my( $p, $gi)= @_;
4244 my $t= $p->{twig};
4245 $t->{twig_ignore_level}++;
4246 my $action= $t->{twig_ignore_action};
4247  
4248 $t->_output_ignored( $p) unless $action eq 'discard';
4249 return;
4250 }
4251  
4252 sub _twig_ignore_end
4253 { # warn " in _twig_ignore_end...\n"; # DEBUG handler
4254  
4255 my( $p, $gi)= @_;
4256 my $t= $p->{twig};
4257  
4258 my $action= $t->{twig_ignore_action};
4259 $t->_output_ignored( $p) unless $action eq 'discard';
4260  
4261 $t->{twig_ignore_level}--;
4262  
4263 if( ! $t->{twig_ignore_level})
4264 {
4265 $t->{twig_current} = $t->{twig_ignore_elt};
4266 $t->{twig_current}->{'twig_current'}=1;
4267  
4268 $t->{twig_ignore_elt}->cut; # there could possibly be a memory leak here (delete would avoid it,
4269 # but could also delete elements that should not be deleted)
4270  
4271 # restore the saved stack to the current level
4272 splice( @{$t->{_twig_context_stack}}, $p->depth+ 1 );
4273 #warn "stack: ", _dump_stack( $t->{_twig_context_stack}), "\n";
4274  
4275 $p->setHandlers( @{$t->{twig_saved_handlers}});
4276 # test for handlers
4277 if( $t->{twig_endtag_handlers})
4278 { # look for end tag handlers
4279 my @handlers= _handler( $t, $t->{twig_endtag_handlers}, $gi);
4280 my $last_handler_res=1;
4281 foreach my $handler ( @handlers)
4282 { $last_handler_res= $handler->($t, $gi) || last; }
4283 }
4284 pop @{$t->{_twig_context_stack}};
4285 };
4286 return;
4287 }
4288  
4289 #sub _dump_stack { my( $stack)= @_; return join( ":", map { $_->{$ST_TAG} } @$stack); }
4290  
4291 sub ignore
4292 { my( $t, $elt, $action)= @_;
4293 my $current= $t->{twig_current};
4294  
4295 if( ! ($elt && ref( $elt) && isa( $elt, 'XML::Twig::Elt'))) { $elt= $current; }
4296  
4297 #warn "ignore: current = ", $current->tag, ", elt = ", $elt->tag, ")\n";
4298  
4299 # we need the ($elt == $current->{last_child}) test because the current element is set to the
4300 # parent _before_ handlers are called (and I can't figure out how to fix this)
4301 unless( ($elt == $current) || ($current->{last_child} && ($elt == $current->{last_child})) || $current->in( $elt))
4302 { _croak( "element to be ignored must be ancestor of current element"); }
4303  
4304 $t->{twig_ignore_level}= $current == $elt ? 1 : $t->_level_in_stack( $current) - $t->_level_in_stack($elt) + 1;
4305 #warn "twig_ignore_level: $t->{twig_ignore_level} (current: ", $current->tag, ", elt: ", $elt->tag, ")\n";
4306 $t->{twig_ignore_elt} = $elt; # save it, so we can delete it later
4307  
4308 $action ||= 'discard';
4309 if( !($action eq 'print' || $action eq 'string' || ( ref( $action) && ref( $action) eq 'SCALAR')))
4310 { $action= 'discard'; }
4311  
4312 $t->{twig_ignore_action}= $action;
4313  
4314 my $p= $t->{twig_parser};
4315 my @saved_handlers= $p->setHandlers( %twig_handlers_ignore); # set handlers
4316  
4317 my $get_string= $t->{twig_keep_encoding} ? 'original_string' : 'recognized_string';
4318  
4319 my $default_handler;
4320  
4321 if( $action ne 'discard')
4322 { if( $action eq 'print')
4323 { $p->setHandlers( Default => sub { print $_[0]->$get_string; }); }
4324 else
4325 { my $string_ref;
4326 if( $action eq 'string')
4327 { if( ! exists $t->{twig_buffered_string}) { $t->{twig_buffered_string}=''; }
4328 $string_ref= \$t->{twig_buffered_string};
4329 }
4330 elsif( ref( $action) && ref( $action) eq 'SCALAR')
4331 { $string_ref= $action; }
4332  
4333 $p->setHandlers( Default => sub { $$string_ref .= $_[0]->$get_string; });
4334 }
4335 $t->_output_ignored( $p, $action);
4336 }
4337  
4338 $t->{twig_saved_handlers}= \@saved_handlers; # save current handlers
4339 }
4340  
4341 sub _level_in_stack
4342 { my( $t, $elt)= @_;
4343 my $level=1;
4344 foreach my $elt_in_stack ( @{$t->{_twig_context_stack}} )
4345 { if( $elt_in_stack->{$ST_ELT} && ($elt == $elt_in_stack->{$ST_ELT})) { return $level }
4346 $level++;
4347 }
4348 }
4349  
4350 # select $t->{twig_output_fh} and store the current selected fh
4351 sub _set_fh_to_twig_output_fh
4352 { my $t= shift;
4353 my $output_fh= $t->{twig_output_fh};
4354 if( $output_fh && !$t->{twig_output_fh_selected})
4355 { # there is an output fh
4356 $t->{twig_selected_fh}= select(); # store the currently selected fh
4357 $t->{twig_output_fh_selected}=1;
4358 select $output_fh; # select the output fh for the twig
4359 }
4360 }
4361  
4362 # select the fh that was stored in $t->{twig_selected_fh}
4363 # (before $t->{twig_output_fh} was selected)
4364 sub _set_fh_to_selected_fh
4365 { my $t= shift;
4366 return unless( $t->{twig_output_fh});
4367 my $selected_fh= $t->{twig_selected_fh};
4368 $t->{twig_output_fh_selected}=0;
4369 select $selected_fh;
4370 return;
4371 }
4372  
4373 sub encoding
4374 { return $_[0]->{twig_xmldecl}->{encoding} if( $_[0]->{twig_xmldecl}); }
4375  
4376 sub set_encoding
4377 { my( $t, $encoding)= @_;
4378 $t->{twig_xmldecl} ||={};
4379 $t->set_xml_version( "1.0") unless( $t->xml_version);
4380 $t->{twig_xmldecl}->{encoding}= $encoding;
4381 return $t;
4382 }
4383  
4384 sub output_encoding
4385 { return $_[0]->{output_encoding}; }
4386  
4387 sub set_output_encoding
4388 { my( $t, $encoding)= @_;
4389 my $output_filter= $t->output_filter || '';
4390  
4391 if( ($encoding && $encoding !~ m{^utf-?8$}i) || $t->{twig_keep_encoding} || $output_filter)
4392 { $t->set_output_filter( _encoding_filter( $encoding || '')); }
4393  
4394 $t->{output_encoding}= $encoding;
4395 return $t;
4396 }
4397  
4398 sub xml_version
4399 { return $_[0]->{twig_xmldecl}->{version} if( $_[0]->{twig_xmldecl}); }
4400  
4401 sub set_xml_version
4402 { my( $t, $version)= @_;
4403 $t->{twig_xmldecl} ||={};
4404 $t->{twig_xmldecl}->{version}= $version;
4405 return $t;
4406 }
4407  
4408 sub standalone
4409 { return $_[0]->{twig_xmldecl}->{standalone} if( $_[0]->{twig_xmldecl}); }
4410  
4411 sub set_standalone
4412 { my( $t, $standalone)= @_;
4413 $t->{twig_xmldecl} ||={};
4414 $t->set_xml_version( "1.0") unless( $t->xml_version);
4415 $t->{twig_xmldecl}->{standalone}= $standalone;
4416 return $t;
4417 }
4418  
4419 # SAX methods
4420  
4421 sub toSAX1
4422 { _croak( "cannot use toSAX1 while parsing (use flush_toSAX1)") if (defined $_[0]->{twig_parser});
4423 shift(@_)->_toSAX(@_, \&XML::Twig::Elt::_start_tag_data_SAX1,
4424 \&XML::Twig::Elt::_end_tag_data_SAX1
4425 );
4426 }
4427  
4428 sub toSAX2
4429 { _croak( "cannot use toSAX2 while parsing (use flush_toSAX2)") if (defined $_[0]->{twig_parser});
4430 shift(@_)->_toSAX(@_, \&XML::Twig::Elt::_start_tag_data_SAX2,
4431 \&XML::Twig::Elt::_end_tag_data_SAX2
4432 );
4433 }
4434  
4435 sub _toSAX
4436 { my( $t, $handler, $start_tag_data, $end_tag_data) = @_;
4437  
4438 if( my $start_document = $handler->can( 'start_document'))
4439 { $start_document->( $handler); }
4440  
4441 $t->_prolog_toSAX( $handler);
4442  
4443 if( $t->root) { $t->root->_toSAX( $handler, $start_tag_data, $end_tag_data) ; }
4444 if( my $end_document = $handler->can( 'end_document'))
4445 { $end_document->( $handler); }
4446 }
4447  
4448 sub flush_toSAX1
4449 { shift(@_)->_flush_toSAX(@_, \&XML::Twig::Elt::_start_tag_data_SAX1,
4450 \&XML::Twig::Elt::_end_tag_data_SAX1
4451 );
4452 }
4453  
4454 sub flush_toSAX2
4455 { shift(@_)->_flush_toSAX(@_, \&XML::Twig::Elt::_start_tag_data_SAX2,
4456 \&XML::Twig::Elt::_end_tag_data_SAX2
4457 );
4458 }
4459  
4460 sub _flush_toSAX
4461 { my( $t, $handler, $start_tag_data, $end_tag_data)= @_;
4462  
4463 # the "real" last element processed, as _twig_end has closed it
4464 my $last_elt;
4465 if( $t->{twig_current})
4466 { $last_elt= $t->{twig_current}->{last_child}; }
4467 else
4468 { $last_elt= $t->{twig_root}; }
4469  
4470 my $elt= $t->{twig_root};
4471 unless( $elt->{'flushed'})
4472 { # init unless already done (ie root has been flushed)
4473 if( my $start_document = $handler->can( 'start_document'))
4474 { $start_document->( $handler); }
4475 # flush the DTD
4476 $t->_prolog_toSAX( $handler)
4477 }
4478  
4479 while( $elt)
4480 { my $next_elt;
4481 if( $last_elt && $last_elt->in( $elt))
4482 {
4483 unless( $elt->{'flushed'})
4484 { # just output the front tag
4485 if( my $start_element = $handler->can( 'start_element'))
4486 { if( my $tag_data= $start_tag_data->( $elt))
4487 { $start_element->( $handler, $tag_data); }
4488 }
4489 $elt->{'flushed'}=1;
4490 }
4491 $next_elt= $elt->{first_child};
4492 }
4493 else
4494 { # an element before the last one or the last one,
4495 $next_elt= $elt->{next_sibling};
4496 $elt->_toSAX( $handler, $start_tag_data, $end_tag_data);
4497 $elt->delete;
4498 last if( $last_elt && ($elt == $last_elt));
4499 }
4500 $elt= $next_elt;
4501 }
4502 if( !$t->{twig_parsing})
4503 { if( my $end_document = $handler->can( 'end_document'))
4504 { $end_document->( $handler); }
4505 }
4506 }
4507  
4508 sub _prolog_toSAX
4509 { my( $t, $handler)= @_;
4510 $t->_xmldecl_toSAX( $handler);
4511 $t->_DTD_toSAX( $handler);
4512 }
4513  
4514 sub _xmldecl_toSAX
4515 { my( $t, $handler)= @_;
4516 my $decl= $t->{twig_xmldecl};
4517 my $data= { Version => $decl->{version},
4518 Encoding => $decl->{encoding},
4519 Standalone => $decl->{standalone},
4520 };
4521 if( my $xml_decl= $handler->can( 'xml_decl'))
4522 { $xml_decl->( $handler, $data); }
4523 }
4524  
4525 sub _DTD_toSAX
4526 { my( $t, $handler)= @_;
4527 my $doctype= $t->{twig_doctype};
4528 return unless( $doctype);
4529 my $data= { Name => $doctype->{name},
4530 PublicId => $doctype->{pub},
4531 SystemId => $doctype->{sysid},
4532 };
4533  
4534 if( my $start_dtd= $handler->can( 'start_dtd'))
4535 { $start_dtd->( $handler, $data); }
4536  
4537 # I should call code to export the internal subset here
4538  
4539 if( my $end_dtd= $handler->can( 'end_dtd'))
4540 { $end_dtd->( $handler); }
4541 }
4542  
4543 # input/output filters
4544  
4545 sub latin1
4546 { local $SIG{__DIE__};
4547 if( _use( 'Encode'))
4548 { return encode_convert( 'ISO-8859-15'); }
4549 elsif( _use( 'Text::Iconv'))
4550 { return iconv_convert( 'ISO-8859-15'); }
4551 elsif( _use( 'Unicode::Map8') && _use( 'Unicode::String'))
4552 { return unicode_convert( 'ISO-8859-15'); }
4553 else
4554 { return \&regexp2latin1; }
4555 }
4556  
4557 sub _encoding_filter
4558 {
4559 { local $SIG{__DIE__};
4560 my $encoding= $_[1] || $_[0];
4561 if( _use( 'Encode'))
4562 { my $sub= encode_convert( $encoding);
4563 return $sub;
4564 }
4565 elsif( _use( 'Text::Iconv'))
4566 { return iconv_convert( $encoding); }
4567 elsif( _use( 'Unicode::Map8') && _use( 'Unicode::String'))
4568 { return unicode_convert( $encoding); }
4569 }
4570 _croak( "Encode, Text::Iconv or Unicode::Map8 and Unicode::String need to be installed in order to use encoding options");
4571 }
4572  
4573 # shamelessly lifted from XML::TyePYX (works only with XML::Parse 2.27)
4574 sub regexp2latin1
4575 { my $text=shift;
4576 $text=~s{([\xc0-\xc3])(.)}{ my $hi = ord($1);
4577 my $lo = ord($2);
4578 chr((($hi & 0x03) <<6) | ($lo & 0x3F))
4579 }ge;
4580 return $text;
4581 }
4582  
4583 sub html_encode
4584 { _use( 'HTML::Entities') or croak "cannot use html_encode: missing HTML::Entities";
4585 return HTML::Entities::encode_entities($_[0] );
4586 }
4587  
4588 sub safe_encode
4589 { my $str= shift;
4590 $str= encode( ascii => $str, $FB_HTMLCREF);
4591 return $str;
4592 }
4593  
4594 sub safe_encode_hex
4595 { my $str= shift;
4596 $str= encode( ascii => $str, $FB_XMLCREF);
4597 return $str;
4598 }
4599  
4600 # this one shamelessly lifted from XML::DOM
4601 sub _XmlUtf8Decode
4602 { my ($str, $hex) = @_;
4603 my $len = length ($str);
4604 my $n;
4605  
4606 if ($len == 2)
4607 { my @n = unpack "C2", $str;
4608 $n = (($n[0] & 0x3f) << 6) + ($n[1] & 0x3f);
4609 }
4610 elsif ($len == 3)
4611 { my @n = unpack "C3", $str;
4612 $n = (($n[0] & 0x1f) << 12) + (($n[1] & 0x3f) << 6) + ($n[2] & 0x3f);
4613 }
4614 elsif ($len == 4)
4615 { my @n = unpack "C4", $str;
4616 $n = (($n[0] & 0x0f) << 18) + (($n[1] & 0x3f) << 12)
4617 + (($n[2] & 0x3f) << 6) + ($n[3] & 0x3f);
4618 }
4619 elsif ($len == 1) # just to be complete...
4620 { $n = ord ($str); }
4621 else
4622 { croak "bad value [$str] for _XmlUtf8Decode"; }
4623  
4624 my $char= $hex ? sprintf ("&#x%x;", $n) : "&#$n;";
4625 return $char;
4626 }
4627  
4628 # deprecated, was used for unicode processing pre perl 5.8.0
4629 sub unicode_convert
4630 { my $enc= $_[1] ? $_[1] : $_[0]; # so the method can be called on the twig or directly
4631 _use( 'Unicode::Map8') or croak "Unicode::Map8 not available, needed for encoding filter: $!";
4632 _use( 'Unicode::String') or croak "Unicode::String not available, needed for encoding filter: $!";
4633 import Unicode::String qw(utf8);
4634 ## no critic (ProhibitStringyEval)
4635 my $sub= eval qq{ { $NO_WARNINGS;
4636 my \$cnv;
4637 BEGIN { \$cnv= Unicode::Map8->new(\$enc)
4638 or croak "Can't create converter to \$enc";
4639 }
4640 sub { return \$cnv->to8 (utf8(\$_[0])->ucs2); }
4641 }
4642 };
4643 unless( $sub) { croak $@; }
4644 return $sub;
4645 }
4646  
4647 sub iconv_convert
4648 { my $enc= $_[1] ? $_[1] : $_[0]; # so the method can be called on the twig or directly
4649 _use( 'Text::Iconv') or croak "Text::Iconv not available, needed for encoding filter: $!";
4650 ## no critic (ProhibitStringyEval)
4651 my $sub= eval qq{ { $NO_WARNINGS;
4652 my \$cnv;
4653 BEGIN { \$cnv = Text::Iconv->new( 'utf8', \$enc)
4654 or croak "Can't create iconv converter to \$enc";
4655 }
4656 sub { return \$cnv->convert( \$_[0]); }
4657 }
4658 };
4659 unless( $sub)
4660 { if( $@=~ m{^Unsupported conversion: Invalid argument})
4661 { croak "Unsupported encoding: $enc"; }
4662 else
4663 { croak $@; }
4664 }
4665  
4666 return $sub;
4667 }
4668  
4669 sub encode_convert
4670 { my $enc= $_[1] ? $_[1] : $_[0]; # so the method can be called on the twig or directly
4671 my $sub= eval qq{sub { $NO_WARNINGS; return encode( "$enc", \$_[0]); } }; ## no critic (ProhibitStringyEval)
4672 croak "can't create Encode-based filter: $@" unless( $sub);
4673 return $sub;
4674 }
4675  
4676 # XML::XPath compatibility
4677 sub getRootNode { return $_[0]; }
4678 sub getParentNode { return undef; }
4679 sub getChildNodes { my @children= ($_[0]->root); return wantarray ? @children : \@children; }
4680  
4681 sub _dump
4682 { my $t= shift;
4683 my $dump='';
4684  
4685 $dump="document\n"; # should dump twig level data here
4686 if( $t->root) { $dump .= $t->root->_dump( @_); }
4687  
4688 return $dump;
4689  
4690 }
4691  
4692 1;
4693  
4694 ######################################################################
4695 package XML::Twig::Entity_list;
4696 ######################################################################
4697  
4698 *isa= *UNIVERSAL::isa;
4699  
4700 sub new
4701 { my $class = shift;
4702 my $self={ entities => {}, updated => 0};
4703  
4704 bless $self, $class;
4705 return $self;
4706  
4707 }
4708  
4709 sub add_new_ent
4710 { my $ent_list= shift;
4711 my $ent= XML::Twig::Entity->new( @_);
4712 $ent_list->add( $ent);
4713 return $ent_list;
4714 }
4715  
4716 sub _add_list
4717 { my( $ent_list, $to_add)= @_;
4718 my $ents_to_add= $to_add->{entities};
4719 return $ent_list unless( $ents_to_add && %$ents_to_add);
4720 @{$ent_list->{entities}}{keys %$ents_to_add}= values %$ents_to_add;
4721 $ent_list->{updated}=1;
4722 return $ent_list;
4723 }
4724  
4725 sub add
4726 { my( $ent_list, $ent)= @_;
4727 $ent_list->{entities}->{$ent->{name}}= $ent;
4728 $ent_list->{updated}=1;
4729 return $ent_list;
4730 }
4731  
4732 sub ent
4733 { my( $ent_list, $ent_name)= @_;
4734 return $ent_list->{entities}->{$ent_name};
4735 }
4736  
4737 # can be called with an entity or with an entity name
4738 sub delete
4739 { my $ent_list= shift;
4740 if( isa( ref $_[0], 'XML::Twig::Entity'))
4741 { # the second arg is an entity
4742 my $ent= shift;
4743 delete $ent_list->{entities}->{$ent->{name}};
4744 }
4745 else
4746 { # the second arg was not entity, must be a string then
4747 my $name= shift;
4748 delete $ent_list->{entities}->{$name};
4749 }
4750 $ent_list->{updated}=1;
4751 return $ent_list;
4752 }
4753  
4754 sub print
4755 { my ($ent_list, $fh)= @_;
4756 my $old_select= defined $fh ? select $fh : undef;
4757  
4758 foreach my $ent_name ( sort keys %{$ent_list->{entities}})
4759 { my $ent= $ent_list->{entities}->{$ent_name};
4760 # we have to test what the entity is or un-defined entities can creep in
4761 if( isa( $ent, 'XML::Twig::Entity')) { $ent->print(); }
4762 }
4763 select $old_select if( defined $old_select);
4764 return $ent_list;
4765 }
4766  
4767 sub text
4768 { my ($ent_list)= @_;
4769 return join "\n", map { $ent_list->{entities}->{$_}->text} sort keys %{$ent_list->{entities}};
4770 }
4771  
4772 # return the list of entity names
4773 sub entity_names
4774 { my $ent_list= shift;
4775 return (sort keys %{$ent_list->{entities}}) ;
4776 }
4777  
4778 sub list
4779 { my ($ent_list)= @_;
4780 return map { $ent_list->{entities}->{$_} } sort keys %{$ent_list->{entities}};
4781 }
4782  
4783 1;
4784  
4785 ######################################################################
4786 package XML::Twig::Entity;
4787 ######################################################################
4788  
4789 sub new
4790 { my( $class, $name, $val, $sysid, $pubid, $ndata, $param)= @_;
4791 $class= ref( $class) || $class;
4792  
4793 my $self={};
4794  
4795 $self->{name} = $name;
4796 $self->{val} = $val if( defined $val );
4797 $self->{sysid} = $sysid if( defined $sysid);
4798 $self->{pubid} = $pubid if( defined $pubid);
4799 $self->{ndata} = $ndata if( defined $ndata);
4800 $self->{param} = $param if( defined $param);
4801  
4802 bless $self, $class;
4803 return $self;
4804 }
4805  
4806 sub name { return $_[0]->{name}; }
4807 sub val { return $_[0]->{val}; }
4808 sub sysid { return defined( $_[0]->{sysid}) ? $_[0]->{sysid} : ''; }
4809 sub pubid { return defined( $_[0]->{pubid}) ? $_[0]->{pubid} : ''; }
4810 sub ndata { return defined( $_[0]->{ndata}) ? $_[0]->{ndata} : ''; }
4811 sub param { return defined( $_[0]->{param}) ? $_[0]->{param} : ''; }
4812  
4813 sub print
4814 { my ($ent, $fh)= @_;
4815 my $text= $ent->text;
4816 if( $fh) { print $fh $text . "\n"; }
4817 else { print $text . "\n"; }
4818 }
4819  
4820 sub sprint
4821 { my ($ent)= @_;
4822 return $ent->text;
4823 }
4824  
4825 sub text
4826 { my ($ent)= @_;
4827 #warn "text called: '", $ent->_dump, "'\n";
4828 return '' if( !$ent->{name});
4829 my @tokens;
4830 push @tokens, '<!ENTITY';
4831  
4832 push @tokens, '%' if( $ent->{param});
4833 push @tokens, $ent->{name};
4834  
4835 if( defined $ent->{val} && !defined( $ent->{sysid}) && !defined($ent->{pubid}) )
4836 { push @tokens, _quoted_val( $ent->{val});
4837 }
4838 elsif( defined $ent->{sysid})
4839 { push @tokens, 'PUBLIC', _quoted_val( $ent->{pubid}) if( $ent->{pubid});
4840 push @tokens, 'SYSTEM' unless( $ent->{pubid});
4841 push @tokens, _quoted_val( $ent->{sysid});
4842 push @tokens, 'NDATA', $ent->{ndata} if( $ent->{ndata});
4843 }
4844 return join( ' ', @tokens) . '>';
4845 }
4846  
4847 sub _quoted_val
4848 { my $q= $_[0]=~ m{"} ? q{'} : q{"};
4849 return qq{$q$_[0]$q};
4850 }
4851  
4852 sub _dump
4853 { my( $ent)= @_; return join( " - ", map { "$_ => '$ent->{$_}'" } grep { defined $ent->{$_} } sort keys %$ent); }
4854  
4855 1;
4856  
4857 ######################################################################
4858 package XML::Twig::Notation_list;
4859 ######################################################################
4860  
4861 *isa= *UNIVERSAL::isa;
4862  
4863 sub new
4864 { my $class = shift;
4865 my $self={ notations => {}, updated => 0};
4866  
4867 bless $self, $class;
4868 return $self;
4869  
4870 }
4871  
4872 sub add_new_notation
4873 { my $notation_list= shift;
4874 my $notation= XML::Twig::Notation->new( @_);
4875 $notation_list->add( $notation);
4876 return $notation_list;
4877 }
4878  
4879 sub _add_list
4880 { my( $notation_list, $to_add)= @_;
4881 my $notations_to_add= $to_add->{notations};
4882 return $notation_list unless( $notations_to_add && %$notations_to_add);
4883 @{$notation_list->{notations}}{keys %$notations_to_add}= values %$notations_to_add;
4884 $notation_list->{updated}=1;
4885 return $notation_list;
4886 }
4887  
4888 sub add
4889 { my( $notation_list, $notation)= @_;
4890 $notation_list->{notations}->{$notation->{name}}= $notation;
4891 $notation_list->{updated}=1;
4892 return $notation_list;
4893 }
4894  
4895 sub notation
4896 { my( $notation_list, $notation_name)= @_;
4897 return $notation_list->{notations}->{$notation_name};
4898 }
4899  
4900 # can be called with a notation or with a notation name
4901 sub delete
4902 { my $notation_list= shift;
4903 if( isa( ref $_[0], 'XML::Twig::Notation'))
4904 { # the second arg is a notation
4905 my $notation= shift;
4906 delete $notation_list->{notations}->{$notation->{name}};
4907 }
4908 else
4909 { # the second arg was not a notation, must be a string then
4910 my $name= shift;
4911 delete $notation_list->{notations}->{$name};
4912 }
4913 $notation_list->{updated}=1;
4914 return $notation_list;
4915 }
4916  
4917 sub print
4918 { my ($notation_list, $fh)= @_;
4919 my $old_select= defined $fh ? select $fh : undef;
4920 print {$fh} $notation_list->sprint;
4921 select $old_select if( defined $old_select);
4922 return $notation_list;
4923 }
4924  
4925 sub sprint
4926 { my ($notation_list)= @_;
4927  
4928 my $notations='';
4929 foreach my $notation_name ( sort keys %{$notation_list->{notations}})
4930 { my $notation= $notation_list->{notations}->{$notation_name};
4931 # we have to test what the notation is or un-defined notations can creep in
4932 if( isa( $notation, 'XML::Twig::Notation')) { $notations .= $notation->text() . "\n"; }
4933 }
4934 return $notations;
4935 }
4936  
4937 sub text
4938 { my ($notation_list)= @_;
4939 return join "\n", map { $notation_list->{notations}->{$_}->text} sort keys %{$notation_list->{notations}};
4940 }
4941  
4942 # return the list of notation names
4943 sub notation_names
4944 { my $notation_list= shift;
4945 return (sort keys %{$notation_list->{notations}}) ;
4946 }
4947  
4948 sub list
4949 { my ($notation_list)= @_;
4950 return map { $notation_list->{notations}->{$_} } sort keys %{$notation_list->{notations}};
4951 }
4952  
4953 1;
4954  
4955 ######################################################################
4956 package XML::Twig::Notation;
4957 ######################################################################
4958  
4959 BEGIN
4960 { *sprint= *text;
4961 }
4962  
4963 sub new
4964 { my( $class, $name, $base, $sysid, $pubid)= @_;
4965 $class= ref( $class) || $class;
4966  
4967 my $self={};
4968  
4969 $self->{name} = $name;
4970 $self->{base} = $base if( defined $base );
4971 $self->{sysid} = $sysid if( defined $sysid);
4972 $self->{pubid} = $pubid if( defined $pubid);
4973  
4974 bless $self, $class;
4975 return $self;
4976 }
4977  
4978 sub name { return $_[0]->{name}; }
4979 sub base { return $_[0]->{base}; }
4980 sub sysid { return $_[0]->{sysid}; }
4981 sub pubid { return $_[0]->{pubid}; }
4982  
4983 sub print
4984 { my ($notation, $fh)= @_;
4985 my $text= $notation->text;
4986 if( $fh) { print $fh $text . "\n"; }
4987 else { print $text . "\n"; }
4988 }
4989  
4990 sub text
4991 { my ($notation)= @_;
4992 return '' if( !$notation->{name});
4993 my @tokens;
4994 push @tokens, '<!NOTATION';
4995 push @tokens, $notation->{name};
4996 push @tokens, ( 'PUBLIC', _quoted_val( $notation->{pubid} ) ) if $notation->{pubid};
4997 push @tokens, ( 'SYSTEM') if ! $notation->{pubid} && $notation->{sysid};
4998 push @tokens, (_quoted_val( $notation->{sysid}) ) if $notation->{sysid};
4999  
5000 return join( ' ', @tokens) . '>';
5001 }
5002  
5003 sub _quoted_val
5004 { my $q= $_[0]=~ m{"} ? q{'} : q{"};
5005 return qq{$q$_[0]$q};
5006 }
5007  
5008 sub _dump
5009 { my( $notation)= @_; return join( " - ", map { "$_ => '$notation->{$_}'" } grep { defined $notation->{$_} } sort keys %$notation); }
5010  
5011 1;
5012  
5013 ######################################################################
5014 package XML::Twig::Elt;
5015 ######################################################################
5016  
5017 use Carp;
5018 use Scalar::Util qw(weaken looks_like_number);
5019 use List::Util qw(first);
5020  
5021 *isa= *UNIVERSAL::isa;
5022  
5023 my $CDATA_START = "<![CDATA[";
5024 my $CDATA_END = "]]>";
5025 my $PI_START = "<?";
5026 my $PI_END = "?>";
5027 my $COMMENT_START = "<!--";
5028 my $COMMENT_END = "-->";
5029  
5030 my $XMLNS_URI = 'http://www.w3.org/2000/xmlns/';
5031  
5032 my %POSITION_ALIAS = (
5033 beforebegin => 'before',
5034 afterbegin => 'first_child',
5035 beforeend => 'last_child',
5036 afterend => 'after',
5037 );
5038  
5039 my %OP_POSITION = reverse %POSITION_ALIAS;
5040  
5041 BEGIN
5042 { # set some aliases for methods
5043 *tag = *gi;
5044 *name = *gi;
5045 *set_tag = *set_gi;
5046 *set_name = *set_gi;
5047 *find_nodes = *get_xpath; # as in XML::DOM
5048 *findnodes = *get_xpath; # as in XML::LibXML
5049 *field = *first_child_text;
5050 *trimmed_field = *first_child_trimmed_text;
5051 *is_field = *contains_only_text;
5052 *is = *passes;
5053 *matches = *passes;
5054 *has_child = *first_child;
5055 *has_children = *first_child;
5056 *all_children_pass = *all_children_are;
5057 *all_children_match= *all_children_are;
5058 *getElementsByTagName= *descendants;
5059 *find_by_tag_name= *descendants_or_self;
5060 *unwrap = *erase;
5061 *inner_xml = *xml_string;
5062 *outer_xml = *sprint;
5063 *add_class = *add_to_class;
5064  
5065 *first_child_is = *first_child_matches;
5066 *last_child_is = *last_child_matches;
5067 *next_sibling_is = *next_sibling_matches;
5068 *prev_sibling_is = *prev_sibling_matches;
5069 *next_elt_is = *next_elt_matches;
5070 *prev_elt_is = *prev_elt_matches;
5071 *parent_is = *parent_matches;
5072 *child_is = *child_matches;
5073 *inherited_att = *inherit_att;
5074  
5075 *sort_children_by_value= *sort_children_on_value;
5076  
5077 *has_atts= *att_nb;
5078  
5079 # imports from XML::Twig
5080 *_is_fh= *XML::Twig::_is_fh;
5081  
5082 # XML::XPath compatibility
5083 *string_value = *text;
5084 *toString = *sprint;
5085 *getName = *gi;
5086 *getRootNode = *twig;
5087 *getNextSibling = *_next_sibling;
5088 *getPreviousSibling = *_prev_sibling;
5089 *isElementNode = *is_elt;
5090 *isTextNode = *is_text;
5091 *isPI = *is_pi;
5092 *isPINode = *is_pi;
5093 *isProcessingInstructionNode= *is_pi;
5094 *isComment = *is_comment;
5095 *isCommentNode = *is_comment;
5096 *getTarget = *target;
5097 *getFirstChild = *_first_child;
5098 *getLastChild = *_last_child;
5099  
5100 }
5101  
5102 # can be called as XML::Twig::Elt->new( [[$gi, $atts, [@content]])
5103 # - gi is an optional gi given to the element
5104 # - $atts is a hashref to attributes for the element
5105 # - @content is an optional list of text and elements that will
5106 # be inserted under the element
5107 sub new
5108 { my $class= shift;
5109 $class= ref $class || $class;
5110 my $elt = {};
5111 bless ($elt, $class);
5112  
5113 return $elt unless @_;
5114  
5115 if( @_ == 1 && $_[0]=~ m{^\s*<}) { return $class->parse( @_); }
5116  
5117 # if a gi is passed then use it
5118 my $gi= shift;
5119 $elt->{gi}=$XML::Twig::gi2index{$gi} or $elt->set_gi( $gi);
5120  
5121 my $atts= ref $_[0] eq 'HASH' ? shift : undef;
5122  
5123 if( $atts && defined $atts->{$CDATA})
5124 { delete $atts->{$CDATA};
5125  
5126 my $cdata= $class->new( $CDATA => @_);
5127 return $class->new( $gi, $atts, $cdata);
5128 }
5129  
5130 if( $gi eq $PCDATA)
5131 { if( grep { ref $_ } @_) { croak "element $PCDATA can only be created from text"; }
5132 $elt->{pcdata}= join '', @_;
5133 }
5134 elsif( $gi eq $ENT)
5135 { $elt->{ent}= shift; }
5136 elsif( $gi eq $CDATA)
5137 { if( grep { ref $_ } @_) { croak "element $CDATA can only be created from text"; }
5138 $elt->{cdata}= join '', @_;
5139 }
5140 elsif( $gi eq $COMMENT)
5141 { if( grep { ref $_ } @_) { croak "element $COMMENT can only be created from text"; }
5142 $elt->{comment}= join '', @_;
5143 }
5144 elsif( $gi eq $PI)
5145 { if( grep { ref $_ } @_) { croak "element $PI can only be created from text"; }
5146 $elt->_set_pi( shift, join '', @_);
5147 }
5148 else
5149 { # the rest of the arguments are the content of the element
5150 if( @_)
5151 { $elt->set_content( @_); }
5152 else
5153 { $elt->{empty}= 1; }
5154 }
5155  
5156 if( $atts)
5157 { # the attribute hash can be used to pass the asis status
5158 if( defined $atts->{$ASIS}) { $elt->set_asis( $atts->{$ASIS} ); delete $atts->{$ASIS}; }
5159 if( defined $atts->{$EMPTY}) { $elt->{empty}= $atts->{$EMPTY}; delete $atts->{$EMPTY}; }
5160 if( keys %$atts) { $elt->set_atts( $atts); }
5161 $elt->_set_id( $atts->{$ID}) if( $atts->{$ID});
5162 }
5163  
5164 return $elt;
5165 }
5166  
5167 # optimized version of $elt->new( PCDATA, $text);
5168 sub _new_pcdata
5169 { my $class= $_[0];
5170 $class= ref $class || $class;
5171 my $elt = {};
5172 bless $elt, $class;
5173 $elt->{gi}=$XML::Twig::gi2index{$PCDATA} or $elt->set_gi( $PCDATA);
5174 $elt->{pcdata}= $_[1];
5175 return $elt;
5176 }
5177  
5178 # this function creates an XM:::Twig::Elt from a string
5179 # it is quite clumsy at the moment, as it just creates a
5180 # new twig then returns its root
5181 # there might also be memory leaks there
5182 # additional arguments are passed to new XML::Twig
5183 sub parse
5184 { my $class= shift;
5185 if( ref( $class)) { $class= ref( $class); }
5186 my $string= shift;
5187 my %args= @_;
5188 my $t= XML::Twig->new(%args);
5189 $t->parse( $string);
5190 my $elt= $t->root;
5191 # clean-up the node
5192 delete $elt->{twig}; # get rid of the twig data
5193 delete $elt->{twig_current}; # better get rid of this too
5194 if( $t->{twig_id_list}) { $elt->{twig_id_list}= $t->{twig_id_list}; }
5195 $elt->cut;
5196 undef $t->{twig_root};
5197 return $elt;
5198 }
5199  
5200 sub set_inner_xml
5201 { my( $elt, $xml, @args)= @_;
5202 my $new_elt= $elt->parse( "<dummy>$xml</dummy>", @args);
5203 $elt->cut_children;
5204 $new_elt->paste_first_child( $elt);
5205 $new_elt->erase;
5206 return $elt;
5207 }
5208  
5209 sub set_outer_xml
5210 { my( $elt, $xml, @args)= @_;
5211 my $new_elt= $elt->parse( "<dummy>$xml</dummy>", @args);
5212 $elt->cut_children;
5213 $new_elt->replace( $elt);
5214 $new_elt->erase;
5215 return $new_elt;
5216 }
5217  
5218 sub set_inner_html
5219 { my( $elt, $html)= @_;
5220 my $t= XML::Twig->new->parse_html( "<html>$html</html>");
5221 my $new_elt= $t->root;
5222 if( $elt->tag eq 'head')
5223 { $new_elt->first_child( 'head')->unwrap;
5224 $new_elt->first_child( 'body')->cut;
5225 }
5226 elsif( $elt->tag ne 'html')
5227 { $new_elt->first_child( 'head')->cut;
5228 $new_elt->first_child( 'body')->unwrap;
5229 }
5230 $new_elt->cut;
5231 $elt->cut_children;
5232 $new_elt->paste_first_child( $elt);
5233 $new_elt->erase;
5234 return $elt;
5235 }
5236  
5237 sub set_gi
5238 { my ($elt, $gi)= @_;
5239 $elt->{gi} = $XML::Twig::gi2index{$gi} || XML::Twig::_add_gi($gi);
5240 return $elt;
5241 }
5242  
5243 sub gi { return $XML::Twig::index2gi[$_[0]->{gi}]; }
5244  
5245 sub local_name
5246 { my $elt= shift;
5247 return _local_name( $XML::Twig::index2gi[$elt->{'gi'}]);
5248 }
5249  
5250 sub ns_prefix
5251 { my $elt= shift;
5252 return _ns_prefix( $XML::Twig::index2gi[$elt->{'gi'}]);
5253 }
5254  
5255 # namespace prefix for any qname (can be used for elements or attributes)
5256 sub _ns_prefix
5257 { my $qname= shift;
5258 if( $qname=~ m{^([^:]*):})
5259 { return $1; }
5260 else
5261 { return( ''); } # should it be '' ?
5262 }
5263  
5264 # local name for any qname (can be used for elements or attributes)
5265 sub _local_name
5266 { my $qname= shift;
5267 (my $local= $qname)=~ s{^[^:]*:}{};
5268 return $local;
5269 }
5270  
5271 #sub get_namespace
5272 sub namespace ## no critic (Subroutines::ProhibitNestedSubs);
5273 { my $elt= shift;
5274 my $prefix= defined $_[0] ? shift() : $elt->ns_prefix;
5275 my $ns_att= $prefix ? "xmlns:$prefix" : "xmlns";
5276 my $expanded= $DEFAULT_NS{$prefix} || $elt->_inherit_att_through_cut( $ns_att) || '';
5277 return $expanded;
5278 }
5279  
5280 sub declare_missing_ns ## no critic (Subroutines::ProhibitNestedSubs);
5281 { my $root= shift;
5282 my %missing_prefix;
5283 my $map= $root->_current_ns_prefix_map;
5284  
5285 foreach my $prefix (keys %$map)
5286 { my $prefix_att= $prefix eq '#default' ? 'xmlns' : "xmlns:$prefix";
5287 if( ! $root->{'att'}->{$prefix_att})
5288 { $root->set_att( $prefix_att => $map->{$prefix}); }
5289 }
5290 return $root;
5291 }
5292  
5293 sub _current_ns_prefix_map
5294 { my( $elt)= shift;
5295 my $map;
5296 while( $elt)
5297 { foreach my $att ($elt->att_names)
5298 { my $prefix= $att eq 'xmlns' ? '#default'
5299 : $att=~ m{^xmlns:(.*)$} ? $1
5300 : next
5301 ;
5302 if( ! exists $map->{$prefix}) { $map->{$prefix}= $elt->{'att'}->{$att}; }
5303 }
5304 $elt= $elt->{parent} || ($elt->{former} && $elt->{former}->{parent});
5305 }
5306 return $map;
5307 }
5308  
5309 sub set_ns_decl
5310 { my( $elt, $uri, $prefix)= @_;
5311 my $ns_att= $prefix ? "xmlns:$prefix" : 'xmlns';
5312 $elt->set_att( $ns_att => $uri);
5313 return $elt;
5314 }
5315  
5316 sub set_ns_as_default
5317 { my( $root, $uri)= @_;
5318 my @ns_decl_to_remove;
5319 foreach my $elt ($root->descendants_or_self)
5320 { if( $elt->_ns_prefix && $elt->namespace eq $uri)
5321 { $elt->set_tag( $elt->local_name); }
5322 # store any namespace declaration for that uri
5323 foreach my $ns_decl (grep { $_=~ m{xmlns(:|$)} && $elt->{'att'}->{$_} eq $uri } $elt->att_names)
5324 { push @ns_decl_to_remove, [$elt, $ns_decl]; }
5325 }
5326 $root->set_ns_decl( $uri);
5327 # now remove the ns declarations (if done earlier then descendants of an element with the ns declaration
5328 # are not considered being in the namespace
5329 foreach my $ns_decl_to_remove ( @ns_decl_to_remove)
5330 { my( $elt, $ns_decl)= @$ns_decl_to_remove;
5331 $elt->del_att( $ns_decl);
5332 }
5333  
5334 return $root;
5335 }
5336  
5337 # return #ELT for an element and #PCDATA... for others
5338 sub get_type
5339 { my $gi_nb= $_[0]->{gi}; # the number, not the string
5340 return $ELT if( $gi_nb >= $XML::Twig::SPECIAL_GI);
5341 return $_[0]->gi;
5342 }
5343  
5344 # return the gi if it's a "real" element, 0 otherwise
5345 sub is_elt
5346 { if( $_[0]->{gi} >= $XML::Twig::SPECIAL_GI)
5347 { return $_[0]->gi; }
5348 else
5349 { return 0; }
5350 }
5351  
5352 sub is_pcdata
5353 { my $elt= shift;
5354 return (exists $elt->{'pcdata'});
5355 }
5356  
5357 sub is_cdata
5358 { my $elt= shift;
5359 return (exists $elt->{'cdata'});
5360 }
5361  
5362 sub is_pi
5363 { my $elt= shift;
5364 return (exists $elt->{'target'});
5365 }
5366  
5367 sub is_comment
5368 { my $elt= shift;
5369 return (exists $elt->{'comment'});
5370 }
5371  
5372 sub is_ent
5373 { my $elt= shift;
5374 return (exists $elt->{ent} || $elt->{ent_name});
5375 }
5376  
5377 sub is_text
5378 { my $elt= shift;
5379 return (exists( $elt->{'pcdata'}) || (exists $elt->{'cdata'}));
5380 }
5381  
5382 sub is_empty
5383 { return $_[0]->{empty} || 0; }
5384  
5385 sub set_empty
5386 { $_[0]->{empty}= defined( $_[1]) ? $_[1] : 1; return $_[0]; }
5387  
5388 sub set_not_empty
5389 { delete $_[0]->{empty} if( $_[0]->{'empty'}); return $_[0]; }
5390  
5391 sub set_asis
5392 { my $elt=shift;
5393  
5394 foreach my $descendant ($elt, $elt->_descendants )
5395 { $descendant->{asis}= 1;
5396 if( (exists $descendant->{'cdata'}))
5397 { $descendant->{gi}=$XML::Twig::gi2index{$PCDATA} or $descendant->set_gi( $PCDATA);
5398 $descendant->{pcdata}= $descendant->{cdata};
5399 }
5400  
5401 }
5402 return $elt;
5403 }
5404  
5405 sub set_not_asis
5406 { my $elt=shift;
5407 foreach my $descendant ($elt, $elt->descendants)
5408 { delete $descendant->{asis} if $descendant->{asis};}
5409 return $elt;
5410 }
5411  
5412 sub is_asis
5413 { return $_[0]->{asis}; }
5414  
5415 sub closed
5416 { my $elt= shift;
5417 my $t= $elt->twig || return;
5418 my $curr_elt= $t->{twig_current};
5419 return 1 unless( $curr_elt);
5420 return $curr_elt->in( $elt);
5421 }
5422  
5423 sub set_pcdata
5424 { my( $elt, $pcdata)= @_;
5425  
5426 if( $elt->{extra_data_in_pcdata})
5427 { _try_moving_extra_data( $elt, $pcdata);
5428 }
5429 $elt->{pcdata}= $pcdata;
5430 return $elt;
5431 }
5432  
5433 sub _extra_data_in_pcdata { return $_[0]->{extra_data_in_pcdata}; }
5434 sub _set_extra_data_in_pcdata { $_[0]->{extra_data_in_pcdata}= $_[1]; return $_[0]; }
5435 sub _del_extra_data_in_pcdata { delete $_[0]->{extra_data_in_pcdata}; return $_[0]; }
5436 sub _unshift_extra_data_in_pcdata
5437 { my $e= shift;
5438 $e->{extra_data_in_pcdata}||=[];
5439 unshift @{$e->{extra_data_in_pcdata}}, { text => shift(), offset => shift() };
5440 }
5441 sub _push_extra_data_in_pcdata
5442 { my $e= shift;
5443 $e->{extra_data_in_pcdata}||=[];
5444 push @{$e->{extra_data_in_pcdata}}, { text => shift(), offset => shift() };
5445 }
5446  
5447 sub _extra_data_before_end_tag { return $_[0]->{extra_data_before_end_tag} || ''; }
5448 sub _set_extra_data_before_end_tag { $_[0]->{extra_data_before_end_tag}= $_[1]; return $_[0]}
5449 sub _del_extra_data_before_end_tag { delete $_[0]->{extra_data_before_end_tag}; return $_[0]}
5450 sub _prefix_extra_data_before_end_tag
5451 { my( $elt, $data)= @_;
5452 if($elt->{extra_data_before_end_tag})
5453 { $elt->{extra_data_before_end_tag}= $data . $elt->{extra_data_before_end_tag}; }
5454 else
5455 { $elt->{extra_data_before_end_tag}= $data; }
5456 return $elt;
5457 }
5458  
5459 # internal, in cases where we know there is no extra_data (inlined anyway!)
5460 sub _set_pcdata { $_[0]->{pcdata}= $_[1]; }
5461  
5462 # try to figure out if we can keep the extra_data around
5463 sub _try_moving_extra_data
5464 { my( $elt, $modified)=@_;
5465 my $initial= $elt->{pcdata};
5466 my $cpis= $elt->{extra_data_in_pcdata};
5467  
5468 if( (my $offset= index( $modified, $initial)) != -1)
5469 { # text has been added
5470 foreach (@$cpis) { $_->{offset}+= $offset; }
5471 }
5472 elsif( ($offset= index( $initial, $modified)) != -1)
5473 { # text has been cut
5474 my $len= length( $modified);
5475 foreach my $cpi (@$cpis) { $cpi->{offset} -= $offset; }
5476 $elt->_set_extra_data_in_pcdata( [ grep { $_->{offset} >= 0 && $_->{offset} < $len } @$cpis ]);
5477 }
5478 else
5479 { _match_extra_data_words( $elt, $initial, $modified)
5480 || _match_extra_data_chars( $elt, $initial, $modified)
5481 || $elt->_del_extra_data_in_pcdata;
5482 }
5483 }
5484  
5485 sub _match_extra_data_words
5486 { my( $elt, $initial, $modified)= @_;
5487 my @initial= split /\b/, $initial;
5488 my @modified= split /\b/, $modified;
5489  
5490 return _match_extra_data( $elt, length( $initial), \@initial, \@modified);
5491 }
5492  
5493 sub _match_extra_data_chars
5494 { my( $elt, $initial, $modified)= @_;
5495 my @initial= split //, $initial;
5496 my @modified= split //, $modified;
5497  
5498 return _match_extra_data( $elt, length( $initial), \@initial, \@modified);
5499 }
5500  
5501 sub _match_extra_data
5502 { my( $elt, $length, $initial, $modified)= @_;
5503  
5504 my $cpis= $elt->{extra_data_in_pcdata};
5505  
5506 if( @$initial <= @$modified)
5507 {
5508 my( $ok, $positions, $offsets)= _pos_offset( $initial, $modified);
5509 if( $ok)
5510 { my $offset=0;
5511 my $pos= shift @$positions;
5512 foreach my $cpi (@$cpis)
5513 { while( $cpi->{offset} >= $pos)
5514 { $offset= shift @$offsets;
5515 $pos= shift @$positions || $length +1;
5516 }
5517 $cpi->{offset} += $offset;
5518 }
5519 return 1;
5520 }
5521 }
5522 else
5523 { my( $ok, $positions, $offsets)= _pos_offset( $modified, $initial);
5524 if( $ok)
5525 { #print STDERR "pos: ", join( ':', @$positions), "\n",
5526 # "offset: ", join( ':', @$offsets), "\n";
5527 my $offset=0;
5528 my $pos= shift @$positions;
5529 my $prev_pos= 0;
5530  
5531 foreach my $cpi (@$cpis)
5532 { while( $cpi->{offset} >= $pos)
5533 { $offset= shift @$offsets;
5534 $prev_pos= $pos;
5535 $pos= shift @$positions || $length +1;
5536 }
5537 $cpi->{offset} -= $offset;
5538 if( $cpi->{offset} < $prev_pos) { delete $cpi->{text}; }
5539 }
5540 $elt->_set_extra_data_in_pcdata( [ grep { exists $_->{text} } @$cpis ]);
5541 return 1;
5542 }
5543 }
5544 return 0;
5545 }
5546  
5547 sub _pos_offset
5548 { my( $short, $long)= @_;
5549 my( @pos, @offset);
5550 my( $s_length, $l_length)=(0,0);
5551 while (@$short)
5552 { my $s_word= shift @$short;
5553 my $l_word= shift @$long;
5554 if( $s_word ne $l_word)
5555 { while( @$long && $s_word ne $l_word)
5556 { $l_length += length( $l_word);
5557 $l_word= shift @$long;
5558 }
5559 if( !@$long && $s_word ne $l_word) { return 0; }
5560 push @pos, $s_length;
5561 push @offset, $l_length - $s_length;
5562 }
5563 my $length= length( $s_word);
5564 $s_length += $length;
5565 $l_length += $length;
5566 }
5567 return( 1, \@pos, \@offset);
5568 }
5569  
5570 sub append_pcdata
5571 { $_[0]->{'pcdata'}.= $_[1];
5572 return $_[0];
5573 }
5574  
5575 sub pcdata { return $_[0]->{pcdata}; }
5576  
5577 sub append_extra_data
5578 { $_[0]->{extra_data}.= $_[1];
5579 return $_[0];
5580 }
5581  
5582 sub set_extra_data
5583 { $_[0]->{extra_data}= $_[1];
5584 return $_[0];
5585 }
5586 sub extra_data { return $_[0]->{extra_data} || ''; }
5587  
5588 sub set_target
5589 { my( $elt, $target)= @_;
5590 $elt->{target}= $target;
5591 return $elt;
5592 }
5593 sub target { return $_[0]->{target}; }
5594  
5595 sub set_data
5596 { $_[0]->{'data'}= $_[1];
5597 return $_[0];
5598 }
5599 sub data { return $_[0]->{data}; }
5600  
5601 sub set_pi
5602 { my $elt= shift;
5603 unless( $elt->{gi} == $XML::Twig::gi2index{$PI})
5604 { $elt->cut_children;
5605 $elt->{gi}=$XML::Twig::gi2index{$PI} or $elt->set_gi( $PI);
5606 }
5607 return $elt->_set_pi( @_);
5608 }
5609  
5610 sub _set_pi
5611 { $_[0]->set_target( $_[1]);
5612 $_[0]->{data}= $_[2];
5613 return $_[0];
5614 }
5615  
5616 sub pi_string { my $string= $PI_START . $_[0]->{target};
5617 my $data= $_[0]->{data};
5618 if( defined( $data) && $data ne '') { $string .= " $data"; }
5619 $string .= $PI_END ;
5620 return $string;
5621 }
5622  
5623 sub set_comment
5624 { my $elt= shift;
5625 unless( $elt->{gi} == $XML::Twig::gi2index{$COMMENT})
5626 { $elt->cut_children;
5627 $elt->{gi}=$XML::Twig::gi2index{$COMMENT} or $elt->set_gi( $COMMENT);
5628 }
5629 $elt->{comment}= $_[0];
5630 return $elt;
5631 }
5632  
5633 sub _set_comment { $_[0]->{comment}= $_[1]; return $_[0]; }
5634 sub comment { return $_[0]->{comment}; }
5635 sub comment_string { return $COMMENT_START . _comment_escaped_string( $_[0]->{comment}) . $COMMENT_END; }
5636 # comments cannot start or end with
5637 sub _comment_escaped_string
5638 { my( $c)= @_;
5639 $c=~ s{^-}{ -};
5640 $c=~ s{-$}{- };
5641 $c=~ s{--}{- -}g;
5642 return $c;
5643 }
5644  
5645 sub set_ent { $_[0]->{ent}= $_[1]; return $_[0]; }
5646 sub ent { return $_[0]->{ent}; }
5647 sub ent_name { return substr( $_[0]->{ent}, 1, -1);}
5648  
5649 sub set_cdata
5650 { my $elt= shift;
5651 unless( $elt->{gi} == $XML::Twig::gi2index{$CDATA})
5652 { $elt->cut_children;
5653 $elt->insert_new_elt( first_child => $CDATA, @_);
5654 return $elt;
5655 }
5656 $elt->{cdata}= $_[0];
5657 return $_[0];
5658 }
5659  
5660 sub _set_cdata
5661 { $_[0]->{cdata}= $_[1];
5662 return $_[0];
5663 }
5664  
5665 sub append_cdata
5666 { $_[0]->{cdata}.= $_[1];
5667 return $_[0];
5668 }
5669 sub cdata { return $_[0]->{cdata}; }
5670  
5671 sub contains_only_text
5672 { my $elt= shift;
5673 return 0 unless $elt->is_elt;
5674 foreach my $child ($elt->_children)
5675 { return 0 if $child->is_elt; }
5676 return $elt;
5677 }
5678  
5679 sub contains_only
5680 { my( $elt, $exp)= @_;
5681 my @children= do { my $elt= $elt; my @children=(); my $child= $elt->{first_child}; while( $child) { push @children, $child; $child= $child->{next_sibling}; } @children; };
5682 foreach my $child (@children)
5683 { return 0 unless $child->is( $exp); }
5684 return @children || 1;
5685 }
5686  
5687 sub contains_a_single
5688 { my( $elt, $exp)= @_;
5689 my $child= $elt->{first_child} or return 0;
5690 return 0 unless $child->passes( $exp);
5691 return 0 if( $child->{next_sibling});
5692 return $child;
5693 }
5694  
5695 sub root
5696 { my $elt= shift;
5697 while( $elt->{parent}) { $elt= $elt->{parent}; }
5698 return $elt;
5699 }
5700  
5701 sub _root_through_cut
5702 { my $elt= shift;
5703 while( $elt->{parent} || ($elt->{former} && $elt->{former}->{parent})) { $elt= $elt->{parent} || ($elt->{former} && $elt->{former}->{parent}); }
5704 return $elt;
5705 }
5706  
5707 sub twig
5708 { my $elt= shift;
5709 my $root= $elt->root;
5710 return $root->{twig};
5711 }
5712  
5713 sub _twig_through_cut
5714 { my $elt= shift;
5715 my $root= $elt->_root_through_cut;
5716 return $root->{twig};
5717 }
5718  
5719 # used for navigation
5720 # returns undef or the element, depending on whether $elt passes $cond
5721 # $cond can be
5722 # - empty: the element passes the condition
5723 # - ELT ('#ELT'): the element passes the condition if it is a "real" element
5724 # - TEXT ('#TEXT'): the element passes if it is a CDATA or PCDATA element
5725 # - a string with an XPath condition (only a subset of XPath is actually
5726 # supported).
5727 # - a regexp: the element passes if its gi matches the regexp
5728 # - a code ref: the element passes if the code, applied on the element,
5729 # returns true
5730  
5731 my %cond_cache; # expression => coderef
5732  
5733 sub reset_cond_cache { %cond_cache=(); }
5734  
5735 {
5736 sub _install_cond
5737 { my $cond= shift;
5738 my $test;
5739 my $init='';
5740  
5741 my $original_cond= $cond;
5742  
5743 my $not= ($cond=~ s{^\s*!}{}) ? '!' : '';
5744  
5745 if( ref $cond eq 'CODE') { return $cond; }
5746  
5747 if( ref $cond eq 'Regexp')
5748 { $test = qq{(\$_[0]->gi=~ /$cond/)}; }
5749 else
5750 { my @tests;
5751 while( $cond)
5752 {
5753 # the condition is a string
5754 if( $cond=~ s{$ELT$SEP}{})
5755 { push @tests, qq{\$_[0]->is_elt}; }
5756 elsif( $cond=~ s{$TEXT$SEP}{})
5757 { push @tests, qq{\$_[0]->is_text}; }
5758 elsif( $cond=~ s{^\s*($REG_TAG_PART)$SEP}{})
5759 { push @tests, _gi_test( $1); }
5760 elsif( $cond=~ s{^\s*($REG_REGEXP)$SEP}{})
5761 { # /regexp/
5762 push @tests, qq{ \$_[0]->gi=~ $1 };
5763 }
5764 elsif( $cond=~ s{^\s*($REG_TAG_PART)?\s* # $1
5765 \[\s*(-?)\s*(\d+)\s*\] # [$2]
5766 $SEP}{}xo
5767 )
5768 { my( $gi, $neg, $index)= ($1, $2, $3);
5769 my $siblings= $neg ? q{$_[0]->_next_siblings} : q{$_[0]->_prev_siblings};
5770 if( $gi && ($gi ne '*'))
5771 #{ $test= qq{((\$_[0]->gi eq "$gi") && (scalar( grep { \$_->gi eq "$gi" } $siblings) + 1 == $index))}; }
5772 { push @tests, _and( _gi_test( $gi), qq{ (scalar( grep { \$_->gi eq "$gi" } $siblings) + 1 == $index)}); }
5773 else
5774 { push @tests, qq{(scalar( $siblings) + 1 == $index)}; }
5775 }
5776 elsif( $cond=~ s{^\s*($REG_TAG_PART?)\s*($REG_PREDICATE)$SEP}{})
5777 { my( $gi, $predicate)= ( $1, $2);
5778 push @tests, _and( _gi_test( $gi), _parse_predicate_in_step( $predicate));
5779 }
5780 elsif( $cond=~ s{^\s*($REG_NAKED_PREDICATE)$SEP}{})
5781 { push @tests, _parse_predicate_in_step( $1); }
5782 else
5783 { croak "wrong navigation condition '$original_cond' ($@)"; }
5784 }
5785 $test= @tests > 1 ? '(' . join( '||', map { "($_)" } @tests) . ')' : $tests[0];
5786 }
5787  
5788 #warn "init: '$init' - test: '$test'\n";
5789  
5790 my $sub= qq{sub { $NO_WARNINGS; $init; return $not($test) ? \$_[0] : undef; } };
5791 my $s= eval $sub; ## no critic (ProhibitStringyEval)
5792 # warn "cond: $cond\n$sub\n";
5793 if( $@)
5794 { croak "wrong navigation condition '$original_cond' ($@);" }
5795 return $s;
5796 }
5797  
5798 sub _gi_test
5799 { my( $full_gi)= @_;
5800  
5801 # optimize if the gi exists, including the case where the gi includes a dot
5802 my $index= $XML::Twig::gi2index{$full_gi};
5803  
5804 if( $index)
5805 { return qq{\$_[0]->{gi} == $index}; }
5806 elsif( defined $css_sel && $css_sel == 0 ) {
5807 # css_sel explicitely set to 0 forces treating names with dots as just plain tag names
5808 my $new_index = XML::Twig::_add_gi( $full_gi );
5809 return qq{(\$_[0]->{gi} == $new_index)};
5810 }
5811 my( $gi, $class, $id)= $full_gi=~ m{^(.*?)(?:[.](.*)|[#](.*))?$};
5812  
5813 my $gi_test='';
5814 if( $gi && $gi ne '*' )
5815 { # 2 options, depending on whether the gi exists in gi2index
5816 # start optimization
5817 my $index= $XML::Twig::gi2index{$gi};
5818 if( $index)
5819 { # the gi exists, use its index as a faster shortcut
5820 $gi_test = qq{\$_[0]->{gi} == $index};
5821 }
5822 else
5823 # end optimization
5824 { # it does not exist (but might be created later), compare the strings
5825 $gi_test = qq{ (\$_[0]->{gi} == (\$XML::Twig::gi2index{'$gi'} || 0))};
5826 }
5827 }
5828 else
5829 { $gi_test= 1; }
5830  
5831 my $class_test='';
5832 if( $class)
5833 { my @classes = grep { $_ } split /\./, $class;
5834 $class_test = join ' && ', qq{(defined( \$_[0]->{att}->{class}))}, map { qq{(\$_[0]->{att}->{class}=~ m{\\b$_\\b})} } @classes;
5835 }
5836  
5837 my $id_test='';
5838 if( $id)
5839 { $id_test = qq{ defined( \$_[0]->{att}->{$ID}) && \$_[0]->{att}->{$ID} eq '$id' }; }
5840  
5841 my $test = _and( $gi_test, $class_test, $id_test);
5842 # account for the (unlikely) case where a tag.class condition is actually a tag.withdot condition
5843 # but no tag.withdot element has yet been seen
5844 if( $gi && $gi ne '*' && $class) { $test = qq{((\$_[0]->{full_gi} == (\$XML::Twig::gi2index{'$gi'} || 0)) || $test)}; }
5845 #warn " full_gi is [$full_gi], test is: [$test]";
5846  
5847 return $test;
5848  
5849 }
5850  
5851 # input: the original predicate
5852 sub _parse_predicate_in_step
5853 { my $cond= shift; my $initial_cond= $cond;
5854 my %PERL_ALPHA_TEST= ( '=' => ' eq ', '!=' => ' ne ', '>' => ' gt ', '>=' => ' ge ', '<' => ' lt ', '<=' => ' le ');
5855  
5856 $cond=~ s{^\s*\[\s*}{};
5857 $cond=~ s{\s*\]\s*$}{};
5858 $cond=~ s{(?<token>
5859 (?<tstr>$REG_STRING|$REG_REGEXP) # strings or regexps
5860 |\@(?<tatt>$REG_TAG_NAME)(?=\s*(?:[><=!]|!~|=~)) # @att (followed by a comparison operator)
5861 |\@(?<tbare_att>$REG_TAG_NAME) # @att (not followed by a comparison operator)
5862 |=~|!~ # matching operators
5863 |(?<tbare_num_test>[><]=?|=|!=)(?=\s*[\d+-]) # test before a number
5864 |(?<tbare_test>[><]=?|=|!=) # test, other cases
5865 # this bit is a mess, but it is the only solution with this half-baked parser
5866 |(?<tstr_re>(?:string|text)\(\s*(?<tag>$REG_TAG_NAME?)\s*\)\s*(?<op>$REG_MATCH)\s*(?<re>$REG_REGEXP)) # string(child) =~ /regexp/
5867 |(?<tstr_num>(?:string|text)\(\s*(?<tag>$REG_NAME?)\s*\)\s*(?<op>$REG_COMP)\s*(?<nb>$REG_NUMBER)) # string(child) = nb (or other test)
5868 |(?<tstr_str>(?:string|text)\(\s*(?<tag>$REG_TAG_NAME?)\s*\)\s*(?<op>!?=)\s*(?<string>$REG_STRING)) # string(child) = "value" (or !=)
5869 |(?<tstring_test>(?:string|text)\(\s*(?<tag>$REG_TAG_NAME)\s*\)\s*(?<op>[<>]=?)\s*(?<string>$REG_STRING)) # string(child) > "value"
5870 |(?<tfunc>$REG_FUNCTION) # no arg functions
5871 |(?<tand_or>and|or)
5872 )}
5873 {
5874 my %m = %+; # save the named captures so further regexp do not overwrite them
5875 if( defined $m{tstr} ) { $m{token} }
5876 elsif( $m{tatteq0} ) { "( \$_[0]->{att} && exists( \$_[0]->{att}->{'$m{tatteq0}'}) \$_[0]->{att}->{'$m{tatteq0}'})"; }
5877 elsif( $m{tatt} ) { "( \$_[0]->{att} && exists( \$_[0]->{att}->{'$m{tatt}'}) && \$_[0]->{att}->{'$m{tatt}'})"; }
5878 elsif( $m{tbare_att} ) { "(\$_[0]->{att} && defined( \$_[0]->{att}->{'$m{tbare_att}'}))"; }
5879 elsif( $m{tbare_num_test} && ($m{tbare_num_test} eq '=') ) { "==" } # others tests are unchanged
5880 elsif( $m{tbare_test} ) { $PERL_ALPHA_TEST{$m{tbare_test}} }
5881 elsif( $m{tfunc} && $m{tfunc}=~ m{^(?:string|text)})
5882 { "\$_[0]->text"; }
5883 elsif( $m{tstr_num}) { my $op = $m{op} eq '=' ? '==' : $m{op};
5884 if($m{tag})
5885 { "(first { (\$_->gi eq '$m{tag}') && (\$_->text $op $m{nb}) } \$_[0]->_children)"; }
5886 else
5887 { "(\$_[0]->text $op $m{nb})";
5888 }
5889 }
5890 elsif( $m{tstr_re} ) { if($m{tag})
5891 { "(first { (\$_->gi eq '$m{tag}') && (\$_->text $m{op} $m{re}) } \$_[0]->_children)"; }
5892 else
5893 { "(\$_[0]->text $m{op} $m{re} )";}
5894 }
5895 elsif( $m{tstr_str} ){ my $op = $PERL_ALPHA_TEST{$m{op}};
5896 if($m{tag})
5897 { "(first { (\$_->gi eq '$m{tag}') && (\$_->text $op $m{string}) } \$_[0]->_children)"; }
5898 else
5899 { "(\$_[0]->text $op $m{string} )"; }
5900 }
5901  
5902 elsif( $m{tstring_test} )
5903 { "(first { (\$_->gi eq '$m{tag}') && (\$_->text $m{op} $m{string}) } \$m{_}[0]->_children)"; }
5904 elsif( $m{tand_or}) { $m{tand_or }eq 'and' ? '&&' : '||' ; }
5905 else { $m{token}; }
5906 }gexs;
5907 #warn "[$initial_cond] => [$cond]\n";
5908 return "($cond)";
5909 }
5910  
5911 sub _op
5912 { my $op= shift;
5913 if( $op eq '=') { $op= 'eq'; }
5914 elsif( $op eq '!=') { $op= 'ne'; }
5915 return $op;
5916 }
5917  
5918 sub passes
5919 { my( $elt, $cond)= @_;
5920 return $elt unless $cond;
5921 my $sub= ($cond_cache{$cond} ||= _install_cond( $cond));
5922 return $sub->( $elt);
5923 }
5924 }
5925  
5926 sub set_parent
5927 { $_[0]->{parent}= $_[1];
5928 weaken( $_[0]->{parent});
5929 }
5930  
5931 sub parent
5932 { my $elt= shift;
5933 my $cond= shift || return $elt->{parent};
5934 do { $elt= $elt->{parent} || return; } until ( $elt->passes( $cond));
5935 return $elt;
5936 }
5937  
5938 sub set_first_child
5939 { $_[0]->{'first_child'}= $_[1];
5940 }
5941  
5942 sub first_child
5943 { my $elt= shift;
5944 my $cond= shift || return $elt->{first_child};
5945 my $child= $elt->{first_child};
5946 my $test_cond= ($cond_cache{$cond} ||= _install_cond( $cond));
5947 while( $child && !$test_cond->( $child))
5948 { $child= $child->{next_sibling}; }
5949 return $child;
5950 }
5951  
5952 sub _first_child { return $_[0]->{first_child}; }
5953 sub _last_child { return $_[0]->{last_child}; }
5954 sub _next_sibling { return $_[0]->{next_sibling}; }
5955 sub _prev_sibling { return $_[0]->{prev_sibling}; }
5956 sub _parent { return $_[0]->{parent}; }
5957 sub _next_siblings { my $elt= shift; my @siblings; while( $elt= $elt->{next_sibling}) { push @siblings, $elt; } return @siblings; }
5958 sub _prev_siblings { my $elt= shift; my @siblings; while( $elt= $elt->{prev_sibling}) { push @siblings, $elt; } return @siblings; }
5959  
5960 # converts HTML style positions like 'beforebegin' to twig style positions like 'first_child'
5961 sub _op_position {
5962 my ($position) = @_;
5963 return $POSITION_ALIAS{$position} // $position;
5964 }
5965  
5966 # builds a message with allowed position
5967 sub _allowed_positions {
5968 return join ', ', map { "$_ (or $OP_POSITION{$_})" } sort keys %OP_POSITION;
5969 }
5970  
5971 # sets a field
5972 # arguments $record, $cond, @content
5973 sub set_field
5974 { my $record = shift;
5975 my $cond = shift;
5976 my $child= $record->first_child( $cond);
5977 if( $child)
5978 { $child->set_content( @_); }
5979 else
5980 { if( $cond=~ m{^\s*($REG_TAG_NAME)})
5981 { my $gi= $1;
5982 $child= $record->insert_new_elt( last_child => $gi, @_);
5983 }
5984 else
5985 { croak "can't create a field name from $cond"; }
5986 }
5987 return $child;
5988 }
5989  
5990 sub set_last_child
5991 { $_[0]->{'last_child'}= $_[1];
5992 delete $_[0]->{empty};
5993 weaken( $_[0]->{'last_child'});
5994 }
5995  
5996 sub last_child
5997 { my $elt= shift;
5998 my $cond= shift || return $elt->{last_child};
5999 my $test_cond= ($cond_cache{$cond} ||= _install_cond( $cond));
6000 my $child= $elt->{last_child};
6001 while( $child && !$test_cond->( $child) )
6002 { $child= $child->{prev_sibling}; }
6003 return $child
6004 }
6005  
6006 sub set_prev_sibling
6007 { $_[0]->{'prev_sibling'}= $_[1];
6008 weaken( $_[0]->{'prev_sibling'});
6009 }
6010  
6011 sub prev_sibling
6012 { my $elt= shift;
6013 my $cond= shift || return $elt->{prev_sibling};
6014 my $test_cond= ($cond_cache{$cond} ||= _install_cond( $cond));
6015 my $sibling= $elt->{prev_sibling};
6016 while( $sibling && !$test_cond->( $sibling) )
6017 { $sibling= $sibling->{prev_sibling}; }
6018 return $sibling;
6019 }
6020  
6021 sub set_next_sibling { $_[0]->{'next_sibling'}= $_[1]; }
6022  
6023 sub next_sibling
6024 { my $elt= shift;
6025 my $cond= shift || return $elt->{next_sibling};
6026 my $test_cond= ($cond_cache{$cond} ||= _install_cond( $cond));
6027 my $sibling= $elt->{next_sibling};
6028 while( $sibling && !$test_cond->( $sibling) )
6029 { $sibling= $sibling->{next_sibling}; }
6030 return $sibling;
6031 }
6032  
6033 # methods dealing with the class attribute, convenient if you work with xhtml
6034 sub class { $_[0]->{att}->{class}; }
6035 # lvalue version of class. separate from class to avoid problem with scalar/list context
6036 sub lclass :lvalue
6037 { $_[0]->{att}->{class}; }
6038  
6039 sub set_class { my( $elt, $class)= @_; $elt->set_att( class => $class); }
6040  
6041 # adds a class to an element
6042 sub add_to_class
6043 { my( $elt, $new_class)= @_;
6044 return $elt unless $new_class;
6045 my $class= $elt->class;
6046 my %class= $class ? map { $_ => 1 } split /\s+/, $class : ();
6047 $class{$new_class}= 1;
6048 $elt->set_class( join( ' ', sort keys %class));
6049 }
6050  
6051 sub remove_class
6052 { my( $elt, $class_to_remove)= @_;
6053 return $elt unless $class_to_remove;
6054 my $class= $elt->class;
6055 my %class= $class ? map { $_ => 1 } split /\s+/, $class : ();
6056 delete $class{$class_to_remove};
6057 $elt->set_class( join( ' ', sort keys %class));
6058 }
6059  
6060 sub att_to_class { my( $elt, $att)= @_; $elt->set_class( $elt->{'att'}->{$att}); }
6061 sub add_att_to_class { my( $elt, $att)= @_; $elt->add_to_class( $elt->{'att'}->{$att}); }
6062 sub move_att_to_class { my( $elt, $att)= @_; $elt->add_to_class( $elt->{'att'}->{$att});
6063 $elt->del_att( $att);
6064 }
6065 sub tag_to_class { my( $elt)= @_; $elt->set_class( $elt->tag); }
6066 sub add_tag_to_class { my( $elt)= @_; $elt->add_to_class( $elt->tag); }
6067 sub set_tag_class { my( $elt, $new_tag)= @_; $elt->add_tag_to_class; $elt->set_tag( $new_tag); }
6068  
6069 sub tag_to_span
6070 { my( $elt)= @_;
6071 $elt->set_class( $elt->tag) unless( $elt->tag eq 'span' && $elt->class); # set class to span unless it would mean replacing it with span
6072 $elt->set_tag( 'span');
6073 }
6074  
6075 sub tag_to_div
6076 { my( $elt)= @_;
6077 $elt->set_class( $elt->tag) unless( $elt->tag eq 'div' && $elt->class); # set class to div unless it would mean replacing it with div
6078 $elt->set_tag( 'div');
6079 }
6080  
6081 sub in_class
6082 { my( $elt, $class)= @_;
6083 my $elt_class= $elt->class;
6084 return unless( defined $elt_class);
6085 return $elt->class=~ m{(?:^|\s)\Q$class\E(?:\s|$)} ? $elt : 0;
6086 }
6087  
6088 # get or set all attributes
6089 # argument can be a hash or a hashref
6090 sub set_atts
6091 { my $elt= shift;
6092 my %atts;
6093 tie %atts, 'Tie::IxHash' if( keep_atts_order());
6094 %atts= @_ == 1 ? %{$_[0]} : @_;
6095 $elt->{att}= \%atts;
6096 if( exists $atts{$ID}) { $elt->_set_id( $atts{$ID}); }
6097 return $elt;
6098 }
6099  
6100 sub atts { return $_[0]->{att}; }
6101 sub att_names { return (sort keys %{$_[0]->{att}}); }
6102 sub del_atts { delete $_[0]->{att}; return $_[0]; }
6103  
6104 # get or set a single attribute (set works for several atts)
6105 sub set_att
6106 { my $elt= shift;
6107 if( $_[0] && ref( $_[0]) && !$_[1])
6108 { croak "improper call to set_att, usage is \$elt->set_att( att1 => 'val1', att2 => 'val2',...)"; }
6109  
6110 $elt->{att} ||= {};
6111 # need to test on keys because the att hash is created, empty, by some methods,
6112 # like sprint (through autovivification)
6113 tie %{$elt->{att}}, 'Tie::IxHash' if keep_atts_order() && ! keys %{$elt->{att}};
6114  
6115 while(@_)
6116 { my( $att, $val)= (shift, shift);
6117 $elt->{att}->{$att}= $val;
6118 if( $att eq $ID) { $elt->_set_id( $val); }
6119 }
6120 return $elt;
6121 }
6122  
6123 sub att { $_[0]->{att}->{$_[1]}; }
6124 # lvalue version of att. separate from class to avoid problems with scalar/list context
6125 sub latt :lvalue
6126 { $_[0]->{att}->{$_[1]}; }
6127  
6128 sub del_att
6129 { my $elt= shift;
6130 while( @_) { delete $elt->{'att'}->{shift()}; }
6131 return $elt;
6132 }
6133  
6134 sub att_exists { return exists $_[0]->{att}->{$_[1]}; }
6135  
6136 # delete one or more attributes from all descendants of an element
6137 sub strip_att
6138 { my( $elt, @att)= @_;
6139 $_->del_att( @att) foreach ($elt->descendants_or_self);
6140 return $elt;
6141 }
6142  
6143 sub change_att_name
6144 { my( $elt, $old_name, $new_name)= @_;
6145 my $value= $elt->{'att'}->{$old_name};
6146 return $elt unless( defined $value);
6147 $elt->del_att( $old_name)
6148 ->set_att( $new_name => $value);
6149 return $elt;
6150 }
6151  
6152 sub lc_attnames
6153 { my $elt= shift;
6154 foreach my $att ($elt->att_names)
6155 { if( $att ne lc $att) { $elt->change_att_name( $att, lc $att); } }
6156 return $elt;
6157 }
6158  
6159 sub set_twig_current { $_[0]->{twig_current}=1; }
6160 sub del_twig_current { delete $_[0]->{twig_current}; }
6161  
6162 # get or set the id attribute
6163 sub set_id
6164 { my( $elt, $id)= @_;
6165 $elt->del_id() if( exists $elt->{att}->{$ID});
6166 $elt->set_att($ID, $id);
6167 $elt->_set_id( $id);
6168 return $elt;
6169 }
6170  
6171 # only set id, does not update the attribute value
6172 sub _set_id
6173 { my( $elt, $id)= @_;
6174 my $t= $elt->twig || $elt;
6175 $t->{twig_id_list}->{$id}= $elt;
6176 weaken( $t->{twig_id_list}->{$id});
6177 return $elt;
6178 }
6179  
6180 sub id { return $_[0]->{att}->{$ID}; }
6181  
6182 # methods used to add ids to elements that don't have one
6183 BEGIN
6184 { my $id_nb = "0001";
6185 my $id_seed = "twig_id_";
6186  
6187 sub set_id_seed ## no critic (Subroutines::ProhibitNestedSubs);
6188 { $id_seed= $_[1]; $id_nb=1; }
6189  
6190 sub add_id ## no critic (Subroutines::ProhibitNestedSubs);
6191 { my $elt= shift;
6192 if( defined $elt->{'att'}->{$ID})
6193 { return $elt->{'att'}->{$ID}; }
6194 else
6195 { my $id= $_[0] && ref( $_[0]) && isa( $_[0], 'CODE') ? $_[0]->( $elt) : $id_seed . $id_nb++;
6196 $elt->set_id( $id);
6197 return $id;
6198 }
6199 }
6200 }
6201  
6202 # delete the id attribute and remove the element from the id list
6203 sub del_id
6204 { my $elt= shift;
6205 if( ! exists $elt->{att}->{$ID}) { return $elt };
6206 my $id= $elt->{att}->{$ID};
6207  
6208 delete $elt->{att}->{$ID};
6209  
6210 my $t= shift || $elt->twig;
6211 unless( $t) { return $elt; }
6212 if( exists $t->{twig_id_list}->{$id}) { delete $t->{twig_id_list}->{$id}; }
6213  
6214 return $elt;
6215 }
6216  
6217 # return the list of children
6218 sub children
6219 { my $elt= shift;
6220 my @children;
6221 my $child= $elt->first_child( @_);
6222 while( $child)
6223 { push @children, $child;
6224 $child= $child->next_sibling( @_);
6225 }
6226 return @children;
6227 }
6228  
6229 sub _children
6230 { my $elt= shift;
6231 my @children=();
6232 my $child= $elt->{first_child};
6233 while( $child)
6234 { push @children, $child;
6235 $child= $child->{next_sibling};
6236 }
6237 return @children;
6238 }
6239  
6240 sub children_copy
6241 { my $elt= shift;
6242 my @children;
6243 my $child= $elt->first_child( @_);
6244 while( $child)
6245 { push @children, $child->copy;
6246 $child= $child->next_sibling( @_);
6247 }
6248 return @children;
6249 }
6250  
6251 sub children_count
6252 { my $elt= shift;
6253 my $cond= shift;
6254 my $count=0;
6255 my $child= $elt->{first_child};
6256 while( $child)
6257 { $count++ if( $child->passes( $cond));
6258 $child= $child->{next_sibling};
6259 }
6260 return $count;
6261 }
6262  
6263 sub children_text
6264 { my $elt= shift;
6265 return wantarray() ? map { $_->text} $elt->children( @_)
6266 : join( '', map { $_->text} $elt->children( @_) )
6267 ;
6268 }
6269  
6270 sub children_trimmed_text
6271 { my $elt= shift;
6272 return wantarray() ? map { $_->trimmed_text} $elt->children( @_)
6273 : join( '', map { $_->trimmed_text} $elt->children( @_) )
6274 ;
6275 }
6276  
6277 sub all_children_are
6278 { my( $parent, $cond)= @_;
6279 foreach my $child ($parent->_children)
6280 { return 0 unless( $child->passes( $cond)); }
6281 return $parent;
6282 }
6283  
6284 sub ancestors
6285 { my( $elt, $cond)= @_;
6286 my @ancestors;
6287 while( $elt->{parent})
6288 { $elt= $elt->{parent};
6289 push @ancestors, $elt if( $elt->passes( $cond));
6290 }
6291 return @ancestors;
6292 }
6293  
6294 sub ancestors_or_self
6295 { my( $elt, $cond)= @_;
6296 my @ancestors;
6297 while( $elt)
6298 { push @ancestors, $elt if( $elt->passes( $cond));
6299 $elt= $elt->{parent};
6300 }
6301 return @ancestors;
6302 }
6303  
6304 sub _ancestors
6305 { my( $elt, $include_self)= @_;
6306 my @ancestors= $include_self ? ($elt) : ();
6307 while( $elt= $elt->{parent}) { push @ancestors, $elt; }
6308 return @ancestors;
6309 }
6310  
6311 sub inherit_att
6312 { my $elt= shift;
6313 my $att= shift;
6314 my %tags= map { ($_, 1) } @_;
6315  
6316 do
6317 { if( (defined $elt->{'att'}->{$att})
6318 && ( !%tags || $tags{$XML::Twig::index2gi[$elt->{'gi'}]})
6319 )
6320 { return $elt->{'att'}->{$att}; }
6321 } while( $elt= $elt->{parent});
6322 return undef;
6323 }
6324  
6325 sub _inherit_att_through_cut
6326 { my $elt= shift;
6327 my $att= shift;
6328 my %tags= map { ($_, 1) } @_;
6329  
6330 do
6331 { if( (defined $elt->{'att'}->{$att})
6332 && ( !%tags || $tags{$XML::Twig::index2gi[$elt->{'gi'}]})
6333 )
6334 { return $elt->{'att'}->{$att}; }
6335 } while( $elt= $elt->{parent} || ($elt->{former} && $elt->{former}->{parent}));
6336 return undef;
6337 }
6338  
6339 sub current_ns_prefixes
6340 { my $elt= shift;
6341 my %prefix;
6342 $prefix{''}=1 if( $elt->namespace( ''));
6343 while( $elt)
6344 { my @ns= grep { !m{^xml} } map { m{^([^:]+):} } ($XML::Twig::index2gi[$elt->{'gi'}], $elt->att_names);
6345 $prefix{$_}=1 foreach (@ns);
6346 $elt= $elt->{parent};
6347 }
6348  
6349 return (sort keys %prefix);
6350 }
6351  
6352 # kinda counter-intuitive actually:
6353 # the next element is found by looking for the next open tag after from the
6354 # current one, which is the first child, if it exists, or the next sibling
6355 # or the first next sibling of an ancestor
6356 # optional arguments are:
6357 # - $subtree_root: a reference to an element, when the next element is not
6358 # within $subtree_root anymore then next_elt returns undef
6359 # - $cond: a condition, next_elt returns the next element matching the condition
6360  
6361 sub next_elt
6362 { my $elt= shift;
6363 my $subtree_root= 0;
6364 $subtree_root= shift if( ref( $_[0]) && isa( $_[0], 'XML::Twig::Elt'));
6365 my $cond= shift;
6366 my $next_elt;
6367  
6368 my $ind; # optimization
6369 my $test_cond;
6370 if( $cond) # optimization
6371 { unless( defined( $ind= $XML::Twig::gi2index{$cond}) ) # optimization
6372 { $test_cond= ($cond_cache{$cond} ||= _install_cond( $cond)); } # optimization
6373 } # optimization
6374  
6375 do
6376 { if( $next_elt= $elt->{first_child})
6377 { # simplest case: the elt has a child
6378 }
6379 elsif( $next_elt= $elt->{next_sibling})
6380 { # no child but a next sibling (just check we stay within the subtree)
6381  
6382 # case where elt is subtree_root, is empty and has a sibling
6383 return undef if( $subtree_root && ($elt == $subtree_root));
6384  
6385 }
6386 else
6387 { # case where the element has no child and no next sibling:
6388 # get the first next sibling of an ancestor, checking subtree_root
6389  
6390 # case where elt is subtree_root, is empty and has no sibling
6391 return undef if( $subtree_root && ($elt == $subtree_root));
6392  
6393 $next_elt= $elt->{parent} || return undef;
6394  
6395 until( $next_elt->{next_sibling})
6396 { return undef if( $subtree_root && ($subtree_root == $next_elt));
6397 $next_elt= $next_elt->{parent} || return undef;
6398 }
6399 return undef if( $subtree_root && ($subtree_root == $next_elt));
6400 $next_elt= $next_elt->{next_sibling};
6401 }
6402 $elt= $next_elt; # just in case we need to loop
6403 } until( ! defined $elt
6404 || ! defined $cond
6405 || (defined $ind && ($elt->{gi} eq $ind)) # optimization
6406 || (defined $test_cond && ($test_cond->( $elt)))
6407 );
6408  
6409 return $elt;
6410 }
6411  
6412 # return the next_elt within the element
6413 # just call next_elt with the element as first and second argument
6414 sub first_descendant { return $_[0]->next_elt( @_); }
6415  
6416 # get the last descendant, # then return the element found or call prev_elt with the condition
6417 sub last_descendant
6418 { my( $elt, $cond)= @_;
6419 my $last_descendant= $elt->_last_descendant;
6420 if( !$cond || $last_descendant->matches( $cond))
6421 { return $last_descendant; }
6422 else
6423 { return $last_descendant->prev_elt( $elt, $cond); }
6424 }
6425  
6426 # no argument allowed here, just go down the last_child recursively
6427 sub _last_descendant
6428 { my $elt= shift;
6429 while( my $child= $elt->{last_child}) { $elt= $child; }
6430 return $elt;
6431 }
6432  
6433 # counter-intuitive too:
6434 # the previous element is found by looking
6435 # for the first open tag backwards from the current one
6436 # it's the last descendant of the previous sibling
6437 # if it exists, otherwise it's simply the parent
6438 sub prev_elt
6439 { my $elt= shift;
6440 my $subtree_root= 0;
6441 if( defined $_[0] and (ref( $_[0]) && isa( $_[0], 'XML::Twig::Elt')))
6442 { $subtree_root= shift ;
6443 return undef if( $elt == $subtree_root);
6444 }
6445 my $cond= shift;
6446 # get prev elt
6447 my $prev_elt;
6448 do
6449 { return undef if( $elt == $subtree_root);
6450 if( $prev_elt= $elt->{prev_sibling})
6451 { while( $prev_elt->{last_child})
6452 { $prev_elt= $prev_elt->{last_child}; }
6453 }
6454 else
6455 { $prev_elt= $elt->{parent} || return undef; }
6456 $elt= $prev_elt; # in case we need to loop
6457 } until( $elt->passes( $cond));
6458  
6459 return $elt;
6460 }
6461  
6462 sub _following_elt
6463 { my( $elt)= @_;
6464 while( $elt && !$elt->{next_sibling})
6465 { $elt= $elt->{parent}; }
6466 return $elt ? $elt->{next_sibling} : undef;
6467 }
6468  
6469 sub following_elt
6470 { my( $elt, $cond)= @_;
6471 $elt= $elt->_following_elt || return undef;
6472 return $elt if( !$cond || $elt->matches( $cond));
6473 return $elt->next_elt( $cond);
6474 }
6475  
6476 sub following_elts
6477 { my( $elt, $cond)= @_;
6478 if( !$cond) { undef $cond; }
6479 my $following= $elt->following_elt( $cond);
6480 if( $following)
6481 { my @followings= $following;
6482 while( $following= $following->next_elt( $cond))
6483 { push @followings, $following; }
6484 return( @followings);
6485 }
6486 else
6487 { return (); }
6488 }
6489  
6490 sub _preceding_elt
6491 { my( $elt)= @_;
6492 while( $elt && !$elt->{prev_sibling})
6493 { $elt= $elt->{parent}; }
6494 return $elt ? $elt->{prev_sibling}->_last_descendant : undef;
6495 }
6496  
6497 sub preceding_elt
6498 { my( $elt, $cond)= @_;
6499 $elt= $elt->_preceding_elt || return undef;
6500 return $elt if( !$cond || $elt->matches( $cond));
6501 return $elt->prev_elt( $cond);
6502 }
6503  
6504 sub preceding_elts
6505 { my( $elt, $cond)= @_;
6506 if( !$cond) { undef $cond; }
6507 my $preceding= $elt->preceding_elt( $cond);
6508 if( $preceding)
6509 { my @precedings= $preceding;
6510 while( $preceding= $preceding->prev_elt( $cond))
6511 { push @precedings, $preceding; }
6512 return( @precedings);
6513 }
6514 else
6515 { return (); }
6516 }
6517  
6518 # used in get_xpath
6519 sub _self
6520 { my( $elt, $cond)= @_;
6521 return $cond ? $elt->matches( $cond) : $elt;
6522 }
6523  
6524 sub next_n_elt
6525 { my $elt= shift;
6526 my $offset= shift || return undef;
6527 foreach (1..$offset)
6528 { $elt= $elt->next_elt( @_) || return undef; }
6529 return $elt;
6530 }
6531  
6532 # checks whether $elt is included in $ancestor, returns 1 in that case
6533 sub in
6534 { my ($elt, $ancestor)= @_;
6535 if( ref( $ancestor) && isa( $ancestor, 'XML::Twig::Elt'))
6536 { # element
6537 while( $elt= $elt->{parent}) { return $elt if( $elt == $ancestor); }
6538 }
6539 else
6540 { # condition
6541 while( $elt= $elt->{parent}) { return $elt if( $elt->matches( $ancestor)); }
6542 }
6543 return 0;
6544 }
6545  
6546 sub first_child_text
6547 { my $elt= shift;
6548 my $dest=$elt->first_child(@_) or return '';
6549 return $dest->text;
6550 }
6551  
6552 sub fields
6553 { my $elt= shift;
6554 return map { $elt->field( $_) } @_;
6555 }
6556  
6557 sub first_child_trimmed_text
6558 { my $elt= shift;
6559 my $dest=$elt->first_child(@_) or return '';
6560 return $dest->trimmed_text;
6561 }
6562  
6563 sub first_child_matches
6564 { my $elt= shift;
6565 my $dest= $elt->{first_child} or return undef;
6566 return $dest->passes( @_);
6567 }
6568  
6569 sub last_child_text
6570 { my $elt= shift;
6571 my $dest=$elt->last_child(@_) or return '';
6572 return $dest->text;
6573 }
6574  
6575 sub last_child_trimmed_text
6576 { my $elt= shift;
6577 my $dest=$elt->last_child(@_) or return '';
6578 return $dest->trimmed_text;
6579 }
6580  
6581 sub last_child_matches
6582 { my $elt= shift;
6583 my $dest= $elt->{last_child} or return undef;
6584 return $dest->passes( @_);
6585 }
6586  
6587 sub child_text
6588 { my $elt= shift;
6589 my $dest=$elt->child(@_) or return '';
6590 return $dest->text;
6591 }
6592  
6593 sub child_trimmed_text
6594 { my $elt= shift;
6595 my $dest=$elt->child(@_) or return '';
6596 return $dest->trimmed_text;
6597 }
6598  
6599 sub child_matches
6600 { my $elt= shift;
6601 my $nb= shift;
6602 my $dest= $elt->child( $nb) or return undef;
6603 return $dest->passes( @_);
6604 }
6605  
6606 sub prev_sibling_text
6607 { my $elt= shift;
6608 my $dest= $elt->_prev_sibling(@_) or return '';
6609 return $dest->text;
6610 }
6611  
6612 sub prev_sibling_trimmed_text
6613 { my $elt= shift;
6614 my $dest= $elt->_prev_sibling(@_) or return '';
6615 return $dest->trimmed_text;
6616 }
6617  
6618 sub prev_sibling_matches
6619 { my $elt= shift;
6620 my $dest= $elt->{prev_sibling} or return undef;
6621 return $dest->passes( @_);
6622 }
6623  
6624 sub next_sibling_text
6625 { my $elt= shift;
6626 my $dest= $elt->next_sibling(@_) or return '';
6627 return $dest->text;
6628 }
6629  
6630 sub next_sibling_trimmed_text
6631 { my $elt= shift;
6632 my $dest= $elt->next_sibling(@_) or return '';
6633 return $dest->trimmed_text;
6634 }
6635  
6636 sub next_sibling_matches
6637 { my $elt= shift;
6638 my $dest= $elt->{next_sibling} or return undef;
6639 return $dest->passes( @_);
6640 }
6641  
6642 sub prev_elt_text
6643 { my $elt= shift;
6644 my $dest= $elt->prev_elt(@_) or return '';
6645 return $dest->text;
6646 }
6647  
6648 sub prev_elt_trimmed_text
6649 { my $elt= shift;
6650 my $dest= $elt->prev_elt(@_) or return '';
6651 return $dest->trimmed_text;
6652 }
6653  
6654 sub prev_elt_matches
6655 { my $elt= shift;
6656 my $dest= $elt->prev_elt or return undef;
6657 return $dest->passes( @_);
6658 }
6659  
6660 sub next_elt_text
6661 { my $elt= shift;
6662 my $dest= $elt->next_elt(@_) or return '';
6663 return $dest->text;
6664 }
6665  
6666 sub next_elt_trimmed_text
6667 { my $elt= shift;
6668 my $dest= $elt->next_elt(@_) or return '';
6669 return $dest->trimmed_text;
6670 }
6671  
6672 sub next_elt_matches
6673 { my $elt= shift;
6674 my $dest= $elt->next_elt or return undef;
6675 return $dest->passes( @_);
6676 }
6677  
6678 sub parent_text
6679 { my $elt= shift;
6680 my $dest= $elt->parent(@_) or return '';
6681 return $dest->text;
6682 }
6683  
6684 sub parent_trimmed_text
6685 { my $elt= shift;
6686 my $dest= $elt->parent(@_) or return '';
6687 return $dest->trimmed_text;
6688 }
6689  
6690 sub parent_matches
6691 { my $elt= shift;
6692 my $dest= $elt->{parent} or return undef;
6693 return $dest->passes( @_);
6694 }
6695  
6696 sub is_first_child
6697 { my $elt= shift;
6698 my $parent= $elt->{parent} or return 0;
6699 my $first_child= $parent->first_child( @_) or return 0;
6700 return ($first_child == $elt) ? $elt : 0;
6701 }
6702  
6703 sub is_last_child
6704 { my $elt= shift;
6705 my $parent= $elt->{parent} or return 0;
6706 my $last_child= $parent->last_child( @_) or return 0;
6707 return ($last_child == $elt) ? $elt : 0;
6708 }
6709  
6710 # returns the depth level of the element
6711 # if 2 parameter are used then counts the 2cd element name in the
6712 # ancestors list
6713 sub level
6714 { my( $elt, $cond)= @_;
6715 my $level=0;
6716 my $name=shift || '';
6717 while( $elt= $elt->{parent}) { $level++ if( !$cond || $elt->matches( $cond)); }
6718 return $level;
6719 }
6720  
6721 # checks whether $elt has an ancestor that satisfies $cond, returns the ancestor
6722 sub in_context
6723 { my ($elt, $cond, $level)= @_;
6724 $level= -1 unless( $level) ; # $level-- will never hit 0
6725  
6726 while( $level)
6727 { $elt= $elt->{parent} or return 0;
6728 if( $elt->matches( $cond)) { return $elt; }
6729 $level--;
6730 }
6731 return 0;
6732 }
6733  
6734 sub _descendants
6735 { my( $subtree_root, $include_self)= @_;
6736 my @descendants= $include_self ? ($subtree_root) : ();
6737  
6738 my $elt= $subtree_root;
6739 my $next_elt;
6740  
6741 MAIN: while( 1)
6742 { if( $next_elt= $elt->{first_child})
6743 { # simplest case: the elt has a child
6744 }
6745 elsif( $next_elt= $elt->{next_sibling})
6746 { # no child but a next sibling (just check we stay within the subtree)
6747  
6748 # case where elt is subtree_root, is empty and has a sibling
6749 last MAIN if( $elt == $subtree_root);
6750 }
6751 else
6752 { # case where the element has no child and no next sibling:
6753 # get the first next sibling of an ancestor, checking subtree_root
6754  
6755 # case where elt is subtree_root, is empty and has no sibling
6756 last MAIN if( $elt == $subtree_root);
6757  
6758 # backtrack until we find a parent with a next sibling
6759 $next_elt= $elt->{parent} || last;
6760 until( $next_elt->{next_sibling})
6761 { last MAIN if( $subtree_root == $next_elt);
6762 $next_elt= $next_elt->{parent} || last MAIN;
6763 }
6764 last MAIN if( $subtree_root == $next_elt);
6765 $next_elt= $next_elt->{next_sibling};
6766 }
6767 $elt= $next_elt || last MAIN;
6768 push @descendants, $elt;
6769 }
6770 return @descendants;
6771 }
6772  
6773 sub descendants
6774 { my( $subtree_root, $cond)= @_;
6775 my @descendants=();
6776 my $elt= $subtree_root;
6777  
6778 # this branch is pure optimization for speed: if $cond is a gi replace it
6779 # by the index of the gi and loop here
6780 # start optimization
6781 my $ind;
6782 if( !$cond || ( defined ( $ind= $XML::Twig::gi2index{$cond})) )
6783 {
6784 my $next_elt;
6785  
6786 while( 1)
6787 { if( $next_elt= $elt->{first_child})
6788 { # simplest case: the elt has a child
6789 }
6790 elsif( $next_elt= $elt->{next_sibling})
6791 { # no child but a next sibling (just check we stay within the subtree)
6792  
6793 # case where elt is subtree_root, is empty and has a sibling
6794 last if( $subtree_root && ($elt == $subtree_root));
6795 }
6796 else
6797 { # case where the element has no child and no next sibling:
6798 # get the first next sibling of an ancestor, checking subtree_root
6799  
6800 # case where elt is subtree_root, is empty and has no sibling
6801 last if( $subtree_root && ($elt == $subtree_root));
6802  
6803 # backtrack until we find a parent with a next sibling
6804 $next_elt= $elt->{parent} || last;
6805 until( $next_elt->{next_sibling})
6806 { last if( $subtree_root && ($subtree_root == $next_elt));
6807 $next_elt= $next_elt->{parent} || last;
6808 }
6809 last if( $subtree_root && ($subtree_root == $next_elt));
6810 $next_elt= $next_elt->{next_sibling};
6811 }
6812 $elt= $next_elt || last;
6813 push @descendants, $elt if( !$cond || ($elt->{gi} eq $ind));
6814 }
6815 }
6816 else
6817 # end optimization
6818 { # branch for a complex condition: use the regular (slow but simple) way
6819 while( $elt= $elt->next_elt( $subtree_root, $cond))
6820 { push @descendants, $elt; }
6821 }
6822 return @descendants;
6823 }
6824  
6825 sub descendants_or_self
6826 { my( $elt, $cond)= @_;
6827 my @descendants= $elt->passes( $cond) ? ($elt) : ();
6828 push @descendants, $elt->descendants( $cond);
6829 return @descendants;
6830 }
6831  
6832 sub sibling
6833 { my $elt= shift;
6834 my $nb= shift;
6835 if( $nb > 0)
6836 { foreach( 1..$nb)
6837 { $elt= $elt->next_sibling( @_) or return undef; }
6838 }
6839 elsif( $nb < 0)
6840 { foreach( 1..(-$nb))
6841 { $elt= $elt->prev_sibling( @_) or return undef; }
6842 }
6843 else # $nb == 0
6844 { return $elt->passes( $_[0]); }
6845 return $elt;
6846 }
6847  
6848 sub sibling_text
6849 { my $elt= sibling( @_);
6850 return $elt ? $elt->text : undef;
6851 }
6852  
6853 sub child
6854 { my $elt= shift;
6855 my $nb= shift;
6856 if( $nb >= 0)
6857 { $elt= $elt->first_child( @_) or return undef;
6858 foreach( 1..$nb)
6859 { $elt= $elt->next_sibling( @_) or return undef; }
6860 }
6861 else
6862 { $elt= $elt->last_child( @_) or return undef;
6863 foreach( 2..(-$nb))
6864 { $elt= $elt->prev_sibling( @_) or return undef; }
6865 }
6866 return $elt;
6867 }
6868  
6869 sub prev_siblings
6870 { my $elt= shift;
6871 my @siblings=();
6872 while( $elt= $elt->prev_sibling( @_))
6873 { unshift @siblings, $elt; }
6874 return @siblings;
6875 }
6876  
6877 sub siblings
6878 { my $elt= shift;
6879 return grep { $_ ne $elt } $elt->{parent}->children( @_);
6880 }
6881  
6882 sub pos
6883 { my $elt= shift;
6884 return 0 if ($_[0] && !$elt->matches( @_));
6885 my $pos=1;
6886 $pos++ while( $elt= $elt->prev_sibling( @_));
6887 return $pos;
6888 }
6889  
6890 sub next_siblings
6891 { my $elt= shift;
6892 my @siblings=();
6893 while( $elt= $elt->next_sibling( @_))
6894 { push @siblings, $elt; }
6895 return @siblings;
6896 }
6897  
6898 # used by get_xpath: parses the xpath expression and generates a sub that performs the
6899 # search
6900 { my %axis2method;
6901 BEGIN { %axis2method= ( child => 'children',
6902 descendant => 'descendants',
6903 'descendant-or-self' => 'descendants_or_self',
6904 parent => 'parent_is',
6905 ancestor => 'ancestors',
6906 'ancestor-or-self' => 'ancestors_or_self',
6907 'following-sibling' => 'next_siblings',
6908 'preceding-sibling' => 'prev_siblings',
6909 following => 'following_elts',
6910 preceding => 'preceding_elts',
6911 self => '_self',
6912 );
6913 }
6914  
6915 sub _install_xpath
6916 { my( $xpath_exp, $type)= @_;
6917 my $original_exp= $xpath_exp;
6918 my $sub= 'my $elt= shift; my @results;';
6919  
6920 # grab the root if expression starts with a /
6921 if( $xpath_exp=~ s{^/}{})
6922 { $sub .= '@results= ($elt->twig) || croak "cannot use an XPath query starting with a / on a node not attached to a whole twig";'; }
6923 elsif( $xpath_exp=~ s{^\./}{})
6924 { $sub .= '@results= ($elt);'; }
6925 else
6926 { $sub .= '@results= ($elt);'; }
6927  
6928 #warn "xpath_exp= '$xpath_exp'\n";
6929  
6930 while( $xpath_exp &&
6931 $xpath_exp=~s{^\s*(/?)
6932 # the xxx=~/regexp/ is a pain as it includes /
6933 (\s*(?:(?:($REG_AXIS)::)?(\*|$REG_TAG_PART|\.\.|\.)\s*)?($REG_PREDICATE_ALT*)
6934 )
6935 (/|$)}{}xo)
6936  
6937 { my( $wildcard, $sub_exp, $axis, $gi, $predicates)= ($1, $2, $3, $4, $5);
6938 if( $axis && ! $gi)
6939 { _croak_and_doublecheck_xpath( $original_exp, "error in xpath expression $original_exp"); }
6940  
6941 # grab a parent
6942 if( $sub_exp eq '..')
6943 { _croak_and_doublecheck_xpath( $original_exp, "error in xpath expression $original_exp") if( $wildcard);
6944 $sub .= '@results= map { $_->{parent}} @results;';
6945 }
6946 # test the element itself
6947 elsif( $sub_exp=~ m{^\.(.*)$}s)
6948 { $sub .= "\@results= grep { \$_->matches( q{$1}) } \@results;" }
6949 # grab children
6950 else
6951 {
6952 if( !$axis)
6953 { $axis= $wildcard ? 'descendant' : 'child'; }
6954 if( !$gi or $gi eq '*') { $gi=''; }
6955 my $function;
6956  
6957 # "special" predicates, that return just one element
6958 if( $predicates && ($predicates =~ m{^\s*\[\s*((-\s*)?\d+)\s*\]\s*$}))
6959 { # [<nb>]
6960 my $offset= $1;
6961 $offset-- if( $offset > 0);
6962 $function= $axis eq 'descendant' ? "next_n_elt( $offset, '$gi')"
6963 : $axis eq 'child' ? "child( $offset, '$gi')"
6964 : _croak_and_doublecheck_xpath( $original_exp, "error [$1] not supported along axis '$axis'")
6965 ;
6966 $sub .= "\@results= grep { \$_ } map { \$_->$function } \@results;"
6967 }
6968 elsif( $predicates && ($predicates =~ m{^\s*\[\s*last\s*\(\s*\)\s*\]\s*$}) )
6969 { # last()
6970 _croak_and_doublecheck_xpath( $original_exp, "error in xpath expression $original_exp, usage of // and last() not supported") if( $wildcard);
6971 $sub .= "\@results= map { \$_->last_child( '$gi') } \@results;";
6972 }
6973 else
6974 { # follow the axis
6975 #warn "axis: '$axis' - method: '$axis2method{$axis}' - gi: '$gi'\n";
6976  
6977 my $follow_axis= " \$_->$axis2method{$axis}( '$gi')";
6978 my $step= $follow_axis;
6979  
6980 # now filter using the predicate
6981 while( $predicates=~ s{^\s*($REG_PREDICATE_ALT)\s*}{}o)
6982 { my $pred= $1;
6983 $pred=~ s{^\s*\[\s*}{};
6984 $pred=~ s{\s*\]\s*$}{};
6985 my $test="";
6986 my $pos;
6987 if( $pred=~ m{^(-?\s*\d+)$})
6988 { my $pos= $1;
6989 if( $step=~ m{^\s*grep(.*) (\$_->\w+\(\s*'[^']*'\s*\))})
6990 { $step= "XML::Twig::_first_n $1 $pos, $2"; }
6991 else
6992 { if( $pos > 0) { $pos--; }
6993 $step= "($step)[$pos]";
6994 }
6995 #warn "number predicate '$pos' - generated step '$step'\n";
6996 }
6997 else
6998 { my $syntax_error=0;
6999 do
7000 { if( $pred =~ s{^string\(\s*\)\s*=\s*($REG_STRING)\s*}{}o) # string()="string" pred
7001 { $test .= "\$_->text eq $1"; }
7002 elsif( $pred =~ s{^string\(\s*\)\s*!=\s*($REG_STRING)\s*}{}o) # string()!="string" pred
7003 { $test .= "\$_->text ne $1"; }
7004 elsif( $pred =~ s{^string\(\s*\)\s*(>|<|>=|<=|!=|=)\s*($REG_NUMBER)\s*}{}o) # string() <comp> <number> pred
7005 { $test .= _gen_nb_comparison( '$_->text', $1, $2); }
7006 elsif( $pred =~ s{^string\(\s*\)\s*($REG_MATCH)\s*($REG_REGEXP)\s*}{}o) # string()=~/regex/ pred
7007 { my( $match, $regexp)= ($1, $2);
7008 $test .= "\$_->text $match $regexp";
7009 }
7010 elsif( $pred =~ s{^string\(\s*\)\s*}{}o) # string() pred
7011 { $test .= "\$_->text"; }
7012 elsif( $pred=~ s{^@($REG_TAG_NAME)\s*($REG_OP)\s*($REG_STRING|$REG_NUMBER)}{}o) # @att="val" pred
7013 { my( $att, $oper, $val)= ($1, _op( $2), $3);
7014 $test .= qq{((defined \$_->{'att'}->{"$att"}) && (\$_->{'att'}->{"$att"} $oper $val))};
7015 }
7016 elsif( $pred =~ s{^@($REG_TAG_NAME)\s*($REG_MATCH)\s*($REG_REGEXP)\s*}{}o) # @att=~/regex/ pred XXX
7017 { my( $att, $match, $regexp)= ($1, $2, $3);
7018 $test .= qq{((defined \$_->{'att'}->{"$att"}) && (\$_->{'att'}->{"$att"} $match $regexp))};;
7019 }
7020 elsif( $pred=~ s{^@($REG_TAG_NAME)\s*}{}o) # @att pred
7021 { $test .= qq{(defined \$_->{'att'}->{"$1"})}; }
7022 elsif( $pred=~ s{^\s*(?:not|!)\s*@($REG_TAG_NAME)\s*}{}o) # not @att pred
7023 { $test .= qq{((\$_->is_elt) && (not defined \$_->{'att'}->{"$1"}))}; }
7024 elsif( $pred=~ s{^\s*([()])}{}) # ( or ) (just add to the test)
7025 { $test .= qq{$1}; }
7026 elsif( $pred=~ s{^\s*(and|or)\s*}{})
7027 { $test .= lc " $1 "; }
7028 else
7029 { $syntax_error=1; }
7030  
7031 } while( !$syntax_error && $pred);
7032 _croak_and_doublecheck_xpath( $original_exp, "error in xpath expression $original_exp at $pred") if( $pred);
7033 $step= " grep { $test } $step ";
7034 }
7035 }
7036 #warn "step: '$step'";
7037 $sub .= "\@results= grep defined, map { $step } \@results;";
7038 }
7039 }
7040 }
7041  
7042 if( $xpath_exp)
7043 { _croak_and_doublecheck_xpath( $original_exp, "error in xpath expression $original_exp around $xpath_exp"); }
7044  
7045 $sub .= q{return XML::Twig::_unique_elts( @results); };
7046 #warn "generated: '$sub'\n";
7047 my $s= eval "sub { $NO_WARNINGS; $sub }"; ## no critic (ProhibitStringyEval)
7048 if( $@)
7049 { _croak_and_doublecheck_xpath( $original_exp, "error in xpath expression $original_exp ($@);") }
7050 return( $s);
7051 }
7052 }
7053  
7054 sub _gen_nb_comparison {
7055 my ($xml, $op, $nb) = @_;
7056 if ($op eq '=') { $op = '=='; }
7057 return "$xml $op $nb";
7058 }
7059  
7060 sub _croak_and_doublecheck_xpath
7061 { my $xpath_expression= shift;
7062 my $mess= join( "\n", @_);
7063 if( $XML::Twig::XPath::VERSION || 0)
7064 { my $check_twig= XML::Twig::XPath->new;
7065 if( eval { $check_twig->{twig_xp}->_parse( $xpath_expression) })
7066 { $mess .= "\nthe expression is a valid XPath statement, and you are using XML::Twig::XPath, but"
7067 . "\nyou are using either 'find_nodes' or 'get_xpath' where the method you likely wanted"
7068 . "\nto use is 'findnodes', which is the only one that uses the full XPath engine"
7069 . "\nalternatively, check that the main twig is an XML::Twig::XPath object,"
7070 . "\n and not just an XML::Twig object"
7071 . "\n"
7072 ;
7073 }
7074 }
7075 croak $mess;
7076 }
7077  
7078 { # extremely elaborate caching mechanism
7079 my %xpath; # xpath_expression => subroutine_code;
7080 sub get_xpath
7081 { my( $elt, $xpath_exp, $offset)= @_;
7082 my $sub= ($xpath{$xpath_exp} ||= _install_xpath( $xpath_exp));
7083 return $sub->( $elt) unless( defined $offset);
7084 my @res= $sub->( $elt);
7085 return $res[$offset];
7086 }
7087 }
7088  
7089 sub findvalues
7090 { my $elt= shift;
7091 return map { $_->text } $elt->get_xpath( @_);
7092 }
7093  
7094 sub findvalue
7095 { my $elt= shift;
7096 return join '', map { $_->text } $elt->get_xpath( @_);
7097 }
7098  
7099 # XML::XPath compatibility
7100 sub getElementById { return $_[0]->twig->elt_id( $_[1]); }
7101 sub getChildNodes { my @children= do { my $elt= $_[0]; my @children=(); my $child= $elt->{first_child}; while( $child) { push @children, $child; $child= $child->{next_sibling}; } @children; }; return wantarray ? @children : \@children; }
7102  
7103 sub _flushed { return $_[0]->{flushed}; }
7104 sub _set_flushed { $_[0]->{flushed}=1; }
7105 sub _del_flushed { delete $_[0]->{flushed}; }
7106  
7107 sub cut
7108 { my $elt= shift;
7109 my( $parent, $prev_sibling, $next_sibling);
7110 $parent= $elt->{parent};
7111 if( ! $parent && $elt->is_elt)
7112 { # are we cutting the root?
7113 my $t= $elt->{twig};
7114 if( $t && ! $t->{twig_parsing})
7115 { delete $t->{twig_root};
7116 delete $elt->{twig};
7117 return $elt;
7118 } # cutt`ing the root
7119 else
7120 { return; } # cutting an orphan, returning $elt would break backward compatibility
7121 }
7122  
7123 # save the old links, that'll make it easier for some loops
7124 foreach my $link ( qw(parent prev_sibling next_sibling) )
7125 { $elt->{former}->{$link}= $elt->{$link};
7126 weaken( $elt->{former}->{$link});
7127 }
7128  
7129 # if we cut the current element then its parent becomes the current elt
7130 if( $elt->{twig_current})
7131 { my $twig_current= $elt->{parent};
7132 $elt->twig->{twig_current}= $twig_current;
7133 $twig_current->{'twig_current'}=1;
7134 delete $elt->{'twig_current'};
7135 }
7136  
7137 if( $parent->{first_child} && $parent->{first_child} == $elt)
7138 { $parent->{first_child}= $elt->{next_sibling};
7139 # cutting can make the parent empty
7140 if( ! $parent->{first_child}) { $parent->{empty}= 1; }
7141 }
7142  
7143 if( $parent->{last_child} && $parent->{last_child} == $elt)
7144 { delete $parent->{empty}; $parent->{last_child}=$elt->{prev_sibling}; weaken( $parent->{last_child}); ;
7145 }
7146  
7147 if( $prev_sibling= $elt->{prev_sibling})
7148 { $prev_sibling->{next_sibling}= $elt->{next_sibling}; }
7149 if( $next_sibling= $elt->{next_sibling})
7150 { $next_sibling->{prev_sibling}=$elt->{prev_sibling}; weaken( $next_sibling->{prev_sibling}); ; }
7151  
7152 $elt->{parent}=undef; weaken( $elt->{parent}); ;
7153 $elt->{prev_sibling}=undef; weaken( $elt->{prev_sibling}); ;
7154 $elt->{next_sibling}= undef;
7155  
7156 # merge 2 (now) consecutive text nodes if they are of the same type
7157 # (type can be PCDATA or CDATA)
7158 if( $prev_sibling && $next_sibling && $prev_sibling->is_text && ( $XML::Twig::index2gi[$prev_sibling->{'gi'}] eq $XML::Twig::index2gi[$next_sibling->{'gi'}]))
7159 { $prev_sibling->merge_text( $next_sibling); }
7160  
7161 return $elt;
7162 }
7163  
7164 sub former_next_sibling { return $_[0]->{former}->{next_sibling}; }
7165 sub former_prev_sibling { return $_[0]->{former}->{prev_sibling}; }
7166 sub former_parent { return $_[0]->{former}->{parent}; }
7167  
7168 sub cut_children
7169 { my( $elt, $exp)= @_;
7170 my @children= $elt->children( $exp);
7171 foreach (@children) { $_->cut; }
7172 if( ! $elt->has_children) { $elt->{empty}= 1; }
7173 return @children;
7174 }
7175  
7176 sub cut_descendants
7177 { my( $elt, $exp)= @_;
7178 my @descendants= $elt->descendants( $exp);
7179 foreach ($elt->descendants( $exp)) { $_->cut; }
7180 if( ! $elt->has_children) { $elt->{empty}= 1; }
7181 return @descendants;
7182 }
7183  
7184 sub erase
7185 { my $elt= shift;
7186 #you cannot erase the current element
7187 if( $elt->{twig_current})
7188 { croak "trying to erase an element before it has been completely parsed"; }
7189 if( my $parent= $elt->{parent})
7190 { # normal case
7191 $elt->_move_extra_data_after_erase;
7192 my @children= do { my $elt= $elt; my @children=(); my $child= $elt->{first_child}; while( $child) { push @children, $child; $child= $child->{next_sibling}; } @children; };
7193 if( @children)
7194 {
7195 # elt has children, move them up
7196  
7197 # the first child may need to be merged with a previous text
7198 my $first_child= shift @children;
7199 $first_child->move( before => $elt);
7200 my $prev= $first_child->{prev_sibling};
7201 if( $prev && $prev->is_text && ($XML::Twig::index2gi[$first_child->{'gi'}] eq $XML::Twig::index2gi[$prev->{'gi'}]) )
7202 { $prev->merge_text( $first_child); }
7203  
7204 # move the rest of the children
7205 foreach my $child (@children)
7206 { $child->move( before => $elt); }
7207  
7208 # now the elt had no child, delete it
7209 $elt->delete;
7210  
7211 # now see if we need to merge the last child with the next element
7212 my $last_child= $children[-1] || $first_child; # if no last child, then it's also the first child
7213 my $next= $last_child->{next_sibling};
7214 if( $next && $next->is_text && ($XML::Twig::index2gi[$last_child->{'gi'}] eq $XML::Twig::index2gi[$next->{'gi'}]) )
7215 { $last_child->merge_text( $next); }
7216  
7217 # if parsing and have now a PCDATA text, mark so we can normalize later on if need be
7218 if( $parent->{twig_current} && $last_child->is_text) { $parent->{twig_to_be_normalized}=1; }
7219 }
7220 else
7221 { # no children, just cut the elt
7222 $elt->delete;
7223 }
7224 }
7225 else
7226 { # trying to erase the root (of a twig or of a cut/new element)
7227 my @children= do { my $elt= $elt; my @children=(); my $child= $elt->{first_child}; while( $child) { push @children, $child; $child= $child->{next_sibling}; } @children; };
7228 unless( @children == 1)
7229 { croak "can only erase an element with no parent if it has a single child"; }
7230 $elt->_move_extra_data_after_erase;
7231 my $child= shift @children;
7232 $child->{parent}=undef; weaken( $child->{parent}); ;
7233 my $twig= $elt->twig;
7234 $twig->set_root( $child);
7235 }
7236  
7237 return $elt;
7238  
7239 }
7240  
7241 sub _move_extra_data_after_erase
7242 { my( $elt)= @_;
7243 # extra_data
7244 if( my $extra_data= $elt->{extra_data})
7245 { my $target= $elt->{first_child} || $elt->{next_sibling};
7246 if( $target)
7247 {
7248 if( $target->is( $ELT))
7249 { $target->set_extra_data( $extra_data . ($target->extra_data || '')); }
7250 elsif( $target->is( $TEXT))
7251 { $target->_unshift_extra_data_in_pcdata( $extra_data, 0); } # TO CHECK
7252 }
7253 else
7254 { my $parent= $elt->{parent}; # always exists or the erase cannot be performed
7255 $parent->_prefix_extra_data_before_end_tag( $extra_data);
7256 }
7257 }
7258  
7259 # extra_data_before_end_tag
7260 if( my $extra_data= $elt->{extra_data_before_end_tag})
7261 { if( my $target= $elt->{next_sibling})
7262 { if( $target->is( $ELT))
7263 { $target->set_extra_data( $extra_data . ($target->extra_data || '')); }
7264 elsif( $target->is( $TEXT))
7265 {
7266 $target->_unshift_extra_data_in_pcdata( $extra_data, 0);
7267 }
7268 }
7269 elsif( my $parent= $elt->{parent})
7270 { $parent->_prefix_extra_data_before_end_tag( $extra_data); }
7271 }
7272  
7273 return $elt;
7274  
7275 }
7276 BEGIN
7277 { my %method= ( before => \&paste_before,
7278 after => \&paste_after,
7279 first_child => \&paste_first_child,
7280 last_child => \&paste_last_child,
7281 within => \&paste_within,
7282 );
7283  
7284 # paste elt somewhere around ref
7285 # pos can be first_child (default), last_child, before, after or within
7286 sub paste ## no critic (Subroutines::ProhibitNestedSubs);
7287 { my $elt= shift;
7288 if( $elt->{parent})
7289 { croak "cannot paste an element that belongs to a tree"; }
7290 my $pos;
7291 my $ref;
7292 if( ref $_[0])
7293 { $pos= 'first_child';
7294 croak "wrong argument order in paste, should be $_[1] first" if($_[1]);
7295 }
7296 else
7297 { $pos= _op_position(shift); }
7298  
7299 if( my $method= $method{$pos})
7300 {
7301 unless( ref( $_[0]) && isa( $_[0], 'XML::Twig::Elt'))
7302 { if( ! defined( $_[0]))
7303 { croak "missing target in paste"; }
7304 elsif( ! ref( $_[0]))
7305 { croak "wrong target type in paste (not a reference), should be XML::Twig::Elt or a subclass"; }
7306 else
7307 { my $ref= ref $_[0];
7308 croak "wrong target type in paste: '$ref', should be XML::Twig::Elt or a subclass";
7309 }
7310 }
7311 $ref= $_[0];
7312 # check here so error message lists the caller file/line
7313 if( !$ref->{parent} && ($pos=~ m{^(before|after)$}) && !(exists $elt->{'target'}) && !(exists $elt->{'comment'}))
7314 { croak "cannot paste $1 root"; }
7315 $elt->$method( @_);
7316 }
7317 else
7318 { croak "tried to paste in wrong position '$pos', allowed positions are "
7319 . _allowed_positions() . ".\n";
7320 }
7321 if( (my $ids= $elt->{twig_id_list}) && (my $t= $ref->twig) )
7322 { $t->{twig_id_list}||={};
7323 foreach my $id (keys %$ids)
7324 { $t->{twig_id_list}->{$id}= $ids->{$id};
7325 weaken( $t->{twig_id_list}->{$id});
7326 }
7327 }
7328 return $elt;
7329 }
7330  
7331 sub paste_before
7332 { my( $elt, $ref)= @_;
7333 my( $parent, $prev_sibling, $next_sibling );
7334  
7335 # trying to paste before an orphan (root or detached wlt)
7336 unless( $ref->{parent})
7337 { if( my $t= $ref->twig)
7338 { if( (exists $elt->{'comment'}) || (exists $elt->{'target'})) # we can still do this
7339 { $t->_add_cpi_outside_of_root( leading_cpi => $elt); return; }
7340 else
7341 { croak "cannot paste before root"; }
7342 }
7343 else
7344 { croak "cannot paste before an orphan element"; }
7345 }
7346 $parent= $ref->{parent};
7347 $prev_sibling= $ref->{prev_sibling};
7348 $next_sibling= $ref;
7349  
7350 $elt->{parent}=$parent; weaken( $elt->{parent}); ;
7351 if( $parent->{first_child} == $ref) { $parent->{first_child}= $elt; }
7352  
7353 if( $prev_sibling) { $prev_sibling->{next_sibling}= $elt; }
7354 $elt->{prev_sibling}=$prev_sibling; weaken( $elt->{prev_sibling}); ;
7355  
7356 $next_sibling->{prev_sibling}=$elt; weaken( $next_sibling->{prev_sibling}); ;
7357 $elt->{next_sibling}= $ref;
7358 return $elt;
7359 }
7360  
7361 sub paste_after
7362 { my( $elt, $ref)= @_;
7363 my( $parent, $prev_sibling, $next_sibling );
7364  
7365 # trying to paste after an orphan (root or detached wlt)
7366 unless( $ref->{parent})
7367 { if( my $t= $ref->twig)
7368 { if( (exists $elt->{'comment'}) || (exists $elt->{'target'})) # we can still do this
7369 { $t->_add_cpi_outside_of_root( trailing_cpi => $elt); return; }
7370 else
7371 { croak "cannot paste after root"; }
7372 }
7373 else
7374 { croak "cannot paste after an orphan element"; }
7375 }
7376 $parent= $ref->{parent};
7377 $prev_sibling= $ref;
7378 $next_sibling= $ref->{next_sibling};
7379  
7380 $elt->{parent}=$parent; weaken( $elt->{parent}); ;
7381 if( $parent->{last_child}== $ref) { delete $parent->{empty}; $parent->{last_child}=$elt; weaken( $parent->{last_child}); ; }
7382  
7383 $prev_sibling->{next_sibling}= $elt;
7384 $elt->{prev_sibling}=$prev_sibling; weaken( $elt->{prev_sibling}); ;
7385  
7386 if( $next_sibling) { $next_sibling->{prev_sibling}=$elt; weaken( $next_sibling->{prev_sibling}); ; }
7387 $elt->{next_sibling}= $next_sibling;
7388 return $elt;
7389  
7390 }
7391  
7392 sub paste_first_child
7393 { my( $elt, $ref)= @_;
7394 my( $parent, $prev_sibling, $next_sibling );
7395 $parent= $ref;
7396 $next_sibling= $ref->{first_child};
7397  
7398 $elt->{parent}=$parent; weaken( $elt->{parent}); ;
7399 $parent->{first_child}= $elt;
7400 unless( $parent->{last_child}) { delete $parent->{empty}; $parent->{last_child}=$elt; weaken( $parent->{last_child}); ; }
7401  
7402 $elt->{prev_sibling}=undef; weaken( $elt->{prev_sibling}); ;
7403  
7404 if( $next_sibling) { $next_sibling->{prev_sibling}=$elt; weaken( $next_sibling->{prev_sibling}); ; }
7405 $elt->{next_sibling}= $next_sibling;
7406 return $elt;
7407 }
7408  
7409 sub paste_last_child
7410 { my( $elt, $ref)= @_;
7411 my( $parent, $prev_sibling, $next_sibling );
7412 $parent= $ref;
7413 $prev_sibling= $ref->{last_child};
7414  
7415 $elt->{parent}=$parent; weaken( $elt->{parent}); ;
7416 delete $parent->{empty}; $parent->{last_child}=$elt; weaken( $parent->{last_child}); ;
7417 unless( $parent->{first_child}) { $parent->{first_child}= $elt; }
7418  
7419 $elt->{prev_sibling}=$prev_sibling; weaken( $elt->{prev_sibling}); ;
7420 if( $prev_sibling) { $prev_sibling->{next_sibling}= $elt; }
7421  
7422 $elt->{next_sibling}= undef;
7423 return $elt;
7424 }
7425  
7426 sub paste_within
7427 { my( $elt, $ref, $offset)= @_;
7428 my $text= $ref->is_text ? $ref : $ref->next_elt( $TEXT, $ref);
7429 my $new= $text->split_at( $offset);
7430 $elt->paste_before( $new);
7431 return $elt;
7432 }
7433 }
7434  
7435 # load an element into a structure similar to XML::Simple's
7436 sub simplify
7437 { my $elt= shift;
7438  
7439 # normalize option names
7440 my %options= @_;
7441 %options= map { my ($key, $val)= ($_, $options{$_});
7442 $key=~ s{(\w)([A-Z])}{$1_\L$2}g;
7443 $key => $val
7444 } keys %options;
7445  
7446 # check options
7447 my @allowed_options= qw( keyattr forcearray noattr content_key
7448 var var_regexp variables var_attr
7449 group_tags forcecontent
7450 normalise_space normalize_space
7451 );
7452 my %allowed_options= map { $_ => 1 } @allowed_options;
7453 foreach my $option (keys %options)
7454 { carp "invalid option $option\n" unless( $allowed_options{$option}); }
7455  
7456 $options{normalise_space} ||= $options{normalize_space} || 0;
7457  
7458 $options{content_key} ||= 'content';
7459 if( $options{content_key}=~ m{^-})
7460 { # need to remove the - and to activate extra folding
7461 $options{content_key}=~ s{^-}{};
7462 $options{extra_folding}= 1;
7463 }
7464 else
7465 { $options{extra_folding}= 0; }
7466  
7467 $options{forcearray} ||=0;
7468 if( isa( $options{forcearray}, 'ARRAY'))
7469 { my %forcearray_tags= map { $_ => 1 } @{$options{forcearray}};
7470 $options{forcearray_tags}= \%forcearray_tags;
7471 $options{forcearray}= 0;
7472 }
7473  
7474 $options{keyattr} ||= ['name', 'key', 'id'];
7475 if( ref $options{keyattr} eq 'ARRAY')
7476 { foreach my $keyattr (@{$options{keyattr}})
7477 { my( $prefix, $att)= ($keyattr=~ m{^([+-])?(.*)});
7478 $prefix ||= '';
7479 $options{key_for_all}->{$att}= 1;
7480 $options{remove_key_for_all}->{$att}=1 unless( $prefix eq '+');
7481 $options{prefix_key_for_all}->{$att}=1 if( $prefix eq '-');
7482 }
7483 }
7484 elsif( ref $options{keyattr} eq 'HASH')
7485 { while( my( $elt, $keyattr)= each %{$options{keyattr}})
7486 { my( $prefix, $att)= ($keyattr=~ m{^([+-])?(.*)});
7487 $prefix ||='';
7488 $options{key_for_elt}->{$elt}= $att;
7489 $options{remove_key_for_elt}->{"$elt#$att"}=1 unless( $prefix);
7490 $options{prefix_key_for_elt}->{"$elt#$att"}=1 if( $prefix eq '-');
7491 }
7492 }
7493  
7494 $options{var}||= $options{var_attr}; # for compat with XML::Simple
7495 if( $options{var}) { $options{var_values}= {}; }
7496 else { $options{var}=''; }
7497  
7498 if( $options{variables})
7499 { $options{var}||= 1;
7500 $options{var_values}= $options{variables};
7501 }
7502  
7503 if( $options{var_regexp} and !$options{var})
7504 { warn "var option not used, var_regexp option ignored\n"; }
7505 $options{var_regexp} ||= '\$\{?(\w+)\}?';
7506  
7507 $elt->_simplify( \%options);
7508  
7509 }
7510  
7511 sub _simplify
7512 { my( $elt, $options)= @_;
7513  
7514 my $data;
7515  
7516 my $gi= $XML::Twig::index2gi[$elt->{'gi'}];
7517 my @children= do { my $elt= $elt; my @children=(); my $child= $elt->{first_child}; while( $child) { push @children, $child; $child= $child->{next_sibling}; } @children; };
7518 my %atts= $options->{noattr} || !$elt->{att} ? () : %{$elt->{att}};
7519 my $nb_atts= keys %atts;
7520 my $nb_children= $elt->children_count + $nb_atts;
7521  
7522 my %nb_children;
7523 foreach (@children) { $nb_children{$_->tag}++; }
7524 foreach (keys %atts) { $nb_children{$_}++; }
7525  
7526 my $arrays; # tag => array where elements are stored
7527  
7528 # store children
7529 foreach my $child (@children)
7530 { if( $child->is_text)
7531 { # generate with a content key
7532 my $text= $elt->_text_with_vars( $options);
7533 if( $options->{normalise_space} >= 2) { $text= _normalize_space( $text); }
7534 if( $options->{force_content}
7535 || $nb_atts
7536 || (scalar @children > 1)
7537 )
7538 { $data->{$options->{content_key}}= $text; }
7539 else
7540 { $data= $text; }
7541 }
7542 else
7543 { # element with sub-elements
7544 my $child_gi= $XML::Twig::index2gi[$child->{'gi'}];
7545  
7546 my $child_data= $child->_simplify( $options);
7547  
7548 # first see if we need to simplify further the child data
7549 # simplify because of grouped tags
7550 if( my $grouped_tag= $options->{group_tags}->{$child_gi})
7551 { # check that the child data is a hash with a single field
7552 unless( (ref( $child_data) eq 'HASH')
7553 && (keys %$child_data == 1)
7554 && defined ( my $grouped_child_data= $child_data->{$grouped_tag})
7555 )
7556 { croak "error in grouped tag $child_gi"; }
7557 else
7558 { $child_data= $grouped_child_data; }
7559 }
7560 # simplify because of extra folding
7561 if( $options->{extra_folding})
7562 { if( (ref( $child_data) eq 'HASH')
7563 && (keys %$child_data == 1)
7564 && defined( my $content= $child_data->{$options->{content_key}})
7565 )
7566 { $child_data= $content; }
7567 }
7568  
7569 if( my $keyatt= $child->_key_attr( $options))
7570 { # simplify element with key
7571 my $key= $child->{'att'}->{$keyatt};
7572 if( $options->{normalise_space} >= 1) { $key= _normalize_space( $key); }
7573 $data->{$child_gi}->{$key}= $child_data;
7574 }
7575 elsif( $options->{forcearray}
7576 || $options->{forcearray_tags}->{$child_gi}
7577 || ( $nb_children{$child_gi} > 1)
7578 )
7579 { # simplify element to store in an array
7580 if( defined $child_data && $child_data ne "" )
7581 { $data->{$child_gi} ||= [];
7582 push @{$data->{$child_gi}}, $child_data;
7583 }
7584 else
7585 { $data->{$child_gi}= [{}]; }
7586 }
7587 else
7588 { # simplify element to store as a hash field
7589 $data->{$child_gi}=$child_data;
7590 $data->{$child_gi}= defined $child_data && $child_data ne "" ? $child_data : {};
7591 }
7592 }
7593 }
7594  
7595 # store atts
7596 # TODO: deal with att that already have an element by that name
7597 foreach my $att (keys %atts)
7598 { # do not store if the att is a key that needs to be removed
7599 if( $options->{remove_key_for_all}->{$att}
7600 || $options->{remove_key_for_elt}->{"$gi#$att"}
7601 )
7602 { next; }
7603  
7604 my $att_text= $options->{var} ? _replace_vars_in_text( $atts{$att}, $options) : $atts{$att} ;
7605 if( $options->{normalise_space} >= 2) { $att_text= _normalize_space( $att_text); }
7606  
7607 if( $options->{prefix_key_for_all}->{$att}
7608 || $options->{prefix_key_for_elt}->{"$gi#$att"}
7609 )
7610 { # prefix the att
7611 $data->{"-$att"}= $att_text;
7612 }
7613 else
7614 { # normal case
7615 $data->{$att}= $att_text;
7616 }
7617 }
7618  
7619 return $data;
7620 }
7621  
7622 sub _key_attr
7623 { my( $elt, $options)=@_;
7624 return if( $options->{noattr});
7625 if( $options->{key_for_all})
7626 { foreach my $att ($elt->att_names)
7627 { if( $options->{key_for_all}->{$att})
7628 { return $att; }
7629 }
7630 }
7631 elsif( $options->{key_for_elt})
7632 { if( my $key_for_elt= $options->{key_for_elt}->{$XML::Twig::index2gi[$elt->{'gi'}]} )
7633 { return $key_for_elt if( defined( $elt->{'att'}->{$key_for_elt})); }
7634 }
7635 return;
7636 }
7637  
7638 sub _text_with_vars
7639 { my( $elt, $options)= @_;
7640 my $text;
7641 if( $options->{var})
7642 { $text= _replace_vars_in_text( $elt->text, $options);
7643 $elt->_store_var( $options);
7644 }
7645 else
7646 { $text= $elt->text; }
7647 return $text;
7648 }
7649  
7650 sub _normalize_space
7651 { my $text= shift;
7652 $text=~ s{\s+}{ }sg;
7653 $text=~ s{^\s}{};
7654 $text=~ s{\s$}{};
7655 return $text;
7656 }
7657  
7658 sub att_nb
7659 { return 0 unless( my $atts= $_[0]->{att});
7660 return scalar keys %$atts;
7661 }
7662  
7663 sub has_no_atts
7664 { return 1 unless( my $atts= $_[0]->{att});
7665 return scalar keys %$atts ? 0 : 1;
7666 }
7667  
7668 sub _replace_vars_in_text
7669 { my( $text, $options)= @_;
7670  
7671 $text=~ s{($options->{var_regexp})}
7672 { if( defined( my $value= $options->{var_values}->{$2}))
7673 { $value }
7674 else
7675 { warn "unknown variable $2\n";
7676 $1
7677 }
7678 }gex;
7679 return $text;
7680 }
7681  
7682 sub _store_var
7683 { my( $elt, $options)= @_;
7684 if( defined (my $var_name= $elt->{'att'}->{$options->{var}}))
7685 { $options->{var_values}->{$var_name}= $elt->text;
7686 }
7687 }
7688  
7689 # split a text element at a given offset
7690 sub split_at
7691 { my( $elt, $offset)= @_;
7692 my $text_elt= $elt->is_text ? $elt : $elt->first_child( $TEXT) || return '';
7693 my $string= $text_elt->text;
7694 my $left_string= substr( $string, 0, $offset);
7695 my $right_string= substr( $string, $offset);
7696 $text_elt->{pcdata}= (delete $text_elt->{empty} || 1) && $left_string;
7697 my $new_elt= $elt->new( $XML::Twig::index2gi[$elt->{'gi'}], $right_string);
7698 $new_elt->paste( after => $elt);
7699 return $new_elt;
7700 }
7701  
7702 # split an element or its text descendants into several, in place
7703 # all elements (new and untouched) are returned
7704 sub split
7705 { my $elt= shift;
7706 my @text_chunks;
7707 my @result;
7708 if( $elt->is_text) { @text_chunks= ($elt); }
7709 else { @text_chunks= $elt->descendants( $TEXT); }
7710 foreach my $text_chunk (@text_chunks)
7711 { push @result, $text_chunk->_split( 1, @_); }
7712 return @result;
7713 }
7714  
7715 # split an element or its text descendants into several, in place
7716 # created elements (those which match the regexp) are returned
7717 sub mark
7718 { my $elt= shift;
7719 my @text_chunks;
7720 my @result;
7721 if( $elt->is_text) { @text_chunks= ($elt); }
7722 else { @text_chunks= $elt->descendants( $TEXT); }
7723 foreach my $text_chunk (@text_chunks)
7724 { push @result, $text_chunk->_split( 0, @_); }
7725 return @result;
7726 }
7727  
7728 # split a single text element
7729 # return_all defines what is returned: if it is true
7730 # only returns the elements created by matches in the split regexp
7731 # otherwise all elements (new and untouched) are returned
7732 {
7733  
7734 sub _split
7735 { my $elt= shift;
7736 my $return_all= shift;
7737 my $regexp= shift;
7738 my @tags;
7739  
7740 while( @_)
7741 { my $tag= shift();
7742 if( ref $_[0])
7743 { push @tags, { tag => $tag, atts => shift }; }
7744 else
7745 { push @tags, { tag => $tag }; }
7746 }
7747  
7748 unless( @tags) { @tags= { tag => $elt->{parent}->gi }; }
7749  
7750 my @result; # the returned list of elements
7751 my $text= $elt->text;
7752 my $gi= $XML::Twig::index2gi[$elt->{'gi'}];
7753  
7754 # 2 uses: if split matches then the first substring reuses $elt
7755 # once a split has occurred then the last match needs to be put in
7756 # a new element
7757 my $previous_match= 0;
7758  
7759 while( my( $pre_match, @matches)= $text=~ /^(.*?)$regexp(.*)$/gcs)
7760 { $text= pop @matches;
7761 if( $previous_match)
7762 { # match, not the first one, create a new text ($gi) element
7763 $elt= $elt->insert_new_elt( after => $gi, $pre_match);
7764 push @result, $elt if( $return_all);
7765 }
7766 else
7767 { # first match in $elt, re-use $elt for the first sub-string
7768 $elt->set_text( $pre_match);
7769 $previous_match++; # store the fact that there was a match
7770 push @result, $elt if( $return_all);
7771 }
7772  
7773 # now deal with matches captured in the regexp
7774 if( @matches)
7775 { # match, with capture
7776 my $i=0;
7777 foreach my $match (@matches)
7778 { # create new element, text is the match
7779 my $tag = _repl_match( $tags[$i]->{tag}, @matches) || '#PCDATA';
7780 my $atts = \%{$tags[$i]->{atts}} || {};
7781 my %atts= map { _repl_match( $_, @matches) => _repl_match( $atts->{$_}, @matches) } keys %$atts;
7782 $elt= $elt->insert_new_elt( after => $tag, \%atts, $match);
7783 push @result, $elt;
7784 $i= ($i + 1) % @tags;
7785 }
7786 }
7787 else
7788 { # match, no captures
7789 my $tag = $tags[0]->{tag};
7790 my $atts = \%{$tags[0]->{atts}} || {};
7791 $elt= $elt->insert_new_elt( after => $tag, $atts);
7792 push @result, $elt;
7793 }
7794 }
7795 if( $previous_match && $text)
7796 { # there was at least 1 match, and there is text left after the match
7797 $elt= $elt->insert_new_elt( after => $gi, $text);
7798 }
7799  
7800 push @result, $elt if( $return_all);
7801  
7802 return @result; # return all elements
7803 }
7804  
7805 sub _repl_match
7806 { my( $val, @matches)= @_;
7807 $val=~ s{\$(\d+)}{$matches[$1-1]}g;
7808 return $val;
7809 }
7810  
7811 }
7812  
7813 { my %replace_sub; # cache for complex expressions (expression => sub)
7814  
7815 sub subs_text
7816 { my( $elt, $regexp, $replace)= @_;
7817  
7818 my $replacement_string;
7819 my $is_string= _is_string( $replace);
7820  
7821 my @parents;
7822  
7823 foreach my $text_elt ($elt->descendants_or_self( $TEXT))
7824 {
7825 if( $is_string)
7826 { my $text= $text_elt->text;
7827 $text=~ s{$regexp}{ _replace_var( $replace, $1, $2, $3, $4, $5, $6, $7, $8, $9)}egx;
7828 $text_elt->set_text( $text);
7829 }
7830 else
7831 {
7832 my $replace_sub= ( $replace_sub{$replace} ||= _install_replace_sub( $replace));
7833 my $text= $text_elt->text;
7834 my $pos=0; # used to skip text that was previously matched
7835 my $found_hit;
7836 while( my( $pre_match_string, $match_string, @var)= ($text=~ m{(.*?)($regexp)}sg))
7837 { $found_hit=1;
7838 my $match_start = length( $pre_match_string);
7839 my $match = $match_start ? $text_elt->split_at( $match_start + $pos) : $text_elt;
7840 my $match_length = length( $match_string);
7841 my $post_match = $match->split_at( $match_length);
7842 $replace_sub->( $match, @var);
7843  
7844 # go to next
7845 $text_elt= $post_match;
7846 $text= $post_match->text;
7847  
7848 if( $found_hit) { push @parents, $text_elt->{parent} unless $parents[-1] && $parents[-1]== $text_elt->{parent}; }
7849  
7850 }
7851 }
7852 }
7853  
7854 foreach my $parent (@parents) { $parent->normalize; }
7855  
7856 return $elt;
7857 }
7858  
7859 sub _is_string
7860 { return ($_[0]=~ m{&e[ln]t}) ? 0: 1 }
7861  
7862 sub _replace_var
7863 { my( $string, @var)= @_;
7864 unshift @var, undef;
7865 $string=~ s{\$(\d)}{$var[$1]}g;
7866 return $string;
7867 }
7868  
7869 sub _install_replace_sub
7870 { my $replace_exp= shift;
7871 my @item= split m{(&e[ln]t\s*\([^)]*\))}, $replace_exp;
7872 my $sub= q{ my( $match, @var)= @_; my $new; my $last_inserted=$match;};
7873 my( $gi, $exp);
7874 foreach my $item (@item)
7875 { next if ! length $item;
7876 if( $item=~ m{^&elt\s*\(([^)]*)\)})
7877 { $exp= $1; }
7878 elsif( $item=~ m{^&ent\s*\(\s*([^\s)]*)\s*\)})
7879 { $exp= " '#ENT' => $1"; }
7880 else
7881 { $exp= qq{ '#PCDATA' => "$item"}; }
7882 $exp=~ s{\$(\d)}{my $i= $1-1; "\$var[$i]"}eg; # replace references to matches
7883 $sub.= qq{ \$new= \$match->new( $exp); };
7884 $sub .= q{ $new->paste( after => $last_inserted); $last_inserted=$new;};
7885 }
7886 $sub .= q{ $match->delete; };
7887 #$sub=~ s/;/;\n/g; warn "subs: $sub";
7888 my $coderef= eval "sub { $NO_WARNINGS; $sub }"; ## no critic (ProhibitStringyEval)
7889 if( $@) {
7890 my $msg = "invalid replacement expression $replace_exp: " . $@;
7891 if( $@ =~ m{Global symbol .* requires explicit package name} )
7892 { my $suggested = $replace_exp;
7893 $suggested =~ s{\$(\d)}{\\\$$1}g;
7894 $msg = join "\n ",
7895 $msg,
7896 "the problem seems to be that the replacement expression is a single-quoted string".
7897 "that includes variables. Those need to be expanded.",
7898 "ie replace the following:",
7899 " '$replace_exp'",
7900 "with:",
7901 " qq{$suggested}",
7902 ;
7903 }
7904 croak $msg;
7905 }
7906 return $coderef;
7907 }
7908  
7909 }
7910  
7911 sub merge_text
7912 { my( $e1, $e2)= @_;
7913 croak "invalid merge: can only merge 2 elements"
7914 unless( isa( $e2, 'XML::Twig::Elt'));
7915 croak "invalid merge: can only merge 2 text elements"
7916 unless( $e1->is_text && $e2->is_text && ($e1->gi eq $e2->gi));
7917  
7918 my $t1_length= length( $e1->text);
7919  
7920 $e1->set_text( $e1->text . $e2->text);
7921  
7922 if( my $extra_data_in_pcdata= $e2->_extra_data_in_pcdata)
7923 { foreach my $data (@$extra_data_in_pcdata) { $e1->_push_extra_data_in_pcdata( $data->{text}, $data->{offset} + $t1_length); } }
7924  
7925 $e2->delete;
7926  
7927 return $e1;
7928 }
7929  
7930 sub merge
7931 { my( $e1, $e2)= @_;
7932 my @e2_children= $e2->_children;
7933 if( $e1->_last_child && $e1->_last_child->is_pcdata
7934 && @e2_children && $e2_children[0]->is_pcdata
7935 )
7936 { my $t1_length= length( $e1->_last_child->{pcdata});
7937 my $child1= $e1->_last_child;
7938 my $child2= shift @e2_children;
7939 $child1->{pcdata} .= $child2->{pcdata};
7940  
7941 my $extra_data= $e1->_extra_data_before_end_tag . $e2->extra_data;
7942  
7943 if( $extra_data)
7944 { $e1->_del_extra_data_before_end_tag;
7945 $child1->_push_extra_data_in_pcdata( $extra_data, $t1_length);
7946 }
7947  
7948 if( my $extra_data_in_pcdata= $child2->_extra_data_in_pcdata)
7949 { foreach my $data (@$extra_data_in_pcdata) { $child1->_push_extra_data_in_pcdata( $data->{text}, $data->{offset} + $t1_length); } }
7950  
7951 if( my $extra_data_before_end_tag= $e2->_extra_data_before_end_tag)
7952 { $e1->_set_extra_data_before_end_tag( $extra_data_before_end_tag); }
7953 }
7954  
7955 foreach my $e (@e2_children) { $e->move( last_child => $e1); }
7956  
7957 $e2->delete;
7958 return $e1;
7959 }
7960  
7961 # recursively copy an element and returns the copy (can be huge and long)
7962 sub copy
7963 { my $elt= shift;
7964 my $copy= $elt->new( $XML::Twig::index2gi[$elt->{'gi'}]);
7965  
7966 if( $elt->extra_data) { $copy->set_extra_data( $elt->extra_data); }
7967 if( $elt->{extra_data_before_end_tag}) { $copy->_set_extra_data_before_end_tag( $elt->{extra_data_before_end_tag}); }
7968  
7969 if( $elt->is_asis) { $copy->set_asis; }
7970  
7971 if( (exists $elt->{'pcdata'}))
7972 { $copy->{pcdata}= (delete $copy->{empty} || 1) && $elt->{pcdata};
7973 if( $elt->{extra_data_in_pcdata}) { $copy->_set_extra_data_in_pcdata( $elt->{extra_data_in_pcdata}); }
7974 }
7975 elsif( (exists $elt->{'cdata'}))
7976 { $copy->{cdata}= $elt->{cdata};
7977 if( $elt->{extra_data_in_pcdata}) { $copy->_set_extra_data_in_pcdata( $elt->{extra_data_in_pcdata}); }
7978 }
7979 elsif( (exists $elt->{'target'}))
7980 { $copy->_set_pi( $elt->{target}, $elt->{data}); }
7981 elsif( (exists $elt->{'comment'}))
7982 { $copy->{comment}= $elt->{comment}; }
7983 elsif( (exists $elt->{'ent'}))
7984 { $copy->{ent}= $elt->{ent}; }
7985 else
7986 { my @children= do { my $elt= $elt; my @children=(); my $child= $elt->{first_child}; while( $child) { push @children, $child; $child= $child->{next_sibling}; } @children; };
7987 if( my $atts= $elt->{att})
7988 { my %atts;
7989 tie %atts, 'Tie::IxHash' if (keep_atts_order());
7990 %atts= %{$atts}; # we want to do a real copy of the attributes
7991 $copy->set_atts( \%atts);
7992 }
7993 foreach my $child (@children)
7994 { my $child_copy= $child->copy;
7995 $child_copy->paste( 'last_child', $copy);
7996 }
7997 }
7998 # save links to the original location, which can be convenient and is used for namespace resolution
7999 foreach my $link ( qw(parent prev_sibling next_sibling) )
8000 { $copy->{former}->{$link}= $elt->{$link};
8001 weaken( $copy->{former}->{$link});
8002 }
8003  
8004 $copy->{empty}= $elt->{'empty'};
8005  
8006 return $copy;
8007 }
8008  
8009 sub delete
8010 { my $elt= shift;
8011 $elt->cut;
8012 return undef;
8013 }
8014  
8015 # ignores the element
8016 sub ignore
8017 { my $elt= shift;
8018 my $t= $elt->twig;
8019 $t->ignore( $elt, @_);
8020 }
8021  
8022 BEGIN {
8023 my $pretty = 0;
8024 my $quote = '"';
8025 my $INDENT = ' ';
8026 my $empty_tag_style = 0;
8027 my $remove_cdata = 0;
8028 my $keep_encoding = 0;
8029 my $expand_external_entities = 0;
8030 my $keep_atts_order = 0;
8031 my $do_not_escape_amp_in_atts = 0;
8032 my $WRAP = '80';
8033 my $REPLACED_ENTS = qq{&<};
8034  
8035 my ($NSGMLS, $NICE, $INDENTED, $INDENTEDCT, $INDENTEDC, $WRAPPED, $RECORD1, $RECORD2, $INDENTEDA)= (1..9);
8036 my %KEEP_TEXT_TAG_ON_ONE_LINE= map { $_ => 1 } ( $INDENTED, $INDENTEDCT, $INDENTEDC, $INDENTEDA, $WRAPPED);
8037 my %WRAPPED = map { $_ => 1 } ( $WRAPPED, $INDENTEDA, $INDENTEDC);
8038  
8039 my %pretty_print_style=
8040 ( none => 0, # no added \n
8041 nsgmls => $NSGMLS, # nsgmls-style, \n in tags
8042 # below this line styles are UNSAFE (the generated XML can be well-formed but invalid)
8043 nice => $NICE, # \n after open/close tags except when the
8044 # element starts with text
8045 indented => $INDENTED, # nice plus indented
8046 indented_close_tag => $INDENTEDCT, # nice plus indented
8047 indented_c => $INDENTEDC, # slightly more compact than indented (closing
8048 # tags are on the same line)
8049 wrapped => $WRAPPED, # text is wrapped at column
8050 record_c => $RECORD1, # for record-like data (compact)
8051 record => $RECORD2, # for record-like data (not so compact)
8052 indented_a => $INDENTEDA, # nice, indented, and with attributes on separate
8053 # lines as the nsgmls style, as well as wrapped
8054 # lines - to make the xml friendly to line-oriented tools
8055 cvs => $INDENTEDA, # alias for indented_a
8056 );
8057  
8058 my ($HTML, $EXPAND)= (1..2);
8059 my %empty_tag_style=
8060 ( normal => 0, # <tag/>
8061 html => $HTML, # <tag />
8062 xhtml => $HTML, # <tag />
8063 expand => $EXPAND, # <tag></tag>
8064 );
8065  
8066 my %quote_style=
8067 ( double => '"',
8068 single => "'",
8069 # smart => "smart",
8070 );
8071  
8072 my $xml_space_preserve; # set when an element includes xml:space="preserve"
8073  
8074 my $output_filter; # filters the entire output (including < and >)
8075 my $output_text_filter; # filters only the text part (tag names, attributes, pcdata)
8076  
8077 my $replaced_ents= $REPLACED_ENTS;
8078  
8079 # returns those pesky "global" variables so you can switch between twigs
8080 sub global_state ## no critic (Subroutines::ProhibitNestedSubs);
8081 { return
8082 { pretty => $pretty,
8083 quote => $quote,
8084 indent => $INDENT,
8085 empty_tag_style => $empty_tag_style,
8086 remove_cdata => $remove_cdata,
8087 keep_encoding => $keep_encoding,
8088 expand_external_entities => $expand_external_entities,
8089 output_filter => $output_filter,
8090 output_text_filter => $output_text_filter,
8091 keep_atts_order => $keep_atts_order,
8092 do_not_escape_amp_in_atts => $do_not_escape_amp_in_atts,
8093 wrap => $WRAP,
8094 replaced_ents => $replaced_ents,
8095 };
8096 }
8097  
8098 # restores the global variables
8099 sub set_global_state
8100 { my $state= shift;
8101 $pretty = $state->{pretty};
8102 $quote = $state->{quote};
8103 $INDENT = $state->{indent};
8104 $empty_tag_style = $state->{empty_tag_style};
8105 $remove_cdata = $state->{remove_cdata};
8106 $keep_encoding = $state->{keep_encoding};
8107 $expand_external_entities = $state->{expand_external_entities};
8108 $output_filter = $state->{output_filter};
8109 $output_text_filter = $state->{output_text_filter};
8110 $keep_atts_order = $state->{keep_atts_order};
8111 $do_not_escape_amp_in_atts = $state->{do_not_escape_amp_in_atts};
8112 $WRAP = $state->{wrap};
8113 $replaced_ents = $state->{replaced_ents},
8114 }
8115  
8116 # sets global state to defaults
8117 sub init_global_state
8118 { set_global_state(
8119 { pretty => 0,
8120 quote => '"',
8121 indent => $INDENT,
8122 empty_tag_style => 0,
8123 remove_cdata => 0,
8124 keep_encoding => 0,
8125 expand_external_entities => 0,
8126 output_filter => undef,
8127 output_text_filter => undef,
8128 keep_atts_order => undef,
8129 do_not_escape_amp_in_atts => 0,
8130 wrap => $WRAP,
8131 replaced_ents => $REPLACED_ENTS,
8132 });
8133 }
8134  
8135 # set the pretty_print style (in $pretty) and returns the old one
8136 # can be called from outside the package with 2 arguments (elt, style)
8137 # or from inside with only one argument (style)
8138 # the style can be either a string (one of the keys of %pretty_print_style
8139 # or a number (presumably an old value saved)
8140 sub set_pretty_print
8141 { my $style= lc( defined $_[1] ? $_[1] : $_[0]); # so we cover both cases
8142 my $old_pretty= $pretty;
8143 if( $style=~ /^\d+$/)
8144 { croak "invalid pretty print style $style" unless( $style < keys %pretty_print_style);
8145 $pretty= $style;
8146 }
8147 else
8148 { croak "invalid pretty print style '$style'" unless( exists $pretty_print_style{$style});
8149 $pretty= $pretty_print_style{$style};
8150 }
8151 if( $WRAPPED{$pretty} )
8152 { XML::Twig::_use( 'Text::Wrap') or croak( "Text::Wrap not available, cannot use style $style"); }
8153 return $old_pretty;
8154 }
8155  
8156 sub _pretty_print { return $pretty; }
8157  
8158 # set the empty tag style (in $empty_tag_style) and returns the old one
8159 # can be called from outside the package with 2 arguments (elt, style)
8160 # or from inside with only one argument (style)
8161 # the style can be either a string (one of the keys of %empty_tag_style
8162 # or a number (presumably an old value saved)
8163 sub set_empty_tag_style
8164 { my $style= lc( defined $_[1] ? $_[1] : $_[0]); # so we cover both cases
8165 my $old_style= $empty_tag_style;
8166 if( $style=~ /^\d+$/)
8167 { croak "invalid empty tag style $style"
8168 unless( $style < keys %empty_tag_style);
8169 $empty_tag_style= $style;
8170 }
8171 else
8172 { croak "invalid empty tag style '$style'"
8173 unless( exists $empty_tag_style{$style});
8174 $empty_tag_style= $empty_tag_style{$style};
8175 }
8176 return $old_style;
8177 }
8178  
8179 sub _pretty_print_styles
8180 { return (sort { $pretty_print_style{$a} <=> $pretty_print_style{$b} || $a cmp $b } keys %pretty_print_style); }
8181  
8182 sub set_quote
8183 { my $style= $_[1] || $_[0];
8184 my $old_quote= $quote;
8185 croak "invalid quote '$style'" unless( exists $quote_style{$style});
8186 $quote= $quote_style{$style};
8187 return $old_quote;
8188 }
8189  
8190 sub set_remove_cdata
8191 { my $new_value= defined $_[1] ? $_[1] : $_[0];
8192 my $old_value= $remove_cdata;
8193 $remove_cdata= $new_value;
8194 return $old_value;
8195 }
8196  
8197 sub set_indent
8198 { my $new_value= defined $_[1] ? $_[1] : $_[0];
8199 my $old_value= $INDENT;
8200 $INDENT= $new_value;
8201 return $old_value;
8202 }
8203  
8204 sub set_wrap
8205 { my $new_value= defined $_[1] ? $_[1] : $_[0];
8206 my $old_value= $WRAP;
8207 $WRAP= $new_value;
8208 return $old_value;
8209 }
8210  
8211 sub set_keep_encoding
8212 { my $new_value= defined $_[1] ? $_[1] : $_[0];
8213 my $old_value= $keep_encoding;
8214 $keep_encoding= $new_value;
8215 return $old_value;
8216 }
8217  
8218 sub set_replaced_ents
8219 { my $new_value= defined $_[1] ? $_[1] : $_[0];
8220 my $old_value= $replaced_ents;
8221 $replaced_ents= $new_value;
8222 return $old_value;
8223 }
8224  
8225 sub do_not_escape_gt
8226 { my $old_value= $replaced_ents;
8227 $replaced_ents= q{&<}; # & needs to be first
8228 return $old_value;
8229 }
8230  
8231 sub escape_gt
8232 { my $old_value= $replaced_ents;
8233 $replaced_ents= qq{&<>}; # & needs to be first
8234 return $old_value;
8235 }
8236  
8237 sub _keep_encoding { return $keep_encoding; } # so I can use elsewhere in the module
8238  
8239 sub set_do_not_escape_amp_in_atts
8240 { my $new_value= defined $_[1] ? $_[1] : $_[0];
8241 my $old_value= $do_not_escape_amp_in_atts;
8242 $do_not_escape_amp_in_atts= $new_value;
8243 return $old_value;
8244 }
8245  
8246 sub output_filter { return $output_filter; }
8247 sub output_text_filter { return $output_text_filter; }
8248  
8249 sub set_output_filter
8250 { my $new_value= defined $_[1] ? $_[1] : $_[0]; # can be called in object/non-object mode
8251 # if called in object mode with no argument, the filter is undefined
8252 if( isa( $new_value, 'XML::Twig::Elt') || isa( $new_value, 'XML::Twig')) { undef $new_value; }
8253 my $old_value= $output_filter;
8254 if( !$new_value || isa( $new_value, 'CODE') )
8255 { $output_filter= $new_value; }
8256 elsif( $new_value eq 'latin1')
8257 { $output_filter= XML::Twig::latin1();
8258 }
8259 elsif( $XML::Twig::filter{$new_value})
8260 { $output_filter= $XML::Twig::filter{$new_value}; }
8261 else
8262 { croak "invalid output filter '$new_value'"; }
8263  
8264 return $old_value;
8265 }
8266  
8267 sub set_output_text_filter
8268 { my $new_value= defined $_[1] ? $_[1] : $_[0]; # can be called in object/non-object mode
8269 # if called in object mode with no argument, the filter is undefined
8270 if( isa( $new_value, 'XML::Twig::Elt') || isa( $new_value, 'XML::Twig')) { undef $new_value; }
8271 my $old_value= $output_text_filter;
8272 if( !$new_value || isa( $new_value, 'CODE') )
8273 { $output_text_filter= $new_value; }
8274 elsif( $new_value eq 'latin1')
8275 { $output_text_filter= XML::Twig::latin1();
8276 }
8277 elsif( $XML::Twig::filter{$new_value})
8278 { $output_text_filter= $XML::Twig::filter{$new_value}; }
8279 else
8280 { croak "invalid output text filter '$new_value'"; }
8281  
8282 return $old_value;
8283 }
8284  
8285 sub set_expand_external_entities
8286 { my $new_value= defined $_[1] ? $_[1] : $_[0];
8287 my $old_value= $expand_external_entities;
8288 $expand_external_entities= $new_value;
8289 return $old_value;
8290 }
8291  
8292 sub set_keep_atts_order
8293 { my $new_value= defined $_[1] ? $_[1] : $_[0];
8294 my $old_value= $keep_atts_order;
8295 $keep_atts_order= $new_value;
8296 return $old_value;
8297  
8298 }
8299  
8300 sub keep_atts_order { return $keep_atts_order; } # so I can use elsewhere in the module
8301  
8302 my %html_empty_elt;
8303 BEGIN { %html_empty_elt= map { $_ => 1} qw( base meta link hr br param img area input col); }
8304  
8305 sub start_tag
8306 { my( $elt, $option)= @_;
8307  
8308 return if( $elt->{gi} < $XML::Twig::SPECIAL_GI);
8309  
8310 my $extra_data= $elt->{extra_data} || '';
8311  
8312 my $gi= $XML::Twig::index2gi[$elt->{'gi'}];
8313 my $att= $elt->{att}; # should be $elt->{att}, optimized into a pure hash look-up
8314  
8315 my $ns_map= $att ? $att->{'#original_gi'} : '';
8316 if( $ns_map) { $gi= _restore_original_prefix( $ns_map, $gi); }
8317 $gi=~ s{^#default:}{}; # remove default prefix
8318  
8319 if( $output_text_filter) { $gi= $output_text_filter->( $gi); }
8320  
8321 # get the attribute and their values
8322 my $att_sep = $pretty==$NSGMLS ? "\n"
8323 : $pretty==$INDENTEDA ? "\n" . $INDENT x ($elt->level+1) . ' '
8324 : ' '
8325 ;
8326  
8327 my $replace_in_att_value= $replaced_ents . "$quote\t\r\n";
8328 if( $option->{escape_gt} && $replaced_ents !~ m{>}) { $replace_in_att_value.= '>'; }
8329  
8330 my $tag;
8331 my @att_names= grep { !( $_=~ m{^#(?!default:)} ) } $keep_atts_order ? keys %{$att} : sort keys %{$att};
8332 if( @att_names)
8333 { my $atts= join $att_sep, map { my $output_att_name= $ns_map ? _restore_original_prefix( $ns_map, $_) : $_;
8334 if( $output_text_filter)
8335 { $output_att_name= $output_text_filter->( $output_att_name); }
8336 $output_att_name . '=' . $quote . _att_xml_string( $att->{$_}, $replace_in_att_value) . $quote
8337  
8338 }
8339 @att_names
8340 ;
8341 if( $pretty==$INDENTEDA && @att_names == 1) { $att_sep= ' '; }
8342 $tag= "<$gi$att_sep$atts";
8343 }
8344 else
8345 { $tag= "<$gi"; }
8346  
8347 $tag .= "\n" if($pretty==$NSGMLS);
8348  
8349 # force empty if suitable HTML tag, otherwise use the value from the input tree
8350 if( ($empty_tag_style eq $HTML) && !$elt->{first_child} && !$elt->{extra_data_before_end_tag} && $html_empty_elt{$gi})
8351 { $elt->{empty}= 1; }
8352 my $empty= defined $elt->{empty} ? $elt->{empty}
8353 : $elt->{first_child} ? 0
8354 : 1;
8355  
8356 $tag .= (!$elt->{empty} || $elt->{extra_data_before_end_tag}) ? '>' # element has content
8357 : (($empty_tag_style eq $HTML) && $html_empty_elt{$gi}) ? ' />' # html empty element
8358 # cvs-friendly format
8359 : ( $pretty == $INDENTEDA && @att_names > 1) ? "\n" . $INDENT x $elt->level . "/>"
8360 : ( $pretty == $INDENTEDA && @att_names == 1) ? " />"
8361 : $empty_tag_style ? "></" . $XML::Twig::index2gi[$elt->{'gi'}] . ">" # $empty_tag_style is $HTML or $EXPAND
8362 : '/>'
8363 ;
8364  
8365 if( ( (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 1) eq '#') && (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 9) ne '#default:') )) { $tag= ''; }
8366  
8367 #warn "TRACE: ", $tag,": ", Encode::is_utf8( $tag) ? "has flag" : "FLAG NOT SET";
8368  
8369 unless( $pretty) { return defined( $extra_data) ? $extra_data . $tag : $tag; }
8370  
8371 my $prefix='';
8372 my $return=''; # '' or \n is to be printed before the tag
8373 my $indent=0; # number of indents before the tag
8374  
8375 if( $pretty==$RECORD1)
8376 { my $level= $elt->level;
8377 $return= "\n" if( $level < 2);
8378 $indent= 1 if( $level == 1);
8379 }
8380  
8381 elsif( $pretty==$RECORD2)
8382 { $return= "\n";
8383 $indent= $elt->level;
8384 }
8385  
8386 elsif( $pretty==$NICE)
8387 { my $parent= $elt->{parent};
8388 unless( !$parent || $parent->{contains_text})
8389 { $return= "\n"; }
8390 $elt->{contains_text}= 1 if( ($parent && $parent->{contains_text})
8391 || $elt->contains_text);
8392 }
8393  
8394 elsif( $KEEP_TEXT_TAG_ON_ONE_LINE{$pretty})
8395 { my $parent= $elt->{parent};
8396 unless( !$parent || $parent->{contains_text})
8397 { $return= "\n";
8398 $indent= $elt->level;
8399 }
8400 $elt->{contains_text}= 1 if( ($parent && $parent->{contains_text})
8401 || $elt->contains_text);
8402 }
8403  
8404 if( $return || $indent)
8405 { # check for elements in which spaces should be kept
8406 my $t= $elt->twig;
8407 return $extra_data . $tag if( $xml_space_preserve);
8408 if( $t && $t->{twig_keep_spaces_in})
8409 { foreach my $ancestor ($elt->ancestors)
8410 { return $extra_data . $tag if( $t->{twig_keep_spaces_in}->{$XML::Twig::index2gi[$ancestor->{'gi'}]}) }
8411 }
8412  
8413 $prefix= $INDENT x $indent;
8414 if( $extra_data)
8415 { $extra_data=~ s{\s+$}{};
8416 $extra_data=~ s{^\s+}{};
8417 $extra_data= $prefix . $extra_data . $return;
8418 }
8419 }
8420  
8421 return $return . $extra_data . $prefix . $tag;
8422 }
8423  
8424 sub end_tag
8425 { my $elt= shift;
8426 return '' if( ($elt->{gi}<$XML::Twig::SPECIAL_GI)
8427 || ($elt->{'empty'} && !$elt->{extra_data_before_end_tag})
8428 );
8429 my $tag= "<";
8430 my $gi= $XML::Twig::index2gi[$elt->{'gi'}];
8431  
8432 if( my $map= $elt->{'att'}->{'#original_gi'}) { $gi= _restore_original_prefix( $map, $gi); }
8433 $gi=~ s{^#default:}{}; # remove default prefix
8434  
8435 if( $output_text_filter) { $gi= $output_text_filter->( $XML::Twig::index2gi[$elt->{'gi'}]); }
8436 $tag .= "/$gi>";
8437  
8438 $tag = ($elt->{extra_data_before_end_tag} || '') . $tag;
8439  
8440 if( ( (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 1) eq '#') && (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 9) ne '#default:') )) { $tag= ''; }
8441  
8442 return $tag unless $pretty;
8443  
8444 my $prefix='';
8445 my $return=0; # 1 if a \n is to be printed before the tag
8446 my $indent=0; # number of indents before the tag
8447  
8448 if( $pretty==$RECORD1)
8449 { $return= 1 if( $elt->level == 0);
8450 }
8451  
8452 elsif( $pretty==$RECORD2)
8453 { unless( $elt->contains_text)
8454 { $return= 1 ;
8455 $indent= $elt->level;
8456 }
8457 }
8458  
8459 elsif( $pretty==$NICE)
8460 { my $parent= $elt->{parent};
8461 if( ( ($parent && !$parent->{contains_text}) || !$parent )
8462 && ( !$elt->{contains_text}
8463 && ($elt->{has_flushed_child} || $elt->{first_child})
8464 )
8465 )
8466 { $return= 1; }
8467 }
8468  
8469 elsif( $KEEP_TEXT_TAG_ON_ONE_LINE{$pretty})
8470 { my $parent= $elt->{parent};
8471 if( ( ($parent && !$parent->{contains_text}) || !$parent )
8472 && ( !$elt->{contains_text}
8473 && ($elt->{has_flushed_child} || $elt->{first_child})
8474 )
8475 )
8476 { $return= 1;
8477 $indent= $elt->level;
8478 }
8479 }
8480  
8481 if( $return || $indent)
8482 { # check for elements in which spaces should be kept
8483 my $t= $elt->twig;
8484 return $tag if( $xml_space_preserve);
8485 if( $t && $t->{twig_keep_spaces_in})
8486 { foreach my $ancestor ($elt, $elt->ancestors)
8487 { return $tag if( $t->{twig_keep_spaces_in}->{$XML::Twig::index2gi[$ancestor->{'gi'}]}) }
8488 }
8489  
8490 if( $return) { $prefix= ($pretty== $INDENTEDCT) ? "\n$INDENT" : "\n"; }
8491 $prefix.= $INDENT x $indent;
8492 }
8493  
8494 # add a \n at the end of the document (after the root element)
8495 $tag .= "\n" unless( $elt->{parent});
8496  
8497 return $prefix . $tag;
8498 }
8499  
8500 sub _restore_original_prefix
8501 { my( $map, $name)= @_;
8502 my $prefix= _ns_prefix( $name);
8503 if( my $original_prefix= $map->{$prefix})
8504 { if( $original_prefix eq '#default')
8505 { $name=~ s{^$prefix:}{}; }
8506 else
8507 { $name=~ s{^$prefix(?=:)}{$original_prefix}; }
8508 }
8509 return $name;
8510 }
8511  
8512 # buffer used to hold the text to print/sprint, to avoid passing it back and forth between methods
8513 my @sprint;
8514  
8515 # $elt is an element to print
8516 # $fh is an optional filehandle to print to
8517 # $pretty is an optional value, if true a \n is printed after the < of the
8518 # opening tag
8519 sub print
8520 { my $elt= shift;
8521  
8522 my $fh= isa( $_[0], 'GLOB') || isa( $_[0], 'IO::Scalar') ? shift : undef;
8523 my $old_select= defined $fh ? select $fh : undef;
8524 print $elt->sprint( @_);
8525 select $old_select if( defined $old_select);
8526 }
8527  
8528 # those next 2 methods need to be refactored, they are copies of the same methods in XML::Twig
8529 sub print_to_file
8530 { my( $elt, $filename)= (shift, shift);
8531 my $out_fh;
8532 my $mode= $keep_encoding ? '>' : '>:utf8';
8533 open( $out_fh, $mode, $filename) or _croak( "cannot create file $filename: $!");
8534 $elt->print( $out_fh, @_);
8535 close $out_fh;
8536 return $elt;
8537 }
8538  
8539 # probably only works on *nix (at least the chmod bit)
8540 # first print to a temporary file, then rename that file to the desired file name, then change permissions
8541 # to the original file permissions (or to the current umask)
8542 sub safe_print_to_file
8543 { my( $elt, $filename)= (shift, shift);
8544 my $perm= -f $filename ? (stat $filename)[2] & 07777 : ~umask() ; ## no critic (ProhibitLeadingZeros)
8545 XML::Twig::_use( 'File::Temp') || croak "need File::Temp to use safe_print_to_file\n";
8546 XML::Twig::_use( 'File::Basename') || croak "need File::Basename to use safe_print_to_file\n";
8547 my $tmpdir= File::Basename::dirname( $filename);
8548 my( $fh, $tmpfilename) = File::Temp::tempfile( DIR => $tmpdir);
8549 $elt->print_to_file( $tmpfilename, @_);
8550 rename( $tmpfilename, $filename) or unlink $tmpfilename && _croak( "cannot move temporary file to $filename: $!");
8551 chmod $perm, $filename;
8552 return $elt;
8553 }
8554  
8555 # same as print but does not output the start tag if the element
8556 # is marked as flushed
8557 sub flush
8558 { my $elt= shift;
8559 my $up_to= $_[0] && isa( $_[0], 'XML::Twig::Elt') ? shift : $elt;
8560 $elt->twig->flush_up_to( $up_to, @_);
8561 }
8562 sub purge
8563 { my $elt= shift;
8564 my $up_to= $_[0] && isa( $_[0], 'XML::Twig::Elt') ? shift : $elt;
8565 $elt->twig->purge_up_to( $up_to, @_);
8566 }
8567  
8568 sub _flush
8569 { my $elt= shift;
8570  
8571 my $pretty;
8572 my $fh= isa( $_[0], 'GLOB') || isa( $_[0], 'IO::Scalar') ? shift : undef;
8573 my $old_select= defined $fh ? select $fh : undef;
8574 my $old_pretty= defined ($pretty= shift) ? set_pretty_print( $pretty) : undef;
8575  
8576 $xml_space_preserve= 1 if( ($elt->inherit_att( 'xml:space') || '') eq 'preserve');
8577  
8578 $elt->__flush();
8579  
8580 $xml_space_preserve= 0;
8581  
8582 select $old_select if( defined $old_select);
8583 set_pretty_print( $old_pretty) if( defined $old_pretty);
8584 }
8585  
8586 sub __flush
8587 { my $elt= shift;
8588  
8589 if( $elt->{gi} >= $XML::Twig::SPECIAL_GI)
8590 { my $preserve= ($elt->{'att'}->{'xml:space'} || '') eq 'preserve';
8591 $xml_space_preserve++ if $preserve;
8592 unless( $elt->{'flushed'})
8593 { print $elt->start_tag();
8594 }
8595  
8596 # flush the children
8597 my @children= do { my $elt= $elt; my @children=(); my $child= $elt->{first_child}; while( $child) { push @children, $child; $child= $child->{next_sibling}; } @children; };
8598 foreach my $child (@children)
8599 { $child->_flush( $pretty);
8600 $child->{'flushed'}=1;
8601 }
8602 if( ! $elt->{end_tag_flushed})
8603 { print $elt->end_tag;
8604 $elt->{end_tag_flushed}=1;
8605 $elt->{'flushed'}=1;
8606 }
8607 $xml_space_preserve-- if $preserve;
8608 # used for pretty printing
8609 if( my $parent= $elt->{parent}) { $parent->{has_flushed_child}= 1; }
8610 }
8611 else # text or special element
8612 { my $text;
8613 if( (exists $elt->{'pcdata'})) { $text= $elt->pcdata_xml_string;
8614 if( my $parent= $elt->{parent})
8615 { $parent->{contains_text}= 1; }
8616 }
8617 elsif( (exists $elt->{'cdata'})) { $text= $elt->cdata_string;
8618 if( my $parent= $elt->{parent})
8619 { $parent->{contains_text}= 1; }
8620 }
8621 elsif( (exists $elt->{'target'})) { $text= $elt->pi_string; }
8622 elsif( (exists $elt->{'comment'})) { $text= $elt->comment_string; }
8623 elsif( (exists $elt->{'ent'})) { $text= $elt->ent_string; }
8624  
8625 print $output_filter ? $output_filter->( $text) : $text;
8626 }
8627 }
8628  
8629 sub xml_text
8630 { my( $elt, @options)= @_;
8631  
8632 if( @options && grep { lc( $_) eq 'no_recurse' } @options) { return $elt->xml_text_only; }
8633  
8634 my $string='';
8635  
8636 if( ($elt->{gi} >= $XML::Twig::SPECIAL_GI) )
8637 { # sprint the children
8638 my $child= $elt->{first_child} || '';
8639 while( $child)
8640 { $string.= $child->xml_text;
8641 } continue { $child= $child->{next_sibling}; }
8642 }
8643 elsif( (exists $elt->{'pcdata'})) { $string .= $output_filter ? $output_filter->($elt->pcdata_xml_string)
8644 : $elt->pcdata_xml_string;
8645 }
8646 elsif( (exists $elt->{'cdata'})) { $string .= $output_filter ? $output_filter->($elt->cdata_string)
8647 : $elt->cdata_string;
8648 }
8649 elsif( (exists $elt->{'ent'})) { $string .= $elt->ent_string; }
8650  
8651 return $string;
8652 }
8653  
8654 sub xml_text_only
8655 { return join '', map { $_->is_text || (exists $_->{'ent'}) ? $_->xml_text : '' } $_[0]->_children; }
8656  
8657 # same as print but except... it does not print but rather returns the string
8658 # if the second parameter is set then only the content is returned, not the
8659 # start and end tags of the element (but the tags of the included elements are
8660 # returned)
8661  
8662 sub sprint
8663 { my $elt= shift;
8664 my( $old_pretty, $old_empty_tag_style);
8665  
8666 if( $_[0])
8667 { if( isa( $_[0], 'HASH'))
8668 { # "proper way, using a hashref for options
8669 my %args= XML::Twig::_normalize_args( %{shift()});
8670 if( defined $args{PrettyPrint}) { $old_pretty = set_pretty_print( $args{PrettyPrint}); }
8671 if( defined $args{EmptyTags}) { $old_empty_tag_style = set_empty_tag_style( $args{EmptyTags}); }
8672 }
8673 else
8674 { # "old" way, just using the option name
8675 my @other_opt;
8676 foreach my $opt (@_)
8677 { if( exists $pretty_print_style{$opt}) { $old_pretty = set_pretty_print( $opt); }
8678 elsif( exists $empty_tag_style{$opt}) { $old_empty_tag_style = set_empty_tag_style( $opt); }
8679 else { push @other_opt, $opt; }
8680 }
8681 @_= @other_opt;
8682 }
8683 }
8684  
8685 $xml_space_preserve= 1 if( ($elt->inherit_att( 'xml:space') || '') eq 'preserve');
8686  
8687 @sprint=();
8688 $elt->_sprint( @_);
8689 my $sprint= join( '', @sprint);
8690 if( $output_filter) { $sprint= $output_filter->( $sprint); }
8691  
8692 if( ( ($pretty== $WRAPPED) || ($pretty==$INDENTEDC)) && !$xml_space_preserve)
8693 { $sprint= _wrap_text( $sprint); }
8694 $xml_space_preserve= 0;
8695  
8696 if( defined $old_pretty) { set_pretty_print( $old_pretty); }
8697 if( defined $old_empty_tag_style) { set_empty_tag_style( $old_empty_tag_style); }
8698  
8699 return $sprint;
8700 }
8701  
8702 sub _wrap_text
8703 { my( $string)= @_;
8704 my $wrapped;
8705 foreach my $line (split /\n/, $string)
8706 { my( $initial_indent)= $line=~ m{^(\s*)};
8707 my $wrapped_line= Text::Wrap::wrap( '', $initial_indent . $INDENT, $line) . "\n";
8708  
8709 # fix glitch with Text::wrap when the first line is long and does not include spaces
8710 # the first line ends up being too short by 2 chars, but we'll have to live with it!
8711 $wrapped_line=~ s{^ +\n }{}s; # this prefix needs to be removed
8712  
8713 $wrapped .= $wrapped_line;
8714 }
8715 return $wrapped;
8716 }
8717  
8718 sub _sprint
8719 { my $elt= shift;
8720 my $no_tag= shift || 0;
8721 # in case there's some comments or PI's piggybacking
8722  
8723 if( $elt->{gi} >= $XML::Twig::SPECIAL_GI)
8724 {
8725 my $preserve= ($elt->{'att'}->{'xml:space'} || '') eq 'preserve';
8726 $xml_space_preserve++ if $preserve;
8727  
8728 push @sprint, $elt->start_tag unless( $no_tag);
8729  
8730 # sprint the children
8731 my $child= $elt->{first_child};
8732 while( $child)
8733 { $child->_sprint;
8734 $child= $child->{next_sibling};
8735 }
8736 push @sprint, $elt->end_tag unless( $no_tag);
8737 $xml_space_preserve-- if $preserve;
8738 }
8739 else
8740 { push @sprint, $elt->{extra_data} if( $elt->{extra_data}) ;
8741 if( (exists $elt->{'pcdata'})) { push @sprint, $elt->pcdata_xml_string; }
8742 elsif( (exists $elt->{'cdata'})) { push @sprint, $elt->cdata_string; }
8743 elsif( (exists $elt->{'target'})) { if( ($pretty >= $INDENTED) && !$elt->{parent}->{contains_text}) { push @sprint, "\n" . $INDENT x $elt->level; }
8744 push @sprint, $elt->pi_string;
8745 }
8746 elsif( (exists $elt->{'comment'})) { if( ($pretty >= $INDENTED) && !$elt->{parent}->{contains_text}) { push @sprint, "\n" . $INDENT x $elt->level; }
8747 push @sprint, $elt->comment_string;
8748 }
8749 elsif( (exists $elt->{'ent'})) { push @sprint, $elt->ent_string; }
8750 }
8751  
8752 return;
8753 }
8754  
8755 # just a shortcut to $elt->sprint( 1)
8756 sub xml_string
8757 { my $elt= shift;
8758 isa( $_[0], 'HASH') ? $elt->sprint( shift(), 1) : $elt->sprint( 1);
8759 }
8760  
8761 sub pcdata_xml_string
8762 { my $elt= shift;
8763 if( defined( my $string= $elt->{pcdata}) )
8764 {
8765 if( ! $elt->{extra_data_in_pcdata})
8766 {
8767 $string=~ s/([$replaced_ents])/$XML::Twig::base_ent{$1}/g unless( !$replaced_ents || $keep_encoding || $elt->{asis});
8768 $string=~ s{\Q]]>}{]]&gt;}g;
8769 }
8770 else
8771 { _gen_mark( $string); # used by _(un)?protect_extra_data
8772 foreach my $data (reverse @{$elt->{extra_data_in_pcdata}})
8773 { my $substr= substr( $string, $data->{offset});
8774 if( $keep_encoding || $elt->{asis})
8775 { substr( $string, $data->{offset}, 0, $data->{text}); }
8776 else
8777 { substr( $string, $data->{offset}, 0, _protect_extra_data( $data->{text})); }
8778 }
8779 unless( $keep_encoding || $elt->{asis})
8780 {
8781 $string=~ s{([$replaced_ents])}{$XML::Twig::base_ent{$1}}g ;
8782 $string=~ s{\Q]]>}{]]&gt;}g;
8783 _unprotect_extra_data( $string);
8784 }
8785 }
8786 return $output_text_filter ? $output_text_filter->( $string) : $string;
8787 }
8788 else
8789 { return ''; }
8790 }
8791  
8792 { my $mark;
8793 my( %char2ent, %ent2char);
8794 BEGIN
8795 { %char2ent= ( '<' => 'lt', '&' => 'amp', '>' => 'gt');
8796 %ent2char= map { $char2ent{$_} => $_ } keys %char2ent;
8797 }
8798  
8799 # generate a unique mark (a string) not found in the string,
8800 # used to mark < and & in the extra data
8801 sub _gen_mark
8802 { $mark="AAAA";
8803 $mark++ while( index( $_[0], $mark) > -1);
8804 return $mark;
8805 }
8806  
8807 sub _protect_extra_data
8808 { my( $extra_data)= @_;
8809 $extra_data=~ s{([<&>])}{:$mark:$char2ent{$1}:}g;
8810 return $extra_data;
8811 }
8812  
8813 sub _unprotect_extra_data
8814 { $_[0]=~ s{:$mark:(\w+):}{$ent2char{$1}}g; }
8815  
8816 }
8817  
8818 sub cdata_string
8819 { my $cdata= $_[0]->{cdata};
8820 unless( defined $cdata) { return ''; }
8821 if( $remove_cdata)
8822 { $cdata=~ s/([$replaced_ents])/$XML::Twig::base_ent{$1}/g; }
8823 else
8824 { # if the CDATA includes the end of CDATA marker, we need to split it
8825 $cdata=~ s{$CDATA_END}{]]$CDATA_END$CDATA_START>}g;
8826 $cdata= $CDATA_START . $cdata . $CDATA_END;
8827 }
8828 return $cdata;
8829 }
8830  
8831 sub att_xml_string
8832 { my $elt= shift;
8833 my $att= shift;
8834  
8835 my $replace= $replaced_ents . "$quote\n\r\t";
8836 if($_[0] && $_[0]->{escape_gt} && ($replace!~ m{>}) ) { $replace .='>'; }
8837  
8838 if( defined (my $string= $elt->{att}->{$att}))
8839 { return _att_xml_string( $string, $replace); }
8840 else
8841 { return ''; }
8842 }
8843  
8844 # escaped xml string for an attribute value
8845 sub _att_xml_string
8846 { my( $string, $escape)= @_;
8847 if( !defined( $string)) { return ''; }
8848 if( $keep_encoding)
8849 { $string=~ s{$quote}{$XML::Twig::base_ent{$quote}}g;
8850 }
8851 else
8852 {
8853 if( $do_not_escape_amp_in_atts)
8854 { $escape=~ s{^.}{}; # seems like the most backward compatible way to remove & from the list
8855 $string=~ s{([$escape])}{$XML::Twig::base_ent{$1}}g;
8856 $string=~ s{&(?!(\w+|#\d+|[xX][0-9a-fA-F]+);)}{&amp;}g; # dodgy: escape & that do not start an entity
8857 }
8858 else
8859 { $string=~ s{([$escape])}{$XML::Twig::base_ent{$1}}g;
8860 $string=~ s{\Q]]>}{]]&gt;}g;
8861 }
8862 }
8863  
8864 return $output_text_filter ? $output_text_filter->( $string) : $string;
8865 }
8866  
8867 sub ent_string
8868 { my $ent= shift;
8869 my $ent_text= $ent->{ent};
8870 my( $t, $el, $ent_string);
8871 if( $expand_external_entities
8872 && ($t= $ent->twig)
8873 && ($el= $t->entity_list)
8874 && ($ent_string= $el->{entities}->{$ent->ent_name}->{val})
8875 )
8876 { return $ent_string; }
8877 else
8878 { return $ent_text; }
8879 }
8880  
8881 # returns just the text, no tags, for an element
8882 sub text
8883 { my( $elt, @options)= @_;
8884  
8885 if( @options && grep { lc( $_) eq 'no_recurse' } @options) { return $elt->text_only; }
8886 my $sep = (@options && grep { lc( $_) eq 'sep' } @options) ? ' ' : '';
8887  
8888 my $string;
8889  
8890 if( (exists $elt->{'pcdata'})) { return $elt->{pcdata} . $sep; }
8891 elsif( (exists $elt->{'cdata'})) { return $elt->{cdata} . $sep; }
8892 elsif( (exists $elt->{'target'})) { return $elt->pi_string . $sep; }
8893 elsif( (exists $elt->{'comment'})) { return $elt->{comment} . $sep; }
8894 elsif( (exists $elt->{'ent'})) { return $elt->{ent} . $sep ; }
8895  
8896 my $child= $elt->{first_child} ||'';
8897 while( $child)
8898 {
8899 my $child_text= $child->text( @options);
8900 $string.= defined( $child_text) ? $sep . $child_text : '';
8901 } continue { $child= $child->{next_sibling}; }
8902  
8903 unless( defined $string) { $string=''; }
8904  
8905 return $output_text_filter ? $output_text_filter->( $string) : $string;
8906 }
8907  
8908 sub text_only
8909 { return join '', map { $_->is_text || (exists $_->{'ent'}) ? $_->text : '' } $_[0]->_children; }
8910  
8911 sub trimmed_text
8912 { my $elt= shift;
8913 my $text= $elt->text( @_);
8914 $text=~ s{\s+}{ }sg;
8915 $text=~ s{^\s}{};
8916 $text=~ s{\s$}{};
8917 return $text;
8918 }
8919  
8920 sub trim
8921 { my( $elt)= @_;
8922 my $pcdata= $elt->first_descendant( $TEXT);
8923 (my $pcdata_text= $pcdata->text)=~ s{^\s+}{}s;
8924 $pcdata->set_text( $pcdata_text);
8925 $pcdata= $elt->last_descendant( $TEXT);
8926 ($pcdata_text= $pcdata->text)=~ s{\s+$}{};
8927 $pcdata->set_text( $pcdata_text);
8928 foreach my $pcdata ($elt->descendants( $TEXT))
8929 { ($pcdata_text= $pcdata->text)=~ s{\s+}{ }g;
8930 $pcdata->set_text( $pcdata_text);
8931 }
8932 return $elt;
8933 }
8934  
8935 # remove cdata sections (turns them into regular pcdata) in an element
8936 sub remove_cdata
8937 { my $elt= shift;
8938 foreach my $cdata ($elt->descendants_or_self( $CDATA))
8939 { if( $keep_encoding)
8940 { my $data= $cdata->{cdata};
8941 $data=~ s{([&<"'])}{$XML::Twig::base_ent{$1}}g;
8942 $cdata->{pcdata}= (delete $cdata->{empty} || 1) && $data;
8943 }
8944 else
8945 { $cdata->{pcdata}= (delete $cdata->{empty} || 1) && $cdata->{cdata}; }
8946 $cdata->{gi}=$XML::Twig::gi2index{$PCDATA} or $cdata->set_gi( $PCDATA);
8947 undef $cdata->{cdata};
8948 }
8949 }
8950  
8951 sub _is_private { return _is_private_name( $_[0]->gi); }
8952 sub _is_private_name { return $_[0]=~ m{^#(?!default:)}; }
8953  
8954 } # end of block containing package globals ($pretty_print, $quotes, keep_encoding...)
8955  
8956 # merges consecutive #PCDATAs in am element
8957 sub normalize
8958 { my( $elt)= @_;
8959 my @descendants= $elt->descendants( $PCDATA);
8960 while( my $desc= shift @descendants)
8961 { if( ! length $desc->{pcdata}) { $desc->delete; next; }
8962 while( @descendants && $desc->{next_sibling} && $desc->{next_sibling}== $descendants[0])
8963 { my $to_merge= shift @descendants;
8964 $desc->merge_text( $to_merge);
8965 }
8966 }
8967 return $elt;
8968 }
8969  
8970 # SAX export methods
8971 sub toSAX1
8972 { _toSAX(@_, \&_start_tag_data_SAX1, \&_end_tag_data_SAX1); }
8973  
8974 sub toSAX2
8975 { _toSAX(@_, \&_start_tag_data_SAX2, \&_end_tag_data_SAX2); }
8976  
8977 sub _toSAX
8978 { my( $elt, $handler, $start_tag_data, $end_tag_data)= @_;
8979 if( $elt->{gi} >= $XML::Twig::SPECIAL_GI)
8980 { my $data= $start_tag_data->( $elt);
8981 _start_prefix_mapping( $elt, $handler, $data);
8982 if( $data && (my $start_element = $handler->can( 'start_element')))
8983 { unless( $elt->{'flushed'}) { $start_element->( $handler, $data); } }
8984  
8985 foreach my $child ($elt->_children)
8986 { $child->_toSAX( $handler, $start_tag_data, $end_tag_data); }
8987  
8988 if( (my $data= $end_tag_data->( $elt)) && (my $end_element = $handler->can( 'end_element')) )
8989 { $end_element->( $handler, $data); }
8990 _end_prefix_mapping( $elt, $handler);
8991 }
8992 else # text or special element
8993 { if( (exists $elt->{'pcdata'}) && (my $characters= $handler->can( 'characters')))
8994 { $characters->( $handler, { Data => $elt->{pcdata} }); }
8995 elsif( (exists $elt->{'cdata'}))
8996 { if( my $start_cdata= $handler->can( 'start_cdata'))
8997 { $start_cdata->( $handler); }
8998 if( my $characters= $handler->can( 'characters'))
8999 { $characters->( $handler, {Data => $elt->{cdata} }); }
9000 if( my $end_cdata= $handler->can( 'end_cdata'))
9001 { $end_cdata->( $handler); }
9002 }
9003 elsif( ((exists $elt->{'target'})) && (my $pi= $handler->can( 'processing_instruction')))
9004 { $pi->( $handler, { Target =>$elt->{target}, Data => $elt->{data} }); }
9005 elsif( ((exists $elt->{'comment'})) && (my $comment= $handler->can( 'comment')))
9006 { $comment->( $handler, { Data => $elt->{comment} }); }
9007 elsif( ((exists $elt->{'ent'})))
9008 {
9009 if( my $se= $handler->can( 'skipped_entity'))
9010 { $se->( $handler, { Name => $elt->ent_name }); }
9011 elsif( my $characters= $handler->can( 'characters'))
9012 { if( defined $elt->ent_string)
9013 { $characters->( $handler, {Data => $elt->ent_string}); }
9014 else
9015 { $characters->( $handler, {Data => $elt->ent_name}); }
9016 }
9017 }
9018  
9019 }
9020 }
9021  
9022 sub _start_tag_data_SAX1
9023 { my( $elt)= @_;
9024 my $name= $XML::Twig::index2gi[$elt->{'gi'}];
9025 return if( ( (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 1) eq '#') && (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 9) ne '#default:') ));
9026 my $attributes={};
9027 my $atts= $elt->{att};
9028 while( my( $att, $value)= each %$atts)
9029 { $attributes->{$att}= $value unless( ( $att=~ m{^#(?!default:)} )); }
9030 my $data= { Name => $name, Attributes => $attributes};
9031 return $data;
9032 }
9033  
9034 sub _end_tag_data_SAX1
9035 { my( $elt)= @_;
9036 return if( ( (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 1) eq '#') && (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 9) ne '#default:') ));
9037 return { Name => $XML::Twig::index2gi[$elt->{'gi'}] };
9038 }
9039  
9040 sub _start_tag_data_SAX2
9041 { my( $elt)= @_;
9042 my $data={};
9043  
9044 my $name= $XML::Twig::index2gi[$elt->{'gi'}];
9045 return if( ( (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 1) eq '#') && (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 9) ne '#default:') ));
9046 $data->{Name} = $name;
9047 $data->{Prefix} = $elt->ns_prefix;
9048 $data->{LocalName} = $elt->local_name;
9049 $data->{NamespaceURI} = $elt->namespace;
9050  
9051 # save a copy of the data so we can re-use it for the end tag
9052 my %sax2_data= %$data;
9053 $elt->{twig_elt_SAX2_data}= \%sax2_data;
9054  
9055 # add the attributes
9056 $data->{Attributes}= $elt->_atts_to_SAX2;
9057  
9058 return $data;
9059 }
9060  
9061 sub _atts_to_SAX2
9062 { my $elt= shift;
9063 my $SAX2_atts= {};
9064 foreach my $att (keys %{$elt->{att}})
9065 {
9066 next if( ( $att=~ m{^#(?!default:)} ));
9067 my $SAX2_att={};
9068 $SAX2_att->{Name} = $att;
9069 $SAX2_att->{Prefix} = _ns_prefix( $att);
9070 $SAX2_att->{LocalName} = _local_name( $att);
9071 $SAX2_att->{NamespaceURI} = $elt->namespace( $SAX2_att->{Prefix});
9072 $SAX2_att->{Value} = $elt->{'att'}->{$att};
9073 my $SAX2_att_name= "{$SAX2_att->{NamespaceURI}}$SAX2_att->{LocalName}";
9074  
9075 $SAX2_atts->{$SAX2_att_name}= $SAX2_att;
9076 }
9077 return $SAX2_atts;
9078 }
9079  
9080 sub _start_prefix_mapping
9081 { my( $elt, $handler, $data)= @_;
9082 if( my $start_prefix_mapping= $handler->can( 'start_prefix_mapping')
9083 and my @new_prefix_mappings= grep { /^\{[^}]*\}xmlns/ || /^\{$XMLNS_URI\}/ } keys %{$data->{Attributes}}
9084 )
9085 { foreach my $prefix (@new_prefix_mappings)
9086 { my $prefix_string= $data->{Attributes}->{$prefix}->{LocalName};
9087 if( $prefix_string eq 'xmlns') { $prefix_string=''; }
9088 my $prefix_data=
9089 { Prefix => $prefix_string,
9090 NamespaceURI => $data->{Attributes}->{$prefix}->{Value}
9091 };
9092 $start_prefix_mapping->( $handler, $prefix_data);
9093 $elt->{twig_end_prefix_mapping} ||= [];
9094 push @{$elt->{twig_end_prefix_mapping}}, $prefix_string;
9095 }
9096 }
9097 }
9098  
9099 sub _end_prefix_mapping
9100 { my( $elt, $handler)= @_;
9101 if( my $end_prefix_mapping= $handler->can( 'end_prefix_mapping'))
9102 { foreach my $prefix (@{$elt->{twig_end_prefix_mapping}})
9103 { $end_prefix_mapping->( $handler, { Prefix => $prefix} ); }
9104 }
9105 }
9106  
9107 sub _end_tag_data_SAX2
9108 { my( $elt)= @_;
9109 return if( ( (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 1) eq '#') && (substr( $XML::Twig::index2gi[$elt->{'gi'}], 0, 9) ne '#default:') ));
9110 return $elt->{twig_elt_SAX2_data};
9111 }
9112  
9113 sub contains_text
9114 { my $elt= shift;
9115 my $child= $elt->{first_child};
9116 while ($child)
9117 { return 1 if( $child->is_text || (exists $child->{'ent'}));
9118 $child= $child->{next_sibling};
9119 }
9120 return 0;
9121 }
9122  
9123 # creates a single pcdata element containing the text as child of the element
9124 # options:
9125 # - force_pcdata: when set to a true value forces the text to be in a #PCDATA
9126 # even if the original element was a #CDATA
9127 sub set_text
9128 { my( $elt, $string, %option)= @_;
9129  
9130 if( $XML::Twig::index2gi[$elt->{'gi'}] eq $PCDATA)
9131 { return $elt->{pcdata}= (delete $elt->{empty} || 1) && $string; }
9132 elsif( $XML::Twig::index2gi[$elt->{'gi'}] eq $CDATA)
9133 { if( $option{force_pcdata})
9134 { $elt->{gi}=$XML::Twig::gi2index{$PCDATA} or $elt->set_gi( $PCDATA);
9135 $elt->{cdata}= '';
9136 return $elt->{pcdata}= (delete $elt->{empty} || 1) && $string;
9137 }
9138 else
9139 { $elt->{cdata}= $string;
9140 return $string;
9141 }
9142 }
9143 elsif( $elt->contains_a_single( $PCDATA) )
9144 { # optimized so we have a slight chance of not losing embedded comments and pi's
9145 $elt->{first_child}->set_pcdata( $string);
9146 return $elt;
9147 }
9148  
9149 foreach my $child (@{[$elt->_children]})
9150 { $child->delete; }
9151  
9152 my $pcdata= $elt->_new_pcdata( $string);
9153 $pcdata->paste( $elt);
9154  
9155 delete $elt->{empty};
9156  
9157 return $elt;
9158 }
9159  
9160 # set the content of an element from a list of strings and elements
9161 sub set_content
9162 { my $elt= shift;
9163  
9164 return $elt unless defined $_[0];
9165  
9166 # attributes can be given as a hash (passed by ref)
9167 if( ref $_[0] eq 'HASH')
9168 { my $atts= shift;
9169 $elt->del_atts; # usually useless but better safe than sorry
9170 $elt->set_atts( $atts);
9171 return $elt unless defined $_[0];
9172 }
9173  
9174 # check next argument for #EMPTY
9175 if( !(ref $_[0]) && ($_[0] eq $EMPTY) )
9176 { $elt->{empty}= 1; return $elt; }
9177  
9178 # case where we really want to do a set_text, the element is '#PCDATA'
9179 # or contains a single PCDATA and we only want to add text in it
9180 if( ($XML::Twig::index2gi[$elt->{'gi'}] eq $PCDATA || $elt->contains_a_single( $PCDATA))
9181 && (@_ == 1) && !( ref $_[0]))
9182 { $elt->set_text( $_[0]);
9183 return $elt;
9184 }
9185 elsif( ($XML::Twig::index2gi[$elt->{'gi'}] eq $CDATA) && (@_ == 1) && !( ref $_[0]))
9186 { $elt->{cdata}= $_[0];
9187 return $elt;
9188 }
9189  
9190 # delete the children
9191 foreach my $child (@{[$elt->_children]})
9192 { $child->delete; }
9193  
9194 if( @_) { delete $elt->{empty}; }
9195  
9196 foreach my $child (@_)
9197 { if( ref( $child) && isa( $child, 'XML::Twig::Elt'))
9198 { # argument is an element
9199 $child->paste( 'last_child', $elt);
9200 }
9201 else
9202 { # argument is a string
9203 if( (my $pcdata= $elt->{last_child}) && $elt->{last_child}->is_pcdata)
9204 { # previous child is also pcdata: just concatenate
9205 $pcdata->{pcdata}= (delete $pcdata->{empty} || 1) && $pcdata->{pcdata} . $child
9206 }
9207 else
9208 { # previous child is not a string: create a new pcdata element
9209 $pcdata= $elt->_new_pcdata( $child);
9210 $pcdata->paste( 'last_child', $elt);
9211 }
9212 }
9213 }
9214  
9215 return $elt;
9216 }
9217  
9218 # inserts an element (whose gi is given) as child of the element
9219 # all children of the element are now children of the new element
9220 # returns the new element
9221 sub insert
9222 { my ($elt, @args)= @_;
9223 # first cut the children
9224 my @children= do { my $elt= $elt; my @children=(); my $child= $elt->{first_child}; while( $child) { push @children, $child; $child= $child->{next_sibling}; } @children; };
9225 foreach my $child (@children)
9226 { $child->cut; }
9227 # insert elements
9228 while( my $gi= shift @args)
9229 { my $new_elt= $elt->new( $gi);
9230 # add attributes if needed
9231 if( defined( $args[0]) && ( isa( $args[0], 'HASH')) )
9232 { $new_elt->set_atts( shift @args); }
9233 # paste the element
9234 $new_elt->paste( $elt);
9235 delete $elt->{empty};
9236 $elt= $new_elt;
9237 }
9238 # paste back the children
9239 foreach my $child (@children)
9240 { $child->paste( 'last_child', $elt); }
9241 return $elt;
9242 }
9243  
9244 # insert a new element
9245 # $elt->insert_new_element( $opt_position, $gi, $opt_atts_hash, @opt_content);
9246 # the element is created with the same syntax as new
9247 # position is the same as in paste, first_child by default
9248 sub insert_new_elt {
9249 my $elt= shift;
9250 my $position= _op_position($_[0]);
9251 if( $OP_POSITION{$position} )
9252 { shift; }
9253 else
9254 { $position= 'first_child'; }
9255  
9256 my $new_elt = !ref( $_[0] ) && $_[0] =~ m{^\s*<}
9257 ? $elt->parse( $_[0], elt_class => ref($elt) ) # arg is an xml string
9258 : $elt->new( @_); # args are tag/atts/conten
9259  
9260 $new_elt->paste( $position, $elt);
9261 }
9262  
9263 # wraps an element in elements which gi's are given as arguments
9264 # $elt->wrap_in( 'td', 'tr', 'table') wraps the element as a single
9265 # cell in a table for example
9266 # returns the new element
9267 sub wrap_in
9268 { my $elt= shift;
9269 my $t= $elt->twig;
9270 while( my $gi = shift @_)
9271 { my $new_elt = $elt->new( $gi);
9272 if( $elt->{twig_current})
9273 { $t->{twig_current}= $new_elt;
9274 delete $elt->{'twig_current'};
9275 $new_elt->{'twig_current'}=1;
9276 if ( $t->{twig_in_cdata} || $t->{twig_in_pcdata} ) {
9277 $t->{twig_in_cdata} = 0;
9278 $t->{twig_in_pcdata} = 0;
9279 $t->{twig_current}=$elt->{parent};
9280 delete $new_elt->{'twig_current'};
9281 $elt->{parent}->set_twig_current;
9282 }
9283 }
9284  
9285 if( my $parent= $elt->{parent})
9286 { $new_elt->{parent}=$parent; weaken( $new_elt->{parent}); ;
9287 if( $parent->{first_child} == $elt) { $parent->{first_child}= $new_elt; }
9288 if( $parent->{last_child} == $elt) { delete $parent->{empty}; $parent->{last_child}=$new_elt; weaken( $parent->{last_child}); ; }
9289 }
9290 else
9291 { # wrapping the root
9292 my $twig= $elt->twig;
9293 if( $twig && $twig->root && ($twig->root eq $elt) )
9294 { $twig->set_root( $new_elt);
9295 }
9296 }
9297  
9298 if( my $prev_sibling= $elt->{prev_sibling})
9299 { $new_elt->{prev_sibling}=$prev_sibling; weaken( $new_elt->{prev_sibling}); ;
9300 $prev_sibling->{next_sibling}= $new_elt;
9301 }
9302  
9303 if( my $next_sibling= $elt->{next_sibling})
9304 { $new_elt->{next_sibling}= $next_sibling;
9305 $next_sibling->{prev_sibling}=$new_elt; weaken( $next_sibling->{prev_sibling}); ;
9306 }
9307 $new_elt->{first_child}= $elt;
9308 delete $new_elt->{empty}; $new_elt->{last_child}=$elt; weaken( $new_elt->{last_child}); ;
9309  
9310 $elt->{parent}=$new_elt; weaken( $elt->{parent}); ;
9311 $elt->{prev_sibling}=undef; weaken( $elt->{prev_sibling}); ;
9312 $elt->{next_sibling}= undef;
9313  
9314 # add the attributes if the next argument is a hash ref
9315 if( defined( $_[0]) && (isa( $_[0], 'HASH')) )
9316 { $new_elt->set_atts( shift @_); }
9317  
9318 $elt= $new_elt;
9319 }
9320  
9321 return $elt;
9322 }
9323  
9324 sub replace
9325 { my( $elt, $ref)= @_;
9326  
9327 if( $elt->{parent}) { $elt->cut; }
9328  
9329 if( my $parent= $ref->{parent})
9330 { $elt->{parent}=$parent; weaken( $elt->{parent}); ;
9331 if( $parent->{first_child} == $ref) { $parent->{first_child}= $elt; }
9332 if( $parent->{last_child} == $ref) { delete $parent->{empty}; $parent->{last_child}=$elt; weaken( $parent->{last_child}); ; }
9333 }
9334 elsif( $ref->twig && $ref == $ref->twig->root)
9335 { $ref->twig->set_root( $elt); }
9336  
9337 if( my $prev_sibling= $ref->{prev_sibling})
9338 { $elt->{prev_sibling}=$prev_sibling; weaken( $elt->{prev_sibling}); ;
9339 $prev_sibling->{next_sibling}= $elt;
9340 }
9341 if( my $next_sibling= $ref->{next_sibling})
9342 { $elt->{next_sibling}= $next_sibling;
9343 $next_sibling->{prev_sibling}=$elt; weaken( $next_sibling->{prev_sibling}); ;
9344 }
9345  
9346 $ref->{parent}=undef; weaken( $ref->{parent}); ;
9347 $ref->{prev_sibling}=undef; weaken( $ref->{prev_sibling}); ;
9348 $ref->{next_sibling}= undef;
9349 return $ref;
9350 }
9351  
9352 sub replace_with
9353 { my $ref= shift;
9354 my $elt= shift;
9355 $elt->replace( $ref);
9356 foreach my $new_elt (reverse @_)
9357 { $new_elt->paste( after => $elt); }
9358 return $elt;
9359 }
9360  
9361 # move an element, same syntax as paste, except the element is first cut
9362 sub move
9363 { my $elt= shift;
9364 $elt->cut;
9365 $elt->paste( @_);
9366 return $elt;
9367 }
9368  
9369 # adds a prefix to an element, creating a pcdata child if needed
9370 sub prefix
9371 { my ($elt, $prefix, $option)= @_;
9372 my $asis= ($option && ($option eq 'asis')) ? 1 : 0;
9373 if( (exists $elt->{'pcdata'})
9374 && (($asis && $elt->{asis}) || (!$asis && ! $elt->{asis}))
9375 )
9376 { $elt->{pcdata}= (delete $elt->{empty} || 1) && $prefix . $elt->{pcdata}; }
9377 elsif( $elt->{first_child} && $elt->{first_child}->is_pcdata
9378 && ( ($asis && $elt->{first_child}->{asis})
9379 || (!$asis && ! $elt->{first_child}->{asis}))
9380 )
9381 {
9382 $elt->{first_child}->set_pcdata( $prefix . $elt->{first_child}->pcdata);
9383 }
9384 else
9385 { my $new_elt= $elt->_new_pcdata( $prefix);
9386 my $pos= (exists $elt->{'pcdata'}) ? 'before' : 'first_child';
9387 $new_elt->paste( $pos => $elt);
9388 if( $asis) { $new_elt->set_asis; }
9389 }
9390 return $elt;
9391 }
9392  
9393 # adds a suffix to an element, creating a pcdata child if needed
9394 sub suffix
9395 { my ($elt, $suffix, $option)= @_;
9396 my $asis= ($option && ($option eq 'asis')) ? 1 : 0;
9397 if( (exists $elt->{'pcdata'})
9398 && (($asis && $elt->{asis}) || (!$asis && ! $elt->{asis}))
9399 )
9400 { $elt->{pcdata}= (delete $elt->{empty} || 1) && $elt->{pcdata} . $suffix; }
9401 elsif( $elt->{last_child} && $elt->{last_child}->is_pcdata
9402 && ( ($asis && $elt->{last_child}->{asis})
9403 || (!$asis && ! $elt->{last_child}->{asis}))
9404 )
9405 { $elt->{last_child}->set_pcdata( $elt->{last_child}->pcdata . $suffix); }
9406 else
9407 { my $new_elt= $elt->_new_pcdata( $suffix);
9408 my $pos= (exists $elt->{'pcdata'}) ? 'after' : 'last_child';
9409 $new_elt->paste( $pos => $elt);
9410 if( $asis) { $new_elt->set_asis; }
9411 }
9412 return $elt;
9413 }
9414  
9415 # create a path to an element ('/root/.../gi)
9416 sub path
9417 { my $elt= shift;
9418 my @context= ( $elt, $elt->ancestors);
9419 return "/" . join( "/", reverse map {$_->gi} @context);
9420 }
9421  
9422 sub xpath
9423 { my $elt= shift;
9424 my $xpath;
9425 foreach my $ancestor (reverse $elt->ancestors_or_self)
9426 { my $gi= $XML::Twig::index2gi[$ancestor->{'gi'}];
9427 $xpath.= "/$gi";
9428 my $index= $ancestor->prev_siblings( $gi) + 1;
9429 unless( ($index == 1) && !$ancestor->next_sibling( $gi))
9430 { $xpath.= "[$index]"; }
9431 }
9432 return $xpath;
9433 }
9434  
9435 # methods used mainly by wrap_children
9436  
9437 # return a string with the
9438 # for an element <foo><elt att="val">...</elt><elt2/><elt>...</elt></foo>
9439 # returns '<elt att="val"><elt2><elt>'
9440 sub _stringify_struct
9441 { my( $elt, %opt)= @_;
9442 my $string='';
9443 my $pretty_print= set_pretty_print( 'none');
9444 foreach my $child ($elt->_children)
9445 { $child->add_id; $string .= $child->start_tag( { escape_gt => 1 }) ||''; }
9446 set_pretty_print( $pretty_print);
9447 return $string;
9448 }
9449  
9450 # wrap a series of elements in a new one
9451 sub _wrap_range
9452 { my $elt= shift;
9453 my $gi= shift;
9454 my $atts= isa( $_[0], 'HASH') ? shift : undef;
9455 my $range= shift; # the string with the tags to wrap
9456  
9457 my $t= $elt->twig;
9458  
9459 # get the tags to wrap
9460 my @to_wrap;
9461 while( $range=~ m{<[\w.*-]+\s+[^>]*id=("[^"]*"|'[^']*')[^>]*>}g)
9462 { push @to_wrap, $t->elt_id( substr( $1, 1, -1)); }
9463  
9464 return '' unless @to_wrap;
9465  
9466 my $to_wrap= shift @to_wrap;
9467 my %atts= %$atts;
9468 my $new_elt= $to_wrap->wrap_in( $gi, \%atts);
9469 $_->move( last_child => $new_elt) foreach (@to_wrap);
9470  
9471 return '';
9472 }
9473  
9474 # wrap children matching a regexp in a new element
9475 sub wrap_children
9476 { my( $elt, $regexp, $gi, $atts)= @_;
9477  
9478 $atts ||={};
9479  
9480 my $elt_as_string= $elt->_stringify_struct; # stringify the elt structure
9481 $regexp=~ s{(<[^>]*>)}{_match_expr( $1)}eg; # in the regexp, replace gi's by the proper regexp
9482 $elt_as_string=~ s{($regexp)}{$elt->_wrap_range( $gi, $atts, $1)}eg; # then do the actual replace
9483  
9484 return $elt;
9485 }
9486  
9487 sub _match_expr
9488 { my $tag= shift;
9489 $tag=~s{<\*}{<#star};
9490 my( $gi, %atts)= XML::Twig::_parse_start_tag( $tag);
9491 if( $gi eq '#star') { $gi= $REG_TAG_NAME; }
9492 return _match_tag( $gi, %atts);
9493 }
9494  
9495 sub _match_tag
9496 { my( $elt, %atts)= @_;
9497 my $string= "<$elt\\b";
9498 foreach my $key (sort keys %atts)
9499 { my $val= qq{\Q$atts{$key}\E};
9500 $string.= qq{[^>]*$key=(?:"$val"|'$val')};
9501 }
9502 $string.= qq{[^>]*>};
9503 return "(?:$string)";
9504 }
9505  
9506 sub field_to_att
9507 { my( $elt, $cond, $att)= @_;
9508 $att ||= $cond;
9509 my $child= $elt->first_child( $cond) or return undef;
9510 $elt->set_att( $att => $child->text);
9511 $child->cut;
9512 return $elt;
9513 }
9514  
9515 sub att_to_field
9516 { my( $elt, $att, $tag)= @_;
9517 $tag ||= $att;
9518 my $child= $elt->insert_new_elt( first_child => $tag, $elt->{'att'}->{$att});
9519 $elt->del_att( $att);
9520 return $elt;
9521 }
9522  
9523 # sort children methods
9524  
9525 sub sort_children_on_field
9526 { my $elt = shift;
9527 my $field = shift;
9528 my $get_key= sub { return $_[0]->field( $field) };
9529 return $elt->sort_children( $get_key, @_);
9530 }
9531  
9532 sub sort_children_on_att
9533 { my $elt = shift;
9534 my $att = shift;
9535 my $get_key= sub { return $_[0]->{'att'}->{$att} };
9536 return $elt->sort_children( $get_key, @_);
9537 }
9538  
9539 sub sort_children_on_value
9540 { my $elt = shift;
9541 #my $get_key= eval qq{ sub { $NO_WARNINGS; return \$_[0]->text } };
9542 my $get_key= \&text;
9543 return $elt->sort_children( $get_key, @_);
9544 }
9545  
9546 sub sort_children
9547 { my( $elt, $get_key, %opt)=@_;
9548 $opt{order} ||= 'normal';
9549 $opt{type} ||= 'alpha';
9550 #my( $par_a, $par_b)= ($opt{order} eq 'reverse') ? qw( b a) : qw ( a b) ;
9551 #my $op= ($opt{type} eq 'numeric') ? '<=>' : 'cmp' ;
9552 my @children= $elt->cut_children;
9553 if( $opt{type} eq 'numeric')
9554 { @children= map { $_->[1] }
9555 sort { $a->[0] <=> $b->[0] }
9556 map { [ $get_key->( $_), $_] } @children;
9557 }
9558 elsif( $opt{type} eq 'alpha')
9559 { @children= map { $_->[1] }
9560 sort { $a->[0] cmp $b->[0] }
9561 map { [ $get_key->( $_), $_] } @children;
9562 }
9563 else
9564 { croak "wrong sort type '$opt{type}', should be either 'alpha' or 'numeric'"; }
9565  
9566 @children= reverse @children if( $opt{order} eq 'reverse');
9567 $elt->set_content( @children);
9568 }
9569  
9570 # comparison methods
9571  
9572 sub before
9573 { my( $a, $b)=@_;
9574 if( $a->cmp( $b) == -1) { return 1; } else { return 0; }
9575 }
9576  
9577 sub after
9578 { my( $a, $b)=@_;
9579 if( $a->cmp( $b) == 1) { return 1; } else { return 0; }
9580 }
9581  
9582 sub lt
9583 { my( $a, $b)=@_;
9584 return 1 if( $a->cmp( $b) == -1);
9585 return 0;
9586 }
9587  
9588 sub le
9589 { my( $a, $b)=@_;
9590 return 1 unless( $a->cmp( $b) == 1);
9591 return 0;
9592 }
9593  
9594 sub gt
9595 { my( $a, $b)=@_;
9596 return 1 if( $a->cmp( $b) == 1);
9597 return 0;
9598 }
9599  
9600 sub ge
9601 { my( $a, $b)=@_;
9602 return 1 unless( $a->cmp( $b) == -1);
9603 return 0;
9604 }
9605  
9606 sub cmp
9607 { my( $a, $b)=@_;
9608  
9609 # easy cases
9610 return 0 if( $a == $b);
9611 return 1 if( $a->in($b)); # a in b => a starts after b
9612 return -1 if( $b->in($a)); # b in a => a starts before b
9613  
9614 # ancestors does not include the element itself
9615 my @a_pile= ($a, $a->ancestors);
9616 my @b_pile= ($b, $b->ancestors);
9617  
9618 # the 2 elements are not in the same twig
9619 return undef unless( $a_pile[-1] == $b_pile[-1]);
9620  
9621 # find the first non common ancestors (they are siblings)
9622 my $a_anc= pop @a_pile;
9623 my $b_anc= pop @b_pile;
9624  
9625 while( $a_anc == $b_anc)
9626 { $a_anc= pop @a_pile;
9627 $b_anc= pop @b_pile;
9628 }
9629  
9630 # from there move left and right and figure out the order
9631 my( $a_prev, $a_next, $b_prev, $b_next)= ($a_anc, $a_anc, $b_anc, $b_anc);
9632 while()
9633 { $a_prev= $a_prev->{prev_sibling} || return( -1);
9634 return 1 if( $a_prev == $b_next);
9635 $a_next= $a_next->{next_sibling} || return( 1);
9636 return -1 if( $a_next == $b_prev);
9637 $b_prev= $b_prev->{prev_sibling} || return( 1);
9638 return -1 if( $b_prev == $a_next);
9639 $b_next= $b_next->{next_sibling} || return( -1);
9640 return 1 if( $b_next == $a_prev);
9641 }
9642 }
9643  
9644 sub _dump
9645 { my( $elt, $option)= @_;
9646  
9647 my $atts = defined $option->{atts} ? $option->{atts} : 1;
9648 my $extra = defined $option->{extra} ? $option->{extra} : 0;
9649 my $short_text = defined $option->{short_text} ? $option->{short_text} : 40;
9650 # $option->{depth} is the number of levels that should be dumped
9651  
9652 my $sp= '| ';
9653 my $indent= $sp x $elt->level;
9654 my $indent_sp= ' ' x $elt->level;
9655  
9656 my $dump='';
9657 if( $elt->is_elt)
9658 {
9659 $dump .= $indent . '|-' . $XML::Twig::index2gi[$elt->{'gi'}];
9660  
9661 if( $atts && (my @atts= $elt->att_names) )
9662 { $dump .= ' ' . join( ' ', map { qq{$_="} . $elt->{'att'}->{$_} . qq{"} } @atts); }
9663  
9664 $dump .= "\n";
9665 if( $extra) { $dump .= $elt->_dump_extra_data( $indent, $indent_sp, $short_text); }
9666  
9667 if( exists $option->{depth}) { $option->{depth}--; }
9668 $dump .= join( "", map { $_->_dump( $option) } do { my $elt= $elt; my @children=(); my $child= $elt->{first_child}; while( $child) { push @children, $child; $child= $child->{next_sibling}; } @children; }) unless exists $option->{depth} && $option->{depth} <= 0;
9669 }
9670 else
9671 {
9672 if( (exists $elt->{'pcdata'}))
9673 { $dump .= "$indent|-PCDATA: '" . _short_text( $elt->{pcdata}, $short_text) . "'\n" }
9674 elsif( (exists $elt->{'ent'}))
9675 { $dump .= "$indent|-ENTITY: '" . _short_text( $elt->{ent}, $short_text) . "'\n" }
9676 elsif( (exists $elt->{'cdata'}))
9677 { $dump .= "$indent|-CDATA: '" . _short_text( $elt->{cdata}, $short_text) . "'\n" }
9678 elsif( (exists $elt->{'comment'}))
9679 { $dump .= "$indent|-COMMENT: '" . _short_text( $elt->comment_string, $short_text) . "'\n" }
9680 elsif( (exists $elt->{'target'}))
9681 { $dump .= "$indent|-PI: '" . $elt->{target} . "' - '" . _short_text( $elt->{data}, $short_text) . "'\n" }
9682 if( $extra) { $dump .= $elt->_dump_extra_data( $indent, $indent_sp, $short_text); }
9683 }
9684 return $dump;
9685 }
9686  
9687 sub _dump_extra_data
9688 { my( $elt, $indent, $indent_sp, $short_text)= @_;
9689 my $dump='';
9690 if( $elt->extra_data)
9691 { my $extra_data = $indent . "|-- (cpi before) '" . _short_text( $elt->extra_data, $short_text) . "'";
9692 $extra_data=~ s{\n}{$indent_sp}g;
9693 $dump .= $extra_data . "\n";
9694 }
9695 if( $elt->{extra_data_in_pcdata})
9696 { foreach my $data ( @{$elt->{extra_data_in_pcdata}})
9697 { my $extra_data = $indent . "|-- (cpi offset $data->{offset}) '" . _short_text( $data->{text}, $short_text) . "'";
9698 $extra_data=~ s{\n}{$indent_sp}g;
9699 $dump .= $extra_data . "\n";
9700 }
9701 }
9702 if( $elt->{extra_data_before_end_tag})
9703 { my $extra_data = $indent . "|-- (cpi end) '" . _short_text( $elt->{extra_data_before_end_tag}, $short_text) . "'";
9704 $extra_data=~ s{\n}{$indent_sp}g;
9705 $dump .= $extra_data . "\n";
9706 }
9707 return $dump;
9708 }
9709  
9710 sub _short_text
9711 { my( $string, $length)= @_;
9712 if( !$length || (length( $string) < $length) ) { return $string; }
9713 my $l1= (length( $string) -5) /2;
9714 my $l2= length( $string) - ($l1 + 5);
9715 return substr( $string, 0, $l1) . ' ... ' . substr( $string, -$l2);
9716 }
9717  
9718 sub _and { return _join_defined( ' && ', map { $_ } @_); }
9719 sub _join_defined { return join( shift(), grep { $_ } @_); }
9720  
9721 1;
9722 __END__
« blib/lib/XML/speedup.pl ↑ Summary blib/lib/XML/Twig/XPath.pm »