]> source.dussan.org Git - poi.git/commitdiff
Fixes to Unicode support. Believe that most unicode issues are now fixed. Bug 37622...
authorJason Height <jheight@apache.org>
Tue, 3 Jan 2006 11:41:36 +0000 (11:41 +0000)
committerJason Height <jheight@apache.org>
Tue, 3 Jan 2006 11:41:36 +0000 (11:41 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@365609 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/model/Workbook.java
src/java/org/apache/poi/hssf/record/formula/StringPtg.java
src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/testcases/org/apache/poi/hssf/record/TestBoundSheetRecord.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java [new file with mode: 0644]

index afb8e8039abe87f604acb8a61edd308942936ecc..6863f046c4f5cc3d27f0297c13665851e047dc6c 100644 (file)
@@ -457,10 +457,11 @@ public class Workbook implements Model
      * @param sheetnum the sheet number (0 based)
      * @param sheetname the name for the sheet
      */
-
-    // for compatibility
     public void setSheetName(int sheetnum, String sheetname ) {
-        setSheetName( sheetnum, sheetname, (byte)0 );
+        checkSheets(sheetnum);
+        BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum );
+        sheet.setSheetname(sheetname);
+        sheet.setSheetnameLength( (byte)sheetname.length() );
     }
 
     /**
@@ -481,6 +482,14 @@ public class Workbook implements Model
         return false;
     }
 
+    /**
+     * sets the name for a given sheet forcing the encoding. This is STILL A BAD IDEA.
+     * Poi now automatically detects unicode
+     *
+     *@deprecated 3-Jan-06 Simply use setSheetNam e(int sheetnum, String sheetname)
+     * @param sheetnum the sheet number (0 based)
+     * @param sheetname the name for the sheet
+     */    
     public void setSheetName(int sheetnum, String sheetname, short encoding ) {
         checkSheets(sheetnum);
         BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum );
index c15578610b49a4ad2063f9047d5d3ed8448c4e21..96c5427c20c9972a4238cf225cd3175805eb97c6 100644 (file)
@@ -69,8 +69,8 @@ public class StringPtg
         if (value.length() >255) {
             throw new IllegalArgumentException("String literals in formulas cant be bigger than 255 characters ASCII");
         }
-        this.field_2_options=0;
-        this.fHighByte.setBoolean(field_2_options, false);
+        this.field_2_options=0;        
+        field_2_options = (byte)this.fHighByte.setBoolean(field_2_options, StringUtil.hasMultibyte(value));
         this.field_3_string=value;
         this.field_1_length=value.length(); //for the moment, we support only ASCII strings in formulas we create
     }
index f7b97574a933eb84e8dcf262f12152e0446dd51b..c7822c26641e50310abe837c7022c1ea503c588a 100644 (file)
@@ -381,13 +381,13 @@ public class HSSFCell
                         int sst = 0;
 
                         UnicodeString str = getRichStringCellValue().getUnicodeString();
-                        if (encoding == ENCODING_COMPRESSED_UNICODE)
-                        {
-                            str.setCompressedUnicode();
-                        } else if (encoding == ENCODING_UTF_16)
-                        {
-                            str.setUncompressedUnicode();
-                        }
+//jmh                        if (encoding == ENCODING_COMPRESSED_UNICODE)
+//jmh                        {
+//                      jmh                            str.setCompressedUnicode();
+//                      jmh                        } else if (encoding == ENCODING_UTF_16)
+//                      jmh                        {
+//                      jmh                            str.setUncompressedUnicode();
+//                      jmh                        }
                         sst = book.addSSTString(str);
                         lrec.setSSTIndex(sst);
                         getRichStringCellValue().setUnicodeString(book.getSSTString(sst));
@@ -572,13 +572,13 @@ public class HSSFCell
             int index = 0;
 
             UnicodeString str = value.getUnicodeString();            
-            if (encoding == ENCODING_COMPRESSED_UNICODE)
-            {
-                str.setCompressedUnicode();
-            } else if (encoding == ENCODING_UTF_16)
-            {
-                str.setUncompressedUnicode();
-            }
+//          jmh            if (encoding == ENCODING_COMPRESSED_UNICODE)
+//          jmh            {
+//          jmh                str.setCompressedUnicode();
+//          jmh            } else if (encoding == ENCODING_UTF_16)
+//          jmh            {
+//          jmh                str.setUncompressedUnicode();
+//          jmh            }
             index = book.addSSTString(str);            
             (( LabelSSTRecord ) record).setSSTIndex(index);
             stringValue = value;
