#!/usr/bin/perl # Copyright 2007 Gérald Sédrati-Dinet # # 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 # 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA use strict; use warnings; use Sleepycat::DbXml 'simple'; use IO::File ; use File::Basename; use Getopt::Long; # Some files are related to the executable location use FindBin qw($Bin); use lib "$Bin"; # Default values my $path2DbEnv = "$Bin/dbxml"; my $theContainer = 'mps.dbxml'; # Used for metadata my $mdURI = 'http://wiki.ffii.fr/spikini/GroupeMemoirePolitique'; my $mdName = 'timeStamp'; # Parse command line options Getopt::Long::Configure("bundling"); GetOptions( 'path2dbenv|p=s' => \$path2DbEnv, 'container|c=s' => \$theContainer, 'help|h' => sub { print STDERR <] [-c ] [, ]* Options can be: --path2dbenv, -p : path to directory of DB XML database default: $Bin/dbxml --container, -c : DB XML container can be candidates, mps, meps or ministers (for convenience the final "s" can be omitted, also ".dbxml" can be appended) default: mps.dbxml --help, -h: print this message USAGE exit 0; } ); $theContainer =~ s/s?(?:\.dbxml)?$/s/; # Open a container in the db environment my $env = new DbEnv(0); $env->set_cachesize(0, 64 * 1024, 1); $env->open($path2DbEnv, Db::DB_INIT_MPOOL|Db::DB_CREATE|Db::DB_INIT_LOCK|Db::DB_INIT_LOG, 0); my $theMgr = new XmlManager($env); my $container = $theMgr->openContainer("$theContainer.dbxml", Db::DB_CREATE); # Iterate through the list of files to add, read each into a string and # put that string into the now opened container. eval { foreach my $theFile (@ARGV) { # Load the xml document into a string my $xmlString; my $inFile = new IO::File "<$theFile" or die "Cannot open $theFile: $!\n" ; while (<$inFile>) { $xmlString .= $_ ; } $inFile->close(); # declare an xml document my $xmlDoc = $theMgr->createDocument(); # Set the xml document's content to be the xmlString we just obtained. $xmlDoc->setContent( $xmlString ); # Get the document name. this strips off any path and suffix information. (my $theName,undef,undef) = fileparse($theFile, qr/\.[^.]*/); # Set the document name $xmlDoc->setName( $theName ); # Set metadata who's value is the current date and time. # Get the local time my $timeString = time ; # Set the localtime onto the timeStamp metadata attribute $xmlDoc->setMetaData( $mdURI, $mdName, $timeString ); # place that document into the container $container->putDocument($xmlDoc); warn "\tAdded $theFile to container $theContainer.\n" ; } # end files2add iterator } ; if (my $e = catch std::exception) { warn "Error adding XML data to container $theContainer\n" ; warn $e->what() . "\n"; exit( -1 ); } elsif ($@) { warn "Error adding XML data to container $theContainer\n" ; warn $@; exit( -1 ); } exit 0 ;