KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > page > manage > BaseMimeTypeMap


1 /*--
2
3  Copyright (C) 2001-2002 Anthony Eden.
4  All rights reserved.
5
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact me@anthonyeden.com.
21
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Anthony Eden (me@anthonyeden.com).
25
26  In addition, I request (but do not require) that you include in the
27  end-user documentation provided with the redistribution and/or in the
28  software itself an acknowledgement equivalent to the following:
29      "This product includes software developed by
30       Anthony Eden (http://www.anthonyeden.com/)."
31
32  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
36  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  POSSIBILITY OF SUCH DAMAGE.
43
44  For more information on JPublish, please see <http://www.jpublish.org/>.
45
46  */

47 package com.openedit.page.manage;
48
49 import java.io.File JavaDoc;
50 import java.io.FileInputStream JavaDoc;
51 import java.io.InputStream JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.Map JavaDoc;
54 import java.util.Properties JavaDoc;
55 import java.util.StringTokenizer JavaDoc;
56
57 import com.openedit.OpenEditRuntimeException;
58 import com.openedit.util.FileUtils;
59
60
61 /**
62  * Data structure representing a MIME type map.
63  *
64  * @author Anthony Eden
65  * @author Matthew Avery, mavery@einnovation.com
66  */

67 public class BaseMimeTypeMap implements MimeTypeMap
68 {
69     protected String JavaDoc fieldDefaultMimeType;
70     protected Map JavaDoc fieldInternalMap;
71     protected File JavaDoc fieldRootDirectory;
72     
73     public BaseMimeTypeMap()
74     {
75         fieldDefaultMimeType = "application/octet-stream";
76     }
77
78     /**
79      * @see com.openedit.page.manage.MimeTypeMap#setDefaultMimeType(java.lang.String)
80      */

81     public void setDefaultMimeType(String JavaDoc inMimeType)
82     {
83         fieldDefaultMimeType = inMimeType;
84     }
85
86     /**
87      * @see com.openedit.page.manage.MimeTypeMap#getDefaultMimeType()
88      */

89     public String JavaDoc getDefaultMimeType()
90     {
91         return fieldDefaultMimeType;
92     }
93
94     /**
95      * @see com.openedit.page.manage.MimeTypeMap#getMimeType(java.lang.String)
96      */

97     public String JavaDoc getMimeType(String JavaDoc inExtension)
98     {
99         if ( inExtension == null )
100         {
101             return getDefaultMimeType();
102         }
103         String JavaDoc mimeType = (String JavaDoc) get(inExtension.toLowerCase());
104
105         if (mimeType == null)
106         {
107             mimeType = getDefaultMimeType();
108         }
109
110         return mimeType;
111     }
112
113     /**
114      * @see com.openedit.page.manage.MimeTypeMap#getPathMimeType(java.lang.String)
115      */

116     public String JavaDoc getPathMimeType(String JavaDoc path)
117     {
118         return getMimeType(getExtension(path));
119     }
120
121     protected String JavaDoc getExtension(String JavaDoc inPath)
122     {
123         String JavaDoc result = null;
124
125         if (inPath != null)
126         {
127             int index = inPath.lastIndexOf('.');
128
129             if (index > -1)
130             {
131                 result = inPath.substring(index + 1);
132             }
133         }
134
135         return result;
136     }
137
138     public void addMappings(String JavaDoc mimeType, String JavaDoc exts)
139     {
140         String JavaDoc ext;
141
142         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(exts, ","); st.hasMoreTokens();)
143         {
144             ext = st.nextToken();
145             put(ext, mimeType);
146         }
147     }
148     
149     public Map JavaDoc getInternalMap()
150     {
151         if ( fieldInternalMap == null )
152         {
153             fieldInternalMap = new HashMap JavaDoc();
154             loadMimeTypes(fieldInternalMap);
155         }
156         return fieldInternalMap;
157     }
158     
159     protected void loadMimeTypes(Map JavaDoc inInternalMap)
160     {
161         //first load the internal mimetypes.properties
162
InputStream JavaDoc in = getClass().getClassLoader().getResourceAsStream("mimetypes.properties");
163         if ( in == null)
164         {
165             throw new OpenEditRuntimeException("Could not load mimetypes");
166         }
167             Properties JavaDoc values = new Properties JavaDoc();
168             try
169             {
170                 values.load(in);
171                 inInternalMap.putAll(values);
172             }
173             catch ( Exception JavaDoc ex)
174             {
175                 throw new OpenEditRuntimeException(ex);
176             }
177             finally
178             {
179                 FileUtils.safeClose(in);
180             }
181         //check load up any additional ones from the WEB-INF/mimytypes.properties location
182
File JavaDoc props2 = new File JavaDoc( getRootDirectory(), "/WEB-INF/mimetypes.properties");
183         if ( props2.exists())
184         {
185             values = new Properties JavaDoc();
186             try
187             {
188                 in = new FileInputStream JavaDoc(props2);
189                 values.load(in);
190                 inInternalMap.putAll(values);
191             }
192             catch ( Exception JavaDoc ex)
193             {
194                 throw new OpenEditRuntimeException(ex);
195             }
196             finally
197             {
198                 FileUtils.safeClose(in);
199             }
200         }
201     }
202
203     public void setInternalMap( Map JavaDoc internalMap )
204     {
205         fieldInternalMap = internalMap;
206     }
207     /**
208      * @see com.openedit.page.manage.MimeTypeMap#get(java.lang.Object)
209      */

210     public Object JavaDoc get( Object JavaDoc key )
211     {
212         return getInternalMap().get( key );
213     }
214     /**
215      * @see com.openedit.page.manage.MimeTypeMap#put(java.lang.Object, java.lang.Object)
216      */

217     public Object JavaDoc put( Object JavaDoc arg0, Object JavaDoc arg1 )
218     {
219         return getInternalMap().put( arg0, arg1 );
220     }
221
222     public File JavaDoc getRootDirectory()
223     {
224         return fieldRootDirectory;
225     }
226
227     public void setRootDirectory(File JavaDoc inRootDirectory)
228     {
229         fieldRootDirectory = inRootDirectory;
230     }
231 }
232
Popular Tags