| 1 | #/*==================================================================== | ||
| 2 | # * Babel Objects, Version 1.0 | ||
| 3 | # * ==================================================================== | ||
| 4 | # * | ||
| 5 | # * Copyright (c) 2000 The Babel Objects Network. All rights reserved. | ||
| 6 | # * | ||
| 7 | # * This source file is subject to version 1.1 of The Babel Objects | ||
| 8 | # * License, that is bundled with this package in the file LICENSE, | ||
| 9 | # * and is available through the world wide web at : | ||
| 10 | # * | ||
| 11 | # * http://www.BabelObjects.Org/law/license/1.1.txt | ||
| 12 | # * | ||
| 13 | # * If you did not receive a copy of the Babel Objects license and are | ||
| 14 | # * unable to obtain it through the world wide web, please send a note | ||
| 15 | # * to license@BabelObjects.Org so we can mail you a copy immediately. | ||
| 16 | # * | ||
| 17 | # * -------------------------------------------------------------------- | ||
| 18 | # * | ||
| 19 | # * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | ||
| 20 | # * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
| 21 | # * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 22 | # * DISCLAIMED. IN NO EVENT SHALL THE BABEL OBJECTS NETWORK OR | ||
| 23 | # * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 24 | # * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| 25 | # * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
| 26 | # * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
| 27 | # * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| 28 | # * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
| 29 | # * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | # * SUCH DAMAGE. | ||
| 31 | # * | ||
| 32 | # * ==================================================================== | ||
| 33 | # * | ||
| 34 | # * This software consists of voluntary contributions made by many | ||
| 35 | # * individuals on behalf of The Babel Objects Network. For more | ||
| 36 | # * information on The Babel Objects Network, please see | ||
| 37 | # * <http://www.BabelObjects.org/>. | ||
| 38 | # * | ||
| 39 | # */ | ||
| 40 | package BabelObjects::Component::Data::Configurator; | ||
| 41 | |||
| 42 | use Carp; | ||
| 43 | use strict; | ||
| 44 | |||
| 45 | use BabelObjects::Util::Dvlpt::Log; | ||
| 46 | use BabelObjects::Util::Facility::XMLFacility; | ||
| 47 | |||
| 48 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD); | ||
| 49 | |||
| 50 | require Exporter; | ||
| 51 | require AutoLoader; | ||
| 52 | |||
| 53 | @ISA = qw(Exporter AutoLoader); | ||
| 54 | # Items to export into callers namespace by default. Note: do not export | ||
| 55 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 56 | # Do not simply export all your public functions/methods/constants. | ||
| 57 | @EXPORT = qw( | ||
| 58 | ); | ||
| 59 | $VERSION = '1.00'; | ||
| 60 | |||
| 61 | my $aLog; | ||
| 62 | |||
| 63 | my %fields = ( | ||
| 64 | runData => undef | ||
| 65 | ); | ||
| 66 | |||
| 67 | sub new { | ||
| 68 | my $proto = shift; | ||
| 69 | my $args = shift; | ||
| 70 | |||
| 71 | my $class = ref($proto) || $proto; | ||
| 72 | |||
| 73 | my $self = { | ||
| 74 | _permitted => \%fields, | ||
| 75 | %fields, | ||
| 76 | }; | ||
| 77 | |||
| 78 | bless ($self, $class); | ||
| 79 | |||
| 80 | my %parameters = %$args; | ||
| 81 | |||
| 82 | $aLog = new BabelObjects::Util::Dvlpt::Log(); | ||
| 83 | #$aLog->log("\n-- BabelObjects::Component::Data::Configurator --"); | ||
| 84 | |||
| 85 | foreach (keys %parameters) { | ||
| 86 | # the following lines are useful to verify argument values | ||
| 87 | #$aLog->log("Before : $_ = ".$parameters{$_}); | ||
| 88 | $self->$_($parameters{$_}); | ||
| 89 | #$aLog->log("After : $_ = ".$self->$_); | ||
| 90 | } | ||
| 91 | |||
| 92 | return $self; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub getBoInfo { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | # this method is a little silly because the information | ||
| 99 | # is redundant between here and xml file | ||
| 100 | my $doc = $self->runData->getConfParameters(); | ||
| 101 | my $context = $self->runData->getParameter("context"); | ||
| 102 | |||
| 103 | #$aLog->log("getBoInfo"); | ||
| 104 | #$aLog->log(" Doc = $doc"); | ||
| 105 | #$aLog->log(" Context = $context"); | ||
| 106 | |||
| 107 | my %attributes; | ||
| 108 | |||
| 109 | my $xmlHelper = new BabelObjects::Util::Facility::XMLFacility(); | ||
| 110 | my @parameters = $xmlHelper->getElementsByPath($doc, "$context/parameter", "([^\/]+)\/([^\/]+)"); | ||
| 111 | |||
| 112 | foreach (@parameters) { | ||
| 113 | #$aLog->log(" Name = $_"); | ||
| 114 | my $nodes = $_->getElementsByTagName("*",0); | ||
| 115 | my $name = $nodes->item(0)->getNodeName; | ||
| 116 | #$aLog->log(" Name = $name"); | ||
| 117 | $attributes{lc($name)} = lc($name); | ||
| 118 | } | ||
| 119 | |||
| 120 | return %attributes; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub getValue { | ||
| 124 | my $self = shift; | ||
| 125 | my $key = shift; | ||
| 126 | |||
| 127 | my $context = $self->runData->getParameter("context"); | ||
| 128 | |||
| 129 | #$aLog->log("getValue"); | ||
| 130 | #$aLog->log(" Context = $context"); | ||
| 131 | #$aLog->log(" Key = $key"); | ||
| 132 | |||
| 133 | # A modifier quand la librairie aura été externalisée | ||
| 134 | my $doc = $self->runData->getConfParameters(); | ||
| 135 | my $xmlHelper = new BabelObjects::Util::Facility::XMLFacility(); | ||
| 136 | |||
| 137 | return ($xmlHelper->getElementsByPath($doc, $context."/parameter/".$key))[0] | ||
| 138 | ->getFirstChild()->getData(); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub getValueOld { | ||
| 142 | my $self = shift; | ||
| 143 | my $key = shift; | ||
| 144 | my $x; | ||
| 145 | |||
| 146 | my $doc = $self->parameters; | ||
| 147 | my $context = $self->context; | ||
| 148 | |||
| 149 | foreach $x ($doc->$context->test) { | ||
| 150 | my $name = $x->name->getString; | ||
| 151 | if (lc($name) eq $key) { | ||
| 152 | return $x->value->getString; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | return "PB"; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub AUTOLOAD { | ||
| 160 | my $self = shift; | ||
| 161 | my $type = ref($self) or croak "$self is not an object"; | ||
| 162 | |||
| 163 | my $name = $AUTOLOAD; | ||
| 164 | $name =~ s/.*://; # strip fully-qualified portion | ||
| 165 | |||
| 166 | unless (exists $self->{_permitted}->{$name} ) { | ||
| 167 | # croak "Can't access `$name' field in class $type"; | ||
| 168 | # On intercepte ici les erreurs liées aux tentatives d'appel | ||
| 169 | # des méthodes inexistantes | ||
| 170 | #print "Dispatcher AUTOLOAD = $AUTOLOAD\n"; | ||
| 171 | return $AUTOLOAD; | ||
| 172 | } | ||
| 173 | |||
| 174 | if (@_) { | ||
| 175 | return $self->{$name} = shift; | ||
| 176 | } else { | ||
| 177 | return $self->{$name}; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | 1; | ||
| 182 | __END__ |