KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.jar.JarFile JavaDoc;
26 import java.util.jar.Manifest JavaDoc;
27
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.DirectoryScanner;
30 import org.apache.tools.ant.Project;
31 import org.apache.tools.ant.types.FileSet;
32
33 /**
34  * A set of useful methods relating to extensions.
35  *
36  */

37 public class ExtensionUtil {
38     /**
39      * Class is not meant to be instantiated.
40      */

41     private ExtensionUtil() {
42         //all methods static
43
}
44
45     /**
46      * Convert a list of extensionAdapter objects to extensions.
47      *
48      * @param adapters the list of ExtensionAdapterss to add to convert
49      * @throws BuildException if an error occurs
50      */

51     static ArrayList JavaDoc toExtensions(final List JavaDoc adapters)
52         throws BuildException {
53         final ArrayList JavaDoc results = new ArrayList JavaDoc();
54
55         final int size = adapters.size();
56         for (int i = 0; i < size; i++) {
57             final ExtensionAdapter adapter =
58                 (ExtensionAdapter) adapters.get(i);
59             final Extension extension = adapter.toExtension();
60             results.add(extension);
61         }
62
63         return results;
64     }
65
66     /**
67      * Generate a list of extensions from a specified fileset.
68      *
69      * @param libraries the list to add extensions to
70      * @param fileset the filesets containing librarys
71      * @throws BuildException if an error occurs
72      */

73     static void extractExtensions(final Project project,
74                                    final List JavaDoc libraries,
75                                    final List JavaDoc fileset)
76         throws BuildException {
77         if (!fileset.isEmpty()) {
78             final Extension[] extensions = getExtensions(project,
79                                                           fileset);
80             for (int i = 0; i < extensions.length; i++) {
81                 libraries.add(extensions[ i ]);
82             }
83         }
84     }
85
86     /**
87      * Retrieve extensions from the specified libraries.
88      *
89      * @param libraries the filesets for libraries
90      * @return the extensions contained in libraries
91      * @throws BuildException if failing to scan libraries
92      */

93     private static Extension[] getExtensions(final Project project,
94                                               final List JavaDoc libraries)
95         throws BuildException {
96         final ArrayList JavaDoc extensions = new ArrayList JavaDoc();
97         final Iterator JavaDoc iterator = libraries.iterator();
98         while (iterator.hasNext()) {
99             final FileSet fileSet = (FileSet) iterator.next();
100
101             boolean includeImpl = true;
102             boolean includeURL = true;
103
104             if (fileSet instanceof LibFileSet) {
105                 LibFileSet libFileSet = (LibFileSet) fileSet;
106                 includeImpl = libFileSet.isIncludeImpl();
107                 includeURL = libFileSet.isIncludeURL();
108             }
109
110             final DirectoryScanner scanner = fileSet.getDirectoryScanner(project);
111             final File JavaDoc basedir = scanner.getBasedir();
112             final String JavaDoc[] files = scanner.getIncludedFiles();
113             for (int i = 0; i < files.length; i++) {
114                 final File JavaDoc file = new File JavaDoc(basedir, files[ i ]);
115                 loadExtensions(file, extensions, includeImpl, includeURL);
116             }
117         }
118         return (Extension[]) extensions.toArray(new Extension[extensions.size()]);
119     }
120
121     /**
122      * Load list of available extensions from specified file.
123      *
124      * @param file the file
125      * @param extensionList the list to add available extensions to
126      * @throws BuildException if there is an error
127      */

128     private static void loadExtensions(final File JavaDoc file,
129                                         final List JavaDoc extensionList,
130                                         final boolean includeImpl,
131                                         final boolean includeURL)
132         throws BuildException {
133         try {
134             final JarFile JavaDoc jarFile = new JarFile JavaDoc(file);
135             final Extension[] extensions =
136                 Extension.getAvailable(jarFile.getManifest());
137             for (int i = 0; i < extensions.length; i++) {
138                 final Extension extension = extensions[ i ];
139                 addExtension(extensionList, extension, includeImpl, includeURL);
140             }
141         } catch (final Exception JavaDoc e) {
142             throw new BuildException(e.getMessage(), e);
143         }
144     }
145
146     /**
147      * Add extension to list.
148      * If extension should not have implementation details but
149      * does strip them. If extension should not have url but does
150      * then strip it.
151      *
152      * @param extensionList the list of extensions to add to
153      * @param originalExtension the extension
154      * @param includeImpl false to exclude implementation details
155      * @param includeURL false to exclude implementation URL
156      */

157     private static void addExtension(final List JavaDoc extensionList,
158                                       final Extension originalExtension,
159                                       final boolean includeImpl,
160                                       final boolean includeURL) {
161         Extension extension = originalExtension;
162         if (!includeURL
163             && null != extension.getImplementationURL()) {
164             extension =
165                 new Extension(extension.getExtensionName(),
166                                extension.getSpecificationVersion().toString(),
167                                extension.getSpecificationVendor(),
168                                extension.getImplementationVersion().toString(),
169                                extension.getImplementationVendor(),
170                                extension.getImplementationVendorID(),
171                                null);
172         }
173
174         final boolean hasImplAttributes =
175             null != extension.getImplementationURL()
176             || null != extension.getImplementationVersion()
177             || null != extension.getImplementationVendorID()
178             || null != extension.getImplementationVendor();
179
180         if (!includeImpl && hasImplAttributes) {
181             extension =
182                 new Extension(extension.getExtensionName(),
183                                extension.getSpecificationVersion().toString(),
184                                extension.getSpecificationVendor(),
185                                null,
186                                null,
187                                null,
188                                extension.getImplementationURL());
189         }
190
191         extensionList.add(extension);
192     }
193
194     /**
195      * Retrieve manifest for specified file.
196      *
197      * @param file the file
198      * @return the manifest
199      * @throws BuildException if errror occurs (file doesn't exist,
200      * file not a jar, manifest doesn't exist in file)
201      */

202     static Manifest JavaDoc getManifest(final File JavaDoc file)
203         throws BuildException {
204         try {
205             final JarFile JavaDoc jarFile = new JarFile JavaDoc(file);
206             Manifest JavaDoc m = jarFile.getManifest();
207             if (m == null) {
208                 throw new BuildException(file + " doesn't have a MANIFEST");
209             }
210             return m;
211         } catch (final IOException JavaDoc ioe) {
212             throw new BuildException(ioe.getMessage(), ioe);
213         }
214     }
215 }
216
Popular Tags