| 1 | # ***** BEGIN LICENSE BLOCK ***** | ||
| 2 | # Zimbra Collaboration Suite Server | ||
| 3 | # Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Zimbra, Inc. | ||
| 4 | # | ||
| 5 | # The contents of this file are subject to the Zimbra Public License | ||
| 6 | # Version 1.3 ("License"); you may not use this file except in | ||
| 7 | # compliance with the License. You may obtain a copy of the License at | ||
| 8 | # http://www.zimbra.com/license. | ||
| 9 | # | ||
| 10 | # Software distributed under the License is distributed on an "AS IS" | ||
| 11 | # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. | ||
| 12 | # ***** END LICENSE BLOCK ***** | ||
| 13 | |||
| 14 | package ZCS::LocalConfig::File; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | use XML::Parser (); | ||
| 19 | use base qw(ZCS::LocalConfig::_base); | ||
| 20 | |||
| 21 | our $VERSION = 1.0; | ||
| 22 | |||
| 23 | { | ||
| 24 | my $File; | ||
| 25 | |||
| 26 | sub file { | ||
| 27 | my ( $self, $value ) = @_; | ||
| 28 | if ( ref($self) ) { | ||
| 29 | $self->{file} = $value if $value; | ||
| 30 | return $self->{file} if $self->{file}; | ||
| 31 | } | ||
| 32 | $File = $value if $value; | ||
| 33 | return $File || "/opt/zimbra/conf/localconfig.xml"; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | sub load { | ||
| 38 | my ($self) = @_; | ||
| 39 | |||
| 40 | my $file = $self->file; | ||
| 41 | my $parser = XML::Parser->new( | ||
| 42 | ErrorContext => 2, | ||
| 43 | Style => "Objects", | ||
| 44 | ); | ||
| 45 | |||
| 46 | # parsefile may die | ||
| 47 | local ($@); | ||
| 48 | my $parsed = eval { $parser->parsefile($file); }; | ||
| 49 | if ($@) { | ||
| 50 | chomp( my $err = $@ ); | ||
| 51 | return $self->_set_err( 1, "load: parsefile '$file' failed: $@" ); | ||
| 52 | } | ||
| 53 | |||
| 54 | my @tree = @{ ( $parsed || [] ) }; | ||
| 55 | my $conf_tree = []; | ||
| 56 | foreach my $o (@tree) { | ||
| 57 | if ( ref($o) =~ /::localconfig$/ ) { | ||
| 58 | $conf_tree = $o->Kids; | ||
| 59 | last; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | my %lc; | ||
| 64 | foreach my $o (@$conf_tree) { | ||
| 65 | |||
| 66 | # inspect ZCS::LocalConfig::File::key objects | ||
| 67 | next unless ref($o) =~ /::key$/; | ||
| 68 | $lc{ $o->name } = $o->value; | ||
| 69 | } | ||
| 70 | |||
| 71 | $self->_set_err( 0, "load: no data returned" ) unless (%lc); | ||
| 72 | $self->_set_conf( \%lc ); | ||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | package ZCS::LocalConfig::File::Characters; | ||
| 77 | sub Text { return $_[0]->{Text}; } | ||
| 78 | |||
| 79 | package ZCS::LocalConfig::File::localconfig; | ||
| 80 | sub Kids { return $_[0]->{Kids}; } | ||
| 81 | |||
| 82 | package ZCS::LocalConfig::File::key; | ||
| 83 | sub Kids { return $_[0]->{Kids}; } | ||
| 84 | sub name { return $_[0]->{name}; } | ||
| 85 | |||
| 86 | sub value { | ||
| 87 | foreach my $o ( @{ $_[0]->{Kids} } ) { | ||
| 88 | next unless ref($o) =~ /::value$/; | ||
| 89 | return $o->value; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | package ZCS::LocalConfig::File::value; | ||
| 94 | |||
| 95 | sub value { | ||
| 96 | foreach my $o ( @{ $_[0]->{Kids} } ) { | ||
| 97 | next unless ref($o) =~ /::Characters$/; | ||
| 98 | return $o->Text; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; |