e.g. Calendar Search Help
You must enter a value before pressing Search
ant

Class: org.apache.tools.ant.taskdefs.optional.extension.JarLibAvailableTask   ©

 OK to copy?
001 /*
002  * Copyright  2002-2004 The Apache Software Foundation
003  *
004  *  Licensed under the Apache License, Version 2.0 (the "License");
005  *  you may not use this file except in compliance with the License.
006  *  You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  *  Unless required by applicable law or agreed to in writing, software
011  *  distributed under the License is distributed on an "AS IS" BASIS,
012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  *  See the License for the specific language governing permissions and
014  *  limitations under the License.
015  *
016  */
017 package org.apache.tools.ant.taskdefs.optional.extension;
018 
019 import java.io.File;
020 import java.util.Iterator;
021 import java.util.Vector;
022 import java.util.jar.Manifest;
023 import org.apache.tools.ant.BuildException;
024 import org.apache.tools.ant.Task;
025 
026 /**
027  * Checks whether an extension is present in a fileset or an extensionSet.
028  *
029  * @ant.task name="jarlib-available"
030  */
031 public class JarLibAvailableTask extends Task {
032     /**
033      * The library to display information about.
034      */
035     private File libraryFile;
036 
037     /**
038      * Filesets specifying all the librarys
039      * to display information about.
040      */
041     private final Vector extensionFileSets = new Vector();
042 
043     /**
044      * The name of the property to set if extension is available.
045      */
046     private String propertyName;
047 
048     /**
049      * The extension that is required.
050      */
051     private ExtensionAdapter requiredExtension;
052 
053     /**
054      * The name of property to set if extensions are available.
055      *
056      * @param property The name of property to set if extensions is available.
057      */
058     public void setProperty(final String property) {
059         this.propertyName = property;
060     }
061 
062     /**
063      * The JAR library to check.
064      *
065      * @param file The jar library to check.
066      */
067     public void setFile(final File file) {
068         this.libraryFile = file;
069     }
070 
071     /**
072      * Set the Extension looking for.
073      *
074      * @param extension Set the Extension looking for.
075      */
076     public void addConfiguredExtension(final ExtensionAdapter extension) {
077         if (null != requiredExtension) {
078             final String message = "Can not specify extension to "
079                 + "search for multiple times.";
080             throw new BuildException(message);
081         }
082         requiredExtension = extension;
083     }
084 
085     /**
086      * Adds a set of extensions to search in.
087      *
088      * @param extensionSet a set of extensions to search in.
089      */
090     public void addConfiguredExtensionSet(final ExtensionSet extensionSet) {
091         extensionFileSets.addElement(extensionSet);
092     }
093 
094     /**
095      * Execute the task.
096      *
097      * @throws BuildException if somethign goes wrong.
098      */
099     public void execute() throws BuildException {
100         validate();
101 
102         final Extension test = requiredExtension.toExtension();
103 
104         // Check if list of files to check has been specified
105         if (!extensionFileSets.isEmpty()) {
106             final Iterator iterator = extensionFileSets.iterator();
107             while (iterator.hasNext()) {
108                 final ExtensionSet extensionSet
109                     = (ExtensionSet) iterator.next();
110                 final Extension[] extensions =
111                     extensionSet.toExtensions(getProject());
112                 for (int i = 0; i < extensions.length; i++) {
113                     final Extension extension = extensions[ i ];
114                     if (extension.isCompatibleWith(test)) {
115                         getProject().setNewProperty(propertyName, "true");
116                     }
117                 }
118             }
119         } else {
120             final Manifest manifest = ExtensionUtil.getManifest(libraryFile);
121             final Extension[] extensions = Extension.getAvailable(manifest);
122             for (int i = 0; i < extensions.length; i++) {
123                 final Extension extension = extensions[ i ];
124                 if (extension.isCompatibleWith(test)) {
125                     getProject().setNewProperty(propertyName, "true");
126                 }
127             }
128         }
129     }
130 
131     /**
132      * Validate the tasks parameters.
133      *
134      * @throws BuildException if invalid parameters found
135      */
136     private void validate() throws BuildException {
137         if (null == requiredExtension) {
138             final String message = "Extension element must be specified.";
139             throw new BuildException(message);
140         }
141 
142         if (null == libraryFile && extensionFileSets.isEmpty()) {
143             final String message = "File attribute not specified.";
144             throw new BuildException(message);
145         }
146         if (null != libraryFile && !libraryFile.exists()) {
147             final String message = "File '" + libraryFile + "' does not exist.";
148             throw new BuildException(message);
149         }
Rate150         if (null != libraryFile && !libraryFile.isFile()) {
151             final String message = "\'" + libraryFile + "\' is not a file.";
152             throw new BuildException(message);
153         }
154     }
155 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
150 0% of 0