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

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

 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.io.IOException;
021 import java.util.ArrayList;
022 import java.util.Iterator;
023 import java.util.jar.JarFile;
024 import java.util.jar.Manifest;
025 import org.apache.tools.ant.BuildException;
026 import org.apache.tools.ant.DirectoryScanner;
027 import org.apache.tools.ant.Project;
028 import org.apache.tools.ant.types.FileSet;
029 
030 /**
031  * A set of useful methods relating to extensions.
032  *
033  * @version $Revision: 1.4.2.6 $ $Date: 2004/03/09 17:01:45 $
034  */
035 public class ExtensionUtil {
036     /**
037      * Class is not meant to be instantiated.
038      */
039     private ExtensionUtil() {
040     }
041 
042     /**
043      * Convert a list of extensionAdapter objects to extensions.
044      *
045      * @param adapters the list of ExtensionAdapterss to add to convert
046      * @throws BuildException if an error occurs
047      */
048     static ArrayList toExtensions(final ArrayList adapters)
049         throws BuildException {
050         final ArrayList results = new ArrayList();
051 
052         final int size = adapters.size();
053         for (int i = 0; i < size; i++) {
054             final ExtensionAdapter adapter =
055                 (ExtensionAdapter) adapters.get(i);
056             final Extension extension = adapter.toExtension();
057             results.add(extension);
058         }
059 
060         return results;
061     }
062 
063     /**
064      * Generate a list of extensions from a specified fileset.
065      *
066      * @param librarys the list to add extensions to
067      * @param fileset the filesets containing librarys
068      * @throws BuildException if an error occurs
069      */
070     static void extractExtensions(final Project project,
071                                    final ArrayList librarys,
072                                    final ArrayList fileset)
073         throws BuildException {
074         if (!fileset.isEmpty()) {
075             final Extension[] extensions = getExtensions(project,
076                                                           fileset);
077             for (int i = 0; i < extensions.length; i++) {
078                 librarys.add(extensions[ i ]);
079             }
080         }
081     }
082 
083     /**
084      * Retrieve extensions from the specified librarys.
085      *
086      * @param librarys the filesets for librarys
087      * @return the extensions contained in librarys
088      * @throws BuildException if failing to scan librarys
089      */
090     private static Extension[] getExtensions(final Project project,
091                                               final ArrayList librarys)
092         throws BuildException {
093         final ArrayList extensions = new ArrayList();
094         final Iterator iterator = librarys.iterator();
095         while (iterator.hasNext()) {
096             final FileSet fileSet = (FileSet) iterator.next();
097 
098             boolean includeImpl = true;
099             boolean includeURL = true;
100 
101             if (fileSet instanceof LibFileSet) {
102                 LibFileSet libFileSet = (LibFileSet) fileSet;
103                 includeImpl = libFileSet.isIncludeImpl();
104                 includeURL = libFileSet.isIncludeURL();
105             }
106 
107             final DirectoryScanner scanner = fileSet.getDirectoryScanner(project);
108             final File basedir = scanner.getBasedir();
109             final String[] files = scanner.getIncludedFiles();
110             for (int i = 0; i < files.length; i++) {
111                 final File file = new File(basedir, files[ i ]);
112                 loadExtensions(file, extensions, includeImpl, includeURL);
113             }
114         }
115         return (Extension[]) extensions.toArray(new Extension[extensions.size()]);
116     }
117 
118     /**
119      * Load list of available extensions from specified file.
120      *
121      * @param file the file
122      * @param extensionList the list to add available extensions to
123      * @throws BuildException if there is an error
124      */
125     private static void loadExtensions(final File file,
126                                         final ArrayList extensionList,
127                                         final boolean includeImpl,
128                                         final boolean includeURL)
129         throws BuildException {
130         try {
131             final JarFile jarFile = new JarFile(file);
Rate132             final Extension[] extensions =
133                 Extension.getAvailable(jarFile.getManifest());
134             for (int i = 0; i < extensions.length; i++) {
135                 final Extension extension = extensions[ i ];
136                 addExtension(extensionList, extension, includeImpl, includeURL);
137             }
138         } catch (final Exception e) {
139             throw new BuildException(e.getMessage(), e);
140         }
141     }
142 
143     /**
144      * Add extension to list.
145      * If extension should not have implementation details but
146      * does strip them. If extension should not have url but does
147      * then strip it.
148      *
149      * @param extensionList the list of extensions to add to
150      * @param originalExtension the extension
151      * @param includeImpl false to exclude implementation details
152      * @param includeURL false to exclude implementation URL
153      */
154     private static void addExtension(final ArrayList extensionList,
155                                       final Extension originalExtension,
156                                       final boolean includeImpl,
157                                       final boolean includeURL) {
158         Extension extension = originalExtension;
159         if (!includeURL
160             && null != extension.getImplementationURL()) {
161             extension =
162                 new Extension(extension.getExtensionName(),
163                                extension.getSpecificationVersion().toString(),
164                                extension.getSpecificationVendor(),
165                                extension.getImplementationVersion().toString(),
166                                extension.getImplementationVendor(),
167                                extension.getImplementationVendorID(),
168                                null);
169         }
170 
171         final boolean hasImplAttributes =
172             null != extension.getImplementationURL()
173             || null != extension.getImplementationVersion()
174             || null != extension.getImplementationVendorID()
175             || null != extension.getImplementationVendor();
176 
177         if (!includeImpl && hasImplAttributes) {
178             extension =
179                 new Extension(extension.getExtensionName(),
180                                extension.getSpecificationVersion().toString(),
181                                extension.getSpecificationVendor(),
182                                null,
183                                null,
184                                null,
185                                extension.getImplementationURL());
186         }
187 
188         extensionList.add(extension);
189     }
190 
191     /**
192      * retrieve manifest for specified file.
193      *
194      * @param file the file
195      * @return the manifest
196      * @throws BuildException if errror occurs (file not exist,
197      *         file not a jar, manifest not exist in file)
198      */
199     static Manifest getManifest(final File file)
200         throws BuildException {
201         try {
202             final JarFile jarFile = new JarFile(file);
Rate203             Manifest m = jarFile.getManifest();
204             if (m == null) {
205                 throw new BuildException(file + " doesn't have a MANIFEST");
206             }
207             return m;
208         } catch (final IOException ioe) {
209             throw new BuildException(ioe.getMessage(), ioe);
210         }
211     }
212 }

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