sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1774508 - in /sis/branches/JDK8: core/sis-utility/src/main/java/org/apache/sis/internal/util/ storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/ storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/ storage/sis-xmlst...
Date Thu, 15 Dec 2016 18:26:20 GMT
Author: desruisseaux
Date: Thu Dec 15 18:26:19 2016
New Revision: 1774508

URL: http://svn.apache.org/viewvc?rev=1774508&view=rev
Log:
Re-enable parsing of dates in metadata.
Remove the parseMetadata110() method, which is replaced by JAXB usage.

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/TemporalUtilities.java
    sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXReader.java
    sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter100.java
    sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter110.java
    sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/Metadata.java
    sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamReader.java
    sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXReaderTest.java
    sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXWriterTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/TemporalUtilities.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/TemporalUtilities.java?rev=1774508&r1=1774507&r2=1774508&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/TemporalUtilities.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/TemporalUtilities.java
[UTF-8] Thu Dec 15 18:26:19 2016
@@ -32,7 +32,7 @@ import org.apache.sis.internal.system.De
  * @author  Martin Desruisseaux (Geomatys)
  * @author  Guilhem Legal (Geomatys)
  * @since   0.3
- * @version 0.3
+ * @version 0.8
  * @module
  */
 public final class TemporalUtilities extends Static {
@@ -56,8 +56,8 @@ public final class TemporalUtilities ext
     /**
      * Returns a temporal factory if available.
      *
-     * @return The temporal factory.
-     * @throws UnsupportedOperationException If the temporal factory is not available on
the classpath.
+     * @return the temporal factory.
+     * @throws UnsupportedOperationException if the temporal factory is not available on
the classpath.
      */
     public static TemporalFactory getTemporalFactory() throws UnsupportedOperationException
{
         final TemporalFactory factory = DefaultFactories.forClass(TemporalFactory.class);
@@ -77,12 +77,12 @@ public final class TemporalUtilities ext
     /**
      * Creates an instant for the given date.
      *
-     * @param  time The date for which to create instant.
-     * @return The instant.
-     * @throws UnsupportedOperationException If the temporal factory is not available on
the classpath.
+     * @param  time  the date for which to create instant, or {@code null}.
+     * @return the instant, or {@code null} if the given time was null.
+     * @throws UnsupportedOperationException if the temporal factory is not available on
the classpath.
      */
     public static Instant createInstant(final Date time) throws UnsupportedOperationException
{
-        return createInstant(getTemporalFactory(), time);
+        return (time != null) ? createInstant(getTemporalFactory(), time) : null;
     }
 
     /**
@@ -90,10 +90,10 @@ public final class TemporalUtilities ext
      * {@link TemporalFactory#createPosition(Date)} method accepts null dates, which stand
for
      * undetermined position.
      *
-     * @param  begin The begin date, inclusive.
-     * @param  end The end date, inclusive.
-     * @return The period.
-     * @throws UnsupportedOperationException If the temporal factory is not available on
the classpath.
+     * @param  begin  the begin date, inclusive.
+     * @param  end    the end date, inclusive.
+     * @return the period.
+     * @throws UnsupportedOperationException if the temporal factory is not available on
the classpath.
      */
     public static Period createPeriod(final Date begin, final Date end) throws UnsupportedOperationException
{
         final TemporalFactory factory = getTemporalFactory();

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXReader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXReader.java?rev=1774508&r1=1774507&r2=1774508&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXReader.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXReader.java
[UTF-8] Thu Dec 15 18:26:19 2016
@@ -17,20 +17,11 @@
 package org.apache.sis.internal.gpx;
 
 import java.util.List;
-import java.util.Arrays;
 import java.util.ArrayList;
 import java.io.IOException;
 import java.io.EOFException;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.time.Instant;
-import java.time.LocalDate;
-import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeParseException;
-import java.time.temporal.Temporal;
-import java.time.temporal.TemporalAccessor;
-import java.time.temporal.UnsupportedTemporalTypeException;
 import javax.xml.transform.stax.StAXSource;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
@@ -115,17 +106,23 @@ public class GPXReader extends StaxStrea
     private int trackId;
 
     /**
+     * {@code true} if we reached the end of {@code <gpx>} element.
+     */
+    private boolean finished;
+
+    /**
      * Creates a new GPX reader from the given file, URL, stream or reader object.
      *
      * @param  owner      the data store for which this reader is created.
      * @param  connector  information about the storage (URL, stream, <i>etc</i>).
      * @throws DataStoreException if the input type is not recognized.
      * @throws XMLStreamException if an error occurred while opening the XML file.
+     * @throws URISyntaxException if an error occurred while parsing URI in GPX 1.0 metadata.
      * @throws JAXBException if an error occurred while parsing GPX 1.1 metadata.
      * @throws EOFException if the file seems to be truncated.
      */
     public GPXReader(final GPXStore owner, final StorageConnector connector)
-            throws DataStoreException, XMLStreamException, JAXBException, EOFException
+            throws DataStoreException, XMLStreamException, JAXBException, URISyntaxException,
EOFException
     {
         super(owner, connector);
         types = Types.DEFAULT;
@@ -178,52 +175,30 @@ public class GPXReader extends StaxStrea
                                         Errors.Keys.NestedElementNotAllowed_1, Tags.GPX));
                             }
                             case Tags.METADATA: {
+                                // GPX 1.1 metadata
                                 metadata = (Metadata) XML.unmarshal(new StAXSource(reader),
null);
-                                return;
-                            }
-                            case Tags.NAME: {
-                                metadata().name = reader.getElementText();
-                                break;
-                            }
-                            case Tags.DESCRIPTION: {
-                                metadata().description = reader.getElementText();
-                                break;
-                            }
-                            case Tags.AUTHOR: {
-                                if (metadata().author == null) metadata.author = new Person();
-                                metadata.author.name = reader.getElementText();
-                                break;
-                            }
-                            case Tags.EMAIL: {
-                                if (metadata().author == null) metadata.author = new Person();
-                                metadata.author.email = reader.getElementText();
-                                break;
-                            }
-                            case Tags.URL: {
-                                try {
-                                    metadata().links.add(new Link(new URI(reader.getElementText())));
-                                } catch (URISyntaxException ex) {
-                                    throw new XMLStreamException(ex);
-                                }
                                 break;
                             }
-                            case Tags.URL_NAME: {
-                                //reader.getElementText();
-                                break;
-                            }
-                            case Tags.TIME:     metadata().time     = parseTime(reader.getElementText());
break;
-                            case Tags.KEYWORDS: metadata().keywords = Arrays.asList(reader.getElementText().split("
")); break;
-                            case Tags.BOUNDS:   metadata().bounds   = parseBound(); break;
-                            case Tags.WAY_POINT:
+                            // GPX 1.0 metadata
+                            case Tags.NAME:         metadata().name        = getElementText();
  break;
+                            case Tags.DESCRIPTION:  metadata().description = getElementText();
  break;
+                            case Tags.AUTHOR:       person()  .name        = getElementText();
  break;
+                            case Tags.EMAIL:        person()  .email       = getElementText();
  break;
+                            case Tags.URL:          link()    .uri         = getElementAsURI();
 break;
+                            case Tags.URL_NAME:     link()    .text        = getElementText();
  break;
+                            case Tags.TIME:         metadata().time        = getElementAsDate();
break;
+                            case Tags.KEYWORDS:     metadata().keywords    = getElementAsList();
break;
+                            case Tags.BOUNDS:       metadata().bounds      = parseBound();
      break;
+                            case Tags.WAY_POINT:    // stop metadata parsing.
                             case Tags.TRACKS:
-                            case Tags.ROUTES: return;
+                            case Tags.ROUTES:       return;
                         }
                     }
                     break;
                 }
                 case END_ELEMENT: {
                     if (isNamespace(reader.getNamespaceURI()) && Tags.GPX.equals(reader.getLocalName()))
{
-                        // TODO
+                        finished = true;
                     }
                     break;
                 }
