You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2012/02/27 16:29:26 UTC

svn commit: r1294186 - /poi/trunk/src/java/org/apache/poi/ss/util/WorkbookUtil.java

Author: yegor
Date: Mon Feb 27 15:29:26 2012
New Revision: 1294186

URL: http://svn.apache.org/viewvc?rev=1294186&view=rev
Log:
follow-on to r1294180

Modified:
    poi/trunk/src/java/org/apache/poi/ss/util/WorkbookUtil.java

Modified: 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=1294186&r1=1294185&r2=1294186&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/util/WorkbookUtil.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/util/WorkbookUtil.java Mon Feb 27 15:29:26 2012
@@ -45,40 +45,63 @@ public class WorkbookUtil {
 	 * @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) {
+		return createSafeSheetName(nameProposal, ' ');
+	}
+
+    /**
+     * 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.ss.usermodel.Workbook#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: : 0x0000, 0x0003, / \ ? * ] [ </li>
+     * <li>Sheet names must not begin or end with ' (apostrophe)</li>
+     * </ul>
+     *
+     * @param nameProposal can be any string, will be truncated if necessary,
+     *        allowed to be null
+     * @param replaceChar the char to replace invalid characters.
+     * @return a valid string, "empty" if to short, "null" if null
+     */
+    public final static String createSafeSheetName(final String nameProposal, char replaceChar) {
+        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 '\u0000':
                 case '\u0003':
                 case ':':
-				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();
-	}
+                case '/':
+                case '\\':
+                case '?':
+                case '*':
+                case ']':
+                case '[':
+                    result.setCharAt(i, replaceChar);
+                    break;
+                case '\'':
+                    if (i==0 || i==length-1) {
+                        result.setCharAt(i, replaceChar);
+                    }
+                    break;
+                default:
+                    // all other chars OK
+            }
+        }
+        return result.toString();
+    }
 
     /**
      * Validates sheet name.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org