From 369170467880ccab6c4cc9aeeae1caae15d0a469 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Tue, 25 Feb 2020 21:27:07 +0000 Subject: [PATCH] Sonar fixes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1874530 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/examples/html/excelStyle.css | 1 - .../apache/poi/hpsf/ClassIDPredefined.java | 1 + .../poi/hssf/usermodel/HSSFCellStyle.java | 7 +-- src/java/org/apache/poi/util/IntList.java | 8 +-- .../opc/internal/PackagePropertiesPart.java | 61 +++++++++---------- 5 files changed, 35 insertions(+), 43 deletions(-) diff --git a/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css b/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css index db2c526f63..f056123780 100644 --- a/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css +++ b/src/examples/src/org/apache/poi/ss/examples/html/excelStyle.css @@ -32,7 +32,6 @@ word-spacing: 0; white-space: pre-wrap; unicode-bidi: normal; - vertical-align: 0; background-image: none; text-shadow: none; list-style-image: none; diff --git a/src/java/org/apache/poi/hpsf/ClassIDPredefined.java b/src/java/org/apache/poi/hpsf/ClassIDPredefined.java index 9e1c5a4a28..61454769ec 100644 --- a/src/java/org/apache/poi/hpsf/ClassIDPredefined.java +++ b/src/java/org/apache/poi/hpsf/ClassIDPredefined.java @@ -147,6 +147,7 @@ public enum ClassIDPredefined { return (classID == null) ? null : LOOKUP.get(classID.toString()); } + @SuppressWarnings("java:S1201") public boolean equals(ClassID classID) { return getClassID().equals(classID); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index 14a97a2772..eb4be956d3 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -119,12 +119,7 @@ public final class HSSFCellStyle implements CellStyle, Duplicatable { // we keep the cached data in ThreadLocal members in order to // avoid multi-threading issues when different workbooks are accessed in // multiple threads at the same time - private static final ThreadLocal lastDateFormat = new ThreadLocal() { - @Override - protected Short initialValue() { - return Short.MIN_VALUE; - } - }; + private static final ThreadLocal lastDateFormat = ThreadLocal.withInitial(() -> Short.MIN_VALUE); private static final ThreadLocal> lastFormats = new ThreadLocal<>(); private static final ThreadLocal getDataFormatStringCache = new ThreadLocal<>(); diff --git a/src/java/org/apache/poi/util/IntList.java b/src/java/org/apache/poi/util/IntList.java index 03571bf953..f536e089e9 100644 --- a/src/java/org/apache/poi/util/IntList.java +++ b/src/java/org/apache/poi/util/IntList.java @@ -284,8 +284,8 @@ public class IntList if (o == this) { return true; } - - if (o.getClass()!=this.getClass()) { + + if (!(o instanceof IntList)) { return false; } @@ -294,13 +294,13 @@ public class IntList if (other._limit != _limit) { return false; } - + for (int i=0; i< _limit; i++) { if (other._array[i] != _array[i]) { return false; } } - + return true; } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java index eccb7f930d..aab394a73d 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java @@ -26,6 +26,8 @@ import java.util.Locale; import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidOperationException; @@ -675,41 +677,35 @@ public final class PackagePropertiesPart extends PackagePart implements PackageP } Matcher m = TIME_ZONE_PAT.matcher(dateStr); + Date d = null; if (m.find()) { - String dateTzStr = dateStr.substring(0, m.start())+ - m.group(1)+m.group(2); - for (String fStr : TZ_DATE_FORMATS) { - SimpleDateFormat df = new SimpleDateFormat(fStr, Locale.ROOT); - df.setTimeZone(LocaleUtil.TIMEZONE_UTC); - Date d = df.parse(dateTzStr, new ParsePosition(0)); - if (d != null) { - return Optional.of(d); - } - } + String dateTzStr = dateStr.substring(0, m.start())+m.group(1)+m.group(2); + d = parseDateFormat(TZ_DATE_FORMATS, dateTzStr); } - String dateTzStr = dateStr.endsWith("Z") ? dateStr : (dateStr + "Z"); - for (String fStr : DATE_FORMATS) { + if (d == null) { + String dateTzStr = dateStr.endsWith("Z") ? dateStr : (dateStr + "Z"); + d = parseDateFormat(DATE_FORMATS, dateTzStr); + } + if (d != null) { + return Optional.of(d); + } + + //if you're here, no pattern matched, throw exception + String allFormats = Stream.of(TZ_DATE_FORMATS, DATE_FORMATS) + .flatMap(Stream::of).collect(Collectors.joining(", ")); + throw new InvalidFormatException("Date " + dateStr + " not well formatted, expected format in: "+ allFormats); + } + + private static Date parseDateFormat(String[] formats, String dateTzStr) { + for (String fStr : formats) { SimpleDateFormat df = new SimpleDateFormat(fStr, Locale.ROOT); df.setTimeZone(LocaleUtil.TIMEZONE_UTC); Date d = df.parse(dateTzStr, new ParsePosition(0)); if (d != null) { - return Optional.of(d); + return d; } } - //if you're here, no pattern matched, throw exception - StringBuilder sb = new StringBuilder(); - int i = 0; - for (String fStr : TZ_DATE_FORMATS) { - if (i++ > 0) { - sb.append(", "); - } - sb.append(fStr); - } - for (String fStr : DATE_FORMATS) { - sb.append(", ").append(fStr); - } - throw new InvalidFormatException("Date " + dateStr + " not well formatted, " - + "expected format in: "+ sb); + return null; } /** @@ -720,13 +716,14 @@ public final class PackagePropertiesPart extends PackagePart implements PackageP * @return The formated date or null. * @see java.text.SimpleDateFormat */ - private String getDateValue(Optional d) { - if (d == null || !d.isPresent()) { - return ""; - } + private static String getDateValue(Optional d) { + return d.map(PackagePropertiesPart::getDateValue).orElse(""); + } + + private static String getDateValue(Date d) { SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATEFORMAT, Locale.ROOT); df.setTimeZone(LocaleUtil.TIMEZONE_UTC); - return df.format(d.get()); + return df.format(d); } @Override