line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: Cindy.pm 120 2013-01-31 11:34:41Z jo $ |
2
|
|
|
|
|
|
|
# Cindy - Content INjection |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Copyright (c) 2008 Joachim Zobel . All rights reserved. |
5
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
6
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Cindy; |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
94305
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
93
|
|
12
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
46
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
6
|
use base qw(Exporter); |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
216
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.19'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our @EXPORT= qw(get_html_doc get_xml_doc |
19
|
|
|
|
|
|
|
parse_html_string parse_xml_string |
20
|
|
|
|
|
|
|
parse_cis parse_cis_string |
21
|
|
|
|
|
|
|
inject dump_xpath_profile); |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
1
|
|
430
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Cindy::Sheet; |
25
|
|
|
|
|
|
|
use Cindy::Log; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub get_html_doc($) |
28
|
|
|
|
|
|
|
{ |
29
|
|
|
|
|
|
|
my ($file) = @_; |
30
|
|
|
|
|
|
|
my $parser = XML::LibXML->new(); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
return $parser->parse_html_file($file); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub get_xml_doc($) |
36
|
|
|
|
|
|
|
{ |
37
|
|
|
|
|
|
|
my ($file) = @_; |
38
|
|
|
|
|
|
|
my $parser = XML::LibXML->new(); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return $parser->parse_file($file); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub omit_nodes { |
44
|
|
|
|
|
|
|
my ($doc, $tag) = @_; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $found = $doc->find( "///$tag" ); |
47
|
|
|
|
|
|
|
foreach my $node ($found->get_nodelist()) { |
48
|
|
|
|
|
|
|
my $parent = $node->parentNode; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
foreach my $child ($node->childNodes()) { |
51
|
|
|
|
|
|
|
$parent->insertBefore($child->cloneNode(1), $node); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
$parent->removeChild($node); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub parse_html_string($;$) |
59
|
|
|
|
|
|
|
{ |
60
|
|
|
|
|
|
|
my ($string, $ropt) = @_; |
61
|
|
|
|
|
|
|
$ropt ||= {}; |
62
|
|
|
|
|
|
|
my $html_parse_noimplied = $ropt->{html_parse_noimplied}; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $dont_omit = !$html_parse_noimplied |
65
|
|
|
|
|
|
|
|| ($string =~ /
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my $parser = XML::LibXML->new(); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $doc = $parser->parse_html_string($string, $ropt); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
if (!$dont_omit) { |
72
|
|
|
|
|
|
|
# Until HTML_PARSE_NOIMPLIED is implemented by |
73
|
|
|
|
|
|
|
# libxml2 (and passed by XML::LibXML) we need |
74
|
|
|
|
|
|
|
# to remove html/body tags that have been added to |
75
|
|
|
|
|
|
|
# fragments. |
76
|
|
|
|
|
|
|
omit_nodes($doc, 'html'); |
77
|
|
|
|
|
|
|
omit_nodes($doc, 'body'); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
return $doc; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub parse_xml_string($) |
83
|
|
|
|
|
|
|
{ |
84
|
|
|
|
|
|
|
my $parser = XML::LibXML->new(); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
return $parser->parse_string($_[0]); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub parse_cis($) |
90
|
|
|
|
|
|
|
{ |
91
|
|
|
|
|
|
|
return Cindy::Sheet::parse_cis($_[0]); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub parse_cis_string($) |
95
|
|
|
|
|
|
|
{ |
96
|
|
|
|
|
|
|
return Cindy::Sheet::parse_cis_string($_[0]); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# |
100
|
|
|
|
|
|
|
# Get a copied doc. root for modification. |
101
|
|
|
|
|
|
|
# |
102
|
|
|
|
|
|
|
sub get_root_copy($) |
103
|
|
|
|
|
|
|
{ |
104
|
|
|
|
|
|
|
my ($doc) = @_; |
105
|
|
|
|
|
|
|
my $root = $doc->documentElement(); |
106
|
|
|
|
|
|
|
my $rtn = $root->cloneNode( 1 ); |
107
|
|
|
|
|
|
|
return $rtn; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub dump_xpath_profile() |
111
|
|
|
|
|
|
|
{ |
112
|
|
|
|
|
|
|
Cindy::Injection::dump_profile(); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub inject($$$) |
116
|
|
|
|
|
|
|
{ |
117
|
|
|
|
|
|
|
my ($data, $doc, $descriptions) = @_; |
118
|
|
|
|
|
|
|
my $docroot = get_root_copy($doc); |
119
|
|
|
|
|
|
|
# my $dataroot = get_root_copy($data); |
120
|
|
|
|
|
|
|
my $dataroot = $data->getDocumentElement(); |
121
|
|
|
|
|
|
|
# Create a root description with action none |
122
|
|
|
|
|
|
|
# to hold the description list |
123
|
|
|
|
|
|
|
my $descroot = Cindy::Injection->new( |
124
|
|
|
|
|
|
|
'.', 'none', '.', 'xpath', |
125
|
|
|
|
|
|
|
sublist => $descriptions); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Connect the copied docroot with the output document. |
128
|
|
|
|
|
|
|
# This has to be done before the tree is matched. |
129
|
|
|
|
|
|
|
my $out = XML::LibXML::Document->new($doc->getVersion, $doc->getEncoding); |
130
|
|
|
|
|
|
|
$out->setDocumentElement($docroot); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# Run the sheet |
133
|
|
|
|
|
|
|
$descroot->run($dataroot, $docroot); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
return $out; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
__END__ |