1 4 package org.openedit.repository.filesystem; 5 6 import java.io.ByteArrayInputStream ; 7 import java.io.File ; 8 import java.io.FileInputStream ; 9 import java.io.FileNotFoundException ; 10 import java.io.InputStream ; 11 import java.util.Date ; 12 13 import org.openedit.repository.ContentItem; 14 import org.openedit.repository.RepositoryException; 15 16 import com.openedit.util.PathUtilities; 17 18 21 public class FileItem extends ContentItem 22 { 23 protected File fieldFile; 24 27 public FileItem() 28 { 29 } 30 protected String getParentPath() 31 { 32 return PathUtilities.extractDirectoryPath( getPath() ); 33 } 34 35 public Date lastModified() 36 { 37 return new Date ( getFile().lastModified() ); 38 } 39 40 public InputStream getInputStream() throws RepositoryException 41 { 42 if ( isFolder() ) 43 { 44 return createFileListingStream(); 45 } 46 try 47 { 48 if (getFile().exists() ) 49 { 50 return new FileInputStream ( getFile() ); 51 } 52 } 53 catch( FileNotFoundException e ) 54 { 55 throw new RepositoryException( e ); 56 } 57 return null; 58 } 59 60 protected InputStream createFileListingStream() 61 { 62 File [] files = getFile().listFiles(); 63 StringBuffer sb = new StringBuffer (); 64 for ( int i = 0; i < files.length; i++ ) 65 { 66 if ( !files[i].getName().equals(".versions") ) 67 { 68 sb.append( files[i].getName() + "\n" ); 69 } 70 } 71 return new ByteArrayInputStream ( sb.toString().getBytes() ); 72 } 73 public boolean exists() 74 { 75 return getFile().exists(); 76 } 77 78 public boolean isFolder() 79 { 80 return getFile().isDirectory(); 81 } 82 83 public boolean isWritable() 84 { 85 return true; 86 } 87 88 public File getFile() 89 { 90 return fieldFile; 91 } 92 public void setFile( File file ) 93 { 94 fieldFile = file; 95 } 96 99 public long getLength() 100 { 101 return getFile().length(); 102 } 103 } 104 | Popular Tags |