| 1 | package PITA::XML::Storable; | ||
| 2 | |||
| 3 | # A PITA::XML class that can be loaded from and saved to a file | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Params::Util qw{ _INSTANCE }; | ||
| 7 | |||
| 8 | use vars qw{$VERSION}; | ||
| 9 | BEGIN { | ||
| 10 | $VERSION = '0.52'; | ||
| 11 | } | ||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | ##################################################################### | ||
| 18 | # Main Methods | ||
| 19 | |||
| 20 | sub read { | ||
| 21 | my $class = shift; | ||
| 22 | my $fh = PITA::XML->_FH(shift); | ||
| 23 | |||
| 24 | ### NOTE: DISABLED TILL WE FINALIZE THE SCHEMA | ||
| 25 | # Validate the document and reset the handle | ||
| 26 | # $class->validate( $fh ); | ||
| 27 | # $fh->seek( 0, 0 ) or Carp::croak( | ||
| 28 | # 'Failed to reset file after validation (seek to 0)' | ||
| 29 | # ); | ||
| 30 | |||
| 31 | # Build the object from the file and validate | ||
| 32 | my $self = bless { }, $class; | ||
| 33 | my $parser = XML::SAX::ParserFactory->parser( | ||
| 34 | Handler => PITA::XML::SAXParser->new( $self ), | ||
| 35 | ); | ||
| 36 | $parser->parse_file($fh); | ||
| 37 | |||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub write { | ||
| 42 | my $self = shift; | ||
| 43 | |||
| 44 | # Prepate the driver params | ||
| 45 | my @params = _INSTANCE($_[0], 'XML::SAX::Base') | ||
| 46 | ? ( Handler => shift ) | ||
| 47 | : defined($_[0]) | ||
| 48 | ? ( Output => shift ) | ||
| 49 | : Carp::croak("Did not provide an output destination to ->write"); | ||
| 50 | |||
| 51 | # Create the SAX Driver | ||
| 52 | my $driver = PITA::XML::SAXDriver->new( @params ); | ||
| 53 | unless ( $driver ) { | ||
| 54 | die("Failed to create SAXDriver for report"); | ||
| 55 | } | ||
| 56 | |||
| 57 | # Parse ourself with the driver to driver the writing | ||
| 58 | # of the output. | ||
| 59 | $driver->parse( $self ); | ||
| 60 | |||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; |