On Wed, May 09, 2001 at 01:36:12PM -0700, Morgan Delagrange wrote:
> Hi all,
> 
> I didn't mean to issue a proclamation here.  :)  Are there dissenting
> views to handling relative paths via getResource(), or is this silent
> consent?  I'll probably go ahead and implement getResource() if there are
> no immediate objections, but we can easily change it before the release.

Hi,

I was feeling keen, so went ahead and implemented the "ambidextrous" suggestion
of Craig's:

Craig McClanahan:
> However, using this also restricts you to resources in the same web app,
> whereas building an http URL is more general purpose.  It wouldn't be hard
> to make the tag ambidextrous -- for example, if the specified path starts
> with a slash, assume that it's a context-relative resource, but otherwise
> treat it as a URL.

In addition, page-relative paths are handled by first converting to
context-relative, and then using getResource(). Hope this is the right
approach.


Oh, and I also changed the code so the "name" attribute can point to an
org.w3c.dom.Node (it can currently point to String, Reader, InputStream or
StringReader).

Patch, and two rough example pages attached.

thanks,

--Jeff


> - Morgan

[..]
> > > > On Mon, 7 May 2001, Morgan Delagrange wrote:
> > > > 
> > > > > Reviewing this patch brings up an interesting design question.  This patch
> > > > > builds an http url, but maybe that's not the way to go.  Using this
> > > > > method:
> > > > > 
> > > > >   String xsl = application.getResource("style.xsl").toString();
> > > > > 
> > > > > we can create a file url, which is going to be significantly more
> > > > > performant.  Which is preferable?
[..]
Index: BaseSourceTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-taglibs/xsl/src/org/apache/taglibs/xsl/Attic/BaseSourceTag.java,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 BaseSourceTag.java
--- BaseSourceTag.java  2001/04/30 18:06:05     1.1.2.1
+++ BaseSourceTag.java  2001/05/15 06:59:52
@@ -4,12 +4,18 @@
 import java.io.Reader;
 import java.io.StringReader;
 
+import java.net.MalformedURLException;
+
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.jsp.JspTagException;
 import javax.servlet.jsp.PageContext;
 import javax.servlet.jsp.tagext.BodyTagSupport;
 
 import javax.xml.transform.Source;
 import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.dom.DOMSource;
+
+import org.w3c.dom.Node;
 
 public class BaseSourceTag extends BodyTagSupport {
 
@@ -65,25 +71,50 @@
       Object o = null;
 
       if(-1 == _scope) {
-        pageContext.findAttribute(_name);      
+        o = pageContext.findAttribute(_name);
       } else {
-        pageContext.getAttribute(_name,_scope);
+        o = pageContext.getAttribute(_name,_scope);
       }
-
       if (o instanceof Reader) {
         source = new StreamSource( ((Reader) o) );
       } else if (o instanceof InputStream) {
         source = new StreamSource( ((InputStream) o) );
       } else if (o instanceof String) { 
         source = new StreamSource( new StringReader((String) o) );
+      } else if (o instanceof org.w3c.dom.Node) { 
+        source = new DOMSource( (Node) o );
       } else {
-        throw new JspTagException("'name' must reference a Reader, an InputStream, or 
a String attribute.");
+        throw new JspTagException("'name' must reference a Reader, an InputStream, 
+String or org.w3c.dom.Node attribute. Name currently points to "+(o==null? "null":"a 
+"+o.getClass()) );
       }
 
     } else if(null != _uri) {
+      if (_uri.indexOf(":") == -1) {
+        // Context or page relative
+        if (!_uri.startsWith("/")) {
+          // page-relative, so let's make it context-relative
+          _uri = getMiddle()+"/"+_uri;
+        }
+        try {
+          // Now handle context-relative
+          _uri = pageContext.getServletContext().getResource(_uri).toString();
+        } catch (MalformedURLException e) {
+          e.printStackTrace();
+          throw new JspTagException("Malformed context-relative URI: 
+"+e.getMessage());
+        }
+      }
       source = new StreamSource(_uri);
     }
-
     return source;
   }
+
+  /** Get the middle part of the URL, from the context name to the beginning of
+   * the filename. Eg, from
+   * <code>http://localhost:8080/context/foo/bar/my.jsp</code>, this returns
+   * <code>foo/bar</code>.
+   */
+  private String getMiddle() {
+    String servletPath = 
+((HttpServletRequest)pageContext.getRequest()).getServletPath();
+    return servletPath.substring(0, servletPath.lastIndexOf('/'));
+  }
+
 }
<%@page import="javax.xml.parsers.*, org.w3c.dom.*, org.xml.sax.*, java.io.*" 
session="true"%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/xsl-2.0"; prefix="xslt" %>

<%--
Demonstrates how the "name" attribute can reference a pre-built org.w3c.dom.Node (XML 
Document).
Note: this should be handled with an xslt:import tag, like the old xsl taglib..

 Jeff Turner <[EMAIL PROTECTED]>
--%>

<%
DocumentBuilder builder;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
String XML = "<employees> <employee id=\"456\"> <first-name>Jane</first-name> 
<last-name>Smith</last-name> <telephone>888-555-1212</telephone> 
</employee></employees>";
Document guimap = builder.parse(new InputSource(new StringReader(XML)));
session.setAttribute("mapdoc", guimap);
session.setAttribute("test", "testing!");
%>

<html>
<form>

<xslt:xform>
  <xslt:style uri="xml/employeeList.xsl"/>
  <xslt:doc name="mapdoc"/>
  <xslt:param name="highlight_id">456</xslt:param>
</xslt:xform>

</form>
</html>

<%@ taglib uri="http://jakarta.apache.org/taglibs/xsl-2.0"; prefix="xslt" %>

<!-- Simple demo of page and context-relative URLs -->

<xslt:xform>
  <xslt:style uri="xml/employeeList.xsl"/>
  <xslt:doc uri="xml/employees.xml"/>
  <xslt:param name="highlight_id">456</xslt:param>
</xslt:xform>

Reply via email to