FileDocCategorySizeDatePackage
JstlBaseTLV.javaAPI DocGlassfish v2 API12106Sat May 05 19:18:02 BST 2007org.apache.taglibs.standard.tlv

JstlBaseTLV

public abstract class JstlBaseTLV extends javax.servlet.jsp.tagext.TagLibraryValidator

A base class to support SAX-based validation in JSTL.

author
Shawn Bayern

Fields Summary
private final String
EXP_ATT_PARAM
protected static final String
VAR
protected static final String
SCOPE
protected static final String
PAGE_SCOPE
protected static final String
REQUEST_SCOPE
protected static final String
SESSION_SCOPE
protected static final String
APPLICATION_SCOPE
protected final String
JSP
private static final int
TYPE_UNDEFINED
protected static final int
TYPE_CORE
protected static final int
TYPE_FMT
protected static final int
TYPE_SQL
protected static final int
TYPE_XML
private int
tlvType
protected String
uri
protected String
prefix
protected Vector
messageVector
protected Map
config
protected boolean
failed
protected String
lastElementId
Constructors Summary
public JstlBaseTLV()

		// the last element we've seen

    //*********************************************************************
    // Constructor and lifecycle management

      
	super();
	init();
    
Methods Summary
private voidconfigure(java.lang.String info)

        // construct our configuration map
	config = new HashMap();

	// leave the map empty if we have nothing to configure
	if (info == null)
	    return;

	// separate parameter into space-separated tokens and store them
	StringTokenizer st = new StringTokenizer(info);
	while (st.hasMoreTokens()) {
	    String pair = st.nextToken();
	    StringTokenizer pairTokens = new StringTokenizer(pair, ":");
	    String element = pairTokens.nextToken();
	    String attribute = pairTokens.nextToken();
	    Object atts = config.get(element);
	    if (atts == null) {
	        atts = new HashSet();
	        config.put(element, atts);
	    }
	    ((Set) atts).add(attribute);
	}
    
protected voidfail(java.lang.String message)

        failed = true;
        messageVector.add(new ValidationMessage(lastElementId, message));
    
protected abstract org.xml.sax.helpers.DefaultHandlergetHandler()

protected java.lang.StringgetLocalPart(java.lang.String qname)

	int colon = qname.indexOf(":");
	if (colon == -1)
	    return qname;
	else
	    return qname.substring(colon + 1);
    
protected booleanhasAttribute(org.xml.sax.Attributes a, java.lang.String att)

        return (a.getValue(att) != null);
    
protected booleanhasDanglingScope(org.xml.sax.Attributes a)

	return (a.getValue(SCOPE) != null && a.getValue(VAR) == null);
    
protected booleanhasEmptyVar(org.xml.sax.Attributes a)

	if ("".equals(a.getValue(VAR)))
	    return true;
	return false;
    
protected booleanhasNoInvalidScope(org.xml.sax.Attributes a)

        String scope = a.getValue(SCOPE);

	if ((scope != null)
	    && !scope.equals(PAGE_SCOPE)
	    && !scope.equals(REQUEST_SCOPE)
	    && !scope.equals(SESSION_SCOPE)
	    && !scope.equals(APPLICATION_SCOPE))
	    return false;

        return true;
    
private voidinit()

	messageVector = null;
	prefix = null;
	config = null;
    
protected booleanisCoreTag(java.lang.String tagUri, java.lang.String tagLn, java.lang.String target)

	return isTag( TYPE_CORE, tagUri, tagLn, target );
    
protected booleanisFmtTag(java.lang.String tagUri, java.lang.String tagLn, java.lang.String target)

	return isTag( TYPE_FMT, tagUri, tagLn, target );
    
protected booleanisJspTag(java.lang.String tagUri, java.lang.String tagLn, java.lang.String target)

        return isTag(tagUri, tagLn, JSP, target);
    
