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
25      // use PropertySource.getPropertyDescriptors() instead
26      protected Map<String, String> propertyDefinitions = new LinkedHashMap<>();
27      protected boolean showSuppressedViolations = true;
28      protected Writer writer;
29  
30      public AbstractRenderer(String name, String description) {
31          this.name = name;
32          this.description = description;
33      }
34  
35      /**
36       * {@inheritDoc}
37       */
38      public String getName() {
39          return name;
40      }
41  
42      /**
43       * {@inheritDoc}
44       */
45      public void setName(String name) {
46          this.name = name;
47      }
48  
49      /**
50       * {@inheritDoc}
51       */
52      public String getDescription() {
53          return description;
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      public void setDescription(String description) {
60          this.description = description;
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Deprecated
67      // use PropertySource.getPropertyDescriptors() instead
68      public Map<String, String> getPropertyDefinitions() {
69          return propertyDefinitions;
70      }
71  
72      /**
73       * Define a property.
74       * 
75       * @param name The property name.
76       * @param description The description of the property.
77       */
78      @Deprecated
79      // please use AbstractPropertySource.definePropertyDescriptor() directly
80      // instead
81      protected void defineProperty(String name, String description) {
82          StringProperty propertyDescriptor = new StringProperty(name, description, null, 0);
83          definePropertyDescriptor(propertyDescriptor);
84          propertyDefinitions.put(name, description);
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      public boolean isShowSuppressedViolations() {
91          return showSuppressedViolations;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      public void setShowSuppressedViolations(boolean showSuppressedViolations) {
98          this.showSuppressedViolations = showSuppressedViolations;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     public void setWriter(Writer writer) {
105         this.writer = writer;
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     public Writer getWriter() {
112         return writer;
113     }
114 
115     public void flush() {
116         try {
117             this.writer.flush();
118         } catch (IOException e) {
119             throw new IllegalStateException(e);
120         } finally {
121             IOUtils.closeQuietly(writer);
122         }
123     }
124 }