1 22 package javax.activation; 23 24 import java.awt.datatransfer.DataFlavor ; 26 import java.awt.datatransfer.UnsupportedFlavorException ; 27 import java.io.OutputStream ; 28 import java.io.IOException ; 29 30 35 public class ObjectDataContentHandler 36 implements DataContentHandler 37 { 38 39 43 46 private DataFlavor [] transferFlavors; 47 48 51 private Object obj; 52 53 56 private String mimeType; 57 58 61 private DataContentHandler handler; 62 63 64 68 74 public ObjectDataContentHandler(DataContentHandler handler, 75 Object object, String mimetype) 76 { 77 this.handler = handler; 78 obj = object; 79 mimeType = mimetype; 80 } 81 82 83 87 91 public DataContentHandler getDCH() 92 { 93 return handler; 94 } 95 96 100 public DataFlavor [] getTransferDataFlavors() 101 { 102 if (transferFlavors==null) 103 { 104 if (handler!=null) 105 transferFlavors = handler.getTransferDataFlavors(); 106 else 107 { 108 transferFlavors = new DataFlavor [1]; 109 Class t = obj.getClass(); 110 transferFlavors[0] = new ActivationDataFlavor (t, mimeType, mimeType); 111 } 112 } 113 return transferFlavors; 114 } 115 116 122 public Object getTransferData(DataFlavor flavor, DataSource source) 123 throws UnsupportedFlavorException , IOException 124 { 125 if (handler!=null) 126 return handler.getTransferData(flavor, source); 127 getTransferDataFlavors(); 128 if (flavor.equals(transferFlavors[0])) 129 return obj; 130 else 131 throw new UnsupportedFlavorException (flavor); 132 } 133 134 139 public Object getContent(DataSource source) 140 { 141 return obj; 142 } 143 144 151 public void writeTo(Object object, String mimeType, OutputStream stream) 152 throws IOException 153 { 154 if (handler!=null) 155 handler.writeTo(object, mimeType, stream); 156 else 157 throw new UnsupportedDataTypeException ("No handler for MIME content type: "+mimeType); 158 } 159 160 } 161 | Popular Tags |