1 |
779 |
jeremybenn |
/* Indexer.java -- add index.list file to jar
|
2 |
|
|
Copyright (C) 2006, 2008 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.classpath.tools.jar;
|
40 |
|
|
|
41 |
|
|
import gnu.java.net.IndexListParser;
|
42 |
|
|
|
43 |
|
|
import java.io.ByteArrayInputStream;
|
44 |
|
|
import java.io.File;
|
45 |
|
|
import java.io.IOException;
|
46 |
|
|
import java.io.OutputStream;
|
47 |
|
|
import java.text.MessageFormat;
|
48 |
|
|
import java.util.Enumeration;
|
49 |
|
|
import java.util.LinkedHashSet;
|
50 |
|
|
import java.util.StringTokenizer;
|
51 |
|
|
import java.util.jar.Attributes;
|
52 |
|
|
import java.util.jar.JarEntry;
|
53 |
|
|
import java.util.jar.JarFile;
|
54 |
|
|
import java.util.jar.Manifest;
|
55 |
|
|
|
56 |
|
|
public class Indexer
|
57 |
|
|
extends Updater
|
58 |
|
|
{
|
59 |
|
|
private void indexJarFile(StringBuilder result, File fileName,
|
60 |
|
|
boolean verbose)
|
61 |
|
|
throws IOException
|
62 |
|
|
{
|
63 |
|
|
if (verbose)
|
64 |
|
|
{
|
65 |
|
|
String msg = MessageFormat.format(Messages.getString("Indexer.Indexing"), //$NON-NLS-1$
|
66 |
|
|
new Object[] { fileName });
|
67 |
|
|
System.err.println(msg);
|
68 |
|
|
}
|
69 |
|
|
JarFile jf = new JarFile(fileName);
|
70 |
|
|
|
71 |
|
|
// Index the files in this jar.
|
72 |
|
|
// The results look a little better if we keep them
|
73 |
|
|
// in insertion order.
|
74 |
|
|
LinkedHashSet<String> entries = new LinkedHashSet<String>();
|
75 |
|
|
Enumeration e = jf.entries();
|
76 |
|
|
while (e.hasMoreElements())
|
77 |
|
|
{
|
78 |
|
|
JarEntry entry = (JarEntry) e.nextElement();
|
79 |
|
|
String name = entry.getName();
|
80 |
|
|
if (name.startsWith("META-INF/")) //$NON-NLS-1$
|
81 |
|
|
continue;
|
82 |
|
|
int index = name.lastIndexOf('/');
|
83 |
|
|
if (index != -1)
|
84 |
|
|
name = name.substring(0, index);
|
85 |
|
|
entries.add(name);
|
86 |
|
|
}
|
87 |
|
|
if (! entries.isEmpty())
|
88 |
|
|
{
|
89 |
|
|
result.append(fileName);
|
90 |
|
|
// Any line ending will do.
|
91 |
|
|
result.append('\n');
|
92 |
|
|
for (String s : entries)
|
93 |
|
|
{
|
94 |
|
|
result.append(s);
|
95 |
|
|
result.append('\n');
|
96 |
|
|
}
|
97 |
|
|
// Paragraph break.
|
98 |
|
|
result.append('\n');
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
// Now read pointed-to jars.
|
102 |
|
|
Manifest m = jf.getManifest();
|
103 |
|
|
if (m != null)
|
104 |
|
|
{
|
105 |
|
|
File parent = fileName.getParentFile();
|
106 |
|
|
Attributes attrs = m.getMainAttributes();
|
107 |
|
|
String jars = attrs.getValue(Attributes.Name.CLASS_PATH);
|
108 |
|
|
if (jars != null)
|
109 |
|
|
{
|
110 |
|
|
StringTokenizer st = new StringTokenizer(jars, " "); //$NON-NLS-1$
|
111 |
|
|
while (st.hasMoreTokens())
|
112 |
|
|
{
|
113 |
|
|
String name = st.nextToken();
|
114 |
|
|
indexJarFile(result, new File(parent, name), verbose);
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
jf.close();
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
protected void writeCommandLineEntries(Main parameters, OutputStream os)
|
123 |
|
|
throws IOException
|
124 |
|
|
{
|
125 |
|
|
// This is a pretty lame design. We know the super call will
|
126 |
|
|
// only have side effects and won't actually write anything important.
|
127 |
|
|
super.writeCommandLineEntries(parameters, os);
|
128 |
|
|
|
129 |
|
|
// Now compute our index file and write it.
|
130 |
|
|
StringBuilder contents = new StringBuilder();
|
131 |
|
|
indexJarFile(contents, parameters.archiveFile, parameters.verbose);
|
132 |
|
|
if (contents.length() != 0)
|
133 |
|
|
{
|
134 |
|
|
// Insert in reverse order to avoid computing anything.
|
135 |
|
|
contents.insert(0, "1.0\n\n"); //$NON-NLS-1$
|
136 |
|
|
contents.insert(0, IndexListParser.JAR_INDEX_VERSION_KEY);
|
137 |
|
|
ByteArrayInputStream in
|
138 |
|
|
= new ByteArrayInputStream(contents.toString().getBytes());
|
139 |
|
|
writeFile(false, in, IndexListParser.JAR_INDEX_FILE, parameters.verbose);
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
}
|