EMMA Coverage Report (generated Thu Nov 26 15:54:18 CST 2009)
[all classes][org.eclipse.pde.api.tools.internal.model]

COVERAGE SUMMARY FOR SOURCE FILE [StubArchiveApiTypeContainer.java]

nameclass, %method, %block, %line, %
StubArchiveApiTypeContainer.java100% (2/2)42%  (8/19)38%  (181/481)42%  (48.3/116)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class StubArchiveApiTypeContainer$ArchiveApiTypeRoot100% (1/1)43%  (3/7)27%  (42/157)32%  (10.3/32)
compareTo (Object): int 0%   (0/1)0%   (0/7)0%   (0/1)
equals (Object): boolean 0%   (0/1)0%   (0/14)0%   (0/4)
getTypeName (): String 0%   (0/1)0%   (0/21)0%   (0/3)
hashCode (): int 0%   (0/1)0%   (0/4)0%   (0/1)
getContents (): byte [] 100% (1/1)30%  (30/99)36%  (7.3/20)
StubArchiveApiTypeContainer$ArchiveApiTypeRoot (StubArchiveApiTypeContainer, ... 100% (1/1)100% (5/5)100% (2/2)
getStructure (): IApiType 100% (1/1)100% (7/7)100% (1/1)
     
class StubArchiveApiTypeContainer100% (1/1)42%  (5/12)43%  (139/324)45%  (38/84)
accept (ApiTypeContainerVisitor): void 0%   (0/1)0%   (0/92)0%   (0/23)
equals (Object): boolean 0%   (0/1)0%   (0/12)0%   (0/3)
findTypeRoot (String, String): IApiTypeRoot 0%   (0/1)0%   (0/4)0%   (0/1)
getContainerType (): int 0%   (0/1)0%   (0/2)0%   (0/1)
getPackageNames (): String [] 0%   (0/1)0%   (0/34)0%   (0/9)
hashCode (): int 0%   (0/1)0%   (0/4)0%   (0/1)
toString (): String 0%   (0/1)0%   (0/18)0%   (0/3)
open (): ZipFile 100% (1/1)54%  (14/26)60%  (3/5)
close (): void 100% (1/1)69%  (11/16)67%  (4/6)
findTypeRoot (String): IApiTypeRoot 100% (1/1)95%  (38/40)91%  (10/11)
StubArchiveApiTypeContainer (IApiElement, String): void 100% (1/1)100% (12/12)100% (4/4)
init (): void 100% (1/1)100% (64/64)100% (17/17)

1/*******************************************************************************
2 * Copyright (c) 2009 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 * 
8 * Contributors:
9 *     IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.pde.api.tools.internal.model;
12 
13import java.io.IOException;
14import java.io.InputStream;
15import java.util.ArrayList;
16import java.util.Arrays;
17import java.util.Collections;
18import java.util.Enumeration;
19import java.util.HashMap;
20import java.util.HashSet;
21import java.util.Iterator;
22import java.util.List;
23import java.util.Map;
24import java.util.Set;
25import java.util.zip.ZipEntry;
26import java.util.zip.ZipFile;
27 
28import org.eclipse.core.runtime.CoreException;
29import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
30import org.eclipse.pde.api.tools.internal.provisional.model.ApiTypeContainerVisitor;
31import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement;
32import org.eclipse.pde.api.tools.internal.provisional.model.IApiType;
33import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer;
34import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot;
35import org.eclipse.pde.api.tools.internal.util.Util;
36 
37/**
38 * {@link IApiTypeContainer} container for an archive (jar or zip) file.
39 * 
40 * @since 1.0.0
41 */
42public class StubArchiveApiTypeContainer extends ApiElement implements IApiTypeContainer {
43                
44        /**
45         * {@link IApiTypeRoot} implementation within an archive
46         */
47        static class ArchiveApiTypeRoot extends AbstractApiTypeRoot implements Comparable {
48                
49                private String fTypeName;
50 
51                /* (non-Javadoc)
52                 * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeRoot#getStructure()
53                 */
54                public IApiType getStructure() throws CoreException {
55                        return TypeStructureBuilder.buildStubTypeStructure(getContents(), getApiComponent(), this);
56                }
57                /**
58                 * Constructs a new handle to an {@link IApiTypeRoot} in the archive.
59                 * 
60                 * @param container archive
61                 * @param entryName zip entry name
62                 */
63                public ArchiveApiTypeRoot(StubArchiveApiTypeContainer container, String entryName) {
64                        super(container, entryName);
65                }
66 
67                /* (non-Javadoc)
68                 * @see org.eclipse.pde.api.tools.manifest.IClassFile#getTypeName()
69                 */
70                public String getTypeName() {
71                        if (fTypeName == null) {
72                                fTypeName = getName().replace('/', '.').substring(0, getName().length() - Util.DOT_CLASS_SUFFIX.length()); 
73                        }
74                        return fTypeName; 
75                }
76 
77                /* (non-Javadoc)
78                 * @see java.lang.Comparable#compareTo(java.lang.Object)
79                 */
80                public int compareTo(Object o) {
81                        return getTypeName().compareTo(((ArchiveApiTypeRoot)o).getTypeName());
82                }
83                
84                /**
85                 * @see java.lang.Object#equals(java.lang.Object)
86                 */
87                public boolean equals(Object obj) {
88                        if (obj instanceof ArchiveApiTypeRoot) {
89                                ArchiveApiTypeRoot classFile = (ArchiveApiTypeRoot) obj;
90                                return this.getName().equals(classFile.getName());
91                        }
92                        return false;
93                }
94                
95                /**
96                 * @see java.lang.Object#hashCode()
97                 */
98                public int hashCode() {
99                        return getName().hashCode();
100                }
101 
102                /* (non-Javadoc)
103                 * @see org.eclipse.pde.api.tools.internal.model.AbstractApiTypeRoot#getContents()
104                 */
105                public byte[] getContents() throws CoreException {
106                        StubArchiveApiTypeContainer archive = (StubArchiveApiTypeContainer) getParent();
107                        ZipFile zipFile = archive.open();
108                        ZipEntry entry = zipFile.getEntry(getName());
109                        InputStream stream = null;
110                        if (entry != null) {
111                                try {
112                                        stream = zipFile.getInputStream(entry);
113                                } catch (IOException e) {
114                                        abort("Failed to open class file: " + getTypeName() + " in archive: " + archive.fLocation, e); //$NON-NLS-1$ //$NON-NLS-2$
115                                        return null;
116                                }
117                                try {
118                                        return Util.getInputStreamAsByteArray(stream, -1);
119                                }
120                                catch(IOException ioe) {
121                                        abort("Unable to read class file: " + getTypeName(), ioe); //$NON-NLS-1$
122                                        return null; // never gets here
123                                }
124                                finally {
125                                        try {
126                                                stream.close();
127                                        } catch (IOException e) {
128                                                ApiPlugin.log(e);
129                                        }
130                                }
131                        }
132                        abort("Class file not found: " + getTypeName() + " in archive: " + archive.fLocation, null); //$NON-NLS-1$ //$NON-NLS-2$
133                        return null;
134                }
135        }
136        
137        /**
138         * Location of the archive in the local file system.
139         */
140        String fLocation;
141        
142        /**
143         * Cache of package names to class file paths in that package,
144         * or <code>null</code> if not yet initialized.
145         */
146        private Map fPackages;
147        
148        /**
149         * Cache of package names in this archive.
150         */
151        private String[] fPackageNames;
152        
153        /**
154         * Open zip file, or <code>null</code> if file is currently closed.
155         */
156        private ZipFile fZipFile = null;
157 
158        /**
159         * Constructs an {@link IApiTypeContainer} container for the given jar or zip file
160         * at the specified location.
161         * 
162         * @param parent the parent {@link IApiElement} or <code>null</code> if none
163         * @param path location of the file in the local file system
164         */
165        public StubArchiveApiTypeContainer(IApiElement parent, String path) {
166                super(parent, IApiElement.API_TYPE_CONTAINER, path);
167                this.fLocation = path;
168        }
169 
170        /**
171         * @see org.eclipse.pde.api.tools.internal.AbstractApiTypeContainer#accept(org.eclipse.pde.api.tools.internal.provisional.ApiTypeContainerVisitor)
172         */
173        public void accept(ApiTypeContainerVisitor visitor) throws CoreException {
174                if(visitor.visit(this)) {
175                        init();
176                        List packages = new ArrayList(fPackages.keySet());
177                        Collections.sort(packages);
178                        Iterator iterator = packages.iterator();
179                        while (iterator.hasNext()) {
180                                String pkg = (String) iterator.next();
181                                if (visitor.visitPackage(pkg)) {
182                                        List types = new ArrayList((Set) fPackages.get(pkg));
183                                        Iterator cfIterator = types.iterator();
184                                        List classFiles = new ArrayList(types.size());
185                                        while (cfIterator.hasNext()) {
186                                                String entryName = (String) cfIterator.next();
187                                                classFiles.add(new ArchiveApiTypeRoot(this, entryName));
188                                        }
189                                        Collections.sort(classFiles);
190                                        cfIterator = classFiles.iterator();
191                                        while (cfIterator.hasNext()) {
192                                                ArchiveApiTypeRoot classFile = (ArchiveApiTypeRoot) cfIterator.next();
193                                                visitor.visit(pkg, classFile);
194                                                visitor.end(pkg, classFile);
195                                        }
196                                }
197                                visitor.endVisitPackage(pkg);
198                        }
199                }
200                visitor.end(this);
201        }
202 
203        /**
204         * @see java.lang.Object#toString()
205         */
206        public String toString() {
207                StringBuffer buff = new StringBuffer();
208                buff.append("Archive Class File Container: "+getName()); //$NON-NLS-1$
209                return buff.toString();
210        }
211        
212        /**
213         * @see org.eclipse.pde.api.tools.internal.AbstractApiTypeContainer#close()
214         */
215        public synchronized void close() throws CoreException {
216                if (fZipFile != null) {
217                        try {
218                                fZipFile.close();
219                                fZipFile = null;
220                        } catch (IOException e) {
221                                abort("Failed to close class file archive", e); //$NON-NLS-1$
222                        }
223                }
224        }
225 
226        /**
227         * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#findTypeRoot(java.lang.String)
228         */
229        public IApiTypeRoot findTypeRoot(String qualifiedName) throws CoreException {
230                init();
231                int index = qualifiedName.lastIndexOf('.');
232                String packageName = Util.DEFAULT_PACKAGE_NAME;
233                if (index >= 0) {
234                        packageName = qualifiedName.substring(0, index);
235                }
236                Set classFileNames = (Set) fPackages.get(packageName);
237                if (classFileNames != null) {
238                        String fileName = qualifiedName.replace('.', '/');
239                        if (classFileNames.contains(fileName)) {
240                                return new ArchiveApiTypeRoot(this, fileName);
241                        }
242                }
243                return null;
244        }
245 
246        /**
247         * @see org.eclipse.pde.api.tools.internal.AbstractApiTypeContainer#getPackageNames()
248         */
249        public String[] getPackageNames() throws CoreException {
250                init();
251                synchronized (this) {
252                        if (fPackageNames == null) {
253                                Set names = fPackages.keySet();
254                                String[] result = new String[names.size()];
255                                names.toArray(result);
256                                Arrays.sort(result);
257                                fPackageNames = result;
258                        }
259                        return fPackageNames;
260                }
261        }
262        
263        /**
264         * Initializes cache of packages and types.
265         * 
266         * @throws CoreException
267         */
268        private synchronized void init() throws CoreException {
269                ZipFile zipFile = open();
270                if (fPackages == null) {
271                        fPackages = new HashMap();
272                        Enumeration entries = zipFile.entries();
273                        while (entries.hasMoreElements()) {
274                                ZipEntry entry = (ZipEntry) entries.nextElement();
275                                String name = entry.getName();
276                                String pkg = Util.DEFAULT_PACKAGE_NAME;
277                                int index = name.lastIndexOf('/');
278                                if (index >= 0) {
279                                        pkg = name.substring(0, index).replace('/', '.');
280                                }
281                                Set fileNames = (Set) fPackages.get(pkg);
282                                if (fileNames == null) {
283                                        fileNames = new HashSet();
284                                        fPackages.put(pkg, fileNames);
285                                }
286                                fileNames.add(name);
287                        }
288                }
289        }
290        
291        /**
292         * Returns an open zip file for this archive.
293         * 
294         * @return zip file
295         * @throws IOException if unable to open the archive
296         */
297        synchronized ZipFile open() throws CoreException {
298                if (fZipFile == null) {
299                        try {
300                                fZipFile = new ZipFile(fLocation);
301                        } catch (IOException e) {
302                                abort("Failed to open archive: " + fLocation, e); //$NON-NLS-1$
303                        }
304                }
305                return fZipFile;
306        }
307 
308        /* (non-Javadoc)
309         * @see java.lang.Object#equals(java.lang.Object)
310         */
311        public boolean equals(Object obj) {
312                if (obj instanceof StubArchiveApiTypeContainer) {
313                        return this.fLocation.equals(((StubArchiveApiTypeContainer) obj).fLocation);
314                }
315                return false;
316        }
317        /* (non-Javadoc)
318         * @see java.lang.Object#hashCode()
319         */
320        public int hashCode() {
321                return this.fLocation.hashCode();
322        }
323 
324        /**
325         * @see org.eclipse.pde.api.tools.internal.provisional.IApiTypeContainer#findTypeRoot(java.lang.String, java.lang.String)
326         */
327        public IApiTypeRoot findTypeRoot(String qualifiedName, String id) throws CoreException {
328                return findTypeRoot(qualifiedName);
329        }
330        
331        /* (non-Javadoc)
332         * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer#getContainerType()
333         */
334        public int getContainerType() {
335                return ARCHIVE;
336        }
337}

[all classes][org.eclipse.pde.api.tools.internal.model]
EMMA 2.0.5312 EclEmma Fix 1 (C) Vladimir Roubtsov