KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > renderer > ExtendableRendererFactory


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.renderer;
17
18 import org.apache.fop.render.Renderer;
19 import org.apache.fop.render.pcl.PCLRenderer;
20 import org.apache.fop.render.pdf.PDFRenderer;
21 import org.apache.fop.render.ps.PSRenderer;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * An extendable FOP Renderer factory.
28  * When given a MIME type, find a Renderer which supports that MIME
29  * type. This factory is extendable as new <code>Renderer</code>s can
30  * be added at runtime.
31  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
32  * @version CVS $Id: ExtendableRendererFactory.java 123903 2005-01-02 21:26:59Z antonio $
33  */

34 public class ExtendableRendererFactory implements RendererFactory {
35
36   protected final static Map JavaDoc renderers = new HashMap JavaDoc();
37
38   protected final static RendererFactory singleton = new ExtendableRendererFactory();
39
40   private ExtendableRendererFactory() {
41     // Add the default renderers which come with Apache FOP.
42
addRenderer("application/pdf", PDFRenderer.class);
43     addRenderer("application/postscript", PSRenderer.class);
44     addRenderer("application/vnd.hp-PCL", PCLRenderer.class);
45   }
46
47   /**
48    * Get a reference to this Renderer Factory.
49    */

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

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

78   public void addRenderer(String JavaDoc mimeType, Class JavaDoc rendererClass) {
79     renderers.put(mimeType, rendererClass);
80   }
81
82   /**
83    * Remove the mapping from a specified MIME type.
84    * @param mimeType The MIME type to remove from the mapping.
85    */

86   public void removeRenderer(String JavaDoc mimeType) {
87     renderers.remove(mimeType);
88   }
89 }
90
Popular Tags