#!/bin/perl -w

# script that breaks apart RDFIG chump annotatations and posts
# these to the annotea annotation server, em@w3.org
# $Id: annopost.pl,v 1.5 2001/11/13 19:05:33 em Exp $

use strict;
use XML::DOM;
use XML::XSLT;
use LWP::UserAgent;
use Getopt::Long;

# command line variables

my $xmlfile = '';
my $xslfile = '';
my $user = '';
my $passwd = '';
my $server = '';

GetOptions ('xml=s' => \$xmlfile,
	    'xsl=s' => \$xslfile,
	    'user=s' => \$user,
	    'passwd=s' => \$passwd,
	    'server=s' => \$server);

if (($xmlfile eq '') ||
    ($xslfile eq '') ||
    ($user eq '') ||
    ($passwd eq '') ||
    ($server eq '')) {
  print "Usage: annopost.pl --xml xmlfile --xsl xslfile --user username --passwd password --server annoteaserver\n";
  exit 1;
}



# create the HTTP POST request

my $request = new HTTP::Request( 'POST', $server);
$request->header( 'Content-Type' => 'application/xml' );
$request->authorization_basic($user, $passwd);

# RDF/XML information for each Annotation

my $header = '<?xml version="1.0" encoding="utf-8"?> <rdf:RDF xmlns:an="http://www.w3.org/2000/10/annotation-ns#" xmlns:dc="http://purl.org/dc/elements/1.0/" xmlns:h="http://www.w3.org/1999/xx/http#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> ';
my $footer = '</rdf:RDF>';

# load XML document and parse via XSLT input file

my $xslt = XML::XSLT->new ($xslfile, warnings => 1);
$xslt->transform ($xmlfile);

# create the dom object based on xslt parsetree

my $dom = new XML::DOM::Parser;	# create parser
my $doc = $dom->parse($xslt->toString);

# get date associated with chump file (or system if not availiable)
# and use to id log file

my $logdate = '';

$_ = $doc->getElementsByTagName('last-updated')->item(0)->toString;

if (/\<last\-updated\>(.*) (.*)\<\/last\-updated\>/) {
  $logdate = $1;
} else {
  $logdate = `date --iso-8601`;
  $logdate =~ tr/\n//d;
}

my $logfile = "log/annoadded." . $logdate . ".xml";
open (LOGFILE, ">>$logfile") || die "sorry, system can\'t open outfile for reading/appending to";


# iterate over created annotations

foreach my $tag ('an:Annotation') {

  my $nodes= $doc->getElementsByTagName($tag);
  my $n = $nodes->getLength;

  print LOGFILE "<log date=\"", $logdate, "\">\n";

  for (my $i = 0; $i < $n; $i++) {

      # calculate body length of annotation in question

      my $annotation = $nodes->item($i);
      my $anode = $annotation->getElementsByTagName('html')->item(0);
      my $bodycontent = $anode->toString;
      my $length = length($bodycontent);

      # get parent node

      my $dnode = $annotation->getElementsByTagName('rdf:Description')->item(0);

      # create node for storing the content length

      my $lnode = $doc->createElement('h:ContentLength');
      $dnode->appendChild($lnode);

      my $ltext = $doc->createTextNode($length);
      $lnode->appendChild($ltext);

      process_annotations($doc, $annotation, $i);
  }

  print LOGFILE "</log>\n";

}

sub process_annotations {	# annotation handler

  my ( $doc, $annotation, $idx)= @_;

  # build request content message by cating RDF/XML header and annotation

  my $message = $header . $annotation->toString . $footer;
  $request->content( $message );

  print LOGFILE "<post annoation_id =\"", $idx, "\">\n";
  print LOGFILE "<message>\n", $message, "</message>\n";

  my $agent = new LWP::UserAgent;
  $agent->agent( "$0/0.1 " . $agent->agent ); #-- send the request and get the response
  my $response = $agent->request( $request );

  print LOGFILE "<response>\n", $response->as_string, "</response>\n";

  print LOGFILE "</post>\n\n";
}

exit;

