1. /*
  2. * Copyright 2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.extension;
  18. import java.io.File;
  19. import java.text.ParseException;
  20. import java.util.jar.Manifest;
  21. import org.apache.tools.ant.BuildException;
  22. /**
  23. * Utility class to output the information in a jar relating
  24. * to "Optional Packages" (formely known as "Extensions")
  25. * and Package Specifications.
  26. *
  27. * @version $Revision: 1.4.2.4 $ $Date: 2004/03/09 17:01:45 $
  28. */
  29. class LibraryDisplayer {
  30. /**
  31. * Display the extensions and specifications contained
  32. * within specified file.
  33. *
  34. * @param file the file
  35. * @throws BuildException if fail to read file
  36. */
  37. void displayLibrary(final File file)
  38. throws BuildException {
  39. final Manifest manifest = ExtensionUtil.getManifest(file);
  40. displayLibrary(file, manifest);
  41. }
  42. /**
  43. * Display the extensions and specifications contained
  44. * within specified file.
  45. *
  46. * @param file the file to use while reporting
  47. * @param manifest the manifest of file
  48. * @throws BuildException if fail to read file
  49. */
  50. void displayLibrary(final File file,
  51. final Manifest manifest)
  52. throws BuildException {
  53. final Extension[] available = Extension.getAvailable(manifest);
  54. final Extension[] required = Extension.getRequired(manifest);
  55. final Extension[] options = Extension.getOptions(manifest);
  56. final Specification[] specifications = getSpecifications(manifest);
  57. if (0 == available.length && 0 == required.length && 0 == options.length
  58. && 0 == specifications.length) {
  59. return;
  60. }
  61. final String message = "File: " + file;
  62. final int size = message.length();
  63. printLine(size);
  64. System.out.println(message);
  65. printLine(size);
  66. if (0 != available.length) {
  67. System.out.println("Extensions Supported By Library:");
  68. for (int i = 0; i < available.length; i++) {
  69. final Extension extension = available[ i ];
  70. System.out.println(extension.toString());
  71. }
  72. }
  73. if (0 != required.length) {
  74. System.out.println("Extensions Required By Library:");
  75. for (int i = 0; i < required.length; i++) {
  76. final Extension extension = required[ i ];
  77. System.out.println(extension.toString());
  78. }
  79. }
  80. if (0 != options.length) {
  81. System.out.println("Extensions that will be used by Library if present:");
  82. for (int i = 0; i < options.length; i++) {
  83. final Extension extension = options[ i ];
  84. System.out.println(extension.toString());
  85. }
  86. }
  87. if (0 != specifications.length) {
  88. System.out.println("Specifications Supported By Library:");
  89. for (int i = 0; i < specifications.length; i++) {
  90. final Specification specification = specifications[ i ];
  91. displaySpecification(specification);
  92. }
  93. }
  94. }
  95. /**
  96. * Print out a line of '-'s equal to specified size.
  97. *
  98. * @param size the number of dashes to printout
  99. */
  100. private void printLine(final int size) {
  101. for (int i = 0; i < size; i++) {
  102. System.out.print("-");
  103. }
  104. System.out.println();
  105. }
  106. /**
  107. * Get specifications from manifest.
  108. *
  109. * @param manifest the manifest
  110. * @return the specifications or null if none
  111. * @throws BuildException if malformed specification sections
  112. */
  113. private Specification[] getSpecifications(final Manifest manifest)
  114. throws BuildException {
  115. try {
  116. return Specification.getSpecifications(manifest);
  117. } catch (final ParseException pe) {
  118. throw new BuildException(pe.getMessage(), pe);
  119. }
  120. }
  121. /**
  122. * Print out specification details.
  123. *
  124. * @param specification the specification
  125. */
  126. private void displaySpecification(final Specification specification) {
  127. final String[] sections = specification.getSections();
  128. if (null != sections) {
  129. final StringBuffer sb = new StringBuffer("Sections: ");
  130. for (int i = 0; i < sections.length; i++) {
  131. sb.append(" ");
  132. sb.append(sections[ i ]);
  133. }
  134. System.out.println(sb);
  135. }
  136. System.out.println(specification.toString());
  137. }
  138. }