protected booleanisSpecified(javax.servlet.jsp.tagext.TagData data, java.lang.String attributeName)

        return (data.getAttribute(attributeName) != null);
    
protected booleanisSqlTag(java.lang.String tagUri, java.lang.String tagLn, java.lang.String target)

	return isTag( TYPE_SQL, tagUri, tagLn, target );
    
protected booleanisTag(java.lang.String tagUri, java.lang.String tagLn, java.lang.String matchUri, java.lang.String matchLn)

	if (tagUri == null
                || tagUri.length() == 0
	        || tagLn == null
		|| matchUri == null
		|| matchLn == null)
	    return false;
        // match beginning of URI since some suffix *_rt tags can
        // be nested in EL enabled tags as defined by the spec
        if (tagUri.length() > matchUri.length()) {
            return (tagUri.startsWith(matchUri) && tagLn.equals(matchLn));
        } else {
            return (matchUri.startsWith(tagUri) && tagLn.equals(matchLn));
        }
    
private booleanisTag(int type, java.lang.String tagUri, java.lang.String tagLn, java.lang.String target)

        return ( this.tlvType == type && isTag(tagUri, tagLn, this.uri, target) );
    
protected booleanisXmlTag(java.lang.String tagUri, java.lang.String tagLn, java.lang.String target)

	return isTag( TYPE_XML, tagUri, tagLn, target );
    
public voidrelease()

	super.release();
	init();
    
public synchronized javax.servlet.jsp.tagext.ValidationMessage[]validate(int type, java.lang.String prefix, java.lang.String uri, javax.servlet.jsp.tagext.PageData page)

	try {
	    this.tlvType = type;
	    this.uri = uri;
	    // initialize
	    messageVector = new Vector();

	    // save the prefix
	    this.prefix = prefix;

	    // parse parameters if necessary
	    try {
		if (config == null)
		    configure((String) getInitParameters().get(EXP_ATT_PARAM));
	    } catch (NoSuchElementException ex) {
		// parsing error
	        return vmFromString(
		    Resources.getMessage("TLV_PARAMETER_ERROR",
			EXP_ATT_PARAM));
	    }

	    // get a handler
	    DefaultHandler h = getHandler();

	    // parse the page
	    SAXParserFactory f = SAXParserFactory.newInstance();
	    f.setValidating(false);
	    f.setNamespaceAware(true);
	    SAXParser p = f.newSAXParser();
	    p.parse(page.getInputStream(), h);

	    if (messageVector.size() == 0)
		return null;
	    else
		return vmFromVector(messageVector);

	} catch (SAXException ex) {
	    return vmFromString(ex.toString());
	} catch (ParserConfigurationException ex) {
	    return vmFromString(ex.toString());
	} catch (IOException ex) {
	    return vmFromString(ex.toString());
	}
    
protected java.lang.StringvalidateExpression(java.lang.String elem, java.lang.String att, java.lang.String expr)


	// let's just use the cache kept by the ExpressionEvaluatorManager
	ExpressionEvaluator current;
	try {
	    current =
	        ExpressionEvaluatorManager.getEvaluatorByName(
                  ExpressionEvaluatorManager.EVALUATOR_CLASS);
	} catch (JspException ex) {
	    // (using JspException here feels ugly, but it's what EEM uses)
	    return ex.getMessage();
	}
	
	String response = current.validate(att, expr);
	if (response == null)
	    return response;
	else
	    return "tag = '" + elem + "' / attribute = '" + att + "': "
		+ response;
    
static javax.servlet.jsp.tagext.ValidationMessage[]vmFromString(java.lang.String message)

	return new ValidationMessage[] {
	    new ValidationMessage(null, message)
	};
    
static javax.servlet.jsp.tagext.ValidationMessage[]vmFromVector(java.util.Vector v)

	ValidationMessage[] vm = new ValidationMessage[v.size()];
	for (int i = 0; i < vm.length; i++)
	   vm[i] = (ValidationMessage) v.get(i);
	return vm;