Index: /gs3-extensions/html-to-expeditee/trunk/src/perllib/ExpediteeExportAction.pm
===================================================================
--- /gs3-extensions/html-to-expeditee/trunk/src/perllib/ExpediteeExportAction.pm	(revision 26632)
+++ /gs3-extensions/html-to-expeditee/trunk/src/perllib/ExpediteeExportAction.pm	(revision 26632)
@@ -0,0 +1,121 @@
+###########################################################################
+#
+# ExpediteeExportAction.pm -- 
+# Based on HTMLToExpediteeAction.pm - Used for exporting INDIVIDUAL
+# Greenstone documents to Expeditee. If you want to export an entire
+# collection, use HTMLToExpediteeAction.pm.
+#
+# A component of the Greenstone digital library software
+# from the New Zealand Digital Library Project at the 
+# University of Waikato, New Zealand.
+#
+# Copyright (C) 2009 New Zealand Digital Library Project
+#
+# This program 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.
+#
+# This program 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 this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+###########################################################################
+
+package ExpediteeExportAction;
+
+use strict;
+
+use cgiactions::baseaction;
+
+use ExpediteeFrameIO;
+use JSON;
+
+use Devel::Peek;
+
+BEGIN {
+#    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/perl-5.8");
+}
+
+@ExpediteeExportAction::ISA = ('baseaction');
+
+# c=collect and site=xxxx implicitly handled by code
+# 'c' is handled by base action, site is tested for only if greenstone3.
+
+my $action_table=
+{
+	    "generate-frame"     => { 'compulsory-args' => [ "fn", "json" ],
+			      'optional-args'   => [] }, 
+};
+
+sub new
+{
+    my $class = shift (@_);
+    my ($gsdl_cgi,$iis6_mode) = @_;
+
+    my $self = new baseaction($action_table,$gsdl_cgi,$iis6_mode);
+
+    return bless $self, $class;
+}
+
+sub generate_frame
+{
+	my $self = shift @_;
+	
+	my $username  = $self->{'username'};
+    my $collect   = $self->{'collect'};
+    my $gsdl_cgi  = $self->{'gsdl_cgi'};
+    my $gsdlhome  = $self->{'gsdlhome'};
+
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+
+    my $fn = $self->{'fn'};
+	
+	my $json_str_unicode = $self->{'json'}; 
+	
+	my $found = $self->{'found'};
+	
+	 if (!Encode::is_utf8( $json_str_unicode )) {
+         $json_str_unicode = Encode::decode_utf8( $json_str_unicode );
+     }
+	 
+	# decode_json expects string to convert to be raw utf8
+    my $json_str_utf8 = Encode::encode("utf8",$json_str_unicode);
+    my $exp_frame_tree = decode_json $json_str_utf8;	
+    
+    my $output_dir = &util::filename_cat($collect_dir,$collect,"export");
+
+    if (!-d $output_dir) {
+	&util::mk_dir($output_dir);
+    }
+
+    my $expeditee_frame_io = new ExpediteeFrameIO($output_dir,"kgas1A");
+	
+	 $expeditee_frame_io->buildFrame($exp_frame_tree);
+	 
+	 if($expeditee_frame_io->saveFrame("$fn.exp")){
+		#need to determine if $fn is followed by another number or $fn is the last number in the frameset.
+	 
+		#if $fn is last number, write out next free frame num
+		unless($found){
+			#write out next free frame num
+			$expeditee_frame_io->saveLastFrameNumber($fn);
+		}
+				
+		#otherwise just carry on.
+		
+		$gsdl_cgi->generate_message("Item Exported to Expeditee $fn");
+	 }else{
+		$gsdl_cgi->generate_error("Failed to save frame number $fn");
+	 }
+	 
+	 
+}
+
+1;
Index: /gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/export-individual-expeditee.pl.in
===================================================================
--- /gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/export-individual-expeditee.pl.in	(revision 26632)
+++ /gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/export-individual-expeditee.pl.in	(revision 26632)
@@ -0,0 +1,254 @@
+#!/cygdrive/c/strawberry/perl/bin/perl -w
+
+# Need to specify the full path of Perl above, e.g. for Windows something like
+#!C:\\Perl32\\bin\\perl -w
+
+##
+# This is used for exporting individual Greenstone
+# documents to Expeditee. To export entire collections,
+# use html-to-expeditee.pl
+##
+use strict;
+my $iis6_mode = 0;
+
+if($iis6_mode){
+	#Change into cgi-bin directory
+	chdir("cgi-bin");
+}
+
+eval("require \"gsdlCGI.pm\"");
+if ($@)
+{
+    print STDOUT "Content-type:text/plain\n\n";
+    print STDOUT "ERROR: $@\n";
+    exit 0;
+}
+
+sub display_output
+{
+	my($docID,$c,$assocfilepath,$site,$fn,$found) = @_;
+	
+	my $iframeURL = "library/collection/$c/document/$docID"."?p.showAssocFilePath=1";
+	
+	#Render an html page to display & export item.
+	my $html = <<EOT;
+<html>
+	<head>
+		<title>Export Item to Expeditee</title>
+		<base href=".."/>
+		
+		<link type="text/css" href="ext/html-to-expeditee/jquery/css/le-frog/jquery-ui-1.8.16.custom.css" rel="stylesheet"/>
+		<script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-1.6.2.min.js"></script>
+		<script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
+		<script type="text/javascript" src="ext/html-to-expeditee/js/html-to-expeditee.js"></script>
+		<script type="text/javascript" src="ext/html-to-expeditee/js/gsajax-min.js"></script>
+		
+		<style type="text/css">
+		body{
+			font-family: Arial;
+			font-size: 16px;
+			color: #000;
+		}
+		</style>
+	<script type="text/javascript">
+	function load(){
+	
+		var iframe = document.getElementById("iframe");
+		if(iframe.style.display != "block"){
+			iframe.height = "90%";
+			iframe.style.display = "block";
+		}
+		
+		var iframeDoc;
+		if( iframe.contentDocument){
+			/* Firefox & Chrome */
+			iframeDoc = iframe.contentDocument;
+		}else if(iframe.contentWindow){
+			/* IE */
+			iframeDoc = iframe.contentWindow.document;
+		}
+		
+		var gsContent = iframeDoc.getElementById("gs_content");
+						
+		//hide rightSidebar div
+		var rightSidebar = iframeDoc.getElementById("rightSidebar");
+		if(rightSidebar !== undefined){
+			rightSidebar.style.display = "none";
+		}
+		
+		var expFrameTree = htmlToExpeditee(gsContent);
+		var expFrame = JSON.stringify(expFrameTree);
+		
+		var site = "$site";
+		var url = "cgi-bin/export-individual-expeditee.pl";
+		var params = "c=" + "$c";
+		
+		if(site.match(/\\w/)){
+			params += "&site=" + site;
+		}
+		params += "&a=generate-frame&fn=" + "$fn";
+		params += "&found=" + "$found";
+		params += "&json=" + escape(expFrame);
+		
+		var request = urlPostSync(url,params);
+		
+		alert("Document has been successfully exported to Expeditee. It can be found in the collection's export directory at: $fn.exp");
+	}
+	</script>
+	</head>
+	<body>
+	
+	<p id="msg">Exporting following document to Expeditee... </p>
+	
+	<hr style="margin: 10px;"/>
+	
+	<!-- Load up an iframe to display URL in-->
+	<div align="center"><iframe id="iframe" width="1000px" height="90%" src="$iframeURL" onload="load();"></iframe</div>
+	
+	</body>
+</html>
+EOT
+	print "Content-type:text/html\n\n";
+	print $html;
+}
+
+##For debugging.
+sub check{
+
+	my($check1,$check2,$check3,$check4,$check5) = @_;
+
+	print "Content-type:text/html\n\n";
+	print $check1;
+	print $check2;
+	print $check3;
+	print $check4;
+	print $check5;
+}
+
+sub main
+{
+	my $gsdl_cgi = new gsdlCGI();
+	
+	#Load needed GSDL modules
+	$gsdl_cgi->setup_gsdl();
+	
+	my $gsdlhome = $ENV{'GSDLHOME'};
+	$gsdl_cgi->checked_chdir($gsdlhome);
+	
+	#require cgiactions::HtmlToExpediteeAction;
+	require cgiactions::ExpediteeExportAction;
+	$gsdl_cgi->parse_cgi_args();
+	
+	$gsdl_cgi->{'xml'} = 0;
+	
+	my $fn = $gsdl_cgi->param("fn");
+
+	if(defined $fn){
+		
+		my $site = $gsdl_cgi->clean_param("site");
+		my $c = $gsdl_cgi->clean_param("c");
+		my $a = $gsdl_cgi-> clean_param("a");
+		my $json = $gsdl_cgi->clean_param("json");
+		my $found = $gsdl_cgi->clean_param("found");
+		#check($site,$c,$a,$json,$found);
+		
+		my $action = new ExpediteeExportAction($gsdl_cgi,$iis6_mode);
+		$action->do_action();
+		
+		#my $action = new HtmlToExpediteeAction($gsdl_cgi,$iis6_mode);
+		#$action->do_action();
+	}else{
+		
+		printf("fn not defined\n");
+		my $docID = $gsdl_cgi->clean_param("docID");
+		my $c = $gsdl_cgi->clean_param("c");
+		my $assocfilepath = $gsdl_cgi->clean_param("assocfilepath");
+		my $site = $gsdl_cgi->clean_param("site");
+		
+		if(!defined $site){
+			$gsdl_cgi->generate_error("No site specified.");
+		}
+		
+		#Open export directory
+		my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+		my $export_dir = &util::filename_cat($collect_dir,$c,"export");
+		
+		my $frameInf = "$export_dir\\frame.inf";
+		my $fn = "";
+		my @files;
+		
+		#check if export directory exists otherwise create it.
+		if(-d $export_dir){
+			opendir DIR, $export_dir or die "cannot open dir $export_dir: $!";
+			@files = readdir DIR;
+			closedir DIR;
+		}else{
+			print "Reached!\n";
+			&util::mk_dir($export_dir);
+			
+			my $zeroFrame = "0.exp";
+			open(ZERO_FRAME,">>$export_dir\\$zeroFrame") || die "$!";
+			close ZERO_FRAME;
+			
+			opendir DIR, $export_dir or die "can't open dir $export_dir: $!\n";
+			@files = readdir DIR;
+			closedir DIR;
+		}
+		
+		my $found = 0;
+		for my $file(@files){
+			if($file =~ m/\d/){
+				
+				my $fullFile = "$export_dir\\$file";
+				open FILE, $fullFile or die $!;
+				my @lines = <FILE>;
+				
+				for my $line(@lines){
+					if($line eq "T \@assocfilepath $assocfilepath\n"){
+						$fn = $fullFile;
+						$found = 1;
+					}
+				}
+				close(FILE);
+			}
+		}
+		
+		unless($found){
+			#use frame.inf file to get file number
+			if(-e $frameInf){
+				
+				open FRAME_INF, $frameInf or die $!;
+				my @lines = <FRAME_INF>;
+				
+				for my $line(@lines){
+					$fn = $line;
+				}
+			}else{
+				
+				print "$frameInf not found\n";
+
+				$fn = 0;
+			}
+		}
+		
+		#now get frame number out of file name
+		my @getFrameNumArray = split('\\\\',$fn);
+		@getFrameNumArray = reverse(@getFrameNumArray);
+		$fn = $getFrameNumArray[0];
+		
+		$fn =~ s/[^0-9]//g;
+		unless($found){
+			$fn = $fn + 1;
+		}
+		#unless($found){
+		# $fn = $fn + 1;
+		#}
+		
+		print "fn = ".$fn."\n";
+		
+	display_output($docID,$c,$assocfilepath,$site,$fn,$found);
+	}
+	
+}
+
+&main();
