libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / jar / Indexer.java
blob06c5de9fb6d70d6b18ff96d3ff54f3ea1049bb9f
1 /* Indexer.java -- add index.list file to jar
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package gnu.classpath.tools.jar;
41 import gnu.java.net.IndexListParser;
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.Iterator;
50 import java.util.LinkedHashSet;
51 import java.util.StringTokenizer;
52 import java.util.jar.Attributes;
53 import java.util.jar.JarEntry;
54 import java.util.jar.JarFile;
55 import java.util.jar.Manifest;
57 public class Indexer
58 extends Updater
60 private void indexJarFile(StringBuilder result, File fileName,
61 boolean verbose)
62 throws IOException
64 if (verbose)
66 String msg = MessageFormat.format(Messages.getString("Indexer.Indexing"), //$NON-NLS-1$
67 new Object[] { fileName });
68 System.err.println(msg);
70 JarFile jf = new JarFile(fileName);
72 // Index the files in this jar.
73 // The results look a little better if we keep them
74 // in insertion order.
75 LinkedHashSet<String> entries = new LinkedHashSet<String>();
76 Enumeration e = jf.entries();
77 while (e.hasMoreElements())
79 JarEntry entry = (JarEntry) e.nextElement();
80 String name = entry.getName();
81 if (name.startsWith("META-INF/")) //$NON-NLS-1$
82 continue;
83 int index = name.lastIndexOf('/');
84 if (index != -1)
85 name = name.substring(0, index);
86 entries.add(name);
88 if (! entries.isEmpty())
90 result.append(fileName);
91 // Any line ending will do.
92 result.append('\n');
93 Iterator i = entries.iterator();
94 while (i.hasNext())
96 result.append(i.next());
97 result.append('\n');
99 // Paragraph break.
100 result.append('\n');
103 // Now read pointed-to jars.
104 Manifest m = jf.getManifest();
105 if (m != null)
107 File parent = fileName.getParentFile();
108 Attributes attrs = m.getMainAttributes();
109 String jars = attrs.getValue(Attributes.Name.CLASS_PATH);
110 if (jars != null)
112 StringTokenizer st = new StringTokenizer(jars, " "); //$NON-NLS-1$
113 while (st.hasMoreTokens())
115 String name = st.nextToken();
116 indexJarFile(result, new File(parent, name), verbose);
121 jf.close();
124 protected void writeCommandLineEntries(Main parameters, OutputStream os)
125 throws IOException
127 // This is a pretty lame design. We know the super call will
128 // only have side effects and won't actually write anything important.
129 super.writeCommandLineEntries(parameters, os);
131 // Now compute our index file and write it.
132 StringBuilder contents = new StringBuilder();
133 indexJarFile(contents, parameters.archiveFile, parameters.verbose);
134 if (contents.length() != 0)
136 // Insert in reverse order to avoid computing anything.
137 contents.insert(0, "1.0\n\n"); //$NON-NLS-1$
138 contents.insert(0, IndexListParser.JAR_INDEX_VERSION_KEY);
139 ByteArrayInputStream in
140 = new ByteArrayInputStream(contents.toString().getBytes());
141 writeFile(false, in, IndexListParser.JAR_INDEX_FILE, parameters.verbose);