You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by cr...@apache.org on 2001/02/16 23:17:11 UTC

cvs commit: jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/parser TreeNode.java

craigmcc    01/02/16 14:17:10

  Modified:    jasper/src/share/org/apache/jasper/compiler JspUtil.java
                        TagLibraryInfoImpl.java TldLocationsCache.java
               jasper/src/share/org/apache/jasper/parser TreeNode.java
  Log:
  Stage 2 of making Jasper able to load an XML parser from a different class
  loader.
  
  * Modified TagLibraryInfoImpl (which parses TLDs) to use the new
    parsing routines instead of the old ones in JspUtil.
  
  * Added findChild("name") method to TreeNode, to optimize the very
    common case where there is only one child with a given name.
  
  * Updated TldLocationsCache (which scans web.xml files) to use the
    new findChild() method.
  
  * Deprecated the JspUtils.parseXMLDoc() method since nobody is now
    using it.
  
  Before going on to Stage 3 (parsing JSP pages in XML syntax), it is
  necessary to go implement the special classloader stuff.  That's next.
  
  Revision  Changes    Path
  1.8       +7 -3      jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/JspUtil.java
  
  Index: JspUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/JspUtil.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- JspUtil.java	2000/11/30 21:47:55	1.7
  +++ JspUtil.java	2001/02/16 22:17:02	1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/JspUtil.java,v 1.7 2000/11/30 21:47:55 pierred Exp $
  - * $Revision: 1.7 $
  - * $Date: 2000/11/30 21:47:55 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/JspUtil.java,v 1.8 2001/02/16 22:17:02 craigmcc Exp $
  + * $Revision: 1.8 $
  + * $Date: 2001/02/16 22:17:02 $
    *
    * ====================================================================
    * 
  @@ -191,6 +191,8 @@
   
       /**
        * Parses the XML document contained in the InputStream.
  +     *
  +     * @deprecated Use ParserUtils.parseXMLDocument() instead
        */
       public static Document parseXMLDoc(String uri, InputStream in) 
   	throws JasperException 
  @@ -202,6 +204,8 @@
        * Parses the XML document contained in the InputStream.
        * This XML document is either web.xml or a tld.
        * [The TLD has to be cached internally (see MyEntityResolver)]
  +     *
  +     * @deprecated Use ParserUtils.parseXMLDocument() instead
        */
       public static Document parseXMLDocJaxp(String uri, InputStream in)
   	throws JasperException
  
  
  
  1.19      +149 -155  jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/TagLibraryInfoImpl.java
  
  Index: TagLibraryInfoImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/TagLibraryInfoImpl.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- TagLibraryInfoImpl.java	2001/02/04 01:05:46	1.18
  +++ TagLibraryInfoImpl.java	2001/02/16 22:17:04	1.19
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/TagLibraryInfoImpl.java,v 1.18 2001/02/04 01:05:46 glenn Exp $
  - * $Revision: 1.18 $
  - * $Date: 2001/02/04 01:05:46 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/TagLibraryInfoImpl.java,v 1.19 2001/02/16 22:17:04 craigmcc Exp $
  + * $Revision: 1.19 $
  + * $Date: 2001/02/16 22:17:04 $
    *
    * The Apache Software License, Version 1.1
    *
  @@ -62,6 +62,7 @@
   import java.net.URL;
   import java.net.MalformedURLException;
   import java.net.JarURLConnection;
  +import java.util.Iterator;
   import java.util.zip.ZipInputStream;
   import java.util.zip.ZipEntry;
   import java.util.jar.*;
  @@ -89,8 +90,8 @@
   import javax.servlet.jsp.tagext.VariableInfo;
   import javax.servlet.jsp.tagext.TagVariableInfo;
   
  -import org.w3c.dom.*;
  -import org.xml.sax.*;
  +// import org.w3c.dom.*;
  +// import org.xml.sax.*;
   
   import org.apache.jasper.JspCompilationContext;
   import org.apache.jasper.JasperException;
  @@ -98,6 +99,10 @@
   
   import org.apache.jasper.logging.Logger;
   
  +import org.apache.jasper.parser.ParserUtils;
  +import org.apache.jasper.parser.TreeNode;
  +
  +
   /**
    * Implementation of the TagLibraryInfo class from the JSP spec. 
    *
  @@ -109,8 +114,6 @@
       static private final String TAGLIB_TLD = "META-INF/taglib.tld";
       static private final String WEB_XML = "/WEB-INF/web.xml";
   
  -    Document tld;
  -
       Hashtable jarEntries;
   
       JspCompilationContext ctxt;
  @@ -263,45 +266,50 @@
       private void parseTLD(String uri, InputStream in) 
           throws JasperException
       {
  -	tld = JspUtil.parseXMLDoc(uri, in);
  +
           Vector tagVector = new Vector();
  -        NodeList list = tld.getElementsByTagName("taglib");
   
  -        if (list.getLength() != 1)
  -            throw new JasperException(
  -                Constants.getString("jsp.error.more.than.one.taglib",
  -                                    new Object[]{uri}));
  -
  -        Element elem = (Element) list.item(0);
  -        list = elem.getChildNodes();
  -
  -        for(int i = 0; i < list.getLength(); i++) {
  -	    Node tmp = list.item(i);
  -	    if (! (tmp instanceof Element)) continue;
  -            Element e = (Element) tmp;
  -            String tname = e.getTagName();
  -            if (tname.equals("tlibversion") || tname.equals("tlib-version")) {
  -		this.tlibversion = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("jspversion") || tname.equals("jsp-version")) {
  -		this.jspversion = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("shortname") || tname.equals("short-name")) {
  -		this.shortname = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("uri")) {
  -		this.urn = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("info")) {
  -		this.info = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("validator")) {
  -		this.tagLibraryValidator = createValidator(e);
  -            } else if (tname.equals("tag"))
  -                tagVector.addElement(createTagInfo(e));
  +        // Create an iterator over the child elements of our <taglib> element
  +        ParserUtils pu = new ParserUtils();
  +        TreeNode tld = pu.parseXMLDocument(uri, in);
  +        Iterator list = tld.findChildren();
  +
  +        // Process each child element of our <taglib> element
  +        while (list.hasNext()) {
  +
  +            TreeNode element = (TreeNode) list.next();
  +            String tname = element.getName();
  +
  +            if ("tlibversion".equals(tname) ||          // JSP 1.1
  +                "tlib-version".equals(tname))           // JSP 1.2
  +                this.tlibversion = element.getBody();
  +            else if ("jspversion".equals(tname) ||
  +                     "jsp-version".equals(tname))
  +                this.jspversion = element.getBody();
  +            else if ("shortname".equals(tname) ||
  +                     "short-name".equals(tname))
  +                this.shortname = element.getBody();
  +            else if ("uri".equals(tname))
  +                this.urn = element.getBody();
  +            else if ("info".equals(tname) ||
  +                     "description".equals(tname))
  +                this.info = element.getBody();
  +            else if ("validator".equals(tname))
  +                this.tagLibraryValidator = createValidator(element);
  +            else if ("tag".equals(tname))
  +                tagVector.addElement(createTagInfo(element));
  +            else if ("display-name".equals(tname) ||    // Ignored elements
  +                     "small-icon".equals(tname) ||
  +                     "large-icon".equals(tname) ||
  +                     "listener".equals(tname))
  +                ;
               else {
                   Constants.message("jsp.warning.unknown.element.in.TLD", 
  -                                  new Object[] {
  -                                      e.getTagName()
  -                                  },
  +                                  new Object[] {tname},
                                     Logger.WARNING
                                     );
  -	    }
  +            }
  +
           }
   
   
  @@ -309,7 +317,7 @@
           tagVector.copyInto (this.tags);
       }
   
  -    private TagInfo createTagInfo(Element elem) throws JasperException {
  +    private TagInfo createTagInfo(TreeNode elem) throws JasperException {
           String name = null;
   	String tagclass = null;
   	String teiclass = null;
  @@ -321,46 +329,41 @@
           
           Vector attributeVector = new Vector();
           Vector variableVector = new Vector();
  -        NodeList list = elem.getChildNodes();
  -        for(int i = 0; i < list.getLength(); i++) {
  -            Node tmp  =  list.item(i);
  -	    if (! (tmp instanceof Element)) continue;
  -	    Element e = (Element) tmp;
  -            String tname = e.getTagName();
  -            if (tname.equals("name")) {
  -		name = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("tagclass") || tname.equals("tag-class")) {
  -		tagclass = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("teiclass") || tname.equals("tei-class")) {
  -		teiclass = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("bodycontent") || tname.equals("body-content")) {
  -		bodycontent = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("info") || tname.equals("tlib-description")) {
  -		info = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("attribute")) {
  -                attributeVector.addElement(createAttribute(e));
  -
  -	    // JSP 1.2
  -
  -	    } else if (tname.equals("display-name")) {
  -		displayName = JspUtil.getElementChildTextData(e);
  -	    } else if (tname.equals("small-icon")) {
  -		smallIcon = JspUtil.getElementChildTextData(e);
  -	    } else if (tname.equals("large-icon")) {
  -		largeIcon = JspUtil.getElementChildTextData(e);
  -	    } else if (tname.equals("variable")) {
  -		if (teiclass != null) {
  -		    // teiclass comes first in the tag element
  -		    // only need to make the check here
  -		    throw new JasperException(
  -			Constants.getString("tld.error.variableNotAllowed"));
  -		}
  -		variableVector.addElement(createVariable(e));
  -	    } else {
  +        Iterator list = elem.findChildren();
  +        while (list.hasNext()) {
  +            TreeNode element = (TreeNode) list.next();
  +            String tname = element.getName();
  +
  +            if ("name".equals(tname))
  +                name = element.getBody();
  +            else if ("tagclass".equals(tname) ||
  +                     "tag-class".equals(tname))
  +                tagclass = element.getBody();
  +            else if ("teiclass".equals(tname) ||
  +                     "tei-class".equals(tname))
  +                teiclass = element.getBody();
  +            else if ("bodycontent".equals(tname) ||
  +                     "body-content".equals(tname))
  +                bodycontent = element.getBody();
  +            else if ("display-name".equals(tname))
  +                displayName = element.getBody();
  +            else if ("small-icon".equals(tname))
  +                smallIcon = element.getBody();
  +            else if ("large-icon".equals(tname))
  +                largeIcon = element.getBody();
  +            else if ("info".equals(tname) ||
  +                     "description".equals(tname))
  +                info = element.getBody();
  +            else if ("variable".equals(tname)) {
  +                if (teiclass != null)
  +                    throw new JasperException
  +                        (Constants.getString("tld.error.variableNotAllowed"));
  +                variableVector.addElement(createVariable(element));
  +            } else if ("attribute".equals(tname))
  +                attributeVector.addElement(createAttribute(element));
  +            else {
                   Constants.message("jsp.warning.unknown.element.in.tag", 
  -                                  new Object[] {
  -                                      e.getTagName()
  -                                  },
  +                                  new Object[] {tname},
                                     Logger.WARNING
                                     );
   	    }
  @@ -414,71 +417,66 @@
           return taginfo;
       }
   
  -    TagAttributeInfo createAttribute(Element elem) {
  +    TagAttributeInfo createAttribute(TreeNode elem) {
  +        //        p("createAttribute\n" + elem);
           String name = null;
           boolean required = false, rtexprvalue = false, reqTime = false;
           String type = null;
           
  -        NodeList list = elem.getChildNodes();
  -        for(int i = 0; i < list.getLength(); i++) {
  -            Node tmp  = list.item(i);
  -	    if (! (tmp instanceof Element)) continue;
  -	    Element e = (Element) tmp;
  -            String tname = e.getTagName();
  -            if (tname.equals("name"))  {
  -		name = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("required"))  {
  -                String s = JspUtil.getElementChildTextData(e);
  -                if (s != null) {
  -		    required = JspUtil.booleanValue(s);
  -		}
  -            } else if (tname.equals("rtexprvalue")) {
  -		String s = JspUtil.getElementChildTextData(e);
  -                if (s != null) {
  -		    rtexprvalue = JspUtil.booleanValue(s);
  -		}
  -            } else if (tname.equals("type")) {
  -		type = JspUtil.getElementChildTextData(e);
  -            } else 
  +        Iterator list = elem.findChildren();
  +        while (list.hasNext()) {
  +            TreeNode element = (TreeNode) list.next();
  +            String tname = element.getName();
  +
  +            if ("name".equals(tname))
  +                name = element.getBody();
  +            else if ("required".equals(tname)) {
  +                String s = element.getBody();
  +                if (s != null)
  +                    required = JspUtil.booleanValue(s);
  +            } else if ("rtexprvalue".equals(tname)) {
  +                String s = element.getBody();
  +                if (s != null)
  +                    rtexprvalue = JspUtil.booleanValue(s);
  +            } else if ("type".equals(tname))
  +                type = element.getBody();
  +            else {
                   Constants.message("jsp.warning.unknown.element.in.attribute", 
  -                                  new Object[] {
  -                                      e.getTagName()
  -                                  },
  +                                  new Object[] {tname},
                                     Logger.WARNING
                                     );
  +            }
           }
           
  -	//     return new TagAttributeInfo(name, required, rtexprvalue, type);
  +        // p("name=" + name + ", required=" + required + ", type=" + type +
  +        //   ", rtexprvalue=" + rtexprvalue);
           return new TagAttributeInfo(name, required, type, rtexprvalue);
       }
   
  -    TagVariableInfo createVariable(Element elem) {
  +    TagVariableInfo createVariable(TreeNode elem) {
           String nameGiven = null;
           String nameFromAttribute = null;
   	String className = null;
   	boolean declare = true;
   	int scope = VariableInfo.NESTED;
           
  -        NodeList list = elem.getChildNodes();
  -        for(int i=0; i<list.getLength(); i++) {
  -            Node tmp  = list.item(i);
  -	    if (!(tmp instanceof Element)) continue;
  -	    Element e = (Element) tmp;
  -            String tname = e.getTagName();
  -            if (tname.equals("name-given"))  {
  -		nameGiven = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("name-from-attribute"))  {
  -		nameFromAttribute = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("variable-class"))  {
  -		className = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("declare"))  {
  -                String s = JspUtil.getElementChildTextData(e);
  +        Iterator list = elem.findChildren();
  +        while (list.hasNext()) {
  +            TreeNode element = (TreeNode) list.next();
  +            String tname = element.getName();
  +            if ("name-given".equals(tname))
  +                nameGiven = element.getBody();
  +            else if ("name-from-attribute".equals(tname))
  +                nameFromAttribute = element.getBody();
  +            else if ("variable-class".equals(tname))
  +                className = element.getBody();
  +            else if ("declare".equals(tname)) {
  +                String s = element.getBody();
  +                if (s != null)
  +                    declare = JspUtil.booleanValue(s);
  +            } else if ("scope".equals(tname)) {
  +                String s = element.getBody();
                   if (s != null) {
  -		    declare = JspUtil.booleanValue(s);
  -		}
  -            } else if (tname.equals("scope")) {
  -		String s = JspUtil.getElementChildTextData(e);
  -                if (s != null) {
   		    if ("NESTED".equals(s)) {
   			scope = VariableInfo.NESTED;
   		    } else if ("AT_BEGIN".equals(s)) {
  @@ -489,7 +487,7 @@
   		}
               } else {
                   Constants.message("jsp.warning.unknown.element.in.variable",
  -                                  new Object[] {e.getTagName()},
  +                                  new Object[] {tname},
                                     Logger.WARNING);
   	    }
           }
  @@ -497,24 +495,22 @@
   				   className, declare, scope);
       }
   
  -    private TagLibraryValidator createValidator(Element elem) {
  +    private TagLibraryValidator createValidator(TreeNode elem) {
           String validatorClass = null;
   	Map initParams = new Hashtable();
  -        
  -        NodeList list = elem.getChildNodes();
  -        for(int i=0; i<list.getLength(); i++) {
  -            Node tmp  = list.item(i);
  -	    if (!(tmp instanceof Element)) continue;
  -	    Element e = (Element) tmp;
  -            String tname = e.getTagName();
  -            if (tname.equals("validator-class"))  {
  -		validatorClass = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("init-param"))  {
  -		String[] initParam = createInitParam(e);
  +
  +        Iterator list = elem.findChildren();
  +        while (list.hasNext()) {
  +            TreeNode element = (TreeNode) list.next();
  +            String tname = element.getName();
  +            if ("validator-class".equals(tname))
  +                validatorClass = element.getBody();
  +            else if ("init-param".equals(tname)) {
  +		String[] initParam = createInitParam(element);
   		initParams.put(initParam[0], initParam[1]);
               } else {
                   Constants.message("jsp.warning.unknown.element.in.validator", //@@@ add in properties
  -                                  new Object[] {e.getTagName()},
  +                                  new Object[] {tname},
                                     Logger.WARNING);
   	    }
           }
  @@ -541,24 +537,22 @@
   	return tlv;
       }
   
  -    String[] createInitParam(Element elem) {
  +    String[] createInitParam(TreeNode elem) {
           String[] initParam = new String[2];
           
  -        NodeList list = elem.getChildNodes();
  -        for(int i=0; i<list.getLength(); i++) {
  -            Node tmp  = list.item(i);
  -	    if (!(tmp instanceof Element)) continue;
  -	    Element e = (Element) tmp;
  -            String tname = e.getTagName();
  -            if (tname.equals("param-name"))  {
  -		initParam[0] = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("param-value"))  {
  -		initParam[1] = JspUtil.getElementChildTextData(e);
  -            } else if (tname.equals("description"))  {
  -		// do nothing
  -            } else {
  +        Iterator list = elem.findChildren();
  +        while (list.hasNext()) {
  +            TreeNode element = (TreeNode) list.next();
  +            String tname = element.getName();
  +            if ("param-name".equals(tname))
  +                initParam[0] = element.getBody();
  +            else if ("param-value".equals(tname))
  +                initParam[1] = element.getBody();
  +            else if ("description".equals(tname))
  +                ; // Do nothing
  +            else {
                   Constants.message("jsp.warning.unknown.element.in.initParam", //@@@ properties
  -                                  new Object[] {e.getTagName()},
  +                                  new Object[] {tname},
                                     Logger.WARNING);
   	    }
           }
  
  
  
  1.5       +11 -16    jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/TldLocationsCache.java
  
  Index: TldLocationsCache.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/compiler/TldLocationsCache.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TldLocationsCache.java	2001/02/16 07:14:33	1.4
  +++ TldLocationsCache.java	2001/02/16 22:17:05	1.5
  @@ -163,19 +163,13 @@
               // Parse the next <taglib> element
               TreeNode taglib = (TreeNode) taglibs.next();
               String tagUri = null;
  -            Iterator uris = taglib.findChildren("taglib-uri");
  -            while (uris.hasNext()) {
  -                TreeNode uri = (TreeNode) uris.next();
  -                if (tagUri == null)
  -                    tagUri = uri.getBody();
  -            }
               String tagLoc = null;
  -            Iterator locs = taglib.findChildren("taglib-location");
  -            while (locs.hasNext()) {
  -                TreeNode loc = (TreeNode) locs.next();
  -                if (tagLoc == null)
  -                    tagLoc = loc.getBody();
  -            }
  +            TreeNode child = taglib.findChild("taglib-uri");
  +            if (child != null)
  +                tagUri = child.getBody();
  +            child = taglib.findChild("taglib-location");
  +            if (child != null)
  +                tagLoc = child.getBody();
   
               // Save this location if appropriate
               if (tagLoc == null)
  @@ -291,10 +285,11 @@
                   return null;
               }
               TreeNode taglib = (TreeNode) taglibs.next();
  -            Iterator uris = taglib.findChildren("uri");
  -            while (uris.hasNext()) {
  -                TreeNode uri = (TreeNode) uris.next();
  -                return uri.getBody();
  +            TreeNode uri = taglib.findChild("uri");
  +            if (uri != null) {
  +                String body = uri.getBody();
  +                if (body != null)
  +                    return body;
               }
           }
           return null; // No <uri> element is present
  
  
  
  1.2       +22 -1     jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/parser/TreeNode.java
  
  Index: TreeNode.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/jasper/src/share/org/apache/jasper/parser/TreeNode.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TreeNode.java	2001/02/16 07:14:34	1.1
  +++ TreeNode.java	2001/02/16 22:17:08	1.2
  @@ -72,7 +72,7 @@
    * synchronized.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2001/02/16 07:14:34 $
  + * @version $Revision: 1.2 $ $Date: 2001/02/16 22:17:08 $
    */
   
   public class TreeNode {
  @@ -203,6 +203,27 @@
               return (Collections.EMPTY_LIST.iterator());
           else
               return (attributes.keySet().iterator());
  +
  +    }
  +
  +
  +    /**
  +     * Return the first child node of this node with the specified name,
  +     * if there is one; otherwise, return <code>null</code>.
  +     *
  +     * @param name Name of the desired child element
  +     */
  +    public TreeNode findChild(String name) {
  +
  +        if (children == null)
  +            return (null);
  +        Iterator items = children.iterator();
  +        while (items.hasNext()) {
  +            TreeNode item = (TreeNode) items.next();
  +            if (name.equals(item.getName()))
  +                return (item);
  +        }
  +        return (null);
   
       }