alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *  Copyright 1999-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.jasper.compiler;

import java.io.CharArrayWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.jsp.tagext.TagInfo;
import javax.servlet.jsp.tagext.TagLibraryInfo;

import org.apache.jasper.Constants;
import org.apache.jasper.JasperException;
import org.apache.tomcat.util.log.Log;

/**
 * The class that parses the JSP input and calls the right methods on
 * the code generator backend. 
 *
 * @author Anil K. Vijendran
 * @author Rajiv Mordani
 */
public class Parser {
    /**
     * The input source we read from...
     */
    private JspReader reader;

    /**
     * The backend that is notified of constructs recognized in the input... 
     */
    private ParseEventListener listener;

    /*
     * Char buffer for HTML data
     */
    CharArrayWriter caw;

    /*
     * Marker for start and end of the tempate data.
     */
    Mark tmplStart;
    Mark tmplStop;

    /*
     * Name of the current file.
     * Useful to preserve the line number information in
     * case of an include.
     */
    String currentFile;

    public interface Action {
        void execute(Mark start, Mark stop) throws JasperException;
    }

    public Parser(JspReader reader, final ParseEventListener lnr) {
	this.reader = reader;
	this.listener = new DelegatingListener(lnr,
                                               new Action() {
                                                       public void execute(Mark start, Mark stop) 
                                                           throws JasperException 
                                                       {
                                                           Parser.this.flushCharData(start, stop);
                                                       }
                                                   });
	this.caw = new CharArrayWriter();
	this.currentFile = reader.mark().getFile();
    }

    static final Vector coreElements = new Vector();

    /*
     * JSP directives
     */
    static final class Directive implements CoreElement {
	private static final String OPEN_DIRECTIVE  = "<%@";
	private static final String CLOSE_DIRECTIVE = "%>";

	static final String[] directives = {
	  "page",
	  "include",
	  "taglib"
	};

	private static final JspUtil.ValidAttribute[] pageDvalidAttrs = {
	    new JspUtil.ValidAttribute ("language"),
	    new JspUtil.ValidAttribute ("extends"),
	    new JspUtil.ValidAttribute ("import"),
	    new JspUtil.ValidAttribute ("session"),
	    new JspUtil.ValidAttribute ("buffer"),
	    new JspUtil.ValidAttribute ("autoFlush"),
	    new JspUtil.ValidAttribute ("isThreadSafe"),
	    new JspUtil.ValidAttribute ("info"),
	    new JspUtil.ValidAttribute ("errorPage"),
	    new JspUtil.ValidAttribute ("isErrorPage"),
	    new JspUtil.ValidAttribute ("contentType")
	};

	private static final JspUtil.ValidAttribute[] includeDvalidAttrs = {
	    new JspUtil.ValidAttribute ("file", true)
	};

	private static final JspUtil.ValidAttribute[] tagDvalidAttrs = {
	    new JspUtil.ValidAttribute ("uri", true),
	    new JspUtil.ValidAttribute ("prefix", true)
	};

	public boolean accept(ParseEventListener listener, JspReader reader, 
			      Parser parser) throws JasperException
	{
	    String close;
	    String open;
	    
	    if (reader.matches(OPEN_DIRECTIVE)) {
		open = OPEN_DIRECTIVE;
		close = CLOSE_DIRECTIVE;
	    } else
		return false;

	    Mark start = reader.mark();
	    reader.advance(open.length());
	    reader.skipSpaces();
	    
	    // Check which directive it is.
	    String match = null;
	    for(int i = 0; i < directives.length; i++)
		if (reader.matches(directives[i])) {
		    match = directives[i];
		    break;
		}
	    if (match == null)
		throw new ParseException(reader.mark(),
					 Constants.getString("jsp.error.invalid.directive"));

	    reader.advance(match.length());

	    // Parse the attr-val pairs.
	    Hashtable attrs = reader.parseTagAttributes();
	    if (match.equals ("page"))
	        JspUtil.checkAttributes ("Page directive", attrs, 
					 pageDvalidAttrs, start);
	    else if (match.equals("include"))
	        JspUtil.checkAttributes ("Include directive", attrs, 
					 includeDvalidAttrs, start);
	    else if (match.equals("taglib"))
	        JspUtil.checkAttributes ("Taglib directive", attrs, 
					 tagDvalidAttrs, start);
	    
	    // Match close.
	    reader.skipSpaces();
	    if (!reader.matches(close))
                throw new ParseException(reader.mark(), 
                                         Constants.getString("jsp.error.unterminated", 
                                                             new Object[] { open }));
	    else
		reader.advance(close.length());

	    Mark stop = reader.mark();

	    listener.setTemplateInfo(parser.tmplStart, parser.tmplStop);
	    listener.handleDirective(match, start, stop, attrs);
	    return true;
	}

    }
  
    static {
	coreElements.addElement(new Directive());
    }

    /*
     * Include action
     */
    static final class Include implements CoreElement {
	private static final String OPEN_INCLUDE = "
     */

    // declarations
    static final class Comment implements CoreElement {

	private static final String OPEN_COMMENT  = "<%--";
	private static final String CLOSE_COMMENT = "--%>";

	public boolean accept(ParseEventListener listener, JspReader reader, Parser parser) 
	    throws JasperException 
	{

	    if (reader.matches(OPEN_COMMENT)) {
		reader.advance(OPEN_COMMENT.length());
		Mark start = reader.mark();
		Mark stop = reader.skipUntil(CLOSE_COMMENT);
		if (stop == null)
		    throw new ParseException(Constants.getString("jsp.error.unterminated", 
                                                                 new Object[] { OPEN_COMMENT }));

		parser.flushCharData(parser.tmplStart, parser.tmplStop);
		return true;
	    }
	    return false;
	}
    }
	
    static {
	coreElements.addElement(new Comment());
    }

    /*
     * Scripting elements
     */
    
    // declarations
    static final class Declaration implements CoreElement {

	private static final String OPEN_DECL  = "<%!";
	private static final String CLOSE_DECL = "%>";

        private static final JspUtil.ValidAttribute[] validAttributes = {
        };

	public boolean accept(ParseEventListener listener, JspReader reader, Parser parser) 
	    throws JasperException 
	{
	    String close, open, end_open = null;
            Hashtable attrs = null;
	    Mark start;
				
	    if (reader.matches(OPEN_DECL)) {
		open = OPEN_DECL;
		close = CLOSE_DECL;
	    } else
		return false;

	    reader.advance(open.length());
	    start = reader.mark();

            if (end_open != null) {
                attrs = reader.parseTagAttributes();

		reader.skipSpaces();
		if (!reader.matches(end_open)) 
		    throw new ParseException(reader.mark(),
			Constants.getString("jsp.error.unterminated"));
	        reader.advance(end_open.length());
		reader.skipSpaces();

		JspUtil.checkAttributes("Declaration", attrs, validAttributes, start);
            }

	    Mark stop = reader.skipUntil(close);
	    if (stop == null)
		throw new ParseException(Constants.getString("jsp.error.unterminated", 
                                                             new Object[] { open }));

	    listener.setTemplateInfo(parser.tmplStart, parser.tmplStop);	    
	    listener.handleDeclaration(start, stop, attrs);
	    return true;
	}
    }
	
    static {
	coreElements.addElement(new Declaration());
    }
    
    
    // expressions
    static final class Expression implements CoreElement {

	private static final String OPEN_EXPR  = "<%=";
	private static final String CLOSE_EXPR = "%>";

        private static final JspUtil.ValidAttribute[] validAttributes = {
        };

	public boolean accept(ParseEventListener listener, JspReader reader, Parser parser) 
	    throws JasperException
	{
	    String close, open, end_open=null;
            Hashtable attrs = null;
	    Mark start;
		
	    if (reader.matches(OPEN_EXPR)) {
		open = OPEN_EXPR;
		close = CLOSE_EXPR;
	    } else
		return false;

	    reader.advance(open.length());
	    start = reader.mark();

            if (end_open != null) {
                attrs = reader.parseTagAttributes();

		reader.skipSpaces();
		if (!reader.matches(end_open)) 
		    throw new ParseException(reader.mark(),
			Constants.getString("jsp.error.unterminated"));
	        reader.advance(end_open.length());
		reader.skipSpaces();

                JspUtil.checkAttributes("Expression", attrs, validAttributes, start);
            }

	    Mark stop = reader.skipUntil(close);
	    if (stop == null)
		throw new ParseException(reader.mark(), 
                                         Constants.getString("jsp.error.unterminated", 
                                                                 new Object[] { open }));
	    listener.setTemplateInfo(parser.tmplStart, parser.tmplStop);	    
	    listener.handleExpression(start, stop, attrs);
	    return true;
	}
    }

    static {
	coreElements.addElement(new Expression());
    }

    // scriptlets
    static final class Scriptlet implements CoreElement {

	private static final String OPEN_SCRIPTLET  = "<%";
	private static final String CLOSE_SCRIPTLET = "%>";

        private static final JspUtil.ValidAttribute[] validAttributes = {
        };

	public boolean accept(ParseEventListener listener, JspReader reader, Parser parser) 
	    throws JasperException
	{
	    String close, open, end_open = null;
            Hashtable attrs = null;
	    Mark start;
	    
	    if (reader.matches(OPEN_SCRIPTLET)) {
		open = OPEN_SCRIPTLET;
		close = CLOSE_SCRIPTLET;
	    } else
		return false;
		
	    reader.advance(open.length());
	    start = reader.mark();

            if (end_open != null) {
                attrs = reader.parseTagAttributes();

		reader.skipSpaces();
		if (!reader.matches(end_open)) 
		    throw new ParseException(reader.mark(),
			Constants.getString("jsp.error.unterminated"));
	        reader.advance(end_open.length());
		reader.skipSpaces();

                JspUtil.checkAttributes("Scriptlet", attrs, validAttributes, start);
            }

	    Mark stop = reader.skipUntil(close);
	    if (stop == null)
		throw new ParseException(reader.mark(), 
                                         Constants.getString("jsp.error.unterminated", 
                                                                 new Object[] { open }));
	    listener.setTemplateInfo(parser.tmplStart, parser.tmplStop);	    
	    listener.handleScriptlet(start, stop, attrs);
	    return true;
	}
    }

    static {
	coreElements.addElement(new Scriptlet());
    }

    /*
     * UseBean
     */
    static final class Bean implements CoreElement {

	private static final String OPEN_BEAN  = "";
                            // Parse until the end of the tag body. 
                            // Then skip the tag end... 
                            parser.parse(tagEnd);
                            try {
                                reader.advance(tagEnd.length());
                            } catch (ParseException ex) {
                                throw new ParseException(
                                    start,
                                    Constants.getString("jsp.error.unterminated.user.tag", 
                                        new Object[]{JspUtil.escapeXml(tagEnd)}));
			    }
			    listener.setTemplateInfo(parser.tmplStart, parser.tmplStop);
                            listener.handleTagEnd(parser.tmplStop, reader.mark(), prefix, 
                                                  shortTagName, attrs, tli, ti);
                        } else
                            throw new ParseException(start, 
                                                     "Internal Error: Invalid BODY_CONTENT type");
		} else 
		    throw new ParseException(start, 
					     "Unterminated user-defined tag");
	    }
            return true;
        }
    }

    static {
        coreElements.addElement(new Tag());
    }
    
    

    /*
     * Plugin
     */
    static final class Plugin implements CoreElement {
	private static final String OPEN_PLUGIN  = "
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.