cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nicola...@apache.org
Subject cvs commit: xml-cocoon2/src/java/org/apache/cocoon/serialization HSSFSerializer.java
Date Fri, 26 Apr 2002 14:09:22 GMT
nicolaken    02/04/26 07:09:22

  Modified:    src/documentation/xdocs/userdocs index.xml
               src/documentation/xdocs/userdocs/serializers
                        xls-serializer.xml
               src/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf
                        HSSFElementProcessorFactory.java
               src/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf/elements
                        Cell.java EPCell.java
               src/java/org/apache/cocoon/serialization HSSFSerializer.java
  Added:       src/java/org/apache/cocoon/components/elementprocessor
                        LocaleAware.java
  Log:
  [PATCH] HSSF internazionalization problems with numbers Bug#: 8264
  by Andy Oliver (acoliver@apache.org)
  
  Revision  Changes    Path
  1.3       +1 -1      xml-cocoon2/src/documentation/xdocs/userdocs/index.xml
  
  Index: index.xml
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/documentation/xdocs/userdocs/index.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- index.xml	18 Apr 2002 16:18:04 -0000	1.2
  +++ index.xml	26 Apr 2002 14:09:22 -0000	1.3
  @@ -18,7 +18,7 @@
     concepts, but a number of details as well.  Read the Introduction and ensure that you
     understand concepts such as the sitemap, generators, transformers, serializers
     and actions.  This guide will fill in the details, such as "So how do I turn
  -  this query into XML date".
  +  this query into XML data".
     </p>
     
     <p>
  
  
  
  1.2       +11 -3     xml-cocoon2/src/documentation/xdocs/userdocs/serializers/xls-serializer.xml
  
  Index: xls-serializer.xml
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/documentation/xdocs/userdocs/serializers/xls-serializer.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- xls-serializer.xml	18 Apr 2002 16:21:10 -0000	1.1
  +++ xls-serializer.xml	26 Apr 2002 14:09:22 -0000	1.2
  @@ -30,7 +30,7 @@
                              and then refactor it into an XSLT page.
                              </p>
                              <p>The HSSF Serializer supports most of the
  -                           functionalities supported by the 
  +                           functionality supplied by the 
                              <link href="http://jakarta.apache.org/poi/hssf">HSSF 
                              API</link> which is part of the <link 
                              href="http://jakarta.apache.org/poi">Jakarta POI
  @@ -47,10 +47,18 @@
                           need a sitemap of course.  Once you have that well,
                           you're half there.  Add </p>
                           <source>
  -                        &lt;map:serializer name="xls" src="org.apache.cocoon.serialization.HSSFSerializer"
mime-type="application/vnd.ms-excel"/&gt;
  +                        &lt;map:serializer name="xls" src="org.apache.cocoon.serialization.HSSFSerializer"
mime-type="application/vnd.ms-excel" locale="us"/&gt;
                           </source>
                           <p>
  -                        to the sitemap between the map:serializers tags.  Next,
  +                        to the sitemap between the map:serializers tags.  The locale is
optional
  +                        and is used only to validate numbers.  Please note that numbers
not in US-default
  +                        format may mot be compatible with Gnumeric (its less cosmopolitan
then the 
  +                        HSSF Serializer ;-) ).  Setting the locale lets you use default
number formats
  +                        from other locales.  Set this to a two letter lowercase country
