You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/08/12 11:11:56 UTC
(commons-vfs) 04/06: Add FileSystemConfigBuilder.getParamOrDefault(FileSystemOptions, String, T)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git
commit 654bcf1110f7da2275a293ed6299b1ca19aa3397
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 11 17:24:36 2024 -0400
Add FileSystemConfigBuilder.getParamOrDefault(FileSystemOptions, String,
T)
- ZipFileSystemConfigBuilder.getCharset(FileSystemOptions) returns UTF-8
instead of null when absent
---
.../commons/vfs2/provider/webdav/WebdavFileSystem.java | 1 -
.../org/apache/commons/vfs2/FileSystemConfigBuilder.java | 15 +++++++++++++++
.../java/org/apache/commons/vfs2/FileSystemOptions.java | 7 ++++++-
.../apache/commons/vfs2/provider/zip/ZipFileSystem.java | 9 +++++----
.../vfs2/provider/zip/ZipFileSystemConfigBuilder.java | 5 +++--
.../provider/zip/ZipProviderWithCharsetNullTestCase.java | 3 ++-
src/changes/changes.xml | 6 ++++++
7 files changed, 37 insertions(+), 9 deletions(-)
diff --git a/commons-vfs2-jackrabbit1/src/main/java/org/apache/commons/vfs2/provider/webdav/WebdavFileSystem.java b/commons-vfs2-jackrabbit1/src/main/java/org/apache/commons/vfs2/provider/webdav/WebdavFileSystem.java
index ff40765c..1709fb66 100644
--- a/commons-vfs2-jackrabbit1/src/main/java/org/apache/commons/vfs2/provider/webdav/WebdavFileSystem.java
+++ b/commons-vfs2-jackrabbit1/src/main/java/org/apache/commons/vfs2/provider/webdav/WebdavFileSystem.java
@@ -27,7 +27,6 @@ import org.apache.commons.vfs2.provider.AbstractFileName;
import org.apache.commons.vfs2.provider.DefaultURLStreamHandler;
import org.apache.commons.vfs2.provider.GenericFileName;
import org.apache.commons.vfs2.provider.http.HttpFileSystem;
-import org.apache.http.client.protocol.HttpClientContext;
/**
* A WebDAV file system.
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java
index ca19c824..4a39aef4 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java
@@ -508,6 +508,21 @@ public abstract class FileSystemConfigBuilder {
return fileSystemOptions == null ? null : fileSystemOptions.getOption(getConfigClass(), name);
}
+ /**
+ * Gets a named parameter.
+ *
+ * @param <T> The expected return type.
+ * @param fileSystemOptions file system options to query, may be null.
+ * @param name get option with this name
+ * @param defaultValue The default value if absent.
+ * @return the named option or {@code defaultValue}.
+ *
+ * @since 2.10.0
+ */
+ protected <T> T getParamOrDefault(final FileSystemOptions fileSystemOptions, final String name, final T defaultValue) {
+ return fileSystemOptions == null ? defaultValue : fileSystemOptions.getOptionOrDefault(getConfigClass(), name, defaultValue);
+ }
+
/**
* Gets a named parameter.
*
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemOptions.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemOptions.java
index 958f52b5..f4dd9fa6 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemOptions.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemOptions.java
@@ -192,7 +192,12 @@ public final class FileSystemOptions implements Cloneable, Comparable<FileSystem
}
<T> T getOption(final Class<? extends FileSystem> fileSystemClass, final String name) {
- return (T) options.get(new FileSystemOptionKey(fileSystemClass, name));
+ return getOptionOrDefault(fileSystemClass, name, null);
+ }
+
+ <T> T getOptionOrDefault(final Class<? extends FileSystem> fileSystemClass, final String name, final T defaultValue) {
+ final T value = (T) options.getOrDefault(new FileSystemOptionKey(fileSystemClass, name), defaultValue);
+ return value != null ? value : defaultValue;
}
@Override
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystem.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystem.java
index 2389ad61..b8a8990c 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystem.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystem.java
@@ -19,6 +19,7 @@ package org.apache.commons.vfs2.provider.zip;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
@@ -98,14 +99,13 @@ public class ZipFileSystem extends AbstractFileSystem {
protected ZipFile createZipFile(final File file) throws FileSystemException {
try {
- return charset == null ? new ZipFile(file) : new ZipFile(file, charset);
+ return new ZipFile(file, charset);
} catch (final IOException ioe) {
throw new FileSystemException("vfs.provider.zip/open-zip-file.error", file, ioe);
}
}
- protected ZipFileObject createZipFileObject(final AbstractFileName name, final ZipEntry entry)
- throws FileSystemException {
+ protected ZipFileObject createZipFileObject(final AbstractFileName name, final ZipEntry entry) throws FileSystemException {
return new ZipFileObject(name, entry, this, true);
}
@@ -124,7 +124,8 @@ public class ZipFileSystem extends AbstractFileSystem {
}
/**
- * Gets the Charset.
+ * Gets the Charset, defaults to {@link StandardCharsets#UTF_8}, the value used in {@link ZipFile}.
+ *
* @return the Charset.
*/
protected Charset getCharset() {
diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystemConfigBuilder.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystemConfigBuilder.java
index 0c0b7c7f..eaa50169 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystemConfigBuilder.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/zip/ZipFileSystemConfigBuilder.java
@@ -18,6 +18,7 @@
package org.apache.commons.vfs2.provider.zip;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemConfigBuilder;
@@ -49,13 +50,13 @@ public class ZipFileSystemConfigBuilder extends FileSystemConfigBuilder {
}
/**
- * Gets the Charset from the FileSystemOptions.
+ * Gets the Charset from the FileSystemOptions or {@link StandardCharsets#UTF_8} if absent.
*
* @param fileSystemOptions The source FileSystemOptions.
* @return the Charset from the FileSystemOptions.
*/
public Charset getCharset(final FileSystemOptions fileSystemOptions) {
- return getParam(fileSystemOptions, KEY_CHARSET);
+ return getParamOrDefault(fileSystemOptions, KEY_CHARSET, StandardCharsets.UTF_8);
}
@Override
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ZipProviderWithCharsetNullTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ZipProviderWithCharsetNullTestCase.java
index 9f84fdef..e504d409 100644
--- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ZipProviderWithCharsetNullTestCase.java
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/zip/ZipProviderWithCharsetNullTestCase.java
@@ -19,6 +19,7 @@ package org.apache.commons.vfs2.provider.zip;
import static org.apache.commons.vfs2.VfsTestUtils.getTestResource;
import java.io.File;
+import java.nio.charset.StandardCharsets;
import junit.framework.Test;
@@ -59,7 +60,7 @@ public class ZipProviderWithCharsetNullTestCase extends AbstractProviderTestConf
final FileSystem fileSystem = resolvedFile.getFileSystem();
Assertions.assertTrue(fileSystem instanceof ZipFileSystem);
final ZipFileSystem zipFileSystem = (ZipFileSystem) fileSystem;
- Assertions.assertNull(zipFileSystem.getCharset());
+ Assertions.assertEquals(StandardCharsets.UTF_8, zipFileSystem.getCharset());
return resolvedFile;
}
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c0ff7eb4..7ec1e0f9 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -200,6 +200,9 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Arnout Engelen">
Simplify UriParser #558.
</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory">
+ ZipFileSystemConfigBuilder.getCharset(FileSystemOptions) returns UTF-8 instead of null when absent.
+ </action>
<!-- ADD -->
<action type="add" issue="VFS-848" dev="ggregory" due-to="beise, Gary Gregory">
Config option for trailing slash in webdav URI #425.
@@ -252,6 +255,9 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">
Add a BOM POM commons-vfs2-bom.
</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">
+ Add FileSystemConfigBuilder.getParamOrDefault(FileSystemOptions, String, T).
+ </action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">
Bump actions/cache and others.