@@ -245,6 +220,26 @@ public class GPXReader extends StaxStrea
         return metadata;
     }
 
+    private Person person() {
+        final Metadata metadata = metadata();
+        if (metadata.author == null) {
+            metadata.author = new Person();
+        }
+        return metadata.author;
+    }
+
+    private Link link() {
+        final List<Link> links = metadata().links;
+        final Link first;
+        if (links.isEmpty()) {
+            first = new Link();
+            links.add(first);
+        } else {
+            first = links.get(0);
+        }
+        return first;
+    }
+
     /**
      * Get GPX file version.
      * This method will return a result only if called only after the input has been set.
@@ -286,6 +281,7 @@ public class GPXReader extends StaxStrea
      *         or underlying stream caused an exception
      */
     public boolean hasNext() throws XMLStreamException {
+        if (finished) return false;
         findNext();
         return current != null;
     }
@@ -310,8 +306,6 @@ public class GPXReader extends StaxStrea
      */
     private void findNext() throws XMLStreamException {
         final XMLStreamReader reader = getReader();
-        if (current != null) return;
-
         boolean first = true;
         while ( first || (current == null && reader.hasNext()) ) {
             final int type;
@@ -338,76 +332,6 @@ public class GPXReader extends StaxStrea
     }
 
     /**
-     * Parse current metadata element.
-     * The stax reader must be placed to the start element of the metadata.
-     */
-    private Metadata parseMetadata110() throws XMLStreamException, EOFException {
-        final XMLStreamReader reader = getReader();
-        final Metadata metadata = new Metadata();
-        while (reader.hasNext()) {
-            switch (reader.next()) {
-                case START_ELEMENT: {
-                    switch (reader.getLocalName()) {
-                        case Tags.NAME: metadata.name = reader.getElementText(); break;
-                        case Tags.DESCRIPTION: metadata.description = reader.getElementText();
break;
-                        case Tags.AUTHOR: metadata.author = parsePerson(); break;
-                        case Tags.COPYRIGHT: metadata.copyright = parseCopyright(); break;
-                        case Tags.LINK: metadata.links.add(parseLink()); break;
-                        case Tags.TIME: metadata.time = parseTime(reader.getElementText());
break;
-                        case Tags.KEYWORDS: metadata.keywords = Arrays.asList(reader.getElementText().split("
")); break;
-                        case Tags.BOUNDS: metadata.bounds = parseBound(); break;
-                    }
-                    break;
-                }
-                case END_ELEMENT: {
-                    switch (reader.getLocalName()) {
-                        case Tags.METADATA:
-                            // End of the metadata element
-                            return metadata;
-                    }
-                    break;
-                }
-            }
-        }
-        throw new XMLStreamException("Error in xml file, relation tag without end.");
-    }
-
-    /**
-     * Parse current copyright element.
-     * The stax reader must be placed to the start element of the copyright.
-     */
-    private Copyright parseCopyright() throws XMLStreamException {
-        final XMLStreamReader reader = getReader();
-        final Copyright copyright = new Copyright();
-        copyright.author = reader.getAttributeValue(null, Attributes.AUTHOR);
-
-        while (reader.hasNext()) {
-            switch (reader.next()) {
-                case START_ELEMENT: {
-                    final String localName = reader.getLocalName();
-                    if (Tags.YEAR.equalsIgnoreCase(localName)) {
-                        copyright.year = Integer.valueOf(reader.getElementText());
-                    } else if (Tags.LICENSE.equalsIgnoreCase(localName)) {
-                        try {
-                            copyright.license = new URI(reader.getElementText());
-                        } catch (URISyntaxException ex) {
-                            throw new XMLStreamException(ex);
-                        }
-                    }
-                    break;
-                }
-                case END_ELEMENT: {
-                    if (Tags.COPYRIGHT.equalsIgnoreCase(reader.getLocalName())) {
-                        return copyright;
-                    }
-                    break;
-                }
-            }
-        }
-        throw new XMLStreamException("Error in xml file, copyright tag without end.");
-    }
-
-    /**
      * Parse current URI element.
      * The stax reader must be placed to the start element.
      */
@@ -444,39 +368,6 @@ public class GPXReader extends StaxStrea
     }
 
     /**
-     * Parse current Person element.
-     * The stax reader must be placed to the start element.
-     */
-    private Person parsePerson() throws XMLStreamException {
-        final XMLStreamReader reader = getReader();
-        final Person person = new Person();
-
-        while (reader.hasNext()) {
-            switch (reader.next()) {
-                case START_ELEMENT: {
-                    final String localName = reader.getLocalName();
-                    if (Tags.NAME.equalsIgnoreCase(localName)) {
-                        person.name = reader.getElementText();
-                    } else if (Tags.EMAIL.equalsIgnoreCase(localName)) {
-                        person.email = reader.getElementText();
-                    } else if (Tags.LINK.equalsIgnoreCase(localName)) {
-                        person.link = parseLink();
-                    }
-                    break;
-                }
-                case END_ELEMENT: {
-                    if (Tags.AUTHOR.equalsIgnoreCase(reader.getLocalName())) {
-                        // End of the author element
-                        return person;
-                    }
-                    break;
-                }
-            }
-        }
-        throw new XMLStreamException("Error in xml file, person tag without end.");
-    }
-
-    /**
      * Parse current Envelope element.
      * The stax reader must be placed to the start element.
      */
@@ -531,7 +422,7 @@ public class GPXReader extends StaxStrea
                     if (Tags.ELEVATION.equalsIgnoreCase(localName)) {
                         feature.setPropertyValue(Tags.ELEVATION, Double.valueOf(reader.getElementText()));
                     } else if (Tags.TIME.equalsIgnoreCase(localName)) {
-                        feature.setPropertyValue(Tags.TIME, parseTime(reader.getElementText()));
+                        feature.setPropertyValue(Tags.TIME, getElementAsTemporal());
                     } else if (Tags.MAGNETIC_VAR.equalsIgnoreCase(localName)) {
                         feature.setPropertyValue(Tags.MAGNETIC_VAR, Double.valueOf(reader.getElementText()));
                     } else if (Tags.GEOID_HEIGHT.equalsIgnoreCase(localName)) {
@@ -741,28 +632,4 @@ public class GPXReader extends StaxStrea
         }
         throw new XMLStreamException("Error in xml file, "+Tags.TRACKS+" tag without end.");
     }
-
-    /**
-     * Parse date or date time from string.
-     * Dates and times in GPX files are Coordinated Universal Time (UTC) using ISO 8601 format.
-     *
-     * @param dateStr date in ISO date or data time format
-     */
-    private static Temporal parseTime(String dateStr) {
-        try {
-            final DateTimeFormatter format = DateTimeFormatter.ISO_INSTANT;
-            final TemporalAccessor accessor = format.parse(dateStr);
-            return Instant.from(accessor);
-        } catch (UnsupportedTemporalTypeException | DateTimeParseException ex) {
-            try {
-                final DateTimeFormatter format = DateTimeFormatter.ISO_DATE;
-                final TemporalAccessor accessor = format.parse(dateStr);
-                return LocalDate.from(accessor);
-            } catch (UnsupportedTemporalTypeException | DateTimeParseException e) {
-                final DateTimeFormatter format = DateTimeFormatter.ISO_DATE_TIME;
-                final TemporalAccessor accessor = format.parse(dateStr);
-                return LocalDateTime.from(accessor);
-            }
-        }
-    }
 }

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter100.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter100.java?rev=1774508&r1=1774507&r2=1774508&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter100.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter100.java
[UTF-8] Thu Dec 15 18:26:19 2016
@@ -228,7 +228,7 @@ public class GPXWriter100 extends StaxSt
         writeLinkURIs(metadata.links);
 
         if (metadata.time != null) {
-            writeSimpleTag(namespace, Tags.TIME, toString(metadata.time));
+            writeSimpleTag(namespace, Tags.TIME, metadata.time.toInstant().toString());
         }
 
         writeSimpleTag(namespace, Tags.KEYWORDS, toSpaceSeparatedList(metadata.keywords));
