Author: nick
Date: Wed Aug  4 14:13:42 2010
New Revision: 982260

URL: http://svn.apache.org/viewvc?rev=982260&view=rev
Log:
Add patch from bug #49690 - Add WorkbookUtil, which provies a way of generating 
valid sheet names

Added:
    poi/trunk/src/java/org/apache/poi/ss/util/WorkbookUtil.java
    poi/trunk/src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java
Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    
poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java
    poi/trunk/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=982260&r1=982259&r2=982260&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Wed Aug  4 14:13:42 
2010
@@ -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>

Modified: 
poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java?rev=982260&r1=982259&r2=982260&view=diff
==============================================================================
--- 
poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java 
(original)
+++ 
poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/NewSheet.java 
Wed Aug  4 14:13:42 2010
@@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel.ex
 
 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();

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java?rev=982260&r1=982259&r2=982260&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java 
(original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java Wed Aug 
 4 14:13:42 2010
@@ -84,6 +84,8 @@ public final class BoundSheetRecord exte
        /**
         * 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) {

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java?rev=982260&r1=982259&r2=982260&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java 
(original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java Wed Aug  
4 14:13:42 2010
@@ -725,7 +725,8 @@ public final class HSSFWorkbook extends 
      *
      * @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

Added: poi/trunk/src/java/org/apache/poi/ss/util/WorkbookUtil.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/util/WorkbookUtil.java?rev=982260&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/util/WorkbookUtil.java (added)
+++ poi/trunk/src/java/org/apache/poi/ss/util/WorkbookUtil.java Wed Aug  4 
14:13:42 2010
@@ -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();
+       }
+}

Added: poi/trunk/src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java?rev=982260&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/ss/util/TestWorkbookUtil.java Wed 
Aug  4 14:13:42 2010
@@ -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);
+       }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to