KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > VFS


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.virtual;
23
24 import java.io.IOException JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.jboss.virtual.plugins.vfs.helpers.WrappingVirtualFileHandlerVisitor;
30 import org.jboss.virtual.spi.VFSContext;
31 import org.jboss.virtual.spi.VFSContextFactory;
32 import org.jboss.virtual.spi.VFSContextFactoryLocator;
33 import org.jboss.virtual.spi.VirtualFileHandler;
34
35 /**
36  * Virtual File System
37  *
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 1.1 $
41  */

42 public class VFS
43 {
44    /** The VFS Context */
45    private final VFSContext context;
46
47    /**
48     * Get the virtual file system for a root uri
49     *
50     * @param rootURI the root URI
51     * @return the virtual file system
52     * @throws IOException if there is a problem accessing the VFS
53     * @throws IllegalArgumentException if the rootURL is null
54     */

55    public static VFS getVFS(URI JavaDoc rootURI) throws IOException JavaDoc
56    {
57       VFSContextFactory factory = VFSContextFactoryLocator.getFactory(rootURI);
58       if (factory == null)
59          throw new IOException JavaDoc("No context factory for " + rootURI);
60       VFSContext context = factory.getVFS(rootURI);
61       return context.getVFS();
62    }
63
64    /**
65     * Get the root virtual file
66     *
67     * @param rootURI the root uri
68     * @return the virtual file
69     * @throws IOException if there is a problem accessing the VFS
70     * @throws IllegalArgumentException if the rootURL
71     */

72    public static VirtualFile getRoot(URI JavaDoc rootURI) throws IOException JavaDoc
73    {
74       VFS vfs = getVFS(rootURI);
75       return vfs.getRoot();
76    }
77
78    /**
79     * Get a virtual file
80     *
81     * @param rootURI the root uri
82     * @param name the path name
83     * @return the virtual file
84     * @throws IOException if there is a problem accessing the VFS
85     * @throws IllegalArgumentException if the rootURL or name is null
86     */

87    public static VirtualFile getVirtualFile(URI JavaDoc rootURI, String JavaDoc name) throws IOException JavaDoc
88    {
89       VFS vfs = getVFS(rootURI);
90       return vfs.findChild(name);
91    }
92
93    /**
94     * Get the virtual file system for a root url
95     *
96     * @param rootURL the root url
97     * @return the virtual file system
98     * @throws IOException if there is a problem accessing the VFS
99     * @throws IllegalArgumentException if the rootURL is null
100     */

101    public static VFS getVFS(URL JavaDoc rootURL) throws IOException JavaDoc
102    {
103       VFSContextFactory factory = VFSContextFactoryLocator.getFactory(rootURL);
104       if (factory == null)
105          throw new IOException JavaDoc("No context factory for " + rootURL);
106       VFSContext context = factory.getVFS(rootURL);
107       return context.getVFS();
108    }
109
110    /**
111     * Get the root virtual file
112     *
113     * @param rootURL the root url
114     * @return the virtual file
115     * @throws IOException if there is a problem accessing the VFS
116     * @throws IllegalArgumentException if the rootURL
117     */

118    public static VirtualFile getRoot(URL JavaDoc rootURL) throws IOException JavaDoc
119    {
120       VFS vfs = getVFS(rootURL);
121       return vfs.getRoot();
122    }
123
124    /**
125     * Get a virtual file
126     *
127     * @param rootURL the root url
128     * @param name the path name
129     * @return the virtual file
130     * @throws IOException if there is a problem accessing the VFS
131     * @throws IllegalArgumentException if the rootURL or name is null
132     */

133    public static VirtualFile getVirtualFile(URL JavaDoc rootURL, String JavaDoc name) throws IOException JavaDoc
134    {
135       VFS vfs = getVFS(rootURL);
136       return vfs.findChild(name);
137    }
138
139    /**
140     * Create a new VFS.
141     *
142     * @param context the context
143     * @throws IllegalArgumentException for a null context
144     */

145    public VFS(VFSContext context)
146    {
147       if (context == null)
148          throw new IllegalArgumentException JavaDoc("Null name");
149       this.context = context;
150    }
151    
152    /**
153     * Get the root file of this VFS
154     *
155     * @return the root
156     * @throws IOException for any problem accessing the VFS
157     */

158    public VirtualFile getRoot() throws IOException JavaDoc
159    {
160       VirtualFileHandler handler = context.getRoot();
161       return handler.getVirtualFile();
162    }
163    
164    /**
165     * Find a child from the root
166     *
167     * @param path the child path
168     * @return the child
169     * @throws IOException for any problem accessing the VFS (including the child does not exist)
170     * @throws IllegalArgumentException if the path is null
171     */

172    public VirtualFile findChild(String JavaDoc path) throws IOException JavaDoc
173    {
174       if (path == null)
175          throw new IllegalArgumentException JavaDoc("Null path");
176       
177       VirtualFileHandler handler = context.getRoot();
178       path = VFSUtils.fixName(path);
179       VirtualFileHandler result = context.findChild(handler, path);
180       return result.getVirtualFile();
181    }
182    
183    /**
184     * Find a child from the root
185     *
186     * @Deprecated use {@link #findChild(String)}
187     * @param path the child path
188     * @return the child
189     * @throws IOException for any problem accessing the VFS (including the child does not exist)
190     * @throws IllegalArgumentException if the path is null
191     */

192    @Deprecated JavaDoc
193    public VirtualFile findChildFromRoot(String JavaDoc path) throws IOException JavaDoc
194    {
195       return findChild(path);
196    }
197    
198    /**
199     * Get the children
200     *
201     * @return the children
202     * @throws IOException for any problem accessing the virtual file system
203     * @throws IllegalStateException if the root is a leaf node
204     */

205    public List JavaDoc<VirtualFile> getChildren() throws IOException JavaDoc
206    {
207       return getRoot().getChildren(null);
208    }
209
210    /**
211     * Get the children
212     *
213     * @param filter to filter the children
214     * @return the children
215     * @throws IOException for any problem accessing the virtual file system
216     * @throws IllegalStateException if the root is a leaf node
217     */

218    public List JavaDoc<VirtualFile> getChildren(VirtualFileFilter filter) throws IOException JavaDoc
219    {
220       return getRoot().getChildren(filter);
221    }
222    
223    /**
224     * Get all the children recursively<p>
225     *
226     * This always uses {@link VisitorAttributes#RECURSE}
227     *
228     * @return the children
229     * @throws IOException for any problem accessing the virtual file system
230     * @throws IllegalStateException if the root is a leaf node
231     */

232    public List JavaDoc<VirtualFile> getChildrenRecursively() throws IOException JavaDoc
233    {
234       return getRoot().getChildrenRecursively(null);
235    }
236    
237    /**
238     * Get all the children recursively<p>
239     *
240     * This always uses {@link VisitorAttributes#RECURSE}
241     *
242     * @param filter to filter the children
243     * @return the children
244     * @throws IOException for any problem accessing the virtual file system
245     * @throws IllegalStateException if the root is a leaf node
246     */

247    public List JavaDoc<VirtualFile> getChildrenRecursively(VirtualFileFilter filter) throws IOException JavaDoc
248    {
249       return getRoot().getChildrenRecursively(filter);
250    }
251    
252    /**
253     * Visit the virtual file system from the root
254     *
255     * @param visitor the visitor
256     * @throws IOException for any problem accessing the VFS
257     * @throws IllegalArgumentException if the visitor is null
258     * @throws IllegalStateException if the root is a leaf node
259     */

260    public void visit(VirtualFileVisitor visitor) throws IOException JavaDoc
261    {
262       VirtualFileHandler handler = context.getRoot();
263       if (handler.isLeaf())
264          throw new IllegalStateException JavaDoc("File cannot contain children: " + handler);
265       
266       WrappingVirtualFileHandlerVisitor wrapper = new WrappingVirtualFileHandlerVisitor(visitor);
267       context.visit(handler, wrapper);
268    }
269
270    /**
271     * Visit the virtual file system
272     *
273     * @param file the file
274     * @param visitor the visitor
275     * @throws IOException for any problem accessing the VFS
276     * @throws IllegalArgumentException if the file or visitor is null
277     * @throws IllegalStateException if the root is a leaf node
278     */

279    protected void visit(VirtualFile file, VirtualFileVisitor visitor) throws IOException JavaDoc
280    {
281       if (file == null)
282          throw new IllegalArgumentException JavaDoc("Null file");
283
284       VirtualFileHandler handler = file.getHandler();
285       WrappingVirtualFileHandlerVisitor wrapper = new WrappingVirtualFileHandlerVisitor(visitor);
286       VFSContext handlerContext = handler.getVFSContext();
287       handlerContext.visit(handler, wrapper);
288    }
289
290    @Override JavaDoc
291    public String JavaDoc toString()
292    {
293       return context.toString();
294    }
295
296    @Override JavaDoc
297    public int hashCode()
298    {
299       return context.hashCode();
300    }
301    
302    @Override JavaDoc
303    public boolean equals(Object JavaDoc obj)
304    {
305       if (obj == this)
306          return true;
307       if (obj == null || obj instanceof VFS == false)
308          return false;
309       VFS other = (VFS) obj;
310       return context.equals(other.context);
311    }
312 }
313
Popular Tags