001    package echopoint.template;
002    /* 
003     * This file is part of the Echo Point Project.  This project is a collection
004     * of Components that have extended the Echo Web Application Framework.
005     *
006     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     *
018     * Alternatively, the contents of this file may be used under the terms of
019     * either the GNU General Public License Version 2 or later (the "GPL"), or
020     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
021     * in which case the provisions of the GPL or the LGPL are applicable instead
022     * of those above. If you wish to allow use of your version of this file only
023     * under the terms of either the GPL or the LGPL, and not to allow others to
024     * use your version of this file under the terms of the MPL, indicate your
025     * decision by deleting the provisions above and replace them with the notice
026     * and other provisions required by the GPL or the LGPL. If you do not delete
027     * the provisions above, a recipient may use your version of this file under
028     * the terms of any one of the MPL, the GPL or the LGPL.
029     */
030    
031    import java.io.IOException;
032    import java.io.InputStream;
033    import java.util.HashMap;
034    import java.util.Map;
035    
036    /**
037     * <code>JspTemplateDataSource</code> takes it template
038     * data from a JSP include path.
039     * <p>
040     * By default there is no caching of JSP template dtaa.  This is 
041     * because the JSP engines are excellent at deciding when JSP
042     * content needs recompiling.
043     * <p>
044     * Also since a Java class is compiled out of JSP content, then
045     * the speed at which they operate menas caching may not have a 
046     * dramatic effect.
047     */
048    public class JspTemplateDataSource extends AbstractTemplateDataSource {
049            
050            private String jspPath;
051            private Map requestAttributeMap = new HashMap();
052    
053            
054            /**
055             * Creates a <code>JspTemplateDataSource</code> with no 
056             * JSP path.
057             */
058            public JspTemplateDataSource() {
059                    this("");
060            }
061            
062            /**
063             * Creates a <code>JspTemplateDataSource</code> that
064             * takes its template data from a JSP path 
065             */
066            public JspTemplateDataSource(String jspPath) {
067                    this.jspPath = jspPath;
068                    setCachingHints(null);
069                    setContentType("jsp/xhtml");
070            }
071            
072            /**
073             * @return Returns the JSP Path in use.
074             */
075            public String getJspPath() {
076                    return jspPath;
077            }
078            /**
079             * Sets the JSP path to use for template data.
080             * 
081             * @param jspPath -  The newValue to set.
082             */
083            public void setJspPath(String jspPath) {
084                    this.jspPath = jspPath;
085            }
086            
087            /**
088             * @see echopoint.template.TemplateDataSource#getCanonicalName()
089             */
090            public String getCanonicalName() {
091                    return "jsp:" + jspPath;
092            }
093            /**
094             * This always returns null because the InputStream of JSP data
095             * can only be obtained by the backend rendering engine.  It does
096             * this by exuting the JSP path and capturing the output data.
097             *  
098             * @see echopoint.template.TemplateDataSource#getInputStream()
099             */
100            public InputStream getInputStream() throws IOException {
101                    return null;
102            }
103            
104            /**
105             * Returns the value of the named attribute as an Object, or 
106             * null if no attribute of the given name exists.
107             * <p>
108             * Attribute names should follow the same conventions 
109             * as package names. Names beginning with 
110             * java.*, javax.*, and com.sun.*, are reserved for use 
111             * by Sun Microsystems.
112             *  
113             * @param name a <code>String </code> specifying the name of the attribute
114             * @return an Object containing the value of the attribute, or null if the 
115             * attribute does not exist
116             */
117            public Object getAttribute(String name) {
118                    return requestAttributeMap.get(name);
119            }
120            
121            /**
122             * Returns an array of String containing the names of the attributes 
123             * available to this JSP request. This method returns an zero length array 
124             * if there are no attributes available to it.
125             * 
126             * @return an array of String containing the names of the attributes 
127             */
128            public String[] getAttributeNames() {
129                    return (String[]) requestAttributeMap.keySet().toArray(new String[requestAttributeMap.keySet().size()]);
130            }
131            
132            /**
133             * Associates a Java bean with a specified name.  This will be inserted
134             * into the ServletRequest (with Request scope) before the JSP is called
135             * <p>
136             * Bean names should follow the same conventions as package names. Names 
137             * beginning with java.*, javax.*, and com.sun.*, are reserved for use by 
138             * Sun Microsystems.
139             * <p>
140             * If the value passed in is null, the effect is the same as 
141             * calling <code>removeAttribute(java.lang.String)</code>.
142             * 
143             * @param name - the name to associated with the java Bean.  This will
144             *                                      become a Request scope attribute within the JSP. 
145             * @param javaBean - the object to be associated with the name
146             */
147            public void setAttribute(String name, Object javaBean) {
148                    requestAttributeMap.put(name,javaBean);
149            }
150            
151            /**
152             * Removes an attribute. 
153             * <p>
154             * Attribute names should follow the same conventions 
155             * as package names. Names beginning with 
156             * java.*, javax.*, and com.sun.*, are reserved for use 
157             * by Sun Microsystems.
158             *
159             * @param name - the name associated with the attribute value.   
160             */
161            public void removeAttribute(String name) {
162                    requestAttributeMap.remove(name);
163            }
164    }