alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/compiler/GetPropertyGenerator.java,v 1.9 2004/02/23 06:22:36 billbarker Exp $
 * $Revision: 1.9 $
 * $Date: 2004/02/23 06:22:36 $
 *
 *
 *  Copyright 1999-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.jasper.compiler;

import java.util.Hashtable;

import org.apache.jasper.Constants;
import org.apache.jasper.JasperException;
import org.apache.jasper.runtime.JspRuntimeLibrary;

/**
 * Generator for 
 *
 * @author Mandar Raje
 */
public class GetPropertyGenerator 
    extends GeneratorBase 
    implements ServiceMethodPhase 
{
    Hashtable attrs;
    BeanRepository beanInfo;
    
    public GetPropertyGenerator (Mark start, Mark stop, Hashtable attrs,
				 BeanRepository beanInfo) {
	this.attrs = attrs;
	this.beanInfo = beanInfo;
    }
    
    public void generate (ServletWriter writer, Class phase) 
	throws JasperException	{
	    String name     = getAttribute ("name");
	    String property = getAttribute ("property");

	    // Should ideally throw exception here if the bean
	    // is not present in the pageContext.
	    
	    if (beanInfo.checkVariable(name)) {
		// Bean is defined using useBean.
                // introspect at compile time
                Class cls = beanInfo.getBeanType(name);
		String clsName = cls.getName();
                java.lang.reflect.Method meth = JspRuntimeLibrary.getReadMethod(cls, property);
                
                String methodName = meth.getName();
		writer.println("out.print(" +
			       Constants.JSP_RUNTIME_PACKAGE +
			       ".JspRuntimeLibrary.toString(" +
			       "(((" + clsName + ")pageContext.findAttribute(" +
                               "\"" + name + "\"))." + methodName + "())));");
	    } else {
                // Get the class name and then introspect at runtime.
		writer.println("out.print(" +
			       Constants.JSP_RUNTIME_PACKAGE + ".JspRuntimeLibrary.toString(" +
			       Constants.JSP_RUNTIME_PACKAGE + ".JspRuntimeLibrary." +
			       "handleGetProperty(pageContext.findAttribute(" +
			       "\"" + name + "\"), \"" + property + "\")));"
	        );
	    }
    }
    
    public String getAttribute(String name) {
	return (attrs != null) ? (String) attrs.get(name) : null;
    }
}

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.