You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Manifest.java 38 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.util.Vector;
  56. import java.util.Hashtable;
  57. import java.util.Enumeration;
  58. import java.io.BufferedReader;
  59. import java.io.File;
  60. import java.io.FileReader;
  61. import java.io.FileWriter;
  62. import java.io.IOException;
  63. import java.io.InputStream;
  64. import java.io.InputStreamReader;
  65. import java.io.PrintWriter;
  66. import java.io.Reader;
  67. import java.io.StringWriter;
  68. import java.io.UnsupportedEncodingException;
  69. import org.apache.tools.ant.BuildException;
  70. import org.apache.tools.ant.Project;
  71. import org.apache.tools.ant.Task;
  72. import org.apache.tools.ant.types.EnumeratedAttribute;
  73. import org.apache.tools.ant.util.CollectionUtils;
  74. /**
  75. * Class to manage Manifest information
  76. *
  77. * @author Conor MacNeill
  78. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  79. * @author <a href="mailto:j_a_fernandez@yahoo.com">Jose Alberto Fernandez</a>
  80. *
  81. * @since Ant 1.4
  82. *
  83. * @ant.task category="java"
  84. */
  85. public class Manifest extends Task {
  86. /** The standard manifest version header */
  87. public static final String ATTRIBUTE_MANIFEST_VERSION
  88. = "Manifest-Version";
  89. /** The standard Signature Version header */
  90. public static final String ATTRIBUTE_SIGNATURE_VERSION
  91. = "Signature-Version";
  92. /** The Name Attribute is the first in a named section */
  93. public static final String ATTRIBUTE_NAME = "Name";
  94. /** The From Header is disallowed in a Manifest */
  95. public static final String ATTRIBUTE_FROM = "From";
  96. /** The Class-Path Header is special - it can be duplicated */
  97. public static final String ATTRIBUTE_CLASSPATH = "class-path";
  98. /** Default Manifest version if one is not specified */
  99. public static final String DEFAULT_MANIFEST_VERSION = "1.0";
  100. /** The max length of a line in a Manifest */
  101. public static final int MAX_LINE_LENGTH = 72;
  102. /**
  103. * Max length of a line section which is continued. Need to allow
  104. * for the CRLF.
  105. */
  106. public static final int MAX_SECTION_LENGTH = MAX_LINE_LENGTH - 2;
  107. /** The End-Of-Line marker in manifests */
  108. public static final String EOL = "\r\n";
  109. /**
  110. * Helper class for Manifest's mode attribute.
  111. */
  112. public static class Mode extends EnumeratedAttribute {
  113. /**
  114. * Get Allowed values for the mode attribute.
  115. *
  116. * @return a String array of the allowed values.
  117. */
  118. public String[] getValues() {
  119. return new String[] {"update", "replace"};
  120. }
  121. }
  122. /**
  123. * Class to hold manifest attributes
  124. */
  125. public static class Attribute {
  126. /** The attribute's name */
  127. private String name = null;
  128. /** The attribute's value */
  129. private Vector values = new Vector();
  130. /**
  131. * For multivalued attributes, this is the index of the attribute
  132. * currently being defined.
  133. */
  134. private int currentIndex = 0;
  135. /**
  136. * Construct an empty attribute */
  137. public Attribute() {
  138. }
  139. /**
  140. * Construct an attribute by parsing a line from the Manifest
  141. *
  142. * @param line the line containing the attribute name and value
  143. *
  144. * @throws ManifestException if the line is not valid
  145. */
  146. public Attribute(String line) throws ManifestException {
  147. parse(line);
  148. }
  149. /**
  150. * Construct a manifest by specifying its name and value
  151. *
  152. * @param name the attribute's name
  153. * @param value the Attribute's value
  154. */
  155. public Attribute(String name, String value) {
  156. this.name = name;
  157. setValue(value);
  158. }
  159. /**
  160. * @see java.lang.Object#hashCode
  161. */
  162. public int hashCode() {
  163. int hashCode = 0;
  164. if (name != null) {
  165. hashCode += name.hashCode();
  166. }
  167. hashCode += values.hashCode();
  168. return hashCode;
  169. }
  170. /**
  171. * @see java.lang.Object#equals
  172. */
  173. public boolean equals(Object rhs) {
  174. if (rhs == null || rhs.getClass() != getClass()) {
  175. return false;
  176. }
  177. if (rhs == this) {
  178. return true;
  179. }
  180. Attribute rhsAttribute = (Attribute) rhs;
  181. String lhsKey = getKey();
  182. String rhsKey = rhsAttribute.getKey();
  183. if ((lhsKey == null && rhsKey != null)
  184. || (lhsKey != null && rhsKey == null)
  185. || !lhsKey.equals(rhsKey)) {
  186. return false;
  187. }
  188. return CollectionUtils.equals(values, rhsAttribute.values);
  189. }
  190. /**
  191. * Parse a line into name and value pairs
  192. *
  193. * @param line the line to be parsed
  194. *
  195. * @throws ManifestException if the line does not contain a colon
  196. * separating the name and value
  197. */
  198. public void parse(String line) throws ManifestException {
  199. int index = line.indexOf(": ");
  200. if (index == -1) {
  201. throw new ManifestException("Manifest line \"" + line
  202. + "\" is not valid as it does not "
  203. + "contain a name and a value separated by ': ' ");
  204. }
  205. name = line.substring(0, index);
  206. setValue(line.substring(index + 2));
  207. }
  208. /**
  209. * Set the Attribute's name
  210. *
  211. * @param name the attribute's name
  212. */
  213. public void setName(String name) {
  214. this.name = name;
  215. }
  216. /**
  217. * Get the Attribute's name
  218. *
  219. * @return the attribute's name.
  220. */
  221. public String getName() {
  222. return name;
  223. }
  224. /**
  225. * Get the attribute's Key - its name in lower case.
  226. *
  227. * @return the attribute's key.
  228. */
  229. public String getKey() {
  230. if (name == null) {
  231. return null;
  232. }
  233. return name.toLowerCase();
  234. }
  235. /**
  236. * Set the Attribute's value
  237. *
  238. * @param value the attribute's value
  239. */
  240. public void setValue(String value) {
  241. if (currentIndex >= values.size()) {
  242. values.addElement(value);
  243. currentIndex = values.size() - 1;
  244. } else {
  245. values.setElementAt(value, currentIndex);
  246. }
  247. }
  248. /**
  249. * Get the Attribute's value
  250. *
  251. * @return the attribute's value.
  252. */
  253. public String getValue() {
  254. if (values.size() == 0) {
  255. return null;
  256. }
  257. String fullValue = "";
  258. for (Enumeration e = getValues(); e.hasMoreElements();) {
  259. String value = (String) e.nextElement();
  260. fullValue += value + " ";
  261. }
  262. return fullValue.trim();
  263. }
  264. /**
  265. * Add a new value to this attribute - making it multivalued
  266. *
  267. * @param value the attribute's additional value
  268. */
  269. public void addValue(String value) {
  270. currentIndex++;
  271. setValue(value);
  272. }
  273. /**
  274. * Get all the attribute's values
  275. *
  276. * @return an enumeration of the attributes values
  277. */
  278. public Enumeration getValues() {
  279. return values.elements();
  280. }
  281. /**
  282. * Add a continuation line from the Manifest file
  283. *
  284. * When lines are too long in a manifest, they are continued on the
  285. * next line by starting with a space. This method adds the continuation
  286. * data to the attribute value by skipping the first character.
  287. *
  288. * @param line the continuation line.
  289. */
  290. public void addContinuation(String line) {
  291. String currentValue = (String) values.elementAt(currentIndex);
  292. setValue(currentValue + line.substring(1));
  293. }
  294. /**
  295. * Write the attribute out to a print writer.
  296. *
  297. * @param writer the Writer to which the attribute is written
  298. *
  299. * @throws IOException if the attribte value cannot be written
  300. */
  301. public void write(PrintWriter writer) throws IOException {
  302. for (Enumeration e = getValues(); e.hasMoreElements();) {
  303. writeValue(writer, (String) e.nextElement());
  304. }
  305. }
  306. /**
  307. * Write a single attribute value out
  308. *
  309. * @param writer the Writer to which the attribute is written
  310. * @param value the attribute value
  311. *
  312. * @throws IOException if the attribte value cannot be written
  313. */
  314. private void writeValue(PrintWriter writer, String value)
  315. throws IOException {
  316. String line = name + ": " + value;
  317. while (line.getBytes().length > MAX_LINE_LENGTH) {
  318. // try to find a MAX_LINE_LENGTH byte section
  319. int breakIndex = MAX_SECTION_LENGTH;
  320. String section = line.substring(0, breakIndex);
  321. while (section.getBytes().length > MAX_SECTION_LENGTH
  322. && breakIndex > 0) {
  323. breakIndex--;
  324. section = line.substring(0, breakIndex);
  325. }
  326. if (breakIndex == 0) {
  327. throw new IOException("Unable to write manifest line "
  328. + name + ": " + value);
  329. }
  330. writer.print(section + EOL);
  331. line = " " + line.substring(breakIndex);
  332. }
  333. writer.print(line + EOL);
  334. }
  335. }
  336. /**
  337. * Class to represent an individual section in the
  338. * Manifest. A section consists of a set of attribute values,
  339. * separated from other sections by a blank line.
  340. */
  341. public static class Section {
  342. /** Warnings for this section */
  343. private Vector warnings = new Vector();
  344. /**
  345. * The section's name if any. The main section in a
  346. * manifest is unnamed.
  347. */
  348. private String name = null;
  349. /** The section's attributes.*/
  350. private Hashtable attributes = new Hashtable();
  351. /** Index used to retain the attribute ordering */
  352. private Vector attributeIndex = new Vector();
  353. /**
  354. * Set the Section's name
  355. *
  356. * @param name the section's name
  357. */
  358. public void setName(String name) {
  359. this.name = name;
  360. }
  361. /**
  362. * Get the Section's name
  363. *
  364. * @return the section's name.
  365. */
  366. public String getName() {
  367. return name;
  368. }
  369. /**
  370. * Read a section through a reader
  371. *
  372. * @param reader the reader from which the section is read
  373. *
  374. * @return the name of the next section if it has been read as
  375. * part of this section - This only happens if the
  376. * Manifest is malformed.
  377. *
  378. * @throws ManifestException if the section is not valid according
  379. * to the JAR spec
  380. * @throws IOException if the section cannot be read from the reader.
  381. */
  382. public String read(BufferedReader reader)
  383. throws ManifestException, IOException {
  384. Attribute attribute = null;
  385. while (true) {
  386. String line = reader.readLine();
  387. if (line == null || line.length() == 0) {
  388. return null;
  389. }
  390. if (line.charAt(0) == ' ') {
  391. // continuation line
  392. if (attribute == null) {
  393. if (name != null) {
  394. // a continuation on the first line is a
  395. // continuation of the name - concatenate this
  396. // line and the name
  397. name += line.substring(1);
  398. } else {
  399. throw new ManifestException("Can't start an "
  400. + "attribute with a continuation line " + line);
  401. }
  402. } else {
  403. attribute.addContinuation(line);
  404. }
  405. } else {
  406. attribute = new Attribute(line);
  407. String nameReadAhead = addAttributeAndCheck(attribute);
  408. if (nameReadAhead != null) {
  409. return nameReadAhead;
  410. }
  411. }
  412. }
  413. }
  414. /**
  415. * Merge in another section
  416. *
  417. * @param section the section to be merged with this one.
  418. *
  419. * @throws ManifestException if the sections cannot be merged.
  420. */
  421. public void merge(Section section) throws ManifestException {
  422. if (name == null && section.getName() != null
  423. || name != null
  424. && !(name.equalsIgnoreCase(section.getName()))) {
  425. throw new ManifestException("Unable to merge sections "
  426. + "with different names");
  427. }
  428. Enumeration e = section.getAttributeKeys();
  429. while (e.hasMoreElements()) {
  430. String attributeName = (String) e.nextElement();
  431. Attribute attribute = section.getAttribute(attributeName);
  432. if (attributeName.equals(ATTRIBUTE_CLASSPATH) &&
  433. attributes.containsKey(attributeName)) {
  434. Attribute ourClassPath = getAttribute(attributeName);
  435. Enumeration cpe = attribute.getValues();
  436. while (cpe.hasMoreElements()) {
  437. String value = (String) cpe.nextElement();
  438. ourClassPath.addValue(value);
  439. }
  440. } else {
  441. // the merge file always wins
  442. storeAttribute(attribute);
  443. }
  444. }
  445. // add in the warnings
  446. Enumeration warnEnum = section.warnings.elements();
  447. while (warnEnum.hasMoreElements()) {
  448. warnings.addElement(warnEnum.nextElement());
  449. }
  450. }
  451. /**
  452. * Write the section out to a print writer.
  453. *
  454. * @param writer the Writer to which the section is written
  455. *
  456. * @throws IOException if the section cannot be written
  457. */
  458. public void write(PrintWriter writer) throws IOException {
  459. if (name != null) {
  460. Attribute nameAttr = new Attribute(ATTRIBUTE_NAME, name);
  461. nameAttr.write(writer);
  462. }
  463. Enumeration e = getAttributeKeys();
  464. while (e.hasMoreElements()) {
  465. String key = (String) e.nextElement();
  466. Attribute attribute = getAttribute(key);
  467. attribute.write(writer);
  468. }
  469. writer.print(EOL);
  470. }
  471. /**
  472. * Get a attribute of the section
  473. *
  474. * @param attributeName the name of the attribute
  475. * @return a Manifest.Attribute instance if the attribute is
  476. * single-valued, otherwise a Vector of Manifest.Attribute
  477. * instances.
  478. */
  479. public Attribute getAttribute(String attributeName) {
  480. return (Attribute) attributes.get(attributeName.toLowerCase());
  481. }
  482. /**
  483. * Get the attribute keys.
  484. *
  485. * @return an Enumeration of Strings, each string being the lower case
  486. * key of an attribute of the section.
  487. */
  488. public Enumeration getAttributeKeys() {
  489. return attributeIndex.elements();
  490. }
  491. /**
  492. * Get the value of the attribute with the name given.
  493. *
  494. * @param attributeName the name of the attribute to be returned.
  495. *
  496. * @return the attribute's value or null if the attribute does not exist
  497. * in the section
  498. */
  499. public String getAttributeValue(String attributeName) {
  500. Attribute attribute = getAttribute(attributeName.toLowerCase());
  501. if (attribute == null) {
  502. return null;
  503. }
  504. return attribute.getValue();
  505. }
  506. /**
  507. * Remove tge given attribute from the section
  508. *
  509. * @param attributeName the name of the attribute to be removed.
  510. */
  511. public void removeAttribute(String attributeName) {
  512. String key = attributeName.toLowerCase();
  513. attributes.remove(key);
  514. attributeIndex.removeElement(key);
  515. }
  516. /**
  517. * Add an attribute to the section.
  518. *
  519. * @param attribute the attribute to be added to the section
  520. *
  521. * @exception ManifestException if the attribute is not valid.
  522. */
  523. public void addConfiguredAttribute(Attribute attribute)
  524. throws ManifestException {
  525. String check = addAttributeAndCheck(attribute);
  526. if (check != null) {
  527. throw new BuildException("Specify the section name using "
  528. + "the \"name\" attribute of the <section> element rather "
  529. + "than using a \"Name\" manifest attribute");
  530. }
  531. }
  532. /**
  533. * Add an attribute to the section
  534. *
  535. * @param attribute the attribute to be added.
  536. *
  537. * @return the value of the attribute if it is a name
  538. * attribute - null other wise
  539. *
  540. * @exception ManifestException if the attribute already
  541. * exists in this section.
  542. */
  543. public String addAttributeAndCheck(Attribute attribute)
  544. throws ManifestException {
  545. if (attribute.getName() == null || attribute.getValue() == null) {
  546. throw new BuildException("Attributes must have name and value");
  547. }
  548. if (attribute.getKey().equalsIgnoreCase(ATTRIBUTE_NAME)) {
  549. warnings.addElement("\"" + ATTRIBUTE_NAME + "\" attributes "
  550. + "should not occur in the main section and must be the "
  551. + "first element in all other sections: \""
  552. + attribute.getName() + ": " + attribute.getValue() + "\"");
  553. return attribute.getValue();
  554. }
  555. if (attribute.getKey().startsWith(ATTRIBUTE_FROM.toLowerCase())) {
  556. warnings.addElement("Manifest attributes should not start "
  557. + "with \"" + ATTRIBUTE_FROM + "\" in \""
  558. + attribute.getName() + ": " + attribute.getValue() + "\"");
  559. } else {
  560. // classpath attributes go into a vector
  561. String attributeKey = attribute.getKey();
  562. if (attributeKey.equals(ATTRIBUTE_CLASSPATH)) {
  563. Attribute classpathAttribute =
  564. (Attribute) attributes.get(attributeKey);
  565. if (classpathAttribute == null) {
  566. storeAttribute(attribute);
  567. } else {
  568. Enumeration e = attribute.getValues();
  569. while (e.hasMoreElements()) {
  570. String value = (String) e.nextElement();
  571. classpathAttribute.addValue(value);
  572. }
  573. }
  574. } else if (attributes.containsKey(attributeKey)) {
  575. throw new ManifestException("The attribute \""
  576. + attribute.getName() + "\" may not occur more "
  577. + "than once in the same section");
  578. } else {
  579. storeAttribute(attribute);
  580. }
  581. }
  582. return null;
  583. }
  584. /**
  585. * Store an attribute and update the index.
  586. *
  587. * @param attribute the attribute to be stored
  588. */
  589. private void storeAttribute(Attribute attribute) {
  590. if (attribute == null) {
  591. return;
  592. }
  593. String attributeKey = attribute.getKey();
  594. attributes.put(attributeKey, attribute);
  595. if (!attributeIndex.contains(attributeKey)) {
  596. attributeIndex.addElement(attributeKey);
  597. }
  598. }
  599. /**
  600. * Get the warnings for this section.
  601. *
  602. * @return an Enumeration of warning strings.
  603. */
  604. public Enumeration getWarnings() {
  605. return warnings.elements();
  606. }
  607. /**
  608. * @see java.lang.Object#hashCode
  609. */
  610. public int hashCode() {
  611. int hashCode = 0;
  612. if (name != null) {
  613. hashCode += name.hashCode();
  614. }
  615. hashCode += attributes.hashCode();
  616. return hashCode;
  617. }
  618. /**
  619. * @see java.lang.Object#equals
  620. */
  621. public boolean equals(Object rhs) {
  622. if (rhs == null || rhs.getClass() != getClass()) {
  623. return false;
  624. }
  625. if (rhs == this) {
  626. return true;
  627. }
  628. Section rhsSection = (Section) rhs;
  629. return attributes.equals(rhsSection.attributes);
  630. }
  631. }
  632. /** The version of this manifest */
  633. private String manifestVersion = DEFAULT_MANIFEST_VERSION;
  634. /** The main section of this manifest */
  635. private Section mainSection = new Section();
  636. /** The named sections of this manifest */
  637. private Hashtable sections = new Hashtable();
  638. /** Index of sections - used to retain order of sections in manifest */
  639. private Vector sectionIndex = new Vector();
  640. /**
  641. * The file to which the manifest should be written when used as a task
  642. */
  643. private File manifestFile;
  644. /**
  645. * The mode with which the manifest file is written
  646. */
  647. private Mode mode;
  648. /**
  649. * Construct a manifest from Ant's default manifest file.
  650. *
  651. * @return the default manifest.
  652. * @exception BuildException if there is a problem loading the
  653. * default manifest
  654. */
  655. public static Manifest getDefaultManifest() throws BuildException {
  656. try {
  657. String defManifest = "/org/apache/tools/ant/defaultManifest.mf";
  658. InputStream in = Manifest.class.getResourceAsStream(defManifest);
  659. if (in == null) {
  660. throw new BuildException("Could not find default manifest: "
  661. + defManifest);
  662. }
  663. try {
  664. return new Manifest(new InputStreamReader(in, "ASCII"));
  665. } catch (UnsupportedEncodingException e) {
  666. return new Manifest(new InputStreamReader(in));
  667. }
  668. } catch (ManifestException e) {
  669. throw new BuildException("Default manifest is invalid !!", e);
  670. } catch (IOException e) {
  671. throw new BuildException("Unable to read default manifest", e);
  672. }
  673. }
  674. /** Construct an empty manifest */
  675. public Manifest() {
  676. mode = new Mode();
  677. mode.setValue("replace");
  678. manifestVersion = null;
  679. }
  680. /**
  681. * Read a manifest file from the given reader
  682. *
  683. * @param r is the reader from which the Manifest is read
  684. *
  685. * @throws ManifestException if the manifest is not valid according
  686. * to the JAR spec
  687. * @throws IOException if the manifest cannot be read from the reader.
  688. */
  689. public Manifest(Reader r) throws ManifestException, IOException {
  690. BufferedReader reader = new BufferedReader(r);
  691. // This should be the manifest version
  692. String nextSectionName = mainSection.read(reader);
  693. String readManifestVersion
  694. = mainSection.getAttributeValue(ATTRIBUTE_MANIFEST_VERSION);
  695. if (readManifestVersion != null) {
  696. manifestVersion = readManifestVersion;
  697. mainSection.removeAttribute(ATTRIBUTE_MANIFEST_VERSION);
  698. }
  699. String line = null;
  700. while ((line = reader.readLine()) != null) {
  701. if (line.length() == 0) {
  702. continue;
  703. }
  704. Section section = new Section();
  705. if (nextSectionName == null) {
  706. Attribute sectionName = new Attribute(line);
  707. if (!sectionName.getName().equalsIgnoreCase(ATTRIBUTE_NAME)) {
  708. throw new ManifestException("Manifest sections should "
  709. + "start with a \"" + ATTRIBUTE_NAME
  710. + "\" attribute and not \""
  711. + sectionName.getName() + "\"");
  712. }
  713. nextSectionName = sectionName.getValue();
  714. } else {
  715. // we have already started reading this section
  716. // this line is the first attribute. set it and then
  717. // let the normal read handle the rest
  718. Attribute firstAttribute = new Attribute(line);
  719. section.addAttributeAndCheck(firstAttribute);
  720. }
  721. section.setName(nextSectionName);
  722. nextSectionName = section.read(reader);
  723. addConfiguredSection(section);
  724. }
  725. }
  726. /**
  727. * Add a section to the manifest
  728. *
  729. * @param section the manifest section to be added
  730. *
  731. * @exception ManifestException if the secti0on is not valid.
  732. */
  733. public void addConfiguredSection(Section section)
  734. throws ManifestException {
  735. String sectionName = section.getName();
  736. if (sectionName == null) {
  737. throw new BuildException("Sections must have a name");
  738. }
  739. sections.put(sectionName, section);
  740. if (!sectionIndex.contains(sectionName)) {
  741. sectionIndex.addElement(sectionName);
  742. }
  743. }
  744. /**
  745. * Add an attribute to the manifest - it is added to the main section.
  746. *
  747. * @param attribute the attribute to be added.
  748. *
  749. * @exception ManifestException if the attribute is not valid.
  750. */
  751. public void addConfiguredAttribute(Attribute attribute)
  752. throws ManifestException {
  753. mainSection.addConfiguredAttribute(attribute);
  754. }
  755. /**
  756. * Merge the contents of the given manifest into this manifest
  757. *
  758. * @param other the Manifest to be merged with this one.
  759. *
  760. * @throws ManifestException if there is a problem merging the
  761. * manfest according to the Manifest spec.
  762. */
  763. public void merge(Manifest other) throws ManifestException {
  764. merge(other, false);
  765. }
  766. /**
  767. * Merge the contents of the given manifest into this manifest
  768. *
  769. * @param other the Manifest to be merged with this one.
  770. * @param overwriteMain whether to overwrite the main section
  771. * of the current manifest
  772. *
  773. * @throws ManifestException if there is a problem merging the
  774. * manfest according to the Manifest spec.
  775. */
  776. public void merge(Manifest other, boolean overwriteMain)
  777. throws ManifestException {
  778. if (other != null) {
  779. if (overwriteMain) {
  780. mainSection = other.mainSection;
  781. } else {
  782. mainSection.merge(other.mainSection);
  783. }
  784. if (other.manifestVersion != null) {
  785. manifestVersion = other.manifestVersion;
  786. }
  787. Enumeration e = other.getSectionNames();
  788. while (e.hasMoreElements()) {
  789. String sectionName = (String) e.nextElement();
  790. Section ourSection = (Section) sections.get(sectionName);
  791. Section otherSection
  792. = (Section) other.sections.get(sectionName);
  793. if (ourSection == null) {
  794. if (otherSection != null) {
  795. addConfiguredSection(otherSection);
  796. }
  797. } else {
  798. ourSection.merge(otherSection);
  799. }
  800. }
  801. }
  802. }
  803. /**
  804. * Write the manifest out to a print writer.
  805. *
  806. * @param writer the Writer to which the manifest is written
  807. *
  808. * @throws IOException if the manifest cannot be written
  809. */
  810. public void write(PrintWriter writer) throws IOException {
  811. writer.print(ATTRIBUTE_MANIFEST_VERSION + ": " + manifestVersion + EOL);
  812. String signatureVersion
  813. = mainSection.getAttributeValue(ATTRIBUTE_SIGNATURE_VERSION);
  814. if (signatureVersion != null) {
  815. writer.print(ATTRIBUTE_SIGNATURE_VERSION + ": "
  816. + signatureVersion + EOL);
  817. mainSection.removeAttribute(ATTRIBUTE_SIGNATURE_VERSION);
  818. }
  819. mainSection.write(writer);
  820. // add it back
  821. if (signatureVersion != null) {
  822. try {
  823. Attribute svAttr = new Attribute(ATTRIBUTE_SIGNATURE_VERSION,
  824. signatureVersion);
  825. mainSection.addConfiguredAttribute(svAttr);
  826. } catch (ManifestException e) {
  827. // shouldn't happen - ignore
  828. }
  829. }
  830. Enumeration e = sectionIndex.elements();
  831. while (e.hasMoreElements()) {
  832. String sectionName = (String) e.nextElement();
  833. Section section = getSection(sectionName);
  834. section.write(writer);
  835. }
  836. }
  837. /**
  838. * Convert the manifest to its string representation
  839. *
  840. * @return a multiline string with the Manifest as it
  841. * appears in a Manifest file.
  842. */
  843. public String toString() {
  844. StringWriter sw = new StringWriter();
  845. try {
  846. write(new PrintWriter(sw));
  847. } catch (IOException e) {
  848. return null;
  849. }
  850. return sw.toString();
  851. }
  852. /**
  853. * Get the warnings for this manifest.
  854. *
  855. * @return an enumeration of warning strings
  856. */
  857. public Enumeration getWarnings() {
  858. Vector warnings = new Vector();
  859. Enumeration warnEnum = mainSection.getWarnings();
  860. while (warnEnum.hasMoreElements()) {
  861. warnings.addElement(warnEnum.nextElement());
  862. }
  863. // create a vector and add in the warnings for all the sections
  864. Enumeration e = sections.elements();
  865. while (e.hasMoreElements()) {
  866. Section section = (Section) e.nextElement();
  867. Enumeration e2 = section.getWarnings();
  868. while (e2.hasMoreElements()) {
  869. warnings.addElement(e2.nextElement());
  870. }
  871. }
  872. return warnings.elements();
  873. }
  874. /**
  875. * @see java.lang.Object#hashCode
  876. */
  877. public int hashCode() {
  878. int hashCode = 0;
  879. if (manifestVersion != null) {
  880. hashCode += manifestVersion.hashCode();
  881. }
  882. hashCode += mainSection.hashCode();
  883. hashCode += sections.hashCode();
  884. return hashCode;
  885. }
  886. /**
  887. * @see java.lang.Object#equals
  888. */
  889. public boolean equals(Object rhs) {
  890. if (rhs == null || rhs.getClass() != getClass()) {
  891. return false;
  892. }
  893. if (rhs == this) {
  894. return true;
  895. }
  896. Manifest rhsManifest = (Manifest) rhs;
  897. if (manifestVersion == null) {
  898. if (rhsManifest.manifestVersion != null) {
  899. return false;
  900. }
  901. } else if (!manifestVersion.equals(rhsManifest.manifestVersion)) {
  902. return false;
  903. }
  904. if (!mainSection.equals(rhsManifest.mainSection)) {
  905. return false;
  906. }
  907. return sections.equals(rhsManifest.sections);
  908. }
  909. /**
  910. * The name of the manifest file to write (if used as a task).
  911. *
  912. * @param f the Manifest file to be written
  913. */
  914. public void setFile(File f) {
  915. manifestFile = f;
  916. }
  917. /**
  918. * Shall we update or replace an existing manifest?
  919. *
  920. * @param m the mode value - update or replace.
  921. */
  922. public void setMode(Mode m) {
  923. mode = m;
  924. }
  925. /**
  926. * Get the version of the manifest
  927. *
  928. * @return the manifest's version string
  929. */
  930. public String getManifestVersion() {
  931. return manifestVersion;
  932. }
  933. /**
  934. * Get the main section of the manifest
  935. *
  936. * @return the main section of the manifest
  937. */
  938. public Section getMainSection() {
  939. return mainSection;
  940. }
  941. /**
  942. * Get a particular section from the manifest
  943. *
  944. * @param name the name of the section desired.
  945. * @return the specified section or null if that section
  946. * does not exist in the manifest
  947. */
  948. public Section getSection(String name) {
  949. return (Section) sections.get(name);
  950. }
  951. /**
  952. * Get the section names in this manifest.
  953. *
  954. * @return an Enumeration of section names
  955. */
  956. public Enumeration getSectionNames() {
  957. return sectionIndex.elements();
  958. }
  959. /**
  960. * Create or update the Manifest when used as a task.
  961. *
  962. * @throws BuildException if the manifest cannot be written.
  963. */
  964. public void execute() throws BuildException {
  965. if (manifestFile == null) {
  966. throw new BuildException("the file attribute is required");
  967. }
  968. Manifest toWrite = getDefaultManifest();
  969. Manifest current = null;
  970. BuildException error = null;
  971. if (manifestFile.exists()) {
  972. FileReader f = null;
  973. try {
  974. f = new FileReader(manifestFile);
  975. current = new Manifest(f);
  976. } catch (ManifestException m) {
  977. error = new BuildException("Existing manifest " + manifestFile
  978. + " is invalid", m, location);
  979. } catch (IOException e) {
  980. error = new BuildException("Failed to read " + manifestFile,
  981. e, location);
  982. } finally {
  983. if (f != null) {
  984. try {
  985. f.close();
  986. } catch (IOException e) {}
  987. }
  988. }
  989. }
  990. try {
  991. if (mode.getValue().equals("update") && manifestFile.exists()) {
  992. if (current != null) {
  993. toWrite.merge(current);
  994. } else if (error != null) {
  995. throw error;
  996. }
  997. }
  998. toWrite.merge(this);
  999. } catch (ManifestException m) {
  1000. throw new BuildException("Manifest is invalid", m, location);
  1001. }
  1002. if (toWrite.equals(current)) {
  1003. log("Manifest has not changed, do not recreate",
  1004. Project.MSG_VERBOSE);
  1005. return;
  1006. }
  1007. PrintWriter w = null;
  1008. try {
  1009. w = new PrintWriter(new FileWriter(manifestFile));
  1010. toWrite.write(w);
  1011. } catch (IOException e) {
  1012. throw new BuildException("Failed to write " + manifestFile,
  1013. e, location);
  1014. } finally {
  1015. if (w != null) {
  1016. w.close();
  1017. }
  1018. }
  1019. }
  1020. }