@@ -856,6 +856,8 @@ public class HSSFCell
      * @see #ENCODING_UTF_16
      *
      * @return -1, 1 or 0 for unchanged, compressed or uncompressed (used only with String type)
+     * 
+     * @deprecated As of 3-Jan-06 POI now automatically handles Unicode without forcing the encoding.
      */
     public short getEncoding()
     {
@@ -870,6 +872,7 @@ public class HSSFCell
      * @see #ENCODING_UTF_16
      *
      * @param encoding either ENCODING_COMPRESSED_UNICODE (0) or ENCODING_UTF_16 (1)
+     * @deprecated As of 3-Jan-06 POI now automatically handles Unicode without forcing the encoding.
      */
 
     public void setEncoding(short encoding)
index fe6559a8c16b04938b99b46c9d06a7def4832218..91b4683f50e6b36f5878f0fc4fe78347debf1189 100644 (file)
@@ -339,7 +339,15 @@ public class HSSFWorkbook
         return workbook.getWindowOne().getDisplayedTab();
     }
 
+    /**
+     * @deprecated POI will now properly handle unicode strings without
+     * forceing an encoding
+     */
     public final static byte ENCODING_COMPRESSED_UNICODE = 0;
+    /**
+     * @deprecated POI will now properly handle unicode strings without
+     * forceing an encoding
+     */
     public final static byte ENCODING_UTF_16             = 1;
 
 
@@ -354,9 +362,23 @@ public class HSSFWorkbook
         if (workbook.doesContainsSheetName( name, sheet ))
             throw new IllegalArgumentException( "The workbook already contains a sheet with this name" );
 
-        workbook.setSheetName( sheet, name, ENCODING_COMPRESSED_UNICODE );
+        if (sheet > (sheets.size() - 1))
+        {
+            throw new RuntimeException("Sheet out of bounds");
+        }
+        
+        workbook.setSheetName( sheet, name);
     }
 
