line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#============================================================= -*-perl-*- |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Kite::XML::Parser |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION |
6
|
|
|
|
|
|
|
# XML parser for kite related XML markup. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# AUTHOR |
9
|
|
|
|
|
|
|
# Andy Wardley |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
# COPYRIGHT |
12
|
|
|
|
|
|
|
# Copyright (C) 2000 Andy Wardley. All Rights Reserved. |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
# This module is free software; you can redistribute it and/or |
15
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# VERSION |
18
|
|
|
|
|
|
|
# $Id: Parser.pm,v 1.1 2000/10/17 11:58:16 abw Exp $ |
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
#======================================================================== |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
package Kite::XML::Parser; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
require 5.004; |
25
|
|
|
|
|
|
|
|
26
|
2
|
|
|
2
|
|
1956
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
68
|
|
27
|
2
|
|
|
2
|
|
3127
|
use XML::Parser; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use Kite::Base; |
29
|
|
|
|
|
|
|
use base qw( Kite::Base ); |
30
|
|
|
|
|
|
|
use vars qw( $VERSION $DEBUG $AUTOLOAD $NODEBASE $NODEPKG ); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$VERSION = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/); |
33
|
|
|
|
|
|
|
$DEBUG = 0 unless defined $DEBUG; |
34
|
|
|
|
|
|
|
$NODEBASE = 'Kite::XML::Node'; |
35
|
|
|
|
|
|
|
$NODEPKG = { |
36
|
|
|
|
|
|
|
kite => "$NODEBASE\::Kite", |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
41
|
|
|
|
|
|
|
# new(@xml_parser_args) |
42
|
|
|
|
|
|
|
# |
43
|
|
|
|
|
|
|
# Constructor method which returns an XML::Parser instance. |
44
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub new { |
47
|
|
|
|
|
|
|
my $self = shift; |
48
|
|
|
|
|
|
|
return XML::Parser->new(@_, Handlers => { |
49
|
|
|
|
|
|
|
Init => \&xml_init, |
50
|
|
|
|
|
|
|
Start => \&xml_start, |
51
|
|
|
|
|
|
|
Char => \&xml_char, |
52
|
|
|
|
|
|
|
End => \&xml_end, |
53
|
|
|
|
|
|
|
Final => \&xml_final, |
54
|
|
|
|
|
|
|
}); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
58
|
|
|
|
|
|
|
# xml_init($expat) |
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
# Called at start of parse. Initialises element stack. |
61
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub xml_init { |
64
|
|
|
|
|
|
|
my $expat = shift; |
65
|
|
|
|
|
|
|
$expat->{ STACK } = []; |
66
|
|
|
|
|
|
|
debug("init\n") if $DEBUG; |
67
|
|
|
|
|
|
|
return 1; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
71
|
|
|
|
|
|
|
# xml_start($expat, element, @attributes) |
72
|
|
|
|
|
|
|
# |
73
|
|
|
|
|
|
|
# Called on each element start tag. Instantiates new node elements and |
74
|
|
|
|
|
|
|
# pushes it onto the stack. |
75
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub xml_start { |
78
|
|
|
|
|
|
|
my ($expat, $element, @attr) = @_; |
79
|
|
|
|
|
|
|
my $stack = $expat->{ STACK }; |
80
|
|
|
|
|
|
|
my $top = $stack->[-1]; |
81
|
|
|
|
|
|
|
my ($factory, $node); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
if ($top) { |
84
|
|
|
|
|
|
|
$node = $top->child($element, @attr) |
85
|
|
|
|
|
|
|
|| die($top->error(), " at line ", |
86
|
|
|
|
|
|
|
$expat->current_line(), "\n"); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
else { |
89
|
|
|
|
|
|
|
# determine package name |
90
|
|
|
|
|
|
|
my $pkg = $NODEPKG->{ $element } |
91
|
|
|
|
|
|
|
|| die("invalid element '$element' at line ", |
92
|
|
|
|
|
|
|
$expat->current_line(), "\n"); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# load module |
95
|
|
|
|
|
|
|
my $mod = $pkg; |
96
|
|
|
|
|
|
|
$mod =~ s/::/\//g; |
97
|
|
|
|
|
|
|
$mod .= '.pm'; |
98
|
|
|
|
|
|
|
require $mod; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# instantiate node |
101
|
|
|
|
|
|
|
$node = $pkg->new(@attr) |
102
|
|
|
|
|
|
|
|| die ($pkg->error(), " at line ", |
103
|
|
|
|
|
|
|
$expat->current_line(), "\n"); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
push(@$stack, $node); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
109
|
|
|
|
|
|
|
# xml_char($expat, $text) |
110
|
|
|
|
|
|
|
# |
111
|
|
|
|
|
|
|
# Called when character data is encountered. Calls the char() method |
112
|
|
|
|
|
|
|
# on the element node on top of the stack. |
113
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub xml_char { |
116
|
|
|
|
|
|
|
my ($expat, $text) = @_; |
117
|
|
|
|
|
|
|
my $stack = $expat->{ STACK }; |
118
|
|
|
|
|
|
|
my $top = $stack->[-1]; |
119
|
|
|
|
|
|
|
$top->char($text) |
120
|
|
|
|
|
|
|
|| die ($top->error(), " at line ", $expat->current_line, "\n"); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
124
|
|
|
|
|
|
|
# xml_end($expat, element) |
125
|
|
|
|
|
|
|
# |
126
|
|
|
|
|
|
|
# Called on each element end tag. Pops the top element node off the |
127
|
|
|
|
|
|
|
# stack, saving it in RESULT if it's the last item. |
128
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub xml_end { |
131
|
|
|
|
|
|
|
my ($expat, $element) = @_; |
132
|
|
|
|
|
|
|
my $stack = $expat->{ STACK }; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
my $node = pop(@$stack); |
135
|
|
|
|
|
|
|
$expat->{ RESULT } = $node |
136
|
|
|
|
|
|
|
unless @$stack; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
140
|
|
|
|
|
|
|
# xml_final($expat) |
141
|
|
|
|
|
|
|
# |
142
|
|
|
|
|
|
|
# Called at end of parse. Returns RESULT. |
143
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub xml_final { |
146
|
|
|
|
|
|
|
my $expat = shift; |
147
|
|
|
|
|
|
|
return $expat->{ RESULT }; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
1; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
__END__ |