tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Danno Ferrin" <DFER...@novell.com>
Subject PATCH: jakarta-tomcat/src/share/org/apache/jasper/compiler/TagLibraryInfoImpl.java
Date Fri, 12 Nov 1999 01:33:46 GMT
The XML Parser makes whitspace exposed as content data at the same level =
as elements, simple fix, just only look at elements

diff -w TagLibraryImpl.java TagLibraryInfoImpl.java.1.5
327,329c327
<             Object o =3D list.item(i);
<             if (o instanceof Element) {
<                 Element e =3D (Element) o;
---
>             Element e =3D (Element) list.item(i);
360d357
<             };
375,377c372
<             Object o =3D list.item(i);
<             if (o instanceof Element) {
<                   Element e =3D (Element) o;
---
>             Element e =3D (Element) list.item(i);
408d402
<             };
457,458d450
<             Object o =3D list.item(i);
<             if (o instanceof Element) {
488d479
<             };

- - - - Full File with whitespace indentations
FILE:jakarta-tomcat/src/share/org/apache/jasper/compiler/TagLibraryInfo.jav=
a
----
/*
 * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/compiler/T=
agLibraryInfoImpl.java,v 1.5 1999/10/21 02:47:51 mandar Exp $
 * $Revision: 1.5 $
 * $Date: 1999/10/21 02:47:51 $
 *
 * ====================================================================
 * 
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights=20
 * 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.=20
 *
 * 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: =20
 *       "This product includes software developed by the=20
 *        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=20
 *    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.
 * =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
 *
 * 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
 * <http://www.apache.org/>.
 *
 */=20

package org.apache.jasper.compiler;

import java.net.URL;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipEntry;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Vector;
import java.util.Hashtable;

import javax.servlet.jsp.tagext.TagLibraryInfo;
import javax.servlet.jsp.tagext.TagInfo;
import javax.servlet.jsp.tagext.TagAttributeInfo;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.*;
import org.xml.sax.*;
import com.sun.xml.tree.*;
import com.sun.xml.parser.*;

import org.apache.jasper.JspEngineContext;
import org.apache.jasper.JasperException;
import org.apache.jasper.Constants;
import org.apache.jasper.runtime.JspLoader;

/**
 * Implementation of the TagLibraryInfo class from the JSP spec.=20
 *
 * @author Anil K. Vijendran
 * @author Mandar Raje
 */
public class TagLibraryInfoImpl extends TagLibraryInfo {
    static private final String TLD =3D "META-INF/taglib.tld";
    static private final String WEBAPP_INF =3D "/WEB-INF/web.xml";

    XmlDocument tld;

    Hashtable jarEntries;
    Hashtable tagCaches =3D new Hashtable();
   =20
    JspEngineContext ctxt;

   =20

    private final void print(String name, String value, PrintWriter w) {
        if (value !=3D null) {
            w.print(name+" =3D {\n\t");
            w.print(value);
            w.print("\n}\n");
        }
    }

