Coverage Report - org.webslinger.ext.commons.vfs.object.ObjectFileSystem
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectFileSystem
0%
0/29
0%
0/6
0
ObjectFileSystem$1
0%
0/8
N/A
0
 
 1  
 package org.webslinger.ext.commons.vfs.object;
 2  
 
 3  
 import java.io.ByteArrayInputStream;
 4  
 import java.io.ByteArrayOutputStream;
 5  
 import java.io.InputStream;
 6  
 import java.io.IOException;
 7  
 import java.io.OutputStream;
 8  
 import java.io.OutputStreamWriter;
 9  
 import java.io.PrintWriter;
 10  
 import java.util.ArrayList;
 11  
 import java.nio.ByteBuffer;
 12  
 import java.nio.CharBuffer;
 13  
 import java.util.Arrays;
 14  
 import java.util.Collection;
 15  
 import java.util.Collections;
 16  
 import java.util.HashMap;
 17  
 import java.util.Iterator;
 18  
 import java.util.List;
 19  
 import java.util.ListIterator;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.apache.commons.vfs.Capability;
 23  
 import org.apache.commons.vfs.FileName;
 24  
 import org.apache.commons.vfs.FileObject;
 25  
 import org.apache.commons.vfs.FileSystem;
 26  
 import org.apache.commons.vfs.FileSystemException;
 27  
 import org.apache.commons.vfs.FileSystemManager;
 28  
 import org.apache.commons.vfs.FileSystemOptions;
 29  
 import org.apache.commons.vfs.FileType;
 30  
 import org.apache.commons.vfs.impl.VirtualFileName;
 31  
 import org.apache.commons.vfs.provider.AbstractFileSystem;
 32  
 
 33  
 import org.webslinger.collections.CollectionUtil;
 34  
 import org.webslinger.io.Charsets;
 35  
 import org.webslinger.resolver.ObjectWalker;
 36  
 
 37  0
 public class ObjectFileSystem extends AbstractFileSystem {
 38  
     private final Object rootObject;
 39  0
     private final ObjectWalker walker = new ObjectWalker();
 40  
 
 41  
     public ObjectFileSystem(Object rootObject, FileSystemOptions options) throws FileSystemException {
 42  0
         super(new VirtualFileName("object",  FileName.ROOT_PATH, FileType.FOLDER), null, options);
 43  0
         this.rootObject = rootObject;
 44  0
     }
 45  
 
 46  
     public void addCapabilities(Collection caps) {
 47  0
     }
 48  
 
 49  
     public FileObject createFile(FileName name) {
 50  0
         return new ObjectFileObject(this, name);
 51  
     }
 52  
 
 53  
     private Object getObject(String path) {
 54  0
         ListIterator<String> it = CollectionUtil.parse(path, "/");
 55  0
         Object object = walker.get(rootObject, it);
 56  0
         if (object == null || it.hasNext()) return null;
 57  0
         return object;
 58  
     }
 59  
 
 60  
     public InputStream getInputStream(String path) throws Exception {
 61  0
         char[] chars = walker.getChars(rootObject, CollectionUtil.parse(path, "/"));
 62  0
         if (chars == null) return null;
 63  0
         ByteBuffer buf = Charsets.UTF8.encode(CharBuffer.wrap(chars));
 64  0
         byte[] bytes = buf.array();
 65  0
         return new ByteArrayInputStream(bytes, buf.arrayOffset(), buf.limit());
 66  
     }
 67  
 
 68  
     public OutputStream getOutputStream(final String path) throws Exception {
 69  0
         return new ByteArrayOutputStream() {
 70  
             public void close() throws IOException {
 71  0
                 super.close();
 72  
                 try {
 73  0
                     CharBuffer buf = Charsets.UTF8.decode(ByteBuffer.wrap(toByteArray()));
 74  0
                     walker.setChars(rootObject, CollectionUtil.parse(path, "/"), buf.array(), buf.arrayOffset(), buf.limit());
 75  0
                 } catch (Exception e) {
 76  0
                     throw (IOException) new IOException(e.getMessage()).initCause(e);
 77  0
                 }
 78  0
             }
 79  
         };
 80  
     }
 81  
 
 82  
     public FileType getType(String path) {
 83  0
         ListIterator<String> it = CollectionUtil.parse(path, "/");
 84  0
         Object object = walker.get(rootObject, it);
 85  0
         if (object == null || it.hasNext()) return null;
 86  0
         FileType type = walker.hasChildren(object, it) ? FileType.FOLDER : FileType.FILE;
 87  0
         return type;
 88  
     }
 89  
 
 90  
     public FileObject[] getChildren(String path) throws FileSystemException {
 91  0
         ListIterator<String> it = CollectionUtil.parse(path, "/");
 92  0
         String[] names = walker.list(rootObject, it);
 93  0
         if (names == null || it.hasNext()) return null;
 94  0
         FileObject[] children = new FileObject[names.length];
 95  0
         for (int i = 0; i < names.length; i++) {
 96  0
             children[i] = resolveFile(names[i]);
 97  
         }
 98  0
         return children;
 99  
     }
 100  
 }
 101