cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/samples Sex.java Form2Bean.java
Date Thu, 06 Nov 2003 22:58:37 GMT
ugo         2003/11/06 14:58:37

  Modified:    src/blocks/woody/samples/forms form2_bind_bean.xml
                        form2_jx.xml form2_template.xml form2_model.xml
               src/blocks/woody/conf woody-datatype.xconf
               src/blocks/woody/samples/flow binding_example.js
               src/blocks/woody/java/org/apache/cocoon/woody/samples
                        Form2Bean.java
  Added:       src/blocks/woody/java/org/apache/cocoon/woody/datatype
                        Enum.java
               src/blocks/woody/java/org/apache/cocoon/woody/datatype/convertor
                        EnumConvertorBuilder.java EnumConvertor.java
               src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl
                        EnumTypeBuilder.java EnumType.java
               src/blocks/woody/java/org/apache/cocoon/woody/samples
                        Sex.java
  Log:
  First stab at implementing an "enum" datatype and convertor for Woody.
  
  Revision  Changes    Path
  1.6       +1 -0      cocoon-2.1/src/blocks/woody/samples/forms/form2_bind_bean.xml
  
  Index: form2_bind_bean.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/forms/form2_bind_bean.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- form2_bind_bean.xml	18 Sep 2003 20:17:03 -0000	1.5
  +++ form2_bind_bean.xml	6 Nov 2003 22:58:36 -0000	1.6
  @@ -16,6 +16,7 @@
     <wb:value id="birthday" path="birthday"/>
     <wb:value id="number" path="aNumber"/>
     <wb:value id="choose" path="choose"/>
  +  <wb:value id="sex" path="sex"/>
   
     <!-- An aggregatefield is a special kind of widget that lets
     the user edit the value of multiple widgets through one text box
  
  
  
  1.5       +2 -0      cocoon-2.1/src/blocks/woody/samples/forms/form2_jx.xml
  
  Index: form2_jx.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/forms/form2_jx.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- form2_jx.xml	12 Aug 2003 12:59:10 -0000	1.4
  +++ form2_jx.xml	6 Nov 2003 22:58:36 -0000	1.5
  @@ -12,6 +12,8 @@
       number: ${form2bean.aNumber}
       <br/>
       I choose: ${form2bean.choose}
  +    <br/>
  +    Sex: ${form2bean.sex}
   
       <table border="1">
         <tr>
  
  
  
  1.4       +4 -0      cocoon-2.1/src/blocks/woody/samples/forms/form2_template.xml
  
  Index: form2_template.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/forms/form2_template.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- form2_template.xml	12 Aug 2003 13:29:54 -0000	1.3
  +++ form2_template.xml	6 Nov 2003 22:58:36 -0000	1.4
  @@ -47,6 +47,10 @@
             <td/>
             <td valign="top"><wt:widget id="choose"/> <wt:widget-label id="choose"/></td>
           </tr>
  +        <tr>
  +          <td valign="top"><wt:widget-label id="sex"/></td>
  +          <td valign="top"><wt:widget id="sex"/></td>
  +        </tr>
         </table>
   
         <wt:widget-label id="contacts"/><br/>
  
  
  
  1.10      +13 -0     cocoon-2.1/src/blocks/woody/samples/forms/form2_model.xml
  
  Index: form2_model.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/forms/form2_model.xml,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- form2_model.xml	1 Nov 2003 20:09:13 -0000	1.9
  +++ form2_model.xml	6 Nov 2003 22:58:36 -0000	1.10
  @@ -79,6 +79,19 @@
         <wd:label>I choose</wd:label>
       </wd:booleanfield>
     
  +    <wd:field id="sex">
  +      <wd:label>Sex</wd:label>
  +      <wd:datatype base="enum">
  +      	<wd:convertor type="enum">
  +          <wd:enum>org.apache.cocoon.woody.samples.Sex</wd:enum>
  +      	</wd:convertor>
  +      </wd:datatype>
  +      <wd:selection-list>
  +        <wd:item value="male"/>
  +        <wd:item value="female"/>
  +      </wd:selection-list>
  +    </wd:field>
  +  
       <wd:repeater id="contacts">
         <wd:widgets>
           <wd:output id="id">
  
  
  
  1.6       +5 -0      cocoon-2.1/src/blocks/woody/conf/woody-datatype.xconf
  
  Index: woody-datatype.xconf
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/conf/woody-datatype.xconf,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- woody-datatype.xconf	22 Oct 2003 20:22:07 -0000	1.5
  +++ woody-datatype.xconf	6 Nov 2003 22:58:36 -0000	1.6
  @@ -40,6 +40,11 @@
             <convertor name="formatting" src="org.apache.cocoon.woody.datatype.convertor.FormattingIntegerConvertorBuilder"/>
           </convertors>
         </datatype>  
  +      <datatype name="enum" src="org.apache.cocoon.woody.datatype.typeimpl.EnumTypeBuilder">
  +        <convertors default="enum" plain="enum">
  +          <convertor name="enum" src="org.apache.cocoon.woody.datatype.convertor.EnumConvertorBuilder"/>
  +        </convertors>
  +      </datatype>
       </datatypes>
       <validation-rules>
         <validation-rule name="length" src="org.apache.cocoon.woody.datatype.validationruleimpl.LengthValidationRuleBuilder"/>
  
  
  
  1.14      +1 -0      cocoon-2.1/src/blocks/woody/samples/flow/binding_example.js
  
  Index: binding_example.js
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/flow/binding_example.js,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- binding_example.js	6 Nov 2003 17:06:31 -0000	1.13
  +++ binding_example.js	6 Nov 2003 22:58:36 -0000	1.14
  @@ -91,6 +91,7 @@
       bean.setPhoneZone("2");
       bean.setPhoneNumber("123456");
       bean.setBirthday(new java.util.Date());
  +    bean.setSex(Packages.org.apache.cocoon.woody.samples.Sex.FEMALE);
       var contact = new Packages.org.apache.cocoon.woody.samples.Contact();
       contact.setId("1");
       contact.setFirstName("Herman");
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/Enum.java
  
  Index: Enum.java
  ===================================================================
  /*
   * $Id: Enum.java,v 1.1 2003/11/06 22:58:36 ugo Exp $
   */
  package org.apache.cocoon.woody.datatype;
  
  import java.util.Locale;
  
  /**
   * Description of Enum.
   * @version CVS $Id: Enum.java,v 1.1 2003/11/06 22:58:36 ugo Exp $
   */
  public interface Enum {
  
      public String convertToString(Locale locale);
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/convertor/EnumConvertorBuilder.java
  
  Index: EnumConvertorBuilder.java
  ===================================================================
  /*
   * $Id: EnumConvertorBuilder.java,v 1.1 2003/11/06 22:58:36 ugo Exp $
   */
  package org.apache.cocoon.woody.datatype.convertor;
  
  import org.apache.cocoon.woody.Constants;
  import org.apache.cocoon.woody.util.DomHelper;
  import org.w3c.dom.Element;
  
  /**
   * Description of EnumConvertorBuilder.
   */
  public class EnumConvertorBuilder implements ConvertorBuilder {
  
      /* (non-Javadoc)
       * @see org.apache.cocoon.woody.datatype.convertor.ConvertorBuilder#build(org.w3c.dom.Element)
       */
      public Convertor build(Element configElement) throws Exception {
          if (configElement == null) {
              return null;
          }
          Element enumEl = DomHelper.getChildElement(configElement,
                  Constants.WD_NS, "enum", true);
          String clazz = enumEl.getFirstChild().getNodeValue();
          return new EnumConvertor(clazz);
      }
  
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/convertor/EnumConvertor.java
  
  Index: EnumConvertor.java
  ===================================================================
  /*
   * $Id: EnumConvertor.java,v 1.1 2003/11/06 22:58:37 ugo Exp $
   */
  package org.apache.cocoon.woody.datatype.convertor;
  
  import java.lang.reflect.Method;
  import java.util.Locale;
  
  import org.apache.cocoon.woody.datatype.Enum;
  
  /**
   * Description of EnumConvertor.
   */
  public class EnumConvertor implements Convertor {
  
      private String className;
      
      public EnumConvertor(String className) {
          this.className = className;
      }
      
      /* (non-Javadoc)
       * @see org.apache.cocoon.woody.datatype.convertor.Convertor#convertFromString(java.lang.String,
java.util.Locale, org.apache.cocoon.woody.datatype.convertor.Convertor.FormatCache)
       */
      public Object convertFromString(
          String value,
          Locale locale,
          FormatCache formatCache) {
          try {
              Method method = getTypeClass().
                  getMethod("fromString", new Class[] { String.class, Locale.class});
              return method.invoke(null, new Object[] { value, locale});
          } catch (Exception e) {
              // FIXME: I'd like to throw a o.a.c.ProcessingException here,
              // but unfortunately it's a checked exception.
              // Checked exceptions are evil, aren't they?
              throw new RuntimeException(e);
          }
      }
  
      /* (non-Javadoc)
       * @see org.apache.cocoon.woody.datatype.convertor.Convertor#convertToString(java.lang.Object,
java.util.Locale, org.apache.cocoon.woody.datatype.convertor.Convertor.FormatCache)
       */
      public String convertToString(
          Object value,
          Locale locale,
          FormatCache formatCache) {
          return ((Enum) value).convertToString(locale);
      }
  
      /* (non-Javadoc)
       * @see org.apache.cocoon.woody.datatype.convertor.Convertor#getTypeClass()
       */
      public Class getTypeClass() {
          try {
              // FIXME: use the correct class loader.
              return Class.forName(className);
          }
          catch (ClassNotFoundException e) {
              // FIXME: I'd like to throw a o.a.c.ProcessingException here,
              // but unfortunately it's a checked exception.
              // Checked exceptions are evil, aren't they?
              throw new RuntimeException("Class " + className + " not found", e);
          }
      }
  
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/EnumTypeBuilder.java
  
  Index: EnumTypeBuilder.java
  ===================================================================
  /*
   * $Id: EnumTypeBuilder.java,v 1.1 2003/11/06 22:58:37 ugo Exp $
   */
  package org.apache.cocoon.woody.datatype.typeimpl;
  
  import org.apache.cocoon.woody.datatype.Datatype;
  import org.apache.cocoon.woody.datatype.DatatypeManager;
  import org.w3c.dom.Element;
  
  /**
   * Description of EnumTypeBuilder.
   */
  public class EnumTypeBuilder extends AbstractDatatypeBuilder {
  
      /* (non-Javadoc)
       * @see org.apache.cocoon.woody.datatype.DatatypeBuilder#build(org.w3c.dom.Element,
boolean, org.apache.cocoon.woody.datatype.DatatypeManager)
       */
      public Datatype build(Element datatypeElement,
  						  boolean arrayType,
  						  DatatypeManager datatypeManager) throws Exception {
          EnumType type = new EnumType();
          type.setArrayType(arrayType);
          type.setBuilder(this);
  
          buildValidationRules(datatypeElement, type, datatypeManager);
          buildConvertor(datatypeElement, type);
  
          return type;
      }
  
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/EnumType.java
  
  Index: EnumType.java
  ===================================================================
  /*
   * $Id: EnumType.java,v 1.1 2003/11/06 22:58:37 ugo Exp $
   */
  package org.apache.cocoon.woody.datatype.typeimpl;
  
  /**
   * Description of EnumType.
   */
  public class EnumType extends AbstractDatatype {
      
      public EnumType() {
      }
      
      /* (non-Javadoc)
       * @see org.apache.cocoon.woody.datatype.Datatype#getTypeClass()
       */
      public Class getTypeClass() {
          return this.getConvertor().getTypeClass();
      }
  
      /* (non-Javadoc)
       * @see org.apache.cocoon.woody.datatype.Datatype#getDescriptiveName()
       */
      public String getDescriptiveName() {
          return this.getConvertor().getTypeClass().getName();
      }
  
  }
  
  
  
  1.4       +15 -0     cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/samples/Form2Bean.java
  
  Index: Form2Bean.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/samples/Form2Bean.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Form2Bean.java	12 Aug 2003 12:59:10 -0000	1.3
  +++ Form2Bean.java	6 Nov 2003 22:58:37 -0000	1.4
  @@ -68,8 +68,23 @@
       private Date birthday;
       private int aNumber;
       private boolean choose;
  +    private Sex sex;
   
       private Collection contacts = new ArrayList();
  +
  +    /**
  +     * @return Returns the sex.
  +     */
  +    public Sex getSex() {
  +        return sex;
  +    }
  +
  +    /**
  +     * @param sex The sex to set.
  +     */
  +    public void setSex(Sex sex) {
  +        this.sex = sex;
  +    }
   
       public Form2Bean() {
       }
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/samples/Sex.java
  
  Index: Sex.java
  ===================================================================
  /*
   * $Id: Sex.java,v 1.1 2003/11/06 22:58:37 ugo Exp $
   */
  package org.apache.cocoon.woody.samples;
  
  import java.util.Locale;
  
  import org.apache.cocoon.woody.datatype.Enum;
  
  /**
   * Description of Sex.
   * @version CVS $Id: Sex.java,v 1.1 2003/11/06 22:58:37 ugo Exp $
   */
  public class Sex implements Enum {
  
      public static final Sex MALE = new Sex("M");
      public static final Sex FEMALE = new Sex("F");
      private String code;
  
      private Sex(String code) { this.code = code; }
  
      public String toString() {
        // Will probably have some i18n support here
        switch(code.charAt(0)) {
            case 'M' : return "male";
            case 'F' : return "female";
            default : return "unknown"; // Should never happen
        }
      }
  
      public static Sex fromString(String value, Locale locale) {
          if (value.equals("male")) return Sex.MALE;
          if (value.equals("female")) return Sex.FEMALE;
          return null;
      }
  
      /* (non-Javadoc)
       * @see org.apache.cocoon.woody.datatype.Enum#convertToString(java.lang.Object, java.util.Locale)
       */
      public String convertToString(Locale locale) {
          return toString();
      }
  
  }
  
  
  

Mime
View raw message