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.io.IOException;
7   import java.io.Writer;
8   import java.util.LinkedHashMap;
9   import java.util.Map;
10  
11  import net.sourceforge.pmd.AbstractPropertySource;
12  import net.sourceforge.pmd.lang.rule.properties.StringProperty;
13  
14  import org.apache.commons.io.IOUtils;
15  
16  /**
17   * Abstract base class for {@link Renderer} implementations.
18   */
19  public abstract class AbstractRenderer extends AbstractPropertySource implements Renderer {
20  
21      protected String name;
22      protected String description;
23  
24      @Deprecated // use PropertySource.getPropertyDescriptors() instead
25      protected Map<String, String> propertyDefinitions = new LinkedHashMap<String, String>();
26      protected boolean showSuppressedViolations = true;
27      protected Writer writer;
28  
29      public AbstractRenderer(String name, String description) {
30  		this.name = name;
31  		this.description = description;
32      }
33  
34      /**
35       * {@inheritDoc}
36       */
37      public String getName() {
38  	return name;
39      }
40  
41      /**
42       * {@inheritDoc}
43       */
44      public void setName(String name) {
45  	this.name = name;
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      public String getDescription() {
52  	return description;
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      public void setDescription(String description) {
59  	this.description = description;
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Deprecated // use PropertySource.getPropertyDescriptors() instead
66      public Map<String, String> getPropertyDefinitions() {
67  	return propertyDefinitions;
68      }
69  
70      /**
71       * Define a property.
72       * @param name The property name.
73       * @param description The description of the property.
74       */
75      @Deprecated // please use AbstractPropertySource.definePropertyDescriptor() directly instead
76      protected void defineProperty(String name, String description) {
77  	StringProperty propertyDescriptor = new StringProperty(name, description, null, 0);
78  	definePropertyDescriptor(propertyDescriptor);
79  	propertyDefinitions.put(name, description);
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      public boolean isShowSuppressedViolations() {
86  	return showSuppressedViolations;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      public void setShowSuppressedViolations(boolean showSuppressedViolations) {
93  	this.showSuppressedViolations = showSuppressedViolations;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      public void setWriter(Writer writer) {
100 	    this.writer = writer;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     public Writer getWriter() {
107 	    return writer;
108     }
109 
110     public void flush()  {
111         try {
112     		this.writer.flush();
113     	} catch (IOException e) {
114     		throw new IllegalStateException(e);
115     	} finally {
116     		IOUtils.closeQuietly(writer);
117     	}
118     }
119 }