View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.renderers;
5   
6   import java.lang.reflect.Constructor;
7   import java.lang.reflect.InvocationTargetException;
8   import java.lang.reflect.Modifier;
9   import java.util.Collections;
10  import java.util.Map;
11  import java.util.Properties;
12  import java.util.TreeMap;
13  import java.util.logging.Level;
14  import java.util.logging.Logger;
15  
16  import net.sourceforge.pmd.PropertyDescriptor;
17  
18  /**
19   * This class handles the creation of Renderers.
20   * @see Renderer
21   */
22  public class RendererFactory {
23  
24      private static final Logger LOG = Logger.getLogger(RendererFactory.class.getName());
25  
26      public static final Map<String, Class<? extends Renderer>> REPORT_FORMAT_TO_RENDERER;
27      static {
28  	Map<String, Class<? extends Renderer>> map = new TreeMap<>();
29  	map.put(XMLRenderer.NAME, XMLRenderer.class);
30  	map.put(IDEAJRenderer.NAME, IDEAJRenderer.class);
31  	map.put(TextColorRenderer.NAME, TextColorRenderer.class);
32  	map.put(TextRenderer.NAME, TextRenderer.class);
33  	map.put(TextPadRenderer.NAME, TextPadRenderer.class);
34  	map.put(EmacsRenderer.NAME, EmacsRenderer.class);
35  	map.put(CSVRenderer.NAME, CSVRenderer.class);
36  	map.put(HTMLRenderer.NAME, HTMLRenderer.class);
37  	map.put(XSLTRenderer.NAME, XSLTRenderer.class);
38  	map.put(YAHTMLRenderer.NAME, YAHTMLRenderer.class);
39  	map.put(SummaryHTMLRenderer.NAME, SummaryHTMLRenderer.class);
40  	map.put(VBHTMLRenderer.NAME, VBHTMLRenderer.class);
41  	REPORT_FORMAT_TO_RENDERER = Collections.unmodifiableMap(map);
42      }
43  
44      /**
45       * Construct an instance of a Renderer based on report format name.
46       * @param reportFormat The report format name.
47       * @param properties Initialization properties for the corresponding Renderer.
48       * @return A Renderer instance.
49       */
50      public static Renderer createRenderer(String reportFormat, Properties properties) {
51  	Class<? extends Renderer> rendererClass = getRendererClass(reportFormat);
52  	Constructor<? extends Renderer> constructor = getRendererConstructor(rendererClass);
53  
54  	Renderer renderer;
55  	try {
56  	    if (constructor.getParameterTypes().length > 0) {
57  		LOG.warning("The renderer uses a deprecated mechanism to use the properties. Please define the needed properties with this.definePropertyDescriptor(..).");
58  		renderer = constructor.newInstance(properties);
59  	    } else {
60  		renderer = constructor.newInstance();
61  
62  		for (PropertyDescriptor<?> prop : renderer.getPropertyDescriptors()) {
63  		    String value = properties.getProperty(prop.name());
64  		    if (value != null) {
65  			@SuppressWarnings("unchecked")
66  			PropertyDescriptor<Object> prop2 = (PropertyDescriptor<Object>)prop;
67  			Object valueFrom = prop2.valueFrom(value);
68  			renderer.setProperty(prop2, valueFrom);
69  		    }
70  		}
71  	    }
72  	} catch (InstantiationException e) {
73  	    throw new IllegalArgumentException("Unable to construct report renderer class: " + e.getLocalizedMessage());
74  	} catch (IllegalAccessException e) {
75  	    throw new IllegalArgumentException("Unable to construct report renderer class: " + e.getLocalizedMessage());
76  	} catch (InvocationTargetException e) {
77  	    throw new IllegalArgumentException("Unable to construct report renderer class: "
78  		    + e.getTargetException().getLocalizedMessage());
79  	}
80  	// Warn about legacy report format usages
81  	if (REPORT_FORMAT_TO_RENDERER.containsKey(reportFormat) && !reportFormat.equals(renderer.getName())) {
82  	    if (LOG.isLoggable(Level.WARNING)) {
83  	    LOG.warning("Report format '" + reportFormat + "' is deprecated, and has been replaced with '"
84  		    + renderer.getName()
85  		    + "'. Future versions of PMD will remove support for this deprecated Report format usage.");
86  	    }
87  	}
88  	return renderer;
89      }
90  
91      @SuppressWarnings("unchecked")
92      private static Class<? extends Renderer> getRendererClass(String reportFormat) {
93  	Class<? extends Renderer> rendererClass = REPORT_FORMAT_TO_RENDERER.get(reportFormat);
94  
95  	// Look up a custom renderer class
96  	if (rendererClass == null && !"".equals(reportFormat)) {
97  	    try {
98  		Class<?> clazz = Class.forName(reportFormat);
99  		if (!Renderer.class.isAssignableFrom(clazz)) {
100 		    throw new IllegalArgumentException("Custom report renderer class does not implement the "
101 			    + Renderer.class.getName() + " interface.");
102 		} else {
103 		    rendererClass = (Class<? extends Renderer>) clazz;
104 		}
105 	    } catch (ClassNotFoundException e) {
106 		throw new IllegalArgumentException("Can't find the custom format " + reportFormat + ": "
107 			+ e);
108 	    }
109 	}
110 	return rendererClass;
111     }
112 
113     private static Constructor<? extends Renderer> getRendererConstructor(Class<? extends Renderer> rendererClass) {
114 	Constructor<? extends Renderer> constructor = null;
115 
116 	// 1) Properties constructor? - deprecated
117 	try {
118 	    constructor = rendererClass.getConstructor(Properties.class);
119 	    if (!Modifier.isPublic(constructor.getModifiers())) {
120 		constructor = null;
121 	    }
122 	} catch (NoSuchMethodException e) {
123 	    // Ok
124 	}
125 
126 	// 2) No-arg constructor?
127 	try {
128 	    constructor = rendererClass.getConstructor();
129 	    if (!Modifier.isPublic(constructor.getModifiers())) {
130 		constructor = null;
131 	    }
132 	} catch (NoSuchMethodException e2) {
133 	    // Ok
134 	}
135 
136 	if (constructor == null) {
137 	    throw new IllegalArgumentException(
138 		    "Unable to find either a public java.util.Properties or no-arg constructors for Renderer class: "
139 			    + rendererClass.getName());
140 	}
141 	return constructor;
142     }
143 }