xref: /AOO41X/main/solenv/bin/modules/installer/converter.pm (revision ff0525f24f03981d56b7579b645949f111420994)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package installer::converter;
25
26use installer::globals;
27
28#############################
29# Converter
30#############################
31
32sub convert_array_to_hash
33{
34    my ($arrayref) = @_;
35
36    my %newhash = ();
37
38    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
39    {
40        my $line = ${$arrayref}[$i];
41
42        if ( $line =~ /^\s*([\w-]+?)\s+(.*?)\s*$/ )
43        {
44            my $key = $1;
45            my $value = $2;
46            $newhash{$key} = $value;
47        }
48    }
49
50    return \%newhash;
51}
52
53sub convert_hash_into_array
54{
55    my ($hashref) = @_;
56
57    my @array = ();
58    my $key;
59
60    foreach $key (keys %{$hashref})
61    {
62        my $value = $hashref->{$key};
63        my $input = "$key = $value\n";
64        push(@array ,$input);
65    }
66
67    return \@array
68}
69
70#############################################################################
71# Converting a string list with separator $listseparator
72# into an array
73#############################################################################
74
75sub convert_stringlist_into_array_without_linebreak_and_quotes
76{
77    my ( $includestringref, $listseparator ) = @_;
78
79    my @newarray = ();
80    my $first;
81    my $last = ${$includestringref};
82
83    while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
84    {
85        $first = $1;
86        $last = $2;
87        $first =~ s/\"//g;
88        push(@newarray, $first);
89    }
90
91    $last =~ s/\"//g;
92    push(@newarray, $last);
93
94    return \@newarray;
95}
96
97#############################################################################
98# Converting a string list with separator $listseparator
99# into an array
100#############################################################################
101
102sub convert_stringlist_into_array
103{
104    my ( $includestringref, $listseparator ) = @_;
105
106    my @newarray = ();
107    my $first;
108    my $last = ${$includestringref};
109
110    while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
111    {
112        $first = $1;
113        $last = $2;
114        # Problem with two directly following listseparators. For example a path with two ";;" directly behind each other
115        $first =~ s/^$listseparator//;
116        push(@newarray, "$first\n");
117    }
118
119    push(@newarray, "$last\n");
120
121    return \@newarray;
122}
123
124#############################################################################
125# Converting a string list with separator $listseparator
126# into an array
127#############################################################################
128
129sub convert_stringlist_into_array_without_newline
130{
131    my ( $includestringref, $listseparator ) = @_;
132
133    my @newarray = ();
134    my $first;
135    my $last = ${$includestringref};
136
137    while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
138    {
139        $first = $1;
140        $last = $2;
141        push(@newarray, "$first");
142    }
143
144    push(@newarray, "$last");
145
146    return \@newarray;
147}
148
149#############################################################################
150# Converting a string list with separator $listseparator
151# into a hash with values 1.
152#############################################################################
153
154sub convert_stringlist_into_hash
155{
156    my ( $includestringref, $listseparator ) = @_;
157
158    my %newhash = ();
159    my $first;
160    my $last = ${$includestringref};
161
162    while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/) # "$" for minimal matching
163    {
164        $first = $1;
165        $last = $2;
166        $newhash{$first} = 1;
167    }
168
169    $newhash{$last} = 1;
170
171    return \%newhash;
172}
173
174#############################################################################
175# Converting a string list with separator $listseparator
176# into an array
177#############################################################################
178
179sub convert_whitespace_stringlist_into_array
180{
181    my ( $includestringref ) = @_;
182
183    my @newarray = ();
184    my $first;
185    my $last = ${$includestringref};
186
187    while ( $last =~ /^\s*(\S+?)\s+(\S+)\s*$/)  # "$" for minimal matching
188    {
189        $first = $1;
190        $last = $2;
191        push(@newarray, "$first\n");
192    }
193
194    push(@newarray, "$last\n");
195
196    return \@newarray;
197}
198
199#############################################################################
200# Converting an array into a comma separated string
201#############################################################################
202
203sub convert_array_to_comma_separated_string
204{
205    my ( $arrayref ) = @_;
206
207    my $newstring = "";
208
209    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
210    {
211        my $arrayentry = ${$arrayref}[$i];
212        $arrayentry =~ s/\s*$//;
213        $newstring = $newstring . $arrayentry . ",";
214    }
215
216    $newstring =~ s/\,\s*$//;
217
218    return $newstring;
219}
220
221#############################################################################
222# Converting an array into a space separated string
223#############################################################################
224
225sub convert_array_to_space_separated_string
226{
227    my ( $arrayref ) = @_;
228
229    my $newstring = "";
230
231    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
232    {
233        my $arrayentry = ${$arrayref}[$i];
234        $arrayentry =~ s/\s*$//;
235        $newstring = $newstring . $arrayentry . " ";
236    }
237
238    $newstring =~ s/\s*$//;
239
240    return $newstring;
241}
242
243#############################################################################
244# The file name contains for some files "/". If this programs runs on
245# a windows platform, this has to be converted to "\".
246#############################################################################
247
248sub convert_slash_to_backslash
249{
250    my ($filesarrayref) = @_;
251
252    for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
253    {
254        my $onefile = ${$filesarrayref}[$i];
255        if ( $onefile->{'Name'} ) { $onefile->{'Name'} =~ s/\//\\/g; }
256    }
257}
258
259############################################################################
260# Creating a copy of an existing file object
261# No converter
262############################################################################
263
264sub copy_item_object
265{
266    my ($olditemhashref, $newitemhashref) = @_;
267
268    foreach $key (keys %{$olditemhashref})
269    {
270        my $value = $olditemhashref->{$key};
271        $newitemhashref->{$key} = $value;
272    }
273}
274
275#################################################################
276# Windows pathes must not contain the following structure:
277# c:\dirA\dirB\..\dirC
278# This has to be exchanged to
279# c:\dirA\dirC
280#################################################################
281
282sub make_path_conform
283{
284    my ( $path ) = @_;
285
286    my $oldpath = $path;
287
288    while ( $path =~ /(^.*)(\Q$installer::globals::separator\E.*?[^\.])(\Q$installer::globals::separator\E\.\.)(\Q$installer::globals::separator\E.*$)/ )
289    {
290        my $part1 = $1;
291        my $part2 = $4;
292
293        # $2 must not end with a "." ! Problem with "..\.."
294
295        $path = $part1 . $part2;
296    }
297
298    return $path;
299}
300
301#################################################################
302# Copying an item collector
303# A reference to an array consisting of references to hashes.
304#################################################################
305
306sub copy_collector
307{
308    my ( $oldcollector ) = @_;
309
310    my @newcollector = ();
311
312    for ( my $i = 0; $i <= $#{$oldcollector}; $i++ )
313    {
314        my %newhash = ();
315        my $key;
316
317        foreach $key (keys %{${$oldcollector}[$i]})
318        {
319            $newhash{$key} = ${$oldcollector}[$i]->{$key};
320        }
321
322        push(@newcollector, \%newhash);
323    }
324
325    return \@newcollector;
326}
327
328#################################################################
329# Copying an array
330#################################################################
331
332sub copy_array_from_references
333{
334    my ( $arrayref ) = @_;
335
336    my @newarray = ();
337
338    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
339    {
340        push(@newarray, ${$arrayref}[$i]);
341    }
342
343    return \@newarray;
344}
345
346###########################################################
347# Copying a hash
348###########################################################
349
350sub copy_hash_from_references
351{
352    my ($hashref) = @_;
353
354    my %newhash = ();
355    my $key;
356
357    foreach $key (keys %{$hashref})
358    {
359        $newhash{$key} = $hashref->{$key};
360    }
361
362    return \%newhash;
363}
364
365#################################################################
366# Combining two arrays, first wins
367#################################################################
368
369sub combine_arrays_from_references_first_win
370{
371    my ( $arrayref1, $arrayref2 ) = @_;
372
373    my $hashref1 = convert_array_to_hash($arrayref1);
374    my $hashref2 = convert_array_to_hash($arrayref2);
375    my %commonhash = ();
376    my @newarray = ();
377
378    # starting with second hash
379    foreach my $key ( keys %{$hashref2} ) { $commonhash{$key} = $hashref2->{$key}; }
380    # overwriting with first hash
381    foreach my $key ( keys %{$hashref1} ) { $commonhash{$key} = $hashref1->{$key}; }
382
383    # Creating the new array
384    foreach my $key ( keys %commonhash ) { push(@newarray, "$key $commonhash{$key}\n"); }
385
386    return \@newarray;
387}
388
389#################################################################
390# Combining two arrays
391#################################################################
392
393sub combine_arrays_from_references
394{
395    my ( $arrayref1, $arrayref2 ) = @_;
396
397    my @newarray = ();
398
399    for ( my $i = 0; $i <= $#{$arrayref1}; $i++ )
400    {
401        push(@newarray, ${$arrayref1}[$i]);
402    }
403
404    for ( my $i = 0; $i <= $#{$arrayref2}; $i++ )
405    {
406        push(@newarray, ${$arrayref2}[$i]);
407    }
408
409    return \@newarray;
410}
411
412#################################################################
413# Returning the current ending number of a directory
414#################################################################
415
416sub get_number_from_directory
417{
418    my ( $directory ) = @_;
419
420    my $number = 0;
421
422    if ( $directory =~ /\_(\d+)\s*$/ )
423    {
424        $number = $1;
425    }
426
427    return $number;
428}
429
430#################################################################
431# Replacing separators, that are included into quotes
432#################################################################
433
434sub replace_masked_separator
435{
436    my ($string, $separator, $replacementstring) = @_;
437
438    $string =~ s/\\\Q$separator\E/$replacementstring/g;
439
440    return $string;
441}
442
443#################################################################
444# Resolving separators, that were replaced
445# in function mask_separator_in_quotes
446#################################################################
447
448sub resolve_masked_separator
449{
450    my ($arrayref, $separator, $replacementstring) = @_;
451
452    for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
453    {
454        ${$arrayref}[$i] =~ s/$replacementstring/$separator/g
455    }
456}
457
4581;
459