Author: desruisseaux
Date: Fri May 31 17:16:55 2013
New Revision: 1488318
URL: http://svn.apache.org/r1488318
Log:
Added support for options (mostly URL encoding for now).
Added:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Options.java
(with props)
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java
(with props)
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/util/OptionsTest.java
(with props)
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java
(with props)
Modified:
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreException.java
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/package-info.java
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/IOUtilitiesTest.java
Added:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Options.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Options.java?rev=1488318&view=auto
==============================================================================
---
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Options.java
(added)
+++
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Options.java
[UTF-8] Fri May 31 17:16:55 2013
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.util;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.io.IOException;
+import org.apache.sis.util.Static;
+import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.io.TableAppender;
+import org.apache.sis.setup.OptionKey;
+
+
+/**
+ * Utility methods working with {@link OptionKey}. The user needs to hold a
{@code Map<OptionKey<?>,Object>} field.
+ * The methods in this class needs only that field value for performing their
work.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+public final class Options extends Static {
+ /**
+ * Do not allow instantiation of this class.
+ */
+ private Options() {
+ }
+
+ /**
+ * Returns the option value for the given key, or {@code null} if none.
+ *
+ * @param <T> The type of option value.
+ * @param options The map where to search for the value, or {@code null}
if not yet created.
+ * @param key The option for which to get the value.
+ * @return The current value for the given option, or {@code null} if none.
+ */
+ public static <T> T get(final Map<?,?> options, final OptionKey<T> key) {
+ ArgumentChecks.ensureNonNull("key", key);
+ return (options != null) ? key.getElementType().cast(options.get(key))
: null;
+ }
+
+ /**
+ * Sets the option value for the given key.
+ *
+ * @param <T> The type of option value.
+ * @param options The map where to set the value, or {@code null} if not
yet created.
+ * @param key The option for which to set the value.
+ * @param value The new value for the given option, or {@code null} for
removing the value.
+ * @return The map of options, as a new map if the given map was null.
+ */
+ public static <T> Map<OptionKey<?>,Object> set(Map<OptionKey<?>,Object>
options, final OptionKey<T> key, final T value) {
+ ArgumentChecks.ensureNonNull("key", key);
+ ArgumentChecks.ensureCanCast("value", key.getElementType(), value);
+ if (value != null) {
+ if (options == null) {
+ options = new HashMap<>();
+ }
+ options.put(key, value);
+ } else if (options != null) {
+ options.remove(key);
+ }
+ return options;
+ }
+
+ /**
+ * Lists the options in the given string builder.
+ * This method is used for {@link Object#toString()} implementations.
+ *
+ * @param options The map of options, or {@code null} if none.
+ * @param header String to append if the given map is non-null.
+ * @param buffer The buffer where to write the option values.
+ */
+ public static void list(final Map<? extends OptionKey<?>,?> options, final
String header, final StringBuilder buffer) {
+ if (options != null) {
+
buffer.append(header).append("options={").append(System.lineSeparator());
+ final TableAppender table = new TableAppender(buffer, "");
+ table.setMultiLinesCells(true);
+ for (final Map.Entry<? extends OptionKey<?>,?> entry :
options.entrySet()) {
+ table.append(" ").append(entry.getKey().getName());
+ table.nextColumn();
+ table.append(" = ").append(String.valueOf(entry.getValue()));
+ table.nextLine();
+ }
+ try {
+ table.flush();
+ } catch (IOException e) {
+ throw new AssertionError(e);
+ }
+ buffer.append('}');
+ }
+ }
+}
Propchange:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Options.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Options.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Added:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java?rev=1488318&view=auto
==============================================================================
---
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java
(added)
+++
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java
[UTF-8] Fri May 31 17:16:55 2013
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.setup;
+
+import java.nio.ByteBuffer;
+import java.io.Serializable;
+import java.io.ObjectStreamException;
+import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.logging.Logging;
+
+
+/**
+ * Keys in a map of options. This class defines a set of static constants for
commonly-used options.
+ * Developers can subclass this class for defining their own options.
+ *
+ * @param <T> The type of option values.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+public class OptionKey<T> implements Serializable {
+ /**
+ * For cross-version compatibility.
+ */
+ private static final long serialVersionUID = -7580514229639750246L;
+
+ /**
+ * The encoding of a URL (<strong>not</strong> the encoding of the
document content).
+ * This option may be used when converting a {@link String} or a {@link
java.net.URL}
+ * to a {@link java.net.URI} or a {@link java.io.File}:
+ *
+ * <ul>
+ * <li>URI are always encoded in UTF-8.</li>
+ * <li>URL are often encoded in UTF-8, but not necessarily. Other
encodings are possible
+ * (while not recommended), or some URL may not be encoded at
all.</li>
+ * </ul>
+ *
+ * If this option is not provided, then the URL is assumed
<strong>not</strong> encoded.
+ *
+ * <p><b>Example:</b> Given the {@code "file:Map%20with%20spaces.png"}
URL, then:</p>
+ * <ul>
+ * <li>If the URL encoding option is set to {@code "UTF-8"} or {@code
"ISO-8859-1"}, then:<ul>
+ * <li>the encoded URI will be {@code
"file:Map%20with%20spaces.png"};</li>
+ * <li>the decoded URI or the file will be {@code "file:Map with
spaces.png"}.</li>
+ * </ul></li>
+ * <li>If the URL encoding option is set to {@code null} or is not
provided, then:<ul>
+ * <li>the encoded URI will be {@code
"file:Map%2520with%2520spaces.png"},
+ * i.e. the percent sign will be encoded as {@code "%25"};</li>
+ * <li>the decoded URI or the file will be {@code
"file:Map%20with%20spaces.png"}.</li>
+ * </ul></li>
+ * </ul>
+ *
+ * This option has not effect on URI encoding, which is always UTF-8.
+ *
+ * @see java.net.URLDecoder
+ */
+ public static final OptionKey<String> URL_ENCODING = new
OptionKey<>("URL_ENCODING", String.class);
+
+ /**
+ * The byte buffer to use for input/output operations. Some {@link
org.apache.sis.storage.DataStore}
+ * implementations allow a byte buffer to be specified, thus allowing
users to choose the buffer
+ * {@linkplain ByteBuffer#capacity() capacity}, whether the buffer
{@linkplain ByteBuffer#isDirect()
+ * is direct}, or to recycle existing buffers.
+ *
+ * <p>It is user's responsibility to ensure that:</p>
+ * <ul>
+ * <li>The buffer does not contains any valuable data, as it will be
{@linkplain ByteBuffer#clear() cleared}.</li>
+ * <li>The same buffer is not used concurrently by two different {@code
DataStore} instances.</li>
+ * </ul>
+ */
+ public static final OptionKey<ByteBuffer> BYTE_BUFFER = new
OptionKey<>("BYTE_BUFFER", ByteBuffer.class);
+
+ /**
+ * The name of this key. For {@code OptionKey} instances, it shall be the
name of the static constants.
+ * For subclasses of {@code OptionKey}, there is no restriction.
+ */
+ private final String name;
+
+ /**
+ * The type of values.
+ */
+ private final Class<T> type;
+
+ /**
+ * Creates a new key of the given name for values of the given type.
+ *
+ * @param name The key name.
+ * @param type The type of values.
+ */
+ protected OptionKey(final String name, final Class<T> type) {
+ ArgumentChecks.ensureNonEmpty("name", name);
+ ArgumentChecks.ensureNonNull ("type", type);
+ this.name = name;
+ this.type = type;
+ }
+
+ /**
+ * Returns the name of this option key.
+ *
+ * @return The name of this option key.
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Returns the type of values associated to this option key.
+ *
+ * @return The type of values.
+ */
+ public final Class<T> getElementType() {
+ return type;
+ }
+
+ /**
+ * Returns {@code true} if the given object is an instance of the same
class having the same name and type.
+ *
+ * @param object The object to compare with this {@code OptionKey} for
equality.
+ */
+ @Override
+ public boolean equals(final Object object) {
+ if (object == this) {
+ return true;
+ }
+ if (object != null && object.getClass() == getClass()) {
+ final OptionKey<?> that = (OptionKey<?>) object;
+ return name.equals(that.name) && type == that.type;
+ }
+ return false;
+ }
+
+ /**
+ * Returns a hash code value for this object.
+ */
+ @Override
+ public int hashCode() {
+ return name.hashCode() ^ (int) serialVersionUID;
+ }
+
+ /**
+ * Returns a string representation of this option key.
+ * The default implementation returns the value of {@link #getName()}.
+ */
+ @Override
+ public String toString() {
+ return getName();
+ }
+
+ /**
+ * Resolves this option key on deserialization. This method is invoked
+ * only for instance of the exact {@code OptionKey} class, not subclasses.
+ */
+ private Object readResolve() throws ObjectStreamException {
+ try {
+ return OptionKey.class.getField(name).get(null);
+ } catch (ReflectiveOperationException e) {
+ /*
+ * This may happen if we are deserializing a stream produced by a
more recent SIS library
+ * than the one running in this JVM. This class should be robust
to this situation, since
+ * we override the 'equals' and 'hashCode' methods. This option is
likely to be ignored,
+ * but options are expected to be optional...
+ */
+ Logging.recoverableException(OptionKey.class, "readResolve", e);
+ return this;
+ }
+ }
+}
Propchange:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Added:
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/util/OptionsTest.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/util/OptionsTest.java?rev=1488318&view=auto
==============================================================================
---
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/util/OptionsTest.java
(added)
+++
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/util/OptionsTest.java
[UTF-8] Fri May 31 17:16:55 2013
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.util;
+
+import java.util.Map;
+import org.apache.sis.setup.OptionKey;
+import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+import static org.apache.sis.test.TestUtilities.getSingleton;
+
+
+/**
+ * Tests the {@link Options} class.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+public final strictfp class OptionsTest extends TestCase {
+ /**
+ * Tests the {@link Options#set(Map, OptionKey, Object)} method
+ * followed by {@link Options#get(Map, OptionKey)}.
+ */
+ @Test
+ public void testSetAndGet() {
+ assertNull(Options.get(null, OptionKey.URL_ENCODING));
+ assertNull(Options.set(null, OptionKey.URL_ENCODING, null));
+
+ final Map<OptionKey<?>,Object> options = Options.set(null,
OptionKey.URL_ENCODING, "UTF-8");
+ assertEquals("UTF-8", getSingleton(options.values()));
+ assertEquals("UTF-8", Options.get(options, OptionKey.URL_ENCODING));
+
+ assertSame(options, Options.set(options, OptionKey.URL_ENCODING,
"ISO-8859-1"));
+ assertEquals("ISO-8859-1", getSingleton(options.values()));
+ assertEquals("ISO-8859-1", Options.get(options,
OptionKey.URL_ENCODING));
+ }
+
+ /**
+ * Tests the {@link Options#list(Map, String, StringBuilder)} method.
+ */
+ @Test
+ @DependsOnMethod("testSetAndGet")
+ public void testList() {
+ final Map<OptionKey<?>,Object> options = Options.set(null,
OptionKey.URL_ENCODING, "UTF-8");
+
+ final StringBuilder buffer = new StringBuilder();
+ Options.list(options, "", buffer);
+ assertMultilinesEquals(
+ "options={\n" +
+ " URL_ENCODING = UTF-8\n" +
+ "}", buffer.toString());
+ }
+}
Propchange:
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/util/OptionsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/util/OptionsTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Added:
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java?rev=1488318&view=auto
==============================================================================
---
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java
(added)
+++
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java
[UTF-8] Fri May 31 17:16:55 2013
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.setup;
+
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+import static org.apache.sis.setup.OptionKey.*;
+
+
+/**
+ * Tests {@link OptionKey}.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+public final strictfp class OptionKeyTest extends TestCase {
+ /**
+ * Tests the serialization of constants.
+ * Those constants shall be resolved to their singleton instance on
deserialization.
+ */
+ @Test
+ public void testSerialization() {
+ assertSame(URL_ENCODING, assertSerializedEquals(URL_ENCODING));
+ assertSame(BYTE_BUFFER, assertSerializedEquals(BYTE_BUFFER ));
+ }
+}
Propchange:
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Modified:
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java?rev=1488318&r1=1488317&r2=1488318&view=diff
==============================================================================
---
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] (original)
+++
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] Fri May 31 17:16:55 2013
@@ -35,6 +35,7 @@ import org.junit.BeforeClass;
org.apache.sis.internal.test.XMLComparatorTest.class,
// Most basic functions of SIS library.
+ org.apache.sis.setup.OptionKeyTest.class,
org.apache.sis.util.ArraysExtTest.class,
org.apache.sis.util.CharactersTest.class,
org.apache.sis.util.CharSequencesTest.class,
@@ -51,6 +52,7 @@ import org.junit.BeforeClass;
org.apache.sis.math.StatisticsTest.class,
org.apache.sis.math.StatisticsFormatTest.class,
org.apache.sis.internal.util.UtilitiesTest.class,
+ org.apache.sis.internal.util.OptionsTest.class,
org.apache.sis.internal.jdk8.JDK8Test.class,
// Collections.
Modified:
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java?rev=1488318&r1=1488317&r2=1488318&view=diff
==============================================================================
---
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
[UTF-8] (original)
+++
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
[UTF-8] Fri May 31 17:16:55 2013
@@ -117,6 +117,7 @@ public class ChannelDataInput {
this.buffer = buffer;
this.channelOffset = (channel instanceof SeekableByteChannel) ?
((SeekableByteChannel) channel).position() : 0;
if (!filled) {
+ buffer.clear();
channel.read(buffer);
buffer.flip();
}
Modified:
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java?rev=1488318&r1=1488317&r2=1488318&view=diff
==============================================================================
---
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
[UTF-8] (original)
+++
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
[UTF-8] Fri May 31 17:16:55 2013
@@ -34,6 +34,8 @@ import org.apache.sis.util.ArgumentCheck
import org.apache.sis.util.resources.Errors;
import org.apache.sis.internal.storage.IOUtilities;
import org.apache.sis.internal.storage.ChannelImageInputStream;
+import org.apache.sis.internal.util.Options;
+import org.apache.sis.setup.OptionKey;
/**
@@ -68,6 +70,12 @@ public class DataStoreConnection impleme
private static final long serialVersionUID = 2524083964906593093L;
/**
+ * The default size of the {@link ByteBuffer} to be created.
+ * Users can override this value by providing a value for {@link
OptionKey#BYTE_BUFFER}.
+ */
+ private static final int DEFAULT_BUFFER_SIZE = 4096;
+
+ /**
* The input/output object given at construction time.
*
* @see #getStorage()
@@ -118,6 +126,14 @@ public class DataStoreConnection impleme
private transient long streamOrigin;
/**
+ * The options, created only when first needed.
+ *
+ * @see #getOption(OptionKey)
+ * @see #setOption(OptionKey, Object)
+ */
+ private transient Map<OptionKey<?>, Object> options;
+
+ /**
* Creates a new data store connection wrapping the given input/output
object.
* The object can be of any type, but the class javadoc lists the most
typical ones.
*
@@ -129,6 +145,33 @@ public class DataStoreConnection impleme
}
/**
+ * Returns the option value for the given key, or {@code null} if none.
+ *
+ * @param <T> The type of option value.
+ * @param key The option for which to get the value.
+ * @return The current value for the given option, or {@code null} if none.
+ */
+ public <T> T getOption(final OptionKey<T> key) {
+ return Options.get(options, key);
+ }
+
+ /**
+ * Sets the option value for the given key. The default implementation
recognizes the given options:
+ *
+ * <ul>
+ * <li>{@link OptionKey#URL_ENCODING} for converting URL to URI or
filename, if needed.</li>
+ * <li>{@link OptionKey#BYTE_BUFFER} for allowing users to control the
byte buffer to be created.</li>
+ * </ul>
+ *
+ * @param <T> The type of option value.
+ * @param key The option for which to set the value.
+ * @param value The new value for the given option, or {@code null} for
removing the value.
+ */
+ public <T> void setOption(final OptionKey<T> key, final T value) {
+ options = Options.set(options, key, value);
+ }
+
+ /**
* Returns the input/output object given at construction time.
* The object can be of any type, but the class javadoc lists the most
typical ones.
*
@@ -289,9 +332,12 @@ public class DataStoreConnection impleme
* SIS data stores will want to access directly the channel and
the buffer. We will fallback
* on the ImageIO.createImageInputStream(Object) method only in
last resort.
*/
- final ReadableByteChannel channel = IOUtilities.open(storage,
null);
+ final ReadableByteChannel channel = IOUtilities.open(storage,
getOption(OptionKey.URL_ENCODING));
if (channel != null) {
- final ByteBuffer buffer = ByteBuffer.allocate(4096);
+ ByteBuffer buffer = getOption(OptionKey.BYTE_BUFFER);
+ if (buffer == null) {
+ buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
+ }
asDataInput = new ChannelImageInputStream(getStorageName(),
channel, buffer, false);
asByteBuffer = buffer.asReadOnlyBuffer();
} else {
@@ -434,8 +480,8 @@ public class DataStoreConnection impleme
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder(40);
-
buffer.append(Classes.getShortClassName(this)).append("[“").append(getStorageName());
- // TODO: more info here.
- return buffer.append("”]").toString();
+
buffer.append(Classes.getShortClassName(this)).append("[“").append(getStorageName()).append('”');
+ Options.list(options, ", ", buffer);
+ return buffer.append(']').toString();
}
}
Modified:
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreException.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreException.java?rev=1488318&r1=1488317&r2=1488318&view=diff
==============================================================================
---
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreException.java
[UTF-8] (original)
+++
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreException.java
[UTF-8] Fri May 31 17:16:55 2013
@@ -18,7 +18,7 @@ package org.apache.sis.storage;
/**
- * Throws when a {@link DataStore} can not completed a read or write operation.
+ * Thrown when a {@link DataStore} can not complete a read or write operation.
*
* @author Johann Sorel (Geomatys)
* @since 0.3 (derived from geotk-3.10)
Modified:
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/package-info.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/package-info.java?rev=1488318&r1=1488317&r2=1488318&view=diff
==============================================================================
---
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/package-info.java
[UTF-8] (original)
+++
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/package-info.java
[UTF-8] Fri May 31 17:16:55 2013
@@ -16,9 +16,11 @@
*/
/**
- * Place holder for future {@code DataStore} services.
+ * {@linkplain org.apache.sis.storage.DataStore Data store} base types for
retrieving and saving geospatial data
+ * in various storage formats.
*
* @author Johann Sorel (Geomatys)
+ * @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-3.10)
* @version 0.3
* @module
Modified:
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/IOUtilitiesTest.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/IOUtilitiesTest.java?rev=1488318&r1=1488317&r2=1488318&view=diff
==============================================================================
---
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/IOUtilitiesTest.java
[UTF-8] (original)
+++
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/IOUtilitiesTest.java
[UTF-8] Fri May 31 17:16:55 2013
@@ -100,6 +100,10 @@ public final strictfp class IOUtilitiesT
IOUtilities.toURI(new
URL("file:/Users/name/Map%20with%20spaces.png"), "UTF-8"));
assertEquals(new URI("file:/Users/name/Map%20with%20spaces.png"),
IOUtilities.toURI(new
URL("file:/Users/name/Map%20with%20spaces.png"), "ISO-8859-1"));
+
+ // Here the URL is considered non-encoded, so the method shall encode
the % sign.
+ assertEquals(new URI("file:/Users/name/Map%2520with%2520spaces.png"),
+ IOUtilities.toURI(new
URL("file:/Users/name/Map%20with%20spaces.png"), null));
}
/**