1 | /*
|
---|
2 | * Copyright 2002,2004-2005 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 |
|
---|
19 | import java.io.File;
|
---|
20 | import java.text.ParseException;
|
---|
21 | import java.util.jar.Manifest;
|
---|
22 | import org.apache.tools.ant.BuildException;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Utility class to output the information in a jar relating
|
---|
26 | * to "Optional Packages" (formely known as "Extensions")
|
---|
27 | * and Package Specifications.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | class LibraryDisplayer {
|
---|
31 | /**
|
---|
32 | * Display the extensions and specifications contained
|
---|
33 | * within specified file.
|
---|
34 | *
|
---|
35 | * @param file the file
|
---|
36 | * @throws BuildException if fail to read file
|
---|
37 | */
|
---|
38 | void displayLibrary(final File file)
|
---|
39 | throws BuildException {
|
---|
40 | final Manifest manifest = ExtensionUtil.getManifest(file);
|
---|
41 | displayLibrary(file, manifest);
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Display the extensions and specifications contained
|
---|
46 | * within specified file.
|
---|
47 | *
|
---|
48 | * @param file the file to use while reporting
|
---|
49 | * @param manifest the manifest of file
|
---|
50 | * @throws BuildException if fail to read file
|
---|
51 | */
|
---|
52 | void displayLibrary(final File file,
|
---|
53 | final Manifest manifest)
|
---|
54 | throws BuildException {
|
---|
55 | final Extension[] available = Extension.getAvailable(manifest);
|
---|
56 | final Extension[] required = Extension.getRequired(manifest);
|
---|
57 | final Extension[] options = Extension.getOptions(manifest);
|
---|
58 | final Specification[] specifications = getSpecifications(manifest);
|
---|
59 |
|
---|
60 | if (0 == available.length && 0 == required.length && 0 == options.length
|
---|
61 | && 0 == specifications.length) {
|
---|
62 | return;
|
---|
63 | }
|
---|
64 |
|
---|
65 | final String message = "File: " + file;
|
---|
66 | final int size = message.length();
|
---|
67 | printLine(size);
|
---|
68 | System.out.println(message);
|
---|
69 | printLine(size);
|
---|
70 | if (0 != available.length) {
|
---|
71 | System.out.println("Extensions Supported By Library:");
|
---|
72 | for (int i = 0; i < available.length; i++) {
|
---|
73 | final Extension extension = available[ i ];
|
---|
74 | System.out.println(extension.toString());
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (0 != required.length) {
|
---|
79 | System.out.println("Extensions Required By Library:");
|
---|
80 | for (int i = 0; i < required.length; i++) {
|
---|
81 | final Extension extension = required[ i ];
|
---|
82 | System.out.println(extension.toString());
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (0 != options.length) {
|
---|
87 | System.out.println("Extensions that will be used by Library if present:");
|
---|
88 | for (int i = 0; i < options.length; i++) {
|
---|
89 | final Extension extension = options[ i ];
|
---|
90 | System.out.println(extension.toString());
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (0 != specifications.length) {
|
---|
95 | System.out.println("Specifications Supported By Library:");
|
---|
96 | for (int i = 0; i < specifications.length; i++) {
|
---|
97 | final Specification specification = specifications[ i ];
|
---|
98 | displaySpecification(specification);
|
---|
99 | }
|
---|
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 | }
|
---|