KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
22 import java.util.Vector JavaDoc;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.DirectoryScanner;
25 import org.apache.tools.ant.Task;
26 import org.apache.tools.ant.types.FileSet;
27
28 /**
29  * Displays the "Optional Package" and "Package Specification" information
30  * contained within the specified JARs.
31  *
32  * <p>Prior to JDK1.3, an "Optional Package" was known as an Extension.
33  * The specification for this mechanism is available in the JDK1.3
34  * documentation in the directory
35  * $JDK_HOME/docs/guide/extensions/versioning.html. Alternatively it is
36  * available online at <a HREF="http://java.sun.com/j2se/1.3/docs/guide/extensions/versioning.html">
37  * http://java.sun.com/j2se/1.3/docs/guide/extensions/versioning.html</a>.</p>
38  *
39  * @ant.task name="jarlib-display"
40  */

41 public class JarLibDisplayTask extends Task {
42     /**
43      * The library to display information about.
44      */

45     private File JavaDoc libraryFile;
46
47     /**
48      * Filesets specifying all the librarys
49      * to display information about.
50      */

51     private final Vector JavaDoc libraryFileSets = new Vector JavaDoc();
52
53     /**
54      * The JAR library to display information for.
55      *
56      * @param file The jar library to display information for.
57      */

58     public void setFile(final File JavaDoc file) {
59         this.libraryFile = file;
60     }
61
62     /**
63      * Adds a set of files about which library data will be displayed.
64      *
65      * @param fileSet a set of files about which library data will be displayed.
66      */

67     public void addFileset(final FileSet fileSet) {
68         libraryFileSets.addElement(fileSet);
69     }
70
71     /**
72      * Execute the task.
73      *
74      * @throws BuildException if the task fails.
75      */

76     public void execute() throws BuildException {
77         validate();
78
79         final LibraryDisplayer displayer = new LibraryDisplayer();
80         // Check if list of files to check has been specified
81
if (!libraryFileSets.isEmpty()) {
82             final Iterator JavaDoc iterator = libraryFileSets.iterator();
83             while (iterator.hasNext()) {
84                 final FileSet fileSet = (FileSet) iterator.next();
85                 final DirectoryScanner scanner
86                     = fileSet.getDirectoryScanner(getProject());
87                 final File JavaDoc basedir = scanner.getBasedir();
88                 final String JavaDoc[] files = scanner.getIncludedFiles();
89                 for (int i = 0; i < files.length; i++) {
90                     final File JavaDoc file = new File JavaDoc(basedir, files[ i ]);
91                     displayer.displayLibrary(file);
92                 }
93             }
94         } else {
95             displayer.displayLibrary(libraryFile);
96         }
97     }
98
99     /**
100      * Validate the tasks parameters.
101      *
102      * @throws BuildException if invalid parameters found
103      */

104     private void validate() throws BuildException {
105         if (null == libraryFile && libraryFileSets.isEmpty()) {
106             final String JavaDoc message = "File attribute not specified.";
107             throw new BuildException(message);
108         }
109         if (null != libraryFile && !libraryFile.exists()) {
110             final String JavaDoc message = "File '" + libraryFile + "' does not exist.";
111             throw new BuildException(message);
112         }
113         if (null != libraryFile && !libraryFile.isFile()) {
114             final String JavaDoc message = "\'" + libraryFile + "\' is not a file.";
115             throw new BuildException(message);
116         }
117     }
118 }
119
Popular Tags