#! /usr/bin/env perl use v5.10.1; use strict; use warnings; use Getopt::Long; use Pod::Usage; use XML::LibXML; # Parse arguments my $input; my $output; my $compact; my $help; my $man; GetOptions ("output|O=s" => \$output, "compact" => \$compact, "help|h|?" => \$help, "man" => \$man, "" => \$input) or pod2usage(2); pod2usage(1) if $help; pod2usage(-exitval => 0, -verbose => 2) if $man; # Open old defense XML file my $xmlin; if (defined $input || $#ARGV == -1) { $xmlin = *STDIN; } else { open $xmlin, "<", $ARGV[0] or die $!; } binmode $xmlin; my $dom = XML::LibXML->load_xml(IO => $xmlin); close $xmlin unless $xmlin eq *STDIN; my $new = XML::LibXML->createDocument; my $root = $new->createElement( "defense" ); $new->setDocumentElement( $root ); $root->setAttributeNode( $new->createAttribute( "duration", "420" ) ); my $last_grp; foreach my $group ($dom->getElementsByTagName("group")) { $last_grp = $new->createElement( "group" ); $root->appendChild( $last_grp ); $last_grp->setAttributeNode( $new->createAttribute( "title", $group->getElementsByTagName("title")->shift->textContent ) ); my $last_qst; foreach my $question ($group->getElementsByTagName("question")) { $last_qst = $new->createElement( "question" ); $last_grp->appendChild( $last_qst ); $last_qst->setAttributeNode( $new->createAttribute( "title", $question->getElementsByTagName("description")->shift->textContent ) ); if ($question->getElementsByTagName("kind")->shift->textContent =~ "QCM") { $last_qst->setAttributeNode( $new->createAttribute( "type", "qcm" ) ); } my $ask = $new->createElement( "ask" ); $ask->appendChild( $dom->createTextNode( $question->getElementsByTagName("intitule")->shift->textContent ) ); $last_qst->appendChild( $ask ); foreach my $answer ($question->getElementsByTagName("answer")) { my $ans = $new->createElement( "answer" ); if ($answer->getElementsByTagName("str")->shift->textContent =~ ">|<") { $ans->appendChild( $dom->createCDATASection( $answer->getElementsByTagName("str")->shift->textContent ) ); } else { $ans->appendChild( $dom->createTextNode( $answer->getElementsByTagName("str")->shift->textContent ) ); } $last_qst->appendChild( $ans ); } if ($question->getElementsByTagName("good_answer")->shift) { my $expln = $new->createElement( "explanation" ); if ($question->getElementsByTagName("good_answer")->shift->textContent =~ ">|<") { $expln->appendChild( $dom->createCDATASection( $question->getElementsByTagName("good_answer")->shift->textContent ) ); } else { $expln->appendChild( $dom->createTextNode( $question->getElementsByTagName("good_answer")->shift->textContent ) ); } $last_qst->appendChild( $expln ); } } } # Save defense XML file my $xmlout; if (defined $output) { open $xmlout, '>', $output; binmode $xmlout; } else { $xmlout = *STDOUT; } print {$xmlout} $new->toString(0) if ($compact); print {$xmlout} $new->toString(1) if (! $compact); close $xmlout unless $xmlout eq *STDOUT; __END__ =head1 NAME defense_converter.pl - Convert old style defense XML to new style =head1 SYNOPSIS defense_converter.pl [options] [file] =head1 DESCRIPTION Parse the XML file given (or stdin if no file is given) and convert it to the new XML defense form. Options: -output=file.xml save new XML to this location -compact product a non-idented XML -help brief help message -man full documentation =head1 OPTIONS =over 8 =item B<-output=file.xml> Save the new style XML to a file instead of printing it on standard output. =item B<-compact> Product a non-idented XML. =item B<-help> Print a brief help message and exits. =item B<-man> Prints the manual page and exits. =back =cut