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

📄 linktag.java

📁 一个java写的business process management系统
💻 JAVA
字号:
/*
 * Copyright (c) 2003-2004, Alexander Greif
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Flow4J-Eclipse project nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.orthanc.flow4j.runtime.web.taglib;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import net.orthanc.flow4j.runtime.web.Flow4JRuntimeWebConsts;
import net.orthanc.flow4j.runtime.web.Flow4JRuntimeWebUtils;

/**
 * Class LinkTag TODO
 * @author agreif
 * 
 */
public class LinkTag
	extends Flow4JBodyTagSupport
	implements IParameterHolderTag {

	protected String flowStart;
	protected String servletPath;
	protected List params;
	protected boolean ignoreLevels = false;

	/**
	 * TODO
	 * @param string
	 */
	public void setFlowstart(String string) {
		flowStart = string;
	}

	/**
	 * TODO
	 * @param string
	 */
	public void setServletpath(String string) {
		servletPath = string;
	}

	/**
	 * TODO
	 * @return
	 */
	private String getServletpath() {
		return servletPath == null
			? Flow4JRuntimeWebUtils.getServletPath()
			: servletPath;
	}

	/**
	 * TODO
	 * @param b
	 */
	public void setIgnorelevels(String value) {
		ignoreLevels = "true".equals(value);
	}

	public void addParameter(ParamTag.Parameter param) {
		if (params == null)
			params = new ArrayList();
		if (param.getValue() != null)
			params.add(param);
	}

	/**
	 * TODO
	 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
	 */
	public int doStartTag() throws JspException {
		return EVAL_BODY_INCLUDE;
	}

	/**
	 * TODO
	 * @see javax.servlet.jsp.tagext.Tag#doEndTag()
	 */
	public int doEndTag() throws JspException {
		//	the original request before it was dispatched
		//	to the template given in the flow
		HttpServletRequest origRequest =
			(HttpServletRequest) getHttpServletRequest().getAttribute(
				Flow4JRuntimeWebConsts.DICT_KEY_SERVLET_REQUEST);

		boolean servletPathEquals = origRequest != null
			&& origRequest.getServletPath() != null
			&& ("/"+getServletpath()).equals(origRequest.getServletPath());


		StringBuffer link = new StringBuffer();

		if (! servletPathEquals && ! ignoreLevels) {
			String requestURI =
				origRequest != null
					? origRequest.getRequestURI()
					: getHttpServletRequest().getRequestURI();
			StringTokenizer st = new StringTokenizer(requestURI, "/");
			int levels = st.countTokens() - 2;

			for (int i = 0; i < levels; i++)
				link.append("../");
		}

		//if (origRequest == null || origRequest.getServletPath() == null)
		{
			if (! servletPathEquals)
				link.append(getServletpath()).append("/");
		}

		//	append flow and start name
		link.append(URLEncoder.encode(flowStart));

		//	append params
		if (params != null && !params.isEmpty()) {

			link.append("?");
			for (Iterator iter = params.iterator(); iter.hasNext();) {
				ParamTag.Parameter param = (ParamTag.Parameter) iter.next();
				link.append(URLEncoder.encode(param.getName())).append(
					"=").append(
					URLEncoder.encode(param.getValue()));
				if (iter.hasNext())
					link.append("&amp;");
			}
		}
		String result;
		try {
			result = getHttpServletResponse().encodeURL(link.toString());
		} catch (Exception e) {
			result = link.toString();
		}

		try {
			pageContext.getOut().write(result);
		} catch (IOException e) {
			throw new JspException("IOError: " + e.getMessage());
		}

		clearProperties();

		return EVAL_PAGE;
	}

	/* 
	 * TODO
	 * @see net.orthanc.flow4j.runtime.web.taglib.Flow4JBodyTagSupport#clearProperties()
	 */
	protected void clearProperties() {
		params = null;
	}

}

⌨️ 快捷键说明

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