KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > transcoder > ExtendableTranscoderFactory


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.components.transcoder;
17
18 import org.apache.batik.transcoder.Transcoder;
19 import org.apache.batik.transcoder.image.JPEGTranscoder;
20 import org.apache.batik.transcoder.image.PNGTranscoder;
21 import org.apache.batik.transcoder.image.TIFFTranscoder;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * An extendable Batik Transcoder factory.
28  * When given a MIME type, find a Transcoder which supports that MIME
29  * type. This factory is extendable as new <code>Transcoder</code>s can
30  * be added at runtime.
31  *
32  * @author <a HREF="mailto:rossb@apache.org">Ross Burton</a>
33  * @version CVS $Id: ExtendableTranscoderFactory.java 123903 2005-01-02 21:26:59Z antonio $
34  */

35 public class ExtendableTranscoderFactory implements TranscoderFactory {
36
37     protected final static Map JavaDoc transcoders = new HashMap JavaDoc();
38
39     protected final static TranscoderFactory singleton = new ExtendableTranscoderFactory();
40
41     private ExtendableTranscoderFactory() {
42         // Add the default transcoders which come with Batik
43
addTranscoder("image/jpeg", JPEGTranscoder.class);
44         addTranscoder("image/jpg", JPEGTranscoder.class);
45         addTranscoder("image/png", PNGTranscoder.class);
46         addTranscoder("image/tiff", TIFFTranscoder.class);
47     }
48
49     /**
50      * Get a reference to this Transcoder Factory.
51      */

52     public final static TranscoderFactory getTranscoderFactoryImplementation() {
53         return singleton;
54     }
55
56     /**
57      * Create a transcoder for a specified MIME type.
58      * @param mimeType The MIME type of the destination format
59      * @return A suitable transcoder, or <code>null</code> if one cannot be found
60      */

61     public Transcoder createTranscoder(String JavaDoc mimeType) {
62         Class JavaDoc transcoderClass = (Class JavaDoc) transcoders.get(mimeType);
63         if (transcoderClass == null) {
64             return null;
65         } else {
66             try {
67                 return (Transcoder) transcoderClass.newInstance();
68             } catch (Exception JavaDoc ex) {
69                 return null;
70             }
71         }
72     }
73
74     /**
75      * Add a mapping from the specified MIME type to a transcoder.
76      * Note: The transcoder must have a no-argument constructor.
77      * @param mimeType The MIME type of the Transcoder
78      * @param transcoderClass The <code>Class</code> object for the Transcoder.
79      */

80     public void addTranscoder(String JavaDoc mimeType, Class JavaDoc transcoderClass) {
81         transcoders.put(mimeType, transcoderClass);
82     }
83
84     /**
85      * Remove the mapping from a specified MIME type.
86      * @param mimeType The MIME type to remove from the mapping.
87      */

88     public void removeTranscoder(String JavaDoc mimeType) {
89         transcoders.remove(mimeType);
90     }
91 }
92
Popular Tags