Changeset 29679


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

Switch to an Ordered Hash to stop the XML files from being scrambled

Location:
main/trunk/package-kits/scripts
Files:
4 added
2 edited

Legend:

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

    r29678 r29679  
    1111use Greenstone::XML::Tidy;
    1212
    13 my $hash;
     13my $hash = Hash::Ordered->new;
    1414my %commands;
    1515# Command structure:
     
    3232        'Clears the internal state',
    3333        sub {
    34             undef $hash;
    35             $hash = {};
     34            $hash = Hash::Ordered->new;
    3635        }
    3736    ],
     
    4140            my $new = read_xml shift @ARGV;
    4241            # Append the new data to the current data
    43             for my $key (keys %$new) {
    44                 if ($key eq '.attr' and exists $hash->{'.attr'}) {
    45                     for my $attr ($new->{'.attr'}) {
    46                         $hash->{'.attr'}->{$attr} = $new->{'.attr'}->{$attr};
     42            for my $key ($new->keys) {
     43                if ($key eq '.attr' and $hash->exists ('.attr')) {
     44                    my $existing_attr = $hash->get ('.attr');
     45                    my $new_attr      = $new->get ('.attr');
     46                    for my $attr ($new_attr->keys) {
     47                        $existing_attr->set ($attr => $new_attr->get ($attr));
    4748                    }
    48                 } elsif (exists $hash->{$key}) {
    49                     if (ref $hash->{$key} ne 'ARRAY') {
    50                         $hash->{$key} = [ $hash->{$key} ];
     49                } elsif ($hash->exists ($key)) {
     50                    my $existing = $hash->get ($key);
     51                    if (ref $existing ne 'ARRAY') {
     52                        $existing = [ $existing ];
     53                        $hash->set ($key => $existing);
    5154                    }
    52                     push @{$hash->{$key}},
    53                         (ref $new->{$key} eq 'ARRAY' ? @{$new->{$key}} : $new->{$key});
     55                    my $new_val = $new->get ($key);
     56                    push @{$existing},
     57                        (ref $new_val eq 'ARRAY' ? @{$new_val} : $new_val);
    5458                } else {
    55                     $hash->{$key} = $new->{$key};
     59                    $hash->set ($key => $new->get ($key));
    5660                }
    5761            }
  • main/trunk/package-kits/scripts/perllib/Greenstone/XML/Tidy.pm

    r29678 r29679  
    66use XML::Parser;
    77use POSIX 'isatty';
     8use Hash::Ordered;
    89use base 'Exporter';
    910
     
    7071
    7172    sub tidy {
    72         my %hash;
     73        my $hash = Hash::Ordered->new;
    7374        # If the element has attributes, add them to the hash
    7475        my $attr = shift;
    7576        if (scalar keys %$attr > 0) {
    76             $hash{'.attr'} = $attr;
     77            $hash->set ('.attr' => Hash::Ordered->new ($attr));
    7778        }
    7879        # Read any child elements or text value
     
    8182            my $value   = shift;
    8283            if ($element eq 0) {
    83                 $value =~ /^\s*$/ || ($hash{'.value'} .= $value);
     84                unless ($value =~ /^\s*$/) {
     85                    my $existing = $hash->get ('.value');
     86                    defined $existing and ($value .= $existing);
     87                    $hash->set ('.value' => $value);
     88                }
    8489            } else {
    8590                # If there is more than one of a single tupe of child element,
    8691                # That child element must become an array
    87                 if (defined $hash{$element}) {
    88                     unless (ref $hash{$element} eq 'ARRAY') {
    89                         $hash{$element} = [ $hash{$element} ];
     92                if ($hash->exists ($element)) {
     93                    my $existing = $hash->get ($element);
     94                    unless (ref $existing eq 'ARRAY') {
     95                        $hash->set ($element => [ $existing ]);
    9096                    }
    91                     push @{$hash{$element}}, tidy (@{$value});
     97                    push @{$hash->get ($element)}, tidy (@{$value});
    9298                } else {
    93                     $hash{$element} = tidy (@{$value});
     99                    $hash->set ($element => tidy (@{$value}));
    94100                }
    95101            }
    96102        }
    97103        # If the element only has a value, it can become a scalar
    98         if (scalar keys %hash == 1 and defined $hash{'.value'}) {
    99             return $hash{'.value'};
     104        if (scalar $hash->keys == 1 and $hash->exists ('.value')) {
     105            return $hash->get ('.value');
    100106        }
    101         return \%hash;
     107        return $hash;
    102108    }
    103109    return tidy (@{@$data[1]});
     
    119125        print $indent, "<", $tag;
    120126        if (defined $attr) {
    121             for my $key (sort keys %$attr) {
    122                 print " ", $key, '="', $attr->{$key}, '"';
     127            for my $key ($attr->keys) {
     128                print " ", $key, '="', $attr->get ($key), '"';
    123129            }
    124130        }
     
    127133    sub write_element {
    128134        my ($indent, $hash) = @_;
    129         for my $key (sort keys %$hash) {
     135        for my $key ($hash->keys) {
    130136            $key eq '.attr' && next;
    131             my $val = $hash->{$key};
     137            my $val = $hash->get ($key);
    132138            $key eq '.value' && do {
    133139                print $val, "\n";
     
    137143                /^ARRAY$/ && do {
    138144                    for my $element (@$val) {
    139                         open_tag ($indent, $key, $element->{'.attr'});
     145                        open_tag ($indent, $key, $element->get ('.attr'));
    140146                        for (ref $element) {
    141                             /^HASH$/ && do {
     147                            /^Hash::Ordered$/ && do {
    142148                                print "\n";
    143149                                write_element ("  $indent", $element);
     
    150156                    last;
    151157                };
    152                 /^HASH$/ && do {
    153                     open_tag ($indent, $key, $val->{'.attr'});
     158                /^Hash::Ordered$/ && do {
     159                    open_tag ($indent, $key, $val->get ('.attr'));
    154160                    # If the element only has a value with attributes,
    155161                    # it can be formatted on one line
    156                     if (scalar keys %$val == 2 and defined $val->{'.value'}) {
    157                         print $val->{'.value'}, "</", $key, ">\n";
     162                    if (scalar keys %$val == 2 and $val->exists ('.value')) {
     163                        print $val->get ('.value'), "</", $key, ">\n";
    158164                    } else {
    159165                        print "\n";
Note: See TracChangeset for help on using the changeset viewer.