#!/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 XML::Twig; use LWP::UserAgent; use Getopt::Long; # Some files are related to the executable location use FindBin qw($Bin); use lib "$Bin"; # Parse command line options my $container_name; if (defined $ARGV[0] and $ARGV[0] ne '-h' and $ARGV[0] ne '--help') { $container_name = shift or die "You should provide the name of the container, see $0 -h\n"; $container_name =~ s/s?(?:\.dbxml)?$/s/; } # Default values my $path2DbEnv = "$Bin/dbxml"; my $output_dir = "$Bin/xml/$container_name"; Getopt::Long::Configure("bundling"); GetOptions( 'path2dbenv|p=s' => \$path2DbEnv, 'output|o=s' => \$output_dir, 'help|h' => sub { print STDERR <] [-o where can be candidates, mps, meps or ministers (for convenience the final "s" can be omitted, also ".dbxml" can be appended) Options can be: --path2dbenv, -p : path to directory of DB XML database default: $Bin/dbxml --output, -o : output directory of xml pages default: $Bin/xml/ --help, -h: print this message USAGE exit 0; } ); eval { # open a container in the db environment my $env = new DbEnv(0); $env->set_cachesize(0, 64 * 1024, 1); print "Opening $path2DbEnv\n"; $env->open($path2DbEnv, Db::DB_INIT_MPOOL|Db::DB_CREATE|Db::DB_INIT_LOCK|Db::DB_INIT_LOG); my $theMgr = new XmlManager($env); my $container = $theMgr->openContainer("$container_name.dbxml"); my $context = $theMgr->createQueryContext(XmlQueryContext::LiveValues, XmlQueryContext::Eager); my $query = "collection('".$container->getName()."')/politician"; my $results = $theMgr->query($query, $context ); print "Found ".$results->size()." documents matching the expression '$query'\n\n"; my $updateContext = $theMgr->createUpdateContext(); my $theDocument = $theMgr->createDocument(); while( $results->next($theDocument) ) { my $docName = $theDocument->getName(); $docName =~ s/\.xml_\d+/.xml/o; my $docString = $theDocument->getContent(); my $xml = XML::Twig->new(output_encoding => 'UTF-8',pretty_print => 'indented')->parse($docString); my $out_file = "$output_dir/$docName"; open OUT, '>', $out_file or die "Cannot write to $out_file: $!\n"; $xml->flush(\*OUT); close OUT; print "$out_file written.\n"; } exit 0; } ; if (my $e = catch std::exception) { warn "Exception:\n"; warn $e->what() . "\n"; exit( -1 ); } elsif ($@) { warn "Query failed\n"; warn $@; exit( -1 ); }