diff 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
line wrap: on
line diff
--- a/Lib/IMPL/DOM/XMLReader.pm	Fri May 07 18:17:40 2010 +0400
+++ b/Lib/IMPL/DOM/XMLReader.pm	Tue May 11 02:42:59 2010 +0400
@@ -6,6 +6,9 @@
 use IMPL::Class::Property;
 use IMPL::Class::Property::Direct;
 use XML::Parser;
+require IMPL::DOM::Schema;
+require IMPL::DOM::Navigator::Builder;
+require IMPL::DOM::Navigator::SimpleBuilder;
 
 __PACKAGE__->PassThroughArgs;
 
@@ -44,7 +47,6 @@
     $parser->parsefile($in);
 }
 
-
 sub _OnBegin {
     my ($this,$element,%attrs) = @_;
     
@@ -55,8 +57,7 @@
 
 sub _OnEnd {
     my ($this,$element) = @_;
-    
-    $this->{$_current}->nodeValue($this->Navigator->inflateValue( $this->{$_text} ) ) if length $this->{$_text};
+    $this->{$_current}->nodeValue($this->Navigator->inflateValue( $this->{$_text}, $this->{$_current} ) ) if length $this->{$_text};
     $this->{$_text} = pop @{$this->{$_textHistory}};
     $this->{$_current} = $this->Navigator->Back;
 }
@@ -66,6 +67,34 @@
     $this->{$_text} .= $val;
 }
 
+sub LoadDocument {
+	my ($self,$file,$schema) = @_;
+	
+	my $parser;
+	if ($schema) {
+		$schema = IMPL::DOM::Schema->LoadSchema($schema) if not ref $schema;
+		$parser = $self->new(
+			Navigator => IMPL::DOM::Navigator::Builder->new(
+				'IMPL::DOM::Document',
+				$schema
+			)
+		);
+	} else {
+		$parser = $self->new(
+			Navigator => IMPL::DOM::Navigator::SimpleBuilder->new()
+		);
+	}
+	
+	$parser->ParseFile($file);
+	my $doc = $parser->Navigator->Document;
+	if ($schema) {
+		my @errors = $parser->Navigator->BuildErrors;
+		push @errors, $schema->Validate($doc);
+		die new IMPL::Exception("Loaded document doesn't match the schema", @errors) if @errors;
+	}
+	return $doc;
+}
+
 1;
 
 __END__