001    /*
002     * Copyright 2004-2006 Geert Bevin <gbevin[remove] at uwyn dot com>
003     * Distributed under the terms of either:
004     * - the common development and distribution license (CDDL), v1.0; or
005     * - the GNU Lesser General Public License, v2.1 or later
006     * $Id: XhtmlRendererFactory.java 3108 2006-03-13 18:03:00Z gbevin $
007     */
008    package com.uwyn.jhighlight.renderer;
009    
010    import java.util.Collections;
011    import java.util.HashMap;
012    import java.util.Map;
013    import java.util.Set;
014    
015    /**
016     * Provides a single point of entry to instantiate Xhtml renderers.
017     *
018     * @author Geert Bevin (gbevin[remove] at uwyn dot com)
019     * @version $Revision: 3108 $
020     * @since 1.0
021     */
022    public abstract class XhtmlRendererFactory
023    {
024            public final static String GROOVY = "groovy";
025            public final static String JAVA = "java";
026            public final static String BEANSHELL = "beanshell";
027            public final static String BSH = "bsh";
028            public final static String XML = "xml";
029            public final static String XHTML = "xhtml";
030            public final static String LZX = "lzx";
031            public final static String HTML = "html";
032            public final static String CPP = "cpp";
033            public final static String CXX = "cxx";
034            public final static String CPLUSPLUS = "c++";
035            
036            private final static Map RENDERERS_CLASSNAMES = new HashMap() {{
037                            put(GROOVY, GroovyXhtmlRenderer.class.getName());
038                            put(JAVA, JavaXhtmlRenderer.class.getName());
039                            put(BEANSHELL, JavaXhtmlRenderer.class.getName());
040                            put(BSH, JavaXhtmlRenderer.class.getName());
041                            put(XML, XmlXhtmlRenderer.class.getName());
042                            put(XHTML, XmlXhtmlRenderer.class.getName());
043                            put(LZX, XmlXhtmlRenderer.class.getName());
044                            put(HTML, XmlXhtmlRenderer.class.getName());
045                            put(CPP, CppXhtmlRenderer.class.getName());
046                            put(CXX, CppXhtmlRenderer.class.getName());
047                            put(CPLUSPLUS, CppXhtmlRenderer.class.getName());
048                    }};
049            
050            /**
051             * Instantiates an instance of a known <code>XhtmlRenderer</code> according to
052             * the type that's provided.
053             *
054             * @param type The type of renderer, look at the static variables of this
055             * class to see which ones are supported.
056             * @return an instance of the <code>XhtmlRenderer</code> that corresponds to the type; or
057             * <p><code>null</code> if the type wasn't known
058             * @since 1.0
059             */
060            public static Renderer getRenderer(String type)
061            {
062                    String classname = (String)RENDERERS_CLASSNAMES.get(type.toLowerCase());
063                    if (null == classname)
064                    {
065                            return null;
066                    }
067                    
068                    try
069                    {
070                            Class klass = Class.forName(classname);
071                            return (Renderer)klass.newInstance();
072                    }
073                    catch (Exception e)
074                    {
075                            throw new RuntimeException(e);
076                    }
077            }
078            
079            /**
080             * Returned a set with all the supported XHTML renderer types.
081             *
082             * @return a <code>Set</code> with the supported XHTML renderer types as strings.
083             * @since 1.0
084             */
085            public static Set getSupportedTypes()
086            {
087                    return Collections.unmodifiableSet(RENDERERS_CLASSNAMES.keySet());
088            }
089    }