sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1832180 - in /sis/branches/JDK8/core/sis-metadata/src: main/java/org/apache/sis/internal/jaxb/gco/ main/java/org/apache/sis/internal/metadata/ main/java/org/apache/sis/util/iso/ test/java/org/apache/sis/metadata/iso/
Date Thu, 24 May 2018 15:10:32 GMT
Author: desruisseaux
Date: Thu May 24 15:10:32 2018
New Revision: 1832180

URL: http://svn.apache.org/viewvc?rev=1832180&view=rev
Log:
Add a partial support of Record and RecordType (un)marshalling.

Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/gco/GO_Record.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/RecordSchemaSIS.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/TransformationAccuracy.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/DefaultRecord.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/DefaultRecordType.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/RecordDefinition.java
    sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/MarshallingTest.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/gco/GO_Record.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/gco/GO_Record.java?rev=1832180&r1=1832179&r2=1832180&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/gco/GO_Record.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/gco/GO_Record.java
[UTF-8] Thu May 24 15:10:32 2018
@@ -86,7 +86,9 @@ public class GO_Record extends PropertyT
      * @param  metadata  the unmarshalled metadata.
      */
     public final void setElement(final DefaultRecord metadata) {
-        this.metadata = metadata;
+        if (!metadata.getAttributes().isEmpty()) {
+            this.metadata = metadata;
+        }
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/RecordSchemaSIS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/RecordSchemaSIS.java?rev=1832180&r1=1832179&r2=1832180&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/RecordSchemaSIS.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/RecordSchemaSIS.java
[UTF-8] Thu May 24 15:10:32 2018
@@ -18,24 +18,43 @@ package org.apache.sis.internal.metadata
 
 import java.io.Serializable;
 import java.io.ObjectStreamException;
+import java.util.Collections;
+import org.opengis.util.InternationalString;
 import org.apache.sis.internal.util.Constants;
+import org.apache.sis.util.iso.DefaultRecordType;
 import org.apache.sis.util.iso.DefaultRecordSchema;
+import org.apache.sis.util.resources.Vocabulary;
 
 
 /**
  * The system-wide schema in the "SIS" namespace.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.7
+ * @version 1.0
  * @since   0.7
  * @module
  */
 @SuppressWarnings("serial")  // serialVersionUID not needed because of writeReplace().
-final class RecordSchemaSIS extends DefaultRecordSchema implements Serializable {
+public final class RecordSchemaSIS extends DefaultRecordSchema implements Serializable {
     /**
      * The schema used in SIS for creating records.
      */
-    static final DefaultRecordSchema INSTANCE = new RecordSchemaSIS();
+    public static final DefaultRecordSchema INSTANCE = new RecordSchemaSIS();
+
+    /**
+     * The type of record instances for holding a {@link String} value.
+     */
+    public static final DefaultRecordType STRING;
+
+    /**
+     * The type of record instances for holding a {@link Double} value.
+     */
+    public static final DefaultRecordType REAL;
+    static {
+        final InternationalString label = Vocabulary.formatInternational(Vocabulary.Keys.Value);
+        STRING = (DefaultRecordType) INSTANCE.createRecordType("CharacterSequence", Collections.singletonMap(label,
String.class));
+        REAL   = (DefaultRecordType) INSTANCE.createRecordType("Real",              Collections.singletonMap(label,
Double.class));
+    }
 
     /**
      * Creates the unique instance.
@@ -46,6 +65,9 @@ final class RecordSchemaSIS extends Defa
 
     /**
      * On serialization, returns a proxy which will be resolved as {@link #INSTANCE} on deserialization.
+     *
+     * @return the object to use after deserialization.
+     * @throws ObjectStreamException if the serialized object defines an unknown data type.
      */
     protected Object writeReplace() throws ObjectStreamException {
         return new Proxy();

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/TransformationAccuracy.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/TransformationAccuracy.java?rev=1832180&r1=1832179&r2=1832180&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/TransformationAccuracy.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/metadata/TransformationAccuracy.java
[UTF-8] Thu May 24 15:10:32 2018
@@ -34,7 +34,7 @@ import org.apache.sis.util.Static;
  * Creates a record reporting coordinate transformation accuracy.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.8
+ * @version 1.0
  * @since   0.7
  * @module
  */
@@ -46,12 +46,6 @@ public final class TransformationAccurac
             Vocabulary.formatInternational(Vocabulary.Keys.TransformationAccuracy);
 
     /**
-     * The type of record instances which will hold coordinate transformation accuracy values.
-     */
-    private static final RecordType TYPE = RecordSchemaSIS.INSTANCE.createRecordType("Real",
-            Collections.singletonMap(Vocabulary.formatInternational(Vocabulary.Keys.Value),
Double.class));
-
-    /**
      * Cache the positional accuracies. Most coordinate operation use a small set of accuracy
values.
      */
     private static final WeakValueHashMap<Double,PositionalAccuracy> CACHE = new WeakValueHashMap<>(Double.class);
@@ -72,13 +66,14 @@ public final class TransformationAccurac
     public static PositionalAccuracy create(final Double accuracy) {
         PositionalAccuracy p = CACHE.get(accuracy);
         if (p == null) {
-            final DefaultRecord record = new DefaultRecord(TYPE);
+            final RecordType type = RecordSchemaSIS.REAL;
+            final DefaultRecord record = new DefaultRecord(type);
             record.setAll(accuracy);
 
             final DefaultQuantitativeResult result = new DefaultQuantitativeResult();
             result.setValues(Collections.singletonList(record));
             result.setValueUnit(Units.METRE);              // In metres by definition in
the EPSG database.
-            result.setValueType(TYPE);
+            result.setValueType(type);
 
             final DefaultAbsoluteExternalPositionalAccuracy element =
                     new DefaultAbsoluteExternalPositionalAccuracy(result);

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/DefaultRecord.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/DefaultRecord.java?rev=1832180&r1=1832179&r2=1832180&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/DefaultRecord.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/DefaultRecord.java
[UTF-8] Thu May 24 15:10:32 2018
@@ -22,9 +22,11 @@ import java.util.AbstractMap;
 import java.util.AbstractSet;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
+import java.util.Collections;
 import java.util.Objects;
 import java.io.Serializable;
 import java.lang.reflect.Array;
+import javax.xml.bind.annotation.XmlValue;
 import org.opengis.util.MemberName;
 import org.opengis.util.Record;
 import org.opengis.util.RecordType;
@@ -33,6 +35,7 @@ import org.apache.sis.util.Utilities;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.internal.util.AbstractMapEntry;
+import org.apache.sis.internal.metadata.RecordSchemaSIS;
 
 
 /**
@@ -69,23 +72,16 @@ public class DefaultRecord implements Re
     private static final long serialVersionUID = -5293250754663538325L;
 
     /**
-     * The type definition of this record.
+     * The type definition of this record. Can not be {@code null}.
      */
     final RecordDefinition definition;
 
     /**
      * The record values in an array. May be an array of primitive type for compactness,
-     * which is why the type is not {@code Object[]}.
+     * which is why the type is not {@code Object[]}. Should never be {@code null}, except
+     * temporarily during XML unmarshalling.
      */
-    private final Object values;
-
-    /**
-     * Constructs an initially empty record. Used by JAXB.
-     */
-    private DefaultRecord() {
-        definition = null;
-        values = null;
-    }
+    private Object values;
 
     /**
      * Creates a new record for the given record type.
@@ -170,8 +166,7 @@ public class DefaultRecord implements Re
      */
     @Override
     public RecordType getRecordType() {
-        // Should never be null, unless temporarily during unmarshalling.
-        return (definition != null) ? definition.getRecordType() : null;
+        return definition.getRecordType();
     }
 
     /**
@@ -185,6 +180,9 @@ public class DefaultRecord implements Re
      */
     @Override
     public Map<MemberName, Object> getAttributes() {
+        if (values == null) {                         // Should never be null, except temporarily
at XML unmarshalling time.
+            return Collections.emptyMap();
+        }
         return new AbstractMap<MemberName, Object>() {
             /** Returns the number of members in the record. */
             @Override
@@ -397,4 +395,49 @@ public class DefaultRecord implements Re
     public String toString() {
         return definition.toString("Record", values);
     }
+
+
+
+
+    //////////////////////////////////////////////////////////////////////////////////////////////////
+    ////////                                                                            
     ////////
+    ////////                               XML support with JAXB                        
     ////////
+    ////////                                                                            
     ////////
+    ////////        The following methods are invoked by JAXB using reflection (even if 
     ////////
+    ////////        they are private) or are helpers for other methods invoked by JAXB. 
     ////////
+    ////////        Those methods can be safely removed if Geographic Markup Language   
     ////////
+    ////////        (GML) support is not needed.                                        
     ////////
+    ////////                                                                            
     ////////
+    //////////////////////////////////////////////////////////////////////////////////////////////////
+
+    /**
+     * Constructs an initially empty record. Used by JAXB.
+     */
+    private DefaultRecord() {
+        definition = RecordSchemaSIS.STRING;
+    }
+
+    /**
+     * Returns the record value as a string.
+     */
+    @XmlValue
+    private String getValue() {
+        if (values != null) {
+            switch (Array.getLength(values)) {
+                case 0:  break;
+                case 1:  return String.valueOf(Array.get(values, 0));
+                default: return definition.toString(null, values);
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Sets the record value as a string.
+     */
+    private void setValue(String value) {
+        if (value != null && !(value = value.trim()).isEmpty()) {
+            values = new String[] {value};
+        }
+    }
 }

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/DefaultRecordType.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/DefaultRecordType.java?rev=1832180&r1=1832179&r2=1832180&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/DefaultRecordType.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/DefaultRecordType.java
[UTF-8] Thu May 24 15:10:32 2018
@@ -27,6 +27,7 @@ import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.InvalidObjectException;
 import javax.xml.bind.annotation.XmlType;
+import javax.xml.bind.annotation.XmlValue;
 import org.opengis.util.Type;
 import org.opengis.util.TypeName;
 import org.opengis.util.LocalName;
@@ -37,11 +38,13 @@ import org.opengis.util.NameFactory;
 import org.opengis.util.Record;
 import org.opengis.util.RecordType;
 import org.opengis.util.RecordSchema;
+import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.collection.Containers;
 import org.apache.sis.util.ObjectConverters;
 import org.apache.sis.internal.converter.SurjectiveConverter;
+import org.apache.sis.internal.metadata.RecordSchemaSIS;
 
 
 /**
@@ -86,7 +89,7 @@ import org.apache.sis.internal.converter
  * so users wanting serialization may need to provide their own schema.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 0.5
+ * @version 1.0
  *
  * @see DefaultRecord
  * @see DefaultRecordSchema
@@ -448,7 +451,40 @@ public class DefaultRecordType extends R
      */
     @SuppressWarnings("unused")
     private DefaultRecordType() {
-        typeName  = null;
-        container = null;
+        final DefaultRecordType type = RecordSchemaSIS.STRING;
+        typeName  = type.typeName;
+        container = type.container;
+    }
+
+    /**
+     * Returns the record type value as a string. Current implementation returns the members
with
+     * one member per line, but it may change in any future version for adapting to common
practice.
+     */
+    @XmlValue
+    private String getValue() {
+        switch (size()) {
+            case 0:  return null;
+            case 1:  return String.valueOf(memberTypes[0]);
+            default: return toString(null, null);
+        }
+    }
+
+    /**
+     * Sets the record type value as a string. Current implementation expect one member per
line.
+     */
+    private void setValue(final String value) {
+        if (value != null) {
+            final Map<MemberName,Type> members = new LinkedHashMap<>();
+            for (CharSequence element : CharSequences.splitOnEOL(value)) {
+                final int s = ((String) element).indexOf(':');
+                if (s >= 0) {
+                    element = element.subSequence(0, CharSequences.skipTrailingWhitespaces(element,
0, s));
+                    // TODO: the part after ":" is the description. For now, we have no room
for storing it.
+                }
+                final MemberName m = Names.createMemberName(null, null, element, String.class);
+                members.put(m, RecordSchemaSIS.INSTANCE.toAttributeType(String.class));
+            }
+            memberTypes = computeTransientFields(members);
+        }
     }
 }

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/RecordDefinition.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/RecordDefinition.java?rev=1832180&r1=1832179&r2=1832180&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/RecordDefinition.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/util/iso/RecordDefinition.java
[UTF-8] Thu May 24 15:10:32 2018
@@ -48,7 +48,7 @@ import org.apache.sis.internal.util.Coll
  * {@link #computeTransientFields(Map)}.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.5
+ * @version 1.0
  * @since   0.5
  * @module
  */
@@ -233,7 +233,7 @@ abstract class RecordDefinition {
     /**
      * Returns a string representation of a {@code Record} or {@code RecordType}.
      *
-     * @param  head    either {@code "Record"} or {@code "RecordType"}.
+     * @param  head    either {@code "Record"} or {@code "RecordType"} or {@code null}.
      * @param  values  the values as an array, or {@code null} for writing the types instead.
      * @return the string representation.
      */
@@ -241,20 +241,30 @@ abstract class RecordDefinition {
         final StringBuilder buffer = new StringBuilder(250);
         final String lineSeparator = System.lineSeparator();
         final String[] names = new String[size()];
+        final String margin;
         int width = 0;
-        buffer.append(head).append("[“").append(getRecordType().getTypeName()).append("”]
{").append(lineSeparator);
         for (int i=0; i<names.length; i++) {
             width = Math.max(width, (names[i] = members[i].toString()).length());
         }
+        if (head == null) {
+            width  = 0;         // Ignore the width computation, but we still need the names
in the array.
+            margin = "";
+        } else {
+            buffer.append(head).append("[“").append(getRecordType().getTypeName()).append("”]
{").append(lineSeparator);
+            margin = "    ";
+        }
         for (int i=0; i<names.length; i++) {
             final String name = names[i];
-            buffer.append("    ").append(name);
+            buffer.append(margin).append(name);
             final Object value = (values != null) ? Array.get(values, i) : members[i].getAttributeType();
             if (value != null) {
                 buffer.append(CharSequences.spaces(width - name.length())).append(" : ").append(value);
             }
             buffer.append(lineSeparator);
         }
-        return buffer.append('}').append(lineSeparator).toString();
+        if (head != null) {
+            buffer.append('}').append(lineSeparator);
+        }
+        return buffer.toString();
     }
 }

Modified: sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/MarshallingTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/MarshallingTest.java?rev=1832180&r1=1832179&r2=1832180&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/MarshallingTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/MarshallingTest.java
[UTF-8] Thu May 24 15:10:32 2018
@@ -84,12 +84,6 @@ import static org.junit.Assert.*;
  */
 public final class MarshallingTest extends TestUsingFile implements WarningListener<Object>
{
     /**
-     * An XML file containing a metadata.
-     * This is mostly an anti-regression test.
-     */
-    private static final String FILENAME = "Metadata.xml";
-
-    /**
      * The marshaller used to handle marshalling the created DefaultMetadata object.
      */
     private final Marshaller marshaller;



Mime
View raw message