KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > baseadaptor > bundlefile > DirBundleFile


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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  *******************************************************************************/

11
12 package org.eclipse.osgi.baseadaptor.bundlefile;
13
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.NoSuchElementException JavaDoc;
18 import org.eclipse.osgi.internal.baseadaptor.AdaptorMsg;
19 import org.eclipse.osgi.util.NLS;
20
21 /**
22  * A BundleFile that uses a directory as its base file.
23  * @since 3.2
24  */

25 public class DirBundleFile extends BundleFile {
26
27     /**
28      * Constructs a DirBundleFile
29      * @param basefile the base file
30      * @throws IOException
31      */

32     public DirBundleFile(File JavaDoc basefile) throws IOException JavaDoc {
33         super(basefile);
34         if (!BundleFile.secureAction.exists(basefile) || !BundleFile.secureAction.isDirectory(basefile)) {
35             throw new IOException JavaDoc(NLS.bind(AdaptorMsg.ADAPTOR_DIRECTORY_EXCEPTION, basefile));
36         }
37     }
38
39     public File JavaDoc getFile(String JavaDoc path, boolean nativeCode) {
40         File JavaDoc filePath = new File JavaDoc(this.basefile, path);
41         if (BundleFile.secureAction.exists(filePath)) {
42             return filePath;
43         }
44         return null;
45     }
46
47     public BundleEntry getEntry(String JavaDoc path) {
48         File JavaDoc filePath = new File JavaDoc(this.basefile, path);
49         if (!BundleFile.secureAction.exists(filePath)) {
50             return null;
51         }
52         return new FileBundleEntry(filePath, path);
53     }
54
55     public boolean containsDir(String JavaDoc dir) {
56         File JavaDoc dirPath = new File JavaDoc(this.basefile, dir);
57         return BundleFile.secureAction.exists(dirPath) && BundleFile.secureAction.isDirectory(dirPath);
58     }
59
60     public Enumeration JavaDoc getEntryPaths(String JavaDoc path) {
61         if (path.length() > 0 && path.charAt(0) == '/')
62             path = path.substring(1);
63         final java.io.File JavaDoc pathFile = new java.io.File JavaDoc(basefile, path);
64         if (!BundleFile.secureAction.exists(pathFile))
65             return null;
66         if (!BundleFile.secureAction.isDirectory(pathFile))
67             return null;
68         final String JavaDoc[] fileList = BundleFile.secureAction.list(pathFile);
69         if (fileList == null || fileList.length == 0)
70             return null;
71         final String JavaDoc dirPath = path.length() == 0 || path.charAt(path.length() - 1) == '/' ? path : path + '/';
72         return new Enumeration JavaDoc() {
73             int cur = 0;
74
75             public boolean hasMoreElements() {
76                 return fileList != null && cur < fileList.length;
77             }
78
79             public Object JavaDoc nextElement() {
80                 if (!hasMoreElements()) {
81                     throw new NoSuchElementException JavaDoc();
82                 }
83                 java.io.File JavaDoc childFile = new java.io.File JavaDoc(pathFile, fileList[cur]);
84                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(dirPath).append(fileList[cur++]);
85                 if (BundleFile.secureAction.isDirectory(childFile)) {
86                     sb.append("/"); //$NON-NLS-1$
87
}
88                 return sb.toString();
89             }
90         };
91     }
92
93     public void close() {
94         // nothing to do.
95
}
96
97     public void open() {
98         // nothing to do.
99
}
100 }
101
Popular Tags