diff options
author | PJ Fanning <fanningpj@apache.org> | 2022-07-17 01:53:18 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2022-07-17 01:53:18 +0000 |
commit | c589f0ee7a9c5c104244373720ddd6bf0ab68ca9 (patch) | |
tree | 88967bf7b53e347252d69837dbb6cc486dad3973 /poi | |
parent | dc7e2de82eef66051ae7684a9fae15f7540e4460 (diff) | |
download | poi-c589f0ee7a9c5c104244373720ddd6bf0ab68ca9.tar.gz poi-c589f0ee7a9c5c104244373720ddd6bf0ab68ca9.zip |
[github-339] Add extra methods to StringUtil and refactor some code to use them. Thanks to XenoAmess. This closes #339
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902789 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
8 files changed, 87 insertions, 12 deletions
diff --git a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java index 23b2936af3..9dec8e6ca6 100644 --- a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java +++ b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java @@ -658,7 +658,7 @@ public class VBAMacroReader implements Closeable { } catch (EOFException e) { return; } - if (mbcs.trim().length() > 0 && unicode.trim().length() > 0) { + if (StringUtil.isNotBlank(mbcs) && StringUtil.isNotBlank(unicode)) { moduleNames.put(mbcs, unicode); } diff --git a/poi/src/main/java/org/apache/poi/sl/draw/DrawTextParagraph.java b/poi/src/main/java/org/apache/poi/sl/draw/DrawTextParagraph.java index 4671ce34ba..ac29af9e4e 100644 --- a/poi/src/main/java/org/apache/poi/sl/draw/DrawTextParagraph.java +++ b/poi/src/main/java/org/apache/poi/sl/draw/DrawTextParagraph.java @@ -63,6 +63,7 @@ import org.apache.poi.sl.usermodel.TextShape; import org.apache.poi.sl.usermodel.TextShape.TextDirection; import org.apache.poi.util.Internal; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.StringUtil; import org.apache.poi.util.Units; public class DrawTextParagraph implements Drawable { @@ -242,7 +243,7 @@ public class DrawTextParagraph implements Drawable { } public boolean isEmptyParagraph() { - return (lines.isEmpty() || rawText.trim().isEmpty()); + return (lines.isEmpty() || StringUtil.isBlank(rawText)); } @Override diff --git a/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java b/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java index c49d5f0e3c..dcd98bb1dc 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java @@ -42,6 +42,7 @@ import org.apache.poi.ss.util.CellRangeAddressBase; import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.SheetUtil; +import org.apache.poi.util.StringUtil; /** * Evaluates Data Validation constraints.<p> @@ -418,7 +419,7 @@ public class DataValidationEvaluator { * @throws NumberFormatException if the formula is non-numeric when it should be */ private Double evalOrConstant(String formula, DataValidationContext context) throws NumberFormatException { - if (formula == null || formula.trim().isEmpty()) return null; // shouldn't happen, but just in case + if (StringUtil.isBlank(formula)) return null; // shouldn't happen, but just in case try { return Double.valueOf(formula); } catch (NumberFormatException e) { @@ -433,7 +434,7 @@ public class DataValidationEvaluator { if (eval instanceof NumberEval) return Double.valueOf(((NumberEval) eval).getNumberValue()); if (eval instanceof StringEval) { final String value = ((StringEval) eval).getStringValue(); - if (value == null || value.trim().isEmpty()) return null; + if (StringUtil.isBlank(value)) return null; // try to parse the cell value as a double and return it return Double.valueOf(value); } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java b/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java index b3fb95f593..54199cbfca 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java @@ -52,6 +52,7 @@ import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.StringUtil; /** * Abstracted and cached version of a Conditional Format rule for use with a @@ -506,7 +507,7 @@ public class EvaluationConditionalFormatRule implements Comparable<EvaluationCon try { String v = cv.getString(); // see TextFunction.TRIM for implementation - return v == null || v.trim().length() == 0; + return StringUtil.isBlank(v); } catch (Exception e) { // not a valid string value, and not a blank cell (that's checked earlier) return false; @@ -515,7 +516,7 @@ public class EvaluationConditionalFormatRule implements Comparable<EvaluationCon try { String v = cv.getString(); // see TextFunction.TRIM for implementation - return v != null && v.trim().length() > 0; + return StringUtil.isNotBlank(v); } catch (Exception e) { // not a valid string value, but not blank return true; diff --git a/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java b/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java index f362828819..61993d676b 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/FormulaParser.java @@ -80,6 +80,7 @@ import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference.NameType; import org.apache.poi.util.Internal; +import org.apache.poi.util.StringUtil; /** * This class parses a formula string into a List of tokens in RPN order. @@ -255,7 +256,7 @@ public final class FormulaParser { private RuntimeException expected(String s) { String msg; - if (look == '=' && _formulaString.substring(0, _pointer-1).trim().length() < 1) { + if (look == '=' && StringUtil.isBlank(_formulaString.substring(0, _pointer-1))) { msg = "The specified formula '" + _formulaString + "' starts with an equals sign which is not allowed."; } else { diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java index 52fcb05cc0..5e7ffe8b87 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java @@ -29,7 +29,7 @@ import java.util.List; * Implementation of Excel function TEXTJOIN() * * <b>Syntax</b><br> - * <b>TEXTJOIN</b>(<b>delimiter</b>, <b>ignore_empty</b>, <b>text1</b>, <b>[text2]<b>, …)<p> + * <b>TEXTJOIN</b>(<b>delimiter</b>, <b>ignore_empty</b>, <b>text1</b>, <b>[text2]<b>, ...)<p> * * <b>delimiter</b> A text string, either empty, or one or more characters enclosed by double quotes, or a reference to a valid text string. * If a number is supplied, it will be treated as text.<br> diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java index 89c55ad4f7..6b16d7e79b 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -48,6 +48,7 @@ import org.apache.poi.ss.formula.ConditionalFormattingEvaluator; import org.apache.poi.ss.util.DateFormatConverter; import org.apache.poi.ss.util.NumberToTextConverter; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.StringUtil; /** @@ -355,7 +356,7 @@ public class DataFormatter { int formatIndex = numFmt.getIdx(); String formatStr = numFmt.getFormat(); - if(formatStr == null || formatStr.trim().length() == 0) { + if(StringUtil.isBlank(formatStr)) { return null; } return getFormat(cell.getNumericCellValue(), formatIndex, formatStr, isDate1904(cell)); @@ -483,7 +484,7 @@ public class DataFormatter { } // Check for special cases - if(formatStr == null || formatStr.trim().isEmpty()) { + if(StringUtil.isBlank(formatStr)) { return getDefaultFormat(cellValue); } @@ -1361,10 +1362,10 @@ public class DataFormatter { seg2 = result.substring(Math.max(0, len - 7), len - 4); seg1 = result.substring(Math.max(0, len - 10), Math.max(0, len - 7)); - if(seg1.trim().length() > 0) { + if(StringUtil.isNotBlank(seg1)) { sb.append('(').append(seg1).append(") "); } - if(seg2.trim().length() > 0) { + if(StringUtil.isNotBlank(seg2)) { sb.append(seg2).append('-'); } sb.append(seg3); diff --git a/poi/src/main/java/org/apache/poi/util/StringUtil.java b/poi/src/main/java/org/apache/poi/util/StringUtil.java index fb9d61b43d..e0ac29b4cd 100644 --- a/poi/src/main/java/org/apache/poi/util/StringUtil.java +++ b/poi/src/main/java/org/apache/poi/util/StringUtil.java @@ -689,4 +689,74 @@ public final class StringUtil { return prefix + ((newLen == 0) ? "" : new String(string, newOffset, newLen * 2, UTF16LE)); } + + /** + * Gets a CharSequence length or {@code 0} if the CharSequence is + * {@code null}. + * + * copied from commons-lang3 + * + * @param cs + * a CharSequence or {@code null} + * @return CharSequence length or {@code 0} if the CharSequence is + * {@code null}. + */ + public static int length(final CharSequence cs) { + return cs == null ? 0 : cs.length(); + } + + /** + * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p> + * + * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p> + * + * <pre> + * StringUtil.isBlank(null) = true + * StringUtil.isBlank("") = true + * StringUtil.isBlank(" ") = true + * StringUtil.isBlank("bob") = false + * StringUtil.isBlank(" bob ") = false + * </pre> + * + * copied from commons-lang3 + * + * @param cs the CharSequence to check, may be null + * @return {@code true} if the CharSequence is null, empty or whitespace only + */ + public static boolean isBlank(final CharSequence cs) { + final int strLen = length(cs); + if (strLen == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if (!Character.isWhitespace(cs.charAt(i))) { + return false; + } + } + return true; + } + + /** + * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p> + * + * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p> + * + * <pre> + * StringUtil.isNotBlank(null) = false + * StringUtil.isNotBlank("") = false + * StringUtil.isNotBlank(" ") = false + * StringUtil.isNotBlank("bob") = true + * StringUtil.isNotBlank(" bob ") = true + * </pre> + * + * copied from commons-lang3 + * + * @param cs the CharSequence to check, may be null + * @return {@code true} if the CharSequence is + * not empty and not null and not whitespace only + */ + public static boolean isNotBlank(final CharSequence cs) { + return !isBlank(cs); + } + } |