cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Timothy Larson" <Tdlar...@ci.canton.oh.us>
Subject Easy access to request attributes and session DOM from XSP!
Date Mon, 13 Jan 2003 21:38:05 GMT
Here is an easy way to get and set values in request and session attributes
and session DOM's from XSP pages:

    <xsp:logic>set("request:req1", "Hello, World, from request!");</xsp:logic>
    <xsp:expr>get("request:req1")</xsp:expr>

    <xsp:logic>set("session:databases/testdb/@driver", "Hello, World, from session DOM!");</xsp:logic>
    <xsp:expr>get("session:databases/testdb/@driver")</xsp:expr>

It works well as-is, but should probably be made more general, cleaned up, and turned into
a logicsheet.
The support code and three sample uses are below.

Tim

===

<?xml version="1.0"?>
<xsp:page
  language="java"
  xmlns:xsp     = "http://apache.org/xsp"
  xmlns:request = "http://apache.org/xsp/request/2.0"
  xmlns:session = "http://apache.org/xsp/session/2.0" create-session="yes"
  xmlns:input   = "http://apache.org/cocoon/xsp/input/1.0">

  <xsp:structure>
    <xsp:include>org.apache.commons.jxpath.JXPathContext</xsp:include>
  </xsp:structure>

  <xsp:logic>

    Object get (String path)
    {
      String protocol = null;
      String attribute = null;
      String jxpath = null;

      String[] s1 = path.split(":", 2);
      if (s1[0].equals("request")) protocol = "request-attr";
      if (s1[0].equals("session")) protocol = "session-attr";
      if (protocol == null) return null;

      String[] s2 = s1[1].split("/", 2);

      attribute = s2[0];
      if (attribute == null) return null;

      if (s2.length == 2) jxpath = s2[1];

      if (jxpath == null){
        return this._xsp_module_helper.getAttribute(objectModel, protocol, attribute, null);
      }else{
        org.w3c.dom.Element e =
          ((org.w3c.dom.Element)
            (this._xsp_module_helper.getAttribute(objectModel, protocol, attribute, null)));
        JXPathContext jxcontext = JXPathContext.newContext(e);
        return jxcontext.getValue(jxpath);
      }
    }

    void set (String path, Object value)
    {
      String protocol = null;
      String attribute = null;
      String jxpath = null;

      String[] s1 = path.split(":", 2);
      if (s1[0].equals("request")) protocol = "request-attr";
      if (s1[0].equals("session")) protocol = "session-attr";
      if (protocol == null) return;

      String[] s2 = s1[1].split("/", 2);

      attribute = s2[0];
      if (attribute == null) return;

      if (s2.length == 2) jxpath = s2[1];

      if (jxpath == null){
        if (s1[0].equals("request")) request.setAttribute(attribute, value);
        if (s1[0].equals("session")) request.getSession().setAttribute(attribute, value);
      }else{
        org.w3c.dom.Element e =
          ((org.w3c.dom.Element)
            (this._xsp_module_helper.getAttribute(objectModel, protocol, attribute, null)));
        JXPathContext jxcontext = JXPathContext.newContext(e);
        jxcontext.setValue(jxpath, value);
      }
    }

  </xsp:logic>

  <sample-page>

    <xsp:logic>set("request:req1", "Hello, World, from request!");</xsp:logic>
    <xsp:logic>set("session:ses1", "Hello, World, from session!");</xsp:logic>
    <xsp:logic>set("session:databases/testdb/@driver", "Hello, World, from session DOM!");</xsp:logic>

    <request-test><xsp:expr>get("request:req1")</xsp:expr></request-test>
    <session-test><xsp:expr>get("session:ses1")</xsp:expr></session-test>
    <session-DOM-test><xsp:expr>get("session:databases/testdb/@driver")</xsp:expr></session-DOM-test>

  </sample-page>

</xsp:page>



---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


Mime
View raw message