KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > TarScanner


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
19 package org.apache.tools.ant.types;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.types.resources.TarResource;
26 import org.apache.tools.tar.TarEntry;
27 import org.apache.tools.tar.TarInputStream;
28
29 /**
30  * Scans tar archives for resources.
31  */

32 public class TarScanner extends ArchiveScanner {
33
34     /**
35      * Fills the file and directory maps with resources read from the
36      * archive.
37      *
38      * @param src the archive to scan.
39      * @param encoding encoding used to encode file names inside the archive.
40      * @param fileEntries Map (name to resource) of non-directory
41      * resources found inside the archive.
42      * @param matchFileEntries Map (name to resource) of non-directory
43      * resources found inside the archive that matched all include
44      * patterns and didn't match any exclude patterns.
45      * @param dirEntries Map (name to resource) of directory
46      * resources found inside the archive.
47      * @param matchDirEntries Map (name to resource) of directory
48      * resources found inside the archive that matched all include
49      * patterns and didn't match any exclude patterns.
50      */

51     protected void fillMapsFromArchive(Resource src, String JavaDoc encoding,
52                                        Map JavaDoc fileEntries, Map JavaDoc matchFileEntries,
53                                        Map JavaDoc dirEntries, Map JavaDoc matchDirEntries) {
54         TarEntry entry = null;
55         TarInputStream ti = null;
56
57         try {
58             try {
59                 ti = new TarInputStream(src.getInputStream());
60             } catch (IOException JavaDoc ex) {
61                 throw new BuildException("problem opening " + srcFile, ex);
62             }
63             while ((entry = ti.getNextEntry()) != null) {
64                 Resource r = new TarResource(src, entry);
65                 String JavaDoc name = entry.getName();
66                 if (entry.isDirectory()) {
67                     name = trimSeparator(name);
68                     dirEntries.put(name, r);
69                     if (match(name)) {
70                         matchDirEntries.put(name, r);
71                     }
72                 } else {
73                     fileEntries.put(name, r);
74                     if (match(name)) {
75                         matchFileEntries.put(name, r);
76                     }
77                 }
78             }
79         } catch (IOException JavaDoc ex) {
80             throw new BuildException("problem reading " + srcFile, ex);
81         } finally {
82             if (ti != null) {
83                 try {
84                     ti.close();
85                 } catch (IOException JavaDoc ex) {
86                     // swallow
87
}
88             }
89         }
90     }
91 }
92
Popular Tags