From: PJ Fanning Date: Sun, 17 Jul 2022 01:53:18 +0000 (+0000) Subject: [github-339] Add extra methods to StringUtil and refactor some code to use them.... X-Git-Tag: REL_5_2_3~169 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c589f0ee7a9c5c104244373720ddd6bf0ab68ca9;p=poi.git [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 --- diff --git a/poi-examples/src/main/java/org/apache/poi/examples/xssf/eventusermodel/FromHowTo.java b/poi-examples/src/main/java/org/apache/poi/examples/xssf/eventusermodel/FromHowTo.java index 60246e07a3..14d5fbd9e8 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/xssf/eventusermodel/FromHowTo.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/xssf/eventusermodel/FromHowTo.java @@ -25,6 +25,7 @@ import javax.xml.parsers.ParserConfigurationException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; +import org.apache.poi.util.StringUtil; import org.apache.poi.util.XMLHelper; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.model.SharedStrings; @@ -132,7 +133,7 @@ public class FromHowTo { throws SAXException { // Process the last contents as required. // Do now, as characters() may be called more than once - if(nextIsString && !lastContents.trim().isEmpty()) { + if(nextIsString && StringUtil.isNotBlank(lastContents)) { Integer idx = Integer.valueOf(lastContents); lastContents = lruCache.get(idx); if (lastContents == null && !lruCache.containsKey(idx)) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java index 843e4d7e9b..0fc4c624be 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -61,6 +61,7 @@ import org.apache.poi.openxml4j.opc.internal.unmarshallers.UnmarshallContext; import org.apache.poi.openxml4j.util.ZipEntrySource; import org.apache.poi.util.IOUtils; import org.apache.poi.util.NotImplemented; +import org.apache.poi.util.StringUtil; /** * Represents a container that can store multiple data objects. @@ -234,7 +235,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { */ public static OPCPackage open(String path, PackageAccess access) throws InvalidFormatException, InvalidOperationException { - if (path == null || path.trim().isEmpty()) { + if (StringUtil.isBlank(path)) { throw new IllegalArgumentException("'path' must be given"); } @@ -458,8 +459,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { return; } - if (this.originalPackagePath != null - && !this.originalPackagePath.trim().isEmpty()) { + if (StringUtil.isNotBlank(this.originalPackagePath)) { File targetFile = new File(this.originalPackagePath); if (!targetFile.exists() || !(this.originalPackagePath diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java index 0d8851c609..e84145ed27 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSheetHandler.java @@ -27,6 +27,7 @@ import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.util.CellAddress; import org.apache.poi.util.Internal; import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.StringUtil; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; import org.apache.poi.xssf.model.SharedStrings; import org.apache.poi.xssf.usermodel.XSSFComment; @@ -226,7 +227,7 @@ public class XSSFBSheetHandler extends XSSFBParser { private void outputHeaderFooter(XSSFBHeaderFooter headerFooter) { String text = headerFooter.getString(); - if (text != null && !text.trim().isEmpty()) { + if (StringUtil.isNotBlank(text)) { handler.headerFooter(text, headerFooter.isHeader(), headerFooter.getHeaderFooterTypeLabel()); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java index 9a02bcb4a0..f2e3b95ecf 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java @@ -40,6 +40,7 @@ import org.apache.poi.openxml4j.opc.PackageRelationshipCollection; import org.apache.poi.openxml4j.opc.PackagingURIHelper; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianConsts; +import org.apache.poi.util.StringUtil; import org.apache.poi.xssf.binary.XSSFBCommentsTable; import org.apache.poi.xssf.binary.XSSFBParseException; import org.apache.poi.xssf.binary.XSSFBParser; @@ -254,7 +255,7 @@ public class XSSFBReader extends XSSFReader { String relId = sb.toString(); sb.setLength(0); /*offset +=*/ XSSFBUtils.readXLWideString(data, offset, sb); String name = sb.toString(); - if (relId.trim().length() > 0) { + if (StringUtil.isNotBlank(relId)) { sheets.add(new XSSFSheetRef(relId, name)); } } @@ -273,7 +274,7 @@ public class XSSFBReader extends XSSFReader { sb.setLength(0); offset += XSSFBUtils.readXLWideString(data, offset, sb); String name = sb.toString(); - if (relId.trim().length() > 0) { + if (StringUtil.isNotBlank(relId)) { sheets.add(new XSSFSheetRef(relId, name)); } if (offset == data.length) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDataValidationConstraint.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDataValidationConstraint.java index 0aa5ec1406..66d0e47328 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDataValidationConstraint.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDataValidationConstraint.java @@ -20,6 +20,7 @@ import java.util.Arrays; import java.util.regex.Pattern; import org.apache.poi.ss.usermodel.DataValidationConstraint; +import org.apache.poi.util.StringUtil; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationType; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationOperator.Enum; @@ -180,7 +181,7 @@ public class XSSFDataValidationConstraint implements DataValidationConstraint { return s; } protected static boolean isFormulaEmpty(String formula1) { - return formula1 == null || formula1.trim().length()==0; + return StringUtil.isBlank(formula1); } /* (non-Javadoc) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java index d478320beb..6ca78c88e2 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java @@ -27,6 +27,7 @@ import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; import org.apache.poi.util.Beta; import org.apache.poi.util.Internal; +import org.apache.poi.util.StringUtil; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlOptions; import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; @@ -293,7 +294,7 @@ public class XSSFPivotTable extends POIXMLDocumentPart { pivotField.setAxis(STAxis.AXIS_COL); pivotField.setShowAll(false); - if (valueFormat != null && !valueFormat.trim().isEmpty()) { + if (StringUtil.isNotBlank(valueFormat)) { DataFormat df = parentSheet.getWorkbook().createDataFormat(); pivotField.setNumFmtId(df.getFormat(valueFormat)); } @@ -416,7 +417,7 @@ public class XSSFPivotTable extends POIXMLDocumentPart { dataField.setSubtotal(STDataConsolidateFunction.Enum.forInt(function.getValue())); dataField.setName(valueFieldName); dataField.setFld(columnIndex); - if (valueFormat != null && !valueFormat.trim().isEmpty()) { + if (StringUtil.isNotBlank(valueFormat)) { DataFormat df = parentSheet.getWorkbook().createDataFormat(); dataField.setNumFmtId(df.getFormat(valueFormat)); } diff --git a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java index 831170d992..2566f16388 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java +++ b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java @@ -90,6 +90,7 @@ import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.util.ConditionalExecution.DisabledOnJreEx; import org.apache.poi.util.IOUtils; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.StringUtil; import org.apache.poi.util.TempFile; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; @@ -146,7 +147,7 @@ class TestSignatureInfo { // in the xmlsec jar file String additionalJar = System.getProperty("additionaljar"); //System.out.println("Having: " + additionalJar); - assumeTrue(additionalJar == null || additionalJar.trim().length() == 0, + assumeTrue(StringUtil.isBlank(additionalJar), "Not running TestSignatureInfo because we are testing with additionaljar set to " + additionalJar); System.setProperty("org.apache.xml.security.ignoreLineBreaks", "true"); @@ -475,7 +476,7 @@ class TestSignatureInfo { //set proxy info if any String proxy = System.getProperty("http_proxy"); - if (proxy != null && proxy.trim().length() > 0) { + if (StringUtil.isNotBlank(proxy)) { signatureConfig.setProxyUrl(proxy); } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java index da6c6c7e08..25b2594149 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java @@ -779,7 +779,7 @@ public final class TestXWPFParagraph { str.append(par.getText()).append("\n"); } String s = str.toString(); - assertTrue(s.trim().length() > 0, "Having text: \n" + s + "\nTrimmed length: " + s.trim().length()); + assertTrue(StringUtil.isNotBlank(s), "Having text: \n" + s + "\nTrimmed length: " + s.trim().length()); } } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/MAPIMessage.java b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/MAPIMessage.java index 5ffa6d0191..23c9bd770d 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hsmf/MAPIMessage.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hsmf/MAPIMessage.java @@ -53,6 +53,7 @@ import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.CodePageUtil; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.StringUtil; import static org.apache.logging.log4j.util.Unbox.box; @@ -630,7 +631,7 @@ public class MAPIMessage extends POIReadOnlyDocument { */ public MESSAGE_CLASS getMessageClassEnum() throws ChunkNotFoundException { String mc = getStringFromChunk(mainChunks.getMessageClass()); - if (mc == null || mc.trim().length() == 0) { + if (StringUtil.isBlank(mc)) { return MESSAGE_CLASS.UNSPECIFIED; } else if (mc.equalsIgnoreCase("IPM.Note")) { return MESSAGE_CLASS.NOTE; diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordConverter.java b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordConverter.java index 1ff262d456..01c4bed4fe 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordConverter.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordConverter.java @@ -52,6 +52,7 @@ import org.apache.poi.hwpf.usermodel.TableRow; import org.apache.poi.poifs.filesystem.Entry; import org.apache.poi.util.Beta; import org.apache.poi.util.Internal; +import org.apache.poi.util.StringUtil; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -553,7 +554,7 @@ public abstract class AbstractWordConverter { } } - haveAnyText |= text.trim().length() != 0; + haveAnyText |= StringUtil.isNotBlank(text); } return haveAnyText; diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToFoConverter.java b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToFoConverter.java index 9bf16aeb03..fb96e1eb36 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToFoConverter.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToFoConverter.java @@ -48,6 +48,7 @@ import org.apache.poi.hwpf.usermodel.Table; import org.apache.poi.hwpf.usermodel.TableCell; import org.apache.poi.hwpf.usermodel.TableRow; import org.apache.poi.util.Beta; +import org.apache.poi.util.StringUtil; import org.apache.poi.util.XMLHelper; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -438,7 +439,7 @@ public class WordToFoConverter extends AbstractWordConverter Text textNode = foDocumentFacade.createText( bulletText ); inline.appendChild( textNode ); - haveAnyText |= bulletText.trim().length() != 0; + haveAnyText |= StringUtil.isNotBlank(bulletText); } haveAnyText = processCharacters( hwpfDocument, currentTableLevel, 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.

@@ -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 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() * * Syntax
- * TEXTJOIN(delimiter, ignore_empty, text1, [text2], …)

+ * TEXTJOIN(delimiter, ignore_empty, text1, [text2], ...)

* * delimiter 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.
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(); + } + + /** + *

Checks if a CharSequence is empty (""), null or whitespace only.

+ * + *

Whitespace is defined by {@link Character#isWhitespace(char)}.

+ * + *
+     * StringUtil.isBlank(null)      = true
+     * StringUtil.isBlank("")        = true
+     * StringUtil.isBlank(" ")       = true
+     * StringUtil.isBlank("bob")     = false
+     * StringUtil.isBlank("  bob  ") = false
+     * 
+ * + * 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; + } + + /** + *

Checks if a CharSequence is not empty (""), not null and not whitespace only.

+ * + *

Whitespace is defined by {@link Character#isWhitespace(char)}.

+ * + *
+     * StringUtil.isNotBlank(null)      = false
+     * StringUtil.isNotBlank("")        = false
+     * StringUtil.isNotBlank(" ")       = false
+     * StringUtil.isNotBlank("bob")     = true
+     * StringUtil.isNotBlank("  bob  ") = true
+     * 
+ * + * 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); + } + }