filerefererfilterdao.java

### File Source: filerefererfilterdao.java

         1  package net.sourceforge.pebble.dao.file;
         2
         3  import net.sourceforge.pebble.dao.PersistenceException;
         4  import net.sourceforge.pebble.dao.RefererFilterDAO;
         5  import net.sourceforge.pebble.domain.Blog;
         6  import net.sourceforge.pebble.domain.RefererFilter;
         7  import net.sourceforge.pebble.domain.RefererFilterManager;
         8  import org.apache.commons.logging.Log;
         9  import org.apache.commons.logging.LogFactory;
        10
        11  import java.io.*;
        12  import java.util.ArrayList;
        13  import java.util.Collection;
        14  import java.util.Iterator;
        15
        16  /**
        17   * A mock implementation of the CategoryDAO interface that does nothing. This
        18   * is used when performing unit tests.
        19   *
        20   * @author    Simon Brown
        21   */
             /*
        P/P   *  Method: void net.sourceforge.pebble.dao.file.FileRefererFilterDAO()
              */
        22  public class FileRefererFilterDAO implements RefererFilterDAO {
        23
        24    /** the name of the file containing the filter */
        25    private static final String FILTERS_FILE = "refererFilters.txt";
        26
        27    /** the log used by this class */
               /*
        P/P     *  Method: net.sourceforge.pebble.dao.file.FileRefererFilterDAO__static_init
                *
                *  Postconditions:
                *    init'ed(log)
                */
        28    private static Log log = LogFactory.getLog(RefererFilterManager.class);
        29
        30    /**
        31     * Loads the referer filters.
        32     *
        33     * @param rootBlog    the owning Blog instance
        34     * @return  a Collection of RefererFilter instances
        35     * @throws  PersistenceException    if filters cannot be loaded
        36     */
        37    public Collection getRefererFilters(Blog rootBlog) throws PersistenceException {
                 /*
        P/P       *  Method: Collection getRefererFilters(Blog)
                  *
                  *  Preconditions:
                  *    rootBlog != null
                  *    (soft) log != null
                  *
                  *  Postconditions:
                  *    return_value == &new ArrayList(getRefererFilters#1)
                  *    new ArrayList(getRefererFilters#1) num objects == 1
                  *
                  *  Test Vectors:
                  *    java.io.File:exists(...)@43: {1}, {0}
                  */
        38      ArrayList filters = new ArrayList();
        39      String root = rootBlog.getRoot();
        40      try {
        41
        42        File filtersFile = new File(root, FILTERS_FILE);
        43        if (!filtersFile.exists()) {
        44          return filters;
        45        }
        46
        47        BufferedReader reader = new BufferedReader(new FileReader(filtersFile));
        48        String expression = reader.readLine();
        49        while (expression != null) {
        50          filters.add(new RefererFilter(expression));
        51          expression = reader.readLine();
        52        }
        53
        54        reader.close();
        55      } catch (IOException ioe) {
        56        log.error("A " + FILTERS_FILE + " file at " + root + " cannot be loaded", ioe);
        57      }
        58
        59      return filters;
        60    }
        61
        62    /**
        63     * Adds the specified referer filter.
        64     *
        65     * @param filter    the RefererFilter instance to be added
        66     * @param rootBlog    the owning Blog instance
        67     * @throws PersistenceException   if something goes wrong storing the filters
        68     */
        69    public void addRefererFilter(RefererFilter filter, Blog rootBlog) throws PersistenceException {
                 /*
        P/P       *  Method: void addRefererFilter(RefererFilter, Blog)
                  *
                  *  Preconditions:
                  *    rootBlog != null
                  *    (soft) log != null
                  */
        70      Collection filters = getRefererFilters(rootBlog);
        71      filters.add(filter);
        72      store(filters, rootBlog);
        73    }
        74
        75    /**
        76     * Removes the specified referer filter.
        77     *
        78     * @param filter    the RefererFilter instance to be removed
        79     * @param rootBlog    the owning Blog instance
        80     * @throws PersistenceException   if something goes wrong removing the filter
        81     */
        82    public void deleteRefererFilter(RefererFilter filter, Blog rootBlog) throws PersistenceException {
                 /*
        P/P       *  Method: void deleteRefererFilter(RefererFilter, Blog)
                  *
                  *  Preconditions:
                  *    rootBlog != null
                  *    (soft) log != null
                  */
        83      Collection filters = getRefererFilters(rootBlog);
        84      filters.remove(filter);
        85      store(filters, rootBlog);
        86    }
        87
        88    /**
        89     * Helper method to store all filters for a given blog.
        90     *
        91     * @param filters   the Collection of RefererFilter instances to store
        92     * @param rootBlog      the blog to which the filters belong
        93     */
        94    private void store(Collection filters, Blog rootBlog) throws PersistenceException {
        95      try {
                   /*
        P/P         *  Method: void store(Collection, Blog)
                    *
                    *  Preconditions:
                    *    filters != null
                    *    rootBlog != null
                    *
                    *  Presumptions:
                    *    java.util.Iterator:next(...)@102 != null
                    *
                    *  Test Vectors:
                    *    java.util.Iterator:hasNext(...)@101: {1}, {0}
                    */
        96        String root = rootBlog.getRoot();
        97        BufferedWriter writer = new BufferedWriter(new FileWriter(new File(root, FILTERS_FILE)));
        98
        99        Iterator it = filters.iterator();
       100        RefererFilter filter;
       101        while (it.hasNext()) {
       102          filter = (RefererFilter)it.next();
       103          writer.write(filter.getExpression());
       104          writer.newLine();
       105        }
       106
       107        writer.flush();
       108        writer.close();
       109      } catch (IOException ioe) {
       110        log.error(ioe);
       111        throw new PersistenceException("Filters could not be saved : " + ioe.getMessage());
       112      }
       113    }
       114
       115  }

SofCheck Inspector Build Version : 2.22510

filerefererfilterdao.java | 2010-Jun-25 19:40:32
filerefererfilterdao.class | 2010-Jul-19 20:23:40