    public String toString() {
        StringWriter sw =3D new StringWriter();
        PrintWriter out =3D new PrintWriter(sw);
        print("tlibversion", tlibversion, out);
        print("jspversion", jspversion, out);
        print("shortname", shortname, out);
        print("urn", urn, out);
        print("info", info, out);

        for(int i =3D 0; i < tags.length; i++)
            out.println(tags[i].toString());
       =20
        return sw.toString();
    }
   =20
    TagLibraryInfoImpl(JspEngineContext ctxt, String prefix, String =
uriIn)=20
        throws IOException, JasperException
    {
	// XXX. should super be initialized with "dummy" uri?
        super(prefix, uriIn);

	this.ctxt =3D ctxt;
        ZipInputStream zin;
        InputStream in =3D null;
        URL url =3D null;
        boolean relativeURL =3D false;
	this.uri =3D uriIn;

	if (!uriIn.endsWith("jar")) {
	    // Parse web.xml.
	    InputStream is =3D ctxt.getServletContext().getResourceAsStream=
(WEBAPP_INF);
	   =20
	    if (is =3D=3D null) {
		throw new IOException(Constants.getString("jsp.error.webxml=
_not_found"));
	    }
	   =20
	    URL dtdURL =3D  this.getClass().getResource(Constants.WEBAPP_DT=
D_RESOURCE);
	    XmlDocument webtld =3D JspUtil.parseXMLDoc(is, dtdURL,
						     Constants.WEBAPP_DTD_P=
UBLIC_ID);
	    NodeList nList =3D  webtld.getElementsByTagName("taglib");
	   =20
	    // Check if a macthing "taglib" exists.
	    // XXX. Some changes that akv recommended.
	    if (nList.getLength() !=3D 0) {
		for(int i =3D 0; i < nList.getLength(); i++) {
		    Element e =3D (Element) nList.item(i);
		    NodeList nodeL =3D e.getChildNodes();
		    String tagLoc =3D null;
		    boolean match =3D false;
		    for(int j =3D 0; j < nodeL.getLength(); j++) {
			Element em =3D (Element) nodeL.item(j);
			String tname =3D em.getNodeName();
			if (tname.equals("taglib-location")) {
			    tagLoc =3D ((Text)em.getFirstChild()).getData()=
;
                            if (tagLoc !=3D null)
                                tagLoc =3D tagLoc.trim();
			}
			if (tname.equals("taglib-uri")) {
			    String tmpUri =3D  ((Text)em.getFirstChild()).g=
etData();
			    if (tmpUri !=3D null) {
                                tmpUri =3D tmpUri.trim();
                                if (tmpUri.equals(uriIn))                                
   match =3D true;
                            }
			}
		    }
		if (match =3D=3D true && tagLoc !=3D null) this.uri =3D =
tagLoc;
		}
	    }
	   =20
	    // "uri" should point to the correct tld location.

	    if (!uri.startsWith("/")) {
		HttpServletRequest request =3D ctxt.getRequest();
		String actURI =3D  request.getServletPath();
		String baseURI =3D actURI.substring(0, actURI.lastIndexOf('=
/'));
		uri =3D baseURI + '/' + uri;
		//url =3D new URL(uri);
		//in =3D url.openStream();
	    }
	    //else {
	    //relativeURL =3D true;
	    in =3D ctxt.getServletContext().getResourceAsStream(uri);
	    //}
	   =20
	    if (in =3D=3D null)
		throw new JasperException(Constants.getString("jsp.error.tl=
d_not_found",
							      new Object[] =
{TLD}));
	   =20
	    //Now parse the tld.
	    parseTLD(in);
	}
	   =20
	// FIXME Take this stuff out when taglib changes are thoroughly =
tested.
	if (uriIn.endsWith("jar")) {
	   =20
	    if (!uriIn.startsWith("/")) {
		url =3D new URL(uriIn);
		in =3D url.openStream();
	    } else {
		relativeURL =3D true;
		in =3D ctxt.getServletContext().getResourceAsStream(uriIn);=

	    }
	   =20
	    zin =3D new ZipInputStream(in);
	    this.jarEntries =3D new Hashtable();
	    this.ctxt =3D ctxt;
	   =20
	    // First copy this file into our work directory!=20
	    {
		File jspFile =3D new File(ctxt.getJspFile());
		String jarFileName =3D ctxt.getOutputDir()+File.separatorCh=
ar+
		    jspFile.getParent().toString();
		File jspDir =3D new File(jarFileName);
		jspDir.mkdirs();
	   =20
		if (relativeURL)
		    jarFileName =3D jarFileName+File.separatorChar+new =
File(uri).getName();
		else                   =20
		    jarFileName =3D jarFileName+File.separatorChar+
			new File(url.getFile()).getName();
	   =20
		Constants.message("jsp.message.copyinguri",=20
	                          new Object[] { uri, jarFileName },
				  Constants.MED_VERBOSITY);
	   =20
		if (relativeURL)
		    copy(ctxt.getServletContext().getResourceAsStream(uri),=

			 jarFileName);
		else
		    copy(url.openStream(), jarFileName);
	   =20
	        ctxt.getClassLoader().addJar(jarFileName);
	    }
	   =20
	   =20
	    boolean tldFound =3D false;
	    ZipEntry entry;
	    while ((entry =3D zin.getNextEntry()) !=3D null) {
		if (entry.getName().equals(TLD)) {
		    /*******
		     * This hack is necessary because XML reads until the =
end=20
		     * of an inputstream -- does not use available()
		     * -- and closes the inputstream when it can't
		     * read no more.
		     */
		   =20
		    // BEGIN HACK
		    ByteArrayOutputStream baos =3D new ByteArrayOutputStrea=
m();
		    int b;
		    while (zin.available() !=3D 0) {
			b =3D zin.read();
			if (b =3D=3D -1)
			    break;
			baos.write(b);
		    }

		    baos.close();
		    ByteArrayInputStream bais=20
			=3D new ByteArrayInputStream(baos.toByteArray());
		    // END HACK
		    tldFound =3D true;
		    parseTLD(bais);
		} else {
		    ByteArrayOutputStream baos =3D new ByteArrayOutputStrea=
m();
		    int b;
		    while (zin.available() !=3D 0) {
			b =3D zin.read();
			if (b =3D=3D -1)
			    break;
			baos.write(b);
		    }
		    baos.close();
		    jarEntries.put(entry.getName(), baos.toByteArray());
		}
		zin.closeEntry();
	    }
	   =20
	    if (!tldFound)
		throw new JasperException(Constants.getString("jsp.error.tl=
d_not_found",
							      new Object[] =
{
		    TLD
			}
							      ));
	} // Take this out (END of if(endsWith("jar")))
    }
   =20
       =20
    private void parseTLD(InputStream in)=20
        throws JasperException
    {
	URL tagUrl =3D this.getClass().getResource(Constants.TAGLIB_DTD_RES=
OURCE);
	tld =3D JspUtil.parseXMLDoc(in, tagUrl,
				  Constants.TAGLIB_DTD_PUBLIC_ID);
=09
        Vector tagVector =3D new Vector();
        NodeList list =3D tld.getElementsByTagName("taglib");

        if (list.getLength() !=3D 1)
            throw new JasperException(Constants.getString("jsp.error.more.t=
han.one.taglib"));

        Element elem =3D (Element) list.item(0);
        list =3D elem.getChildNodes();

        for(int i =3D 0; i < list.getLength(); i++) {
            Object o =3D list.item(i);
            if (o instanceof Element) {
                Element e =3D (Element) o;
                String tname =3D e.getTagName();
                if (tname.equals("tlibversion")) {
                    Text t =3D (Text) e.getFirstChild();
                    if (t !=3D null)
                        this.tlibversion =3D t.getData();
                } else if (tname.equals("jspversion")) {
                    Text t =3D (Text) e.getFirstChild();
                    if (t !=3D null)
                        this.jspversion =3D t.getData();
                } else if (tname.equals("shortname")) {
                    Text t =3D (Text) e.getFirstChild();
                    if (t !=3D null)
                        this.shortname =3D t.getData();
                } else if (tname.equals("urn")) {
                    Text t =3D (Text) e.getFirstChild();
                    if (t !=3D null)
                        this.urn =3D t.getData();
                } else if (tname.equals("info")) {
                    Text t =3D (Text) e.getFirstChild();
                    if (t !=3D null)
                        this.info =3D t.getData();
                } else if (tname.equals("tag"))
                    tagVector.addElement(createTagInfo(e));
                else=20
                    Constants.message("jsp.warning.unknown.element.in.TLD",=
=20
                                      new Object[] {
                                          e.getTagName()
                                      },
                                      Constants.WARNING
                                      );
            };
        }

        this.tags =3D new TagInfo[tagVector.size()];
        tagVector.copyInto (this.tags);
    }

    private TagInfo createTagInfo(Element elem) throws JasperException {
        String name =3D null, tagclass =3D null, teiclass =3D null;
        String bodycontent =3D "JSP"; // Default body content is JSP
	String info =3D null;
       =20
        Vector attributeVector =3D new Vector();
        NodeList list =3D elem.getChildNodes();
        for(int i =3D 0; i < list.getLength(); i++) {
            Object o =3D list.item(i);
            if (o instanceof Element) {
	            Element e =3D (Element) o;
	            String tname =3D e.getTagName();
	            if (tname.equals("name")) {
	                Text t =3D (Text) e.getFirstChild();
	                if (t !=3D null)
	                    name =3D t.getData();
	            } else if (tname.equals("tagclass")) {
	                Text t =3D (Text) e.getFirstChild();
	                if (t !=3D null)
	                    tagclass =3D t.getData();
	            } else if (tname.equals("teiclass")) {
	                Text t =3D (Text) e.getFirstChild();
	                if (t !=3D null)
	                    teiclass =3D t.getData();
	            } else if (tname.equals("bodycontent")) {
	                Text t =3D (Text) e.getFirstChild();
	                if (t !=3D null)
	                    bodycontent =3D t.getData();
	            } else if (tname.equals("info")) {
	                Text t =3D (Text) e.getFirstChild();
	                if (t !=3D null)
	                    info =3D t.getData();
	            } else if (tname.equals("attribute"))
	                attributeVector.addElement(createAttribute(e));
	            else=20
	                Constants.message("jsp.warning.unknown.element.in.t=
ag",=20
	                                  new Object[] {
	                                      e.getTagName()
	                                  },
	                                  Constants.WARNING
	                                  );
            };
        }
	TagAttributeInfo[] tagAttributeInfo=20
            =3D new TagAttributeInfo[attributeVector.size()];
	attributeVector.copyInto (tagAttributeInfo);

        TagExtraInfo tei =3D null;

        if (teiclass !=3D null && !teiclass.equals(""))
            try {
                Class teiClass =3D ctxt.getClassLoader().loadClass(teiclass=
);
                tei =3D (TagExtraInfo) teiClass.newInstance();
	    } catch (ClassNotFoundException cex) {
                Constants.message("jsp.warning.teiclass.is.null",
                                  new Object[] {
                                      teiclass, cex.getMessage()
                                  },
                                  Constants.WARNING
                                  );
            } catch (IllegalAccessException iae) {
                Constants.message("jsp.warning.teiclass.is.null",
                                  new Object[] {
                                      teiclass, iae.getMessage()
                                  },
                                  Constants.WARNING
                                  );
            } catch (InstantiationException ie) {
                Constants.message("jsp.warning.teiclass.is.null",
                                  new Object[] {
                                      teiclass, ie.getMessage()
                                  },
                                  Constants.WARNING
                                  );
            }
       =20
        TagInfo taginfo =3D new TagInfo(name, tagclass, bodycontent,
                                      info, this,=20
                                      tei,
                                      tagAttributeInfo);
        return taginfo;
    }

    TagAttributeInfo createAttribute(Element elem) {
        String name =3D null;
        boolean required =3D false, rtexprvalue =3D false, reqTime =3D =
false;
        String type =3D null;
       =20
        NodeList list =3D elem.getChildNodes();
        for(int i =3D 0; i < list.getLength(); i++) {
            Object o =3D list.item(i);
            if (o instanceof Element) {
	            Element e =3D (Element) list.item(i);
	            String tname =3D e.getTagName();
	            if (tname.equals("name"))  {
	                Text t =3D (Text) e.getFirstChild();
	                if (t !=3D null)
	                    name =3D t.getData();
	            } else if (tname.equals("required"))  {
	                Text t =3D (Text) e.getFirstChild();
	                if (t !=3D null)
	                    required =3D Boolean.valueOf(t.getData()).boole=
anValue();
	            } else if (tname.equals("rtexprvalue")) {
	                Text t =3D (Text) e.getFirstChild();
	                if (t !=3D null)
	                    rtexprvalue =3D Boolean.valueOf(t.getData()).bo=
oleanValue();
	            } else if (tname.equals("reqtime")) {
	                Text t =3D (Text) e.getFirstChild();
	                if (t !=3D null)
	                    reqTime =3D Boolean.valueOf(t.getData()).boolea=
nValue();
	            } else if (tname.equals("type")) {
	                Text t =3D (Text) e.getFirstChild();
	                if (t !=3D null)
	                    type =3D t.getData();
	            } else=20
	                Constants.message("jsp.warning.unknown.element.in.a=
ttribute",=20
	                                  new Object[] {
	                                      e.getTagName()
	                                  },
	                                  Constants.WARNING
	                                  );
            };
        }
       =20
        return new TagAttributeInfo(name, required, rtexprvalue, type,=20
                                    reqTime);
    }

    static void copy(InputStream in, String fileName)=20
        throws IOException, FileNotFoundException=20
    {
        byte[] buf =3D new byte[1024];

        FileOutputStream out =3D new FileOutputStream(fileName);
        int nRead;
        while ((nRead =3D in.read(buf, 0, buf.length)) !=3D -1)
            out.write(buf, 0, nRead);
    }

    TagCache getTagCache(String shortTagName) {
        return (TagCache) tagCaches.get(shortTagName);
    }

    void putTagCache(String shortTagName, TagCache tc) {
        tagCaches.put(shortTagName, tc);
    }
}

Mime
View raw message