001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.extension;
019:
020: import java.io.File;
021: import java.text.ParseException;
022: import java.util.jar.Manifest;
023: import org.apache.tools.ant.BuildException;
024:
025: /**
026: * Utility class to output the information in a jar relating
027: * to "Optional Packages" (formely known as "Extensions")
028: * and Package Specifications.
029: *
030: */
031: class LibraryDisplayer {
032: /**
033: * Display the extensions and specifications contained
034: * within specified file.
035: *
036: * @param file the file
037: * @throws BuildException if fail to read file
038: */
039: void displayLibrary(final File file) throws BuildException {
040: final Manifest manifest = ExtensionUtil.getManifest(file);
041: displayLibrary(file, manifest);
042: }
043:
044: /**
045: * Display the extensions and specifications contained
046: * within specified file.
047: *
048: * @param file the file to use while reporting
049: * @param manifest the manifest of file
050: * @throws BuildException if fail to read file
051: */
052: void displayLibrary(final File file, final Manifest manifest)
053: throws BuildException {
054: final Extension[] available = Extension.getAvailable(manifest);
055: final Extension[] required = Extension.getRequired(manifest);
056: final Extension[] options = Extension.getOptions(manifest);
057: final Specification[] specifications = getSpecifications(manifest);
058:
059: if (0 == available.length && 0 == required.length
060: && 0 == options.length && 0 == specifications.length) {
061: return;
062: }
063:
064: final String message = "File: " + file;
065: final int size = message.length();
066: printLine(size);
067: System.out.println(message);
068: printLine(size);
069: if (0 != available.length) {
070: System.out.println("Extensions Supported By Library:");
071: for (int i = 0; i < available.length; i++) {
072: final Extension extension = available[i];
073: System.out.println(extension.toString());
074: }
075: }
076:
077: if (0 != required.length) {
078: System.out.println("Extensions Required By Library:");
079: for (int i = 0; i < required.length; i++) {
080: final Extension extension = required[i];
081: System.out.println(extension.toString());
082: }
083: }
084:
085: if (0 != options.length) {
086: System.out
087: .println("Extensions that will be used by Library if present:");
088: for (int i = 0; i < options.length; i++) {
089: final Extension extension = options[i];
090: System.out.println(extension.toString());
091: }
092: }
093:
094: if (0 != specifications.length) {
095: System.out.println("Specifications Supported By Library:");
096: for (int i = 0; i < specifications.length; i++) {
097: final Specification specification = specifications[i];
098: displaySpecification(specification);
099: }
100: }
101: }
102:
103: /**
104: * Print out a line of '-'s equal to specified size.
105: *
106: * @param size the number of dashes to printout
107: */
108: private void printLine(final int size) {
109: for (int i = 0; i < size; i++) {
110: System.out.print("-");
111: }
112: System.out.println();
113: }
114:
115: /**
116: * Get specifications from manifest.
117: *
118: * @param manifest the manifest
119: * @return the specifications or null if none
120: * @throws BuildException if malformed specification sections
121: */
122: private Specification[] getSpecifications(final Manifest manifest)
123: throws BuildException {
124: try {
125: return Specification.getSpecifications(manifest);
126: } catch (final ParseException pe) {
127: throw new BuildException(pe.getMessage(), pe);
128: }
129: }
130:
131: /**
132: * Print out specification details.
133: *
134: * @param specification the specification
135: */
136: private void displaySpecification(final Specification specification) {
137: final String[] sections = specification.getSections();
138: if (null != sections) {
139: final StringBuffer sb = new StringBuffer("Sections: ");
140: for (int i = 0; i < sections.length; i++) {
141: sb.append(" ");
142: sb.append(sections[i]);
143: }
144: System.out.println(sb);
145: }
146: System.out.println(specification.toString());
147: }
148: }
|