klute       2004/06/22 09:11:39

  Modified:    src/java/org/apache/poi/hpsf MutableProperty.java
                        MutableSection.java Property.java PropertySet.java
                        PropertySetFactory.java Section.java
                        UnsupportedVariantTypeException.java
                        VariantSupport.java
                        WritingNotSupportedException.java
  Added:       src/java/org/apache/poi/hpsf Constants.java
                        IllegalVariantTypeException.java
                        VariantTypeException.java
  Log:
  - Fix: HPSF now ensures that string properties are always written with the type 
Variant.VT_LPWSTR instead of Variant.VT_LPSTR if the codepage is 1200 (Unicode).
  - Code cleanup (propertyCount in MutableSection removed).
  - Codepage numbers aggregated in the new class org.apache.poi.hpsf.Constants.
  
  Revision  Changes    Path
  1.5       +4 -5      jakarta-poi/src/java/org/apache/poi/hpsf/MutableProperty.java
  
  Index: MutableProperty.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/java/org/apache/poi/hpsf/MutableProperty.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MutableProperty.java      9 Apr 2004 13:05:16 -0000       1.4
  +++ MutableProperty.java      22 Jun 2004 16:11:39 -0000      1.5
  @@ -1,4 +1,3 @@
  -
   /* ====================================================================
      Copyright 2002-2004   Apache Software Foundation
   
  @@ -22,7 +21,7 @@
   
   /**
    * <p>Adds writing capability to the [EMAIL PROTECTED] Property} class.</p>
  - * 
  + *
    * <p>Please be aware that this class' functionality will be merged into the
    * [EMAIL PROTECTED] Property} class at a later time, so the API will change.</p>
    *
  @@ -46,7 +45,7 @@
       /**
        * <p>Creates a <code>MutableProperty</code> as a copy of an existing
        * <code>Property</code>.</p>
  -     * 
  +     *
        * @param p The property to copy.
        */
       public MutableProperty(final Property p)
  @@ -95,11 +94,11 @@
   
       /**
        * <p>Writes the property to an output stream.</p>
  -     * 
  +     *
        * @param out The output stream to write to.
        * @param codepage The codepage to use for writing non-wide strings
        * @return the number of bytes written to the stream
  -     * 
  +     *
        * @exception IOException if an I/O error occurs
        * @exception WritingNotSupportedException if a variant type is to be
        * written that is not yet supported
  
  
  
  1.10      +3 -6      jakarta-poi/src/java/org/apache/poi/hpsf/MutableSection.java
  
  Index: MutableSection.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/java/org/apache/poi/hpsf/MutableSection.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- MutableSection.java       9 Jun 2004 17:51:51 -0000       1.9
  +++ MutableSection.java       22 Jun 2004 16:11:39 -0000      1.10
  @@ -144,7 +144,6 @@
           for (int i = 0; i < properties.length; i++)
               preprops.add(properties[i]);
           dirty = true;
  -        propertyCount = properties.length;
       }
   
   
  @@ -217,7 +216,6 @@
           removeProperty(id);
           preprops.add(p);
           dirty = true;
  -        propertyCount = preprops.size();
       }
   
   
  @@ -236,7 +234,6 @@
                   break;
               }
           dirty = true;
  -        propertyCount = preprops.size();
       }
   
   
  @@ -362,7 +359,7 @@
                       throw new IllegalPropertySetDataException
                           ("The codepage property (ID = 1) must be an " +
                            "Integer object.");
  -                else if (((Integer) p1).intValue() != Property.CP_UNICODE)
  +                else if (((Integer) p1).intValue() != Constants.CP_UNICODE)
                       throw new IllegalPropertySetDataException
                           ("The codepage property (ID = 1) must be " +
                            "1200 (Unicode).");
  @@ -574,7 +571,7 @@
                * dictionary. HPSF always writes Unicode strings to the
                * dictionary. */
               setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
  -                        new Integer(Property.CP_UNICODE));
  +                        new Integer(Constants.CP_UNICODE));
           }
           else
               /* Setting the dictionary to null means to remove property 0.
  
  
  
  1.19      +4 -8      jakarta-poi/src/java/org/apache/poi/hpsf/Property.java
  
  Index: Property.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/java/org/apache/poi/hpsf/Property.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- Property.java     9 Apr 2004 13:05:16 -0000       1.18
  +++ Property.java     22 Jun 2004 16:11:39 -0000      1.19
  @@ -56,9 +56,6 @@
   public class Property
   {
   
  -    /** <p>Codepage 1200 denotes Unicode.</p> */
  -    public static final int CP_UNICODE = 1200;
  -
       /** <p>The property's ID.</p> */
       protected long id;
   
  @@ -118,9 +115,8 @@
        * @param length The property's type/value pair's length in bytes.
        * @param codepage The section's and thus the property's
        * codepage. It is needed only when reading string values.
  -     * 
        * @exception UnsupportedEncodingException if the specified codepage is not
  -     * supported
  +     * supported.
        */
       public Property(final long id, final byte[] src, final long offset,
                       final int length, final int codepage)
  @@ -210,7 +206,7 @@
                * character set is Unicode or not. */
               StringBuffer b = new StringBuffer((int) sLength);
               for (int j = 0; j < sLength; j++)
  -                if (codepage == CP_UNICODE)
  +                if (codepage == Constants.CP_UNICODE)
                   {
                       final int i1 = o + (j * 2);
                       final int i2 = i1 + 1;
  @@ -222,7 +218,7 @@
               /* Strip 0x00 characters from the end of the string: */
               while (b.length() > 0 && b.charAt(b.length() - 1) == 0x00)
                   b.setLength(b.length() - 1);
  -            if (codepage == CP_UNICODE)
  +            if (codepage == Constants.CP_UNICODE)
               {
                   if (sLength % 2 == 1)
                       sLength++;
  
  
  
  1.17      +7 -5      jakarta-poi/src/java/org/apache/poi/hpsf/PropertySet.java
  
  Index: PropertySet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/java/org/apache/poi/hpsf/PropertySet.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- PropertySet.java  9 Apr 2004 13:05:16 -0000       1.16
  +++ PropertySet.java  22 Jun 2004 16:11:39 -0000      1.17
  @@ -233,11 +233,13 @@
        * @throws IOException if the [EMAIL PROTECTED] InputStream} cannot not be
        * accessed as needed.
        * @exception NoPropertySetStreamException if the input stream does not
  -     * contain a property set
  +     * contain a property set.
  +     * @exception UnsupportedEncodingException if a character encoding is not
  +     * supported.
        */
       public PropertySet(final InputStream stream)
           throws NoPropertySetStreamException, MarkUnsupportedException,
  -               IOException
  +               IOException, UnsupportedEncodingException
       {
           if (isPropertySetStream(stream))
           {
  @@ -265,7 +267,7 @@
        * @throws NoPropertySetStreamException if the byte array is not a
        * property set stream.
        * 
  -     * @exception UnsupportedEncodingException if the codepage is not supported
  +     * @exception UnsupportedEncodingException if the codepage is not supported.
        */
       public PropertySet(final byte[] stream, final int offset, final int length)
           throws NoPropertySetStreamException, UnsupportedEncodingException
  @@ -288,7 +290,7 @@
        * @throws NoPropertySetStreamException if the byte array is not a
        * property set stream.
        * 
  -     * @exception UnsupportedEncodingException if the codepage is not supported
  +     * @exception UnsupportedEncodingException if the codepage is not supported.
        */
       public PropertySet(final byte[] stream)
       throws NoPropertySetStreamException, UnsupportedEncodingException
  
  
  
  1.12      +5 -2      jakarta-poi/src/java/org/apache/poi/hpsf/PropertySetFactory.java
  
  Index: PropertySetFactory.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-poi/src/java/org/apache/poi/hpsf/PropertySetFactory.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- PropertySetFactory.java   9 Apr 2004 13:05:16 -0000       1.11
  +++ PropertySetFactory.java   22 Jun 2004 16:11:39 -0000      1.12
  @@ -19,6 +19,7 @@
   
   import java.io.InputStream;
   import java.io.IOException;
  +import java.io.UnsupportedEncodingException;
   import java.rmi.UnexpectedException;
   
   /**
  @@ -48,10 +49,12 @@
        * @throws MarkUnsupportedException if the stream does not support
        * the <code>mark</code> operation.
        * @throws IOException if some I/O problem occurs.
  +     * @exception UnsupportedEncodingException if the specified codepage is not
  +     * supported.
        */
       public static PropertySet create(final InputStream stream)
           throws NoPropertySetStreamException, MarkUnsupportedException,
  -               IOException
  +               UnsupportedEncodingException, IOException
       {
           final PropertySet ps = new PropertySet(stream);
           try
  
  
  
  1.23      +3 -12     jakarta-poi/src/java/org/apache/poi/hpsf/Section.java
  
  Index: Section.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/java/org/apache/poi/hpsf/Section.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- Section.java      9 Apr 2004 13:05:16 -0000       1.22
  +++ Section.java      22 Jun 2004 16:11:39 -0000      1.23
  @@ -105,22 +105,13 @@
   
   
       /**
  -     * FIXME (2): Get rid of this! The property count is implicitly available as
  -     * the length of the "properties" array.
  -     * 
  -     * @see #getPropertyCount
  -     */
  -    protected int propertyCount;
  -
  -
  -    /**
        * <p>Returns the number of properties in this section.</p>
        *
        * @return The number of properties in this section.
        */
       public int getPropertyCount()
       {
  -        return propertyCount;
  +        return properties.length;
       }
   
   
  @@ -188,7 +179,7 @@
           /*
            * Read the number of properties.
            */
  -        propertyCount = (int) LittleEndian.getUInt(src, o1);
  +        final int propertyCount = (int) LittleEndian.getUInt(src, o1);
           o1 += LittleEndian.INT_SIZE;
   
           /*
  
  
  
  1.4       +5 -34     
jakarta-poi/src/java/org/apache/poi/hpsf/UnsupportedVariantTypeException.java
  
  Index: UnsupportedVariantTypeException.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-poi/src/java/org/apache/poi/hpsf/UnsupportedVariantTypeException.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- UnsupportedVariantTypeException.java      9 Apr 2004 13:05:16 -0000       1.3
  +++ UnsupportedVariantTypeException.java      22 Jun 2004 16:11:39 -0000      1.4
  @@ -1,4 +1,3 @@
  -
   /* ====================================================================
      Copyright 2002-2004   Apache Software Foundation
   
  @@ -31,15 +30,10 @@
    * @since 2003-08-05
    * @version $Id$
    */
  -public abstract class UnsupportedVariantTypeException extends HPSFException
  +public abstract class UnsupportedVariantTypeException
  +extends VariantTypeException
   {
   
  -    private Object value;
  -
  -    private long variantType;
  -
  -
  -
       /**
        * <p>Constructor.</p>
        * 
  @@ -49,38 +43,15 @@
       public UnsupportedVariantTypeException(final long variantType,
                                              final Object value)
       {
  -        super("HPSF does not yet support the variant type " + variantType + 
  +        super(variantType, value,
  +              "HPSF does not yet support the variant type " + variantType + 
                 " (" + Variant.getVariantName(variantType) + ", " +
                 HexDump.toHex(variantType) + "). If you want support for " +
                 "this variant type in one of the next POI releases please " +
                 "submit a request for enhancement (RFE) to " +
                 "<http://nagoya.apache.org/bugzilla/>! Thank you!");
  -        this.variantType = variantType;
  -        this.value = value;
       }
   
   
  -
  -    /**
  -     * <p>Returns the offending variant type.</p>
  -     *
  -     * @return the offending variant type.
  -     */
  -    public long getVariantType()
  -    {
  -        return variantType;
  -    }
  -
  -
  -
  -    /**
  -     * <p>Return the value who's variant type is not yet supported.</p>
  -     *
  -     * @return the value who's variant type is not yet supported
  -     */
  -    public Object getValue()
  -    {
  -        return value;
  -    }
   
   }
  
  
  
  1.10      +30 -13    jakarta-poi/src/java/org/apache/poi/hpsf/VariantSupport.java
  
  Index: VariantSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-poi/src/java/org/apache/poi/hpsf/VariantSupport.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- VariantSupport.java       2 Jun 2004 19:37:27 -0000       1.9
  +++ VariantSupport.java       22 Jun 2004 16:11:39 -0000      1.10
  @@ -1,4 +1,3 @@
  -
   /* ====================================================================
      Copyright 2002-2004   Apache Software Foundation
   
  @@ -14,7 +13,7 @@
      See the License for the specific language governing permissions and
      limitations under the License.
   ==================================================================== */
  -        
  +
   package org.apache.poi.hpsf;
   
   import java.io.IOException;
  @@ -41,7 +40,7 @@
    * <li><p>Reading reads from a byte array while writing writes to an byte array
    * output stream.</p></li>
    *
  - * <ul>
  + * </ul>
    *
    * @author Rainer Klute <a
    * href="mailto:[EMAIL PROTECTED]">&lt;[EMAIL PROTECTED]&gt;</a>
  @@ -126,19 +125,26 @@
        * @exception ReadingNotSupportedException if a property is to be written
        * who's variant type HPSF does not yet support
        * @exception UnsupportedEncodingException if the specified codepage is not
  -     * supported
  +     * supported.
        *
        * @see Variant
        */
       public static Object read(final byte[] src, final int offset,
                                 final int length, final long type,
                                 final int codepage)
  -        throws ReadingNotSupportedException, UnsupportedEncodingException
  +    throws ReadingNotSupportedException, UnsupportedEncodingException
       {
           Object value;
           int o1 = offset;
           int l1 = length - LittleEndian.INT_SIZE;
  -        switch ((int) type)
  +        long lType = type;
  +
  +        /* Instead of trying to read 8-bit characters from a Unicode string,
  +         * read 16-bit characters. */
  +        if (codepage == Constants.CP_UNICODE && type == Variant.VT_LPSTR)
  +            lType = Variant.VT_LPWSTR;
  +
  +        switch ((int) lType)
           {
               case Variant.VT_EMPTY:
               {
  @@ -279,11 +285,11 @@
                   ("Codepage number may not be " + codepage);
           switch (codepage)
           {
  -            case 932:
  +            case Constants.CP_SJIS:
                   return "SJIS";
  -            case 1200:
  +            case Constants.CP_UTF16:
                   return "UTF-16";
  -            case 65001:
  +            case Constants.CP_UTF8:
                   return "UTF-8";
               default:
                   return "cp" + codepage;
  @@ -295,6 +301,11 @@
        * <p>Writes a variant value to an output stream. This method ensures that
        * always a multiple of 4 bytes is written.</p>
        *
  +     * <p>If the codepage is UTF-16, which is encouraged, strings
  +     * <strong>must</strong> always be written as [EMAIL PROTECTED] 
Variant#VT_LPWSTR}
  +     * strings, not as [EMAIL PROTECTED] Variant#VT_LPSTR} strings. This method 
ensure this
  +     * by converting strings appropriately, if needed.</p>
  +     *
        * @param out The stream to write the value to.
        * @param type The variant's type.
        * @param value The variant's value.
  @@ -309,8 +320,14 @@
                               final Object value, final int codepage)
           throws IOException, WritingNotSupportedException
       {
  +        long lType = type;
  +
  +        /* Ensure that wide strings are written if the codepage is Unicode. */
  +        if (codepage == Constants.CP_UNICODE && type == Variant.VT_LPSTR)
  +            lType = Variant.VT_LPWSTR;
  +
           int length = 0;
  -        switch ((int) type)
  +        switch ((int) lType)
           {
               case Variant.VT_BOOL:
               {
  @@ -402,10 +419,10 @@
                       out.write(b);
                       length = b.length;
                       writeUnsupportedTypeMessage
  -                        (new WritingNotSupportedException(type, value));
  +                        (new WritingNotSupportedException(lType, value));
                   }
                   else
  -                    throw new WritingNotSupportedException(type, value);
  +                    throw new WritingNotSupportedException(lType, value);
                   break;
               }
           }
  
  
  
  1.5       +2 -2      
jakarta-poi/src/java/org/apache/poi/hpsf/WritingNotSupportedException.java
  
  Index: WritingNotSupportedException.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-poi/src/java/org/apache/poi/hpsf/WritingNotSupportedException.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- WritingNotSupportedException.java 9 Apr 2004 13:05:16 -0000       1.4
  +++ WritingNotSupportedException.java 22 Jun 2004 16:11:39 -0000      1.5
  @@ -36,7 +36,7 @@
       /**
        * <p>Constructor</p>
        * 
  -     * @param variantType The unsupported varian type.
  +     * @param variantType The unsupported variant type.
        * @param value The value.
        */
       public WritingNotSupportedException(final long variantType,
  
  
  
  1.1                  jakarta-poi/src/java/org/apache/poi/hpsf/Constants.java
  
  Index: Constants.java
  ===================================================================
  package org.apache.poi.hpsf;
  
  /**
   * <p>Defines constants of general use.</p>
   *
   * @author Rainer Klute <a
   * href="mailto:[EMAIL PROTECTED]">&lt;[EMAIL PROTECTED]&gt;</a>
   * @since 2004-06-20
   * @version $Id: Constants.java,v 1.1 2004/06/22 16:11:38 klute Exp $
   */
  public class Constants
  {
      
      /** <p>Codepage for SJIS</p> */
      public static final int CP_SJIS = 932;
  
      /** <p>Codepage for UTF-16</p> */
      public static final int CP_UTF16 = 1200;
  
      /** <p>Codepage for UTF-8</p> */
      public static final int CP_UTF8 = 65001;
  
      /** <p>Codepage for Unicode</p> */
      public static final int CP_UNICODE = CP_UTF16;
  
  }
  
  
  
  1.1                  
jakarta-poi/src/java/org/apache/poi/hpsf/IllegalVariantTypeException.java
  
  Index: IllegalVariantTypeException.java
  ===================================================================
  /* ====================================================================
     Copyright 2002-2004   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.poi.hpsf;
  
  import org.apache.poi.util.HexDump;
  
  /**
   * <p>This exception is thrown if HPSF encounters a variant type that is illegal
   * in the current context.</p>
   * 
   * @author Rainer Klute <a
   * href="mailto:[EMAIL PROTECTED]">&lt;[EMAIL PROTECTED]&gt;</a>
   * @since 2004-06-21
   * @version $Id: IllegalVariantTypeException.java,v 1.1 2004/06/22 16:11:39 klute 
Exp $
   */
  public class IllegalVariantTypeException extends VariantTypeException
  {
  
      /**
       * <p>Constructor</p>
       * 
       * @param variantType The unsupported variant type
       * @param value The value
       * @param msg A message string
       */
      public IllegalVariantTypeException(final long variantType,
                                         final Object value, final String msg)
      {
          super(variantType, value, msg);
      }
  
      /**
       * <p>Constructor</p>
       * 
       * @param variantType The unsupported variant type
       * @param value The value
       */
      public IllegalVariantTypeException(final long variantType,
                                         final Object value)
      {
          this(variantType, value, "The variant type " + variantType + " (" +
               Variant.getVariantName(variantType) + ", " + 
               HexDump.toHex(variantType) + ") is illegal in this context.");
      }
  
  }
  
  
  
  1.1                  
jakarta-poi/src/java/org/apache/poi/hpsf/VariantTypeException.java
  
  Index: VariantTypeException.java
  ===================================================================
  /* ====================================================================
     Copyright 2002-2004   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.poi.hpsf;
  
  /**
   * <p>This exception is thrown if HPSF encounters a problem with a variant type.
   * Concrete subclasses specifiy the problem further.</p>
   *
   * @author Rainer Klute <a
   * href="mailto:[EMAIL PROTECTED]">&lt;[EMAIL PROTECTED]&gt;</a>
   * @since 2004-06-21
   * @version $Id: VariantTypeException.java,v 1.1 2004/06/22 16:11:39 klute Exp $
   */
  public abstract class VariantTypeException extends HPSFException
  {
  
      private Object value;
  
      private long variantType;
  
  
  
      /**
       * <p>Constructor.</p>
       *
       * @param variantType The variant type causing the problem
       * @param value The value who's variant type causes the problem
       * @param msg A message text describing the problem
       */
      public VariantTypeException(final long variantType, final Object value,
                                  final String msg)
      {
          super(msg);
          this.variantType = variantType;
          this.value = value;
      }
  
  
  
      /**
       * <p>Returns the offending variant type.</p>
       *
       * @return the offending variant type.
       */
      public long getVariantType()
      {
          return variantType;
      }
  
  
  
      /**
       * <p>Returns the value who's variant type caused the problem.</p>
       *
       * @return the value who's variant type caused the problem
       */
      public Object getValue()
      {
          return value;
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to