Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sitemesh/parser/AbstractPage.java


1   package com.sitemesh.parser;
2   
3   import com.sitemesh.Page;
4   import java.io.*;
5   import java.util.*;
6   import javax.servlet.http.HttpServletRequest;
7   import javax.servlet.http.HttpServletRequestWrapper;
8   
9   /**
10   * Abstract implementation of {@link com.sitemesh.Page} .
11   *
12   * <p>Contains base methods for storing and accessing page properties.
13   * Also stores {@link #pageData} as byte[] and implements write???()
14   * methods.</p>
15   *
16   * <p>Concrete implementations need only set the {@link #pageData} and
17   * call {@link #addProperty(java.lang.String,java.lang.String)} to
18   * add all the required information.</p>
19   *
20   * @author <a href="joe@truemesh.com">Joe Walnes</a>
21   * @version $Revision: 1.7 $
22   *
23   * @see com.sitemesh.Page
24   */
25  public abstract class AbstractPage implements Page {
26  
27    /**
28     * Map of all properties.
29     * Key is String. Value is java.util.List of multiple String values.
30     */
31    private Map properties = new HashMap();
32  
33    /**
34     * Date of page contents.
35     */
36    protected byte[] pageData = new byte[0];
37  
38    /**
39     * RequestURI of original Page.
40     */
41    public HttpServletRequest request;
42  
43    public void writePage( OutputStream out ) throws IOException {
44      out.write( pageData );
45      out.flush();
46    }
47  
48    public void writePage( Writer out ) throws IOException {
49      Reader r = new InputStreamReader( new ByteArrayInputStream( pageData ) );
50      while ( true ) {
51        int i = r.read();
52        if ( i == -1 ) break;
53        out.write(i);
54      }
55    }
56  
57    public int getContentLength() {
58      return pageData.length;
59    }
60  
61    public String getProperty( String name ) {
62      if ( !isPropertySet( name ) ) return null;
63      Iterator i = ( ( List )properties.get( name ) ).iterator();
64      if ( i.hasNext() ) return ( String )i.next();
65      return null;
66    }
67  
68    public int getIntProperty( String name ) {
69      try {
70        return Integer.parseInt( noNull( getProperty( name ) ) );
71      }
72      catch ( NumberFormatException e ) {
73        return 0;
74      }
75    }
76  
77    public long getLongProperty( String name ) {
78      try {
79        return Long.parseLong( noNull( getProperty( name ) ) );
80      }
81      catch ( NumberFormatException e ) {
82        return 0;
83      }
84    }
85  
86    public boolean getBooleanProperty( String name ) {
87      String property = getProperty( name );
88      if ( property == null ) return false;
89      switch ( property.charAt( 0 ) ) {
90        case '1':
91        case 't':
92        case 'T':
93        case 'y':
94        case 'Y':
95          return true;
96        default:
97          return false;
98      }
99    }
100 
101   public String[] getProperties( String name ) {
102     String[] result   = new String[getPropertySize( name )];
103     List propertyList = ( List )properties.get( name );
104     Iterator it       = propertyList.iterator();
105     int i             = 0;
106     while ( it.hasNext() ) {
107       result[i++] = ( String )it.next();
108     }
109     return result;
110   }
111 
112   public boolean isPropertySet( String name ) {
113     return properties.containsKey( name );
114   }
115 
116   public int getPropertySize( String name ) {
117     if ( !isPropertySet( name ) ) return 0;
118     return ( ( List )properties.get( name ) ).size();
119   }
120 
121   public String[] getPropertyKeys() {
122     synchronized( properties ) {
123       Set keys        = properties.keySet();
124       String[] result = new String[keys.size()];
125       Iterator it     = keys.iterator();
126       int i           = 0;
127       while ( it.hasNext() ) {
128         result[i++] = ( String )it.next();
129       }
130       return result;
131     }
132   }
133 
134   /**
135    * Returns false. Override as necessary.
136    *
137    * @see com.sitemesh.Page#shouldCache()
138    */
139   public boolean shouldCache() {
140     return false;
141   }
142 
143   /**
144    * @see com.sitemesh.Page#getRequest()
145    */
146   public HttpServletRequest getRequest() {
147     return request;
148   }
149 
150   /**
151    * Create snapshot of Request.
152    *
153    * @see com.sitemesh.Page#getRequest()
154    */
155   public void setRequest(HttpServletRequest request) {
156     this.request = new PageRequest(request);
157   }
158 
159   /**
160    * Add a property to the properties list.
161    *
162    * <p>Duplicate keys are allowed... property will be given
163    * multiple values. Sub-classes should use this for adding
164    * properties to the page.</p>
165    *
166    * @param name Name of property
167    * @param value Value of property
168    */
169   protected void addProperty( String name, String value ) {
170     List propertyValues;
171     if ( properties.containsKey( name ) ) {
172       propertyValues = ( List )properties.get( name );
173     }
174     else {
175       propertyValues = new ArrayList();
176       properties.put( name, propertyValues );
177     }
178     propertyValues.add( value );
179   }
180 
181   /**
182    * Return String as is, or "" if null. (Prevents NullPointerExceptions)
183    */
184   protected String noNull( String in ) {
185     return in == null ? "" : in;
186   }
187 
188 }
189 
190 
191 class PageRequest extends HttpServletRequestWrapper {
192 
193   private String requestURI, method, pathInfo, pathTranslated,
194                  queryString, servletPath;
195 
196   public PageRequest(HttpServletRequest request) {
197     super(request);
198     requestURI = request.getRequestURI();
199     method = request.getMethod();
200     pathInfo = request.getPathInfo();
201     pathTranslated = request.getPathTranslated();
202     queryString = request.getQueryString();
203     servletPath = request.getServletPath();
204   }
205 
206   public String getRequestURI() {
207     return requestURI;
208   }
209 
210   public String getMethod() {
211     return method;
212   }
213 
214   public String getPathInfo() {
215     return pathInfo;
216   }
217 
218   public String getPathTranslated() {
219     return pathTranslated;
220   }
221 
222   public String getQueryString() {
223     return queryString;
224   }
225 
226   public String getServletPath() {
227     return servletPath;
228   }
229 
230 }