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

📄 xsltresult.java

📁 struts 2 核心包 的源码 有错误是难免的
💻 JAVA
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   XSLTResult.java

package org.apache.struts2.views.xslt;

import com.opensymphony.xwork2.*;
import com.opensymphony.xwork2.util.TextParseUtil;
import com.opensymphony.xwork2.util.ValueStack;
import java.io.*;
import java.net.URL;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

// Referenced classes of package org.apache.struts2.views.xslt:
//			AdapterFactory, ServletURIResolver

public class XSLTResult
	implements Result
{

	private static final long serialVersionUID = 0x592916d3aa9ef0bbL;
	private static final Log LOG = LogFactory.getLog(org/apache/struts2/views/xslt/XSLTResult);
	public static final String DEFAULT_PARAM = "stylesheetLocation";
	private static final Map templatesCache = new HashMap();
	protected boolean noCache;
	private String stylesheetLocation;
	private String matchingPattern;
	private String excludingPattern;
	private String exposedValue;
	private boolean parse;
	private AdapterFactory adapterFactory;

	public XSLTResult()
	{
	}

	public XSLTResult(String stylesheetLocation)
	{
		this();
		setStylesheetLocation(stylesheetLocation);
	}

	public void setNoCache(String val)
	{
		noCache = "true".equals(val);
	}

	/**
	 * @deprecated Method setLocation is deprecated
	 */

	public void setLocation(String location)
	{
		setStylesheetLocation(location);
	}

	public void setStylesheetLocation(String location)
	{
		if (location == null)
		{
			throw new IllegalArgumentException("Null location");
		} else
		{
			stylesheetLocation = location;
			return;
		}
	}

	public String getStylesheetLocation()
	{
		return stylesheetLocation;
	}

	public String getExposedValue()
	{
		return exposedValue;
	}

	public void setExposedValue(String exposedValue)
	{
		this.exposedValue = exposedValue;
	}

	public String getMatchingPattern()
	{
		return matchingPattern;
	}

	public void setMatchingPattern(String matchingPattern)
	{
		this.matchingPattern = matchingPattern;
	}

	public String getExcludingPattern()
	{
		return excludingPattern;
	}

	public void setExcludingPattern(String excludingPattern)
	{
		this.excludingPattern = excludingPattern;
	}

	public void setParse(boolean parse)
	{
		this.parse = parse;
	}

	public void execute(ActionInvocation invocation)
		throws Exception
	{
		long startTime = System.currentTimeMillis();
		String location = getStylesheetLocation();
		if (parse)
		{
			ValueStack stack = ActionContext.getContext().getValueStack();
			location = TextParseUtil.translateVariables(location, stack);
		}
		try
		{
			HttpServletResponse response = ServletActionContext.getResponse();
			Writer writer = response.getWriter();
			Templates templates = null;
			Transformer transformer;
			if (location != null)
			{
				templates = getTemplates(location);
				transformer = templates.newTransformer();
			} else
			{
				transformer = TransformerFactory.newInstance().newTransformer();
			}
			transformer.setURIResolver(getURIResolver());
			String mimeType;
			if (templates == null)
				mimeType = "text/xml";
			else
				mimeType = templates.getOutputProperties().getProperty("media-type");
			if (mimeType == null)
				mimeType = "text/html";
			response.setContentType(mimeType);
			Object result = invocation.getAction();
			if (exposedValue != null)
			{
				ValueStack stack = invocation.getStack();
				result = stack.findValue(exposedValue);
			}
			Source xmlSource = getDOMSourceForStack(result);
			PrintWriter out = response.getWriter();
			LOG.debug((new StringBuilder()).append("xmlSource = ").append(xmlSource).toString());
			transformer.transform(xmlSource, new StreamResult(out));
			out.close();
			if (LOG.isDebugEnabled())
				LOG.debug((new StringBuilder()).append("Time:").append(System.currentTimeMillis() - startTime).append("ms").toString());
			writer.flush();
		}
		catch (Exception e)
		{
			LOG.error((new StringBuilder()).append("Unable to render XSLT Template, '").append(location).append("'").toString(), e);
			throw e;
		}
	}

	protected AdapterFactory getAdapterFactory()
	{
		if (adapterFactory == null)
			adapterFactory = new AdapterFactory();
		return adapterFactory;
	}

	protected void setAdapterFactory(AdapterFactory adapterFactory)
	{
		this.adapterFactory = adapterFactory;
	}

	protected URIResolver getURIResolver()
	{
		return new ServletURIResolver(ServletActionContext.getServletContext());
	}

	protected Templates getTemplates(String path)
		throws TransformerException, IOException
	{
		String pathFromRequest = ServletActionContext.getRequest().getParameter("xslt.location");
		if (pathFromRequest != null)
			path = pathFromRequest;
		if (path == null)
			throw new TransformerException("Stylesheet path is null");
		Templates templates = (Templates)templatesCache.get(path);
		if (noCache || templates == null)
			synchronized (templatesCache)
			{
				URL resource = ServletActionContext.getServletContext().getResource(path);
				if (resource == null)
					throw new TransformerException((new StringBuilder()).append("Stylesheet ").append(path).append(" not found in resources.").toString());
				LOG.debug((new StringBuilder()).append("Preparing XSLT stylesheet templates: ").append(path).toString());
				TransformerFactory factory = TransformerFactory.newInstance();
				templates = factory.newTemplates(new StreamSource(resource.openStream()));
				templatesCache.put(path, templates);
			}
		return templates;
	}

	protected Source getDOMSourceForStack(Object value)
		throws IllegalAccessException, InstantiationException
	{
		return new DOMSource(getAdapterFactory().adaptDocument("result", value));
	}

}

⌨️ 快捷键说明

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