home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 6.7 KB | 271 lines |
- /*
- * @(#)Manifest.java 1.13 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.util.jar;
-
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.IOException;
- import java.util.Map;
- import java.util.HashMap;
- import java.util.Set;
- import java.util.Iterator;
- import java.util.AbstractSet;
-
- /**
- * The Manifest class is used to maintain Manifest entry names and their
- * associated Attributes. There are main Manifest Attributes as well as
- * per-entry Attributes.
- *
- * @author David Connelly
- * @version 1.13, 03/18/98
- * @see Attributes
- * @since JDK1.2
- */
- public
- class Manifest implements Cloneable {
- // manifest main attributes
- private Attributes attr = new Attributes();
-
- // manifest entries
- private Map entries = new Entries();
-
- /*
- * Inner class used to maintain the Map of entry String names and
- * associated Attributes. Also handles runtime type checking of method
- * parameters.
- */
- private static class Entries extends HashMap {
- public Object get(Object name) {
- return super.get((String)name);
- }
-
- public Object put(Object name, Object attr) {
- return (Attributes)super.put((String)name, (Attributes)attr);
- }
-
- public Object remove(Object name) {
- return (Attributes)super.remove((String)name);
- }
-
- public boolean containsKey(Object name) {
- return super.containsKey((String)name);
- }
-
- public void putAll(Map map) {
- super.putAll((Manifest.Entries)map);
- }
-
- public Set entries() {
- final Set c = super.entries();
- return new AbstractSet() {
- public int size() {
- return c.size();
- }
- public Iterator iterator() {
- return new Iterator() {
- private Iterator it = c.iterator();
- public boolean hasNext() {
- return it.hasNext();
- }
- public Object next() {
- return new Entry((Map.Entry)it.next());
- }
- public void remove() {
- it.remove();
- }
- };
- }
- };
- }
-
- // XXX Workaround for inner class bug
- private static class Entry implements Map.Entry {
- private Map.Entry e;
- Entry(Map.Entry e) {
- this.e = e;
- }
- public Object getKey() {
- return (String)e.getKey();
- }
- public Object getValue() {
- return (Attributes)e.getValue();
- }
- public Object setValue(Object o) {
- return (Attributes)e.setValue((Attributes)o);
- }
- public boolean equals(Object o) {
- if (o instanceof Entry) {
- return e.equals(((Entry)o).e);
- } else {
- return false;
- }
- }
- public int hashCode() {
- return e.hashCode();
- }
- }
-
- public boolean equals(Object o) {
- if (o instanceof Entries) {
- return super.equals(o);
- } else {
- return false;
- }
- }
- }
-
- /**
- * Constructs a new, empty Manifest.
- */
- public Manifest() {
- }
-
- /**
- * Constructs a new Manifest that is a copy of the specified Manifest.
- *
- * @param man the Manifest to copy
- */
- public Manifest(Manifest man) {
- attr.putAll(man.getMainAttributes());
- entries.putAll(man.getEntries());
- }
-
- /**
- * Returns the main Attributes for the Manifest.
- */
- public Attributes getMainAttributes() {
- return attr;
- }
-
- /**
- * Returns a Map of the entries contained in this Manifest. Each entry
- * is represented by a String name (key) and associated Attributes (value).
- */
- public Map getEntries() {
- return entries;
- }
-
- /**
- * Returns the Attributes for the specified entry name.
- * This method is merely shorthand for the expression:
- * <pre>
- * (Attributes)getEntries().get(name)
- * </pre>
- */
- public Attributes getAttributes(String name) {
- return (Attributes)getEntries().get(name);
- }
-
- /**
- * Clears the main Attributes as well as the entries in this Manifest.
- */
- public void clear() {
- attr.clear();
- entries.clear();
- }
-
- /**
- * Writes the Manifest to the specified OutputStream.
- *
- * @param out the output stream
- * @exception IOException if an I/O error has occurred
- */
- public void write(OutputStream out) throws IOException {
- DataOutputStream dos = new DataOutputStream(out);
- // Write out the main attributes for the manifest
- attr.write(dos);
- // Now write out the pre-entry attributes
- Iterator it = entries.entries().iterator();
- while (it.hasNext()) {
- Map.Entry e = (Map.Entry)it.next();
- dos.writeBytes("Name: ");
- dos.writeBytes((String)e.getKey());
- dos.writeBytes("\r\n");
- ((Attributes)e.getValue()).write(dos);
- }
- dos.flush();
- }
-
- /**
- * Reads the Manifest from the specified InputStream. The entry
- * names and attributes read will be merged in with the current
- * manifest entries.
- *
- * @param in the input stream
- * @exception IOException if an I/O error has occurred
- */
- public void read(InputStream in) throws IOException {
- DataInputStream dis = new DataInputStream(in);
- // Read the main attributes for the manifest
- attr.read(dis);
- // Now parse the manifest entries
- String line;
- while ((line = dis.readLine()) != null) {
- String name = parseField(line, "Name");
- if (name == null) {
- throw new IOException("invalid manifest format");
- }
- Attributes attr = getAttributes(name);
- if (attr == null) {
- attr = new Attributes();
- entries.put(name, attr);
- }
- attr.read(dis);
- }
- }
-
- /*
- * Parses a header field line from the manifest using the specified
- * header field name. Returns the header field value or null if the
- * name did not match.
- */
- private String parseField(String line, String name) {
- int i = line.indexOf(": ");
- if (i != -1 && name.equalsIgnoreCase(line.substring(0, i))) {
- return line.substring(i + 2);
- } else {
- return null;
- }
- }
-
- /**
- * Returns true if the specified Object is also a Manifest and has
- * the same main Attributes and entries.
- *
- * @param o the object to be compared
- */
- public boolean equals(Object o) {
- if (o instanceof Manifest) {
- Manifest m = (Manifest)o;
- return attr.equals(m.getMainAttributes()) &&
- entries.equals(m.getEntries());
- } else {
- return false;
- }
- }
-
- /**
- * Returns a shallow copy of this Manifest, implemented as follows:
- * <pre>
- * public Object clone() { return new Manifest(this); }
- * </pre>
- */
- public Object clone() {
- return new Manifest(this);
- }
- }
-