001 /* Manifest.java -- Reads, writes and manipulates jar manifest files 002 Copyright (C) 2000, 2004 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 package java.util.jar; 039 040 import gnu.java.util.jar.JarUtils; 041 042 import java.io.IOException; 043 import java.io.InputStream; 044 import java.io.OutputStream; 045 import java.util.Hashtable; 046 import java.util.Map; 047 048 /** 049 * Reads, writes and manipulaties jar manifest files. 050 * XXX 051 * 052 * @since 1.2 053 * @author Mark Wielaard (mark@klomp.org) 054 */ 055 public class Manifest implements Cloneable 056 { 057 // Fields 058 059 /** The main attributes of the manifest (jar file). */ 060 private final Attributes mainAttr; 061 062 /** A map of atrributes for all entries described in this Manifest. */ 063 private final Map<String, Attributes> entries; 064 065 // Constructors 066 067 /** 068 * Creates a new empty Manifest. 069 */ 070 public Manifest() 071 { 072 mainAttr = new Attributes(); 073 entries = new Hashtable<String, Attributes>(); 074 } 075 076 /** 077 * Creates a Manifest from the supplied input stream. 078 * 079 * @see #read(InputStream) 080 * @see #write(OutputStream) 081 * 082 * @param in the input stream to read the manifest from 083 * @exception IOException when an i/o exception occurs or the input stream 084 * does not describe a valid manifest 085 */ 086 public Manifest(InputStream in) throws IOException 087 { 088 this(); 089 read(in); 090 } 091 092 /** 093 * Creates a Manifest from another Manifest. 094 * Makes a deep copy of the main attributes, but a shallow copy of 095 * the other entries. This means that you can freely add, change or remove 096 * the main attributes or the entries of the new manifest without effecting 097 * the original manifest, but adding, changing or removing attributes from 098 * a particular entry also changes the attributes of that entry in the 099 * original manifest. 100 * 101 * @see #clone() 102 * @param man the Manifest to copy from 103 */ 104 public Manifest(Manifest man) 105 { 106 mainAttr = new Attributes(man.getMainAttributes()); 107 entries = new Hashtable<String, Attributes>(man.getEntries()); 108 } 109 110 // Methods 111 112 /** 113 * Gets the main attributes of this Manifest. 114 */ 115 public Attributes getMainAttributes() 116 { 117 return mainAttr; 118 } 119 120 /** 121 * Gets a map of entry Strings to Attributes for all the entries described 122 * in this manifest. Adding, changing or removing from this entries map 123 * changes the entries of this manifest. 124 */ 125 public Map<String, Attributes> getEntries() 126 { 127 return entries; 128 } 129 130 /** 131 * Returns the Attributes associated with the Entry. 132 * <p> 133 * Implemented as: 134 * <code>return (Attributes)getEntries().get(entryName)</code> 135 * 136 * @param entryName the name of the entry to look up 137 * @return the attributes associated with the entry or null when none 138 */ 139 public Attributes getAttributes(String entryName) 140 { 141 return (Attributes) getEntries().get(entryName); 142 } 143 144 /** 145 * Clears the main attributes and removes all the entries from the 146 * manifest. 147 */ 148 public void clear() 149 { 150 mainAttr.clear(); 151 entries.clear(); 152 } 153 154 /** 155 * Read and merge a <code>Manifest</code> from the designated input stream. 156 * 157 * @param in the input stream to read from. 158 * @throws IOException if an I/O related exception occurs during the process. 159 */ 160 public void read(InputStream in) throws IOException 161 { 162 JarUtils.readMFManifest(getMainAttributes(), getEntries(), in); 163 } 164 165 /** 166 * Writes the contents of this <code>Manifest</code> to the designated 167 * output stream. Line-endings are platform-independent and consist of the 168 * 2-codepoint sequence <code>0x0D</code> and <code>0x0A</code>. 169 * 170 * @param out the output stream to write this <code>Manifest</code> to. 171 * @throws IOException if an I/O related exception occurs during the process. 172 */ 173 public void write(OutputStream out) throws IOException 174 { 175 JarUtils.writeMFManifest(getMainAttributes(), getEntries(), out); 176 } 177 178 /** 179 * Makes a deep copy of the main attributes, but a shallow copy of 180 * the other entries. This means that you can freely add, change or remove 181 * the main attributes or the entries of the new manifest without effecting 182 * the original manifest, but adding, changing or removing attributes from 183 * a particular entry also changes the attributes of that entry in the 184 * original manifest. Calls <CODE>new Manifest(this)</CODE>. 185 */ 186 public Object clone() 187 { 188 return new Manifest(this); 189 } 190 191 /** 192 * Checks if another object is equal to this Manifest object. 193 * Another Object is equal to this Manifest object if it is an instance of 194 * Manifest and the main attributes and the entries of the other manifest 195 * are equal to this one. 196 */ 197 public boolean equals(Object o) 198 { 199 return (o instanceof Manifest) && 200 (mainAttr.equals(((Manifest) o).mainAttr)) && 201 (entries.equals(((Manifest) o).entries)); 202 } 203 204 /** 205 * Calculates the hash code of the manifest. Implemented by a xor of the 206 * hash code of the main attributes with the hash code of the entries map. 207 */ 208 public int hashCode() 209 { 210 return mainAttr.hashCode() ^ entries.hashCode(); 211 } 212 213 }