KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > activation > DataSourceDataContentHandler


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  * DataSourceDataContentHandler
32  * @author Andrew Selkirk
33  * @version $Revision: 1.4 $
34  */

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

43   /**
44    * Data source.
45    */

46   private DataSource JavaDoc source;
47
48   /**
49    * List of transfer flavors.
50    */

51   private DataFlavor JavaDoc[] transferFlavors;
52
53   /**
54    * Data content handler.
55    */

56   private DataContentHandler JavaDoc handler;
57
58
59   //-------------------------------------------------------------
60
// Initialization ---------------------------------------------
61
//-------------------------------------------------------------
62

63   /**
64    * Create data source data content handler.
65    * @param handler Data content handler
66    * @param source Data source
67    */

68   public DataSourceDataContentHandler(DataContentHandler JavaDoc handler,
69       DataSource JavaDoc source)
70   {
71     this.handler = handler;
72     this.source = source;
73   }
74
75
76   //-------------------------------------------------------------
77
// Public Accessor Methods ------------------------------------
78
//-------------------------------------------------------------
79

80   /**
81    * Get transfer data flavors.
82    * @return List of data flavors
83    */

84   public DataFlavor JavaDoc[] getTransferDataFlavors()
85   {
86     if (transferFlavors==null)
87     {
88       if (handler!=null)
89         transferFlavors = handler.getTransferDataFlavors();
90       else
91       {
92         transferFlavors = new DataFlavor JavaDoc[1];
93         String JavaDoc mimeType = source.getContentType();
94         transferFlavors[0] = new ActivationDataFlavor JavaDoc(mimeType, mimeType);
95       }
96     }
97     return transferFlavors;
98   }
99
100   /**
101    * Get transfer data based on data flavor and data source
102    * @param flavor Data flavor
103    * @param source Data source
104    * @return Transfer data
105    * @throws IOException IO exception occurred
106    */

107   public Object JavaDoc getTransferData(DataFlavor JavaDoc flavor, DataSource JavaDoc source)
108     throws UnsupportedFlavorException JavaDoc, IOException JavaDoc
109   {
110     if (handler!=null)
111       return handler.getTransferData(flavor, source);
112     getTransferDataFlavors();
113     if (flavor.equals(transferFlavors[0]))
114       return source.getInputStream();
115     else
116       throw new UnsupportedFlavorException JavaDoc(flavor);
117   }
118
119   /**
120    * Get content.
121    * @param source Data source
122    * @return Content object
123    * @throws IOException IO exception occurred
124    */

125   public Object JavaDoc getContent(DataSource JavaDoc source)
126     throws IOException JavaDoc
127   {
128     if (handler!=null)
129       return handler.getContent(source);
130     else
131       return source.getInputStream();
132   }
133
134   /**
135    * Write to.
136    * @param object Object to write
137    * @param mimeType MIME type
138    * @param stream Output stream
139    * @throws IOException IO exception occurred
140    */

141   public void writeTo(Object JavaDoc object, String JavaDoc mimeType, OutputStream JavaDoc stream)
142     throws IOException JavaDoc
143   {
144     if (handler!=null)
145       handler.writeTo(object, mimeType, stream);
146     else
147     {
148       String JavaDoc sourceMimeType = source.getContentType();
149       throw new UnsupportedDataTypeException JavaDoc("No handler for MIME content type: "+sourceMimeType);
150     }
151   }
152
153 }
154
Popular Tags