FileDocCategorySizeDatePackage
TagPluginManager.javaAPI DocGlassfish v2 API7994Fri May 04 22:32:52 BST 2007org.apache.jasper.compiler

TagPluginManager

public class TagPluginManager extends Object
Manages tag plugin optimizations.
author
Kin-man Chung

Fields Summary
private static final String
TAG_PLUGINS_XML
private static final String
TAG_PLUGINS_ROOT_ELEM
private boolean
initialized
private HashMap
tagPlugins
private ServletContext
ctxt
private PageInfo
pageInfo
Constructors Summary
public TagPluginManager(ServletContext ctxt)


       
	this.ctxt = ctxt;
    
Methods Summary
public voidapply(Node.Nodes page, ErrorDispatcher err, PageInfo pageInfo)


	init(err);
	if (tagPlugins == null || tagPlugins.size() == 0) {
	    return;
	}

	this.pageInfo = pageInfo;

        page.visit(new Node.Visitor() {
            public void visit(Node.CustomTag n)
                    throws JasperException {
                invokePlugin(n);
                visitBody(n);
            }
        });

    
private voidinit(ErrorDispatcher err)

	if (initialized)
	    return;

	InputStream is = ctxt.getResourceAsStream(TAG_PLUGINS_XML);
	if (is == null)
	    return;

	TreeNode root = (new ParserUtils()).parseXMLDocument(TAG_PLUGINS_XML,
							     is);
	if (root == null) {
	    return;
	}

	if (!TAG_PLUGINS_ROOT_ELEM.equals(root.getName())) {
	    err.jspError("jsp.error.plugin.wrongRootElement", TAG_PLUGINS_XML,
			 TAG_PLUGINS_ROOT_ELEM);
	}

	tagPlugins = new HashMap();
	Iterator pluginList = root.findChildren("tag-plugin");
	while (pluginList.hasNext()) {
	    TreeNode pluginNode = (TreeNode) pluginList.next();
            TreeNode tagClassNode = pluginNode.findChild("tag-class");
	    if (tagClassNode == null) {
		// Error
		return;
	    }
	    String tagClass = tagClassNode.getBody().trim();
	    TreeNode pluginClassNode = pluginNode.findChild("plugin-class");
	    if (pluginClassNode == null) {
		// Error
		return;
	    }

	    String pluginClassStr = pluginClassNode.getBody();
	    TagPlugin tagPlugin = null;
	    try {
		Class pluginClass = Class.forName(pluginClassStr);
		tagPlugin = (TagPlugin) pluginClass.newInstance();
	    } catch (Exception e) {
		throw new JasperException(e);
	    }
	    if (tagPlugin == null) {
		return;
	    }
	    tagPlugins.put(tagClass, tagPlugin);
	}
	initialized = true;
    
private voidinvokePlugin(Node.CustomTag n)
Invoke tag plugin for the given custom tag, if a plugin exists for the custom tag's tag handler. The given custom tag node will be manipulated by the plugin.

	TagPlugin tagPlugin = (TagPlugin)
		tagPlugins.get(n.getTagHandlerClass().getName());
	if (tagPlugin == null) {
	    return;
	}

	TagPluginContext tagPluginContext = new TagPluginContextImpl(n, pageInfo);
	n.setTagPluginContext(tagPluginContext);
	tagPlugin.doTag(tagPluginContext);