cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pierrick Brihaye" <pierrick.brih...@wanadoo.fr>
Subject Re: XSP : control structures
Date Sat, 18 Jan 2003 19:22:02 GMT
Hi,

Sorry, I have reordered your answer :

> > Are there control structures in XSP ? Here is what I mean (taken from
> > http://xml.apache.org/cocoon/userdocs/xsp/logicsheet.html) :
>
> No.

:-(

> For the example you gave, if msg has either an empty string value, or a
> useful value (in other words not null), why wouldn't you just do:
>
> <greeting><xsp:expr>msg</xsp:expr></greeting> ?

Definitely. It was just a (stupid) example of how to make an xsp without a
line of <xsp:logic>. Don't you think that giving (a bit) more of abstraction
to XSP (especially in the canonical "Hello World" example) would be more
attractive for beginners  ?

However, dont misunderstand me : I dont want the <xsp:logic> tag to
disappear... I'd just like to have more ease in writing xsp/logicsheets when
it is possible. And I think that it is possible :-)

> If you give a more concrete example I bet you could get a discussion going
> on best practices for implementing it.

OK. Here is one from a real logicsheet (not one from me, but it could have
been) :

<!-- build a document -->
<xsl:template name="sdx:document" match="sdx:page//sdx:document">
 <xsl:param name="parameters" select="true()"/>
<xsp:logic>
/*
*    build Document
*/
 sdx_document=null;
</xsp:logic>
 <xsl:if test="$parameters">
<!-- id could be modified or setted by url, or indexed content -->
<xsl:call-template name="sdx:id"/>
<!-- type could be modified by content -->
<xsl:call-template name="sdx:type"/>
<xsl:call-template name="sdx:url"/>
<xsl:call-template name="sdx:file"/>
<xsl:call-template name="sdx:xml"/>
<xsl:call-template name="sdx:dom"/>
</xsl:if>
<xsp:logic>
// set type
if ("text/xml".equals(sdx_type)) sdx_document=new XMLDocument();
else if ("text/html".equals(sdx_type)) sdx_document=new HTMLDocument();
else if (Document.DOCTYPE_XML.equals(sdx_type)) { sdx_type="text/xml";
sdx_document=new XMLDocument(); }
else if (Document.DOCTYPE_HTML.equals(sdx_type)) { sdx_type="text/html";
sdx_document=new HTMLDocument(); }
else if (sdx_xml != null) { sdx_type="text/xml"; sdx_document=new
XMLDocument();}
else if (sdx_url != null) { sdx_type="text/html"; sdx_document=new
HTMLDocument(); }
else if (sdx_dom != null) { sdx_type="text/xml"; sdx_document=new
XMLDocument(); }
else { sdx_document=new XMLDocument(); sdx_type="text/xml"; }
if (sdx_type != null) sdx_document.setMimeType(sdx_type);
// set id
if (sdx_id != null) sdx_document.setId(sdx_id);
else if (sdx_url != null) sdx_document.setId(sdx_url.toExternalForm());
// set content
if (sdx_url != null) sdx_document.setContent(sdx_url);
else if (sdx_xml != null)
sdx_document.setContent(sdx_parameter.getBytes("UTF-8"));
else if (sdx_file != null) sdx_document.setContent(sdx_file);
else if (sdx_dom != null)  ((XMLDocument)sdx_document).setContent(sdx_dom);
else sdx_document=null;
</xsp:logic>
</xsl:template>

With some <xsp:choose> and <xsp:variable> tags, this template could have
been entirely XML...

Now, this an xsp (a bit old : Cocoon 1... but it's mine :-) :

<?xml version="1.0" encoding="ISO-8859-1"?>
<?cocoon-process type="xsp"?>
<?cocoon-process type="xslt"?>
<?xml-stylesheet href="xsl/main.xsl" type="text/xsl"?>
<xsp:page language="java" xmlns:xsp="http://www.apache.org/1999/XSP/Core"
xmlns:sdx="http://www.culture.gouv.fr/ns/sdx/sdx">
 <sdx:page>
  <!-- on est toujours prêt à accepter un logout -->
  <sdx:handleLogout/>
  <xsp:logic>
   <!-- Effectue l'action demandée -->
   String userAction = request.getParameter("execute");
   if (userAction != null)
   {
    if (userAction.equals("login"))
    {
     String userCode = request.getParameter("usercode");
     String userPassword = request.getParameter("password");
     if (userPassword == null) userPassword = "";
     boolean userOK = Utilities.validateUser(sdxEnvironment, session,
userCode, userPassword);
     <Redirect/>
    }
    else if (userAction.equals("add_document"))
    {
     <sdx:uploadDocument fileParam="file" replaceParam="replace"
statusParam="status"/>
     <Redirect/>
    }
    else if (userAction.equals("add_documents"))
    {
     <sdx:uploadDocuments dirParam="directory" statusParam="status"/>
     <Redirect/>
    }
    else if (userAction.equals("add_zip_documents"))
    {
     java.util.zip.ZipFile zf = new
java.util.zip.ZipFile(sdxRequest.getUploadedFilename("file"));
     sdxDBInfo.uploadDocuments(zf, sdxUser.getCode());
     <Redirect/>
    }
    else if (userAction.equals("delete_document"))
    {
     <sdx:deleteDocument idParam="id"/>
     <Redirect/>
    }
    else if (userAction.equals("reindex_documents"))
    {
     sdxDBInfo.reIndex(false);
     <Redirect/>
    }
    else if (userAction.equals("parsed_query"))
    {
     String baseQuery = sdxRequest.getParameter("baseQuery");
     String baseOperator = sdxRequest.getParameter("baseOperator");
     boolean useJokers =  (sdxRequest.getParameter("usejokers") != null);
     <!-- débogage -->
     <parsedQuery>
      <xsp:attribute name="query">
       <xsp:expr>sdxRequest.getParameter("query")</xsp:expr>
      </xsp:attribute>
     </parsedQuery>
     if(useJokers)
     {
      if ((baseQuery != null) &amp;&amp; (baseOperator != null))
      {
       <sdx:executeParsedQuery queryParam="query" useJokers="true"
bqParam="baseQuery" boParam="baseOperator" sfParams="tri"/>
      }
      else
      {
       <sdx:executeParsedQuery queryParam="query" useJokers="true"
sfParams="tri"/>
      }
     }
     else
     {
      if ((baseQuery != null) &amp;&amp; (baseOperator != null))
      {
       <sdx:executeParsedQuery queryParam="query" useJokers="false"
bqParam="baseQuery" boParam="baseOperator" sfParams="tri"/>
      }
      else
      {
       <sdx:executeParsedQuery queryParam="query" useJokers="false"
sfParams="tri"/>
      }
     }
    }
    else if (userAction.equals("field_query"))
    {
     String baseQuery = sdxRequest.getParameter("baseQuery");
     String baseOperator = sdxRequest.getParameter("baseOperator");
     boolean useJokers =  (sdxRequest.getParameter("usejokers") != null);
     if (useJokers)
     {
      if ((baseQuery != null) &amp;&amp; (baseOperator != null))
      {
       <sdx:executeFieldQuery fieldParam="index" valueParam="query"
bqParam="baseQuery" boParam="baseOperator" sfParams="tri"/>
      }
      else
      {
       <sdx:executeFieldQuery fieldParam="index" valueParam="query"
sfParams="tri"/>
      }
     }
     else
     {
      if ((baseQuery != null) &amp;&amp; (baseOperator != null))
      {
       <sdx:executeExactFieldQuery fieldParam="index" valueParam="query"
bqParam="baseQuery" boParam="baseOperator" sfParams="tri"/>
      }
      else
      {
       <sdx:executeExactFieldQuery fieldParam="index" valueParam="query"
sfParams="tri"/>
      }
     }
     <!-- débogage -->
     <index>
      <xsp:attribute name="recherche">
       <xsp:expr>sdxRequest.getParameter("recherche")</xsp:expr>
      </xsp:attribute>
      <xsp:attribute name="index">
       <xsp:expr>sdxRequest.getParameter("index")</xsp:expr>
      </xsp:attribute>
      <xsp:attribute name="query">
       <xsp:expr>sdxRequest.getParameter("query")</xsp:expr>
      </xsp:attribute>
     </index>
    }
    else if (userAction.equals("index_query"))
    {
     String pParam = sdxRequest.getParameter("p");
     if (pParam == null) pParam = "1";
     String hppParam = sdxRequest.getParameter("hpp");
     if (hppParam == null) hppParam = "20";
     <indexQuery>
      <xsp:attribute name="index">
       <xsp:expr>sdxRequest.getParameter("index")</xsp:expr>
      </xsp:attribute>
      <xsp:attribute name="page">
       <xsp:expr>pParam</xsp:expr>
      </xsp:attribute>
      <xsp:attribute name="hpp">
       <xsp:expr>hppParam</xsp:expr>
      </xsp:attribute>
     </indexQuery>
    }
    else if (userAction.equals("query_navigation"))
    {
     <sdx:executeSimpleQuery qidParam="Query" pParam="pageNo"/>
    }
    else if (userAction.equals("show_document"))
    {
     <sdx:includeDocument idParam="id" qidParam="q" noParam="n"/>
    }
    else throw new SDXException("fr", "Paramètre d'exécution incorrect : " +
userAction);
   }
  </xsp:logic>
 </sdx:page>
</xsp:page>

What kind of best pratice is there for this ?

> I think that there has been an intention to keep this kind of structure
> out of xsp - for the reasoning you'll have to search through the dev list
> archives.

I'll do...

>  Generally I think the opinion has been that those types of
> things are better left for later transformation steps.

Well, they're right : my transformations are definitely more complex than my
XSPs :-)
But... I can"t understand this argument : whenever a <xsp:logic> contains a
"if", a "switch", a "for"... statement, it could have corresponding xsp
tags, could'nt it ? Regarding <xsp:variables> evaluation, I reckognize that
it is a far more complex problem to solve. I am ready to have an <xsp:expr>
behaviour :-)

Cheers,

p.b.



---------------------------------------------------------------------
Please check that your question  has not already been answered in the
FAQ before posting.     <http://xml.apache.org/cocoon/faq/index.html>

To unsubscribe, e-mail:     <cocoon-users-unsubscribe@xml.apache.org>
For additional commands, e-mail:   <cocoon-users-help@xml.apache.org>


Mime
View raw message