Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../../img/srcFileCovDistChart0.png 48% of files have more coverage
37   157   14   6,17
10   79   0,38   6
6     2,33  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  HtmlView       Line # 59 37 0% 14 53 0% 0.0
 
No Tests
 
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   
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    */
 
71  0 toggle public String getContentType() {
72  0 AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
73  0 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   
 
83  0 toggle public String getLocalizedString(String key) {
84  0 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    */
 
93  0 toggle protected String getTheme() {
94  0 AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
95  0 String theme = blog.getTheme();
96   
97  0 if (!PebbleContext.getInstance().getConfiguration().isUserThemesEnabled()) {
98  0 if (theme != null && !theme.equals(DEFAULT_THEME) && !theme.equals(SYSTEM_THEME)) {
99  0 return DEFAULT_THEME;
100    }
101    }
102   
103  0 return theme;
104    }
105   
 
106  0 toggle protected int getStatus() {
107  0 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  0 toggle public void dispatch(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws ServletException {
118  0 String theme = getTheme();
119  0 request.setAttribute(Constants.THEME, theme);
120  0 request.setAttribute(Constants.TITLE_KEY, getTitle());
121  0 log.debug("Content is " + getUri());
122  0 request.setAttribute("content", getUri());
123  0 String uri = "/themes/" + theme + "/" + getTemplate() + ".jsp";
124  0 log.debug("Dispatching to " + uri);
125   
126  0 response.setHeader("Cache-Control","no-cache, no-store");
127  0 response.setDateHeader("Expires", 0);
128  0 response.setHeader("Pragma","no-cache");
129   
130  0 try {
131  0 RequestDispatcher dispatcher = context.getRequestDispatcher(uri);
132  0 dispatcher.forward(request, response);
133    } catch (IOException ioe) {
134  0 ioe.printStackTrace();
135  0 throw new ServletException(ioe);
136    } finally {
137  0 AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
138  0 blog.log(request, getStatus());
139    }
140    }
141   
 
142  0 toggle private String getTemplate() {
143  0 if (!(getModel().get(Constants.BLOG_KEY) instanceof Blog)) {
144  0 return "template";
145    }
146  0 if (!getModel().contains(Constants.STATIC_PAGE_KEY)) {
147  0 return "template";
148    }
149  0 StaticPage staticPage = (StaticPage) getModel().get(Constants.STATIC_PAGE_KEY);
150  0 Blog blog = (Blog) getModel().get(Constants.BLOG_KEY);
151  0 String templateFile = blog.getThemeDirectory() + "/" + staticPage.getTemplate() + ".jsp";
152  0 if (new File(templateFile).canRead()) {
153  0 return staticPage.getTemplate();
154    }
155  0 return "template";
156    }
157    }