1 /*
2 * ObjectDataContentHandler.java
3 * Copyright (C) 2004 The Free Software Foundation
4 *
5 * This file is part of GNU Java Activation Framework (JAF), a library.
6 *
7 * GNU JAF is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * GNU JAF is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * As a special exception, if you link this library with other files to
22 * produce an executable, this library does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * This exception does not however invalidate any other reasons why the
25 * executable file might be covered by the GNU General Public License.
26 */
27 package javax.activation;
28
29 import java.awt.datatransfer.DataFlavor;
30 import java.awt.datatransfer.UnsupportedFlavorException;
31 import java.io.IOException;
32 import java.io.OutputStream;
33
34 /**
35 * Data content handler that uses an existing DCH and reified object.
36 *
37 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
38 * @version 1.0.2
39 */
40 class ObjectDataContentHandler
41 implements DataContentHandler
42 {
43
44 private DataContentHandler dch;
45 private Object object;
46 private String mimeType;
47 private DataFlavor[] flavors;
48
49 public ObjectDataContentHandler(DataContentHandler dch, Object object,
50 String mimeType)
51 {
52 this.dch = dch;
53 this.object = object;
54 this.mimeType = mimeType;
55 }
56
57 public Object getContent(DataSource ds)
58 {
59 return object;
60 }
61
62 public DataContentHandler getDCH()
63 {
64 return dch;
65 }
66
67 public Object getTransferData(DataFlavor flavor, DataSource ds)
68 throws UnsupportedFlavorException, IOException
69 {
70 if (dch != null)
71 return dch.getTransferData(flavor, ds);
72 if (flavors == null)
73 getTransferDataFlavors();
74 if (flavor.equals(flavors[0]))
75 return object;
76 throw new UnsupportedFlavorException(flavor);
77 }
78
79 public DataFlavor[] getTransferDataFlavors()
80 {
81 if (flavors == null)
82 {
83 if (dch != null)
84 flavors = dch.getTransferDataFlavors();
85 else
86 {
87 flavors = new DataFlavor[1];
88 flavors[0] = new ActivationDataFlavor(object.getClass(),
89 mimeType, mimeType);
90 }
91 }
92 return flavors;
93 }
94
95 public void writeTo(Object object, String mimeType, OutputStream out)
96 throws IOException
97 {
98 if (dch != null)
99 dch.writeTo(object, mimeType, out);
100 else
101 throw new UnsupportedDataTypeException("no object DCH for MIME type " + mimeType);
102 }
103
104 }