Mercurial > pub > Impl
annotate Lib/IMPL/DOM/XMLReader.pm @ 393:69a1f1508696
minor security refactoring
author | cin |
---|---|
date | Fri, 14 Feb 2014 16:41:12 +0400 |
parents | 5aff94ba842f |
children |
rev | line source |
---|---|
49 | 1 package IMPL::DOM::XMLReader; |
2 use strict; | |
3 use warnings; | |
4 | |
166 | 5 use parent qw(IMPL::Object IMPL::Object::Autofill); |
263 | 6 |
49 | 7 use IMPL::Class::Property; |
8 use XML::Parser; | |
263 | 9 |
10 use IMPL::require { | |
11 Schema => 'IMPL::DOM::Schema', # IMPL::DOM::Schema references IMPL::DOM::XML::Reader | |
12 Builder => 'IMPL::DOM::Navigator::Builder', | |
13 SimpleBuilder => 'IMPL::DOM::Navigator::SimpleBuilder' | |
14 }; | |
49 | 15 |
16 __PACKAGE__->PassThroughArgs; | |
17 | |
18 BEGIN { | |
19 public _direct property Navigator => prop_get | owner_set; | |
152 | 20 public _direct property SkipWhitespace => prop_get | owner_set; |
49 | 21 private _direct property _current => prop_all; |
22 private _direct property _text => prop_all; | |
23 private _direct property _textHistory => prop_all; | |
24 } | |
25 | |
26 sub Parse { | |
27 my ($this,$in) = @_; | |
28 | |
29 my $parser = new XML::Parser( | |
30 Handlers => { | |
31 Start => sub {shift; goto &OnStart($this,@_)}, | |
32 End => sub {shift; goto &OnEnd($this,@_)}, | |
33 Char => sub {shift; goto &OnChar($this,@_)} | |
34 } | |
35 ); | |
36 | |
37 $parser->parse($in); | |
38 } | |
39 | |
40 sub ParseFile { | |
41 my ($this,$in) = @_; | |
42 | |
43 my $parser = new XML::Parser( | |
44 Handlers => { | |
45 Start => sub {shift; unshift @_, $this; goto &_OnBegin;}, | |
46 End => sub {shift; unshift @_, $this; goto &_OnEnd;}, | |
47 Char => sub {shift; unshift @_, $this; goto &_OnChar;} | |
48 } | |
49 ); | |
50 | |
51 $parser->parsefile($in); | |
52 } | |
53 | |
54 sub _OnBegin { | |
55 my ($this,$element,%attrs) = @_; | |
56 | |
57 push @{$this->{$_textHistory}},$this->{$_text}; | |
58 $this->{$_text} = ""; | |
59 $this->{$_current} = $this->Navigator->NavigateCreate($element,%attrs); | |
60 } | |
61 | |
62 sub _OnEnd { | |
63 my ($this,$element) = @_; | |
384 | 64 $this->{$_current}->nodeValue($this->{$_text}) if length $this->{$_text} and (not $this->{$SkipWhitespace} or $this->{$_text} =~ /\S/); |
49 | 65 $this->{$_text} = pop @{$this->{$_textHistory}}; |
66 $this->{$_current} = $this->Navigator->Back; | |
67 } | |
68 | |
69 sub _OnChar { | |
70 my ($this,$val) = @_; | |
71 $this->{$_text} .= $val; | |
72 } | |
73 | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
74 sub LoadDocument { |
194 | 75 my ($self,$file,$schema) = @_; |
76 | |
77 my $parser; | |
78 if ($schema) { | |
79 $schema = IMPL::DOM::Schema->LoadSchema($schema) if not ref $schema; | |
80 $parser = $self->new( | |
81 Navigator => IMPL::DOM::Navigator::Builder->new( | |
82 'IMPL::DOM::Document', | |
83 $schema | |
84 ) | |
85 ); | |
86 } else { | |
87 $parser = $self->new( | |
88 Navigator => IMPL::DOM::Navigator::SimpleBuilder->new() | |
89 ); | |
90 } | |
91 | |
92 $parser->ParseFile($file); | |
93 my $doc = $parser->Navigator->Document; | |
368 | 94 my @errors; |
194 | 95 if ($schema) { |
96 push @errors, $schema->Validate($doc); | |
97 } | |
368 | 98 |
99 if (wantarray) { | |
100 return $doc,\@errors; | |
101 } else { | |
102 die new IMPL::Exception("Loaded document doesn't match the schema", @errors) if @errors; | |
103 return $doc; | |
104 } | |
104
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
105 } |
196bf443b5e1
DOM::Schema RC0 inflators support, validation and some other things,
wizard
parents:
103
diff
changeset
|
106 |
49 | 107 1; |
108 | |
109 __END__ | |
110 | |
111 =pod | |
112 | |
113 =head1 SYNOPSIS | |
114 | |
115 my $reader = new IMPL::DOM::XMLReader(Navigator => $DomBuilder); | |
116 my $obj = $reader->parsefile("data.xml"); | |
117 | |
118 =head1 DESCRIPTION | |
119 | |
180 | 120 Простой класс, использующий навигатор для постороения документа. В зависимости от |
121 используемого навигатора может быть получен различный результат. | |
49 | 122 |
180 | 123 Навигатор должен поодерживать методы C<NavigateCreate> и C<Back> |
49 | 124 |
125 =head1 METHODS | |
126 | |
127 =over | |
128 | |
129 =item C<CTOR(Naviagtor => $builder)> | |
130 | |
180 | 131 Создает новый экземпляр парсера, с указанным навигатором для построения документа |
49 | 132 |
133 =item C<$obj->Parse($in)> | |
134 | |
180 | 135 Строит документ. На вход получает либо xml строку, либо HANDLE. |
49 | 136 |
137 =item C<$obj->ParseFile($fileName)> | |
138 | |
180 | 139 Строит документ из файла с именем C<$fileName>. |
49 | 140 |
141 =back | |
142 | |
143 =cut |