1#!@PERL@
2#
3# mkcollections.pl - small perl script to convert GNU Classpath's
4#                    Collection classes into its own package for java 1.1
5#
6# USAGE: mkcollections.pl <Destination-Path>
7#
8# Copyright (C) 2000 Free Software Foundation, Inc.
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2, or (at your option)
13# any later version.
14#
15# This program 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 General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; see the file COPYING.  If not, write to
22# the Free Software Foundation, 51 Franklin St, Fifth Floor,
23# Boston, MA  02110-1301  USA
24
25my $destpath=@COLLECTIONS_PREFIX@;
26my $classpath=pop;
27my @javalangclasses=qw(UnsupportedOperationException
28		       Comparable
29		       Iterable);
30my @javautilclasses=qw(AbstractCollection
31		       AbstractList
32		       AbstractMap
33		       AbstractSequentialList
34		       AbstractSet
35		       ArrayList
36		       Arrays
37		       List
38		       Collection
39		       Collections
40		       Comparator
41		       ConcurrentModificationException
42		       HashMap
43		       HashSet
44		       Hashtable
45		       Iterator
46		       LinkedList
47		       ListIterator
48		       Map
49		       NoSuchElementException
50		       Random
51		       RandomAccess
52		       Set
53		       SortedMap
54		       SortedSet
55		       TreeMap
56		       TreeSet
57		       Vector);
58my @externalclasses=qw(AbstractQueue
59		       ArrayDeque
60		       Deque
61		       NavigableMap
62		       NavigableSet
63		       Queue);
64
65my $destPkg = $destpath;
66$destPkg  =~ s!/!.!g;
67
68my %imports = ( "Collections" => [ "Enumeration" ],
69		"Hashtable" => [ "Dictionary", "Enumeration" ],
70		"Vector" => [ "Enumeration" ]);
71
72
73sub mymkdir ($)
74  {
75    my ($dir) = @_;
76    if ($dir =~ /^(.*)\/\w+$/)
77      {
78	$parentdir = "$1";
79	if (! (-d $parentdir))
80	  {
81	    mymkdir ($parentdir);
82	  }
83      }
84    print "$dir";
85    mkdir ($dir, 0777);
86  }
87
88sub convert($$$) {
89    my ($file, $infile, $outfile) = @_;
90
91    open (INPUT, "<$infile") || die "Could not open ", $infile, " for reading\n";
92
93    my $dir = "$outfile";
94    $dir =~ /^(.*)\/\S+\.java$/ and
95	$dir = "$1";
96    if (! (-d $dir)) {
97	mymkdir ($dir);
98    }
99
100    open (OUTPUT, ">$outfile") || die "Could not open ", $outfile, " for writing\n";
101    print OUTPUT <<'EOF';
102/* This file was converted from the GNU Classpath Project by a
103 * perl script, written by Jochen Hoenicke <jochen\@gnu.org>.
104 */
105EOF
106    while (<INPUT>) {
107	$_ = "package $destPkg;\n" if ($_ =~ /^package java.(lang|util);$/);
108
109	next if $_ =~ /^import java.io.Object.*putStream.*Field;$/;
110	next if $_ =~ /^import java.io.ObjectStreamField;$/;
111
112	for $clazz (@javalangclasses) {
113	    $_ =~ s/java.lang.$clazz/$clazz/g;
114	}
115	for $clazz (@javautilclasses) {
116	    $_ =~ s/java.util.$clazz/$clazz/g;
117	}
118	for $clazz (@externalclasses) {
119	    $_ =~ s/java.util.$clazz/$clazz/g;
120	}
121
122	$_ =~ s/abstract (interface)/$1/g;
123
124	print OUTPUT $_;
125	if ($_ =~ /^package $destPkg;$/
126	    && exists $imports{$file}) {
127	    for $imp (@{$imports{$file}}) {
128		print OUTPUT "import java.util.$imp;\n";
129	    }
130	}
131    }
132    close (OUTPUT);
133    close (INPUT);
134}
135
136my $file;
137
138for $file (@javalangclasses) {
139    my $infile = "$classpath/java/lang/$file.java";
140    my $outfile = "$destpath/$file.java";
141    print "$outfile\n";
142    convert ($file, $infile, $outfile);
143}
144
145for $file (@javautilclasses) {
146    my $infile = "$classpath/java/util/$file.java";
147    my $outfile = "$destpath/$file.java";
148    print "$outfile\n";
149    convert ($file, $infile, $outfile);
150}
151
152for $file (@externalclasses) {
153    my $infile = "$classpath/external/jsr166/java/util/$file.java";
154    my $outfile = "$destpath/$file.java";
155    print "$outfile\n";
156    convert ($file, $infile, $outfile);
157}
158
159