KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > extension > LibraryDisplayer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.extension;
19
20 import java.io.File JavaDoc;
21 import java.text.ParseException JavaDoc;
22 import java.util.jar.Manifest JavaDoc;
23 import org.apache.tools.ant.BuildException;
24
25 /**
26  * Utility class to output the information in a jar relating
27  * to "Optional Packages" (formely known as "Extensions")
28  * and Package Specifications.
29  *
30  */

31 class LibraryDisplayer {
32     /**
33      * Display the extensions and specifications contained
34      * within specified file.
35      *
36      * @param file the file
37      * @throws BuildException if fail to read file
38      */

39     void displayLibrary(final File JavaDoc file)
40         throws BuildException {
41         final Manifest JavaDoc manifest = ExtensionUtil.getManifest(file);
42         displayLibrary(file, manifest);
43     }
44
45     /**
46      * Display the extensions and specifications contained
47      * within specified file.
48      *
49      * @param file the file to use while reporting
50      * @param manifest the manifest of file
51      * @throws BuildException if fail to read file
52      */

53     void displayLibrary(final File JavaDoc file,
54                          final Manifest JavaDoc manifest)
55         throws BuildException {
56         final Extension[] available = Extension.getAvailable(manifest);
57         final Extension[] required = Extension.getRequired(manifest);
58         final Extension[] options = Extension.getOptions(manifest);
59         final Specification[] specifications = getSpecifications(manifest);
60
61         if (0 == available.length && 0 == required.length && 0 == options.length
62             && 0 == specifications.length) {
63             return;
64         }
65
66         final String JavaDoc message = "File: " + file;
67         final int size = message.length();
68         printLine(size);
69         System.out.println(message);
70         printLine(size);
71         if (0 != available.length) {
72             System.out.println("Extensions Supported By Library:");
73             for (int i = 0; i < available.length; i++) {
74                 final Extension extension = available[ i ];
75                 System.out.println(extension.toString());
76             }
77         }
78
79         if (0 != required.length) {
80             System.out.println("Extensions Required By Library:");
81             for (int i = 0; i < required.length; i++) {
82                 final Extension extension = required[ i ];
83                 System.out.println(extension.toString());
84             }
85         }
86
87         if (0 != options.length) {
88             System.out.println("Extensions that will be used by Library if present:");
89             for (int i = 0; i < options.length; i++) {
90                 final Extension extension = options[ i ];
91                 System.out.println(extension.toString());
92             }
93         }
94
95         if (0 != specifications.length) {
96             System.out.println("Specifications Supported By Library:");
97             for (int i = 0; i < specifications.length; i++) {
98                 final Specification specification = specifications[ i ];
99                 displaySpecification(specification);
100             }
101         }
102     }
103
104     /**
105      * Print out a line of '-'s equal to specified size.
106      *
107      * @param size the number of dashes to printout
108      */

109     private void printLine(final int size) {
110         for (int i = 0; i < size; i++) {
111             System.out.print("-");
112         }
113         System.out.println();
114     }
115
116     /**
117      * Get specifications from manifest.
118      *
119      * @param manifest the manifest
120      * @return the specifications or null if none
121      * @throws BuildException if malformed specification sections
122      */

123     private Specification[] getSpecifications(final Manifest JavaDoc manifest)
124         throws BuildException {
125         try {
126             return Specification.getSpecifications(manifest);
127         } catch (final ParseException JavaDoc pe) {
128             throw new BuildException(pe.getMessage(), pe);
129         }
130     }
131
132     /**
133      * Print out specification details.
134      *
135      * @param specification the specification
136      */

137     private void displaySpecification(final Specification specification) {
138         final String JavaDoc[] sections = specification.getSections();
139         if (null != sections) {
140             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Sections: ");
141             for (int i = 0; i < sections.length; i++) {
142                 sb.append(" ");
143                 sb.append(sections[ i ]);
144             }
145             System.out.println(sb);
146         }
147         System.out.println(specification.toString());
148     }
149 }
150
Popular Tags