+    
+    /**
+     * set the sheet name forcing the encoding. Forcing the encoding IS A BAD IDEA!!!
+     * @deprecated 3-Jan-2006 POI now automatically detects unicode and sets the encoding
+     * appropriately. Simply use setSheetName(int sheet, String encoding) 
+     * @throws IllegalArgumentException if the name is greater than 31 chars
+     * or contains /\?*[]
+     * @param sheet number (0 based)
+     */    
     public void setSheetName( int sheet, String name, short encoding )
     {
         if (workbook.doesContainsSheetName( name, sheet ))
index 6bb26c612c57a525a086e382ba3f8d1e8c239495..3843082fdf035d5a7b5228eac9eae3f5c31cde1e 100644 (file)
@@ -49,9 +49,8 @@ public class TestBoundSheetRecord
     public void testWideRecordLength()
             throws Exception
     {
-        BoundSheetRecord record = new BoundSheetRecord();
-        record.setCompressedUnicodeFlag((byte)0x01);
-        record.setSheetname("Sheet1");
+        BoundSheetRecord record = new BoundSheetRecord();        
+        record.setSheetname("Sheet\u20ac");
         record.setSheetnameLength((byte)6);
 
         assertEquals(" 2  +  2  +  4  +   2   +    1     +    1    + len(str) * 2", 24, record.getRecordSize());
index e07fe26477fa575422c0bd58d36bd9386e88ecc9..7bf6c47937788bc3cbc436f29f0819d9844792b9 100644 (file)
@@ -46,15 +46,15 @@ public class TestHSSFWorkbook extends TestCase
 
         try
         {
-            b.setSheetName( 3,  "name1", HSSFWorkbook.ENCODING_UTF_16 );
+            b.setSheetName( 3,  "name1"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
             fail();
         }
         catch ( IllegalArgumentException pass )
         {
         }
 
-        b.setSheetName( 3,  "name2", HSSFWorkbook.ENCODING_UTF_16 );
-        b.setSheetName( 3,  "name2", HSSFWorkbook.ENCODING_UTF_16 );
+        b.setSheetName( 3,  "name2"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
+        b.setSheetName( 3,  "name2"/*JMH, HSSFWorkbook.ENCODING_UTF_16*/ );
         b.setSheetName( 3,  "name2" );
         
         HSSFWorkbook c = new HSSFWorkbook( );
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java
new file mode 100644 (file)
index 0000000..5eda00e
--- /dev/null
@@ -0,0 +1,95 @@
+package org.apache.poi.hssf.usermodel;\r
+\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.FileInputStream;\r
+\r
+import org.apache.poi.util.TempFile;\r
+\r
+import junit.framework.TestCase;\r
+\r
+public class TestUnicodeWorkbook extends TestCase {\r
+\r
+    public TestUnicodeWorkbook(String s) {\r
+        super(s);\r
+    }\r
+    \r
+    /** Tests that all of the unicode capable string fields can be set, written and then read back\r
+     * \r
+     *\r
+     */\r
+    public void testUnicodeInAll() throws Exception {\r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        //Create a unicode dataformat (contains euro symbol)\r
+        HSSFDataFormat df = wb.createDataFormat();\r
+        final String formatStr = "_([$\u20ac-2]\\\\\\ * #,##0.00_);_([$\u20ac-2]\\\\\\ * \\\\\\(#,##0.00\\\\\\);_([$\u20ac-2]\\\\\\ *\\\"\\-\\\\\"??_);_(@_)";\r
+        short fmt = df.getFormat(formatStr);\r
+        \r
+        //Create a unicode sheet name (euro symbol)\r
+        HSSFSheet s = wb.createSheet("\u20ac");\r
+        \r
+        //Set a unicode header (you guessed it the euro symbol)\r
+        HSSFHeader h = s.getHeader();\r
+        h.setCenter("\u20ac");\r
+        h.setLeft("\u20ac");\r
+        h.setRight("\u20ac");\r
+        \r
+        //Set a unicode footer\r
+        HSSFFooter f = s.getFooter();\r
+        f.setCenter("\u20ac");\r
+        f.setLeft("\u20ac");\r
+        f.setRight("\u20ac");                \r
+\r
+        HSSFRow r = s.createRow(0);\r
+        HSSFCell c = r.createCell((short)1);\r
+        c.setCellValue(12.34);\r
+        c.getCellStyle().setDataFormat(fmt);\r
+        \r
+        HSSFCell c2 = r.createCell((short)2);\r
+        c.setCellValue(new HSSFRichTextString("\u20ac"));\r
+\r
+        HSSFCell c3 = r.createCell((short)3);\r
+        String formulaString = "TEXT(12.34,\"\u20ac###,##\")";\r
+        c3.setCellFormula(formulaString);\r
+\r
+        \r
+        File tempFile = TempFile.createTempFile("unicode", "test.xls");\r
+        FileOutputStream stream = new FileOutputStream(tempFile);\r
+        wb.write(stream);\r
+        \r
+        wb = null;\r
+        FileInputStream in = new FileInputStream(tempFile);\r
+        wb = new HSSFWorkbook(in);\r
+\r
+        //Test the sheetname\r
+        s = wb.getSheet("\u20ac");\r
+        assertNotNull(s);\r
+        \r
+        //Test the header\r
+        h = s.getHeader();\r
+        assertEquals(h.getCenter(), "\u20ac");\r
+        assertEquals(h.getLeft(), "\u20ac");\r
+        assertEquals(h.getRight(), "\u20ac");\r
+        \r
+        //Test the footer\r
+        f = s.getFooter();\r
+        assertEquals(f.getCenter(), "\u20ac");\r
+        assertEquals(f.getLeft(), "\u20ac");\r
+        assertEquals(f.getRight(), "\u20ac");                \r
+\r
+        //Test the dataformat\r
+        r = s.getRow(0);\r
+        c = r.getCell((short)1);\r
+        df = wb.createDataFormat();\r
+        assertEquals(formatStr, df.getFormat(c.getCellStyle().getDataFormat()));\r
+        \r
+        //Test the cell string value\r
+        c2 = r.getCell((short)2);\r
+        assertEquals(c.getRichStringCellValue().getString(), "\u20ac");\r
+        \r
+        //Test the cell formula\r
+        c3 = r.getCell((short)3);\r
+        assertEquals(c3.getCellFormula(), formulaString);\r
+    }\r
+\r
+}\r