1 4 package org.oddjob.io; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileNotFoundException ; 9 import java.io.InputStream ; 10 11 import org.oddjob.OddjobException; 12 13 22 public class FileType { 23 24 29 private File file; 30 31 36 public void setFile(File file) { 37 this.file = file; 38 } 39 40 45 public Object valueFor(Class required) 46 throws ClassCastException { 47 if (required == null) { 48 required = File .class; 49 } 50 if (required.isAssignableFrom(File .class)) { 51 return file; 52 } 53 if (required.isAssignableFrom(InputStream .class)) { 54 try { 55 return new FileInputStream (file); 56 } catch (FileNotFoundException e) { 57 throw new OddjobException("Failed to open file for input."); 58 } 59 } 60 if (required.isAssignableFrom(InputStream .class)) { 61 try { 62 return new FileInputStream (file); 63 } catch (FileNotFoundException e) { 64 throw new OddjobException("Failed to open file for output."); 65 } 66 } 67 if (required.isAssignableFrom(String .class)) { 68 return file.toString(); 69 } 70 71 throw new ClassCastException ("Can not convert a File to " + required); 72 } 73 74 public String toString() { 75 return (String ) valueFor(String .class); 76 } 77 } 78 | Popular Tags |