#!/usr/bin/perl
# Auteur : bbongli@u-paris10.fr (SCD Université Paris Nanterre)
# Le programme XML2BIB.pl permet de convertir un fichier XML (ici, issu d'Excel) au format BibTeX, pour un import par lot dans HAL à l'aide de l'outil Bib2HAL.
# Version : 2
use strict;
use warnings;
# Variables globales
my $res='';
my @tab='';

while (<>){
	chop;
	# SWITCH parental
	SWITCH : {
		# Noeuds parents à babord
		if (($_ =~ /<article>/) || ($_ =~ /<inproceedings>/) || ($_ =~ /<conference>/) || ($_ =~ /<incollection>/) || ($_ =~ /<book>/) || ($_ =~ /<proceedings>/) 
			||($_ =~ /<presconf>/) || ($_ =~ /<poster>/) || ($_ =~ /<manual>/) || ($_ =~ /<unpublished>/) || ($_ =~ /<misc>/) || ($_ =~ /<inbook>/)){
			if ($_ =~ /(<)([\w-]+)(>)/){
			    my $tmp = "@" . $2 . "{";
				$_ =~ s/(<)([\w-]+)(>)/$tmp/g;
				$res = $_;
			}	
		}
		unless ($_ !~/<bibkey>([\w-]+)<\/bibkey>/){
				my $id = $1 . ",";
				$_=~ s/<bibkey>([\w-]+)<\/bibkey>/$id/g;
				$id = $_;
				my $babord = $res . $id;
				push @tab, $babord;
		}
		# Noeuds parents à tribord
		if (($_ =~ /<\/article>/) || ($_ =~ /<\/inproceedings>/) || ($_ =~ /<\/conference>/) || ($_ =~/<\/incollection>/) || ($_=~/<\/book>/) || ($_ =~ /<\/proceedings>/)
			|| ($_ =~ /<\/presconf>/) || ($_ =~ /<\/poster>/) || ($_ =~ /<\/manual>/) || ($_ =~ /<\/unpublished>/) || ($_ =~ /<\/misc>/)|| ($_=~/<\/inbook>/)){		
			if ($_ =~ /(<\/)([\w-]+)(>)/){
				my $tribord = "}" ;
				$_ =~ s/(<\/)([\w-]+)(>)/$tribord/g;
				$tribord = $_;
				push @tab, $tribord;
			}	
		}
	} # Fin de SWITCH parental
	# La gestion des noeuds fils
	if (($_ !~ /<article>/) && ($_ !~ /<\/article>/) && ($_ !~ /<inproceedings>/) && ($_ !~ /<\/inproceedings>/) && ($_ !~ /<conference>/) && ($_ !~ /<\/conference>/) && 
		($_ !~ /<incollection>/) && ($_ !~/<\/incollection>/) && ($_ !~ /<book>/) && ($_!~/<\/book>/) && ($_ !~ /<proceedings>/) && ($_ !~ /<\/proceedings>/) && 
		($_ !~ /<presconf>/) && ($_ !~ /<\/presconf>/) && ($_ !~ /<poster>/) && ($_ !~ /<\/poster>/) && ($_ !~ /<manual>/)&& ($_ !~ /<\/manual>/) && ($_ !~ /<unpublished>/) && 
		($_ !~ /<\/unpublished>/) && ($_ !~ /<misc>/) && ($_ !~ /<\/misc>/) && ($_ !~ /<contact>/) && ($_ !~ /<\/contact>/) && ($_ !~ /<inbook>/) && ($_!~/<\/inbook>/)){
		# SWITCH pour enfants
		SWITCH : {
			# Noeuds fils à babord 
			if ($_ =~ /(<)([\w-]+)(>)/){
				my $babord = $2 . " = {";
				$_ =~ s/(<)([\w-]+)(>)/$babord/g;
				$babord = $_;
				my $tmp = $babord;
			}
			if ($_ =~ /(<\/)([\w-]+)(>)/){
				my $tribord = "}," ;
				$_ =~ s/(<\/)([\w-]+)(>)/$tribord/g;
				$tribord = $_;
				my $tmp = $tribord;
				push @tab, $tmp;
			}	
		} # Fin de SWITCH pour enfants	
	}		
}	
foreach (@tab){
	print  $_ . "\n";
}