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