You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2017/01/12 07:51:53 UTC
svn commit: r1778395 - in /sis/branches/JDK8: application/sis-console/
application/sis-console/src/test/java/org/apache/sis/console/
storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/
storage/sis-storage/src/test/java/org/apache/sis...
Author: desruisseaux
Date: Thu Jan 12 07:51:53 2017
New Revision: 1778395
URL: http://svn.apache.org/viewvc?rev=1778395&view=rev
Log:
Post-merge consolidation, allow MimeTypeDetector to recognize XML root element in default namespace, and add GPX to the list of formats recognized by the command-line tools.
Modified:
sis/branches/JDK8/application/sis-console/pom.xml
sis/branches/JDK8/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeCommandTest.java
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java
sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java
sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Copyright.java
sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Metadata.java
sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Person.java
sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/xml/stream/StaxStreamReader.java
sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/storage/gpx/ReaderTest.java
Modified: sis/branches/JDK8/application/sis-console/pom.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/pom.xml?rev=1778395&r1=1778394&r2=1778395&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/pom.xml (original)
+++ sis/branches/JDK8/application/sis-console/pom.xml Thu Jan 12 07:51:53 2017
@@ -107,6 +107,12 @@ Console application.
<dependencies>
<dependency>
<groupId>org.apache.sis.storage</groupId>
+ <artifactId>sis-xmlstore</artifactId>
+ <version>${project.version}</version>
+ <scope>runtime</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sis.storage</groupId>
<artifactId>sis-netcdf</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
@@ -150,6 +156,13 @@ Console application.
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sis.storage</groupId>
+ <artifactId>sis-xmlstore</artifactId>
+ <version>${project.version}</version>
+ <type>test-jar</type>
+ <scope>test</scope>
</dependency>
</dependencies>
Modified: sis/branches/JDK8/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeCommandTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeCommandTest.java?rev=1778395&r1=1778394&r2=1778395&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeCommandTest.java [UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeCommandTest.java [UTF-8] Thu Jan 12 07:51:53 2017
@@ -17,6 +17,7 @@
package org.apache.sis.console;
import java.net.URL;
+import org.apache.sis.internal.storage.gpx.MetadataTest;
import org.apache.sis.metadata.iso.extent.DefaultExtentTest;
import org.apache.sis.test.DependsOn;
import org.apache.sis.test.TestCase;
@@ -30,7 +31,7 @@ import static org.junit.Assert.*;
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.4
- * @version 0.4
+ * @version 0.8
* @module
*/
@DependsOn(CommandRunnerTest.class)
@@ -38,7 +39,7 @@ public final strictfp class MimeTypeComm
/**
* Tests the sub-command on a metadata file.
*
- * @throws Exception Should never happen.
+ * @throws Exception if an error occurred while reading the test file.
*/
@Test
public void testWithMetadataXML() throws Exception {
@@ -47,6 +48,21 @@ public final strictfp class MimeTypeComm
final MimeTypeCommand test = new MimeTypeCommand(0, CommandRunner.TEST, url.toString());
test.run();
final String output = test.outputBuffer.toString().trim();
- assertTrue(output, output.endsWith("org/apache/sis/metadata/iso/extent/Extent.xml: application/vnd.iso.19139+xml"));
+ assertTrue(output, output.endsWith("Extent.xml: application/vnd.iso.19139+xml"));
+ }
+
+ /**
+ * Tests the sub-command on a GPX file.
+ *
+ * @throws Exception if an error occurred while reading the test file.
+ */
+ @Test
+ public void testWithMetadataGPX() throws Exception {
+ final URL url = MetadataTest.class.getResource("1.1/metadata.xml");
+ assertNotNull("1.1/metadata.xml", url);
+ final MimeTypeCommand test = new MimeTypeCommand(0, CommandRunner.TEST, url.toString());
+ test.run();
+ final String output = test.outputBuffer.toString().trim();
+ assertTrue(output, output.endsWith("metadata.xml: application/gpx+xml"));
}
}
Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java?rev=1778395&r1=1778394&r2=1778395&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/MimeTypeDetector.java [UTF-8] Thu Jan 12 07:51:53 2017
@@ -88,7 +88,7 @@ abstract class MimeTypeDetector {
/**
* Adds the given byte in the {@link #buffer}, increasing its capacity if needed.
*/
- private void add(final int c) {
+ private void remember(final int c) {
if (length == buffer.length) {
buffer = Arrays.copyOf(buffer, length*2);
}
@@ -132,7 +132,7 @@ abstract class MimeTypeDetector {
*
* <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>
+ * This is the same criterion than {@link String#trim()}, but does not include 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.
@@ -151,20 +151,25 @@ abstract class MimeTypeDetector {
*
* @param word the word to search, as US-ASCII characters.
* @param n number of valid characters in {@code word}.
+ * @param c value of {@code afterSpaces(read())}.
* @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;
+ private int matches(final byte[] word, final int n, int c, final char separator) throws IOException {
+ if (c >= 0) {
+ for (int i=0; i<n; i++) {
+ if (c != word[i]) {
+ return (c >= 0) ? 0 : -1;
+ }
+ c = read();
+ }
+ c = afterSpaces(c);
+ if (c >= 0) {
+ return (c == separator) ? 1 : 0;
}
- c = read();
}
- c = afterSpaces(c);
- return (c == separator) ? 1 : (c >= 0) ? 0 : -1;
+ return -1;
}
/**
@@ -177,8 +182,13 @@ abstract class MimeTypeDetector {
return null;
}
/*
- * At this point, we skipped the "<?xml ...?>" header.
- * Find the first < character, skipping comment (if any).
+ * At this point, we skipped the "<?xml ...?>" header. Skip the "<!-- -->" comments (if any),
+ * then find the first '<' character. For example:
+ *
+ * <?xml version="1.0" encoding="UTF-8"?>
+ * (we are here) \u2192 <!-- Licensed to the Apache Software Foundation (ASF) -->
+ * <gmd:MD_Metadata xmlns:gmd = "http://www.isotc211.org/2005/gmd"/>
+ * (we want to go there) \u2191
*/
int c;
while ((c = readAfter('<')) == '!') {
@@ -194,40 +204,50 @@ abstract class MimeTypeDetector {
}
/*
* At this point, we are after the opening bracket of root element.
- * Skip spaces and read the prefix, which is assumed mandatory.
+ * Skip spaces and read the prefix, which may be absent (we will detect that later).
+ * For example if the line is "<gmd:MD_Metadata \u2026 >", we will remember "gmd" here.
*/
c = afterSpaces(c);
while (c > ' ' && c != ':') {
if (c == '>' || c > MAX_ASCII) {
return null;
}
- add(c);
+ remember(c);
c = read();
}
/*
- * At this point, we got the prefix of the root element. Skip the ':'
- * character and find the "xmlns" attribute following spaces.
+ * At this point, we got what we think is the prefix of the root element. If we find the ':' character,
+ * we consider that this was indeed the prefix (e.g. "gmd" in "<gmd:MD_Metadata \u2026 >"). But if we do not
+ * find the ':' character, then we will consider that the element is in the default namespace, in which
+ * case there is no prefix (length == 0). Exemple: "<MD_Metadata xmlns = \u2026 >"
*/
c = afterSpaces(c);
if (c != ':') {
- return null;
+ length = 0; // XML element in the default namespace: it has no prefix.
+ } else {
+ c = afterSpaces(read());
}
+ /*
+ * Search for "xmlns" keyword, ignoring anything before it. If we find a prefix in the previous step,
+ * we will require that "xmlns" is followed by ":prefix" where "prefix" is the prefix that we found.
+ */
while (true) {
- int m = matches(XMLNS, XMLNS.length, ':');
+ int m = matches(XMLNS, XMLNS.length, c, (length == 0) ? '=' : ':');
if (m != 0) {
if (m < 0) {
- return null;
+ return null; // End of file.
}
- m = matches(buffer, length, '=');
+ if (length == 0) break; // Found match for default namespace.
+ m = matches(buffer, length, afterSpaces(read()), '=');
if (m != 0) {
if (m < 0) {
- return null;
+ return null; // End of file.
}
- break;
+ break; // Found match for prefix.
}
}
// Skip everything up to the next space, and check again.
- while ((c = read()) >= ' ');
+ c = afterSpaces(read());
if (c < 0) return null;
}
/*
@@ -244,7 +264,7 @@ abstract class MimeTypeDetector {
if (c < 0 || c > MAX_ASCII) {
return null;
}
- add(c);
+ remember(c);
c = read();
} while (c != '"');
/*
@@ -258,6 +278,7 @@ abstract class MimeTypeDetector {
* instantiating the {@link ProbeResult}.
*/
final ProbeResult probeContent() throws DataStoreException {
+ boolean isSupported = true;
String mimeType;
try {
mimeType = getMimeType();
@@ -268,8 +289,9 @@ abstract class MimeTypeDetector {
if (insufficientBytes) {
return ProbeResult.INSUFFICIENT_BYTES;
}
+ isSupported = false;
mimeType = StoreProvider.MIME_TYPE;
}
- return new ProbeResult(true, mimeType, null);
+ return new ProbeResult(isSupported, mimeType, null);
}
}
Modified: sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java?rev=1778395&r1=1778394&r2=1778395&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java [UTF-8] Thu Jan 12 07:51:53 2017
@@ -25,6 +25,7 @@ import org.apache.sis.test.TestCase;
import org.junit.Test;
import static java.util.Collections.singletonMap;
+import org.apache.sis.xml.Namespaces;
import static org.apache.sis.xml.Namespaces.GMD;
import static org.junit.Assert.*;
@@ -34,11 +35,23 @@ import static org.junit.Assert.*;
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.4
- * @version 0.4
+ * @version 0.8
* @module
*/
public final strictfp class MimeTypeDetectorTest extends TestCase {
/**
+ * Tests a pseudo-XML file in the default namespace, read from a hard-coded string.
+ *
+ * @throws IOException if an error occurred while reading the bytes or characters.
+ */
+ @Test
+ public void testInDefaultNamespace() throws IOException {
+ testFromString("<?xml version=\"1.0\" standalone=\"yes\"?>\n" +
+ "<MD_Metadata xmlns:xsi=\"" + Namespaces.XSI + "\"" +
+ " xmlns=\"" + Namespaces.GMD + "\"/>\n");
+ }
+
+ /**
* Tests a XML file in the {@value org.apache.sis.xml.Namespaces#GMD} namespace
* read from a hard-coded string.
*
@@ -46,7 +59,14 @@ public final strictfp class MimeTypeDete
*/
@Test
public void testGMDFromString() throws IOException {
- final StringReader in = new StringReader(StoreTest.XML);
+ testFromString(StoreTest.XML);
+ }
+
+ /**
+ * Implementation of test methods using a hard-coded XML string as a source.
+ */
+ private static void testFromString(final String xml) throws IOException {
+ final StringReader in = new StringReader(xml);
assertEquals('<', in.read());
assertEquals('?', in.read());
final MimeTypeDetector detector = new MimeTypeDetector(singletonMap(GMD, "application/vnd.iso.19139+xml")) {
@@ -67,16 +87,17 @@ public final strictfp class MimeTypeDete
@Test
@DependsOnMethod("testGMDFromString")
public void testGMDFromInputStream() throws IOException {
- final InputStream in = DefaultExtentTest.getResource("Extent.xml").openStream();
- assertEquals('<', in.read());
- assertEquals('?', in.read());
- final MimeTypeDetector detector = new MimeTypeDetector(singletonMap(GMD, "application/vnd.iso.19139+xml")) {
- @Override int read() throws IOException {
- return in.read();
- }
- };
- final String type = detector.getMimeType();
- in.close();
+ final String type;
+ try (InputStream in = DefaultExtentTest.getResource("Extent.xml").openStream()) {
+ assertEquals('<', in.read());
+ assertEquals('?', in.read());
+ final MimeTypeDetector detector = new MimeTypeDetector(singletonMap(GMD, "application/vnd.iso.19139+xml")) {
+ @Override int read() throws IOException {
+ return in.read();
+ }
+ };
+ type = detector.getMimeType();
+ }
assertEquals("application/vnd.iso.19139+xml", type);
}
}
Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Copyright.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Copyright.java?rev=1778395&r1=1778394&r2=1778395&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Copyright.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Copyright.java [UTF-8] Thu Jan 12 07:51:53 2017
@@ -31,21 +31,23 @@ import org.opengis.metadata.citation.Cit
import org.opengis.metadata.citation.Contact;
import org.opengis.metadata.citation.DateType;
import org.opengis.metadata.citation.OnlineResource;
-import org.opengis.metadata.citation.Party;
import org.opengis.metadata.citation.PresentationForm;
-import org.opengis.metadata.citation.Responsibility;
import org.opengis.metadata.citation.Role;
import org.opengis.metadata.citation.Series;
import org.opengis.metadata.constraint.LegalConstraints;
-import org.opengis.metadata.constraint.Releasability;
import org.opengis.metadata.constraint.Restriction;
import org.opengis.metadata.extent.Extent;
import org.opengis.metadata.identification.BrowseGraphic;
-import org.opengis.metadata.maintenance.Scope;
import org.opengis.util.InternationalString;
import org.apache.sis.util.iso.SimpleInternationalString;
import org.apache.sis.util.iso.Types;
+// Branch-dependent imports
+import org.opengis.metadata.citation.Party;
+import org.opengis.metadata.citation.Responsibility;
+import org.opengis.metadata.constraint.Releasability;
+import org.opengis.metadata.maintenance.Scope;
+
/**
* Information about the copyright holder and any license governing use of a GPX file.
Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Metadata.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Metadata.java?rev=1778395&r1=1778394&r2=1778395&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Metadata.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Metadata.java [UTF-8] Thu Jan 12 07:51:53 2017
@@ -31,7 +31,6 @@ import org.opengis.metadata.citation.Cit
import org.opengis.metadata.citation.CitationDate;
import org.opengis.metadata.citation.DateType;
import org.opengis.metadata.citation.OnlineResource;
-import org.opengis.metadata.citation.Responsibility;
import org.opengis.metadata.constraint.Constraints;
import org.opengis.metadata.constraint.LegalConstraints;
import org.opengis.metadata.extent.Extent;
@@ -50,6 +49,9 @@ import org.apache.sis.metadata.iso.exten
import org.apache.sis.util.iso.SimpleInternationalString;
import org.apache.sis.util.iso.Types;
+// Branch-dependent imports
+import org.opengis.metadata.citation.Responsibility;
+
/**
* Information about the GPX file, author, and copyright restrictions.
Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Person.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Person.java?rev=1778395&r1=1778394&r2=1778395&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Person.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/gpx/Person.java [UTF-8] Thu Jan 12 07:51:53 2017
@@ -25,8 +25,6 @@ import javax.xml.bind.annotation.adapter
import org.opengis.metadata.citation.Address;
import org.opengis.metadata.citation.Contact;
import org.opengis.metadata.citation.OnlineResource;
-import org.opengis.metadata.citation.Party;
-import org.opengis.metadata.citation.Responsibility;
import org.opengis.metadata.citation.Role;
import org.opengis.metadata.citation.Telephone;
import org.opengis.metadata.extent.Extent;
@@ -34,6 +32,10 @@ import org.opengis.util.InternationalStr
import org.apache.sis.util.iso.SimpleInternationalString;
import org.apache.sis.util.iso.Types;
+// Branch-dependent imports
+import org.opengis.metadata.citation.Party;
+import org.opengis.metadata.citation.Responsibility;
+
/**
* Information about a person or organization.
Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/xml/stream/StaxStreamReader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/xml/stream/StaxStreamReader.java?rev=1778395&r1=1778394&r2=1778395&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/xml/stream/StaxStreamReader.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/storage/xml/stream/StaxStreamReader.java [UTF-8] Thu Jan 12 07:51:53 2017
@@ -20,7 +20,6 @@ import java.util.Date;
import java.util.Map;
import java.util.List;
import java.util.Arrays;
-import java.util.function.Predicate;
import java.util.NoSuchElementException;
import java.net.URI;
import java.io.IOException;
@@ -46,6 +45,7 @@ import org.apache.sis.util.resources.Err
// Branch-dependent imports
import java.util.Spliterator;
import java.util.function.Consumer;
+import java.util.function.Predicate;
import java.time.temporal.Temporal;
import java.time.format.DateTimeParseException;
import org.opengis.feature.Feature;
Modified: sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/storage/gpx/ReaderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/storage/gpx/ReaderTest.java?rev=1778395&r1=1778394&r2=1778395&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/storage/gpx/ReaderTest.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/storage/gpx/ReaderTest.java [UTF-8] Thu Jan 12 07:51:53 2017
@@ -19,7 +19,6 @@ package org.apache.sis.internal.storage.
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
-import java.time.Instant;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polyline;
import org.opengis.geometry.Envelope;
@@ -39,6 +38,7 @@ import static org.apache.sis.test.TestUt
import static org.apache.sis.test.TestUtilities.getSingleton;
// Branch-dependent imports
+import java.time.Instant;
import java.util.stream.Stream;
import org.opengis.feature.Feature;
import org.opengis.metadata.content.FeatureTypeInfo;