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