kinman 2002/12/03 16:48:43 Modified: jasper2/src/share/org/apache/jasper EmbededServletOptions.java JspC.java Options.java jasper2/src/share/org/apache/jasper/compiler Compiler.java Node.java TagPluginManager.java jasper2/src/share/org/apache/jasper/compiler/tagplugin TagPlugin.java TagPluginContext.java Removed: jasper2/src/share/org/apache/jasper/compiler/tagplugin TagPluginFactory.java Log: - First cut in defining the tag plugin interface and an implementation of framework in Jasper. WARNING: This is still experimental and subject to change. Revision Changes Path 1.14 +15 -3 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/EmbededServletOptions.java Index: EmbededServletOptions.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/EmbededServletOptions.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- EmbededServletOptions.java 28 Nov 2002 04:18:07 -0000 1.13 +++ EmbededServletOptions.java 4 Dec 2002 00:48:42 -0000 1.14 @@ -70,6 +70,7 @@ import org.apache.jasper.compiler.TldLocationsCache; import org.apache.jasper.compiler.JspConfig; +import org.apache.jasper.compiler.TagPluginManager; import org.apache.jasper.xmlparser.ParserUtils; import java.util.*; @@ -175,6 +176,11 @@ private JspConfig jspConfig = null; /** + * TagPluginManager + */ + private TagPluginManager tagPluginManager = null; + + /** * Java platform encoding to generate the JSP * page servlet. */ @@ -301,6 +307,10 @@ return jspConfig; } + public TagPluginManager getTagPluginManager() { + return tagPluginManager; + } + /** * Create an EmbededServletOptions object using data available from * ServletConfig and ServletContext. @@ -475,6 +485,8 @@ // Setup the jsp config info for this web app. jspConfig = new JspConfig(context); + // Create a Tag plugin instance + tagPluginManager = new TagPluginManager(context); } } 1.19 +15 -6 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/JspC.java Index: JspC.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/JspC.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- JspC.java 16 Nov 2002 04:20:09 -0000 1.18 +++ JspC.java 4 Dec 2002 00:48:42 -0000 1.19 @@ -74,6 +74,8 @@ import org.apache.jasper.logging.Logger; import org.apache.jasper.logging.JasperLogger; import org.apache.jasper.compiler.JspConfig; +import org.apache.jasper.compiler.JspConfig; +import org.apache.jasper.compiler.TagPluginManager; /** * Shell for the jspc compiler. Handles all options associated with the @@ -197,6 +199,9 @@ */ private TldLocationsCache tldLocationsCache = null; + private JspConfig jspConfig = null; + private TagPluginManager tagPluginManager = null; + private boolean listErrors = false; private boolean showSuccess = false; @@ -737,6 +742,8 @@ } catch (MalformedURLException me) { System.out.println("**" + me); } + jspConfig = new JspConfig(context); + tagPluginManager = new TagPluginManager(context); } @@ -945,10 +952,12 @@ * Obtain JSP configuration informantion specified in web.xml. */ public JspConfig getJspConfig() { - // XXX - Stubbed out so Jasper compiles. - initServletContext(); - return new JspConfig( context ); + return jspConfig; } + + public TagPluginManager getTagPluginManager() { + return tagPluginManager; + } } 1.10 +9 -3 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/Options.java Index: Options.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/Options.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- Options.java 16 Nov 2002 04:20:09 -0000 1.9 +++ Options.java 4 Dec 2002 00:48:42 -0000 1.10 @@ -68,6 +68,7 @@ import org.apache.jasper.compiler.TldLocationsCache; import org.apache.jasper.compiler.JspConfig; +import org.apache.jasper.compiler.TagPluginManager; /** * A class to hold all init parameters specific to the JSP engine. @@ -172,4 +173,9 @@ * Obtain JSP configuration informantion specified in web.xml. */ public JspConfig getJspConfig(); + + /** + * Obtain a Tag Plugin Manager + */ + public TagPluginManager getTagPluginManager(); } 1.39 +4 -0 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java Index: Compiler.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java,v retrieving revision 1.38 retrieving revision 1.39 diff -u -r1.38 -r1.39 --- Compiler.java 28 Nov 2002 04:18:08 -0000 1.38 +++ Compiler.java 4 Dec 2002 00:48:42 -0000 1.39 @@ -289,6 +289,10 @@ // Determine which custom tag needs to declare which scripting vars ScriptingVariabler.set(pageNodes, errDispatcher); + // Optimizations by Tag Plugins + TagPluginManager tagPluginManager = options.getTagPluginManager(); + tagPluginManager.apply(pageNodes); + // generate servlet .java file Generator.generate(writer, this, pageNodes); writer.close(); 1.43 +58 -3 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java Index: Node.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java,v retrieving revision 1.42 retrieving revision 1.43 diff -u -r1.42 -r1.43 --- Node.java 28 Nov 2002 04:18:08 -0000 1.42 +++ Node.java 4 Dec 2002 00:48:42 -0000 1.43 @@ -997,6 +997,14 @@ private Vector nestedScriptingVars; private Node.CustomTag customTagParent; private Integer numCount; + private boolean useTagPlugin; + /** + * The following two fields are use for holding the Java + * scriptlets that the tag plugins may generate. Meaningful + * only if useTagPlugin is true; + */ + private Nodes atSTag; + private Nodes atETag; public CustomTag(Attributes attrs, Mark start, String name, String prefix, String shortName, @@ -1213,6 +1221,30 @@ return result; } + + public void setUseTagPlugin(boolean use) { + useTagPlugin = use; + } + + public boolean getUseTagPlugin() { + return useTagPlugin; + } + + public void setAtSTag(Nodes sTag) { + atSTag = sTag; + } + + public Nodes getAtSTag() { + return atSTag; + } + + public void setAtETag(Nodes eTag) { + atETag = eTag; + } + + public Nodes getAtETag() { + return atETag; + } /* * Computes this custom tag's custom nesting level, which corresponds @@ -1250,6 +1282,25 @@ return n; } } + /** + * Represents a attribute value of a Custom tag. Used only by tag plugins + * to indicate generated codes for the specified attribute. + */ + public static class GenAttribute extends Node { + String attribute; + + public GenAttribute(Mark start, String attribute) { + super(start, null); + } + + public void accept(Visitor v) throws JasperException { + v.visit(this); + } + + public String getAttribute() { + return attribute; + } + } /** * Represents the body of a <jsp:text> element @@ -1742,6 +1793,10 @@ } public void visit(JspOutput n) throws JasperException { + doVisit(n); + } + + public void visit(GenAttribute n) throws JasperException { doVisit(n); } } 1.3 +67 -27 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/TagPluginManager.java Index: TagPluginManager.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/TagPluginManager.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- TagPluginManager.java 26 Nov 2002 01:25:29 -0000 1.2 +++ TagPluginManager.java 4 Dec 2002 00:48:42 -0000 1.3 @@ -63,12 +63,12 @@ import java.util.*; import java.io.*; +import javax.servlet.ServletContext; + import org.apache.jasper.JasperException; import org.apache.jasper.xmlparser.ParserUtils; import org.apache.jasper.xmlparser.TreeNode; -import org.apache.jasper.JspCompilationContext; import org.apache.jasper.compiler.tagplugin.TagPlugin; -import org.apache.jasper.compiler.tagplugin.TagPluginFactory; import org.apache.jasper.compiler.tagplugin.TagPluginContext; /** @@ -81,18 +81,31 @@ static private final String plugInsXml = "tagPlugins.xml"; private boolean initialized = false; private Hashtable tagPlugins = null; - private JspCompilationContext ctxt; - + private ServletContext ctxt; - TagPluginManager(JspCompilationContext ctxt) { + public TagPluginManager(ServletContext ctxt) { this.ctxt = ctxt; - init(); } + public void apply(Node.Nodes page) throws JasperException { + + init(); + if (tagPlugins == null || tagPlugins.size() == 0) { + return; + } + + page.visit(new Node.Visitor() { + public void visit(Node.CustomTag n) { + invokePlugin(n); + } + }); + } + private void init() { if (initialized) return; + initialized = true; InputStream is = ctxt.getResourceAsStream(plugInsXml); if (is == null) return; @@ -111,6 +124,7 @@ return; } + tagPlugins = new Hashtable(); Iterator pluginList = tagPluginsNode.findChildren("tag-plugin"); while (pluginList.hasNext()) { TreeNode pluginNode = (TreeNode) pluginList.next(); @@ -142,44 +156,70 @@ } } - public TagPlugin getPlugInClass(Node.CustomTag n, ServletWriter out) { - - if (tagPlugins == null) { - return null; - } - - TagPluginFactory tagPluginFactory = (TagPluginFactory) + /** + * Invoke tag plugin if it exists. The node n will be modified by + * the plugin if that applies + */ + private void invokePlugin(Node.CustomTag n) { + TagPlugin tagPlugin = (TagPlugin) tagPlugins.get(n.getTagHandlerClass().getName()); - if (tagPluginFactory == null) { - return null; + if (tagPlugin == null) { + return; } - TagPluginContext tagPluginContext = new TagPluginContextImpl(n, out); - return tagPluginFactory.createTagPlugin(n.getName(), tagPluginContext); + TagPluginContext tagPluginContext = new TagPluginContextImpl(n); + tagPlugin.doTag(tagPluginContext); } static class TagPluginContextImpl implements TagPluginContext { Node.CustomTag node; - ServletWriter out; + Node.Nodes curNodes; - TagPluginContextImpl(Node.CustomTag n, ServletWriter out) { + TagPluginContextImpl(Node.CustomTag n) { this.node = n; - this.out = out; + curNodes = new Node.Nodes(); + n.setAtETag(curNodes); + curNodes = new Node.Nodes(); + n.setAtSTag(curNodes); + n.setUseTagPlugin(true); } - public ServletWriter getServletWriter() { - return out; + public String getTagName() { + return node.getName(); } public boolean isScriptless() { return node.getChildInfo().isScriptless(); } - public String getAttributeValue(String attribute) { + public boolean isAttributeSpecified(String attribute) { + return getAttribute(attribute) != null; + } + + public void generateJavaSource(String s) { + curNodes.add(new Node.Scriptlet(node.getStart(), null)); + } + + public void generateAttribute(String attribute) { + curNodes.add(new Node.GenAttribute(node.getStart(), null)); + } + + public void dontUseTagPlugin() { + node.setUseTagPlugin(false); + } + + public void generateBody() { + // Since we'll generate the body anyway, this is really a nop, + // except for the fact that it let us put the Java sources the + // plugins produce in the correct order (w.r.t the body). + curNodes = node.getAtETag(); + } + + private Node.JspAttribute getAttribute(String attribute) { Node.JspAttribute[] attrs = node.getJspAttributes(); for (int i=0; i < attrs.length; i++) { if (attrs[i].getName().equals(attribute)) { -// return attrs[i].getProcessedValue(); + return attrs[i]; } } return null; 1.2 +5 -10 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/tagplugin/TagPlugin.java Index: TagPlugin.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/tagplugin/TagPlugin.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TagPlugin.java 23 Nov 2002 01:36:49 -0000 1.1 +++ TagPlugin.java 4 Dec 2002 00:48:43 -0000 1.2 @@ -70,13 +70,8 @@ public interface TagPlugin { /** - * Invoked to generate Java codes at the start of a custom tag. + * Invoked to generate codes a custom tag. */ - void atSTag(); - - /** - * Invoked to generate Java codes at the end of a custom tag. - */ - void atETag(); + void doTag(TagPluginContext c); } 1.2 +35 -10 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/tagplugin/TagPluginContext.java Index: TagPluginContext.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/tagplugin/TagPluginContext.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TagPluginContext.java 23 Nov 2002 01:36:49 -0000 1.1 +++ TagPluginContext.java 4 Dec 2002 00:48:43 -0000 1.2 @@ -65,25 +65,50 @@ /** * This interface allows the plugin author to query about the properties - * of the current tag, and to use Jasper resources, such as ServletWriter. + * of the current tag, and to use Jasper resources to generate more + * efficient implementation for the tag handler under some conditions. */ public interface TagPluginContext { /** + * @return The name for the current tag + */ + String getTagName(); + + /** * @return true if the body of the tag is scriptless. */ boolean isScriptless(); /** - * Get the evaluated value of the given attribute for the current tag - * @return null if the attribute is not specified, or a java expression - * that when evaluateed, represent the attribute value + * @param attribute Name of the attribute + * @return true if the attribute is specified in the tag + */ + boolean isAttributeSpecified(String attribute); + + /** + * Generate Java source codes + */ + void generateJavaSource(String s); + + /** + * Generate Attribute value of a attribute in the custom tag + * NOTE: Currently cannot handle attributes that are fragments. + * @param attribute The specified attribute + */ + void generateAttribute(String attribute); + + /* + * Generate codes for the body of the custom tag */ - String getAttributeValue(String attribute); + void generateBody(); /** - * Used for outputting Java fragments to the output stream. + * Abandon optimization for this tag handler, and instruct the + * Jaser to generate the tag handler calls, as usual. + * Should be invoked if errors are detected, or that the tag body is + * too compilicated for optimization. */ - ServletWriter getServletWriter(); + void dontUseTagPlugin(); }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>