line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#============================================================= -*-perl-*- |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# XML::Schema::Parser |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION |
6
|
|
|
|
|
|
|
# XML parser module which is bound to a particular Schema and/or |
7
|
|
|
|
|
|
|
# Schedule. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# AUTHOR |
10
|
|
|
|
|
|
|
# Andy Wardley |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# COPYRIGHT |
13
|
|
|
|
|
|
|
# Copyright (C) 2001 Canon Research Centre Europe Ltd. |
14
|
|
|
|
|
|
|
# All Rights Reserved. |
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
# This module is free software; you can redistribute it and/or |
17
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
# REVISION |
20
|
|
|
|
|
|
|
# $Id: Parser.pm,v 1.1.1.1 2001/08/29 14:30:17 abw Exp $ |
21
|
|
|
|
|
|
|
# |
22
|
|
|
|
|
|
|
#======================================================================== |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
package XML::Schema::Parser; |
25
|
|
|
|
|
|
|
|
26
|
4
|
|
|
4
|
|
1957
|
use strict; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
149
|
|
27
|
4
|
|
|
4
|
|
4990
|
use XML::Parser; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use XML::Schema::Base; |
29
|
|
|
|
|
|
|
use base qw( XML::Schema::Base ); |
30
|
|
|
|
|
|
|
use vars qw( $VERSION $DEBUG $ERROR $ETYPE @OPTIONAL $XML_PARSER_ARGS ); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$VERSION = sprintf("%d.%02d", q$Revision: 1.1.1.1 $ =~ /(\d+)\.(\d+)/); |
33
|
|
|
|
|
|
|
$DEBUG = 0 unless defined $DEBUG; |
34
|
|
|
|
|
|
|
$ERROR = ''; |
35
|
|
|
|
|
|
|
$ETYPE = 'parser'; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
@OPTIONAL = qw( schema ); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$XML_PARSER_ARGS = { |
40
|
|
|
|
|
|
|
ErrorContext => 2, |
41
|
|
|
|
|
|
|
Namespaces => 1, |
42
|
|
|
|
|
|
|
ParseParamEnt => 1, |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
47
|
|
|
|
|
|
|
# init(\%config) |
48
|
|
|
|
|
|
|
# |
49
|
|
|
|
|
|
|
# Called by new() constructor method to initialise object. |
50
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub init { |
53
|
|
|
|
|
|
|
my ($self, $config) = @_; |
54
|
|
|
|
|
|
|
my ($opt) = @{ $self->_baseargs( qw( @OPTIONAL ) ) }; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$self->_optional($opt, $config) |
57
|
|
|
|
|
|
|
|| return; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
return $self; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
64
|
|
|
|
|
|
|
# schema($schema) |
65
|
|
|
|
|
|
|
# |
66
|
|
|
|
|
|
|
# Retrieve current schema or update with new reference provided. |
67
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub schema { |
70
|
|
|
|
|
|
|
my $self = shift; |
71
|
|
|
|
|
|
|
return @_ ? ($self->{ schema } = shift) |
72
|
|
|
|
|
|
|
: $self->{ schema }; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
77
|
|
|
|
|
|
|
# parsefile($file) |
78
|
|
|
|
|
|
|
# |
79
|
|
|
|
|
|
|
# Parse XML file. |
80
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub parsefile { |
83
|
|
|
|
|
|
|
my ($self, $file) = @_; |
84
|
|
|
|
|
|
|
my $parser = $self->parser() |
85
|
|
|
|
|
|
|
|| return; |
86
|
|
|
|
|
|
|
my $result; |
87
|
|
|
|
|
|
|
eval { |
88
|
|
|
|
|
|
|
$result = $parser->parsefile($file); |
89
|
|
|
|
|
|
|
}; |
90
|
|
|
|
|
|
|
if (my $error = $@) { |
91
|
|
|
|
|
|
|
$error =~ s/\s*at \S+ line \d+\s*$//s; |
92
|
|
|
|
|
|
|
return $self->error($error); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
return $result; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
99
|
|
|
|
|
|
|
# parse($text) |
100
|
|
|
|
|
|
|
# |
101
|
|
|
|
|
|
|
# Parse XML text. |
102
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub parse { |
105
|
|
|
|
|
|
|
my ($self, $text) = @_; |
106
|
|
|
|
|
|
|
my $parser = $self->parser() |
107
|
|
|
|
|
|
|
|| return; |
108
|
|
|
|
|
|
|
my $result; |
109
|
|
|
|
|
|
|
eval { |
110
|
|
|
|
|
|
|
$result = $parser->parse($text); |
111
|
|
|
|
|
|
|
}; |
112
|
|
|
|
|
|
|
if (my $error = $@) { |
113
|
|
|
|
|
|
|
$error =~ s/\s*at \S+ line \d+\s*$//s; |
114
|
|
|
|
|
|
|
return $self->error($error); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
return $result; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
121
|
|
|
|
|
|
|
# parser($schema, \%args) |
122
|
|
|
|
|
|
|
# |
123
|
|
|
|
|
|
|
# Return underlying XML::Parser instance (possibly cached) properly |
124
|
|
|
|
|
|
|
# configured for action. |
125
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub parser { |
128
|
|
|
|
|
|
|
my $self = shift; |
129
|
|
|
|
|
|
|
my $schema = shift |
130
|
|
|
|
|
|
|
|| $self->{ schema } |
131
|
|
|
|
|
|
|
|| return $self->error('no schema'); |
132
|
|
|
|
|
|
|
my $args = $_[0] && ref($_[0]) eq 'HASH' ? shift : { @_ }; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
my $instance = $schema->instance($args) |
135
|
|
|
|
|
|
|
|| return $self->error( $schema->error() ); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
my $handlers = $instance->expat_handlers() |
138
|
|
|
|
|
|
|
|| return $self->error( $schema->error() ); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# handlers can be returned as { Init => ..., etc } or as |
141
|
|
|
|
|
|
|
# { Style => ..., Handlers => { Init => ..., etc } }; we |
142
|
|
|
|
|
|
|
# convert the former to the latter and supply instance class |
143
|
|
|
|
|
|
|
# as the default Style (i.e. recipient of parse events) |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$handlers = { |
146
|
|
|
|
|
|
|
Style => ref $instance, |
147
|
|
|
|
|
|
|
Handlers => $handlers, |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
unless $handlers->{ Handlers }; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $xpargs = { |
152
|
|
|
|
|
|
|
%$XML_PARSER_ARGS, |
153
|
|
|
|
|
|
|
map { defined $args->{$_} ? ( $_, $args->{$_} ) : ( ) } |
154
|
|
|
|
|
|
|
keys %$XML_PARSER_ARGS |
155
|
|
|
|
|
|
|
}; |
156
|
|
|
|
|
|
|
return XML::Parser->new( |
157
|
|
|
|
|
|
|
%$xpargs, |
158
|
|
|
|
|
|
|
%$handlers, |
159
|
|
|
|
|
|
|
); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
1; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
__END__ |