]> source.dussan.org Git - poi.git/commitdiff
Add patch from bug #49690 - Add WorkbookUtil, which provies a way of generating valid...
authorNick Burch <nick@apache.org>
Wed, 4 Aug 2010 14:13:42 +0000 (14:13 +0000)
committerNick Burch <nick@apache.org>
Wed, 4 Aug 2010 14:13:42 +0000 (14:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@982260 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java
src/java/org/apache/poi/hssf/record/BoundSheetRecord.java
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/java/org/apache/poi/ss/util/WorkbookUtil.java [new file with mode: 0644]
src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java [new file with mode: 0644]

index b554d8cea5f4ac045be5f430e554446f0364f5b7..effa0239f0e2f00cbf3f33149b60840fc74e759b 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta2" date="2010-??-??">
+           <action dev="POI-DEVELOPERS" type="add">49690 - Add WorkbookUtil, which provies a way of generating valid sheet names</action>
            <action dev="POI-DEVELOPERS" type="fix">49694 - Use DataFormatter when autosizing columns, to better match the real display width of formatted cells</action>
            <action dev="POI-DEVELOPERS" type="add">49441 - Allow overriding and guessing of HSMF non-unicode string encodings</action>
            <action dev="POI-DEVELOPERS" type="fix">49689 - Allow the setting of user style names on newly created HSSF cell styles</action>
index 5fdf2dd30d3c2129b6ef2dafd95644d86cae733e..40754feec4d3c1fac44355320b409ca51e2c6fe6 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel.examples;
 
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.ss.util.WorkbookUtil;
 
 import java.io.IOException;
 import java.io.FileOutputStream;
@@ -33,7 +34,8 @@ public class NewSheet {
         HSSFWorkbook wb = new HSSFWorkbook();
         HSSFSheet sheet1 = wb.createSheet("new sheet");
         HSSFSheet sheet2 = wb.createSheet(); // create with default name
-        wb.setSheetName(1, "second sheet"); // setting sheet name later
+        final String name = "second sheet";
+        wb.setSheetName(1, WorkbookUtil.createSafeSheetName(name)); // setting sheet name later
         FileOutputStream fileOut = new FileOutputStream("workbook.xls");
         wb.write(fileOut);
         fileOut.close();
index b8b6d72ca367171e90b49d79517244fbdb633d22..7f73a1dd1874c778437ec3584faad5cb0ddb3a54 100644 (file)
@@ -84,6 +84,8 @@ public final class BoundSheetRecord extends StandardRecord {
        /**
         * Set the sheetname for this sheet.  (this appears in the tabs at the bottom)
         * @param sheetName the name of the sheet
+        * @see {@link org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)}
+        *      for a safe way to create valid names
         * @throws IllegalArgumentException if sheet name will cause excel to crash.
         */
        public void setSheetname(String sheetName) {
index de1cb5055089a924d2cc6c6bb07e3d48958648c9..4d5e752ad31ce464b13c5ecfb599c2de9230d83a 100644 (file)
@@ -725,7 +725,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
      *
      * @param sheetname the name for the new sheet. Note - certain length limits
      * apply. See {@link #setSheetName(int, String)}.
-     *
+     * @see {@link org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)}
+        *      for a safe way to create valid names
      * @return HSSFSheet representing the new sheet.
      * @throws IllegalArgumentException
      *             if there is already a sheet present with a case-insensitive
diff --git a/src/java/org/apache/poi/ss/util/WorkbookUtil.java b/src/java/org/apache/poi/ss/util/WorkbookUtil.java
new file mode 100644 (file)
index 0000000..11e01a0
--- /dev/null
@@ -0,0 +1,77 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You 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.ss.util;
+
+
+/**
+ * Helper methods for when working with Usermodel Workbooks
+ */
+public class WorkbookUtil {
+       
+       /**
+        * Creates a valid sheet name, which is conform to the rules.
+        * In any case, the result safely can be used for 
+        * {@link org.apache.poi.hssf.usermodel.HSSFWorkbook#setSheetName(int, String)}.
+        * <br>
+        * Rules:
+        * <ul>
+        * <li>never null</li>
+        * <li>minimum length is 1</li>
+        * <li>maximum length is 31</li>
+        * <li>doesn't contain special chars: / \ ? * ] [ </li>
+        * <li>Sheet names must not begin or end with ' (apostrophe)</li>
+        * </ul>
+        * Invalid characters are replaced by one space character ' '.
+        * 
+        * @param nameProposal can be any string, will be truncated if necessary,
+        *        allowed to be null
+        * @return a valid string, "empty" if to short, "null" if null         
+        */
+       public final static String createSafeSheetName(final String nameProposal) {
+               if (nameProposal == null) {
+                       return "null";
+               }
+               if (nameProposal.length() < 1) {
+                       return "empty";
+               }
+               final int length = Math.min(31, nameProposal.length());
+               final String shortenname = nameProposal.substring(0, length);
+               final StringBuilder result = new StringBuilder(shortenname);
+               for (int i=0; i<length; i++) {
+                       char ch = result.charAt(i);
+                       switch (ch) {
+                               case '/':
+                               case '\\':
+                               case '?':
+                               case '*':
+                               case ']':
+                               case '[':
+                                       result.setCharAt(i, ' ');
+                                       break;
+                               case '\'':
+                                       if (i==0 || i==length-1) {
+                                               result.setCharAt(i, ' ');
+                                       }
+                                       break;
+                               default:
+                                       // all other chars OK
+                       }
+               }
+               return result.toString();
+       }
+}
diff --git a/src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java b/src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java
new file mode 100644 (file)
index 0000000..c1e49ca
--- /dev/null
@@ -0,0 +1,84 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You 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.ss.util;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests WorkbookUtil.
+ *
+ * @see org.apache.poi.ss.util.WorkbookUtil
+ */
+public final class TestWorkbookUtil extends TestCase {
+       /**
+        * borrowed test cases from 
+        * {@link org.apache.poi.hssf.record.TestBoundSheetRecord#testValidNames()}
+        */
+       public void testCreateSafeNames() {
+               
+               String p = "Sheet1";
+               String actual = WorkbookUtil.createSafeSheetName(p);
+               assertEquals(p, actual);
+               
+               p = "O'Brien's sales";
+               actual = WorkbookUtil.createSafeSheetName(p);
+               assertEquals(p, actual);
+               
+               p = " data # ";
+               actual = WorkbookUtil.createSafeSheetName(p);
+               assertEquals(p, actual);
+               
+               p = "data $1.00";
+               actual = WorkbookUtil.createSafeSheetName(p);
+               assertEquals(p, actual);
+               
+               // now the replaced versions ...
+               actual = WorkbookUtil.createSafeSheetName("data?");
+               assertEquals("data ", actual);
+               
+               actual = WorkbookUtil.createSafeSheetName("abc/def");
+               assertEquals("abc def", actual);
+               
+               actual = WorkbookUtil.createSafeSheetName("data[0]");
+               assertEquals("data 0 ", actual);
+               
+               actual = WorkbookUtil.createSafeSheetName("data*");
+               assertEquals("data ", actual);
+               
+               actual = WorkbookUtil.createSafeSheetName("abc\\def");
+               assertEquals("abc def", actual);
+               
+               actual = WorkbookUtil.createSafeSheetName("'data");
+               assertEquals(" data", actual);
+               
+               actual = WorkbookUtil.createSafeSheetName("data'");
+               assertEquals("data ", actual);
+               
+               actual = WorkbookUtil.createSafeSheetName("d'at'a");
+               assertEquals("d'at'a", actual);
+               
+               actual = WorkbookUtil.createSafeSheetName(null);
+               assertEquals("null", actual);
+               
+               actual = WorkbookUtil.createSafeSheetName("");
+               assertEquals("empty", actual);
+               
+               actual = WorkbookUtil.createSafeSheetName("1234567890123456789012345678901TOOLONG");
+               assertEquals("1234567890123456789012345678901", actual);
+       }
+}