KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > activation > ObjectDataContentHandler


1 /*
2   GNU-Classpath Extensions: java bean activation framework
3   Copyright (C) 2000 2001 Andrew Selkirk
4
5   For more information on the classpathx please mail:
6   nferrier@tapsellferrier.co.uk
7
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Lesser General Public License
10   as published by the Free Software Foundation; either version 2
11   of the License, or (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */

22 package javax.activation;
23
24 // Imports
25
import java.awt.datatransfer.DataFlavor JavaDoc;
26 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * Object Data Content Handler.
32  * @author Andrew Selkirk
33  * @version $Revision: 1.3 $
34  */

35 public class ObjectDataContentHandler
36   implements DataContentHandler JavaDoc
37 {
38
39   //-------------------------------------------------------------
40
// Variables --------------------------------------------------
41
//-------------------------------------------------------------
42

43   /**
44    * Transfer data flavors list.
45    */

46   private DataFlavor JavaDoc[] transferFlavors;
47
48   /**
49    * Object.
50    */

51   private Object JavaDoc obj;
52
53   /**
54    * MIME type of object.
55    */

56   private String JavaDoc mimeType;
57
58   /**
59    * Data content handler.
60    */

61   private DataContentHandler JavaDoc handler;
62
63
64   //-------------------------------------------------------------
65
// Initialization ---------------------------------------------
66
//-------------------------------------------------------------
67

68   /**
69    * Create new object data content handler.
70    * @param handler Data content handler
71    * @param object Object
72    * @param mimetype MIME Type
73    */

74   public ObjectDataContentHandler(DataContentHandler JavaDoc handler,
75     Object JavaDoc object, String JavaDoc mimetype)
76   {
77     this.handler = handler;
78     obj = object;
79     mimeType = mimetype;
80   }
81
82
83   //-------------------------------------------------------------
84
// Public Accessor Methods ------------------------------------
85
//-------------------------------------------------------------
86

87   /**
88    * Get data content handler.
89    * @return Data content handler
90    */

91   public DataContentHandler JavaDoc getDCH()
92   {
93     return handler;
94   }
95
96   /**
97    * Get transfer data flavors.
98    * @return List of transfer data flavors
99    */

100   public DataFlavor JavaDoc[] getTransferDataFlavors()
101   {
102     if (transferFlavors==null)
103     {
104       if (handler!=null)
105         transferFlavors = handler.getTransferDataFlavors();
106       else
107       {
108         transferFlavors = new DataFlavor JavaDoc[1];
109         Class JavaDoc t = obj.getClass();
110         transferFlavors[0] = new ActivationDataFlavor JavaDoc(t, mimeType, mimeType);
111       }
112     }
113     return transferFlavors;
114   }
115
116   /**
117    * Get transfer data flavor.
118    * @param flavor Data flavor
119    * @param source Data source
120    * @throws IOException IO exception occurred
121    */

122   public Object JavaDoc getTransferData(DataFlavor JavaDoc flavor, DataSource JavaDoc source)
123     throws UnsupportedFlavorException JavaDoc, IOException JavaDoc
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 JavaDoc(flavor);
132   }
133
134   /**
135    * Get content.
136    * @param source Data source
137    * @return Object content
138    */

139   public Object JavaDoc getContent(DataSource JavaDoc source)
140   {
141     return obj;
142   }
143
144   /**
145    * Write to.
146    * @param object Object to write
147    * @param mimeType MIME type of object
148    * @param stream Output stream to write to
149    * @throws IOException IO exception occurred
150    */

151   public void writeTo(Object JavaDoc object, String JavaDoc mimeType, OutputStream JavaDoc stream)
152     throws IOException JavaDoc
153   {
154     if (handler!=null)
155       handler.writeTo(object, mimeType, stream);
156     else
157       throw new UnsupportedDataTypeException JavaDoc("No handler for MIME content type: "+mimeType);
158   }
159
160 }
161
Popular Tags