File Source: htmlview.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.web.view.HtmlView__static_init
          * 
          *  Postconditions:
          *    init'ed(log)
          */
     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.web.view;
    33  
    34  import java.io.File;
    35  import java.io.IOException;
    36  
         /* 
    P/P   *  Method: void net.sourceforge.pebble.web.view.HtmlView()
          */
    37  import javax.servlet.RequestDispatcher;
    38  import javax.servlet.ServletContext;
    39  import javax.servlet.ServletException;
    40  import javax.servlet.http.HttpServletRequest;
    41  import javax.servlet.http.HttpServletResponse;
    42  
    43  import net.sourceforge.pebble.Constants;
    44  import net.sourceforge.pebble.PebbleContext;
    45  import net.sourceforge.pebble.domain.AbstractBlog;
    46  import net.sourceforge.pebble.domain.Blog;
    47  import net.sourceforge.pebble.domain.StaticPage;
    48  import net.sourceforge.pebble.util.I18n;
    49  
    50  import org.apache.commons.logging.Log;
    51  import org.apache.commons.logging.LogFactory;
    52  
    53  /**
    54   * Represents a HTML view component, implemented by a JSP
    55   * and prepares the model for display.
    56   *
    57   * @author    Simon Brown
    58   */
    59  public abstract class HtmlView extends JspView {
    60  
    61    public static final String SYSTEM_THEME = "_pebble";
    62    public static final String DEFAULT_THEME = "default";
    63  
    64    private static Log log = LogFactory.getLog(HtmlView.class);
    65  
    66    /**
    67     * Gets the title of this view.
    68     *
    69     * @return  the title as a String
    70     */
           /* 
    P/P     *  Method: String getContentType()
            * 
            *  Preconditions:
            *    this.model != null
            *    this.model.data != null
            * 
            *  Presumptions:
            *    java.util.HashMap:get(...)@63 != null
            * 
            *  Postconditions:
            *    return_value != null
            */
    71    public String getContentType() {
    72      AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
    73      return "text/html; charset=" + blog.getCharacterEncoding();
    74    }
    75  
    76    /**
    77     * Gets the title of this view.
    78     *
    79     * @return  the title as a String
    80     */
    81    public abstract String getTitle();
    82  
           /* 
    P/P     *  Method: String getLocalizedString(String)
            * 
            *  Preconditions:
            *    this.model != null
            *    this.model.data != null
            * 
            *  Presumptions:
            *    java.util.HashMap:get(...)@63 != null
            * 
            *  Postconditions:
            *    init'ed(return_value)
            */
    83    public String getLocalizedString(String key) {
    84  	  return I18n.getMessage(((AbstractBlog)getModel().get(Constants.BLOG_KEY)).getLocale(), key);
    85    }
    86    
    87    
    88    /**
    89     * Gets the name of the theme to use.
    90     *
    91     * @return  the theme name as a String
    92     */
           /* 
    P/P     *  Method: String getTheme()
            * 
            *  Preconditions:
            *    net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration != null
            *    this.model != null
            *    this.model.data != null
            * 
            *  Presumptions:
            *    java.util.HashMap:get(...)@63 != null
            * 
            *  Postconditions:
            *    init'ed(return_value)
            * 
            *  Test Vectors:
            *    java.lang.String:equals(...)@98: {1}, {0}
            *    net.sourceforge.pebble.Configuration:isUserThemesEnabled(...)@97: {1}, {0}
            *    net.sourceforge.pebble.domain.AbstractBlog:getTheme(...)@95: Addr_Set{null}, Inverse{null}
            */
    93    protected String getTheme() {
    94      AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
    95      String theme = blog.getTheme();
    96  
    97      if (!PebbleContext.getInstance().getConfiguration().isUserThemesEnabled()) {
    98        if (theme != null && !theme.equals(DEFAULT_THEME) && !theme.equals(SYSTEM_THEME)) {
    99          return DEFAULT_THEME;
   100        }
   101      }
   102  
   103      return theme;
   104    }
   105  
   106    protected int getStatus() {
             /* 
    P/P       *  Method: int getStatus()
              * 
              *  Postconditions:
              *    return_value == 200
              */
   107      return HttpServletResponse.SC_OK;
   108    }
   109  
   110    /**
   111     * Dispatches this view.
   112     *
   113     * @param request  the HttpServletRequest instance
   114     * @param response the HttpServletResponse instance
   115     * @param context
   116     */
   117    public void dispatch(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws ServletException {
             /* 
    P/P       *  Method: void dispatch(HttpServletRequest, HttpServletResponse, ServletContext)
              * 
              *  Preconditions:
              *    context != null
              *    log != null
              *    net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration != null
              *    request != null
              *    response != null
              *    this.model != null
              *    this.model.data != null
              * 
              *  Presumptions:
              *    java.util.HashMap:get(...)@63 != null
              *    javax.servlet.ServletContext:getRequestDispatcher(...)@131 != null
              *    init'ed(net.sourceforge.pebble.Constants.THEME)
              */
   118      String theme = getTheme();
   119      request.setAttribute(Constants.THEME, theme);
   120      request.setAttribute(Constants.TITLE_KEY, getTitle());
   121      log.debug("Content is " + getUri());
   122      request.setAttribute("content", getUri());
   123      String uri = "/themes/" + theme + "/" + getTemplate() + ".jsp";
   124      log.debug("Dispatching to " + uri);
   125  
   126      response.setHeader("Cache-Control","no-cache, no-store");
   127      response.setDateHeader("Expires", 0);
   128      response.setHeader("Pragma","no-cache");
   129  
   130      try {
   131        RequestDispatcher dispatcher = context.getRequestDispatcher(uri);
   132        dispatcher.forward(request, response);
   133      } catch (IOException ioe) {
   134        ioe.printStackTrace();
   135        throw new ServletException(ioe);
   136      } finally {
   137        AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
   138        blog.log(request, getStatus());
   139      }
   140    }
   141  
           /* 
    P/P     *  Method: String getTemplate()
            * 
            *  Preconditions:
            *    this.model != null
            *    this.model.data != null
            * 
            *  Presumptions:
            *    java.util.HashMap:get(...)@63 != null
            * 
            *  Postconditions:
            *    init'ed(return_value)
            * 
            *  Test Vectors:
            *    java.io.File:canRead(...)@152: {0}, {1}
            *    java.util.HashMap:containsKey(...)@82: {1}, {0}
            */
   142    private String getTemplate() {
   143      if (!(getModel().get(Constants.BLOG_KEY) instanceof Blog)) {
   144        return "template";
   145      }
   146      if (!getModel().contains(Constants.STATIC_PAGE_KEY)) {
   147        return "template";
   148      }
   149      StaticPage staticPage = (StaticPage) getModel().get(Constants.STATIC_PAGE_KEY);
   150      Blog blog = (Blog) getModel().get(Constants.BLOG_KEY);
   151      String templateFile = blog.getThemeDirectory() + "/" + staticPage.getTemplate() + ".jsp";
   152      if (new File(templateFile).canRead()) {
   153        return staticPage.getTemplate();
   154      }
   155      return "template";
   156    }
   157  }








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