Save This Page
Home » sitemesh-2.3 » com.opensymphony.module » sitemesh » parser » [javadoc | source]
    1   /*
    2    * Title:        AbstractPage
    3    * Description:
    4    *
    5    * This software is published under the terms of the OpenSymphony Software
    6    * License version 1.1, of which a copy has been included with this
    7    * distribution in the LICENSE.txt file.
    8    */
    9   
   10   package com.opensymphony.module.sitemesh.parser;
   11   
   12   import com.opensymphony.module.sitemesh.Page;
   13   
   14   import javax.servlet.http.HttpServletRequest;
   15   import javax.servlet.http.HttpServletRequestWrapper;
   16   import java.io;
   17   import java.util.HashMap;
   18   import java.util.Map;
   19   import java.util.Set;
   20   
   21   /**
   22    * Abstract implementation of {@link com.opensymphony.module.sitemesh.Page} .
   23    *
   24    * <p>Contains base methods for storing and accessing page properties.
   25    * Also stores {@link #pageData} as byte[] and implements write???()
   26    * methods.</p>
   27    *
   28    * <p>Concrete implementations need only set the {@link #pageData} and
   29    * call {@link #addProperty(java.lang.String,java.lang.String)} to
   30    * add all the required information.</p>
   31    *
   32    * @author <a href="joe@truemesh.com">Joe Walnes</a>
   33    * @version $Revision: 1.4 $
   34    *
   35    * @see com.opensymphony.module.sitemesh.Page
   36    */
   37   public abstract class AbstractPage implements Page {
   38       /**
   39        * Map of all properties.
   40        * Key is String. Value is java.util.List of multiple String values.
   41        */
   42       private Map properties = new HashMap();
   43   
   44       /** Date of page contents. */
   45       protected char[] pageData = new char[0];
   46   
   47       /** RequestURI of original Page. */
   48       private HttpServletRequest request;
   49   
   50       public void writePage(Writer out) throws IOException {
   51           out.write(pageData);
   52       }
   53   
   54       public String getPage() {
   55           try {
   56               StringWriter writer = new StringWriter();
   57               writePage(writer);
   58               return writer.toString();
   59           } catch (IOException e) {
   60               throw new IllegalStateException("Could not get page " + e.getMessage());
   61           }
   62       }
   63   
   64       /**
   65        * Write data of html <code>&lt;body&gt;</code> tag.
   66        *
   67        * <p>Must be implemented. Data written should not actually contain the
   68        * body tags, but all the data in between.
   69        */
   70       public abstract void writeBody(Writer out) throws IOException;
   71   
   72       public String getBody() {
   73           try {
   74               StringWriter writer = new StringWriter();
   75               writeBody(writer);
   76               return writer.toString();
   77           } catch (IOException e) {
   78               throw new IllegalStateException("Could not get body " + e.getMessage());
   79           }
   80       }
   81   
   82       /** Return title of from "title" property. Never returns null. */
   83       public String getTitle() {
   84           return noNull(getProperty("title"));
   85       }
   86   
   87       public int getContentLength() {
   88           return pageData.length;
   89       }
   90   
   91       public String getProperty(String name) {
   92           if (!isPropertySet(name)) return null;
   93           return (String)properties.get(name);
   94       }
   95   
   96       public int getIntProperty(String name) {
   97           try {
   98               return Integer.parseInt(noNull(getProperty(name)));
   99           }
  100           catch (NumberFormatException e) {
  101               return 0;
  102           }
  103       }
  104   
  105       public long getLongProperty(String name) {
  106           try {
  107               return Long.parseLong(noNull(getProperty(name)));
  108           }
  109           catch (NumberFormatException e) {
  110               return 0;
  111           }
  112       }
  113   
  114       public boolean getBooleanProperty(String name) {
  115           String property = getProperty(name);
  116           if (property == null || property.trim().length() == 0) return false;
  117           switch (property.charAt(0)) {
  118               case '1':
  119               case 't':
  120               case 'T':
  121               case 'y':
  122               case 'Y':
  123                   return true;
  124               default:
  125                   return false;
  126           }
  127       }
  128   
  129       public boolean isPropertySet(String name) {
  130           return properties.containsKey(name);
  131       }
  132   
  133       public String[] getPropertyKeys() {
  134           synchronized(properties) {
  135               Set keys = properties.keySet();
  136               return (String[])keys.toArray(new String[keys.size()]);
  137           }
  138       }
  139   
  140       public Map getProperties() {
  141           return properties;
  142       }
  143   
  144       /** @see com.opensymphony.module.sitemesh.Page#getRequest() */
  145       public HttpServletRequest getRequest() {
  146           return request;
  147       }
  148   
  149       /**
  150        * Create snapshot of Request.
  151        *
  152        * @see com.opensymphony.module.sitemesh.Page#getRequest()
  153        */
  154       public void setRequest(HttpServletRequest request) {
  155           this.request = new PageRequest(request);
  156       }
  157   
  158       /**
  159        * Add a property to the properties list.
  160        *
  161        * @param name Name of property
  162        * @param value Value of property
  163        */
  164       public void addProperty(String name, String value) {
  165           properties.put(name, value);
  166       }
  167   
  168       /** Return String as is, or "" if null. (Prevents NullPointerExceptions) */
  169       protected static String noNull(String in) {
  170           return in == null ? "" : in;
  171       }
  172   }
  173   
  174   class PageRequest extends HttpServletRequestWrapper {
  175   
  176       private String requestURI, method, pathInfo, pathTranslated, queryString, servletPath;
  177   
  178       public PageRequest(HttpServletRequest request) {
  179           super(request);
  180           requestURI = request.getRequestURI();
  181           method = request.getMethod();
  182           pathInfo = request.getPathInfo();
  183           pathTranslated = request.getPathTranslated();
  184           queryString = request.getQueryString();
  185           servletPath = request.getServletPath();
  186       }
  187   
  188       public String getRequestURI() {
  189           return requestURI;
  190       }
  191   
  192       public String getMethod() {
  193           return method;
  194       }
  195   
  196       public String getPathInfo() {
  197           return pathInfo;
  198       }
  199   
  200       public String getPathTranslated() {
  201           return pathTranslated;
  202       }
  203   
  204       public String getQueryString() {
  205           return queryString;
  206       }
  207   
  208       public String getServletPath() {
  209           return servletPath;
  210       }
  211   }

Save This Page
Home » sitemesh-2.3 » com.opensymphony.module » sitemesh » parser » [javadoc | source]