01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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 implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.transcoder;
18:
19: import org.apache.batik.transcoder.Transcoder;
20: import org.apache.batik.transcoder.image.JPEGTranscoder;
21: import org.apache.batik.transcoder.image.PNGTranscoder;
22: import org.apache.batik.transcoder.image.TIFFTranscoder;
23:
24: import java.util.HashMap;
25: import java.util.Map;
26:
27: /**
28: * An extendable Batik Transcoder factory.
29: * When given a MIME type, find a Transcoder which supports that MIME
30: * type. This factory is extendable as new <code>Transcoder</code>s can
31: * be added at runtime.
32: *
33: * @author <a href="mailto:rossb@apache.org">Ross Burton</a>
34: * @version CVS $Id: ExtendableTranscoderFactory.java 433543 2006-08-22 06:22:54Z crossley $
35: */
36: public class ExtendableTranscoderFactory implements TranscoderFactory {
37:
38: protected final static Map transcoders = new HashMap();
39:
40: protected final static TranscoderFactory singleton = new ExtendableTranscoderFactory();
41:
42: private ExtendableTranscoderFactory() {
43: // Add the default transcoders which come with Batik
44: addTranscoder("image/jpeg", JPEGTranscoder.class);
45: addTranscoder("image/jpg", JPEGTranscoder.class);
46: addTranscoder("image/png", PNGTranscoder.class);
47: addTranscoder("image/tiff", TIFFTranscoder.class);
48: }
49:
50: /**
51: * Get a reference to this Transcoder Factory.
52: */
53: public final static TranscoderFactory getTranscoderFactoryImplementation() {
54: return singleton;
55: }
56:
57: /**
58: * Create a transcoder for a specified MIME type.
59: * @param mimeType The MIME type of the destination format
60: * @return A suitable transcoder, or <code>null</code> if one cannot be found
61: */
62: public Transcoder createTranscoder(String mimeType) {
63: Class transcoderClass = (Class) transcoders.get(mimeType);
64: if (transcoderClass == null) {
65: return null;
66: } else {
67: try {
68: return (Transcoder) transcoderClass.newInstance();
69: } catch (Exception ex) {
70: return null;
71: }
72: }
73: }
74:
75: /**
76: * Add a mapping from the specified MIME type to a transcoder.
77: * Note: The transcoder must have a no-argument constructor.
78: * @param mimeType The MIME type of the Transcoder
79: * @param transcoderClass The <code>Class</code> object for the Transcoder.
80: */
81: public void addTranscoder(String mimeType, Class transcoderClass) {
82: transcoders.put(mimeType, transcoderClass);
83: }
84:
85: /**
86: * Remove the mapping from a specified MIME type.
87: * @param mimeType The MIME type to remove from the mapping.
88: */
89: public void removeTranscoder(String mimeType) {
90: transcoders.remove(mimeType);
91: }
92: }
|