1 22 package org.jboss.virtual; 23 24 import java.io.IOException ; 25 import java.net.URI ; 26 import java.net.URL ; 27 import java.util.List ; 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 42 public class VFS 43 { 44 45 private final VFSContext context; 46 47 55 public static VFS getVFS(URI rootURI) throws IOException 56 { 57 VFSContextFactory factory = VFSContextFactoryLocator.getFactory(rootURI); 58 if (factory == null) 59 throw new IOException ("No context factory for " + rootURI); 60 VFSContext context = factory.getVFS(rootURI); 61 return context.getVFS(); 62 } 63 64 72 public static VirtualFile getRoot(URI rootURI) throws IOException 73 { 74 VFS vfs = getVFS(rootURI); 75 return vfs.getRoot(); 76 } 77 78 87 public static VirtualFile getVirtualFile(URI rootURI, String name) throws IOException 88 { 89 VFS vfs = getVFS(rootURI); 90 return vfs.findChild(name); 91 } 92 93 101 public static VFS getVFS(URL rootURL) throws IOException 102 { 103 VFSContextFactory factory = VFSContextFactoryLocator.getFactory(rootURL); 104 if (factory == null) 105 throw new IOException ("No context factory for " + rootURL); 106 VFSContext context = factory.getVFS(rootURL); 107 return context.getVFS(); 108 } 109 110 118 public static VirtualFile getRoot(URL rootURL) throws IOException 119 { 120 VFS vfs = getVFS(rootURL); 121 return vfs.getRoot(); 122 } 123 124 133 public static VirtualFile getVirtualFile(URL rootURL, String name) throws IOException 134 { 135 VFS vfs = getVFS(rootURL); 136 return vfs.findChild(name); 137 } 138 139 145 public VFS(VFSContext context) 146 { 147 if (context == null) 148 throw new IllegalArgumentException ("Null name"); 149 this.context = context; 150 } 151 152 158 public VirtualFile getRoot() throws IOException 159 { 160 VirtualFileHandler handler = context.getRoot(); 161 return handler.getVirtualFile(); 162 } 163 164 172 public VirtualFile findChild(String path) throws IOException 173 { 174 if (path == null) 175 throw new IllegalArgumentException ("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 192 @Deprecated 193 public VirtualFile findChildFromRoot(String path) throws IOException 194 { 195 return findChild(path); 196 } 197 198 205 public List <VirtualFile> getChildren() throws IOException 206 { 207 return getRoot().getChildren(null); 208 } 209 210 218 public List <VirtualFile> getChildren(VirtualFileFilter filter) throws IOException 219 { 220 return getRoot().getChildren(filter); 221 } 222 223 232 public List <VirtualFile> getChildrenRecursively() throws IOException 233 { 234 return getRoot().getChildrenRecursively(null); 235 } 236 237 247 public List <VirtualFile> getChildrenRecursively(VirtualFileFilter filter) throws IOException 248 { 249 return getRoot().getChildrenRecursively(filter); 250 } 251 252 260 public void visit(VirtualFileVisitor visitor) throws IOException 261 { 262 VirtualFileHandler handler = context.getRoot(); 263 if (handler.isLeaf()) 264 throw new IllegalStateException ("File cannot contain children: " + handler); 265 266 WrappingVirtualFileHandlerVisitor wrapper = new WrappingVirtualFileHandlerVisitor(visitor); 267 context.visit(handler, wrapper); 268 } 269 270 279 protected void visit(VirtualFile file, VirtualFileVisitor visitor) throws IOException 280 { 281 if (file == null) 282 throw new IllegalArgumentException ("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 291 public String toString() 292 { 293 return context.toString(); 294 } 295 296 @Override 297 public int hashCode() 298 { 299 return context.hashCode(); 300 } 301 302 @Override 303 public boolean equals(Object 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 |