Advertisement
Guest User

XMLParser2

a guest
May 5th, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.13 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use XML::Twig;
  7. use File::Find;
  8. use Archive::Zip qw( :ERROR_CODES );
  9.  
  10. my @files;
  11. my @zfiles;
  12. my @dirs;
  13. my @files_dir;
  14.  
  15. sub extract_zip {
  16.     foreach my $zf(@zfiles) {
  17.         my $zip = Archive::Zip->new();
  18.         print "Extracting files from ".$zf."\n";
  19.         $zip->extractTree('',substr($zf,0,-4).'/') if ($zip->read($zf)) == AZ_OK;
  20.     }
  21. }
  22.  
  23. sub modify_xml {
  24.     foreach my $file(@files) {
  25.         my $t = XML::Twig->new(pretty_print => 'indented');
  26.         print "Working on ".$file."\n";
  27.         $t->parsefile($file);
  28.        
  29.         my $root = $t->root;
  30.         my $widget = $root->first_child('widget');
  31.  
  32.         # Removing protocolVersion from asset tag
  33.         my $protocolversion = $root->del_att('protocolVersion');
  34.  
  35.         # Changing the value of service
  36.         my $service = $widget->first_child('service');
  37.         $service->set_text('S-csWDDAKNaV-') if defined $service;
  38.         # Changing deployed
  39.         my $deployed = $widget->first_child('deployed');
  40.         $deployed->set_text('true') if defined $deployed;
  41.         # Removing ob element
  42.         my $ob = $widget->first_child('ob');
  43.         $ob->delete if defined $ob;
  44.         # Changing appLanguage element
  45.         my $appLanguage = $widget->first_child('appLanguage');
  46.         $appLanguage->cut_children if defined $appLanguage;
  47.         $appLanguage->set_text('es');
  48.         # Adding testStatus
  49.         my $keywords = $widget->first_child('keywords');
  50.         $keywords->insert_new_elt(after=>'testStatus','live') unless $widget->has_child('testStatus');
  51.  
  52.         # Adding testStatus
  53.         my $testStatus = $widget->first_child('testStatus');
  54.         $testStatus->insert_new_elt(after=>'priority','p1') unless $widget->has_child('priority');
  55.                
  56.         # Removing proposedCategory element
  57.         my $proposedCategory = $widget->first_child('proposedCategory');
  58.         $proposedCategory->delete if defined $proposedCategory;
  59.  
  60.         # Adding f4 as true
  61.         my $f3 = $widget->first_child('f3');
  62.         $f3->insert_new_elt(after=>'f4','true') unless $widget->has_child('f4');
  63.  
  64.         # Removing premiumResource and modifying widget
  65.         my $widget = $widget->first_child('widget');
  66.         my $premiumResource = $widget->first_child('premiumResource');
  67.         $premiumResource->delete if defined $premiumResource ;
  68.         $widget->set_att(premiumResource => "APP00");
  69.         #$root->print;
  70.         open (my $fh_out, '>:utf8', $file) or die "unable to open '$file' for writing: $!";
  71.         $t->print($fh_out);
  72.     }
  73. }
  74.  
  75. sub create_zip {
  76.     # Getting files of each directory to create its ZIP file
  77.     foreach my $f_dir(@dirs) {
  78.         my $obj = Archive::Zip->new();
  79.         $obj->addTree($f_dir,'',sub { -f });
  80.                
  81.         my $zip_filename = substr $f_dir,2,length($f_dir); 
  82.         if ($obj->writeToFileNamed($zip_filename.'.zip') != AZ_OK) {                # write to disk
  83.             print "Error in archive creation!\n";
  84.         } else {
  85.             print "Archive ".$zip_filename.".zip created successfully!\n";
  86.         }
  87.     }
  88. }
  89.  
  90. # Extracting ZIP files
  91. find(sub { push (@zfiles, $_) if -f and /\.zip$/},'.');
  92. &extract_zip(@zfiles);
  93.  
  94. # Getting the directory list in the current path
  95. find(sub{push (@dirs, $File::Find::name) if (-d $File::Find::name)},'.');
  96. shift(@dirs);
  97.  
  98. # Modifying XMLs
  99. find(sub { push (@files, $File::Find::name) if -f and /\.xml$/},@dirs);
  100.  
  101. &modify_xml(@files);
  102.  
  103. &create_zip(@dirs);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement