Return-Path: Delivered-To: apmail-jakarta-taglibs-dev-archive@apache.org Received: (qmail 70802 invoked from network); 21 Nov 2001 11:08:18 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 21 Nov 2001 11:08:18 -0000 Received: (qmail 2220 invoked by uid 97); 21 Nov 2001 11:07:28 -0000 Delivered-To: qmlist-jakarta-archive-taglibs-dev@jakarta.apache.org Received: (qmail 2173 invoked by uid 97); 21 Nov 2001 11:07:27 -0000 Mailing-List: contact taglibs-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Tag Libraries Developers List" Reply-To: "Tag Libraries Developers List" Delivered-To: mailing list taglibs-dev@jakarta.apache.org Received: (qmail 9980 invoked by uid 97); 21 Nov 2001 03:29:29 -0000 Date: 21 Nov 2001 03:14:24 -0000 Message-ID: <20011121031424.7196.qmail@icarus.apache.org> From: shawn@apache.org To: jakarta-taglibs-cvs@apache.org Subject: cvs commit: jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/tlv JsptlBaseTLV.java JsptlXmlTLV.java JsptlCoreTLV.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N shawn 01/11/20 19:14:24 Modified: jsptl/conf x.tld jsptl/src/org/apache/taglibs/jsptl/resources Resources.properties jsptl/src/org/apache/taglibs/jsptl/tlv JsptlCoreTLV.java Added: jsptl/src/org/apache/taglibs/jsptl/tei XmlTransformTEI.java jsptl/src/org/apache/taglibs/jsptl/tlv JsptlBaseTLV.java JsptlXmlTLV.java Log: XML library validation. Revision Changes Path 1.5 +9 -18 jakarta-taglibs/jsptl/conf/x.tld Index: x.tld =================================================================== RCS file: /home/cvs/jakarta-taglibs/jsptl/conf/x.tld,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- x.tld 2001/11/21 02:12:32 1.4 +++ x.tld 2001/11/21 03:14:23 1.5 @@ -10,27 +10,20 @@ JSTL x JSTL 1.0 XML library - - choose @@ -207,6 +197,7 @@ transform org.apache.taglibs.jsptl.tag.jx.XmlTransformTag + org.apache.taglibs.jsptl.tei.XmlTransformTEI JSP Conducts a transformation given a source XML document 1.7 +5 -5 jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/resources/Resources.properties Index: Resources.properties =================================================================== RCS file: /home/cvs/jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/resources/Resources.properties,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- Resources.properties 2001/11/21 01:22:59 1.6 +++ Resources.properties 2001/11/21 03:14:23 1.7 @@ -132,7 +132,7 @@ # Generic errors TLV_ILLEGAL_BODY=\ - Encountered illegal body of <{0}:{1}> tag, given its attributes. + Encountered illegal body of <{0}> tag, given its attributes. TLV_MISSING_BODY=\ A body is necessary inside the <{0}> tag, given its attributes. @@ -146,8 +146,8 @@ # Errors customized to particular tags (sort of) :-) -TLV_ILLEGAL_CHOOSE_ORDER=\ - Illegal <{0}gt; after <{1}:{2}> in <{1}:{3}>. +TLV_ILLEGAL_ORDER=\ + Illegal <{0}> after <{1}:{2}> in <{1}:{3}>. -TLV_ILLEGAL_IMPORT_PARAM=\ - Illegal <{0}:{1}> tag within <{0}:{2} {3}="..."gt;. +TLV_ILLEGAL_PARAM=\ + Illegal <{0}:{1}> tag within <{0}:{2} {3}="...">. 1.1 jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/tei/XmlTransformTEI.java Index: XmlTransformTEI.java =================================================================== /* * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * */ package org.apache.taglibs.jsptl.tei; import javax.servlet.jsp.tagext.*; /** *

An implementation of TagExtraInfo that implements validation for * ForEachTag's attributes

* * @author Shawn Bayern */ public class XmlTransformTEI extends TagExtraInfo { final private static String RESULT = "result"; final private static String TRANSFORMER = "transformer"; final private static String VAR = "var"; final private static String XSLT = "xslt"; /* * Currently implements the following rules: * * - If 'items' is not specified, 'begin' and 'end' must be */ public boolean isValid(TagData us) { // disallow both XSLT and TRANSFORMER if (Util.isSpecified(us, XSLT) && Util.isSpecified(us, TRANSFORMER)) return false; // disallow both VAR and RESULT if (Util.isSpecified(us, VAR) && Util.isSpecified(us, RESULT)) return false; return true; } } 1.8 +12 -169 jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/tlv/JsptlCoreTLV.java Index: JsptlCoreTLV.java =================================================================== RCS file: /home/cvs/jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/tlv/JsptlCoreTLV.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- JsptlCoreTLV.java 2001/11/19 04:18:07 1.7 +++ JsptlCoreTLV.java 2001/11/21 03:14:23 1.8 @@ -67,8 +67,8 @@ import org.apache.taglibs.jsptl.resources.Resources; /** - *

A SAX-based TagLibraryValidator for the 'jx' version of JSPTL. - * Current implements the following checks:

+ *

A SAX-based TagLibraryValidator for the core JSTL tag library. + * Currently implements the following checks:

* *
    *
  • Expression syntax validation, with full support for @@ -79,11 +79,12 @@ * 'value' is specified; it *must* have a body otherwise.) For * these purposes, "having a body" refers to non-whitespace * content inside the tag.
  • + *
  • Other minor constraints.
  • *
* * @author Shawn Bayern */ -public class JsptlCoreTLV extends TagLibraryValidator { +public class JsptlCoreTLV extends JsptlBaseTLV { //********************************************************************* // Implementation Overview @@ -124,36 +125,15 @@ private final String DEFAULT = "default"; private final String VAR_READER = "varReader"; - // parameter names - private final String EXP_ATT_PARAM = "expressionAttributes"; //********************************************************************* - // Validation and configuration state (private) + // Contract fulfillment - private String prefix; // our taglib's prefix - private Vector messageVector; // temporary error messages - private Map config; // configuration (Map of Sets) - - //********************************************************************* - // Constructor and lifecycle management - - public JsptlCoreTLV() { - super(); - init(); + protected DefaultHandler getHandler() { + return new Handler(); } - private void init() { - messageVector = null; - prefix = null; - config = null; - } - public void release() { - super.release(); - init(); - } - - //********************************************************************* // SAX event handler @@ -168,8 +148,6 @@ private Stack importWithReaderDepths = new Stack(); private Stack importWithoutReaderDepths = new Stack(); private String lastElementName = null; - private String lastElementId = null; - private boolean failed = false; private boolean bodyNecessary = false; private boolean bodyIllegal = false; private boolean lastImportHadReader = false; @@ -192,8 +170,7 @@ // check body-related constraint if (bodyIllegal) - fail(Resources.getMessage("TLV_ILLEGAL_BODY", - prefix, lastElementName)); + fail(Resources.getMessage("TLV_ILLEGAL_BODY", lastElementName)); // temporarily "install" new expression language if appropriate if (isTag(qn, EXPLANG)) @@ -231,7 +208,7 @@ // make sure is the last tag if (((Boolean) chooseHasOtherwise.peek()).booleanValue()) { - fail(Resources.getMessage("TLV_ILLEGAL_CHOOSE_ORDER", + fail(Resources.getMessage("TLV_ILLEGAL_ORDER", qn, prefix, OTHERWISE, CHOOSE)); } if (isTag(qn, OTHERWISE)) { @@ -246,7 +223,7 @@ // we're in an tag, where // tags are illegal if (isTag(qn, PARAM)) { - fail(Resources.getMessage("TLV_ILLEGAL_IMPORT_PARAM", + fail(Resources.getMessage("TLV_ILLEGAL_PARAM", prefix, PARAM, IMPORT, VAR_READER)); } } @@ -312,14 +289,13 @@ // check and update body-related constraints if (bodyIllegal) - fail(Resources.getMessage("TLV_ILLEGAL_BODY", - prefix, lastElementName)); + fail(Resources.getMessage("TLV_ILLEGAL_BODY", lastElementName)); bodyNecessary = false; // body is no longer necessary! if (!importWithoutReaderDepths.empty()) { // we're in an without a Reader; nothing but // is allowed fail(Resources.getMessage("TLV_ILLEGAL_BODY", - prefix, IMPORT)); + prefix + ":" + IMPORT)); } // make sure has no non-whitespace text @@ -366,144 +342,11 @@ depth--; } - // utility method to help us match elements in our tagset - private boolean isTag(String qn, String unqualifiedName) { - return (qn.equals(prefix + ":" + unqualifiedName)); - } - - // utility method to determine if an attribute exists - private boolean hasAttribute(Attributes a, String att) { - return (a.getValue(att) != null); - } - // are we directly under a ? private boolean chooseChild() { return (!chooseDepths.empty() && (depth - 1) == ((Integer) chooseDepths.peek()).intValue()); } - /* - * method to assist with failure [ as if it's not easy enough - * already :-) ] - */ - private void fail(String message) { - failed = true; - JsptlCoreTLV.this.messageVector.add( - new ValidationMessage(lastElementId, message)); - } - - } - - //********************************************************************* - // Validation entry point - - public synchronized ValidationMessage[] validate( - String prefix, String uri, PageData page) { - try { - - // 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 - Handler h = new Handler(); - - // parse the page - SAXParserFactory f = SAXParserFactory.newInstance(); - f.setValidating(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()); - } - } - - - //********************************************************************* - // Private utility functions - - // parses our configuration parameter for element:attribute pairs - private void configure(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); - } - } - - // delegate validation to the appropriate expression language - private String validateExpression(String evaluator, - String elem, String att, String expr) { - - // let's just use the cache kept by the ExpressionEvaluatorManager - ExpressionEvaluator current; - try { - current = - ExpressionEvaluatorManager.getEvaluatorByName(evaluator); - } 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 "<" + elem + "> / attribute = '" + att + "': " - + response; - } - - // constructs a ValidationMessage[] from a single String and no ID - static ValidationMessage[] vmFromString(String message) { - return new ValidationMessage[] { - new ValidationMessage(null, message) - }; - } - - // constructs a ValidationMessage[] from a ValidationMessage Vector - static ValidationMessage[] vmFromVector(Vector v) { - ValidationMessage[] vm = new ValidationMessage[v.size()]; - for (int i = 0; i < vm.length; i++) - vm[i] = (ValidationMessage) v.get(i); - return vm; } } 1.1 jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/tlv/JsptlBaseTLV.java Index: JsptlBaseTLV.java =================================================================== /* * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * */ package org.apache.taglibs.jsptl.tlv; import java.io.*; import java.util.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import org.apache.taglibs.jsptl.lang.support.ExpressionEvaluator; import org.apache.taglibs.jsptl.lang.support.ExpressionEvaluatorManager; import org.apache.taglibs.jsptl.resources.Resources; /** *

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

* * @author Shawn Bayern */ public abstract class JsptlBaseTLV extends TagLibraryValidator { //********************************************************************* // Implementation Overview /* * We essentially just run the page through a SAX parser, handling * the callbacks that interest us. The SAX parser is supplied by * subclasses using the protected getHandler() method. */ protected abstract DefaultHandler getHandler(); //********************************************************************* // Constants // parameter names private final String EXP_ATT_PARAM = "expressionAttributes"; //********************************************************************* // Validation and configuration state (protected) protected String prefix; // our taglib's prefix protected Vector messageVector; // temporary error messages protected Map config; // configuration (Map of Sets) protected boolean failed; // have we failed >0 times? protected String lastElementId; // the last element we've seen //********************************************************************* // Constructor and lifecycle management public JsptlBaseTLV() { super(); init(); } private void init() { messageVector = null; prefix = null; config = null; } public void release() { super.release(); init(); } //********************************************************************* // Validation entry point public synchronized ValidationMessage[] validate( String prefix, String uri, PageData page) { try { // 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(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 utility functions // delegate validation to the appropriate expression language protected String validateExpression(String evaluator, String elem, String att, String expr) { // let's just use the cache kept by the ExpressionEvaluatorManager ExpressionEvaluator current; try { current = ExpressionEvaluatorManager.getEvaluatorByName(evaluator); } 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 "<" + elem + "> / attribute = '" + att + "': " + response; } // utility method to help us match elements in our tagset protected boolean isTag(String qn, String unqualifiedName) { return (qn.equals(prefix + ":" + unqualifiedName)); } // utility method to determine if an attribute exists protected boolean hasAttribute(Attributes a, String att) { return (a.getValue(att) != null); } /* * method to assist with failure [ as if it's not easy enough * already :-) ] */ protected void fail(String message) { failed = true; messageVector.add(new ValidationMessage(lastElementId, message)); } //********************************************************************* // Private utility functions // parses our configuration parameter for element:attribute pairs private void configure(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); } } // constructs a ValidationMessage[] from a single String and no ID static ValidationMessage[] vmFromString(String message) { return new ValidationMessage[] { new ValidationMessage(null, message) }; } // constructs a ValidationMessage[] from a ValidationMessage Vector static ValidationMessage[] vmFromVector(Vector v) { ValidationMessage[] vm = new ValidationMessage[v.size()]; for (int i = 0; i < vm.length; i++) vm[i] = (ValidationMessage) v.get(i); return vm; } } 1.1 jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/tlv/JsptlXmlTLV.java Index: JsptlXmlTLV.java =================================================================== /* * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . * */ package org.apache.taglibs.jsptl.tlv; import java.io.*; import java.util.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import org.apache.taglibs.jsptl.lang.support.ExpressionEvaluator; import org.apache.taglibs.jsptl.lang.support.ExpressionEvaluatorManager; import org.apache.taglibs.jsptl.resources.Resources; /** *

A SAX-based TagLibraryValidator for the JSTL XML library. * Currently implements the following checks:

* *
    *
  • Expression syntax validation, with full support for * <jx:expressionLanguage>
  • *
  • Choose / when / otherwise constraints
  • *
  • Tag bodies that must either be empty or non-empty given * particular attributes.
  • *
  • Other minor constraints.
  • *
* * @author Shawn Bayern */ public class JsptlXmlTLV extends JsptlBaseTLV { //********************************************************************* // Implementation Overview /* * We essentially just run the page through a SAX parser, handling * the callbacks that interest us. We collapse elements * into the text they contain, since this simplifies processing * somewhat. Even a quick glance at the implementation shows its * necessary, tree-oriented nature: multiple Stacks, an understanding * of 'depth', and so on all are important as we recover necessary * state upon each callback. This TLV demonstrates various techniques, * from the general "how do I use a SAX parser for a TLV?" to * "how do I read my init parameters and then validate?" But also, * the specific SAX methodology was kept as general as possible to * allow for experimentation and flexibility. * * Much of the code and structure is duplicated from JsptlCoreTLV. * An effort has been made to re-use code where unambiguously useful. * However, splitting logic among parent/child classes isn't the * cleanest approach when writing a parser like the one we need. */ //********************************************************************* // Constants // tag names private final String CHOOSE = "choose"; private final String WHEN = "when"; private final String OTHERWISE = "otherwise"; private final String PARSE = "parse"; private final String PARAM = "param"; private final String TRANSFORM = "transform"; private final String EXPLANG = "expressionLanguage"; private final String JSP_TEXT = "jsp:text"; // attribute names private final String EVAL = "evaluator"; private final String VALUE = "value"; private final String SOURCE = "source"; //********************************************************************* // Contract fulfillment protected DefaultHandler getHandler() { return new Handler(); } //********************************************************************* // SAX event handler /** The handler that provides the base of our implementation. */ private class Handler extends DefaultHandler { // parser state private int depth = 0; private Stack chooseDepths = new Stack(); private Stack chooseHasOtherwise = new Stack(); private Stack expressionLanguage = new Stack(); private String lastElementName = null; private boolean bodyNecessary = false; private boolean bodyIllegal = false; public Handler() { // "install" the default evaluator String defaultEvaluator = JsptlCoreTLVHelper.getEvaluatorName(); if (defaultEvaluator != null) expressionLanguage.push(defaultEvaluator); } // process under the existing context (state), then modify it public void startElement( String ns, String ln, String qn, Attributes a) { // for simplicity, we can ignore for our purposes // (don't bother distinguishing between it and its characters) if (qn.equals(JSP_TEXT)) return; // check body-related constraint if (bodyIllegal) fail(Resources.getMessage("TLV_ILLEGAL_BODY", lastElementName)); // temporarily "install" new expression language if appropriate if (isTag(qn, EXPLANG)) expressionLanguage.push(a.getValue(EVAL)); // validate expression syntax if we need to Set expAtts; if (qn.startsWith(prefix + ":") && (expAtts = (Set) config.get(ln)) != null) { for (int i = 0; i < a.getLength(); i++) { String attName = a.getLocalName(i); if (expAtts.contains(attName)) { if (expressionLanguage.empty()) fail("Unexpected failure to determine " + "expression language."); String vMsg = validateExpression( (String) expressionLanguage.peek(), ln, attName, a.getValue(i)); if (vMsg != null) fail(vMsg); } } } // check invariants for if (chooseChild()) { // ensure has the right children if(!isTag(qn, WHEN) && !isTag(qn, OTHERWISE)) { fail(Resources.getMessage("TLV_ILLEGAL_CHILD_TAG", prefix, CHOOSE, qn)); } // make sure is the last tag if (((Boolean) chooseHasOtherwise.peek()).booleanValue()) { fail(Resources.getMessage("TLV_ILLEGAL_ORDER", qn, prefix, OTHERWISE, CHOOSE)); } if (isTag(qn, OTHERWISE)) { chooseHasOtherwise.pop(); chooseHasOtherwise.push(new Boolean(true)); } } // now, modify state // we're a choose, so record new choose-specific state if (isTag(qn, CHOOSE)) { chooseDepths.push(new Integer(depth)); chooseHasOtherwise.push(new Boolean(false)); } // set up a check against illegal attribute/body combinations bodyIllegal = false; bodyNecessary = false; if (isTag(qn, PARSE) || isTag(qn, TRANSFORM)) { if (hasAttribute(a, SOURCE)) bodyIllegal = true; } else if (isTag(qn, PARAM)) { if (hasAttribute(a, VALUE)) bodyIllegal = true; else bodyNecessary = true; } // record the most recent tag (for error reporting) lastElementName = qn; lastElementId = a.getValue("id"); // we're a new element, so increase depth depth++; } public void characters(char[] ch, int start, int length) { // ignore strings that are just whitespace String s = new String(ch, start, length).trim(); if (s.equals("")) return; // check and update body-related constraints if (bodyIllegal) fail(Resources.getMessage("TLV_ILLEGAL_BODY", lastElementName)); bodyNecessary = false; // body is no longer necessary! // make sure has no non-whitespace text if (chooseChild()) { String msg = Resources.getMessage("TLV_ILLEGAL_TEXT_BODY", prefix, CHOOSE, (s.length() < 7 ? s : s.substring(0,7))); fail(msg); } } public void endElement(String ns, String ln, String qn) { // consistently, we ignore JSP_TEXT if (qn.equals(JSP_TEXT)) return; // handle body-related invariant if (bodyNecessary) fail(Resources.getMessage("TLV_MISSING_BODY", lastElementName)); bodyIllegal = false; // reset: we've left the tag // update -related state if (isTag(qn, CHOOSE)) { chooseDepths.pop(); chooseHasOtherwise.pop(); } // update language state if (isTag(qn, EXPLANG)) expressionLanguage.pop(); // update our depth depth--; } // are we directly under a ? private boolean chooseChild() { return (!chooseDepths.empty() && (depth - 1) == ((Integer) chooseDepths.peek()).intValue()); } } } -- To unsubscribe, e-mail: For additional commands, e-mail: