KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > mail > handlers > AbstractDataContentHandler


1 /***********************************************************************
2  * Copyright (c) 2003-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17 package org.apache.james.util.mail.handlers;
18
19 import java.awt.datatransfer.DataFlavor JavaDoc;
20 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import javax.activation.ActivationDataFlavor JavaDoc;
24 import javax.activation.DataContentHandler JavaDoc;
25 import javax.activation.DataSource JavaDoc;
26 import javax.mail.MessagingException JavaDoc;
27
28
29 /**
30  * Abstract class providing common Data Handler behavior.
31  */

32 public abstract class AbstractDataContentHandler implements DataContentHandler JavaDoc
33 {
34
35     private ActivationDataFlavor JavaDoc fieldDataFlavor;
36
37     /**
38      * Default Constructor
39      */

40     public AbstractDataContentHandler()
41     {
42         super();
43     }
44
45     /**
46      * Update the current DataFlavor.
47      *
48      */

49     protected void updateDataFlavor()
50     {
51         setDataFlavor(computeDataFlavor());
52     }
53
54     /**
55      * Compute an ActivationDataFlavor.
56      *
57      * @return A new ActivationDataFlavor
58      */

59     abstract protected ActivationDataFlavor JavaDoc computeDataFlavor();
60
61     protected void setDataFlavor(ActivationDataFlavor JavaDoc aDataFlavor)
62     {
63         fieldDataFlavor = aDataFlavor;
64     }
65
66     /**
67      * @see javax.activation.DataContentHandler#getContent(javax.activation.DataSource)
68      */

69     public Object JavaDoc getContent(DataSource JavaDoc aDataSource) throws IOException JavaDoc
70     {
71         Object JavaDoc content = null;
72         try
73         {
74             content = computeContent(aDataSource);
75         }
76         catch (MessagingException JavaDoc e)
77         {
78             // No-op
79
}
80         return content;
81     }
82
83     /**
84      * Compute the content from aDataSource.
85      *
86      * @param aDataSource
87      * @return new Content built from the DataSource
88      * @throws MessagingException
89      */

90     abstract protected Object JavaDoc computeContent(DataSource JavaDoc aDataSource)
91             throws MessagingException JavaDoc;
92
93     /**
94      * @see javax.activation.DataContentHandler#getTransferData(java.awt.datatransfer.DataFlavor,
95      * javax.activation.DataSource)
96      */

97     public Object JavaDoc getTransferData(DataFlavor JavaDoc aDataFlavor, DataSource JavaDoc aDataSource)
98             throws UnsupportedFlavorException JavaDoc, IOException JavaDoc
99     {
100         Object JavaDoc content = null;
101         if (getDataFlavor().equals(aDataFlavor))
102             content = getContent(aDataSource);
103         return content;
104     }
105
106     /**
107      * @see javax.activation.DataContentHandler#getTransferDataFlavors()
108      */

109     public DataFlavor JavaDoc[] getTransferDataFlavors()
110     {
111         return new DataFlavor JavaDoc[]{getDataFlavor()};
112     }
113
114     /**
115      * Get the DataFlavor, lazily initialised if required.
116      *
117      * @return Returns the dataFlavor, lazily initialised.
118      */

119     protected ActivationDataFlavor JavaDoc getDataFlavor()
120     {
121         ActivationDataFlavor JavaDoc dataFlavor = null;
122         if (null == (dataFlavor = getDataFlavorBasic()))
123         {
124             updateDataFlavor();
125             return getDataFlavor();
126         }
127         return dataFlavor;
128     }
129
130     /**
131      * Get the DataFlavor.
132      *
133      * @return Returns the dataFlavor.
134      */

135     private ActivationDataFlavor JavaDoc getDataFlavorBasic()
136     {
137         return fieldDataFlavor;
138     }
139
140 }
141
Popular Tags