KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > classfile > impl > ZipFileCodeBase


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.classfile.impl;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.MissingResourceException JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.zip.ZipEntry JavaDoc;
30 import java.util.zip.ZipException JavaDoc;
31 import java.util.zip.ZipFile JavaDoc;
32
33 import edu.umd.cs.findbugs.classfile.ICodeBaseEntry;
34 import edu.umd.cs.findbugs.classfile.ICodeBaseIterator;
35 import edu.umd.cs.findbugs.classfile.ICodeBaseLocator;
36 import edu.umd.cs.findbugs.classfile.IScannableCodeBase;
37 import edu.umd.cs.findbugs.classfile.ResourceNotFoundException;
38
39 /**
40  * Implementation of ICodeBase to read from a zip file or jar file.
41  *
42  * @author David Hovemeyer
43  */

44 public class ZipFileCodeBase extends AbstractScannableCodeBase {
45     ZipFile JavaDoc zipFile;
46     
47     /**
48      * Constructor.
49      *
50      * @param codeBaseLocator the codebase locator for this codebase
51      * @param file the File containing the zip file (may be a temp file
52      * if the codebase was copied from a nested zipfile in
53      * another codebase)
54      */

55     public ZipFileCodeBase(ICodeBaseLocator codeBaseLocator, File JavaDoc file) throws IOException JavaDoc {
56         super(codeBaseLocator);
57         try {
58         this.zipFile = new ZipFile JavaDoc(file);
59         setLastModifiedTime(file.lastModified());
60         } catch (ZipException JavaDoc e) {
61             throw new ZipException JavaDoc("Error opening " + file);
62         }
63     }
64     
65     /* (non-Javadoc)
66      * @see edu.umd.cs.findbugs.classfile.ICodeBase#lookupResource(java.lang.String)
67      */

68     public ICodeBaseEntry lookupResource(String JavaDoc resourceName) throws ResourceNotFoundException {
69         // Translate resource name, in case a resource name
70
// has been overridden and the resource is being accessed
71
// using the overridden name.
72
resourceName = translateResourceName(resourceName);
73
74     try {
75         ZipEntry JavaDoc entry = zipFile.getEntry(resourceName);
76         if (entry == null) {
77             throw new ResourceNotFoundException(resourceName);
78         }
79         return new ZipFileCodeBaseEntry(this, entry);
80     }
81     catch (IllegalStateException JavaDoc ise) {
82         // zipFile.getEntry() throws IllegalStateException if the zip file has been closed
83
throw new ResourceNotFoundException(resourceName, ise);
84     }
85     }
86     
87     public ICodeBaseIterator iterator() {
88         final Enumeration JavaDoc<? extends ZipEntry JavaDoc> zipEntryEnumerator = zipFile.entries();
89         
90         return new ICodeBaseIterator() {
91             ZipFileCodeBaseEntry nextEntry;
92             
93             public boolean hasNext() {
94                 scanForNextEntry();
95                 return nextEntry != null;
96             }
97
98             /* (non-Javadoc)
99              * @see edu.umd.cs.findbugs.classfile.ICodeBaseIterator#next()
100              */

101             public ICodeBaseEntry next() throws InterruptedException JavaDoc {
102                 scanForNextEntry();
103                 if (nextEntry == null) {
104                     throw new NoSuchElementException JavaDoc();
105                 }
106                 ICodeBaseEntry result = nextEntry;
107                 nextEntry = null;
108                 return result;
109             }
110
111             private void scanForNextEntry() {
112                 while (nextEntry == null) {
113                     if (!zipEntryEnumerator.hasMoreElements()) {
114                         return;
115                     }
116
117                     ZipEntry JavaDoc zipEntry = zipEntryEnumerator.nextElement();
118                 
119                     if (!zipEntry.isDirectory()) {
120                         setLastModifiedTime(zipEntry.getTime());
121                         nextEntry = new ZipFileCodeBaseEntry(ZipFileCodeBase.this, zipEntry);
122                         break;
123                     }
124                 }
125             }
126             
127         };
128     }
129     
130     /* (non-Javadoc)
131      * @see edu.umd.cs.findbugs.classfile.ICodeBase#getPathName()
132      */

133     public String JavaDoc getPathName() {
134         return zipFile.getName();
135     }
136     
137     /* (non-Javadoc)
138      * @see edu.umd.cs.findbugs.classfile.ICodeBase#close()
139      */

140     public void close() {
141         try {
142             zipFile.close();
143         } catch (IOException JavaDoc e) {
144             // Ignore
145
}
146     }
147     
148     /* (non-Javadoc)
149      * @see java.lang.Object#toString()
150      */

151     @Override JavaDoc
152     public String JavaDoc toString() {
153         return zipFile.getName();
154     }
155 }
156
Popular Tags