⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inserttag.java

📁 jakarta-struts-1.2.4-src
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/tiles/InsertTag.java,v 1.26 2004/03/14 06:23:49 sraeburn Exp $
 * $Revision: 1.26 $
 * $Date: 2004/03/14 06:23:49 $
 *
 * 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.struts.taglib.tiles;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.taglib.tiles.util.TagUtils;
import org.apache.struts.tiles.AttributeDefinition;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.ComponentDefinition;
import org.apache.struts.tiles.Controller;
import org.apache.struts.tiles.DefinitionAttribute;
import org.apache.struts.tiles.DefinitionNameAttribute;
import org.apache.struts.tiles.DefinitionsFactoryException;
import org.apache.struts.tiles.DirectStringAttribute;
import org.apache.struts.tiles.FactoryNotFoundException;
import org.apache.struts.tiles.NoSuchDefinitionException;
import org.apache.struts.tiles.TilesUtil;

/**
 * This is the tag handler for <tiles:insert>, which includes
 * a template. The tag's body content consists of <tiles:put>
 * tags, which are accessed by <tiles:get> in the template.
 *
 * @version $Revision: 1.26 $ $Date: 2004/03/14 06:23:49 $
 */
public class InsertTag
	extends DefinitionTagSupport
	implements PutTagParent, ComponentConstants, PutListTagParent {

	/** 
	 * The role delimiter. 
	 * @deprecated This will be removed in a release after Struts 1.2.
	 */
	public static final String ROLE_DELIMITER = ",";

	/** 
	 * Commons Logging instance. 
	 */
	protected static Log log = LogFactory.getLog(InsertTag.class);

	/* JSP Tag attributes */

	/** 
	 * Flush attribute value. 
	 */
	protected boolean flush = true;

	/** 
	 * Name to insert. 
	 */
	protected String name = null;

	/**
	 * Name of attribute from which to read page name to include. 
	 */
	protected String attribute = null;

	/** 
	 * Name of bean used as entity to include. 
	 */
	protected String beanName = null;

	/** 
	 * Name of bean property, if any. 
	 */
	protected String beanProperty = null;

	/** 
	 * Scope of bean, if any. 
	 */
	protected String beanScope = null;

	/**
	 * Are errors ignored. This is the property for attribute 'ignore'.
	 * Default value is false, which throw an exception.
	 * Only 'attribute not found' errors are ignored.
	 */
	protected boolean isErrorIgnored = false;

	/**
	 * Name of component instance to include.
	 */
	protected String definitionName = null;

	/* Internal properties */
	/**
	 * Does the end tag need to be processed.
	 * Default value is true. Boolean set in case of ignored errors.
	 */
	protected boolean processEndTag = true;

	/** 
	 * Current component context. 
	 */
	protected ComponentContext cachedCurrentContext;

	/** 
	 * Final handler of tag methods. 
	 */
	protected TagHandler tagHandler = null;

	/** 
	 * Trick to allows inner classes to access pageContext. 
	 */
	protected PageContext pageContext = null;

	/**
	 * Reset member values for reuse. This method calls super.release(),
	 * which invokes TagSupport.release(), which typically does nothing.
	 */
	public void release() {

		super.release();
		attribute = null;
		beanName = null;
		beanProperty = null;
		beanScope = null;

		definitionName = null;
		flush = true;
		name = null;
		page = null;
		role = null;
		isErrorIgnored = false;

		releaseInternal();
	}

	/**
	 * Reset internal member values for reuse.
	 */
	protected void releaseInternal() {
		cachedCurrentContext = null;
		processEndTag = true;
		// pageContext = null;  // orion doesn't set it between two tags
		tagHandler = null;
	}

	/**
	 * Set the current page context.
	 * Called by the page implementation prior to doStartTag().
	 * <p>
	 * Needed to allow inner classes to access pageContext.
	 */
	public void setPageContext(PageContext pc) {
		this.pageContext = pc;
		super.setPageContext(pc);
	}

	/**
	 * Get the pageContext property.
	 */
	public PageContext getPageContext() {
		return pageContext;
	}

	/**
	 * Set name.
	 */
	public void setName(String value) {
		this.name = value;
	}

	/**
	 * Get name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Set component.
	 */
	public void setComponent(String name) {
		this.page = name;
	}

	/**
	 * Set definition.
	 */
	public void setDefinition(String name) {
		this.definitionName = name;
	}

	/**
	 * Get definition name.
	 */
	public String getDefinitionName() {
		return definitionName;
	}

	/**
	 * Set attribute.
	 */
	public void setAttribute(String value) {
		this.attribute = value;
	}

	/**
	 * Set bean name.
	 */
	public void setBeanName(String value) {
		this.beanName = value;
	}

	/**
	 * Get bean name.
	 */
	public String getBeanName() {
		return beanName;
	}

	/**
	 * Set bean property.
	 */
	public void setBeanProperty(String value) {
		this.beanProperty = value;
	}

	/**
	 * Get bean property.
	 */
	public String getBeanProperty() {
		return beanProperty;
	}

	/**
	 * Set bean scope.
	 */
	public void setBeanScope(String value) {
		this.beanScope = value;
	}

	/**
	 * Get bean scope.
	 */
	public String getBeanScope() {
		return beanScope;
	}

	/**
	 * Set flush.
	 */
	public void setFlush(boolean flush) {
		this.flush = flush;
	}

	/**
	 * Get flush.
	 */
	public boolean getFlush() {
		return flush;
	}

	/**
	 * Set flush.
	 * Method added for compatibility with JSP1.1
	 */
	public void setFlush(String flush) {
		this.flush = (Boolean.valueOf(flush).booleanValue());
	}

	/**
	 * Set ignore.
	 */
	public void setIgnore(boolean ignore) {
		this.isErrorIgnored = ignore;
	}

	/**
	 * Get ignore.
	 */
	public boolean getIgnore() {
		return isErrorIgnored;
	}

	/////////////////////////////////////////////////////////////////////////

	/**
	 * Add a body attribute.
	 * Erase any attribute with same name previously set.
	 */
	public void putAttribute(String name, Object value) {
		tagHandler.putAttribute(name, value);
	}

	/**
	 * Process nested &lg;put&gt; tag.
	 * Method calls by nested &lg;put&gt; tags.
	 * Nested list is added to current list.
	 * If role is defined, it is checked immediately.
	 */
	public void processNestedTag(PutTag nestedTag) throws JspException {
		// Check role
		HttpServletRequest request =
			(HttpServletRequest) pageContext.getRequest();
		String role = nestedTag.getRole();
		if (role != null && !request.isUserInRole(role)) {
			// not allowed : skip attribute
			return;
		}

		putAttribute(nestedTag.getName(), nestedTag.getRealValue());
	}

	/**
	 * Process nested &lg;putList&gt; tag.
	 * Method calls by nested &lg;putList&gt; tags.
	 * Nested list is added to sub-component attributes
	 * If role is defined, it is checked immediately.
	 */
	public void processNestedTag(PutListTag nestedTag) throws JspException {
		// Check role
		HttpServletRequest request =
			(HttpServletRequest) pageContext.getRequest();
		String role = nestedTag.getRole();
		if (role != null && !request.isUserInRole(role)) {
			// not allowed : skip attribute
			return;
		}

		// Check if a name is defined
		if (nestedTag.getName() == null) {
			throw new JspException("Error - PutList : attribute name is not defined. It is mandatory as the list is added as attribute of 'insert'.");
		}

		// now add attribute to enclosing parent (i.e. : this object).
		putAttribute(nestedTag.getName(), nestedTag.getList());
	}

	/**
	 * Method calls by nested &lg;putList&gt; tags.
	 * A new list is added to current insert object.
	 */
	public void putAttribute(PutListTag nestedTag) throws JspException {
		// Check role
		HttpServletRequest request =
			(HttpServletRequest) pageContext.getRequest();
		String role = nestedTag.getRole();
		if (role != null && !request.isUserInRole(role)) {
			// not allowed : skip attribute
			return;
		}

		putAttribute(nestedTag.getName(), nestedTag.getList());
	}

	/**
	 * Get current component context.
	 */
	private ComponentContext getCurrentContext() {
		if (cachedCurrentContext == null) {
			cachedCurrentContext =
				(ComponentContext) pageContext.getAttribute(
					ComponentConstants.COMPONENT_CONTEXT,
					PageContext.REQUEST_SCOPE);
		}

		return cachedCurrentContext;
	}

	/**
	 * Get instantiated Controller.
	 * Return controller denoted by controllerType, or <code>null</code> if controllerType
	 * is null.
	 * @throws JspException If controller can't be created.
	 */
	private Controller getController() throws JspException {
		if (controllerType == null) {
			return null;
		}

		try {
			return ComponentDefinition.createController(
				controllerName,
				controllerType);

		} catch (InstantiationException ex) {
			throw new JspException(ex.getMessage());
		}
	}

	/**
	 * Process the start tag by checking tag's attributes and creating appropriate handler.
	 * Possible handlers :
	 * <ul>
	 * <li> URL
	 * <li> definition
	 * <li> direct String
	 * </ul>
	 * Handlers also contain sub-component context.
	 */
	public int doStartTag() throws JspException {
		// Check role immediatly to avoid useless stuff.
		// In case of insertion of a "definition", definition's role still checked later.
		// This lead to a double check of "role" ;-(
		HttpServletRequest request =
			(HttpServletRequest) pageContext.getRequest();
		if (role != null && !request.isUserInRole(role)) {
			processEndTag = false;
			return SKIP_BODY;
		}

		try {
			tagHandler = createTagHandler();

		} catch (JspException e) {
			if (isErrorIgnored) {
				processEndTag = false;
				return SKIP_BODY;
			} else {
				throw e;
			}
		}

		return tagHandler.doStartTag();
	}

	/**
	 * Process the end tag by including the template.
	 * Simply call the handler doEndTag
	 */
	public int doEndTag() throws JspException {
		if (!processEndTag) {
			releaseInternal();
			return EVAL_PAGE;
		}

		int res = tagHandler.doEndTag();
		// Reset properties used by object, in order to be able to reuse object.
		releaseInternal();
		return res;
	}

	/**
	 * Process tag attribute and create corresponding tag handler.
	 */
	public TagHandler createTagHandler() throws JspException {
		// Check each tag attribute.
		// page Url attribute must be the last checked  because it can appears concurrently
		// with others attributes.
		if (definitionName != null) {
			return processDefinitionName(definitionName);
		} else if (attribute != null) {
			return processAttribute(attribute);
		} else if (beanName != null) {
			return processBean(beanName, beanProperty, beanScope);
		} else if (name != null) {
			return processName(name);
		} else if (page != null) {
			return processUrl(page);
		} else {
			throw new JspException("Error - Tag Insert : At least one of the following attribute must be defined : template|page|attribute|definition|name|beanName. Check tag syntax");
		}
	}

	/**
	 * Process an object retrieved as a bean or attribute.
	 * Object can be a typed attribute, a String, or anything else.
	 * If typed attribute, use associated type.
	 * Otherwise, apply toString() on object, and use returned string as a name.
	 * @throws JspException - Throws by underlying nested call to 
	 * processDefinitionName()
	 */
	public TagHandler processObjectValue(Object value) throws JspException {
		// First, check if value is one of the Typed Attribute
		if (value instanceof AttributeDefinition) {
			// We have a type => return appropriate IncludeType
			return processTypedAttribute((AttributeDefinition) value);

		} else if (value instanceof ComponentDefinition) {
			return processDefinition((ComponentDefinition) value);
		}

		// Value must denote a valid String
		return processAsDefinitionOrURL(value.toString());
	}

	/**
	 * Process name.
	 * Search in following order :
	 * <ul>
	 * <li>Component context -  if found, process it as value.</li>
	 * <li>definitions factory</li>
	 * <li>URL</li>
	 * <li></li>
	 * </ul>
	 *
	 * @return appropriate tag handler.
	 * @throws JspException - Throws by underlying nested call to 
	 * processDefinitionName()
	 */
	public TagHandler processName(String name) throws JspException {
		Object attrValue = getCurrentContext().getAttribute(name);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -