Changeset 29678


Ignore:
Timestamp:
2015-01-09T14:24:10+13:00 (9 years ago)
Author:
Jeremy Symon
Message:

Moved xml i/o handling to the module for simplicity of use

Location:
main/trunk/package-kits/scripts
Files:
1 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/package-kits/scripts/gs-servlet.pl

    r29674 r29678  
    99
    1010use lib 'perllib';
    11 use XML::Tidy;
     11use Greenstone::XML::Tidy;
    1212
    1313my $hash;
     
    3939        "Parses XML from a file into the internal state\n  input_file (- for STDIN)",
    4040        sub {
    41             my $file = shift @ARGV;
    42             my $new;
    43             if ($file eq '-') {
    44                 if (isatty *STDIN) {
    45                     print STDERR "Reading XML from STDIN. Press ^D to end\n";
    46                 }
    47                 $new = read_xml *STDIN;
    48             } else {
    49                 open FH, '<', $file;
    50                 $new = read_xml *FH;
    51                 close FH;
    52             }
     41            my $new = read_xml shift @ARGV;
    5342            # Append the new data to the current data
    5443            for my $key (keys %$new) {
     
    7362        "Writes the current internal state as XML to a file\n  output_file (- for STDOUT)",
    7463        sub {
    75             my $file = shift @ARGV;
    76             if ($file eq '-') {
    77                 write_xml $hash, *STDOUT;
    78             } else {
    79                 open FH, '>', $file;
    80                 write_xml $hash, *FH;
    81                 close FH;
    82             }
     64            write_xml $hash, shift @ARGV;
    8365        },
    8466        1,
  • main/trunk/package-kits/scripts/perllib/Greenstone/Config.pm

    r29675 r29678  
    1010our $GSDLHOME = defined $ENV{GSDLHOME} ? $ENV{GSDLHOME} : $defaultGSDLHOME;
    1111our $GSDL3HOME= defined $ENV{GSDL3HOME}? $ENV{GSDL3HOME}: $defaultGSDL3HOME;
     12
     131;
  • main/trunk/package-kits/scripts/perllib/Greenstone/Directory.pm

    r29677 r29678  
    1414    my ($parent, $class, $name, %args) = @_;
    1515    ref $class and die "Constructor used as object method\n";
    16     defined $name or die "A site must have a name\n";
     16    defined $name or die "Must have a name\n";
    1717    my $self = bless (
    1818        {
     
    3131    my $parent = shift;
    3232    ref $parent and die "Static method called on object\n";
    33     my $sites = {};
     33    my $dirs = {};
    3434    my $dir = "$Greenstone::Config::GSDL3HOME/$parent";
    35     opendir DH, $dir or die "Could not open sites directory '$dir'\n";
    36     while (my $site = readdir DH) {
     35    opendir DH, $dir or die "Could not open directory '$dir'\n";
     36    while (my $entry = readdir DH) {
    3737        # skip hidden files and current/parent directory
    38         $site =~ /^\./ and next;
     38        $entry =~ /^\./ and next;
    3939        # skip non-directories
    40         -d "$dir/$site" or next;
    41         # We don't really need to store anything more than the fact that the site exists
     40        -d "$dir/$entry" or next;
     41        # We don't really need to store anything more than the fact that the directory exists
    4242        # So we just store nothing, which is enough to put the key in the hash
    4343        # (i.e. it acts as a hashset)
    44         $sites->{$site} = ();
     44        $dirs->{$entry} = ();
    4545    }
    4646    closedir DH;
    47     return $sites;
     47    return $dirs;
    4848}
    4949
    5050sub exists {
    5151    my $self = shift;
    52     my $sites = list $self->{parent};
    53     return exists $sites->{$self->{name}};
     52    my $dirs = list $self->{parent};
     53    return exists $dirs->{$self->{name}};
    5454}
    5555
  • main/trunk/package-kits/scripts/perllib/Greenstone/Site.pm

    r29677 r29678  
    2525    my $dir = $self->dir;
    2626    mkdir "$dir/collect" or die "Failed to create collections directory: $!\n";
    27     open FH, '>', "$dir/siteConfig.xml" or die "Failed to create site config: $!\n";
    28     write_xml $configTemplate, *FH;
    29     close FH;
    30     return 1;
     27    return write_xml $configTemplate, "$dir/siteConfig.xml";
    3128}
    3229
  • main/trunk/package-kits/scripts/perllib/Greenstone/XML/Tidy.pm

    r29677 r29678  
    55use utf8;
    66use XML::Parser;
     7use POSIX 'isatty';
    78use base 'Exporter';
    89
     
    5354=cut
    5455sub read_xml {
    55     my $FH = shift;
    56     local $/ = undef;
    57     my $xml = <$FH>;
    58     # The xml needs a root element, so we wrap it in one
    59     $xml='<__root__>' . $xml . '</__root__>';
     56    my $file = shift;
     57    my $xml;
     58    if ($file eq '-') {
     59        if (isatty *STDIN) {
     60            print STDERR "Reading XML from STDIN. Press ^D to end\n";
     61        }
     62        local $/ = undef;
     63        $xml = '<__root__>' . <STDIN> . '</__root__>';
     64    } else {
     65        $xml = '<!DOCTYPE doc [<!ENTITY real_doc SYSTEM "' . $file . '">]>
     66                <__root__>&real_doc;</__root__>';
     67    }
    6068    # Parse the data using XML::Parser
    6169    my $data = new XML::Parser (Style => 'Tree')->parse ($xml);
     
    98106# Writes a data structure to an XML file
    99107sub write_xml {
    100     my ($hash, $FH) = @_;
    101     select $FH;
     108    my ($hash, $file) = @_;
     109    my $FH;
     110    if ($file ne '-') {
     111        unless (open $FH, '>:utf8', $file) {
     112            print STDERR "Failed to open output file '$file': $!\n";
     113            return 0;
     114        }
     115        select $FH;
     116    }
    102117    sub open_tag {
    103118        my ($indent, $tag, $attr) = @_;
     
    155170    }
    156171    write_element ("", $hash);
    157     select STDOUT;
     172    if ($file ne '-') {
     173        select STDOUT;
     174        close $FH;
     175    }
     176    return 1;
    158177}
     178
     1791;
Note: See TracChangeset for help on using the changeset viewer.