@@ -423,7 +423,7 @@ public class GPXWriter100 extends StaxSt
      * @param temp not null
      * @return String representation
      */
-    protected static String toString(final Temporal temp){
+    protected static String toString(final Temporal temp) {
         if(temp instanceof LocalDate){
             return DateTimeFormatter.ISO_DATE.format(temp);
         }else if(temp instanceof LocalDateTime){

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter110.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter110.java?rev=1774508&r1=1774507&r2=1774508&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter110.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter110.java
[UTF-8] Thu Dec 15 18:26:19 2016
@@ -16,9 +16,9 @@
  */
 package org.apache.sis.internal.gpx;
 
-import java.time.temporal.Temporal;
 import java.util.Collection;
 import java.io.IOException;
+import java.util.Date;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import org.apache.sis.storage.DataStoreException;
@@ -70,9 +70,9 @@ public class GPXWriter110 extends GPXWri
             writeLink(uri);
         }
 
-        final Temporal d = metadata.time;
+        final Date d = metadata.time;
         if (d != null) {
-            writeSimpleTag(namespace, Tags.TIME, toString(d));
+            writeSimpleTag(namespace, Tags.TIME, d.toInstant().toString());
         }
 
         writeSimpleTag(namespace, Tags.KEYWORDS, toSpaceSeparatedList(metadata.keywords));

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/Metadata.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/Metadata.java?rev=1774508&r1=1774507&r2=1774508&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/Metadata.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/Metadata.java
[UTF-8] Thu Dec 15 18:26:19 2016
@@ -16,8 +16,6 @@
  */
 package org.apache.sis.internal.gpx;
 
-import java.time.Instant;
-import java.time.temporal.Temporal;
 import java.util.AbstractList;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -39,11 +37,11 @@ import org.opengis.metadata.extent.Exten
 import org.opengis.metadata.identification.Keywords;
 import org.opengis.util.InternationalString;
 
-import org.apache.sis.internal.simple.SimpleMetadata;
 import org.apache.sis.io.TableAppender;
+import org.apache.sis.internal.simple.SimpleMetadata;
+import org.apache.sis.util.iso.SimpleInternationalString;
 import org.apache.sis.metadata.iso.citation.DefaultCitationDate;
 import org.apache.sis.metadata.iso.identification.DefaultKeywords;
-import org.apache.sis.util.iso.SimpleInternationalString;
 
 
 /**
@@ -58,11 +56,11 @@ import org.apache.sis.util.iso.SimpleInt
  *     <desc> String </desc>
  *     <author> Person </author>
  *     <copyright> Copyright </copyright>
- *     <link> URI </link>
+ *     <link href="URI"/>
  *     <time> Temporal </time>
- *     <keywords> String </keywords>
+ *     <keywords> String[] </keywords>
  *     <bounds> Envelope </bounds>
- *     <extensions> (ignored) </extensions>
+ *     <extensions> any object known to JAXB </extensions>
  *   </metadata>
  * }
  *
@@ -120,9 +118,12 @@ public final class Metadata extends Simp
      * The creation date of the file.
      *
      * @see #getDates()
+     *
+     * @todo We could like to use {@link java.time}, but it does not yet word out-of-the-box
with JAXB
+     *       (we need adapter). Furthermore current GeoAPI interfaces does not yet use {@code
java.time}.
      */
-//  @XmlElement(name = Tags.TIME)
-    public Temporal time;
+    @XmlElement(name = Tags.TIME)
+    public Date time;
 
     /**
      * Keywords associated with the file, or {@code null} if unspecified.
@@ -239,8 +240,7 @@ public final class Metadata extends Simp
     @Override
     public Collection<CitationDate> getDates() {
         if (time != null) {
-            final CitationDate date = new DefaultCitationDate(Date.from(Instant.from(time)),
DateType.CREATION);
-            return Collections.singleton(date);
+            return Collections.singleton(new DefaultCitationDate(time, DateType.CREATION));
         }
         return super.getDates();
     }
@@ -308,7 +308,7 @@ public final class Metadata extends Simp
         append(table, "Author",      author);
         append(table, "Copyright",   copyright);
         append(table, "Link(s)",     links, System.lineSeparator());
-        append(table, "Time",        time);
+        append(table, "Time",        (time != null) ? time.toInstant() : null);
         append(table, "Keywords",    keywords, " ");
         append(table, "Bounds",      bounds);
         table.appendHorizontalSeparator();

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamReader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamReader.java?rev=1774508&r1=1774507&r2=1774508&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamReader.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamReader.java
[UTF-8] Thu Dec 15 18:26:19 2016
@@ -16,12 +16,18 @@
  */
 package org.apache.sis.internal.xml;
 
-import java.io.IOException;
-import java.io.InputStream;
+import java.util.Date;
+import java.util.List;
+import java.util.Arrays;
+import java.util.function.Predicate;
 import java.util.NoSuchElementException;
-import java.io.EOFException;
+import java.net.URI;
 import java.io.Reader;
-import java.util.function.Predicate;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.EOFException;
+import java.net.URISyntaxException;
+import java.time.LocalDate;
 import javax.xml.stream.XMLEventReader;
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamConstants;
@@ -34,6 +40,8 @@ import javax.xml.transform.sax.SAXSource
 import javax.xml.transform.stax.StAXSource;
 import org.xml.sax.InputSource;
 import org.w3c.dom.Node;
+import org.apache.sis.internal.jaxb.Context;
+import org.apache.sis.internal.util.StandardDateFormat;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.DataStoreContentException;
@@ -41,6 +49,11 @@ import org.apache.sis.storage.Unsupporte
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.Classes;
 
+// Branch-dependent imports
+import java.time.temporal.Temporal;
+import java.time.temporal.TemporalAccessor;
+import java.time.format.DateTimeParseException;
+
 
 /**
  * Base class of Apache SIS readers of XML files using STAX parser.
@@ -238,6 +251,83 @@ public abstract class StaxStreamReader e
     }
 
     /**
+     * Returns the current value of {@link XMLStreamReader#getElementText()},
+     * or {@code null} if that value is null or empty.
+     *
+     * @return the current text element, or {@code null} if empty.
+     * @throws XMLStreamException if a text element can not be returned.
+     */
+    protected final String getElementText() throws XMLStreamException {
+        String text = getReader().getElementText();
+        if (text != null) {
+            text = text.trim();
+            if (!text.isEmpty()) {
+                return text;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Returns the current value of {@link XMLStreamReader#getElementText()} as an URI,
+     * or {@code null} if that value is null or empty.
+     *
+     * @return the current text element as an URI, or {@code null} if empty.
+     * @throws XMLStreamException if a text element can not be returned.
+     * @throws URISyntaxException if the text can not be parsed as an URI.
+     */
+    protected final URI getElementAsURI() throws XMLStreamException, URISyntaxException {
+        final Context context = Context.current();
+        return Context.converter(context).toURI(context, getElementText());
+    }
+
+    /**
+     * Returns the current value of {@link XMLStreamReader#getElementText()} as a date,
+     * or {@code null} if that value is null or empty.
+     *
+     * @return the current text element as a date, or {@code null} if empty.
+     * @throws XMLStreamException if a text element can not be returned.
+     * @throws DateTimeParseException if the text can not be parsed as a date.
+     */
+    protected final Date getElementAsDate() throws XMLStreamException {
+        final String text = getElementText();
+        return (text != null) ? StandardDateFormat.toDate(StandardDateFormat.FORMAT.parse(text))
: null;
+    }
+
+    /**
+     * Returns the current value of {@link XMLStreamReader#getElementText()} as a temporal
object,
+     * or {@code null} if that value is null or empty.
+     *
+     * @return the current text element as a temporal object, or {@code null} if empty.
+     * @throws XMLStreamException if a text element can not be returned.
+     * @throws DateTimeParseException if the text can not be parsed as a date.
+     */
+    protected final Temporal getElementAsTemporal() throws XMLStreamException {
+        final String text = getElementText();
+        if (text != null) {
+            final TemporalAccessor accessor = StandardDateFormat.FORMAT.parse(text);
+            if (accessor instanceof Temporal) {
+                return (Temporal) accessor;
+            }
+            return LocalDate.from(accessor);
+            // TODO: need more extensive check.
+        }
+        return null;
+    }
+
+    /**
+     * Returns the current value of {@link XMLStreamReader#getElementText()} as a list of
strings,
+     * or {@code null} if that value is null or empty.
+     *
+     * @return the current text element as a list.
+     * @throws XMLStreamException if a text element can not be returned.
+     */
+    protected final List<String> getElementAsList() throws XMLStreamException {
+        final String text = getElementText();
+        return (text != null) ? Arrays.asList(text.split(" ")) : null;
+    }
+
+    /**
      * Parses the given string as a boolean value. This method performs the same parsing
than
      * {@link Boolean#parseBoolean(String)} with one extension: the "0" value is considered
      * as {@code false} and the "1" value as {@code true}.

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXReaderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXReaderTest.java?rev=1774508&r1=1774507&r2=1774508&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXReaderTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXReaderTest.java
[UTF-8] Thu Dec 15 18:26:19 2016
@@ -20,16 +20,16 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import com.esri.core.geometry.Point;
-import java.time.Instant;
 import org.opengis.geometry.Envelope;
 import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
+import static org.apache.sis.test.TestUtilities.date;
 
 // Branch-dependent imports
-import java.time.format.DateTimeFormatter;
+import java.time.LocalDate;
 import org.opengis.feature.Feature;
 
 
@@ -75,7 +75,7 @@ public final strictfp class GPXReaderTes
 
             assertEquals("Sample", data.name);
             assertEquals("GPX test file", data.description);
-            assertEquals(parseTime("2010-03-01T00:00:00Z"), data.time);
+            assertEquals(date("2010-03-01 00:00:00"), data.time);
             assertArrayEquals(new String[] {"sample", "metadata"}, data.keywords.toArray());
             assertBoundsEquals(-20, 30, 10, 40, data.bounds);
 
@@ -102,12 +102,12 @@ public final strictfp class GPXReaderTes
 
             assertEquals("Sample", data.name);
             assertEquals("GPX test file", data.description);
-//          assertEquals(parseTime("2010-03-01T00:00:00Z"), data.time);
+            assertEquals(date("2010-03-01 00:00:00"), data.time);
             assertArrayEquals(new String[] {"sample", "metadata"}, data.keywords.toArray());
             assertBoundsEquals(-20, 30, 10, 40, data.bounds);
 
             assertEquals("Jean-Pierre", data.author.name);
-//          assertEquals("jean.pierre@test.com", data.author.email);
+            assertEquals("jean.pierre@test.com", data.author.email);
             assertEquals("http://someone-site.org", data.author.link.toString());
 
             assertEquals("Apache", data.copyright.author);
@@ -462,7 +462,7 @@ public final strictfp class GPXReaderTes
             assertEquals(15.0,                  ((Point)f.getPropertyValue("@geometry")).getX(),
DELTA);
             assertEquals(10.0,                  ((Point)f.getPropertyValue("@geometry")).getY(),
DELTA);
             assertEquals(140.0,                 f.getPropertyValue("ele"));
-            assertEquals(parseTime("2010-01-10T00:00:00Z"),f.getPropertyValue("time"));
+            assertEquals(LocalDate.parse("2010-01-10"),f.getPropertyValue("time"));
             assertEquals(35.0,                  f.getPropertyValue("magvar"));
             assertEquals(112.32,                f.getPropertyValue("geoidheight"));
             assertEquals("first point",         f.getPropertyValue("name"));
@@ -532,7 +532,7 @@ public final strictfp class GPXReaderTes
             assertEquals(35.0,                  ((Point) f.getPropertyValue("@geometry")).getX(),
DELTA);
             assertEquals(30.0,                  ((Point) f.getPropertyValue("@geometry")).getY(),
DELTA);
             assertEquals(150.0,                 f.getPropertyValue("ele"));
-            assertEquals(parseTime("2010-01-30T00:00:00Z"),f.getPropertyValue("time"));
+            assertEquals(LocalDate.parse("2010-01-30"),f.getPropertyValue("time"));
             assertEquals(25.0,                  f.getPropertyValue("magvar"));
             assertEquals(142.32,                f.getPropertyValue("geoidheight"));
             assertEquals("third point",         f.getPropertyValue("name"));
@@ -569,9 +569,4 @@ public final strictfp class GPXReaderTes
             fail("unexpected point number :" + num);
         }
     }
-
-    private static Instant parseTime(String str) {
-        final DateTimeFormatter format = DateTimeFormatter.ISO_INSTANT;
-        return Instant.from(format.parse(str));
-    }
 }

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXWriterTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXWriterTest.java?rev=1774508&r1=1774507&r2=1774508&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXWriterTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXWriterTest.java
[UTF-8] Thu Dec 15 18:26:19 2016
@@ -18,8 +18,7 @@ package org.apache.sis.internal.gpx;
 
 import java.net.URI;
 import java.io.File;
-import java.time.Instant;
-import java.time.LocalDate;
+import java.util.Date;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -35,6 +34,7 @@ import org.junit.Test;
 import static org.junit.Assert.*;
 
 // Branch-dependent imports
+import java.time.LocalDate;
 import org.opengis.feature.Feature;
 
 
@@ -91,7 +91,7 @@ public final strictfp class GPXWriterTes
         metaData.author = person;
         metaData.copyright = copyright;
         metaData.links.addAll(Arrays.asList(new Link(new URI("http://adress1.org")), new
Link(new URI("http://adress2.org"))));
-        if (false) metaData.time = Instant.now();   // TODO
+        metaData.time = new Date();
         metaData.keywords = Arrays.asList("test", "sample");
         metaData.bounds = bounds;
 



Mime
View raw message