sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1515573 - in /sis/branches/JDK7/storage/sis-storage: ./ src/main/java/org/apache/sis/internal/storage/xml/ src/main/java/org/apache/sis/storage/ src/test/java/org/apache/sis/internal/storage/xml/ src/test/java/org/apache/sis/test/suite/
Date Mon, 19 Aug 2013 19:31:23 GMT
Author: desruisseaux
Date: Mon Aug 19 19:31:22 2013
New Revision: 1515573

URL: http://svn.apache.org/r1515573
Log:
Detect the "application/vnd.iso.19139+xml" and "application/gml+xml" MIME types
(instead of only application/xml"). More types to be added later.

Added:
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java
  (with props)
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java
  (with props)
Modified:
    sis/branches/JDK7/storage/sis-storage/pom.xml
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/XMLStoreProvider.java
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeResult.java
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/XMLStoreProviderTest.java
    sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java

Modified: sis/branches/JDK7/storage/sis-storage/pom.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/pom.xml?rev=1515573&r1=1515572&r2=1515573&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/pom.xml (original)
+++ sis/branches/JDK7/storage/sis-storage/pom.xml Mon Aug 19 19:31:22 2013
@@ -105,4 +105,19 @@ Provides the interfaces and base classes
     </plugins>
   </build>
 
+
+  <!-- ===========================================================
+           Dependencies
+       =========================================================== -->
+  <dependencies>
+    <!-- Test dependencies -->
+    <dependency>
+      <groupId>org.apache.sis.core</groupId>
+      <artifactId>sis-metadata</artifactId>
+      <version>${project.version}</version>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
 </project>

Added: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java?rev=1515573&view=auto
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java
(added)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java
[UTF-8] Mon Aug 19 19:31:22 2013
@@ -0,0 +1,281 @@
+/*
+ * 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.storage.xml;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Arrays;
+import java.io.IOException;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.storage.ProbeResult;
+import org.apache.sis.xml.Namespaces;
+
+
+/**
+ * Detects the MIME type of a XML document from the namespace of the root element.
+ * This class does not support encoding: it will search only for US-ASCII characters.
+ * It does not prevent usage with encodings like ISO-LATIN-1 or UTF-8, provided that
+ * the characters in the [32 … 122] range (from space to 'z') are the same and can not
+ * be used as part of a multi-byte character.
+ *
+ * <p>This class tries to implement a lightweight detection mechanism. We can not for
instance
+ * unmarshall the whole document with JAXB and look at the class of unmarshalled object,
since
+ * it would be way too heavy.</p>
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ */
+abstract class MimeTypeDetector {
+    /**
+     * The mapping from XML namespace to MIME type.
+     * This map shall be read-only, since we do not synchronize it.
+     */
+    private static final Map<String,String> TYPES = new HashMap<>();
+    static {
+        TYPES.put(Namespaces.GML, "application/gml+xml");
+        TYPES.put(Namespaces.GMD, "application/vnd.iso.19139+xml");
+        TYPES.put(Namespaces.CSW, "application/vnd.ogc.csw_xml");
+        // More types to be added in future versions.
+    }
+
+    /**
+     * The {@code "xmlns"} string as a sequence of bytes.
+     */
+    private static byte[] XMLNS = {'x','m','l','n','s'};
+
+    /**
+     * The maximal US-ASCII value, inclusive.
+     */
+    private static final int MAX_ASCII = 126;
+
+    /**
+     * A buffer for reading a word from the XML document, assumed using US-ASCII characters.
+     */
+    private byte[] buffer = new byte[32];
+
+    /**
+     * Number of valid characters in {@link #buffer} string.
+     */
+    private int length;
+
+    /**
+     * Sets to {@code true} when {@link #read()} implementations reached the {@link java.nio.ByteBuffer}
limit,
+     * but the buffer has enough capacity for more bytes. In such case the {@link #probeContent()}
method will
+     * return {@link ProbeResult#INSUFFICIENT_BYTES}, which means that the method requests
more bytes for
+     * detecting the MIME type.
+     *
+     * @see ProbeResult#INSUFFICIENT_BYTES
+     */
+    boolean insufficientBytes;
+
+    /**
+     * Creates a new instance.
+     */
+    MimeTypeDetector() {
+    }
+
+    /**
+     * Adds the given byte in the {@link #buffer}, increasing its capacity if needed.
+     */
+    private void add(final int c) {
+        if (length == buffer.length) {
+            buffer = Arrays.copyOf(buffer, length*2);
+        }
+        buffer[length++] = (byte) c;
+    }
+
+    /**
+     * Reads a single byte or character, or -1 if we reached the end of the stream portion
that we are allowed
+     * to read. We are typically not allowed to read the full stream because only a limited
amount of bytes is
+     * cached.
+     *
+     * @return The character, or -1 on EOF.
+     * @throws IOException if an error occurred while reading the byte or character.
+     */
+    abstract int read() throws IOException;
+
+    /**
+     * Skips all bytes or characters up to {@code search}, then returns the character after
it.
+     * Characters inside quotes will be ignored.
+     *
+     * @param  search The byte or character to skip.
+     * @return The byte or character after {@code search}, or -1 on EOF.
+     * @throws IOException if an error occurred while reading the bytes or characters.
+     */
+    private int readAfter(final int search) throws IOException {
+        int c;
+        boolean isQuote = false;
+        while ((c = read()) >= 0) {
+            if (c == '"') {
+                isQuote = !isQuote;
+            } else if (c == search && !isQuote) {
+                return read();
+            }
+        }
+        return -1;
+    }
+
+    /**
+     * If the given character is a space, skip it and all following spaces.
+     * Returns the first non-space character.
+     *
+     * <p>For the purpose of this method, a "space" is considered to be the {@code
' '} character
+     * and all control characters (character below 32, which include tabulations and line
feeds).
+     * This is the same criterion than {@link String#trim()}, but is not Unicode spaces.</p>
+     *
+     * @return The first non-space character, or -1 on EOF.
+     * @throws IOException if an error occurred while reading the bytes or characters.
+     */
+    private int afterSpaces(int c) throws IOException {
+        while (c <= ' ' && c >= 0) {
+            c = read();
+        }
+        return c;
+    }
+
+    /**
+     * Skips the spaces if any, then the given characters, then the spaces, then the given
separator.
+     * After this method class, the stream position is on the first character after the separator
if
+     * a match has been found, or after the first unknown character otherwise.
+     *
+     * @param  word The word to search, as US-ASCII characters.
+     * @param  n Number of valid characters in {@code word}.
+     * @param  separator The {@code ':'} or {@code '='} character.
+     * @return 1 if a match is found, 0 if no match, or -1 on EOF.
+     * @throws IOException if an error occurred while reading the bytes or characters.
+     */
+    private int matches(final byte[] word, final int n, final char separator) throws IOException
{
+        int c = afterSpaces(read());
+        for (int i=0; i<n; i++) {
+            if (c != word[i]) {
+                return (c >= 0) ? 0 : -1;
+            }
+            c = read();
+        }
+        c = afterSpaces(c);
+        return (c == separator) ? 1 : (c >= 0) ? 0 : -1;
+    }
+
+    /**
+     * Returns the MIME type, or {@code null} if unknown.
+     *
+     * @throws IOException if an error occurred while reading the bytes or characters.
+     */
+    final String getMimeType() throws IOException {
+        if (readAfter('?') != '>') {
+            return null;
+        }
+        /*
+         * At this point, we skipped the "<?xml ...?>" header.
+         * Find the first < character, skipping comment (if any).
+         */
+        int c;
+        while ((c = readAfter('<')) == '!') {
+            do {
+                c = readAfter('-');
+                while (c == '-') {
+                    c = read();
+                }
+                if (c < 0) {
+                    return null;
+                }
+            } while (c != '>');
+        }
+        /*
+         * At this point, we are after the opening bracket of root element.
+         * Skip spaces and read the prefix, which is assumed mandatory.
+         */
+        c = afterSpaces(c);
+        while (c > ' ' && c != ':') {
+            if (c == '>' || c > MAX_ASCII) {
+                return null;
+            }
+            add(c);
+            c = read();
+        }
+        /*
+         * At this point, we got the prefix of the root element. Skip the ':'
+         * character and find the "xmlns" attribute following spaces.
+         */
+        c = afterSpaces(c);
+        if (c != ':') {
+            return null;
+        }
+        while (true) {
+            int m = matches(XMLNS, XMLNS.length, ':');
+            if (m != 0) {
+                if (m < 0) {
+                    return null;
+                }
+                m = matches(buffer, length, '=');
+                if (m != 0) {
+                    if (m < 0) {
+                        return null;
+                    }
+                    break;
+                }
+            }
+            // Skip everything up to the next space, and check again.
+            while ((c = read()) >= ' ');
+            if (c < 0) return null;
+            continue;
+        }
+        /*
+         * At this point, we found the "xmlns" attribute for the prefix of the root element.
+         * Get the attribute value (i.e. the namespace).
+         */
+        length = 0;
+        c = afterSpaces(read());
+        if (c != '"') {
+            return null;
+        }
+        c = afterSpaces(read());
+        do {
+            if (c < 0 || c > MAX_ASCII) {
+                return null;
+            }
+            add(c);
+            c = read();
+        } while (c != '"');
+        /*
+         * Done reading the "xmlns" attribute value.
+         */
+        return TYPES.get(new String(buffer, 0, length, "US-ASCII"));
+    }
+
+    /**
+     * Wraps the call to {@link #getMimeType()} for catching {@link IOException} and for
+     * instantiating the {@link ProbeResult}.
+     */
+    final ProbeResult probeContent() throws DataStoreException {
+        String mimeType;
+        try {
+            mimeType = getMimeType();
+        } catch (IOException e) {
+            throw new DataStoreException(e);
+        }
+        if (mimeType == null) {
+            if (insufficientBytes) {
+                return ProbeResult.INSUFFICIENT_BYTES;
+            }
+            mimeType = XMLStoreProvider.MIME_TYPE;
+        }
+        return new ProbeResult(true, mimeType, null);
+    }
+}

Propchange: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/XMLStoreProvider.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/XMLStoreProvider.java?rev=1515573&r1=1515572&r2=1515573&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/XMLStoreProvider.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/XMLStoreProvider.java
[UTF-8] Mon Aug 19 19:31:22 2013
@@ -36,6 +36,17 @@ import org.apache.sis.storage.ProbeResul
  */
 public class XMLStoreProvider extends DataStoreProvider {
     /**
+     * The {@value} MIME type, used only of {@link #probeContent(StorageConnector)} can not
determine
+     * a more accurate type.
+     */
+    public static final String MIME_TYPE = "application/xml";
+
+    /**
+     * The read-ahead limit when reading the XML document from a {@link Reader}.
+     */
+    private static final int READ_AHEAD_LIMIT = 2048;
+
+    /**
      * The expected XML header. According XML specification, this declaration is required
to appear
      * at the document beginning (no space allowed before the declaration).
      */
@@ -63,12 +74,25 @@ public class XMLStoreProvider extends Da
             if (buffer.remaining() < HEADER.length) {
                 return ProbeResult.INSUFFICIENT_BYTES;
             }
+            // Quick check for "<?xml " header.
             for (int i=0; i<HEADER.length; i++) {
                 if (buffer.get(i) != HEADER[i]) {
                     return ProbeResult.UNSUPPORTED_STORAGE;
                 }
             }
-            return ProbeResult.SUPPORTED;
+            // Now check for a more accurate MIME type.
+            buffer.position(HEADER.length);
+            final ProbeResult result = new MimeTypeDetector() {
+                @Override int read() {
+                    if (buffer.hasRemaining()) {
+                        return buffer.get();
+                    }
+                    insufficientBytes = (buffer.limit() != buffer.capacity());
+                    return -1;
+                }
+            }.probeContent();
+            buffer.position(0);
+            return result;
         }
         /*
          * We should enter in this block only if the user gave us explicitely a Reader.
@@ -76,15 +100,23 @@ public class XMLStoreProvider extends Da
          */
         final Reader reader = storage.getStorageAs(Reader.class);
         if (reader != null) try {
-            reader.mark(HEADER.length);
+            // Quick check for "<?xml " header.
+            reader.mark(HEADER.length + READ_AHEAD_LIMIT);
             for (int i=0; i<HEADER.length; i++) {
                 if (reader.read() != HEADER[i]) {
                     reader.reset();
                     return ProbeResult.UNSUPPORTED_STORAGE;
                 }
             }
+            // Now check for a more accurate MIME type.
+            final ProbeResult result = new MimeTypeDetector() {
+                private int remaining = READ_AHEAD_LIMIT;
+                @Override int read() throws IOException {
+                    return (--remaining >= 0) ? reader.read() : -1;
+                }
+            }.probeContent();
             reader.reset();
-            return ProbeResult.SUPPORTED;
+            return result;
         } catch (IOException e) {
             throw new DataStoreException(e);
         }

Modified: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeResult.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeResult.java?rev=1515573&r1=1515572&r2=1515573&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeResult.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/ProbeResult.java
[UTF-8] Mon Aug 19 19:31:22 2013
@@ -184,25 +184,25 @@ public class ProbeResult implements Seri
      * The {@link DataStoreProvider} may (at implementation choice) inspect the storage content
for
      * determining a more accurate MIME type.
      *
-     * {@example a generic MIME type for XML documents is <code>"application/xml"</code>.
-     * However many other MIME types exist for XML documents compliant to some particular
shema:
+     * {@section XML types}
+     * A generic MIME type for XML documents is {@code "application/xml"}.
+     * However many other MIME types exist for XML documents compliant to some particular
shema.
+     * Those types can be determined by inspecting the namespace of XML root element.
+     * The following table gives some example:
      *
      * <table class="sis">
-     *   <tr><th>MIME type</th> <th>Description</th></tr>
-     *   <tr><td><code>"application/gml+xml"</code></td>  
                             <td>Official mime type for OGC GML</td></tr>
-     *   <tr><td><code>"application/vnd.eu.europa.ec.inspire.resource+xml"</code></td>
 <td>Official mime type for INSPIRE Resources</td></tr>
-     *   <tr><td><code>"application/vnd.iso.19139+xml"</code></td>
                     <td>Unofficial mime type for ISO 19139 metadata</td></tr>
-     *   <tr><td><code>"application/vnd.ogc.wms_xml"</code></td>
                       <td>Unofficial mime type for OGC WMS</td></tr>
-     *   <tr><td><code>"application/vnd.ogc.wfs_xml"</code></td>
                       <td>Unofficial mime type for OGC WFS </td></tr>
-     *   <tr><td><code>"application/vnd.ogc.csw_xml"</code></td>
                       <td>Unofficial mime type for OGC CSW</td></tr>
-     *   <tr><td><code>"application/vnd.google-earth.kml+xml"</code></td>
              <td></td></tr>
-     *   <tr><td><code>"application/rdf+xml"</code></td>  
                             <td></td></tr>
-     *   <tr><td><code>"application/soap+xml"</code></td> 
                             <td></td></tr>
+     *   <tr><th>MIME type</th>                                       
            <th>Description</th>                                 <th>Namespace</th></tr>
+     *   <tr><td>{@code "application/gml+xml"}</td>                   
            <td>Official mime type for OGC GML</td>              <td>{@value
org.apache.sis.XML.Namespaces#GML}</td></tr>
+     *   <tr><td>{@code "application/vnd.eu.europa.ec.inspire.resource+xml"}</td>
 <td>Official mime type for INSPIRE Resources</td>    <td></td></tr>
+     *   <tr><td>{@code "application/vnd.iso.19139+xml"}</td>         
            <td>Unofficial mime type for ISO 19139 metadata</td> <td>{@value
org.apache.sis.XML.Namespaces#GMD}</td></tr>
+     *   <tr><td>{@code "application/vnd.ogc.wms_xml"}</td>           
            <td>Unofficial mime type for OGC WMS</td>            <td></td></tr>
+     *   <tr><td>{@code "application/vnd.ogc.wfs_xml"}</td>           
            <td>Unofficial mime type for OGC WFS </td>           <td></td></tr>
+     *   <tr><td>{@code "application/vnd.ogc.csw_xml"}</td>           
            <td>Unofficial mime type for OGC CSW</td>            <td>{@value
org.apache.sis.XML.Namespaces#CSW}</td></tr>
+     *   <tr><td>{@code "application/vnd.google-earth.kml+xml"}</td>  
            <td></td><td></td></tr>
+     *   <tr><td>{@code "application/rdf+xml"}</td>                   
            <td></td><td></td></tr>
+     *   <tr><td>{@code "application/soap+xml"}</td>                  
            <td></td><td></td></tr>
      * </table>
      *
-     * Consequently, a XML data store provider may need to read the root element of a XML
document in order
-     * to determine the document MIME type.}
-     *
      * @return The storage MIME type, or {@code null} if unknown or not applicable.
      */
     public String getMimeType() {

Modified: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java?rev=1515573&r1=1515572&r2=1515573&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java
[UTF-8] Mon Aug 19 19:31:22 2013
@@ -620,6 +620,17 @@ public class StorageConnector implements
                 final Charset encoding = getOption(OptionKey.ENCODING);
                 final Reader c = (encoding != null) ? new InputStreamReader(input, encoding)
                                                     : new InputStreamReader(input);
+                /*
+                 * Current implementation does not wrap the above Reader in a BufferedReader
because:
+                 *
+                 * 1) InputStreamReader already uses a buffer internally.
+                 * 2) InputStreamReader does not support mark/reset, which is a desired limitation
for now.
+                 *    This is because reseting the Reader would not reset the underlying
InputStream, which
+                 *    would cause other DataStoreProvider.probeContent(…) methods to fail
if they try to use
+                 *    the InputStream. For now we let the InputStreamReader.mark() to throws
an IOException,
+                 *    but we may need to provide our own subclass of BufferedReader in a
future SIS version
+                 *    if mark/reset support is needed here.
+                 */
                 addViewToClose(c, input);
                 return c;
             }

Added: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java?rev=1515573&view=auto
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java
(added)
+++ sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java
[UTF-8] Mon Aug 19 19:31:22 2013
@@ -0,0 +1,81 @@
+/*
+ * 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.storage.xml;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+import org.apache.sis.xml.MetadataMarshallingTest;
+import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests {@link MimeTypeDetector}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ */
+public final strictfp class MimeTypeDetectorTest extends TestCase {
+    /**
+     * Tests a XML file in the {@value org.apache.sis.xml.Namespaces#GMD} namespace
+     * read from a hard-coded string.
+     *
+     * @throws IOException should never happen.
+     */
+    @Test
+    public void testGMDFromString() throws IOException {
+        final StringReader in = new StringReader(XMLStoreTest.XML);
+        assertEquals('<', in.read());
+        assertEquals('?', in.read());
+        final MimeTypeDetector detector = new MimeTypeDetector() {
+            @Override int read() throws IOException {
+                return in.read();
+            }
+        };
+        final String type = detector.getMimeType();
+        assertEquals("application/vnd.iso.19139+xml", type);
+    }
+
+    /**
+     * Tests a XML file in the {@value org.apache.sis.xml.Namespaces#GMD} namespace
+     * read from an input stream.
+     *
+     * @throws IOException if an error occurred while reading the bytes or characters.
+     */
+    @Test
+    @DependsOnMethod("testGMDFromString")
+    public void testGMDFromInputStream() throws IOException {
+        final InputStream in = MetadataMarshallingTest.class.getResourceAsStream("Extent.xml");
+        assertNotNull("Can not read Extent.xml", in);
+        assertEquals('<', in.read());
+        assertEquals('?', in.read());
+        final MimeTypeDetector detector = new MimeTypeDetector() {
+            @Override int read() throws IOException {
+                return in.read();
+            }
+        };
+        final String type = detector.getMimeType();
+        in.close();
+        assertEquals("application/vnd.iso.19139+xml", type);
+    }
+}

Propchange: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/XMLStoreProviderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/XMLStoreProviderTest.java?rev=1515573&r1=1515572&r2=1515573&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/XMLStoreProviderTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/XMLStoreProviderTest.java
[UTF-8] Mon Aug 19 19:31:22 2013
@@ -44,7 +44,9 @@ public final strictfp class XMLStoreProv
     public void testProbeContentFromReader() throws DataStoreException {
         final XMLStoreProvider p = new XMLStoreProvider();
         final StorageConnector c = new StorageConnector(new StringReader(XMLStoreTest.XML));
-        assertEquals(ProbeResult.SUPPORTED, p.probeContent(c));
+        final ProbeResult      r = p.probeContent(c);
         c.closeAllExcept(null);
+        assertTrue  ("isSupported()", r.isSupported());
+        assertEquals("getMimeType()", "application/vnd.iso.19139+xml", r.getMimeType());
     }
 }

Modified: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java?rev=1515573&r1=1515572&r2=1515573&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
[UTF-8] Mon Aug 19 19:31:22 2013
@@ -35,6 +35,7 @@ import org.junit.BeforeClass;
     org.apache.sis.internal.storage.ChannelImageInputStreamTest.class,
     org.apache.sis.storage.ProbeResultTest.class,
     org.apache.sis.storage.StorageConnectorTest.class,
+    org.apache.sis.internal.storage.xml.MimeTypeDetectorTest.class,
     org.apache.sis.internal.storage.xml.XMLStoreProviderTest.class,
     org.apache.sis.internal.storage.xml.XMLStoreTest.class,
     org.apache.sis.index.GeoHashCoderTest.class



Mime
View raw message