1 #*************************************************************************
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 # Copyright 2000, 2010 Oracle and/or its affiliates.
7 # OpenOffice.org - a multi-platform office productivity suite
9 # This file is part of OpenOffice.org.
11 # OpenOffice.org is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU Lesser General Public License version 3
13 # only, as published by the Free Software Foundation.
15 # OpenOffice.org is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Lesser General Public License version 3 for more details
19 # (a copy is included in the LICENSE file that accompanied this code).
21 # You should have received a copy of the GNU Lesser General Public License
22 # version 3 along with OpenOffice.org. If not, see
23 # <http://www.openoffice.org/license.html>
24 # for a copy of the LGPLv3 License.
26 #*************************************************************************
28 #*************************************************************************
30 # SourceConfigHelper - Perl extension for parsing general info databases
34 #*************************************************************************
36 package SourceConfigHelper
;
46 my @source_config_list; # array of sourceconfig objects
48 #-----------------------------------------------------------------------
50 #-----------------------------------------------------------------------
52 use constant SOURCE_CONFIG_NONE
=> 0;
53 use constant SOURCE_CONFIG_CURRENT_FIRST
=> 1;
54 use constant SOURCE_CONFIG_ENVIRONMENT_FIRST
=> 2;
55 use constant SOURCE_CONFIG_CURRENT_ONLY
=> 3;
56 use constant SOURCE_CONFIG_ENVIRONMENT_ONLY
=> 4;
58 use constant SOURCE_CONFIG_DEFAULT
=> SOURCE_CONFIG_CURRENT_FIRST
;
66 my $class = ref($proto) || $proto;
67 my $init_action = shift;
69 my $SourceConfigCurrent;
70 my $SourceConfigEnvironment;
72 $init_action = SOURCE_CONFIG_DEFAULT
if (!defined ($init_action));
73 if (!eval ($init_action) or ($init_action < SOURCE_CONFIG_NONE
) or ($init_action > SOURCE_CONFIG_ENVIRONMENT_ONLY
)) {
74 croak
("wrong initial parameter: $init_action\n");
77 if ($init_action != SOURCE_CONFIG_NONE
) {
78 my $repositoryHash_ref = {};
79 if ($init_action != SOURCE_CONFIG_ENVIRONMENT_ONLY
) {
80 my $initial_directory = cwd
();
81 my $result = is_repository
($initial_directory, $repositoryHash_ref);
83 $SourceConfigCurrent = SourceConfig
->new($repositoryHash_ref->{REPOSITORY_ROOT
});
86 if ($init_action != SOURCE_CONFIG_CURRENT_ONLY
) {
87 my $source_config = $ENV{SOURCE_ROOT_DIR
} . '/' . SourceConfig
::SOURCE_CONFIG_FILE_NAME
;
88 if (-f
$source_config) {
89 $SourceConfigEnvironment = SourceConfig
->new($source_config);
95 if (($init_action == SOURCE_CONFIG_CURRENT_FIRST
) or ($init_action == SOURCE_CONFIG_CURRENT_ONLY
)) {
96 if (defined ($SourceConfigCurrent)) {
97 push (@source_config_list, $SourceConfigCurrent);
99 if ($init_action == SOURCE_CONFIG_CURRENT_FIRST
) {
100 if (defined ($SourceConfigEnvironment)) {
101 push (@source_config_list, $SourceConfigEnvironment);
105 elsif (($init_action == SOURCE_CONFIG_ENVIRONMENT_FIRST
) or ($init_action == SOURCE_CONFIG_ENVIRONMENT_ONLY
)) {
106 if (defined ($SourceConfigEnvironment)) {
107 push (@source_config_list, $SourceConfigEnvironment);
109 if ($init_action == SOURCE_CONFIG_ENVIRONMENT_FIRST
) {
110 if (defined ($SourceConfigCurrent)) {
111 push (@source_config_list, $SourceConfigCurrent);
117 $self->{SOURCE_CONFIG_LIST
} = \
@source_config_list;
119 bless($self, $class);
125 ############################################################################################
127 sub add_SourceConfig
{
129 my $source_config = shift;
130 push (@
{$self->{SOURCE_CONFIG_LIST
}}, $source_config);
133 ############################################################################################
135 sub get_SourceConfigList
{
137 return @
{$self->{SOURCE_CONFIG_LIST
}};
140 ############################################################################################
142 sub has_SourceConfig
{
145 my $count = @
{$self->{SOURCE_CONFIG_LIST
}};
146 $result = 1 if ($count > 0);
150 ############################################################################################
152 sub get_module_path
{
155 my $function = \
&SourceConfig
::get_module_path
;
157 $result = $self->get_StringResult ($function, $module);
161 ############################################################################################
163 sub get_active_modules
{
165 my $parameter; # empty
166 my $function = \
&SourceConfig
::get_active_modules
;
168 $array_ref = $self->get_ArrayResult ($function, $parameter);
172 ############################################################################################
174 sub get_repositories
{
176 my $parameter; # empty
177 my $function = \
&SourceConfig
::get_repositories
;
179 $array_ref = $self->get_ArrayResult ($function, $parameter);
183 ############################################################################################
185 sub get_module_repository
{
188 my $function = \
&SourceConfig
::get_module_repository
;
190 $result = $self->get_StringResult ($function, $module);
194 ############################################################################################
199 my $function = \
&SourceConfig
::is_active
;
202 $result_ref = $self->get_ResultOfList ($function, $module);
203 my $count = @
$result_ref;
205 foreach my $active (@
$result_ref) {
207 $is_active = $active;
214 ##### private methods #####
216 ############################################################################################
218 # is_repository () : check if the directory is a valid repository
221 # - hash reference, where the output will be stored
223 # output: 0 = FALSE, the directory is no valid repository
224 # 1 = TRUE, the repository root can be found in $repositoryHash_ref->{REPOSITORY_ROOT}
226 ############################################################################################
229 my $directory = shift;
230 my $repositoryHash_ref = shift;
231 $repositoryHash_ref->{INITIAL_DIRECTORY
} = $directory;
232 $repositoryHash_ref->{REPOSITORY_ROOT
} = undef;
233 my $result = RepositoryHelper
::search_via_build_lst
($repositoryHash_ref);
237 ############################################################################################
239 # get_ResultOfList(): give back an array reference from all SourceConfig Objects results
241 # input: - function : reference to the called function of each SourceConfig Object
242 # - parameter : parameter for the called function
244 # output: result : array of all results
246 ############################################################################################
248 sub get_ResultOfList
{
250 my $function = shift;
251 my $parameter = shift;
253 foreach my $source_config (@
{$self->{SOURCE_CONFIG_LIST
}}) {
254 push (@result, &$function ($source_config, $parameter));
259 ############################################################################################
261 # get_StringResult(): give back the first defined result from all SourceConfig Objects
263 # input: - function : reference to the called function of each SourceConfig Object
264 # - parameter : parameter for the called function
266 # output: result : scalar variable (string), undef if no result
268 ############################################################################################
270 sub get_StringResult
{
272 my $function = shift;
273 my $parameter = shift;
275 $result_ref = $self->get_ResultOfList ($function, $parameter);
276 my $count = @
$result_ref;
280 while (($i < $count) and !defined ($value)) { # search the first defined result
281 $value = $$result_ref[$i];
289 ############################################################################################
291 # get_StringResult(): give back a sorted and uniqe array reference of the results
292 # from all SourceConfig Objects
294 # input: - function : reference to the called function of each SourceConfig Object
295 # - parameter : parameter for the called function
297 # output: result : sorted and uniqe array reference
299 ############################################################################################
301 sub get_ArrayResult
{
303 my $function = shift;
304 my $parameter = shift;
307 $result_ref = $self->get_ResultOfList ($function, $parameter);
308 my $count = @
$result_ref;
311 foreach my $module (@
$result_ref) {
312 $moduleHash{$module}++;
314 @modules = sort keys %moduleHash;
321 1; # needed by use or require
327 SourceConfigHelper - Perl extension for handling with SourceConfigObjetcs
331 # example that will read source_config file and return the active repositories
333 use SourceConfigHelper;
335 # Create a new instance:
336 $a = SourceConfigHelper->new();
338 # Get repositories for the actual workspace:
339 $a->get_repositories();
343 SourceConfigHelper is a perl extension to handle more than one objects of SourceConfig
344 to set up a search order for modules.
348 SourceConfigHelper::new()
350 Creates a new instance of SourceConfigHelper. Can be initialized by: default - empty or with a constant of search order. default: the source_config will be taken first from the current repository and second from the environment
351 Possible parameters are:
352 SourceConfigHelper::SOURCE_CONFIG_NONE - no SourceConfig Object will be created
353 SourceConfigHelper::SOURCE_CONFIG_CURRENT_FIRST - use the current repository first
354 SourceConfigHelper::SOURCE_CONFIG_ENVIRONMENT_FIRST - use the repository of the environment first
355 SourceConfigHelper::SOURCE_CONFIG_CURRENT_ONLY - use only the current repository
356 SourceConfigHelper::SOURCE_CONFIG_ENVIRONMENT_ONLY - use only the repository of the environment
358 SourceConfigHelper::get_repositories()
360 Returns sorted list of active repositories for the actual workspace
362 SourceConfigHelper::get_active_modules()
364 Returns a sorted list of active modules
366 SourceConfigHelper::get_all_modules()
368 Returns sorted list of all modules in active repositories.
370 SourceConfigHelper::get_module_path($module)
372 Returns absolute module path. If the module is not active or don't exists, "undef" will be returned.
374 SourceConfigHelper::get_module_repository($module)
376 Returns the module's repository. If the module is not active or don't exists, "undef" will be returned.
378 SourceConfigHelper::is_active()
380 Returns 1 (TRUE) if a module is active
381 Returns 0 (FALSE) if a module is not active
383 SourceConfigHelper::add_SourceConfig($SourceConfigObject)
385 Add the SourceConfigObject to the end of the list
387 SourceConfigHelper::get_SourceConfigList()
389 Return an array of SourceConfigObjects
391 SourceConfigHelper::has_SourceConfig()
393 Returns 1 (TRUE) if one or more SourceConfig Objects is in the list
394 Returns 0 (FALSE) if no SourceConfig Object is in the list (can happen if there is no valid repository)
398 SourceConfigHelper::new()
399 SourceConfigHelper::get_repositories()
400 SourceConfigHelper::get_active_modules()
401 SourceConfigHelper::get_all_modules()
402 SourceConfigHelper::get_module_path($module)
403 SourceConfigHelper::get_module_repository($module)
404 SourceConfigHelper::is_active($module)
405 SourceConfigHelper::add_SourceConfig($SourceConfigObject)
406 SourceConfigHelper::get_SourceConfigList()
407 SourceConfigHelper::has_SourceConfig()
411 Kurt Zenker, kz@openoffice.org