001: /*
002: * Copyright (c) 1999 The Apache Software Foundation. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement:
019: * "This product includes software developed by the
020: * Apache Software Foundation (http://www.apache.org/)."
021: * Alternately, this acknowlegement may appear in the software itself,
022: * if and wherever such third-party acknowlegements normally appear.
023: *
024: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
025: * Foundation" must not be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact apache@apache.org.
028: *
029: * 5. Products derived from this software may not be called "Apache"
030: * nor may "Apache" appear in their names without prior written
031: * permission of the Apache Group.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Apache Software Foundation. For more
049: * information on the Apache Software Foundation, please see
050: * <http://www.apache.org/>.
051: *
052: */
053:
054: package com.sun.portal.providers.jsp.jasper3.jasper.compiler;
055:
056: import java.net.URL;
057: import java.util.zip.ZipInputStream;
058: import java.util.zip.ZipEntry;
059: import java.io.ByteArrayOutputStream;
060: import java.io.ByteArrayInputStream;
061: import java.io.FileInputStream;
062: import java.io.FileOutputStream;
063: import java.io.InputStream;
064: import java.io.PrintWriter;
065: import java.io.StringWriter;
066: import java.io.IOException;
067: import java.io.File;
068: import java.io.FileNotFoundException;
069: import java.util.Vector;
070: import java.util.Hashtable;
071:
072: import javax.servlet.jsp.tagext.TagLibraryInfo;
073: import javax.servlet.jsp.tagext.TagInfo;
074: import javax.servlet.jsp.tagext.TagAttributeInfo;
075: import javax.servlet.jsp.tagext.TagExtraInfo;
076:
077: import org.w3c.dom.*;
078: import org.xml.sax.*;
079:
080: import com.sun.portal.providers.jsp.jasper3.jasper.JspCompilationContext;
081: import com.sun.portal.providers.jsp.jasper3.jasper.JasperException;
082: import com.sun.portal.providers.jsp.jasper3.jasper.Constants; //import com.sun.portal.providers.jsp.jasper3.jasper.runtime.JspLoader;
083:
084: import com.sun.portal.providers.jsp.jasper3.tomcat.logging.Logger;
085:
086: /**
087: * Implementation of the TagLibraryInfo class from the JSP spec.
088: *
089: * @author Anil K. Vijendran
090: * @author Mandar Raje
091: */
092: public class TagLibraryInfoImpl extends TagLibraryInfo {
093: static private final String TLD = "META-INF/taglib.tld";
094: static private final String WEBAPP_INF = "/WEB-INF/web.xml";
095:
096: Document tld;
097:
098: Hashtable jarEntries;
099:
100: JspCompilationContext ctxt;
101:
102: private final void print(String name, String value, PrintWriter w) {
103: if (value != null) {
104: w.print(name + " = {\n\t");
105: w.print(value);
106: w.print("\n}\n");
107: }
108: }
109:
110: public String toString() {
111: StringWriter sw = new StringWriter();
112: PrintWriter out = new PrintWriter(sw);
113: print("tlibversion", tlibversion, out);
114: print("jspversion", jspversion, out);
115: print("shortname", shortname, out);
116: print("urn", urn, out);
117: print("info", info, out);
118: print("uri", uri, out);
119:
120: for (int i = 0; i < tags.length; i++)
121: out.println(tags[i].toString());
122:
123: return sw.toString();
124: }
125:
126: // XXX FIXME
127: // resolveRelativeUri and/or getResourceAsStream don't seem to properly
128: // handle relative paths when dealing when home and getDocBase are set
129: // the following is a workaround until these problems are resolved.
130: private InputStream getResourceAsStream(String uri)
131: throws FileNotFoundException {
132: if (uri.indexOf(":") > 0) {
133: // may be fully qualified (Windows) or may be a URL. Let
134: // getResourceAsStream deal with it.
135: return ctxt.getResourceAsStream(uri);
136: } else {
137: // assume it translates to a real file, and use getRealPath
138: String real = ctxt.getRealPath(uri);
139: return (real == null) ? null : new FileInputStream(real);
140: }
141: }
142:
143: public TagLibraryInfoImpl(JspCompilationContext ctxt,
144: String prefix, String uriIn) throws IOException,
145: JasperException {
146: super (prefix, uriIn);
147:
148: this .ctxt = ctxt;
149: ZipInputStream zin;
150: InputStream in = null;
151: URL url = null;
152: boolean relativeURL = false;
153: this .uri = uriIn;
154:
155: // Parse web.xml.
156: InputStream is = getResourceAsStream(WEBAPP_INF);
157:
158: if (is != null) {
159: Document webtld = JspUtil.parseXMLDoc(is,
160: Constants.WEBAPP_DTD_RESOURCE,
161: Constants.WEBAPP_DTD_PUBLIC_ID);
162: NodeList nList = webtld.getElementsByTagName("taglib");
163:
164: if (nList.getLength() != 0) {
165: for (int i = 0; i < nList.getLength(); i++) {
166: String tagLoc = null;
167: boolean match = false;
168: Element e = (Element) nList.item(i);
169:
170: // Assume only one entry for location and uri.
171: NodeList uriList = e
172: .getElementsByTagName("taglib-uri");
173: Element uriElem = (Element) uriList.item(0);
174: Text t = (Text) uriElem.getFirstChild();
175:
176: if (t != null) {
177: String tmpUri = t.getData();
178: if (tmpUri != null) {
179: tmpUri = tmpUri.trim();
180: if (tmpUri.equals(uriIn)) {
181: match = true;
182: NodeList locList = e
183: .getElementsByTagName("taglib-location");
184: Element locElem = (Element) locList
185: .item(0);
186: Text tl = (Text) locElem
187: .getFirstChild();
188: if (tl != null) {
189: tagLoc = tl.getData();
190: if (tagLoc != null)
191: tagLoc = tagLoc.trim();
192: }
193: }
194: }
195: }
196: if (match == true && tagLoc != null) {
197: this .uri = tagLoc;
198:
199: // If this is a relative path, then it has to be
200: // relative to where web.xml is.
201:
202: // I'm taking the simple way out. Since web.xml
203: // has to be directly under WEB-INF, I'm making
204: // an absolute URI out of it by prepending WEB-INF
205:
206: if (!uri.startsWith("/") && isRelativeURI(uri))
207: uri = "/WEB-INF/" + uri;
208: }
209: }
210: }
211: }
212:
213: // Try to resolve URI relative to the current JSP page
214: if (!uri.startsWith("/") && isRelativeURI(uri))
215: uri = ctxt.resolveRelativeUri(uri);
216:
217: if (!uri.endsWith("jar")) {
218: in = getResourceAsStream(uri);
219:
220: if (in == null)
221: throw new JasperException(Constants
222: .getString("jsp.error.tld_not_found",
223: new Object[] { uri }));
224: // Now parse the tld.
225: parseTLD(in);
226: }
227:
228: // FIXME Take this stuff out when taglib changes are thoroughly tested.
229: // 2000.11.15 commented out the 'copy to work dir' section,
230: // which I believe is what this FIXME comment referred to. (pierred)
231: if (uri.endsWith("jar")) {
232:
233: if (!isRelativeURI(uri)) {
234: url = new URL(uri);
235: in = url.openStream();
236: } else {
237: relativeURL = true;
238: in = getResourceAsStream(uri);
239: }
240:
241: zin = new ZipInputStream(in);
242: this .jarEntries = new Hashtable();
243: this .ctxt = ctxt;
244:
245: /* NOT COMPILED
246: // First copy this file into our work directory!
247: {
248: File jspFile = new File(ctxt.getJspFile());
249: String parent = jspFile.getParent();
250: String jarFileName = ctxt.getOutputDir();
251: if (parent != null) {
252: jarFileName = jarFileName + File.separatorChar +
253: parent;
254: }
255: File jspDir = new File(jarFileName);
256: jspDir.mkdirs();
257:
258: if (relativeURL)
259: jarFileName = jarFileName+File.separatorChar+new File(uri).getName();
260: else
261: jarFileName = jarFileName+File.separatorChar+
262: new File(url.getFile()).getName();
263:
264: Constants.message("jsp.message.copyinguri",
265: new Object[] { uri, jarFileName },
266: Logger.DEBUG);
267:
268: if (relativeURL)
269: copy(getResourceAsStream(uri),
270: jarFileName);
271: else
272: copy(url.openStream(), jarFileName);
273:
274: ctxt.addJar(jarFileName);
275: }
276: */// END NOT COMPILED
277: boolean tldFound = false;
278: ZipEntry entry;
279: while ((entry = zin.getNextEntry()) != null) {
280: if (entry.getName().equals(TLD)) {
281: /*******
282: * This hack is necessary because XML reads until the end
283: * of an inputstream -- does not use available()
284: * -- and closes the inputstream when it can't
285: * read no more.
286: */
287:
288: // BEGIN HACK
289: ByteArrayOutputStream baos = new ByteArrayOutputStream();
290: int b;
291: while (zin.available() != 0) {
292: b = zin.read();
293: if (b == -1)
294: break;
295: baos.write(b);
296: }
297:
298: baos.close();
299: ByteArrayInputStream bais = new ByteArrayInputStream(
300: baos.toByteArray());
301: // END HACK
302: tldFound = true;
303: parseTLD(bais);
304: } else {
305: ByteArrayOutputStream baos = new ByteArrayOutputStream();
306: int b;
307: while (zin.available() != 0) {
308: b = zin.read();
309: if (b == -1)
310: break;
311: baos.write(b);
312: }
313: baos.close();
314: jarEntries.put(entry.getName(), baos.toByteArray());
315: }
316: zin.closeEntry();
317: }
318:
319: if (!tldFound)
320: throw new JasperException(Constants
321: .getString("jsp.error.tld_not_found",
322: new Object[] { TLD }));
323: } // Take this out (END of if(endsWith("jar")))
324: }
325:
326: /** Returns true if the given URI is relative in this web application, false if it is an internet URI.
327: */
328: private boolean isRelativeURI(String uri) {
329: return (uri.indexOf(':') == -1);
330: }
331:
332: private void parseTLD(InputStream in) throws JasperException {
333: tld = JspUtil.parseXMLDoc(in, Constants.TAGLIB_DTD_RESOURCE,
334: Constants.TAGLIB_DTD_PUBLIC_ID);
335:
336: Vector tagVector = new Vector();
337: NodeList list = tld.getElementsByTagName("taglib");
338:
339: if (list.getLength() != 1)
340: throw new JasperException(Constants
341: .getString("jsp.error.more.than.one.taglib"));
342:
343: Element elem = (Element) list.item(0);
344: list = elem.getChildNodes();
345:
346: for (int i = 0; i < list.getLength(); i++) {
347: Node tmp = list.item(i);
348: if (!(tmp instanceof Element))
349: continue;
350: Element e = (Element) tmp;
351: String tname = e.getTagName();
352: if (tname.equals("tlibversion")) {
353: Text t = (Text) e.getFirstChild();
354: if (t != null)
355: this .tlibversion = t.getData().trim();
356: } else if (tname.equals("jspversion")) {
357: Text t = (Text) e.getFirstChild();
358: if (t != null)
359: this .jspversion = t.getData().trim();
360: } else if (tname.equals("shortname")) {
361: Text t = (Text) e.getFirstChild();
362: if (t != null)
363: this .shortname = t.getData().trim();
364: } else if (tname.equals("uri")) {
365: Text t = (Text) e.getFirstChild();
366: if (t != null)
367: this .urn = t.getData().trim();
368: } else if (tname.equals("info")) {
369: Text t = (Text) e.getFirstChild();
370: if (t != null)
371: this .info = t.getData().trim();
372: } else if (tname.equals("tag"))
373: tagVector.addElement(createTagInfo(e));
374: else
375: Constants
376: .message("jsp.warning.unknown.element.in.TLD",
377: new Object[] { e.getTagName() },
378: Logger.WARNING);
379: }
380:
381: this .tags = new TagInfo[tagVector.size()];
382: tagVector.copyInto(this .tags);
383: }
384:
385: private TagInfo createTagInfo(Element elem) throws JasperException {
386: String name = null, tagclass = null, teiclass = null;
387: String bodycontent = "JSP"; // Default body content is JSP
388: String info = null;
389:
390: Vector attributeVector = new Vector();
391: NodeList list = elem.getChildNodes();
392: for (int i = 0; i < list.getLength(); i++) {
393: Node tmp = list.item(i);
394: if (!(tmp instanceof Element))
395: continue;
396: Element e = (Element) tmp;
397: String tname = e.getTagName();
398: if (tname.equals("name")) {
399: Text t = (Text) e.getFirstChild();
400: if (t != null)
401: name = t.getData().trim();
402: } else if (tname.equals("tagclass")) {
403: Text t = (Text) e.getFirstChild();
404: if (t != null)
405: tagclass = t.getData().trim();
406: } else if (tname.equals("teiclass")) {
407: Text t = (Text) e.getFirstChild();
408: if (t != null)
409: teiclass = t.getData().trim();
410: } else if (tname.equals("bodycontent")) {
411: Text t = (Text) e.getFirstChild();
412: if (t != null)
413: bodycontent = t.getData().trim();
414: } else if (tname.equals("info")) {
415: Text t = (Text) e.getFirstChild();
416: if (t != null)
417: info = t.getData().trim();
418: } else if (tname.equals("attribute"))
419: attributeVector.addElement(createAttribute(e));
420: else
421: Constants
422: .message("jsp.warning.unknown.element.in.tag",
423: new Object[] { e.getTagName() },
424: Logger.WARNING);
425: }
426: TagAttributeInfo[] tagAttributeInfo = new TagAttributeInfo[attributeVector
427: .size()];
428: attributeVector.copyInto(tagAttributeInfo);
429:
430: TagExtraInfo tei = null;
431:
432: if (teiclass != null && !teiclass.equals(""))
433: try {
434: Class teiClass = ctxt.getClassLoader().loadClass(
435: teiclass);
436: tei = (TagExtraInfo) teiClass.newInstance();
437: } catch (ClassNotFoundException cex) {
438: Constants.message("jsp.warning.teiclass.is.null",
439: new Object[] { teiclass, cex.getMessage() },
440: Logger.WARNING);
441: } catch (IllegalAccessException iae) {
442: Constants.message("jsp.warning.teiclass.is.null",
443: new Object[] { teiclass, iae.getMessage() },
444: Logger.WARNING);
445: } catch (InstantiationException ie) {
446: Constants.message("jsp.warning.teiclass.is.null",
447: new Object[] { teiclass, ie.getMessage() },
448: Logger.WARNING);
449: }
450:
451: TagInfo taginfo = new TagInfo(name, tagclass, bodycontent,
452: info, this , tei, tagAttributeInfo);
453: return taginfo;
454: }
455:
456: TagAttributeInfo createAttribute(Element elem) {
457: String name = null;
458: boolean required = false, rtexprvalue = false, reqTime = false;
459: String type = null;
460:
461: NodeList list = elem.getChildNodes();
462: for (int i = 0; i < list.getLength(); i++) {
463: Node tmp = list.item(i);
464: if (!(tmp instanceof Element))
465: continue;
466: Element e = (Element) tmp;
467: String tname = e.getTagName();
468: if (tname.equals("name")) {
469: Text t = (Text) e.getFirstChild();
470: if (t != null)
471: name = t.getData().trim();
472: } else if (tname.equals("required")) {
473: Text t = (Text) e.getFirstChild();
474: if (t != null) {
475: required = Boolean.valueOf(t.getData().trim())
476: .booleanValue();
477: if (t.getData().equalsIgnoreCase("yes"))
478: required = true;
479: }
480: } else if (tname.equals("rtexprvalue")) {
481: Text t = (Text) e.getFirstChild();
482: if (t != null) {
483: rtexprvalue = Boolean.valueOf(t.getData().trim())
484: .booleanValue();
485: if (t.getData().equalsIgnoreCase("yes"))
486: rtexprvalue = true;
487: }
488: } else if (tname.equals("type")) {
489: Text t = (Text) e.getFirstChild();
490: if (t != null)
491: type = t.getData().trim();
492: } else
493: Constants
494: .message(
495: "jsp.warning.unknown.element.in.attribute",
496: new Object[] { e.getTagName() },
497: Logger.WARNING);
498: }
499:
500: // return new TagAttributeInfo(name, required, rtexprvalue, type);
501: return new TagAttributeInfo(name, required, type, rtexprvalue);
502: }
503:
504: static void copy(InputStream in, String fileName)
505: throws IOException, FileNotFoundException {
506: byte[] buf = new byte[1024];
507:
508: FileOutputStream out = new FileOutputStream(fileName);
509: int nRead;
510: while ((nRead = in.read(buf, 0, buf.length)) != -1)
511: out.write(buf, 0, nRead);
512: }
513:
514: }
|