📄 manifest.java
字号:
/** The main section of this manifest */ private Section mainSection = new Section(); /** The named sections of this manifest */ private Hashtable sections = new Hashtable(); /** Index of sections - used to retain order of sections in manifest */ private Vector sectionIndex = new Vector(); /** * Construct a manifest from Ant's default manifest file. * * @return the default manifest. * @exception BuildException if there is a problem loading the * default manifest */ public static Manifest getDefaultManifest() throws BuildException { 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); } } /** Construct an empty manifest */ public Manifest() { manifestVersion = null; } /** * 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. */ public Manifest(Reader r) throws ManifestException, IOException { 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); } } /** * Add a section to the manifest * * @param section the manifest section to be added * * @exception ManifestException if the secti0on is not valid. */ public void addConfiguredSection(Section section) throws ManifestException { 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); } } /** * 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. */ public void addConfiguredAttribute(Attribute attribute) throws ManifestException { 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); } } /** * 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. */ public void merge(Manifest other) throws ManifestException { merge(other, false); } /** * 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. */ public void merge(Manifest other, boolean overwriteMain) throws ManifestException { 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); } } } } /** * 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 */ public void write(PrintWriter writer) throws IOException { 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); } } /** * Convert the manifest to its string representation * * @return a multiline string with the Manifest as it * appears in a Manifest file. */ public String toString() { StringWriter sw = new StringWriter(); try { write(new PrintWriter(sw)); } catch (IOException e) { return null; } return sw.toString(); } /** * Get the warnings for this manifest. * * @return an enumeration of warning strings */ public Enumeration getWarnings() { 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(); } /** * @see java.lang.Object#hashCode * @return a hashcode based on the version, main and sections. */ public int hashCode() { int hashCode = 0; if (manifestVersion != null) { hashCode += manifestVersion.hashCode(); } hashCode += mainSection.hashCode(); hashCode += sections.hashCode(); return hashCode; } /** * @see java.lang.Object#equals * @param rhs the object to check for equality. * @return true if the version, main and sections are the same. */ public boolean equals(Object rhs) { 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); } /** * Get the version of the manifest * * @return the manifest's version string */ public String getManifestVersion() { return manifestVersion; } /** * Get the main section of the manifest * * @return the main section of the manifest */ public Section getMainSection() { return mainSection; } /** * 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 */ public Section getSection(String name) { return (Section) sections.get(name); } /** * Get the section names in this manifest. * * @return an Enumeration of section names */ public Enumeration getSectionNames() { return sectionIndex.elements(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -