//# 0 errors, 112 messages
//#
/*
    //#pluginproperties.java:1:1: class: net.sourceforge.pebble.PluginProperties
 * Copyright (c) 2003-2006, Simon Brown
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *
 *   - Neither the name of Pebble nor the names of its contributors may
 *     be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package net.sourceforge.pebble;

import net.sourceforge.pebble.domain.Blog;
import net.sourceforge.pebble.event.response.ContentSpamListener;
import net.sourceforge.pebble.event.response.SpamScoreListener;
import net.sourceforge.pebble.event.response.LinkSpamListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.util.*;

/**
 * Contains properties that can be used by Pebble plugins.
 *
 * @author Simon Brown
 */
public class PluginProperties {

  /**
   * the log used by this class
   */
  private static final Log log = LogFactory.getLog(PluginProperties.class);
    //#pluginproperties.java:54: method: net.sourceforge.pebble.PluginProperties.net.sourceforge.pebble.PluginProperties__static_init
    //#pluginproperties.java:54: Warning: method not available
    //#    -- call on Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.PluginProperties
    //#    method: net.sourceforge.pebble.PluginProperties__static_init
    //#    unanalyzed callee: Log org.apache.commons.logging.LogFactory:getLog(Class)
    //#output(net.sourceforge.pebble.PluginProperties__static_init): __Descendant_Table[net/sourceforge/pebble/PluginProperties]
    //#output(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.getProperties()Ljava/util/Properties;
    //#output(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.getPropertiesAsString()Ljava/lang/String;
    //#output(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.getProperty(Ljava/lang/String;)Ljava/lang/String;
    //#output(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.hasProperty(Ljava/lang/String;)Z
    //#output(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.loadProperties()V
    //#output(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.setProperty(Ljava/lang/String;Ljava/lang/String;)V
    //#output(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.store()V
    //#output(net.sourceforge.pebble.PluginProperties__static_init): log
    //#post(net.sourceforge.pebble.PluginProperties__static_init): __Descendant_Table[net/sourceforge/pebble/PluginProperties] == &__Dispatch_Table
    //#post(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.getProperties()Ljava/util/Properties; == &getProperties
    //#post(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.getPropertiesAsString()Ljava/lang/String; == &getPropertiesAsString
    //#post(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.getProperty(Ljava/lang/String;)Ljava/lang/String; == &getProperty
    //#post(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.hasProperty(Ljava/lang/String;)Z == &hasProperty
    //#post(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.loadProperties()V == &loadProperties
    //#post(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.setProperty(Ljava/lang/String;Ljava/lang/String;)V == &setProperty
    //#post(net.sourceforge.pebble.PluginProperties__static_init): __Dispatch_Table.store()V == &store
    //#post(net.sourceforge.pebble.PluginProperties__static_init): init'ed(log)
    //#pluginproperties.java:54: end of method: net.sourceforge.pebble.PluginProperties.net.sourceforge.pebble.PluginProperties__static_init

  /**
   * the Properties object that backs this instance
   */
  private Properties properties;

  /**
   * the owning blog
   */
  private Blog blog;

  /**
   * Creates a new instance with the specified owning blog.
   *
   * @param blog the owning Blog instance
   */
  public PluginProperties(Blog blog) {
    //#pluginproperties.java:71: method: void net.sourceforge.pebble.PluginProperties.net.sourceforge.pebble.PluginProperties(Blog)
    //#input(void net.sourceforge.pebble.PluginProperties(Blog)): blog
    //#input(void net.sourceforge.pebble.PluginProperties(Blog)): log
    //#input(void net.sourceforge.pebble.PluginProperties(Blog)): this
    //#output(void net.sourceforge.pebble.PluginProperties(Blog)): new Properties(loadProperties#1) num objects
    //#output(void net.sourceforge.pebble.PluginProperties(Blog)): this.blog
    //#output(void net.sourceforge.pebble.PluginProperties(Blog)): this.properties
    //#new obj(void net.sourceforge.pebble.PluginProperties(Blog)): new Properties(loadProperties#1)
    //#pre[1] (void net.sourceforge.pebble.PluginProperties(Blog)): (soft) blog != null
    //#post(void net.sourceforge.pebble.PluginProperties(Blog)): this.blog == blog
    //#post(void net.sourceforge.pebble.PluginProperties(Blog)): (soft) this.blog != null
    //#post(void net.sourceforge.pebble.PluginProperties(Blog)): this.properties == &new Properties(loadProperties#1)
    //#post(void net.sourceforge.pebble.PluginProperties(Blog)): new Properties(loadProperties#1) num objects == 1
    //#unanalyzed(void net.sourceforge.pebble.PluginProperties(Blog)): Effects-of-calling:java.util.Properties
    //#unanalyzed(void net.sourceforge.pebble.PluginProperties(Blog)): Effects-of-calling:java.util.Properties:setProperty
    //#unanalyzed(void net.sourceforge.pebble.PluginProperties(Blog)): Effects-of-calling:net.sourceforge.pebble.domain.Blog:getPluginPropertiesFile
    //#unanalyzed(void net.sourceforge.pebble.PluginProperties(Blog)): Effects-of-calling:java.io.File
    //#unanalyzed(void net.sourceforge.pebble.PluginProperties(Blog)): Effects-of-calling:java.io.File:exists
    //#unanalyzed(void net.sourceforge.pebble.PluginProperties(Blog)): Effects-of-calling:java.io.FileInputStream
    //#unanalyzed(void net.sourceforge.pebble.PluginProperties(Blog)): Effects-of-calling:java.util.Properties:load
    //#unanalyzed(void net.sourceforge.pebble.PluginProperties(Blog)): Effects-of-calling:java.io.FileInputStream:close
    //#unanalyzed(void net.sourceforge.pebble.PluginProperties(Blog)): Effects-of-calling:java.io.IOException:getMessage
    //#unanalyzed(void net.sourceforge.pebble.PluginProperties(Blog)): Effects-of-calling:org.apache.commons.logging.Log:error
    this.blog = blog;
    loadProperties();
  }
    //#pluginproperties.java:74: end of method: void net.sourceforge.pebble.PluginProperties.net.sourceforge.pebble.PluginProperties(Blog)

  /**
   * Helper method to load the properties from disk.
   */
  private void loadProperties() {
    try {
      properties = new Properties();
    //#pluginproperties.java:81: method: void net.sourceforge.pebble.PluginProperties.loadProperties()
    //#input(void loadProperties()): log
    //#input(void loadProperties()): this
    //#input(void loadProperties()): this.blog
    //#output(void loadProperties()): new Properties(loadProperties#1) num objects
    //#output(void loadProperties()): this.properties
    //#new obj(void loadProperties()): new Properties(loadProperties#1)
    //#pre[3] (void loadProperties()): (soft) this.blog != null
    //#presumption(void loadProperties()): org.apache.commons.logging.LogFactory:getLog(...)@54 != null
    //#post(void loadProperties()): this.properties == One-of{&new Properties(loadProperties#1), old this.properties}
    //#post(void loadProperties()): new Properties(loadProperties#1) num objects <= 1
    //#test_vector(void loadProperties()): java.io.File:exists(...)@90: {1}, {0}
      properties.setProperty(ContentSpamListener.REGEX_LIST_KEY, ContentSpamListener.DEFAULT_REGEX_LIST);
      properties.setProperty(ContentSpamListener.THRESHOLD_KEY, "" + ContentSpamListener.DEFAULT_THRESHOLD);
      properties.setProperty(LinkSpamListener.COMMENT_THRESHOLD_KEY, "" + LinkSpamListener.DEFAULT_THRESHOLD);
      properties.setProperty(LinkSpamListener.TRACKBACK_THRESHOLD_KEY, "" + LinkSpamListener.DEFAULT_THRESHOLD);
      properties.setProperty(SpamScoreListener.COMMENT_THRESHOLD_KEY, "" + SpamScoreListener.DEFAULT_THRESHOLD);
      properties.setProperty(SpamScoreListener.TRACKBACK_THRESHOLD_KEY, "" + SpamScoreListener.DEFAULT_THRESHOLD);

      File propertiesFile = new File(blog.getPluginPropertiesFile());
    //#pluginproperties.java:89: Warning: method not available
    //#    -- call on String net.sourceforge.pebble.domain.Blog:getPluginPropertiesFile()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.PluginProperties
    //#    method: void loadProperties()
    //#    unanalyzed callee: String net.sourceforge.pebble.domain.Blog:getPluginPropertiesFile()
      if (!propertiesFile.exists()) {
        return;
      }

      FileInputStream fin = new FileInputStream(propertiesFile);
      properties.load(fin);
      fin.close();
    } catch (FileNotFoundException fnfe) {
      // do nothing - there are no plugin properties to load
    } catch (IOException e) {
      log.error(e.getMessage());
    //#pluginproperties.java:100: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:error(Object)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.PluginProperties
    //#    method: void loadProperties()
    //#    unanalyzed callee: void org.apache.commons.logging.Log:error(Object)
    }
  }
    //#pluginproperties.java:102: end of method: void net.sourceforge.pebble.PluginProperties.loadProperties()

  /**
   * Gets properties file as a String in the format it is saved on disk.
   *
   * @return a String
   */
  public String getPropertiesAsString() {
    StringBuffer buf = new StringBuffer();
    //#pluginproperties.java:110: method: String net.sourceforge.pebble.PluginProperties.getPropertiesAsString()
    //#input(String getPropertiesAsString()): this
    //#input(String getPropertiesAsString()): this.properties
    //#output(String getPropertiesAsString()): return_value
    //#pre[2] (String getPropertiesAsString()): this.properties != null
    //#post(String getPropertiesAsString()): return_value != null
    //#test_vector(String getPropertiesAsString()): java.util.Iterator:hasNext(...)@114: {1}, {0}
    List keys = new ArrayList(properties.keySet());
    Collections.sort(keys);
    Iterator it = keys.iterator();
    while (it.hasNext()) {
      String key = (String) it.next();
      buf.append(key);
      buf.append("=");
      buf.append(properties.getProperty(key));
      buf.append(System.getProperty("line.separator"));
    }

    return buf.toString();
    //#pluginproperties.java:122: end of method: String net.sourceforge.pebble.PluginProperties.getPropertiesAsString()
  }

  /**
   * Determines whether a property with the specified name exists.
   *
   * @param name the name of th eproperty to test for
   * @return true if the property exists, false otherwise
   */
  public boolean hasProperty(String name) {
    return properties.containsKey(name);
    //#pluginproperties.java:132: method: bool net.sourceforge.pebble.PluginProperties.hasProperty(String)
    //#input(bool hasProperty(String)): name
    //#input(bool hasProperty(String)): this
    //#input(bool hasProperty(String)): this.properties
    //#output(bool hasProperty(String)): return_value
    //#pre[3] (bool hasProperty(String)): this.properties != null
    //#post(bool hasProperty(String)): init'ed(return_value)
    //#pluginproperties.java:132: end of method: bool net.sourceforge.pebble.PluginProperties.hasProperty(String)
  }

  /**
   * Gets the named property.
   *
   * @param name the name of the property
   * @return the value of the property, or null if it doesn't exist
   */
  public String getProperty(String name) {
    return properties.getProperty(name);
    //#pluginproperties.java:142: method: String net.sourceforge.pebble.PluginProperties.getProperty(String)
    //#input(String getProperty(String)): name
    //#input(String getProperty(String)): this
    //#input(String getProperty(String)): this.properties
    //#output(String getProperty(String)): return_value
    //#pre[3] (String getProperty(String)): this.properties != null
    //#post(String getProperty(String)): init'ed(return_value)
    //#pluginproperties.java:142: end of method: String net.sourceforge.pebble.PluginProperties.getProperty(String)
  }

  public Properties getProperties() {
    return properties;
    //#pluginproperties.java:146: method: Properties net.sourceforge.pebble.PluginProperties.getProperties()
    //#input(Properties getProperties()): this
    //#input(Properties getProperties()): this.properties
    //#output(Properties getProperties()): return_value
    //#pre[2] (Properties getProperties()): init'ed(this.properties)
    //#post(Properties getProperties()): return_value == this.properties
    //#post(Properties getProperties()): init'ed(return_value)
    //#pluginproperties.java:146: end of method: Properties net.sourceforge.pebble.PluginProperties.getProperties()
  }

  /**
   * Sets the named property.
   *
   * @param name  the name of the property
   * @param value the value of the property
   */
  public void setProperty(String name, String value) {
    properties.setProperty(name, value);
    //#pluginproperties.java:156: method: void net.sourceforge.pebble.PluginProperties.setProperty(String, String)
    //#input(void setProperty(String, String)): name
    //#input(void setProperty(String, String)): this
    //#input(void setProperty(String, String)): this.properties
    //#input(void setProperty(String, String)): value
    //#pre[3] (void setProperty(String, String)): this.properties != null
  }
    //#pluginproperties.java:157: end of method: void net.sourceforge.pebble.PluginProperties.setProperty(String, String)

  /**
   * Helper method to store the properties to disk.
   */
  public void store() {
    try {
      FileOutputStream fout = new FileOutputStream(blog.getPluginPropertiesFile());
    //#pluginproperties.java:164: method: void net.sourceforge.pebble.PluginProperties.store()
    //#pluginproperties.java:164: Warning: method not available
    //#    -- call on String net.sourceforge.pebble.domain.Blog:getPluginPropertiesFile()
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.PluginProperties
    //#    method: void store()
    //#    unanalyzed callee: String net.sourceforge.pebble.domain.Blog:getPluginPropertiesFile()
    //#input(void store()): log
    //#input(void store()): this
    //#input(void store()): this.blog
    //#input(void store()): this.properties
    //#pre[2] (void store()): (soft) this.blog != null
    //#pre[3] (void store()): (soft) this.properties != null
    //#presumption(void store()): org.apache.commons.logging.LogFactory:getLog(...)@54 != null
      if (fout != null) {
        properties.store(fout, "Plugin properties");
        fout.flush();
        fout.close();
      }
    } catch (FileNotFoundException fnfe) {
    } catch (IOException e) {
      log.error(e.getMessage());
    //#pluginproperties.java:172: Warning: method not available
    //#    -- call on void org.apache.commons.logging.Log:error(Object)
    //#    severity: INFORMATIONAL
    //#    class: net.sourceforge.pebble.PluginProperties
    //#    method: void store()
    //#    unanalyzed callee: void org.apache.commons.logging.Log:error(Object)
    }
  }
    //#pluginproperties.java:174: end of method: void net.sourceforge.pebble.PluginProperties.store()

}    //#pluginproperties.java:: end of class: net.sourceforge.pebble.PluginProperties
