FileDocCategorySizeDatePackage
Manifest.javaAPI DocApache Ant 1.7037520Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs

Manifest

public class Manifest extends Object
Holds the data of a jar manifest. Manifests are processed according to the {@link Jar file specification.}. Specifically, a manifest element consists of a set of attributes and sections. These sections in turn may contain attributes. Note in particular that this may result in manifest lines greater than 72 bytes being wrapped and continued on the next line. If an application can not handle the continuation mechanism, it is a defect in the application, not this task.
since
Ant 1.4

Fields Summary
public static final String
ATTRIBUTE_MANIFEST_VERSION
The standard manifest version header
public static final String
ATTRIBUTE_SIGNATURE_VERSION
The standard Signature Version header
public static final String
ATTRIBUTE_NAME
The Name Attribute is the first in a named section
public static final String
ATTRIBUTE_FROM
The From Header is disallowed in a Manifest
public static final String
ATTRIBUTE_CLASSPATH
The Class-Path Header is special - it can be duplicated
public static final String
DEFAULT_MANIFEST_VERSION
Default Manifest version if one is not specified
public static final int
MAX_LINE_LENGTH
The max length of a line in a Manifest
public static final int
MAX_SECTION_LENGTH
Max length of a line section which is continued. Need to allow for the CRLF.
public static final String
EOL
The End-Of-Line marker in manifests
public static final String
ERROR_FROM_FORBIDDEN
Error for attributes
public static final String
JAR_ENCODING
Encoding to be used for JAR files.
private String
manifestVersion
The version of this manifest
private Section
mainSection
The main section of this manifest
private Hashtable
sections
The named sections of this manifest
private Vector
sectionIndex
Index of sections - used to retain order of sections in manifest
Constructors Summary
public Manifest()
Construct an empty manifest

        manifestVersion = null;
    
public Manifest(Reader r)
Read a manifest file from the given reader

param
r is the reader from which the Manifest is read
throws
ManifestException if the manifest is not valid according to the JAR spec
throws
IOException if the manifest cannot be read from the reader.

        BufferedReader reader = new BufferedReader(r);
        // This should be the manifest version
        String nextSectionName = mainSection.read(reader);
        String readManifestVersion
            = mainSection.getAttributeValue(ATTRIBUTE_MANIFEST_VERSION);
        if (readManifestVersion != null) {
            manifestVersion = readManifestVersion;
            mainSection.removeAttribute(ATTRIBUTE_MANIFEST_VERSION);
        }

        String line = null;
        while ((line = reader.readLine()) != null) {
            if (line.length() == 0) {
                continue;
            }

            Section section = new Section();
            if (nextSectionName == null) {
                Attribute sectionName = new Attribute(line);
                if (!sectionName.getName().equalsIgnoreCase(ATTRIBUTE_NAME)) {
                    throw new ManifestException("Manifest sections should "
                        + "start with a \"" + ATTRIBUTE_NAME
                        + "\" attribute and not \""
                        + sectionName.getName() + "\"");
                }
                nextSectionName = sectionName.getValue();
            } else {
                // we have already started reading this section
                // this line is the first attribute. set it and then
                // let the normal read handle the rest
                Attribute firstAttribute = new Attribute(line);
                section.addAttributeAndCheck(firstAttribute);
            }

            section.setName(nextSectionName);
            nextSectionName = section.read(reader);
            addConfiguredSection(section);
        }
    
Methods Summary
public voidaddConfiguredAttribute(org.apache.tools.ant.taskdefs.Manifest$Attribute attribute)
Add an attribute to the manifest - it is added to the main section.

param
attribute the attribute to be added.
exception
ManifestException if the attribute is not valid.

        if (attribute.getKey() == null || attribute.getValue() == null) {
            throw new BuildException("Attributes must have name and value");
        }
        if (attribute.getKey().equalsIgnoreCase(ATTRIBUTE_MANIFEST_VERSION)) {
            manifestVersion = attribute.getValue();
        } else {
            mainSection.addConfiguredAttribute(attribute);
        }
    
public voidaddConfiguredSection(org.apache.tools.ant.taskdefs.Manifest$Section section)
Add a section to the manifest

param
section the manifest section to be added
exception
ManifestException if the secti0on is not valid.

        String sectionName = section.getName();
        if (sectionName == null) {
            throw new BuildException("Sections must have a name");
        }
        sections.put(sectionName, section);
        if (!sectionIndex.contains(sectionName)) {
            sectionIndex.addElement(sectionName);
        }
    
public booleanequals(java.lang.Object rhs)

see
java.lang.Object#equals
param
rhs the object to check for equality.
return
true if the version, main and sections are the same.

        if (rhs == null || rhs.getClass() != getClass()) {
            return false;
        }

        if (rhs == this) {
            return true;
        }

        Manifest rhsManifest = (Manifest) rhs;
        if (manifestVersion == null) {
            if (rhsManifest.manifestVersion != null) {
                return false;
            }
        } else if (!manifestVersion.equals(rhsManifest.manifestVersion)) {
            return false;
        }

        if (!mainSection.equals(rhsManifest.mainSection)) {
            return false;
        }

        return sections.equals(rhsManifest.sections);
    
public static org.apache.tools.ant.taskdefs.ManifestgetDefaultManifest()
Construct a manifest from Ant's default manifest file.

return
the default manifest.
exception
BuildException if there is a problem loading the default manifest


                                           
          
        InputStream in = null;
        InputStreamReader insr = null;
        try {
            String defManifest = "/org/apache/tools/ant/defaultManifest.mf";
            in = Manifest.class.getResourceAsStream(defManifest);
            if (in == null) {
                throw new BuildException("Could not find default manifest: "
                    + defManifest);
            }
            try {
                insr = new InputStreamReader(in, "UTF-8");
                Manifest defaultManifest = new Manifest(insr);
                Attribute createdBy = new Attribute("Created-By",
                    System.getProperty("java.vm.version") + " ("
                    + System.getProperty("java.vm.vendor") + ")");
                defaultManifest.getMainSection().storeAttribute(createdBy);
                return defaultManifest;
            } catch (UnsupportedEncodingException e) {
                insr = new InputStreamReader(in);
                return new Manifest(insr);
            }
        } catch (ManifestException e) {
            throw new BuildException("Default manifest is invalid !!", e);
        } catch (IOException e) {
            throw new BuildException("Unable to read default manifest", e);
        } finally {
            FileUtils.close(insr);
            FileUtils.close(in);
        }
    
public org.apache.tools.ant.taskdefs.Manifest$SectiongetMainSection()
Get the main section of the manifest

return
the main section of the manifest

        return mainSection;
    
public java.lang.StringgetManifestVersion()
Get the version of the manifest

return
the manifest's version string

        return manifestVersion;
    
public org.apache.tools.ant.taskdefs.Manifest$SectiongetSection(java.lang.String name)
Get a particular section from the manifest

param
name the name of the section desired.
return
the specified section or null if that section does not exist in the manifest

        return (Section) sections.get(name);
    
public java.util.EnumerationgetSectionNames()
Get the section names in this manifest.

return
an Enumeration of section names

        return sectionIndex.elements();
    
public java.util.EnumerationgetWarnings()
Get the warnings for this manifest.

return
an enumeration of warning strings

        Vector warnings = new Vector();

        Enumeration warnEnum = mainSection.getWarnings();
        while (warnEnum.hasMoreElements()) {
            warnings.addElement(warnEnum.nextElement());
        }

        // create a vector and add in the warnings for all the sections
        Enumeration e = sections.elements();
        while (e.hasMoreElements()) {
            Section section = (Section) e.nextElement();
            Enumeration e2 = section.getWarnings();
            while (e2.hasMoreElements()) {
                warnings.addElement(e2.nextElement());
            }
        }

        return warnings.elements();
    
public inthashCode()

see
java.lang.Object#hashCode
return
a hashcode based on the version, main and sections.

        int hashCode = 0;

        if (manifestVersion != null) {
            hashCode += manifestVersion.hashCode();
        }
        hashCode += mainSection.hashCode();
        hashCode += sections.hashCode();

        return hashCode;
    
public voidmerge(org.apache.tools.ant.taskdefs.Manifest other)
Merge the contents of the given manifest into this manifest

param
other the Manifest to be merged with this one.
throws
ManifestException if there is a problem merging the manifest according to the Manifest spec.

        merge(other, false);
    
public voidmerge(org.apache.tools.ant.taskdefs.Manifest other, boolean overwriteMain)
Merge the contents of the given manifest into this manifest

param
other the Manifest to be merged with this one.
param
overwriteMain whether to overwrite the main section of the current manifest
throws
ManifestException if there is a problem merging the manifest according to the Manifest spec.

        if (other != null) {
             if (overwriteMain) {
                 mainSection = (Section) other.mainSection.clone();
             } else {
                 mainSection.merge(other.mainSection);
             }

             if (other.manifestVersion != null) {
                 manifestVersion = other.manifestVersion;
             }

             Enumeration e = other.getSectionNames();
             while (e.hasMoreElements()) {
                 String sectionName = (String) e.nextElement();
                 Section ourSection = (Section) sections.get(sectionName);
                 Section otherSection
                    = (Section) other.sections.get(sectionName);
                 if (ourSection == null) {
                     if (otherSection != null) {
                         addConfiguredSection((Section) otherSection.clone());
                     }
                 } else {
                     ourSection.merge(otherSection);
                 }
             }
         }
    
public java.lang.StringtoString()
Convert the manifest to its string representation

return
a multiline string with the Manifest as it appears in a Manifest file.

        StringWriter sw = new StringWriter();
        try {
            write(new PrintWriter(sw));
        } catch (IOException e) {
            return null;
        }
        return sw.toString();
    
public voidwrite(java.io.PrintWriter writer)
Write the manifest out to a print writer.

param
writer the Writer to which the manifest is written
throws
IOException if the manifest cannot be written

        writer.print(ATTRIBUTE_MANIFEST_VERSION + ": " + manifestVersion + EOL);
        String signatureVersion
            = mainSection.getAttributeValue(ATTRIBUTE_SIGNATURE_VERSION);
        if (signatureVersion != null) {
            writer.print(ATTRIBUTE_SIGNATURE_VERSION + ": "
                + signatureVersion + EOL);
            mainSection.removeAttribute(ATTRIBUTE_SIGNATURE_VERSION);
        }
        mainSection.write(writer);

        // add it back
        if (signatureVersion != null) {
            try {
                Attribute svAttr = new Attribute(ATTRIBUTE_SIGNATURE_VERSION,
                    signatureVersion);
                mainSection.addConfiguredAttribute(svAttr);
            } catch (ManifestException e) {
                // shouldn't happen - ignore
            }
        }

        Enumeration e = sectionIndex.elements();
        while (e.hasMoreElements()) {
            String sectionName = (String) e.nextElement();
            Section section = getSection(sectionName);
            section.write(writer);
        }