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

📄 velocitymanager.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:   VelocityManager.java

package org.apache.struts2.views.velocity;

import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.ValueStack;
import java.io.*;
import java.util.*;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsException;
import org.apache.struts2.util.VelocityStrutsUtil;
import org.apache.struts2.views.TagLibrary;
import org.apache.struts2.views.util.ContextUtil;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.ToolboxManager;
import org.apache.velocity.tools.view.context.ChainedContext;
import org.apache.velocity.tools.view.servlet.ServletToolboxManager;

// Referenced classes of package org.apache.struts2.views.velocity:
//			StrutsVelocityContext

public class VelocityManager
{

	private static final Log log = LogFactory.getLog(org/apache/struts2/views/velocity/VelocityManager);
	public static final String STRUTS = "struts";
	private ObjectFactory objectFactory;
	public static final String PARENT = "parent";
	public static final String TAG = "tag";
	private VelocityEngine velocityEngine;
	protected ToolboxManager toolboxManager;
	private String toolBoxLocation;
	private String chainedContextNames[];
	private Properties velocityProperties;
	private String customConfigFile;
	private List tagLibraries;

	public VelocityManager()
	{
		toolboxManager = null;
	}

	public void setObjectFactory(ObjectFactory fac)
	{
		objectFactory = fac;
	}

	public void setContainer(Container container)
	{
		List list = new ArrayList();
		Set prefixes = container.getInstanceNames(org/apache/struts2/views/TagLibrary);
		String prefix;
		for (Iterator i$ = prefixes.iterator(); i$.hasNext(); list.add(container.getInstance(org/apache/struts2/views/TagLibrary, prefix)))
			prefix = (String)i$.next();

		tagLibraries = Collections.unmodifiableList(list);
	}

	public VelocityEngine getVelocityEngine()
	{
		return velocityEngine;
	}

	public Context createContext(ValueStack stack, HttpServletRequest req, HttpServletResponse res)
	{
		VelocityContext chainedContexts[] = prepareChainedContexts(req, res, stack.getContext());
		StrutsVelocityContext context = new StrutsVelocityContext(chainedContexts, stack);
		Map standardMap = ContextUtil.getStandardContext(stack, req, res);
		java.util.Map.Entry entry;
		for (Iterator iterator = standardMap.entrySet().iterator(); iterator.hasNext(); context.put((String)entry.getKey(), entry.getValue()))
			entry = (java.util.Map.Entry)iterator.next();

		context.put("struts", new VelocityStrutsUtil(velocityEngine, context, stack, req, res));
		ServletContext ctx = null;
		try
		{
			ctx = ServletActionContext.getServletContext();
		}
		catch (NullPointerException npe)
		{
			log.debug("internal toolbox context ignored");
		}
		if (toolboxManager != null && ctx != null)
		{
			ChainedContext chained = new ChainedContext(context, req, res, ctx);
			chained.setToolbox(toolboxManager.getToolboxContext(chained));
			return chained;
		} else
		{
			return context;
		}
	}

	protected VelocityContext[] prepareChainedContexts(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Map extraContext)
	{
		if (chainedContextNames == null)
			return null;
		List contextList = new ArrayList();
		for (int i = 0; i < chainedContextNames.length; i++)
		{
			String className = chainedContextNames[i];
			try
			{
				VelocityContext velocityContext = (VelocityContext)objectFactory.buildBean(className, null);
				contextList.add(velocityContext);
			}
			catch (Exception e)
			{
				log.warn((new StringBuilder()).append("Warning.  ").append(e.getClass().getName()).append(" caught while attempting to instantiate a chained VelocityContext, ").append(className).append(" -- skipping").toString());
			}
		}

		if (contextList.size() > 0)
		{
			VelocityContext extraContexts[] = new VelocityContext[contextList.size()];
			contextList.toArray(extraContexts);
			return extraContexts;
		} else
		{
			return null;
		}
	}

	public synchronized void init(ServletContext context)
	{
		if (velocityEngine == null)
			velocityEngine = newVelocityEngine(context);
		initToolbox(context);
	}

	public Properties loadConfiguration(ServletContext context)
	{
		Properties properties;
		String defaultUserDirective;
		String configfile;
		InputStream in;
		String resourceLocation;
		if (context == null)
		{
			String gripe = "Error attempting to create a loadConfiguration from a null ServletContext!";
			log.error(gripe);
			throw new IllegalArgumentException(gripe);
		}
		properties = new Properties();
		applyDefaultConfiguration(context, properties);
		defaultUserDirective = properties.getProperty("userdirective");
		if (customConfigFile != null)
			configfile = customConfigFile;
		else
			configfile = "velocity.properties";
		configfile = configfile.trim();
		in = null;
		resourceLocation = null;
		if (context.getRealPath(configfile) != null)
		{
			String filename = context.getRealPath(configfile);
			if (filename != null)
			{
				File file = new File(filename);
				if (file.isFile())
				{
					resourceLocation = (new StringBuilder()).append(file.getCanonicalPath()).append(" from file system").toString();
					in = new FileInputStream(file);
				}
				if (in == null)
				{
					file = new File(context.getRealPath((new StringBuilder()).append("/WEB-INF/").append(configfile).toString()));
					if (file.isFile())
					{
						resourceLocation = (new StringBuilder()).append(file.getCanonicalPath()).append(" from file system").toString();
						in = new FileInputStream(file);
					}
				}
			}
		}
		if (in == null)
		{
			in = org/apache/struts2/views/velocity/VelocityManager.getClassLoader().getResourceAsStream(configfile);
			if (in != null)
				resourceLocation = (new StringBuilder()).append(configfile).append(" from classloader").toString();
		}
		if (in != null)
		{
			log.info((new StringBuilder()).append("Initializing velocity using ").append(resourceLocation).toString());
			properties.load(in);
		}
		IOException e;
		if (in != null)
			try
			{
				in.close();
			}
			// Misplaced declaration of an exception variable
			catch (IOException e) { }
		break MISSING_BLOCK_LABEL_416;
		e;
		log.warn((new StringBuilder()).append("Unable to load velocity configuration ").append(resourceLocation).toString(), e);
		if (in != null)
			try
			{
				in.close();
			}
			// Misplaced declaration of an exception variable
			catch (IOException e) { }
		break MISSING_BLOCK_LABEL_416;
		Exception exception;
		exception;
		if (in != null)
			try
			{
				in.close();
			}
			catch (IOException e) { }
		throw exception;
		if (velocityProperties != null)
		{
			String key;
			for (Iterator keys = velocityProperties.keySet().iterator(); keys.hasNext(); properties.setProperty(key, velocityProperties.getProperty(key)))
				key = (String)keys.next();

		}
		String userdirective = properties.getProperty("userdirective");
		if (userdirective == null || userdirective.trim().equals(""))
			userdirective = defaultUserDirective;
		else
			userdirective = (new StringBuilder()).append(userdirective.trim()).append(",").append(defaultUserDirective).toString();
		properties.setProperty("userdirective", userdirective);
		if (log.isDebugEnabled())
		{
			log.debug("Initializing Velocity with the following properties ...");
			Iterator iter = properties.keySet().iterator();
			do
			{
				if (!iter.hasNext())
					break;
				String key = (String)iter.next();
				String value = properties.getProperty(key);
				if (log.isDebugEnabled())
					log.debug((new StringBuilder()).append("    '").append(key).append("' = '").append(value).append("'").toString());
			} while (true);
		}
		return properties;
	}

	public void setCustomConfigFile(String val)
	{
		customConfigFile = val;
	}

	public void setToolBoxLocation(String toolboxLocation)
	{
		toolBoxLocation = toolboxLocation;
	}

	public void setChainedContexts(String contexts)
	{
		StringTokenizer st = new StringTokenizer(contexts, ",");
		List contextList = new ArrayList();
		String classname;
		for (; st.hasMoreTokens(); contextList.add(classname))
			classname = st.nextToken();

		if (contextList.size() > 0)
		{
			String chainedContexts[] = new String[contextList.size()];
			contextList.toArray(chainedContexts);
			chainedContextNames = chainedContexts;
		}
	}

	protected void initToolbox(ServletContext context)
	{
		if (toolBoxLocation != null)
			toolboxManager = ServletToolboxManager.getInstance(context, toolBoxLocation);
		else
			Velocity.info("VelocityViewServlet: No toolbox entry in configuration.");
	}

