/** * 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); } } } }
/** * 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); } }