code.  See 
  +                        java.util.Locale for details. 
  +                        </p>
  +                        <p>
  +                        Next,
                           set up an entry for each url or set of urls (via
                           matching rules) resembling this:
                           </p>
  
  
  
  1.1                  xml-cocoon2/src/java/org/apache/cocoon/components/elementprocessor/LocaleAware.java
  
  Index: LocaleAware.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.components.elementprocessor;
  
  /**
   * The LocaleAware interface is for element processors whom require the locale
   * configuration string to control their behavior.  For HSSF this is somewhat
   * of a kludge to get past the fact the Gnumeric XML format does not allow
   * numbers formatted according to different locales in the &lt;Cell&gt; tags.
   * However, the ESQL generator for instance will generate them no other way.
   * 
   * @author Andrew C. Oliver (acoliver@apache.org)
   */
  public interface LocaleAware
  {
      /**
       * Set the locale for a given element processor.
       */
      public void setLocale(String locale);
  }   // end public interface LocaleAware
  
  
  
  1.2       +9 -2      xml-cocoon2/src/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf/HSSFElementProcessorFactory.java
  
  Index: HSSFElementProcessorFactory.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf/HSSFElementProcessorFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HSSFElementProcessorFactory.java	6 Mar 2002 16:58:02 -0000	1.1
  +++ HSSFElementProcessorFactory.java	26 Apr 2002 14:09:22 -0000	1.2
  @@ -66,14 +66,16 @@
   public class HSSFElementProcessorFactory
       extends AbstractElementProcessorFactory
   {
  -
  +    //holds the locale configuration
  +    String locale;
       /**
        * default constructor
        */
   
  -    public HSSFElementProcessorFactory()
  +    public HSSFElementProcessorFactory(String locale)
       {
           super();
  +        this.locale=locale;
           addElementProcessorProgenitor("Attribute", EPAttribute.class);
           addElementProcessorProgenitor("Attributes", EPAttributes.class);
           addElementProcessorProgenitor("Bottom", EPBottom.class);
  @@ -174,6 +176,11 @@
           try
           {
               rval = createNewElementProcessorInstance(( Class ) progenitor);
  +
  +            //every locale aware element processor is passed the locale string
  +            if (rval instanceof LocaleAware) {
  +                   ((LocaleAware)rval).setLocale(locale);
  +            }
           }
           catch (ClassCastException e)
           {
  
  
  
  1.2       +28 -2     xml-cocoon2/src/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf/elements/Cell.java
  
  Index: Cell.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf/elements/Cell.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Cell.java	6 Mar 2002 16:58:02 -0000	1.1
  +++ Cell.java	26 Apr 2002 14:09:22 -0000	1.2
  @@ -58,6 +58,11 @@
   import org.apache.poi.hssf.usermodel.HSSFCellStyle;
   
   import java.io.IOException;
  +import java.text.DecimalFormat;
  +import java.text.NumberFormat;
  +import java.text.ParseException;
  +import java.util.Locale;
  +
   
   /**
    * internal representation of a Cell
  @@ -72,6 +77,7 @@
   
       // original CellType value
       private int      _celltype;
  +    private Locale   locale; 
   
       /**
        * Constructor Cell
  @@ -86,6 +92,15 @@
       }
   
       /**
  +     * if there is a locale that can be used for validation it is
  +     * set here.  Cell expects a fully constructed locale.  It must 
  +     * be passed in before SetContent can be called.
  +     */
  +    void setLocale(Locale locale) {
  +          this.locale = locale;
  +    }
  +
  +    /**
        * set content
        *
        * @param content the value of the cell, as a string
  @@ -101,8 +116,16 @@
               try
               {
                   if (_celltype == CellType.CELL_TYPE_FLOAT)
  -                {
  -                    _cell.setCellValue(Double.parseDouble(content));
  +                {       // if there is a locale set then we'll use it to 
  +                        // parse the string form of the number... otherwise
  +                        // we'll use the default.
  +                        NumberFormat form = null;
  +                        if (locale == null) { 
  +                            form = NumberFormat.getInstance(); 
  +                        } else { 
  +                            form = NumberFormat.getInstance(locale); 
  +                        }
  +                        _cell.setCellValue(form.parse(content).doubleValue());
                   }
                   else
                   {
  @@ -113,6 +136,9 @@
               {
                   throw new IOException("Invalid value for a numeric cell: "
                                         + content);
  +            } 
  +            catch (ParseException e) {
  +                throw new IOException("Invalid value for a numberic cell: " + content);
               }
           }
           else if (_cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
  
  
  
  1.4       +14 -1     xml-cocoon2/src/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf/elements/EPCell.java
  
  Index: EPCell.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf/elements/EPCell.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- EPCell.java	12 Apr 2002 14:01:45 -0000	1.3
  +++ EPCell.java	26 Apr 2002 14:09:22 -0000	1.4
  @@ -53,12 +53,14 @@
   
   import org.apache.cocoon.components.elementprocessor.types.Attribute;
   import org.apache.cocoon.components.elementprocessor.ElementProcessor;
  +import org.apache.cocoon.components.elementprocessor.LocaleAware;
   import org.apache.cocoon.components.elementprocessor.types.NumericConverter;
   import org.apache.cocoon.components.elementprocessor.types.NumericResult;
   import org.apache.cocoon.components.elementprocessor.types.Validator;
   import org.apache.poi.hssf.usermodel.HSSFCell;
   
   import java.io.IOException;
  +import java.util.Locale;
   
   /**
    * implementation of ElementProcessor to handle the "Cell" tag
  @@ -69,7 +71,7 @@
    */
   
   public class EPCell
  -extends BaseElementProcessor {
  +extends BaseElementProcessor implements LocaleAware {
       private Cell                   _cell;
       private NumericResult          _col;
       private NumericResult          _row;
  @@ -90,6 +92,7 @@
       private static final String    _rows_attribute         = "Rows";
       private static final String    _value_type_attribute   = "ValueType";
       private static final String    _value_format_attribute = "ValueFormat";
  +    private String                 locale; // the locale for this EPCell
       private static final Validator _cell_type_validator    = new Validator() {
           public IOException validate(final Number number) {
               return CellType.isValid(number.intValue()) ? null
  @@ -288,6 +291,11 @@
       
       public void endProcessing() throws IOException {
           String content = getContent();
  +        if (content != null && locale != null) {
  +            // if there is a locale then set it (otherwise the default locale
  +            // will be used
  +            getCell().setLocale(new Locale(locale, locale.toUpperCase()));
  +        }
           if (content != null && !content.trim().equals("")) {
               getCell().setContent(getContent());
           }
  @@ -301,6 +309,11 @@
       
       protected Cell getCell() {
           return _cell;
  +    }
  +
  +    // from LocaleAware - set the locale for a cell
  +    public void setLocale(String locale) {
  +        this.locale=locale;
       }
       
       
  
  
  
  1.4       +17 -2     xml-cocoon2/src/java/org/apache/cocoon/serialization/HSSFSerializer.java
  
  Index: HSSFSerializer.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/serialization/HSSFSerializer.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- HSSFSerializer.java	25 Mar 2002 16:44:31 -0000	1.3
  +++ HSSFSerializer.java	26 Apr 2002 14:09:22 -0000	1.4
  @@ -52,6 +52,9 @@
   
   import org.apache.avalon.framework.activity.Initializable;
   import org.apache.avalon.framework.component.ComponentSelector;
  +import org.apache.avalon.framework.configuration.Configurable;
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
   
   import org.apache.cocoon.components.elementprocessor.ElementProcessor;
   import org.apache.cocoon.components.elementprocessor.ElementProcessorFactory;
  @@ -66,10 +69,11 @@
    */
   
   public class HSSFSerializer
  -      extends POIFSSerializer implements Initializable
  +      extends POIFSSerializer implements Initializable, Configurable
   {
     private ElementProcessorFactory _element_processor_factory;
     private final static String _mime_type = "vnd.ms-excel";
  +  String locale;
   
     /**
      *  Constructor
  @@ -88,8 +92,19 @@
      */
     public void initialize() throws Exception{
   
  -    _element_processor_factory  = new HSSFElementProcessorFactory();
  +    _element_processor_factory  = new HSSFElementProcessorFactory(locale);
       setupLogger(_element_processor_factory);
  +  }
  +
  +  public void configure(Configuration conf) throws ConfigurationException {
  +        Configuration[] parameters = conf.getChildren("parameter");
  +        for (int i = 0; i < parameters.length; i++) {
  +            String name = parameters[i].getAttribute("name");
  +            if (name.trim().equals("locale")) {
  +		locale = parameters[i].getAttribute("value");
  +            }
  +        }
  +
     }
           
     /**
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org


Mime
View raw message