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.util.Iterator;
022: import java.util.Vector;
023: import org.apache.tools.ant.BuildException;
024: import org.apache.tools.ant.DirectoryScanner;
025: import org.apache.tools.ant.Task;
026: import org.apache.tools.ant.types.FileSet;
027:
028: /**
029: * Displays the "Optional Package" and "Package Specification" information
030: * contained within the specified JARs.
031: *
032: * <p>Prior to JDK1.3, an "Optional Package" was known as an Extension.
033: * The specification for this mechanism is available in the JDK1.3
034: * documentation in the directory
035: * $JDK_HOME/docs/guide/extensions/versioning.html. Alternatively it is
036: * available online at <a href="http://java.sun.com/j2se/1.3/docs/guide/extensions/versioning.html">
037: * http://java.sun.com/j2se/1.3/docs/guide/extensions/versioning.html</a>.</p>
038: *
039: * @ant.task name="jarlib-display"
040: */
041: public class JarLibDisplayTask extends Task {
042: /**
043: * The library to display information about.
044: */
045: private File libraryFile;
046:
047: /**
048: * Filesets specifying all the librarys
049: * to display information about.
050: */
051: private final Vector libraryFileSets = new Vector();
052:
053: /**
054: * The JAR library to display information for.
055: *
056: * @param file The jar library to display information for.
057: */
058: public void setFile(final File file) {
059: this .libraryFile = file;
060: }
061:
062: /**
063: * Adds a set of files about which library data will be displayed.
064: *
065: * @param fileSet a set of files about which library data will be displayed.
066: */
067: public void addFileset(final FileSet fileSet) {
068: libraryFileSets.addElement(fileSet);
069: }
070:
071: /**
072: * Execute the task.
073: *
074: * @throws BuildException if the task fails.
075: */
076: public void execute() throws BuildException {
077: validate();
078:
079: final LibraryDisplayer displayer = new LibraryDisplayer();
080: // Check if list of files to check has been specified
081: if (!libraryFileSets.isEmpty()) {
082: final Iterator iterator = libraryFileSets.iterator();
083: while (iterator.hasNext()) {
084: final FileSet fileSet = (FileSet) iterator.next();
085: final DirectoryScanner scanner = fileSet
086: .getDirectoryScanner(getProject());
087: final File basedir = scanner.getBasedir();
088: final String[] files = scanner.getIncludedFiles();
089: for (int i = 0; i < files.length; i++) {
090: final File file = new File(basedir, files[i]);
091: displayer.displayLibrary(file);
092: }
093: }
094: } else {
095: displayer.displayLibrary(libraryFile);
096: }
097: }
098:
099: /**
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 message = "File attribute not specified.";
107: throw new BuildException(message);
108: }
109: if (null != libraryFile && !libraryFile.exists()) {
110: final String message = "File '" + libraryFile
111: + "' does not exist.";
112: throw new BuildException(message);
113: }
114: if (null != libraryFile && !libraryFile.isFile()) {
115: final String message = "\'" + libraryFile
116: + "\' is not a file.";
117: throw new BuildException(message);
118: }
119: }
120: }
|