Coverage Report - org.webslinger.commons.vfs.flat.FlatFileObject
 
Classes in this File Line Coverage Branch Coverage Complexity
FlatFileObject
92%
47/51
100%
4/4
0
FlatFileObject$1
38%
3/8
N/A
0
FlatFileObject$2
50%
2/4
N/A
0
FlatFileObject$3
64%
7/11
100%
2/2
0
 
 1  
 package org.webslinger.commons.vfs.flat;
 2  
 
 3  
 import java.io.InputStream;
 4  
 import java.io.IOException;
 5  
 import java.io.OutputStream;
 6  
 import java.util.ArrayList;
 7  
 import java.util.Map;
 8  
 
 9  
 import org.apache.commons.vfs.FileContent;
 10  
 import org.apache.commons.vfs.FileContentInfo;
 11  
 import org.apache.commons.vfs.FileName;
 12  
 import org.apache.commons.vfs.FileObject;
 13  
 import org.apache.commons.vfs.FileType;
 14  
 import org.apache.commons.vfs.FileSystem;
 15  
 import org.apache.commons.vfs.FileSystemException;
 16  
 import org.apache.commons.vfs.RandomAccessContent;
 17  
 import org.apache.commons.vfs.Selectors;
 18  
 import org.apache.commons.vfs.provider.AbstractFileObject;
 19  
 import org.apache.commons.vfs.util.RandomAccessMode;
 20  
 
 21  
 import org.webslinger.commons.vfs.FilteringFileObject;
 22  
 import org.webslinger.commons.vfs.VFSUtil;
 23  
 import org.webslinger.commons.vfs.handlers.attributes.AttributeMapper;
 24  
 import org.webslinger.commons.vfs.operations.AttributeValueOperation;
 25  
 import org.webslinger.commons.vfs.util.FileAttributeMap;
 26  
 import org.webslinger.commons.vfs.util.FileAttributeResolver;
 27  
 import org.webslinger.util.TTLCachedObject;
 28  
 
 29  1930
 public class FlatFileObject extends FilteringFileObject<FileName, FlatFileObject, FlatFileSystem> {
 30  
     // Add hook for copying/svn
 31  
     private final FlatFileSystem ffs;
 32  1
     protected static final FileAttributeResolver resolver = new FileAttributeResolver<FlatFileObject>() {
 33  
         public String[] getAttributeNames(FlatFileObject file) throws Exception {
 34  71
             return file.getAttributeMapper().getAttributeNames();
 35  
         }
 36  
 
 37  
         public Object getAttribute(FlatFileObject file, String name) throws Exception {
 38  49
             return file.getAttributeMapper().getAttribute(name).getObject();
 39  
         }
 40  
 
 41  
         public void setAttribute(FlatFileObject file, String name, Object value) throws Exception {
 42  0
             file.getAttributeMapper().setAttribute(name, value);
 43  0
         }
 44  
 
 45  
         public boolean attributeExists(FlatFileObject file, String name) throws Exception {
 46  0
             return file.getAttributeMapper().attributeExists(name);
 47  
         }
 48  
 
 49  
         public void removeAttribute(FlatFileObject file, String name) throws Exception {
 50  0
             file.getAttributeMapper().removeAttribute(name);
 51  0
         }
 52  
     };
 53  7125
     protected final AttributeValueOperation ATTRIBUTE_VALUE_OPERATION = new AttributeValueOperation() {
 54  
         public TTLCachedObject<Object> getAttributeValue(String name) throws FileSystemException {
 55  
             try {
 56  2
                 return getAttributeMapper().getAttribute(name);
 57  0
             } catch (Exception e) {
 58  0
                 throw VFSUtil.makeFileSystemException(e);
 59  
             }
 60  
         }
 61  
     };
 62  
     protected final FileAttributeMap attributeMap;
 63  
     protected AttributeMapper attributeMapper;
 64  
 
 65  
     public FlatFileObject(FileName name, FlatFileSystem ffs, FileObject realFile) throws FileSystemException {
 66  7125
         super(name, ffs, realFile);
 67  7125
         this.ffs = ffs;
 68  7125
         attributeMap = new FileAttributeMap(this, resolver);
 69  7125
     }
 70  
 
 71  
     protected AttributeMapper getAttributeMapper() throws FileSystemException {
 72  41730
         if (attributeMapper != null) return attributeMapper;
 73  1595
         attributeMapper = ffs.getAttributeHandler().getAttributeMapper(name);
 74  1595
         return attributeMapper;
 75  
     }
 76  
 
 77  
     protected String[] doListChildren() throws FileSystemException {
 78  2576
         FileObject[] realChildren = getRealFile().getChildren();
 79  2576
         ArrayList<String> names = new ArrayList<String>(realChildren.length);
 80  43275
         for (FileObject child: realChildren) {
 81  40699
             String baseName = child.getName().getBaseName();
 82  40699
             names.add(baseName);
 83  
         }
 84  2576
         getFileSystem().excludeNames(names);
 85  2576
         return names.toArray(new String[names.size()]);
 86  
     }
 87  
 
 88  
     // Use .meta/deleted, but delete all children if current is deleted
 89  
     protected void doDelete() throws FileSystemException {
 90  6
         getAttributeMapper().deleteAttributes();
 91  6
         super.doDelete();
 92  6
     }
 93  
 
 94  
     protected void doCreateFolder() throws FileSystemException {
 95  93
         getRealFile().createFolder();
 96  93
     }
 97  
 
 98  
     protected void doCreateFile() throws FileSystemException {
 99  5
         getRealFile().createFile();
 100  5
     }
 101  
 
 102  
     protected void doCopyMetaData(FileObject src) throws FileSystemException {
 103  2
         FileContent content = src.getContent();
 104  8
         for (String name: content.getAttributeNames()) {
 105  6
             doSetAttribute(name, content.getAttribute(name));
 106  
         }
 107  2
         super.doCopyMetaData(src);
 108  2
     }
 109  
 
 110  
     protected Object doGetAttribute(String name) throws FileSystemException {
 111  
         try {
 112  13145
             TTLCachedObject<Object> value = getAttributeMapper().getAttribute(name);
 113  13145
             return value == null ? null : value.getObject();
 114  0
         } catch (Exception e) {
 115  0
             throw VFSUtil.makeFileSystemException(e);
 116  
         }
 117  
     }
 118  
 
 119  
     protected Map<String, Object> doGetAttributes() throws FileSystemException {
 120  32
         return attributeMap;
 121  
     }
 122  
 
 123  
     protected String[] doGetAttributeNames() throws FileSystemException {
 124  10
         return getAttributeMapper().getAttributeNames();
 125  
     }
 126  
 
 127  
     protected boolean doAttributeExists(String name) throws FileSystemException {
 128  21635
         return getAttributeMapper().attributeExists(name);
 129  
     }
 130  
 
 131  
     protected void doSetAttribute(String name, Object value) throws FileSystemException {
 132  38
         getAttributeMapper().setAttribute(name, value);
 133  38
     }
 134  
 
 135  
     protected void doRemoveAttribute(String name) throws FileSystemException {
 136  6
         getAttributeMapper().removeAttribute(name);
 137  6
     }
 138  
 
 139  
     private FileContentInfo doGetDefaultContentInfo() throws Exception {
 140  951
         return super.doGetContentInfo();
 141  
     }
 142  
 
 143  
     private String getContentAttribute(String name) {
 144  
         try {
 145  979
             return (String) doGetAttribute(name);
 146  0
         } catch (FileSystemException e) {
 147  0
             return null;
 148  
         }
 149  
     }
 150  
 
 151  
     public void close() throws FileSystemException {
 152  328
         getAttributeMapper().refresh();
 153  328
         super.close();
 154  328
     }
 155  
 
 156  
     public void refresh() throws FileSystemException {
 157  6440
         getAttributeMapper().refresh();
 158  6440
         super.refresh();
 159  6440
     }
 160  
 
 161  
     protected FileContentInfo doGetContentInfo() throws FileSystemException {
 162  976
         return new FileContentInfo() {
 163  
             public String getContentType() {
 164  977
                 String contentType = getContentAttribute("content-type");
 165  977
                 if (contentType != null) return contentType;
 166  
                 try {
 167  950
                     return doGetDefaultContentInfo().getContentType();
 168  0
                 } catch (Exception e) {
 169  0
                     return null;
 170  
                 }
 171  
             }
 172  
 
 173  
             public String getContentEncoding() {
 174  2
                 String contentEncoding = getContentAttribute("content-encoding");
 175  2
                 if (contentEncoding != null) return contentEncoding;
 176  
                 try {
 177  1
                     return doGetDefaultContentInfo().getContentEncoding();
 178  0
                 } catch (Exception e) {
 179  0
                     return null;
 180  
                 }
 181  
             }
 182  
         };
 183  
     }
 184  
 }
 185