#!/bin/perl -w

#####################################################################
#                                                                   #
#  This example inserts a new document (the first argument) into    #
#  a main document (the second argument).                           #
#  The main documents includes Person elements, included in Subject #
#  elements, themselves included in Category elements.              #
#  The new document contains a single Category, containing a single #
#  Subject containing a single Person.                              #
#  The person is to be inserted in the main document at the appro-  #
#  priate place, creating a Subject and/or a Category if needed.    #
#                                                                   #
#####################################################################

use strict;
use XML::Twig;

my $USAGE= "insert_item <new_person_file> <main_file>";

my $new_person_file = $ARGV[0]  or  die $USAGE;
my $main_file       = $ARGV[1]  or  die $USAGE;

#####################################################################
#  get the new item                                                 #
#####################################################################

my $new_person= new XML::Twig();           # create the twig
$new_person->parsefile( $new_person_file); # parse the person

# get the information

my $e_person_category =  $new_person->root->first_child( 'Category');
my $e_person_subject  =  $e_person_category->first_child( 'Subject');
my $person_category   =  $e_person_category->att( 'value');
my $person_subject    =  $e_person_subject->att( 'value');
my $e_person          =  $e_person_subject->first_child( 'Person');

 

#####################################################################
#  process the main file                                            #
#####################################################################

my $item_pasted=0; # set to 1 if the person is added to a category

my $main= new XML::Twig( TwigHandlers=> { Category => \&category});
$main->parsefile( $main_file);   

# if the person belongs to a new category then add it 
$e_person_category->paste( 'last_child', $main->root) unless( $item_pasted);

$main->flush; # don't forget this one or you'll miss the end of the doc

exit;

sub category
  { my( $twig, $e_main_category)= @_;
    unless( $item_pasted)
      { my $main_category= $e_main_category->att( 'value');
        if(  ($person_category eq $main_category))
          { my @subjects= $e_main_category->children( 'Subject');
            foreach my $e_main_subject ( @subjects)
              { my $main_subject= $e_main_subject->att( 'value');
                if( $person_subject eq $main_subject)
                  { $e_person->paste( 'last_child', $e_main_subject);
                    $item_pasted=1;
                    $twig->finish_print; # no need to process now
                    return
                  }
              }
            unless( $item_pasted)
              { # person belongs to a new subject in the category
                $e_person_subject->paste( 'last_child', $e_main_category);
                $item_pasted=1;
                $twig->finish_print; # no need to process now
                return
              }
          }    
      }
    $twig->flush;
  }
