KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > mail > datasource > SourceDataSource


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

16 package org.apache.cocoon.mail.datasource;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 import javax.activation.DataSource JavaDoc;
23
24 import org.apache.excalibur.source.Source;
25
26 /**
27  * The SourceDataSource class provides an object, that wraps a
28  * Cocoon <code>org.apache.excalibur.source.Source</code> object
29  * in a DataSource interface.
30  *
31  * @see org.apache.excalibur.source.Source
32  * @see javax.activation.DataSource
33  * @author <a HREF="mailto:frank.ridderbusch@gmx.de">Frank Ridderbusch</a>
34  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
35  * @version CVS $Id: SourceDataSource.java 232592 2005-08-14 08:28:57Z antonio $
36  */

37 public class SourceDataSource implements DataSource JavaDoc {
38     private Source JavaDoc src;
39     private String JavaDoc contentType;
40     private String JavaDoc name;
41
42     /**
43      * Creates a new instance of SourceDataSource
44      * @param src A <code>org.apache.excalibur.source.Source</code> Object.
45      */

46     public SourceDataSource(Source JavaDoc src) {
47         this(src, null, null);
48     }
49
50     /**
51      * Creates a new instance of SourceDataSource
52      * @param src A <code>org.apache.excalibur.source.Source</code> Object.
53      */

54     public SourceDataSource(Source JavaDoc src, String JavaDoc type, String JavaDoc name) {
55         this.src = src;
56         this.contentType = type;
57         this.name = name;
58         if (isNullOrEmpty(this.name)) this.name = null;
59         if (isNullOrEmpty(this.contentType)) this.contentType = null;
60     }
61
62     /**
63      * Check String for null or empty.
64      * @param str
65      * @return true if str is null, empty string, or equals "null"
66      */

67      private boolean isNullOrEmpty(String JavaDoc str) {
68          return (str == null || "".equals(str) || "null".equals(str));
69      }
70
71     /**
72      * Returns the result of a call to the <code>Source</code>
73      * objects <code>getMimeType()</code> method. Returns
74      * "application/octet-stream", if <code>getMimeType()</code>
75      * returns <code>null</code>.
76      * @return The content type (mime type) of this <code>DataSource</code> object.
77      * @see org.apache.excalibur.source.Source#getMimeType()
78      */

79     public String JavaDoc getContentType() {
80         if (this.contentType != null) {
81             return this.contentType;
82         }
83         String JavaDoc mimeType = src.getMimeType();
84         if (isNullOrEmpty(mimeType)) {
85             mimeType = "application/octet-stream";
86         }
87         return mimeType;
88     }
89
90     /**
91      * Get the <code>InputStream</code> object from the
92      * <code>Source</code> object.
93      * @throws java.io.IOException if an I/O error occurs.
94      * @return The InputStream object from the <code>Source</code> object.
95      * @see org.apache.excalibur.source.Source#getInputStream()
96      */

97     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
98         return src.getInputStream();
99     }
100
101     /**
102      * Returns the name for this <code>DataSource</code> object. This is
103      * actually the last path component (after the last '/') from the value
104      * returned by the <code>getURI()</code> method of the <code>Source</code>
105      * object.
106      * @return the name for this <code>DataSource</code>
107      * @see org.apache.excalibur.source.Source#getURI()
108      */

109     public String JavaDoc getName() {
110         if (this.name != null){
111             return this.name;
112         }
113         String JavaDoc name = src.getURI();
114         name = name.substring(name.lastIndexOf('/') + 1);
115         return ("".equals(name)? "attachment" : name);
116     }
117
118     /**
119      * Unimplemented. Directly throws <code>IOException</code>.
120      * @throws java.io.IOException since unimplemented
121      * @return nothing
122      */

123     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
124         throw new IOException JavaDoc("no data sink available");
125     }
126 }
127
Popular Tags