jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sh...@apache.org
Subject cvs commit: jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/tei ImportTEI.java
Date Tue, 30 Oct 2001 23:41:27 GMT
shawn       01/10/30 15:41:27

  Modified:    jsptl/conf jr.tld jx.tld
               jsptl/src/org/apache/taglibs/jsptl/tag/common
                        ImportSupport.java
  Removed:     jsptl/src/org/apache/taglibs/jsptl/tei ImportTEI.java
  Log:
  New rules for <import> wrt relative URLs (charEncoding, non-HTTP resources).
  Other minor changes.
  
  Revision  Changes    Path
  1.4       +0 -1      jakarta-taglibs/jsptl/conf/jr.tld
  
  Index: jr.tld
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/jsptl/conf/jr.tld,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- jr.tld	2001/10/25 23:12:56	1.3
  +++ jr.tld	2001/10/30 23:41:27	1.4
  @@ -78,7 +78,6 @@
     <tag>
       <name>import</name>
       <tag-class>org.apache.taglibs.jsptl.tag.jr.ImportTag</tag-class>
  -    <tei-class>org.apache.taglibs.jsptl.tei.ImportTEI</tei-class>
       <body-content>JSP</body-content>
       <description>
           Retrieves an absolute or relative URL and exposes its contents
  
  
  
  1.4       +0 -1      jakarta-taglibs/jsptl/conf/jx.tld
  
  Index: jx.tld
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/jsptl/conf/jx.tld,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- jx.tld	2001/10/25 23:12:56	1.3
  +++ jx.tld	2001/10/30 23:41:27	1.4
  @@ -146,7 +146,6 @@
     <tag>
       <name>import</name>
       <tag-class>org.apache.taglibs.jsptl.tag.jx.ImportTag</tag-class>
  -    <tei-class>org.apache.taglibs.jsptl.tei.ImportTEI</tei-class>
       <body-content>JSP</body-content>
       <description>
   	Retrieves an absolute or relative URL and exposes its contents
  
  
  
  1.4       +83 -20    jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/tag/common/ImportSupport.java
  
  Index: ImportSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/jsptl/src/org/apache/taglibs/jsptl/tag/common/ImportSupport.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ImportSupport.java	2001/10/25 23:33:55	1.3
  +++ ImportSupport.java	2001/10/30 23:41:27	1.4
  @@ -150,9 +150,9 @@
   	        pageContext.setAttribute(varReader, r);
   	    }
   	} catch (IOException ex) {
  -	    throw new JspTagException(ex.getMessage());
  +	    throw new JspTagException(ex.toString());
   	} catch (ServletException ex) {
  -	    throw new JspTagException(ex.getMessage());
  +	    throw new JspTagException(ex.toString());
   	}
   
   	return EVAL_BODY_INCLUDE;
  @@ -172,9 +172,9 @@
   	    }
   	    return EVAL_PAGE;
           } catch (IOException ex) {
  -	    throw new JspTagException(ex.getMessage());
  +	    throw new JspTagException(ex.toString());
           } catch (ServletException ex) {
  -	    throw new JspTagException(ex.getMessage());
  +	    throw new JspTagException(ex.toString());
           }
       }
   
  @@ -280,10 +280,11 @@
   	} else { 
   	    // handle relative URLs ourselves
   
  -	    // URL is relative, so we consider charEncoding an error
  -	    if (charEncoding != null)
  -		throw new JspTagException("&lt;import&gt; cannot take "
  -		    + "'charEncoding' attribute with relative URL");
  +	    // URL is relative, so we must be an HTTP request
  +	    if (!(pageContext.getRequest() instanceof HttpServletRequest
  +		  && pageContext.getResponse() instanceof HttpServletResponse))
  +		throw new JspTagException("Relative &lt;import&gt; from "
  +		    + "non-HTTP request not allowed.");
   
   	    // retrieve an appropriate ServletContext
   	    ServletContext c = null;
  @@ -294,8 +295,7 @@
   	        c = pageContext.getServletContext();
   
   		// normalize the URL if we have an HttpServletRequest
  -		if (pageContext.getRequest() instanceof HttpServletRequest
  -			&& !targetUrl.startsWith("/")) {
  +		if (targetUrl.startsWith("/")) {
   		    String sp = ((HttpServletRequest) 
   			pageContext.getRequest()).getServletPath();
   		    targetUrl = sp.substring(0, sp.lastIndexOf('/'))
  @@ -313,7 +313,7 @@
   	    rd.include(pageContext.getRequest(), irw);
   
   	    // recover the response String from our wrapper
  -	    return irw.toString();
  +	    return irw.getString();
   	}
       }
   
  @@ -345,27 +345,57 @@
   		throw new JspTagException(
   		    "I/O error in &lt;import&gt; occurred reading "
   		    + targetUrl()
  -		    + " [" + ex.getMessage() + "]");
  +		    + " [" + ex + "]");
   	    }
   	}
       }
   
       /** Wraps responses to allow us to retrieve results as Strings. */
  -    private static class ImportResponseWrapper
  -	    extends HttpServletResponseWrapper {
  +    private class ImportResponseWrapper extends HttpServletResponseWrapper {
   
   	//************************************************************
  +	// Overview
  +
  +	/*
  +	 * We provide either a Writer or an OutputStream as requested.
  +	 * We actually have a true Writer and an OutputStream backing
  +	 * both, since we don't want to use a character encoding both
  +	 * ways (Writer -> OutputStream -> Writer).  So we use no
  +	 * encoding at all (as none is relevant) when the target resource
  +	 * uses a Writer.  And we decode the OutputStream's bytes
  +	 * using OUR tag's 'charEncoding' attribute, or ISO-8859-1
  +	 * as the default.  We thus ignore setLocale() and setContentType()
  +	 * in this wrapper.
  +	 *
  +	 * In other words, the target's asserted encoding is used
  +	 * to convert from a Writer to an OutputStream, which is typically
  +	 * the medium through with the target will communicate its
  +	 * ultimate response.  Since we short-circuit that mechanism
  +	 * and read the target's characters directly if they're offered
  +	 * as such, we simply ignore the target's encoding assertion.
  +	 */
  +
  +	//************************************************************
   	// Data
   
   	/** The Writer we convey. */
  -	private StringWriter s = new StringWriter();
  +	private StringWriter sw = new StringWriter();
   
  +	/** A buffer, alternatively, to accumulate bytes. */
  +	private ByteArrayOutputStream bos = new ByteArrayOutputStream();
  +
   	/** A ServletOutputStream we convey, tied to this Writer. */
   	private ServletOutputStream sos = new ServletOutputStream() {
   	    public void write(int b) throws IOException {
  -		s.write(b);
  +		bos.write(b);
   	    }
   	};
  +
  +	/** 'True' if getWriter() was called; false otherwise. */
  +	private boolean isWriterUsed;
  +
  +	/** 'True if getOutputStream() was called; false otherwise. */
  +	private boolean isStreamUsed;
   	
   	//************************************************************
   	// Constructor and methods
  @@ -377,17 +407,50 @@
   	
   	/** Returns a Writer designed to buffer the output. */
   	public PrintWriter getWriter() {
  -	    return new PrintWriter(s);
  +	    if (isStreamUsed)
  +		throw new IllegalStateException(
  +		    "getWriter() was already called");
  +	    isWriterUsed = true;
  +	    return new PrintWriter(sw);
   	}
   	
   	/** Returns a ServletOutputStream designed to buffer the output. */
   	public ServletOutputStream getOutputStream() {
  +	    if (isWriterUsed)
  +		throw new IllegalStateException(
  +		    "getOutputStream() was already called");
  +	    isStreamUsed = true;
   	    return sos;
   	}
   
  -	/** Retrieves the buffered output. */
  -	public String toString() {
  -	    return s.toString();
  +	/** Has no effect. */
  +	public void setContentType(String x) {
  +	   // ignore
  +	}
  +
  +	/** Has no effect. */
  +	public void setLocale(Locale x) {
  +	   // ignore
  +	}
  +
  +	/** 
  +	 * Retrieves the buffered output, using the containing tag's 
  +	 * 'charEncoding' attribute, or the tag's default encoding,
  +	 * <b>if necessary</b>.
  +         */
  +	// notsimpley toString() because we need to throw
  +	// UnsupportedEncodingException
  +	public String getString() throws UnsupportedEncodingException {
  +	    if (isWriterUsed)
  +		return sw.toString();
  +	    else if (isStreamUsed) {
  +		if (charEncoding != null)
  +		    return bos.toString(charEncoding);
  +		else
  +		    return bos.toString(DEFAULT_ENCODING);
  +	    } else
  +		throw new IllegalStateException(
  +		    "neither getOutputStream() nor getWriter() was called");
   	}
       }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:taglibs-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:taglibs-dev-help@jakarta.apache.org>


Mime
View raw message