cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl AbstractDatatype.java AbstractDatatypeBuilder.java LongTypeBuilder.java StringType.java StringTypeBuilder.java
Date Tue, 15 Jul 2003 14:09:26 GMT
bruno       2003/07/15 07:09:26

  Modified:    src/blocks/woody/java/org/apache/cocoon/woody/datatype
                        Datatype.java DatatypeBuilder.java
               src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl
                        AbstractDatatype.java AbstractDatatypeBuilder.java
                        LongTypeBuilder.java StringType.java
                        StringTypeBuilder.java
  Log:
  Various changes/cleanup to Datatype interface.
  Introduce use of Convertors.
  
  Revision  Changes    Path
  1.2       +33 -6     cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/Datatype.java
  
  Index: Datatype.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/Datatype.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Datatype.java	22 Apr 2003 12:04:18 -0000	1.1
  +++ Datatype.java	15 Jul 2003 14:09:25 -0000	1.2
  @@ -51,6 +51,7 @@
   package org.apache.cocoon.woody.datatype;
   
   import org.outerj.expression.ExpressionContext;
  +import org.apache.cocoon.woody.datatype.convertor.Convertor;
   
   import java.util.Locale;
   
  @@ -70,14 +71,23 @@
    * an array of objects. See also {@link #isArrayType}.
    */
   public interface Datatype {
  -    public Object convertFromString(String value);
  -    public Object convertFromStringLocalized(String value, Locale locale);
  -    public String convertToString(Object value);
  -    public String convertToStringLocalized(Object value, Locale locale);
  -    // TODO validate moet wellicht ook nog een context meekrijgen
  +    /**
  +     * Converts a string to an object of this datatype. Returns null if this
  +     * fails. This method uses the same {@link Convertor} as returned by the
  +     * {@link #getConvertor} method.
  +     */
  +    public Object convertFromString(String value, Locale locale);
   
       /**
  -     * Returns null if validation is successful, otherwise returns the failmessage key.
  +     * Converts an object of this datatype to a string representation.
  +     * This method uses the same {@link Convertor} as returned by the
  +     * {@link #getConvertor} method.
  +     */
  +    public String convertToString(Object value, Locale locale);
  +
  +    /**
  +     * Returns null if validation is successful, otherwise returns a
  +     * {@link ValidationError} instance.
        *
        * @param value an Object of the correct type for this datatype (see {@link #getTypeClass},
or
        * if {@link #isArrayType} returns true, an array of objects of that type.
  @@ -111,4 +121,21 @@
        * required.
        */
       public boolean isArrayType();
  +
  +    /**
  +     * Returns the convertor used by this datatype.
  +     */
  +    public Convertor getConvertor();
  +
  +    /**
  +     * Returns the "plain convertor". This is convertor that should have a locale-independent
  +     * string encoding, and guarantees perfect roundtripping. It is used if a value of
this
  +     * datatype needs to be stored but not displayed to the user.
  +     */
  +    public Convertor getPlainConvertor();
  +
  +    /**
  +     * Returns the factory that built this datatype.
  +     */
  +    public DatatypeBuilder getBuilder();
   }
  
  
  
  1.2       +5 -0      cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/DatatypeBuilder.java
  
  Index: DatatypeBuilder.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/DatatypeBuilder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DatatypeBuilder.java	22 Apr 2003 12:04:18 -0000	1.1
  +++ DatatypeBuilder.java	15 Jul 2003 14:09:25 -0000	1.2
  @@ -51,6 +51,7 @@
   package org.apache.cocoon.woody.datatype;
   
   import org.w3c.dom.Element;
  +import org.apache.cocoon.woody.datatype.convertor.Convertor;
   
   /**
    * Implementations of this class build a Datatype from an XML description
  @@ -63,4 +64,8 @@
    */
   public interface DatatypeBuilder {
       public Datatype build(Element datatypeElement, boolean arrayType, DatatypeManager datatypeManager)
throws Exception;
  +
  +    public Convertor getPlainConvertor();
  +
  +    public Convertor buildConvertor(Element convertorEl) throws Exception;
   }
  
  
  
  1.3       +35 -9     cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/AbstractDatatype.java
  
  Index: AbstractDatatype.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/AbstractDatatype.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractDatatype.java	26 Jun 2003 15:32:34 -0000	1.2
  +++ AbstractDatatype.java	15 Jul 2003 14:09:25 -0000	1.3
  @@ -51,14 +51,10 @@
   package org.apache.cocoon.woody.datatype.typeimpl;
   
   import org.outerj.expression.ExpressionContext;
  -import org.apache.cocoon.woody.datatype.Datatype;
  -import org.apache.cocoon.woody.datatype.SelectionList;
  -import org.apache.cocoon.woody.datatype.ValidationError;
  -import org.apache.cocoon.woody.datatype.ValidationRule;
  -
  -import java.util.List;
  -import java.util.ArrayList;
  -import java.util.Iterator;
  +import org.apache.cocoon.woody.datatype.*;
  +import org.apache.cocoon.woody.datatype.convertor.Convertor;
  +
  +import java.util.*;
   
   /**
    * Abstract base class for Datatype implementations. Most concreate datatypes
  @@ -68,6 +64,8 @@
       private List validationRules = new ArrayList();
       private SelectionList selectionList;
       private boolean arrayType = false;
  +    private DatatypeBuilder builder;
  +    private Convertor convertor;
   
       public ValidationError validate(Object value, ExpressionContext expressionContext)
{
           Iterator validationRulesIt = validationRules.iterator();
  @@ -98,7 +96,35 @@
           return arrayType;
       }
   
  -    protected void setArrayType(boolean arrayType) {
  +    void setArrayType(boolean arrayType) {
           this.arrayType = arrayType;
  +    }
  +
  +    void setConvertor(Convertor convertor) {
  +        this.convertor = convertor;
  +    }
  +
  +    void setBuilder(DatatypeBuilder builder) {
  +        this.builder = builder;
  +    }
  +
  +    public Convertor getPlainConvertor() {
  +        return builder.getPlainConvertor();
  +    }
  +
  +    public DatatypeBuilder getBuilder() {
  +        return builder;
  +    }
  +
  +    public Convertor getConvertor() {
  +        return convertor;
  +    }
  +
  +    public Object convertFromString(String value, Locale locale) {
  +        return getConvertor().convertFromString(value, locale, null);
  +    }
  +
  +    public String convertToString(Object value, Locale locale) {
  +        return getConvertor().convertToString(value, locale, null);
       }
   }
  
  
  
  1.4       +53 -1     cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/AbstractDatatypeBuilder.java
  
  Index: AbstractDatatypeBuilder.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/AbstractDatatypeBuilder.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AbstractDatatypeBuilder.java	26 Jun 2003 15:34:40 -0000	1.3
  +++ AbstractDatatypeBuilder.java	15 Jul 2003 14:09:25 -0000	1.4
  @@ -51,11 +51,18 @@
   package org.apache.cocoon.woody.datatype.typeimpl;
   
   import org.apache.cocoon.woody.datatype.*;
  +import org.apache.cocoon.woody.datatype.convertor.Convertor;
  +import org.apache.cocoon.woody.datatype.convertor.ConvertorBuilder;
   import org.apache.cocoon.woody.util.DomHelper;
  +import org.apache.cocoon.woody.util.SimpleServiceSelector;
   import org.apache.cocoon.woody.Constants;
   import org.apache.avalon.framework.component.Composable;
   import org.apache.avalon.framework.component.ComponentManager;
   import org.apache.avalon.framework.component.ComponentException;
  +import org.apache.avalon.framework.configuration.Configurable;
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.service.ServiceException;
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceResolver;
   import org.w3c.dom.Element;
  @@ -68,11 +75,56 @@
    * Abstract base class for datatype builders, most concrete datatype builders
    * will derive from this class.
    */
  -public abstract class AbstractDatatypeBuilder implements DatatypeBuilder, Composable {
  +public abstract class AbstractDatatypeBuilder implements DatatypeBuilder, Composable, Configurable
{
       protected ComponentManager componentManager;
  +    private SimpleServiceSelector convertorBuilders;
  +    private String defaultConvertorHint;
  +    private Convertor plainConvertor;
   
       public void compose(ComponentManager componentManager) throws ComponentException {
           this.componentManager = componentManager;
  +    }
  +
  +    public void configure(Configuration configuration) throws ConfigurationException {
  +        convertorBuilders = new SimpleServiceSelector("convertor", ConvertorBuilder.class);
  +        Configuration convertorsConf = configuration.getChild("convertors");
  +        convertorBuilders.configure(convertorsConf);
  +        defaultConvertorHint = convertorsConf.getAttribute("default");
  +
  +        String plainConvertorHint = convertorsConf.getAttribute("plain");
  +        ConvertorBuilder plainConvertorBuilder;
  +        try {
  +            plainConvertorBuilder = (ConvertorBuilder)convertorBuilders.select(plainConvertorHint);
  +        } catch (ServiceException e) {
  +            throw new ConfigurationException("Convertor defined in plain attribute unavailable.",
e);
  +        }
  +
  +        try {
  +            plainConvertor = plainConvertorBuilder.build(null);
  +        } catch (Exception e) {
  +            throw new ConfigurationException("Error create plain convertor.", e);
  +        }
  +    }
  +
  +    public void buildConvertor(Element datatypeEl, AbstractDatatype datatype) throws Exception
{
  +        Element convertorEl = DomHelper.getChildElement(datatypeEl, Constants.WD_NS, "convertor",
false);
  +        Convertor convertor = buildConvertor(convertorEl);
  +        datatype.setConvertor(convertor);
  +    }
  +
  +    public Convertor buildConvertor(Element convertorEl) throws Exception {
  +        String type = null;
  +        // convertor configuration is allowed to be null, so check that it is not null
  +        if (convertorEl != null)
  +            type = convertorEl.getAttribute("type");
  +        if (type == null || type.equals(""))
  +            type = defaultConvertorHint;
  +        ConvertorBuilder convertorBuilder = (ConvertorBuilder)convertorBuilders.select(type);
  +        return convertorBuilder.build(convertorEl);
  +    }
  +
  +    public Convertor getPlainConvertor() {
  +        return plainConvertor;
       }
   
       protected Source resolve(String src) throws ComponentException, IOException {
  
  
  
  1.2       +2 -5      cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/LongTypeBuilder.java
  
  Index: LongTypeBuilder.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/LongTypeBuilder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LongTypeBuilder.java	22 Apr 2003 12:04:18 -0000	1.1
  +++ LongTypeBuilder.java	15 Jul 2003 14:09:25 -0000	1.2
  @@ -60,13 +60,10 @@
       public Datatype build(Element datatypeElement, boolean arrayType, DatatypeManager datatypeManager)
throws Exception {
           LongType type = new LongType();
           type.setArrayType(arrayType);
  +        type.setBuilder(this);
   
           buildValidationRules(datatypeElement, type, datatypeManager);
  -
  -        // TODO number formatting patterns
  -
  -        // TODO default value
  -
  +        buildConvertor(datatypeElement, type);
           buildSelectionList(datatypeElement, type);
   
           return type;
  
  
  
  1.2       +0 -17     cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/StringType.java
  
  Index: StringType.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/StringType.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StringType.java	22 Apr 2003 12:04:18 -0000	1.1
  +++ StringType.java	15 Jul 2003 14:09:25 -0000	1.2
  @@ -50,28 +50,11 @@
   */
   package org.apache.cocoon.woody.datatype.typeimpl;
   
  -import java.util.Locale;
  -
   /**
    * A {@link org.apache.cocoon.woody.datatype.Datatype Datatype} implementation
    * for strings.
    */
   public class StringType extends AbstractDatatype {
  -    public Object convertFromString(String value) {
  -        return value;
  -    }
  -
  -    public Object convertFromStringLocalized(String value, Locale locale) {
  -        return value;
  -    }
  -
  -    public String convertToString(Object value) {
  -        return (String)value;
  -    }
  -
  -    public String convertToStringLocalized(Object value, Locale locale) {
  -        return (String)value;
  -    }
   
       public Class getTypeClass() {
           return String.class;
  
  
  
  1.2       +2 -0      cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/StringTypeBuilder.java
  
  Index: StringTypeBuilder.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/typeimpl/StringTypeBuilder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StringTypeBuilder.java	22 Apr 2003 12:04:18 -0000	1.1
  +++ StringTypeBuilder.java	15 Jul 2003 14:09:25 -0000	1.2
  @@ -63,8 +63,10 @@
       public Datatype build(Element datatypeElement, boolean arrayType, DatatypeManager datatypeManager)
throws Exception {
           StringType type = new StringType();
           type.setArrayType(arrayType);
  +        type.setBuilder(this);
   
           buildValidationRules(datatypeElement, type, datatypeManager);
  +        buildConvertor(datatypeElement, type);
           buildSelectionList(datatypeElement, type);
   
           return type;
  
  
  

Mime
View raw message