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.renderer;
18:
19: import org.apache.fop.render.Renderer;
20: import org.apache.fop.render.pcl.PCLRenderer;
21: import org.apache.fop.render.pdf.PDFRenderer;
22: import org.apache.fop.render.ps.PSRenderer;
23:
24: import java.util.HashMap;
25: import java.util.Map;
26:
27: /**
28: * An extendable FOP Renderer factory.
29: * When given a MIME type, find a Renderer which supports that MIME
30: * type. This factory is extendable as new <code>Renderer</code>s can
31: * be added at runtime.
32: * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
33: * @version CVS $Id: ExtendableRendererFactory.java 433543 2006-08-22 06:22:54Z crossley $
34: */
35: public class ExtendableRendererFactory implements RendererFactory {
36:
37: protected final static Map renderers = new HashMap();
38:
39: protected final static RendererFactory singleton = new ExtendableRendererFactory();
40:
41: private ExtendableRendererFactory() {
42: // Add the default renderers which come with Apache FOP.
43: addRenderer("application/pdf", PDFRenderer.class);
44: addRenderer("application/postscript", PSRenderer.class);
45: addRenderer("application/vnd.hp-PCL", PCLRenderer.class);
46: }
47:
48: /**
49: * Get a reference to this Renderer Factory.
50: */
51: public final static RendererFactory getRendererFactoryImplementation() {
52: return singleton;
53: }
54:
55: /**
56: * Create a renderer for a specified MIME type.
57: * @param mimeType The MIME type of the destination format
58: * @return A suitable renderer, or <code>null</code> if one cannot be found
59: */
60: public Renderer createRenderer(String mimeType) {
61: Class rendererClass = (Class) renderers.get(mimeType);
62: if (rendererClass == null) {
63: return null;
64: } else {
65: try {
66: return (Renderer) rendererClass.newInstance();
67: } catch (Exception ex) {
68: return null;
69: }
70: }
71: }
72:
73: /**
74: * Add a mapping from the specified MIME type to a renderer.
75: * Note: The renderer must have a no-argument constructor.
76: * @param mimeType The MIME type of the Renderer
77: * @param rendererClass The <code>Class</code> object for the Renderer.
78: */
79: public void addRenderer(String mimeType, Class rendererClass) {
80: renderers.put(mimeType, rendererClass);
81: }
82:
83: /**
84: * Remove the mapping from a specified MIME type.
85: * @param mimeType The MIME type to remove from the mapping.
86: */
87: public void removeRenderer(String mimeType) {
88: renderers.remove(mimeType);
89: }
90: }
|