This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new 264c931b2d Fix ContentType.equals and the M1.10 part name segment
check (#1240)
264c931b2d is described below
commit 264c931b2d682d6170b84f1e57930bc3ca108eeb
Author: PJ Fanning <[email protected]>
AuthorDate: Thu Sep 3 18:38:10 2026 +0100
Fix ContentType.equals and the M1.10 part name segment check (#1240)
ContentType.equals() had an inverted instanceof test, so it returned
true for every argument that was not a ContentType. Because equals()
compares case-insensitively while hashCode() hashed the raw fields,
the two were also inconsistent: content types differing only in case
compared equal but hashed differently, which can miss a lookup in the
partMarshallers/partUnmarshallers maps that are keyed by ContentType.
hashCode() is now derived from the same case-insensitive form equals()
uses.
The [M1.10] check in PackagePartName used the regex "\\." which matches
a backslash followed by any character rather than a dot, so it never
tested what it claimed. Replaced with an explicit all-dots check.
XSSFRow.removeCell kept scanning the CTCell array after removing the
matching entry, leaving the loop index misaligned with the shifted
underlying list. Stop at the match.
Co-authored-by: Claude Fable 5 <[email protected]>
---
.../apache/poi/openxml4j/opc/PackagePartName.java | 18 +++++++++++++++++-
.../poi/openxml4j/opc/internal/ContentType.java | 9 +++++----
.../org/apache/poi/xssf/usermodel/XSSFRow.java | 1 +
.../apache/poi/openxml4j/opc/TestContentType.java | 22 ++++++++++++++++++++++
4 files changed, 45 insertions(+), 5 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagePartName.java
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagePartName.java
index e6a1c67f70..100828196c 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagePartName.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/PackagePartName.java
@@ -263,7 +263,7 @@ public final class PackagePartName implements
Comparable<PackagePartName> {
"A segment shall not end with a dot ('.') character
[M1.9]: " + partUri.getPath());
}
- if (seg.replaceAll("\\\\.", "").isEmpty()) {
+ if (isAllDots(seg)) {
// Normally will never been invoked with the previous
// implementation rule [M1.9]
throw new InvalidFormatException(
@@ -275,6 +275,22 @@ public final class PackagePartName implements
Comparable<PackagePartName> {
}
}
+ /**
+ * Checks if a segment consists of dot ('.') characters only.
+ *
+ * @param segment
+ * The non-empty segment to check.
+ * @return <code>true</code> if the segment holds nothing but dots.
+ */
+ private static boolean isAllDots(String segment) {
+ for (int i = 0; i < segment.length(); i++) {
+ if (segment.charAt(i) != '.') {
+ return false;
+ }
+ }
+ return true;
+ }
+
/**
* Throws an exception if a segment any characters other than pchar
* characters. [M1.6]
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/ContentType.java
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/ContentType.java
index e69529def9..dbe3aa3209 100644
---
a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/ContentType.java
+++
b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/ContentType.java
@@ -20,8 +20,8 @@ package org.apache.poi.openxml4j.opc.internal;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.Locale;
import java.util.Map.Entry;
-import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -206,13 +206,14 @@ public final class ContentType {
@Override
public boolean equals(Object obj) {
- return (!(obj instanceof ContentType))
- || (StringUtil.equalsIgnoreCase(this.toString(),
obj.toString()));
+ return (obj instanceof ContentType)
+ && StringUtil.equalsIgnoreCase(this.toString(),
obj.toString());
}
@Override
public int hashCode() {
- return Objects.hash(type,subType,parameters);
+ // must be consistent with the case-insensitive comparison in equals()
+ return this.toString().toLowerCase(Locale.ROOT).hashCode();
}
/* Getters */
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRow.java
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRow.java
index 5f08082c3a..9cb3f919a8 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRow.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRow.java
@@ -529,6 +529,7 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
for (CTCell ctCell : _row.getCArray()) {
if(ctCell == removed.getCTCell()) {
_row.removeC(i);
+ break;
}
i++;
}
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestContentType.java
b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestContentType.java
index 926fabad65..d69bff5a24 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestContentType.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestContentType.java
@@ -20,6 +20,7 @@ package org.apache.poi.openxml4j.opc;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@@ -113,6 +114,27 @@ public final class TestContentType {
"Must have fail for content type: '" + contentType + "' !");
}
+ /**
+ * A content type is only ever equal to another content type, and the
+ * comparison ignores case (RFC 2616 media types are case-insensitive).
+ */
+ @Test
+ void testEqualsAndHashCode() throws InvalidFormatException {
+ ContentType type = new ContentType("text/xml");
+
+ assertEquals(type, new ContentType("text/xml"));
+ assertEquals(type, new ContentType("TEXT/XML"));
+ assertEquals(type.hashCode(), new ContentType("TEXT/XML").hashCode(),
+ "equal content types must have equal hash codes");
+
+ assertNotEquals(type, new ContentType("text/plain"));
+
+ // must not claim equality with unrelated types
+ assertNotEquals(type, "text/xml");
+ assertNotEquals(type, new Object());
+ assertNotEquals(null, type);
+ }
+
/**
* Check rule M1.15: The package implementer shall require a content type
* that does not include comments and the format designer shall specify
such
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]