cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stef...@locus.apache.org
Subject cvs commit: xml-cocoon/docs WD-xsp.xml
Date Thu, 02 Dec 1999 11:52:54 GMT
stefano     99/12/02 03:52:54

  Modified:    docs     WD-xsp.xml
  Log:
  finished xml-ize XSP WD
  
  Revision  Changes    Path
  1.2       +283 -12   xml-cocoon/docs/WD-xsp.xml
  
  Index: WD-xsp.xml
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/docs/WD-xsp.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WD-xsp.xml	1999/11/30 16:29:51	1.1
  +++ WD-xsp.xml	1999/12/02 11:52:54	1.2
  @@ -188,8 +188,10 @@
     <p>The XSP specification defines some external entities that may be used to reduce
the
     verbosity of XSP document, allowing the inclusion the default DTD via entity mapping.
The
     standard way to include the XSP DTD into XSP documents is:</p>
  -  
  -  <source><![CDATA[  ]]></source>
  +
  +  <source><![CDATA[
  +   <!DOCTYPE page PUBLIC "-//Apache//DTD XSP Layer 1//EN" "http://www.apache.org/1999/XSP/Layer1">
 
  +  ]]></source>
   
   </s2>
   
  @@ -204,15 +206,63 @@
     designed to include as content mark-up elements that belong to other namespaces.
     The XSchema effort will allow multi-namespace validation.</p>
   
  -  <source><![CDATA[  ]]></source>
  +  <source><![CDATA[
  +  
  +  <!ENTITY % elements "xsp:expr | xsp:element | xsp:pi | xsp:comment">
  +  
  +  <!ELEMENT xsp:page (xsp:structure?, xsp:logic?, xsp:content)>
  +  <!ATTLIST xsp:page
  +    language CDATA #REQUIRED
  +    result-ns NMTOKEN #IMPLIED
  +    default-space (preserve|strip) "preserve"
  +    indent-result (yes|no) "no"
  +    xmlns:xsp CDATA #FIXED "http://www.apache.org/1999/XSP/Layer1"
  +    xml:space (default|preserve) "preserve"
  +  >
  +  
  +  <!ELEMENT xsp:structure (xsp:dtd?, xsp:include*)>
  +  
  +  <!ELEMENT xsp:dtd (#PCDATA)>
  +  <!ELEMENT xsp:include (#PCDATA)>
  +  
  +  <!ELEMENT xsp:content (#PCDATA | xsp:logic | %elements;)*>
  +  
  +  <!ELEMENT xsp:logic (#PCDATA | xsp:content | %element;)*>
  +  <!ATTLIST xsp:logic xml:space (default|preserve) "preserve">
  +  
  +  <!ELEMENT xsp:element
  +            (#PCDATA | 
  +            xsp:attribute | 
  +            xsp:element | 
  +            xsp:logic)*>
  +  <!ATTLIST xsp:element
  +            name CDATA #REQUIRED>
  +  
  +  <!ELEMENT xsp:attribute (#PCDATA)>
  +  <!ATTLIST xsp:attribute 
  +            name CDATA #REQUIRED
  +            xml:space (default|preserve) "preserve">
  +  
  +  <!ELEMENT xsp:pi (#PCDATA | xsp:expr>
  +  <!ELEMENT xsp:comment (#PCDATA | xsp:expr)>
  +  <!ELEMENT xsp:expr (#PCDATA)>  
     
  +  ]]></source>
  +  
   </s2>
   </s1>
   
   <s1 title="Example of Usage">
     <p>Consider the following XML source document:</p>
  +
  +  <source><![CDATA[
  +    <?xml version="1.0"?>
   
  -  <source><![CDATA[  ]]></source>
  +    <page>
  +      <title>A Simple XSP Page</title>
  +      <p>Hi, I've been hit <counter/> times.</p>
  +    </page>  
  +  ]]></source>
   
     <p>This simple example shows the power of content/logic/style separation. While
the <code>&lt;title&gt;</code>
     tag has a very special meaning in the <em>page</em> document type, indicating
the page
  @@ -225,13 +275,137 @@
     of this specification to define a way to associate transformation sheets to pages. The
     associated logicsheet that uses the Java language as logic definition may look like:</p>
     
  -  <source><![CDATA[  ]]></source>
  -
  +  <source><![CDATA[
  +    <?xml version="1.0"?>
  +     <xsl:transformation 
  +      xmlns:xsl="http://www.w3.org/1999/XSL/tranform" 
  +      xmlns:xsp="http://www.apache.org/1999/XSP/Layer1"
  +      result-ns="http://www.apache.org/1999/XSP/Layer1"
  +     >
  +        
  +      <xsl:template match="page">
  +       <xsp:page language="java">
  +
  +        <xsp:structure>
  +         <xsp:include>java.lang.*</xsp:include>
  +        </xsp:structure>
  +          
  +        <xsp:logic>
  +         private static int counter = 0;
  +              
  +         private synchronized int currentCount() {
  +           return ++counter;
  +         }
  +        </xsp:logic>
  +           
  +        <xsp:content>
  +         <page>
  +          <xsl:apply-templates/>
  +         </page>
  +        </xsp:content>
  +       </xsp:page>
  +      </xsl:template>
  +         
  +      <xsl:template match="counter">    
  +       <xsp:expr>currentCount()</xsp:expr>
  +      </xsl:template>
  +
  +      <!-- Transcribe everything else verbatim -->
  +      <xsl:template match="*|@*|comment()|pi()|text()">
  +       <xsl:copy>
  +        <xsl:apply-templates/>
  +       </xsl:copy>
  +      </xsl:template>
  +    ]]></source>
       
     <p>After applying the above logic sheet, the resulting document would be equivalent
to the
     following:</p>
     
  -  <source><![CDATA[  ]]></source>
  +  <source><![CDATA[
  +    <xsp:page 
  +     result-ns="http://www.dummy.org/SimpleHomepageDTD"
  +     language="java">
  +
  +     <xsp:structure>
  +      <xsp:include>java.lang.*</xsp:include>
  +     </xsp:structure>
  +
  +     <xsp:logic>
  +      private static int counter = 0;
  +            
  +      private synchronized int currentCount() {
  +        return ++counter;
  +      }
  +     </xsp:logic>
  +
  +     <xsp:content>
  +      <page>
  +       <title>A Sample XSP Page</title>
  +       <p>Hi, I've been hit <xsp:expr>currentCount()</xsp:expr> times.</p>
  +      </page>
  +     </xsp:content>
  +    </xsp:page>     
  +  ]]></source>
  +  
  +  <p>At this point it's worth to note that from an XSP point of view, there is 
  +     no difference in how the XSP page was created, either directly written or 
  +     created with n levels of transformation. So, independently of whether an 
  +     XSL stylesheet or a special algorithm was used to generate the final 
  +     source code, it may look like this [<em>Note</em>: many key issues regarding

  +     servlets were omitted for simplicity and this example must not be 
  +     considered mandating as a way to format XSP into servlet source code]</p>
  +  
  +  <source><![CDATA[
  +    // package automatically created from the full request URL...
  +    package org.apache.cocoon.xsp.example;
  +
  +    // packages imported automatically by source code generator
  +    import java.io.*;
  +    import java.util.*;
  +    import javax.servlet.*;
  +    import javax.servlet.http.*;
  +    import org.w3c.dom.*;
  +      
  +    // packages imported due to XSP structure
  +    import java.lang.*;
  +
  +    // class name automatically created from the request URI file name...
  +    public class Counter extends HttpServlet {
  +
  +      public void init(ServletConfig config) throws ServletException {
  +        super.init(config);
  +      }
  +
  +      private static int counter = 0;
  +                
  +      private synchronized int currentCount() {
  +        return ++counter;
  +      }
  +     
  +      public void service(HttpServletRequest request, HttpServletResponse response) throws
IOException {
  +        Document document = parser.createEmptyDocument();
  +        Element root = document.createElement("page");
  +        document.appendChild(root);
  +      
  +        Element element_1 = document.createElement("title");
  +        root.appendChild(element_1);
  +        Text textNode_1 = document.createTextNode("A Sample XSP Page");
  +        element_1.appendChild(textNode_1);
  +
  +        Element element_2 = document.createElement("p");
  +        root.appendChild(element_2);
  +        Text textNode_2 = document.createTextNode("Hi, I've been hit ");
  +        element_2.appendChild(textNode_2);
  +        Text textNode_3 = document.createTextNode((new StringBuffer.append(currentCount())).toString());
  +        element_2.appendChild(textNode_3);
  +        Text textNode_4 = document.createTextNode("times.");
  +        element_2.appendChild(textNode_4);
  +      
  +        // Produce generated DOM tree on output as an XML stream
  +        ((Child) document).toXMLString(response.getWriter());
  +      }
  +    }  
  +  ]]></source>  
    
     <p>Note that in this example the XML document is being generated as a <em>stream</em>
     but a DOM tree is used to create it. The DOM tree can't be passed directly to
  @@ -242,8 +416,52 @@
     
     <p>To solve this problem and speed up the execution on server side XML
     processing, the XSP can be compiled into something like this:</p>
  +
  +  <source><![CDATA[
  +    // package automatically created from the full request URL...
  +    package org.apache.cocoon.xsp.example;
  +
  +    // packages imported automatically by source code generator
  +    import java.io.*;
  +    import java.util.*;
  +    import org.xml.sax.*;
  +    import org.apache.cocoon.*;
  +      
  +    // packages imported due to XSP structure
  +    import java.lang.*;
  +
  +    // class name automatically created from the request URI file name...
  +    public class Counter extends AbstractProducer {
   
  -  <source><![CDATA[  ]]></source>
  +      private static final char[] chars_0 = {'A',' ','S','i','m','p','l','e',' ','X','S','P','
','P','a','g','e'};
  +      private static final char[] chars_1 = {'H','i',' ','I','\'','v','e',' ','b','e','e','n','
','h','i','t',' '};
  +      private static final char[] chars_3 = {' ','t','i','m','e','s'};
  +
  +      private static int counter = 0;
  +                
  +      private synchronized int currentCount() {
  +        return ++counter;
  +      }
  +     
  +      public void toSAX(Request request, DocumentHandler handler) throws SAXException {
  +        handler.startDocument();
  +          handler.startElement("page", null);
  +            handler.startElement("title", null);
  +              handler.characters(chars_0, 0, 17);
  +            handler.endElement("title");
  +            handler.startElement("p", null);
  +              handler.characters(chars_1, 0, 17);
  +              String string_0 = new StringBuffer.append(currentCount())).toString();
  +              int string_0_length = string_0.length();
  +              char[] chars_2 = char[string_0_length];
  +              string_0.getChars(0, string_0_length, chars_2, 0);
  +              handler.characters(chars_3, 0, 6);
  +            handler.endElement("p");
  +          handler.endElement("page");
  +        handler.endDocument();
  +      }
  +    }  
  +  ]]></source>
   
     <p>The above shows one of the best features of XSP: output independence.
     Since the output objects are not accessible directly from the internal page
  @@ -310,11 +528,64 @@
     programming code by <code>scriptlet</code> tags. The following is an example
to show
     the different results based on the same logic and code.</p>
     
  -  <source><![CDATA[  ]]></source>
  +  <source><![CDATA[
  +    <xsp:logic>
  +      e = request.getParameterNames(); 
  +      if ((e != null) &amp;&amp; (e.hasMoreElements())) {
  +         <xsl:content>
  +           <list>
  +             <title>Servlet Parameters</title>
  +             <xsp:logic>
  +                while (e.hasMoreElements()) { 
  +                   String k = (String) e.nextElement();
  +                   String val = request.getParameter(k); 
  +                   String vals[] = request.getParameterValues(k);
  +                   <xsp:element name="element">
  +                      <xsp:attribute name="name">
  +                         <xsp:expr>k</xsp:expr>
  +                      </xsp:attribute>
  +                      <xsp:logic>
  +                         for(int i = 0; i &lt; vals.length; i++) {
  +                            <item>
  +                               <xsp:expr>vals[i]</xsp:expr>
  +                            </item>
  +                         }
  +                      </xsp:logic>
  +                   </xsp:element>
  +                 }
  +              </xsp:logic>
  +           </list>
  +         </xsl:content>
  +       } 
  +    </xsp:logic>  
  +  ]]></source>
   
     <p>This is the JSP equivalent:</p>
     
  -  <source><![CDATA[  ]]></source>
  +  <source><![CDATA[
  +   <jsp:scriplet>
  +     e = request.getParameterNames(); 
  +     if ((e != null) &amp;&amp; (e.hasMoreElements())) { </jsp:scriplet>
  +       <list>
  +       <title>Servlet Parameters</title>
  +         <jsp:scriplet>
  +         while (e.hasMoreElements()) { 
  +           String k = (String) e.nextElement();
  +           String val = request.getParameter(k); 
  +           String vals[] = request.getParameterValues(k);
  +           out.println("&lt;element name=\"name\"&gt;"); </jsp:scriplet>
  +           <jsp:expression>k</jsp:expression>
  +           <jsp:scriplet>
  +           out.println("&lt;/element&gt;");
  +           for(int i = 0; i &lt; vals.length; i++) { </jsp:scriplet>
  +             <item>
  +               <jsp:expression>vals[i]</jsp:expression>
  +             </item>
  +           <jsp:scriplet>}</jsp:scriplet>
  +         <jsp:scriplet>}</jsp:scriplet>
  +       </list>
  +     <jsp:scriplet>}</jsp:scriplet>  
  +  ]]></source>
   
     <p>It must be noted the use of the <code>out</code> object in the JSP
example while
     XSP provide specific tags to avoid that.</p>
  @@ -378,14 +649,14 @@
   <s1 title="Acknowledgements">
     <p>The following people have greatly contributed to the creation and design of
this draft:</p>
     <ul>
  -   <li><p>Ricardo Rocha &lt;<link href="mailto:rrocha@plenix.org">rrocha@plenix.org</link>&gt;</p></li>
  +   <li><p>Ricardo Rocha &lt;<link href="mailto:ricardo@apache.org">ricardo@apache.org</link>&gt;</p></li>
     </ul>
   </s1>
   
   </appendices>
   
   <footer>
  -   <legal>
  +  <legal>
      <p>Copyright (c) 1999 Apache XML Project. All Rights Reserved.</p>
     </legal>
   </footer>
  
  
  

Mime
View raw message