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;
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)) {
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.
*/
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");
}
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
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;
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());
}
}
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;
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));
}
}
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) {
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;
return s;
}
protected static boolean isFormulaEmpty(String formula1) {
- return formula1 == null || formula1.trim().length()==0;
+ return StringUtil.isBlank(formula1);
}
/* (non-Javadoc)
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.*;
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));
}
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));
}
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;
// 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");
//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);
}
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());
}
}
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;
*/
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;
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;
}
}
- haveAnyText |= text.trim().length() != 0;
+ haveAnyText |= StringUtil.isNotBlank(text);
}
return haveAnyText;
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;
Text textNode = foDocumentFacade.createText( bulletText );
inline.appendChild( textNode );
- haveAnyText |= bulletText.trim().length() != 0;
+ haveAnyText |= StringUtil.isNotBlank(bulletText);
}
haveAnyText = processCharacters( hwpfDocument, currentTableLevel,
} catch (EOFException e) {
return;
}
- if (mbcs.trim().length() > 0 && unicode.trim().length() > 0) {
+ if (StringUtil.isNotBlank(mbcs) && StringUtil.isNotBlank(unicode)) {
moduleNames.put(mbcs, unicode);
}
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 {
}
public boolean isEmptyParagraph() {
- return (lines.isEmpty() || rawText.trim().isEmpty());
+ return (lines.isEmpty() || StringUtil.isBlank(rawText));
}
@Override
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>
* @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) {
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);
}
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
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;
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;
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.
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 {
* 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>
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;
/**
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));
}
// Check for special cases
- if(formatStr == null || formatStr.trim().isEmpty()) {
+ if(StringUtil.isBlank(formatStr)) {
return getDefaultFormat(cellValue);
}
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);
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);
+ }
+
}