From 470ad65745ae6ca58c7620dfa56e5a4b7167b9cd Mon Sep 17 00:00:00 2001 From: Angelo Naselli Date: Sat, 2 Jan 2016 22:53:37 +0100 Subject: Reading xml:lang attribute for lang and building a convenient hash --- lib/ManaTools/ConfigReader.pm | 61 ++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 36 deletions(-) (limited to 'lib') diff --git a/lib/ManaTools/ConfigReader.pm b/lib/ManaTools/ConfigReader.pm index b3c6181f..b189a05d 100644 --- a/lib/ManaTools/ConfigReader.pm +++ b/lib/ManaTools/ConfigReader.pm @@ -121,25 +121,20 @@ has 'data' => ( sub _dataInitialize { my $self = shift; - my $xml = new XML::Simple (KeyAttr=>[]); - my $data = $xml->XMLin($self->fileName()); - if (ref($data->{category}) eq "HASH") { - # one element alone - my @categories; - push @categories, $data->{category}; - $data->{category} = undef; - push @{$data->{category}}, @categories; - } + my $xml = new XML::Simple (); + my $data = $xml->XMLin( + $self->fileName(), + ContentKey => '-content', + ForceArray => ['category', 'title', 'module'], + KeyAttr=>{ + title => "xml:lang", + } + ); $self->catLen( scalar(@{$data->{category}}) ); - - if(ref(@{$data->{category}}[0]->{module}) eq "ARRAY") { - $self->modLen( - scalar(@{@{$data->{category}}[0]->{module}}) - ); - } else { - $self->modLen(1); - } + $self->modLen( + scalar(@{@{$data->{category}}[0]->{module}}) + ); return $data; } @@ -234,24 +229,20 @@ sub hasNextCat { sub getNextCat { my $self = shift; - $self->currCat($self->currCat()+1); - if($self->currCat() >= $self->catLen()) { - return 0; - } - - # Reset the Module Count and Mod length for new Category - $self->currMod(-1); - if(ref(@{$self->data()->{category}}[$self->currCat()]->{module}) eq "ARRAY") { + if ($self->hasNextCat()) { + $self->currCat($self->currCat()+1); + # Reset the Module Count and Mod length for new Category + $self->currMod(-1); $self->modLen( scalar(@{@{$self->data()->{category}}[$self->currCat()]->{module}}) ); - } else { - $self->modLen(1); - } - my $cat = @{$self->data()->{category}}[$self->currCat()]; + my $cat = @{$self->data()->{category}}[$self->currCat()]; - return $cat; + return $cat; + } + + return; } #============================================================= @@ -306,15 +297,13 @@ sub getNextMod { my $ret = 0; - $self->currMod($self->currMod()+1); - - if($self->{modLen} == 1) { - $ret = @{$self->data()->{category}}[$self->currCat()]->{module}; - } else { + if ($self->hasNextMod()) { + $self->currMod($self->currMod()+1); $ret = @{@{$self->data()->{category} }[$self->currCat()]->{module}}[$self->currMod()]; + return $ret; } - return $ret; + return; } no Moose; -- cgit v1.2.1