line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#$Id: Lex.pm 106 2007-06-25 10:35:07Z zag $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package HTML::WebDAO::Lex; |
4
|
1
|
|
|
1
|
|
4050
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Data::Dumper; |
6
|
|
|
|
|
|
|
use HTML::WebDAO::Lexer::Lobject; |
7
|
|
|
|
|
|
|
use HTML::WebDAO::Lexer::Lbase; |
8
|
|
|
|
|
|
|
use HTML::WebDAO::Lexer::Lregclass; |
9
|
|
|
|
|
|
|
use HTML::WebDAO::Lexer::Lobjectref; |
10
|
|
|
|
|
|
|
use HTML::WebDAO::Lexer::Ltext; |
11
|
|
|
|
|
|
|
use HTML::WebDAO::Lexer::Linclude; |
12
|
|
|
|
|
|
|
use HTML::WebDAO::Lexer::Lmethod; |
13
|
|
|
|
|
|
|
use HTML::WebDAO::Base; |
14
|
|
|
|
|
|
|
use base qw( HTML::WebDAO::Base ); |
15
|
|
|
|
|
|
|
__PACKAGE__->attributes qw/ tree auto / ; |
16
|
|
|
|
|
|
|
use strict; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub _init() { |
19
|
|
|
|
|
|
|
my $self = shift; |
20
|
|
|
|
|
|
|
return $self->Init(@_); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub Init { |
24
|
|
|
|
|
|
|
my $self = shift; |
25
|
|
|
|
|
|
|
my %par = @_; |
26
|
|
|
|
|
|
|
$self->auto( [] ); |
27
|
|
|
|
|
|
|
$self->tree( $self->buld_tree( $par{content} ) ) if $par{content}; |
28
|
|
|
|
|
|
|
return 1; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub buld_tree { |
32
|
|
|
|
|
|
|
my $self = shift; |
33
|
|
|
|
|
|
|
my $raw_html = shift; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#Mac and DOS line endings |
36
|
|
|
|
|
|
|
$raw_html =~ s/\r\n?/\n/g; |
37
|
|
|
|
|
|
|
my $mass; |
38
|
|
|
|
|
|
|
$mass = [ split( /(.*?<\/WD>)/is, $raw_html ) ]; |
39
|
|
|
|
|
|
|
my @res; |
40
|
|
|
|
|
|
|
foreach my $text (@$mass) { |
41
|
|
|
|
|
|
|
my @ref; |
42
|
|
|
|
|
|
|
unless ( $text =~ /^
|
43
|
|
|
|
|
|
|
push @ref, |
44
|
|
|
|
|
|
|
HTML::WebDAO::Lexer::Lobject->new( |
45
|
|
|
|
|
|
|
class => "_rawhtml_element", |
46
|
|
|
|
|
|
|
id => "none", |
47
|
|
|
|
|
|
|
childs => [ HTML::WebDAO::Lexer::Ltext->new( value => \$text ) ], |
48
|
|
|
|
|
|
|
context => $self |
49
|
|
|
|
|
|
|
) unless $text =~/^\s*$/; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
else { |
52
|
|
|
|
|
|
|
my $parser = new XML::LibXML; |
53
|
|
|
|
|
|
|
my $dom = $parser->parse_string($text); |
54
|
|
|
|
|
|
|
push @ref, $self->get_obj_tree( $dom->documentElement->childNodes ); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
next unless @ref; |
58
|
|
|
|
|
|
|
push @res, @ref; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
return \@res; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub get_obj_tree { |
64
|
|
|
|
|
|
|
my $self = shift; |
65
|
|
|
|
|
|
|
my %map = ( |
66
|
|
|
|
|
|
|
object => 'HTML::WebDAO::Lexer::Lobject', |
67
|
|
|
|
|
|
|
regclass => 'HTML::WebDAO::Lexer::Lregclass', |
68
|
|
|
|
|
|
|
objectref => 'HTML::WebDAO::Lexer::Lobjectref', |
69
|
|
|
|
|
|
|
text => 'HTML::WebDAO::Lexer::Ltext', |
70
|
|
|
|
|
|
|
include => 'HTML::WebDAO::Lexer::Linclude', |
71
|
|
|
|
|
|
|
default => 'HTML::WebDAO::Lexer::Lbase', |
72
|
|
|
|
|
|
|
method => 'HTML::WebDAO::Lexer::Lmethod' |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
my @result; |
75
|
|
|
|
|
|
|
foreach my $node (@_) { |
76
|
|
|
|
|
|
|
my $node_name = $node->nodeName; |
77
|
|
|
|
|
|
|
my %attr = map { $_->nodeName => $_->value } grep { defined $_ } $node->attributes; |
78
|
|
|
|
|
|
|
my $map_key = $node->nodeName || 'text'; |
79
|
|
|
|
|
|
|
$map_key = $map_key =~ /text$/ ? "text" : $map_key; |
80
|
|
|
|
|
|
|
$attr{name} = $map_key unless exists $attr{name}; |
81
|
|
|
|
|
|
|
if ( $map_key eq 'text' ) { $attr{value} = $node->nodeValue } |
82
|
|
|
|
|
|
|
my $lclass = $map{$map_key} || $map{default}; |
83
|
|
|
|
|
|
|
my @vals = (); |
84
|
|
|
|
|
|
|
if ( my @childs = $node->childNodes ) { |
85
|
|
|
|
|
|
|
@vals = grep { defined $_ } $self->get_obj_tree(@childs); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
my $lobject = $lclass->new( %attr, childs => \@vals, context => $self ) || next; |
88
|
|
|
|
|
|
|
if ( my @res = grep { ref($_) } ( $lobject->get_self ) ) { |
89
|
|
|
|
|
|
|
push @result, @res; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
return @result; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
sub _destroy { |
96
|
|
|
|
|
|
|
my $self = shift; |
97
|
|
|
|
|
|
|
$self->auto( [] ); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
1; |