comparison Lib/IMPL/DOM/XMLReader.pm @ 104:196bf443b5e1

DOM::Schema RC0 inflators support, validation and some other things, Minor and major fixes almost for everything. A 'Source' property of the ValidationErrors generated from a NodeSet or a NodeList is subject to change in the future.
author wizard
date Tue, 11 May 2010 02:42:59 +0400
parents c289ed9662ca
children 7b14e0122b79
comparison
equal deleted inserted replaced
103:c289ed9662ca 104:196bf443b5e1
4 4
5 use base qw(IMPL::Object IMPL::Object::Autofill); 5 use base qw(IMPL::Object IMPL::Object::Autofill);
6 use IMPL::Class::Property; 6 use IMPL::Class::Property;
7 use IMPL::Class::Property::Direct; 7 use IMPL::Class::Property::Direct;
8 use XML::Parser; 8 use XML::Parser;
9 require IMPL::DOM::Schema;
10 require IMPL::DOM::Navigator::Builder;
11 require IMPL::DOM::Navigator::SimpleBuilder;
9 12
10 __PACKAGE__->PassThroughArgs; 13 __PACKAGE__->PassThroughArgs;
11 14
12 BEGIN { 15 BEGIN {
13 public _direct property Navigator => prop_get | owner_set; 16 public _direct property Navigator => prop_get | owner_set;
42 ); 45 );
43 46
44 $parser->parsefile($in); 47 $parser->parsefile($in);
45 } 48 }
46 49
47
48 sub _OnBegin { 50 sub _OnBegin {
49 my ($this,$element,%attrs) = @_; 51 my ($this,$element,%attrs) = @_;
50 52
51 push @{$this->{$_textHistory}},$this->{$_text}; 53 push @{$this->{$_textHistory}},$this->{$_text};
52 $this->{$_text} = ""; 54 $this->{$_text} = "";
53 $this->{$_current} = $this->Navigator->NavigateCreate($element,%attrs); 55 $this->{$_current} = $this->Navigator->NavigateCreate($element,%attrs);
54 } 56 }
55 57
56 sub _OnEnd { 58 sub _OnEnd {
57 my ($this,$element) = @_; 59 my ($this,$element) = @_;
58 60 $this->{$_current}->nodeValue($this->Navigator->inflateValue( $this->{$_text}, $this->{$_current} ) ) if length $this->{$_text};
59 $this->{$_current}->nodeValue($this->Navigator->inflateValue( $this->{$_text} ) ) if length $this->{$_text};
60 $this->{$_text} = pop @{$this->{$_textHistory}}; 61 $this->{$_text} = pop @{$this->{$_textHistory}};
61 $this->{$_current} = $this->Navigator->Back; 62 $this->{$_current} = $this->Navigator->Back;
62 } 63 }
63 64
64 sub _OnChar { 65 sub _OnChar {
65 my ($this,$val) = @_; 66 my ($this,$val) = @_;
66 $this->{$_text} .= $val; 67 $this->{$_text} .= $val;
68 }
69
70 sub LoadDocument {
71 my ($self,$file,$schema) = @_;
72
73 my $parser;
74 if ($schema) {
75 $schema = IMPL::DOM::Schema->LoadSchema($schema) if not ref $schema;
76 $parser = $self->new(
77 Navigator => IMPL::DOM::Navigator::Builder->new(
78 'IMPL::DOM::Document',
79 $schema
80 )
81 );
82 } else {
83 $parser = $self->new(
84 Navigator => IMPL::DOM::Navigator::SimpleBuilder->new()
85 );
86 }
87
88 $parser->ParseFile($file);
89 my $doc = $parser->Navigator->Document;
90 if ($schema) {
91 my @errors = $parser->Navigator->BuildErrors;
92 push @errors, $schema->Validate($doc);
93 die new IMPL::Exception("Loaded document doesn't match the schema", @errors) if @errors;
94 }
95 return $doc;
67 } 96 }
68 97
69 1; 98 1;
70 99
71 __END__ 100 __END__