	protected VelocityEngine newVelocityEngine(ServletContext context)
	{
		if (context == null)
		{
			String gripe = "Error attempting to create a new VelocityEngine from a null ServletContext!";
			log.error(gripe);
			throw new IllegalArgumentException(gripe);
		}
		Properties p = loadConfiguration(context);
		VelocityEngine velocityEngine = new VelocityEngine();
		velocityEngine.setApplicationAttribute(javax/servlet/ServletContext.getName(), context);
		try
		{
			velocityEngine.init(p);
		}
		catch (Exception e)
		{
			String gripe = "Unable to instantiate VelocityEngine!";
			throw new StrutsException(gripe, e);
		}
		return velocityEngine;
	}

	private void applyDefaultConfiguration(ServletContext context, Properties p)
	{
		if (p.getProperty("resource.loader") == null)
			p.setProperty("resource.loader", "strutsfile, strutsclass");
		if (context.getRealPath("") != null)
		{
			p.setProperty("strutsfile.resource.loader.description", "Velocity File Resource Loader");
			p.setProperty("strutsfile.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
			p.setProperty("strutsfile.resource.loader.path", context.getRealPath(""));
			p.setProperty("strutsfile.resource.loader.modificationCheckInterval", "2");
			p.setProperty("strutsfile.resource.loader.cache", "true");
		} else
		{
			String prop = p.getProperty("resource.loader");
			if (prop.indexOf("strutsfile,") != -1)
				prop = replace(prop, "strutsfile,", "");
			else
			if (prop.indexOf(", strutsfile") != -1)
				prop = replace(prop, ", strutsfile", "");
			else
			if (prop.indexOf("strutsfile") != -1)
				prop = replace(prop, "strutsfile", "");
			p.setProperty("resource.loader", prop);
		}
		p.setProperty("strutsclass.resource.loader.description", "Velocity Classpath Resource Loader");
		p.setProperty("strutsclass.resource.loader.class", "org.apache.struts2.views.velocity.StrutsResourceLoader");
		p.setProperty("strutsclass.resource.loader.modificationCheckInterval", "2");
		p.setProperty("strutsclass.resource.loader.cache", "true");
		StringBuffer sb = new StringBuffer();
		for (Iterator i$ = tagLibraries.iterator(); i$.hasNext();)
		{
			TagLibrary tagLibrary = (TagLibrary)i$.next();
			List directives = tagLibrary.getVelocityDirectiveClasses();
			Iterator i$ = directives.iterator();
			while (i$.hasNext()) 
			{
				Class directive = (Class)i$.next();
				addDirective(sb, directive);
			}
		}

		String directives = sb.toString();
		String userdirective = p.getProperty("userdirective");
		if (userdirective == null || userdirective.trim().equals(""))
			userdirective = directives;
		else
			userdirective = (new StringBuilder()).append(userdirective.trim()).append(",").append(directives).toString();
		p.setProperty("userdirective", userdirective);
	}

	private void addDirective(StringBuffer sb, Class clazz)
	{
		sb.append(clazz.getName()).append(",");
	}

	private static final String replace(String string, String oldString, String newString)
	{
		if (string == null)
			return null;
		if (newString == null)
			return string;
		int i = 0;
		if ((i = string.indexOf(oldString, i)) >= 0)
		{
			char string2[] = string.toCharArray();
			char newString2[] = newString.toCharArray();
			int oLength = oldString.length();
			StringBuffer buf = new StringBuffer(string2.length);
			buf.append(string2, 0, i).append(newString2);
			i += oLength;
			int j;
			for (j = i; (i = string.indexOf(oldString, i)) > 0; j = i)
			{
				buf.append(string2, j, i - j).append(newString2);
				i += oLength;
			}

			buf.append(string2, j, string2.length - j);
			return buf.toString();
		} else
		{
			return string;
		}
	}

	public Properties getVelocityProperties()
	{
		return velocityProperties;
	}

	public void setVelocityProperties(Properties velocityProperties)
	{
		this.velocityProperties = velocityProperties;
	}

}

⌨️ 快捷键说明

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