File Source: theme.java

     1  /*
     2   * Copyright (c) 2003-2006, Simon Brown
     3   * All rights reserved.
     4   *
     5   * Redistribution and use in source and binary forms, with or without
     6   * modification, are permitted provided that the following conditions are met:
     7   *
     8   *   - Redistributions of source code must retain the above copyright
     9   *     notice, this list of conditions and the following disclaimer.
    10   *
    11   *   - Redistributions in binary form must reproduce the above copyright
    12   *     notice, this list of conditions and the following disclaimer in
    13   *     the documentation and/or other materials provided with the
    14   *     distribution.
    15   *
    16   *   - Neither the name of Pebble nor the names of its contributors may
    17   *     be used to endorse or promote products derived from this software
    18   *     without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    30   * POSSIBILITY OF SUCH DAMAGE.
    31   */
    32  package net.sourceforge.pebble.domain;
    33  
    34  import net.sourceforge.pebble.util.FileUtils;
    35  import org.apache.commons.logging.Log;
    36  import org.apache.commons.logging.LogFactory;
    37  
    38  import java.io.File;
    39  import java.io.FileInputStream;
    40  import java.io.FileOutputStream;
    41  import java.io.IOException;
    42  import java.nio.channels.FileChannel;
    43  
    44  /**
    45   * Represents the user's editable theme.
    46   *
    47   * @author    Simon Brown
    48   */
    49  public class Theme {
    50  
    51    /** the log used by this class */
           /* 
    P/P     *  Method: net.sourceforge.pebble.domain.Theme__static_init
            * 
            *  Postconditions:
            *    init'ed(log)
            */
    52    private static Log log = LogFactory.getLog(Theme.class);
    53  
    54    /** the name of the theme that should be used as a default */
    55    public static final String DEFAULT_THEME_NAME = "default";
    56  
    57    /** the blog to which this theme belongs */
    58    private Blog blog;
    59  
    60    /** the name of the theme */
    61    private String name;
    62  
    63    /** the path of the live theme (under the webapp root) */
    64    private String pathToLiveThemes;
    65  
    66    /**
    67     * Creates a new Theme instance with the specified details.
    68     *
    69     * @param blog                the owning Blog instance
    70     * @param name                the name of the theme
    71     * @param pathToLiveThemes    the path to the live themes
    72     */
           /* 
    P/P     *  Method: void net.sourceforge.pebble.domain.Theme(Blog, String, String)
            * 
            *  Postconditions:
            *    this.blog == blog
            *    init'ed(this.blog)
            *    this.name == name
            *    init'ed(this.name)
            *    this.pathToLiveThemes == pathToLiveThemes
            *    init'ed(this.pathToLiveThemes)
            */
    73    public Theme(Blog blog, String name, String pathToLiveThemes) {
    74      this.blog = blog;
    75      this.name = name;
    76      this.pathToLiveThemes = pathToLiveThemes;
    77    }
    78  
    79    /**
    80     * Gets the location where the backup version of the blog theme is stored -
    81     * under the blog.dir directory, in a sub-directory called "theme".
    82     *
    83     * @return    an absolute, local path on the filing system
    84     */
    85    String getBackupThemeDirectory() {
             /* 
    P/P       *  Method: String getBackupThemeDirectory()
              * 
              *  Preconditions:
              *    this.blog != null
              * 
              *  Presumptions:
              *    init'ed(java.io.File.separator)
              * 
              *  Postconditions:
              *    return_value != null
              */
    86      return blog.getRoot() + File.separator + "theme";
    87    }
    88  
    89    public File getPathToLiveTheme() {
             /* 
    P/P       *  Method: File getPathToLiveTheme()
              * 
              *  Preconditions:
              *    init'ed(this.name)
              *    init'ed(this.pathToLiveThemes)
              * 
              *  Postconditions:
              *    return_value == &new File(getPathToLiveTheme#1)
              *    new File(getPathToLiveTheme#1) num objects == 1
              */
    90      return new File(pathToLiveThemes, name);
    91    }
    92  
    93    /**
    94     * Restores the theme from the blog.dir to the webapp.
    95     */
    96    public void restore() {
             /* 
    P/P       *  Method: void restore()
              * 
              *  Preconditions:
              *    log != null
              *    this.blog != null
              *    init'ed(this.name)
              *    init'ed(this.pathToLiveThemes)
              */
    97      restore(DEFAULT_THEME_NAME);
    98    }
    99  
   100    /**
   101     * Restores the theme from the blog.dir to the webapp.
   102     */
   103    public void restore(String themeName) {
             /* 
    P/P       *  Method: void restore(String)
              * 
              *  Preconditions:
              *    log != null
              *    this.blog != null
              *    init'ed(this.name)
              *    init'ed(this.pathToLiveThemes)
              * 
              *  Presumptions:
              *    java.io.File:listFiles(...)@105 != null
              * 
              *  Test Vectors:
              *    java.io.File:exists(...)@105: {0}, {1}
              *    java.io.File:listFiles(...).length@105: {1..+Inf}, {0}
              */
   104      File blogTheme = new File(getBackupThemeDirectory());
   105      if (!blogTheme.exists() || blogTheme.listFiles().length == 0) {
   106        copy(themeName);
   107      }
   108  
   109      log.debug("Restoring " + name + " theme from " + getBackupThemeDirectory());
   110      copy(blogTheme, getPathToLiveTheme());
   111    }
   112  
   113    /**
   114     * Restores the theme from the blog.dir to the webapp.
   115     */
   116    public void restoreToSpecifiedTheme(String themeName) {
             /* 
    P/P       *  Method: void restoreToSpecifiedTheme(String)
              * 
              *  Preconditions:
              *    log != null
              *    this.blog != null
              *    init'ed(this.name)
              *    init'ed(this.pathToLiveThemes)
              */
   117      File blogTheme = new File(getBackupThemeDirectory());
   118      FileUtils.deleteFile(blogTheme);
   119      FileUtils.deleteFile(getPathToLiveTheme());
   120      restore(themeName);
   121    }
   122  
   123    /**
   124     * Backs up the theme from the webapp to the blog.dir.
   125     */
   126    public void backup() {
             /* 
    P/P       *  Method: void backup()
              * 
              *  Preconditions:
              *    log != null
              *    this.blog != null
              *    init'ed(this.name)
              *    init'ed(this.pathToLiveThemes)
              */
   127      backup(name);
   128    }
   129  
   130    /**
   131     * Backs up the named theme from the webapp to the blog.dir.
   132     *
   133     * @param themeName   the name of the theme to backup
   134     */
   135    private void backup(String themeName) {
             /* 
    P/P       *  Method: void backup(String)
              * 
              *  Preconditions:
              *    log != null
              *    this.blog != null
              *    init'ed(this.pathToLiveThemes)
              * 
              *  Test Vectors:
              *    java.io.File:exists(...)@141: {0}, {1}
              */
   136      log.debug("Backing up " + themeName + " theme to " + getBackupThemeDirectory());
   137      File liveTheme = new File(pathToLiveThemes, themeName);
   138      File blogTheme = new File(getBackupThemeDirectory());
   139      File blogThemeBackup = new File(getBackupThemeDirectory() + ".bak");
   140  
   141      if (blogTheme.exists()) {
   142        blogTheme.renameTo(blogThemeBackup);
   143      }
   144      copy(liveTheme, blogTheme);
   145      FileUtils.deleteFile(blogThemeBackup);
   146    }
   147  
   148    /**
   149     * Copies the named theme from the webapp to blog.dir/theme.
   150     *
   151     * @param themeName   the name of the theme to backup
   152     */
   153    private void copy(String themeName) {
             /* 
    P/P       *  Method: void copy(String)
              * 
              *  Preconditions:
              *    log != null
              *    this.blog != null
              *    init'ed(this.pathToLiveThemes)
              * 
              *  Test Vectors:
              *    java.io.File:exists(...)@159: {0}, {1}
              */
   154      log.info("Copying " + themeName + " theme to " + getBackupThemeDirectory());
   155      File liveTheme = new File(pathToLiveThemes, themeName);
   156      File blogTheme = new File(getBackupThemeDirectory());
   157      File blogThemeBackup = new File(getBackupThemeDirectory() + ".bak");
   158  
   159      if (blogTheme.exists()) {
   160        blogTheme.renameTo(blogThemeBackup);
   161      }
   162      copy(liveTheme, blogTheme);
   163      FileUtils.deleteFile(blogThemeBackup);
   164    }
   165  
   166    /**
   167     * Copies one file to another.
   168     *
   169     * @param source        the source
   170     * @param destination   the destination
   171     */
   172    private void copy(File source, File destination) {
             /* 
    P/P       *  Method: void copy(File, File)
              * 
              *  Preconditions:
              *    destination != null
              *    source != null
              *    (soft) log != null
              * 
              *  Presumptions:
              *    files.length@177 <= 232-1
              *    files[i]@177 != null
              *    java.io.FileInputStream:getChannel(...)@184 != null
              *    java.io.FileOutputStream:getChannel(...)@185 != null
              * 
              *  Test Vectors:
              *    java.io.File:exists(...)@173: {1}, {0}
              *    java.io.File:isDirectory(...)@180: {0}, {1}
              *    java.io.File:listFiles(...)@177: Addr_Set{null}, Inverse{null}
              */
   173      if (!destination.exists()) {
   174        destination.mkdir();
   175      }
   176  
   177      File files[] = source.listFiles();
   178      if (files != null) {
   179        for (int i = 0; i < files.length; i++) {
   180          if (files[i].isDirectory()) {
   181            copy(files[i], new File(destination, files[i].getName()));
   182          } else {
   183            try {
   184                FileChannel srcChannel = new FileInputStream(files[i]).getChannel();
   185                FileChannel dstChannel = new FileOutputStream(new File(destination, files[i].getName())).getChannel();
   186                dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
   187                srcChannel.close();
   188                dstChannel.close();
   189            } catch (IOException ioe) {
   190              log.error("Could not write to " + destination.getAbsolutePath(), ioe);
   191            }
   192          }
   193        }
   194      }
   195    }
   196  
   197    /**
   198     * Gets the name of this theme.
   199     *
   200     * @return    the name
   201     */
   202    public String getName() {
             /* 
    P/P       *  Method: String getName()
              * 
              *  Preconditions:
              *    init'ed(this.name)
              * 
              *  Postconditions:
              *    return_value == this.name
              *    init'ed(return_value)
              */
   203      return name;
   204    }
   205  
   206  }








SofCheck Inspector Build Version : 2.22510
theme.java 2010-Jun-25 19:40:32
theme.class 2010-Jul-19 20:23:40