You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by co...@apache.org on 2004/03/20 20:29:14 UTC

cvs commit: cocoon-2.1/src/blocks/forms/samples/v2 carselector_template.xml form1_template.xml sitemap.xmap

coliver     2004/03/20 11:29:14

  Modified:    src/blocks/forms/samples/v2 carselector_template.xml
                        form1_template.xml sitemap.xmap
  Added:       src/blocks/forms/java/org/apache/cocoon/forms/generation
                        SaxBuffer.java template.jx
  Log:
  First try at jx macros for form template processing
  
  Revision  Changes    Path
  1.1                  cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/generation/SaxBuffer.java
  
  Index: SaxBuffer.java
  ===================================================================
  /*
   * Copyright 1999-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.cocoon.forms.generation;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.SAXException;
  import org.xml.sax.Locator;
  import org.xml.sax.Attributes;
  import org.xml.sax.ext.LexicalHandler;
  import org.apache.excalibur.xml.sax.XMLizable;
  import java.util.List;
  import java.util.Iterator;
  
  /**
   * Hack to get the fi:styling element into the fi:widget element 
   * (Basically a copy of o.a.c.xml.SaxBuffer, but dumps the events into 
   *  a user-supplied list). TBD: merge these.
   */
  
  public class SaxBuffer 
      implements ContentHandler, LexicalHandler, XMLizable {
  
      private List buffer;
      
      public SaxBuffer(List list) {
          buffer = list;
      }
  
      public void skippedEntity(String name) throws SAXException {
          buffer.add(new SkippedEntity(name));
      }
  
      public void setDocumentLocator(Locator locator) {
          // don't record this event
      }
  
      public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
          buffer.add(new IgnorableWhitespace(ch, start, length));
      }
  
      public void processingInstruction(String target, String data) throws SAXException {
          buffer.add(new PI(target, data));
      }
  
      public void startDocument() throws SAXException {
          buffer.add(StartDocument.SINGLETON);
      }
  
      public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
          buffer.add(new StartElement(namespaceURI, localName, qName, atts));
      }
  
      public void endPrefixMapping(String prefix) throws SAXException {
          buffer.add(new EndPrefixMapping(prefix));
      }
  
      public void characters(char ch[], int start, int length) throws SAXException {
          buffer.add(new Characters(ch, start, length));
      }
  
      public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
          buffer.add(new EndElement(namespaceURI, localName, qName));
      }
  
      public void endDocument() throws SAXException {
          buffer.add(EndDocument.SINGLETON);
      }
  
      public void startPrefixMapping(String prefix, String uri) throws SAXException {
          buffer.add(new StartPrefixMapping(prefix, uri));
      }
  
      public void endCDATA() throws SAXException {
          buffer.add(EndCDATA.SINGLETON);
      }
  
      public void comment(char ch[], int start, int length) throws SAXException {
          buffer.add(new Comment(ch, start, length));
      }
  
      public void startEntity(String name) throws SAXException {
          buffer.add(new StartEntity(name));
      }
  
      public void endDTD() throws SAXException {
          buffer.add(EndDTD.SINGLETON);
      }
  
      public void startDTD(String name, String publicId, String systemId) throws SAXException {
          buffer.add(new StartDTD(name, publicId, systemId));
      }
  
      public void startCDATA() throws SAXException {
          buffer.add(StartCDATA.SINGLETON);
      }
  
      public void endEntity(String name) throws SAXException {
          buffer.add(new EndEntity(name));
      }
  
      public void toSAX(ContentHandler contentHandler) throws SAXException {
          for (Iterator i = buffer.iterator(); i.hasNext();) {
              SaxEvent saxEvent = (SaxEvent)i.next();
              saxEvent.send(contentHandler);
          }
      }
  
      public interface SaxEvent {
          public void send(ContentHandler contentHandler) throws SAXException;
      }
  
      public final static class StartDocument implements SaxEvent {
          static public final StartDocument SINGLETON = new StartDocument();
          public void send(ContentHandler contentHandler) throws SAXException {
              contentHandler.startDocument();
          }
      }
  
      public final static class EndDocument implements SaxEvent {
          static public final EndDocument SINGLETON = new EndDocument();
          public void send(ContentHandler contentHandler) throws SAXException {
              contentHandler.endDocument();
          }
      }
  
      public final static class PI implements SaxEvent {
          private final String target;
          private final String data;
  
          public PI(String target, String data) {
              this.target = target;
              this.data = data;
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              contentHandler.processingInstruction(target, data);
          }
      }
  
      public final static class StartDTD implements SaxEvent {
          private final String name;
          private final String publicId;
          private final String systemId;
  
          public StartDTD(String name, String publicId, String systemId) {
              this.name = name;
              this.publicId = publicId;
              this.systemId = systemId;
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              if (contentHandler instanceof LexicalHandler)
                  ((LexicalHandler)contentHandler).startDTD(name, publicId, systemId);
          }
      }
  
      public final static class EndDTD implements SaxEvent {
          static public final EndDTD SINGLETON = new EndDTD();
          public void send(ContentHandler contentHandler) throws SAXException {
              if (contentHandler instanceof LexicalHandler)
                  ((LexicalHandler)contentHandler).endDTD();
          }
      }
  
      public final static class StartEntity implements SaxEvent {
          private final String name;
  
          public StartEntity(String name) {
              this.name = name;
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              if (contentHandler instanceof LexicalHandler)
                  ((LexicalHandler)contentHandler).startEntity(name);
          }
      }
  
      public final static class EndEntity implements SaxEvent {
          private final String name;
  
          public EndEntity(String name) {
              this.name = name;
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              if (contentHandler instanceof LexicalHandler)
                  ((LexicalHandler)contentHandler).endEntity(name);
          }
      }
  
      public final static class SkippedEntity implements SaxEvent {
          private final String name;
  
          public SkippedEntity(String name) {
              this.name = name;
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              contentHandler.skippedEntity(name);
          }
      }
  
      public final static class StartPrefixMapping implements SaxEvent {
          private final String prefix;
          private final String uri;
  
          public StartPrefixMapping(String prefix, String uri) {
              this.prefix = prefix;
              this.uri = uri;
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              contentHandler.startPrefixMapping(prefix, uri);
          }
      }
  
      public final static class EndPrefixMapping implements SaxEvent {
          private final String prefix;
  
          public EndPrefixMapping(String prefix) {
              this.prefix = prefix;
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              contentHandler.endPrefixMapping(prefix);
          }
      }
  
      public final static class StartElement implements SaxEvent {
          private final String namespaceURI;
          private final String localName;
          private final String qName;
          private final Attributes attrs;
  
          public StartElement(String namespaceURI, String localName, String qName, Attributes attrs) {
              this.namespaceURI = namespaceURI;
              this.localName = localName;
              this.qName = qName;
              this.attrs = new org.xml.sax.helpers.AttributesImpl(attrs);
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              contentHandler.startElement(namespaceURI, localName, qName, attrs);
          }
      }
  
      public final static class EndElement implements SaxEvent {
          private final String namespaceURI;
          private final String localName;
          private final String qName;
  
          public EndElement(String namespaceURI, String localName, String qName) {
              this.namespaceURI = namespaceURI;
              this.localName = localName;
              this.qName = qName;
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              contentHandler.endElement(namespaceURI, localName, qName);
          }
      }
  
      public final static class Characters implements SaxEvent {
          private final char[] ch;
  
          public Characters(char[] ch, int start, int length) {
              this.ch = new char[length];
              System.arraycopy(ch, start, this.ch, 0, length);
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              contentHandler.characters(ch, 0, ch.length);
          }
          
          public void toString(StringBuffer value) {
              value.append(ch);
          }
      }
  
      public final static class Comment implements SaxEvent {
          private final char[] ch;
  
          public Comment(char[] ch, int start, int length) {
              this.ch = new char[length];
              System.arraycopy(ch, start, this.ch, 0, length);
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              if (contentHandler instanceof LexicalHandler)
                  ((LexicalHandler)contentHandler).comment(ch, 0, ch.length);
          }
      }
  
      public final static class StartCDATA implements SaxEvent {
          static public final StartCDATA SINGLETON = new StartCDATA();
          public void send(ContentHandler contentHandler) throws SAXException {
              if (contentHandler instanceof LexicalHandler)
                  ((LexicalHandler)contentHandler).startCDATA();
          }
      }
  
      public final static class EndCDATA implements SaxEvent {
          static public final EndCDATA SINGLETON = new EndCDATA();
          public void send(ContentHandler contentHandler) throws SAXException {
              if (contentHandler instanceof LexicalHandler)
                  ((LexicalHandler)contentHandler).endCDATA();
          }
      }
  
      public final static class IgnorableWhitespace implements SaxEvent {
          private final char[] ch;
  
          public IgnorableWhitespace(char[] ch, int start, int length) {
              this.ch = new char[length];
              System.arraycopy(ch, start, this.ch, 0, length);
          }
  
          public void send(ContentHandler contentHandler) throws SAXException {
              contentHandler.ignorableWhitespace(ch, 0, ch.length);
          }
      }
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/generation/template.jx
  
  Index: template.jx
  ===================================================================
  <jx:template xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
  
  <jx:macro name="form-template" targetNamespace="http://apache.org/cocoon/forms/1.0#template">  
  <jx:parameter name="action"/>  
  <jx:parameter name="method"/>
  <fi:form-template xmlns:fi="http://apache.org/cocoon/forms/1.0#instance" 
                  action="${action}" method="${method}">
  <jx:set var="context_widget_" value="#{.}"/>
  <jx:evalBody/>
  </fi:form-template>
  </jx:macro>
  
  <jx:macro name="widget" targetNamespace="http://apache.org/cocoon/forms/1.0#template">  
  <jx:parameter name="id"/>  
    <jx:set var="context_widget_" value="${context_widget_.getWidget(id)}"/>
    <jx:set var="list_" value="${java.util.LinkedList()}"/>
    <jx:set var="buffer_" value="${Packages.org.apache.cocoon.forms.generation.SaxBuffer(list_)}"/>
    <jx:if test="${empty locale}">
      <jx:set var="locale" value="${java.util.Locale.getDefault()}"/>
    </jx:if>
    ${context_widget_.unwrap().generateSaxFragment(buffer_, locale)}
    <jx:set var="endElement_" value="${list_.removeLast()}"/>
    ${buffer_.toSAX(cocoon.consumer)}
    <jx:evalBody/>
    ${endElement_.send(cocoon.consumer)}
  </jx:macro>
  
  <jx:macro name="repeater-widget-label" targetNamespace="http://apache.org/cocoon/forms/1.0#template">  
  <jx:parameter name="id"/>  
  <jx:parameter name="widget-id"/>
  ${context_widget_.getWidget(id).unwrap().generateWidgetLabel(this['widget-id'], cocoon.consumer)}
  </jx:macro>
  
  <jx:macro name="widget-label" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
  <jx:parameter name="id"/>
  ${context_widget_.getWidget(id).unwrap().generateLabel(cocoon.consumer)}
  </jx:macro>
  
  <jx:macro name="repeater-size" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
  <jx:parameter name="id"/>
  ${context_widget_.getWidget(id).unwrap().generateSize(cocoon.consumer)}
  </jx:macro>
  
  <jx:macro name="repeater-widget" targetNamespace="http://apache.org/cocoon/forms/1.0#template" xmlns:ft="http://apache.org/cocoon/forms/1.0#template">  
  <jx:parameter name="id"/>
    <jx:set var="repeater_" value="${context_widget_.getWidget(id)}"/>
    <jx:set var="lastRow_" value="${repeater_.length - 1}"/>
    <jx:forEach varStatus="loop_" begin="0" end="${lastRow_}">
        <jx:set var="context_widget_" value="${repeater_.getRow(loop_.index)}"/> 
        <jx:evalBody/>
    </jx:forEach>
  </jx:macro>
  
  <jx:macro name="continuation-id" targetNamespace="http://apache.org/cocoon/forms/1.0#template">  
  <fi:continuation-id xmlns:fi="http://apache.org/cocoon/forms/1.0#instance">${cocoon.continuation.id}</fi:continuation-id> 
  </jx:macro>
  
  
  </jx:template>
  
  
  
  
  1.3       +1 -0      cocoon-2.1/src/blocks/forms/samples/v2/carselector_template.xml
  
  Index: carselector_template.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/forms/samples/v2/carselector_template.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- carselector_template.xml	11 Mar 2004 02:56:32 -0000	1.2
  +++ carselector_template.xml	20 Mar 2004 19:29:14 -0000	1.3
  @@ -17,6 +17,7 @@
   <page xmlns:ft="http://apache.org/cocoon/forms/1.0#template" 
         xmlns:fi="http://apache.org/cocoon/forms/1.0#instance"
         xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
  +  <jx:import uri="resource://org/apache/cocoon/forms/generation/template.jx"/>
     <title>Car selector</title>
     <para>This example illustrates how you can programmatically update the
       content of a selection list.</para>
  
  
  
  1.2       +1 -0      cocoon-2.1/src/blocks/forms/samples/v2/form1_template.xml
  
  Index: form1_template.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/forms/samples/v2/form1_template.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- form1_template.xml	9 Mar 2004 10:34:02 -0000	1.1
  +++ form1_template.xml	20 Mar 2004 19:29:14 -0000	1.2
  @@ -18,6 +18,7 @@
   <page xmlns:ft="http://apache.org/cocoon/forms/1.0#template" 
         xmlns:fi="http://apache.org/cocoon/forms/1.0#instance" 
         xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
  +  <jx:import uri="resource://org/apache/cocoon/forms/generation/template.jx"/>
     <title>Sample form</title>
     <content>
       <ft:form-template action="${cocoon.continuation.id}.continue" method="POST">
  
  
  
  1.7       +0 -1      cocoon-2.1/src/blocks/forms/samples/v2/sitemap.xmap
  
  Index: sitemap.xmap
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/forms/samples/v2/sitemap.xmap,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- sitemap.xmap	18 Mar 2004 21:04:40 -0000	1.6
  +++ sitemap.xmap	20 Mar 2004 19:29:14 -0000	1.7
  @@ -73,7 +73,6 @@
         <map:match pattern="*-display-pipeline">
           <!-- pipeline to show the form -->
           <map:generate type="jx" src="{1}_template.xml"/>
  -        <map:transform type="forms"/>
           <map:transform type="i18n">
             <map:parameter name="locale" value="en-US"/>
           </map:transform>