######################################################################
#
# EPrints::Page::DOM
#
######################################################################
#
#
# Copyright 2000-2009 University of Southampton. All Rights Reserved.
# 
#  This file is part of GNU EPrints 3.
#  
#  Copyright (c) 2000-2008 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 3 is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  EPrints 3 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with EPrints 3; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

package EPrints::Page::DOM;

@ISA = qw/ EPrints::Page /;

use strict;

sub new
{
	my( $class, $repository, $page_dom, %options ) = @_;

	EPrints::Utils::process_parameters( \%options, {
		   add_doctype => 1,
	});

	return bless { repository=>$repository, page_dom=>$page_dom, %options }, $class;
}

sub send
{
	my( $self, %options ) = @_;

	if( !defined $self->{page_dom} ) 
	{
		EPrints::abort( "Attempt to send the same page object twice!" );
	}

	binmode(STDOUT,":utf8");

	$self->send_header( %options );

	eval { print EPrints::XML::to_string( $self->{page_dom}, undef, 1 ); };
	if( $@ && $@ !~ m/^Software caused connection abort/ )
	{
		EPrints::abort( "Error in send_page: $@" );	
	}

	EPrints::XML::dispose( $self->{page_dom} );
	delete $self->{page_dom};
}

sub write_to_file
{
	my( $self, $filename, $wrote_files ) = @_;
	
	if( !defined $self->{page_dom} ) 
	{
		EPrints::abort( "Attempt to write the same page object twice!" );
	}

	EPrints::XML::write_xhtml_file( $self->{page_dom}, $filename, add_doctype=>$self->{add_doctype} );

	if( defined $wrote_files )
	{
		$wrote_files->{$filename} = 1;
	}

	EPrints::XML::dispose( $self->{page_dom} );
	delete $self->{page_dom};
}


sub DESTROY
{
	my( $self ) = @_;

	if( defined $self->{page_dom} )
	{
		EPrints::XML::dispose( $self->{page_dom} );
	}
}

1;

