sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1398646 - in /sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis: internal/jaxb/ internal/jaxb/NonMarshalledAuthority.java internal/jaxb/package-info.java xml/IdentifierSpace.java
Date Tue, 16 Oct 2012 05:14:48 GMT
Author: desruisseaux
Date: Tue Oct 16 05:14:47 2012
New Revision: 1398646

URL: http://svn.apache.org/viewvc?rev=1398646&view=rev
Log:
Added the IdentifierSpace interface.

Added:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java
  (with props)
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/package-info.java
  (with props)
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/IdentifierSpace.java  
(with props)

Added: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java?rev=1398646&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java
(added)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java
Tue Oct 16 05:14:47 2012
@@ -0,0 +1,253 @@
+/*
+ * 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.jaxb;
+
+import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.io.Serializable;
+import java.io.ObjectStreamException;
+import java.lang.reflect.Field;
+
+import org.opengis.metadata.Identifier;
+
+import org.apache.sis.internal.simple.SimpleCitation;
+import org.apache.sis.util.logging.Logging;
+import org.apache.sis.xml.IdentifierSpace;
+
+import static org.apache.sis.util.collection.Collections.addIfNonNull;
+
+
+/**
+ * The {@linkplain Identifier#getAuthority() authority of identifiers} that are not expected
to be
+ * marshalled in a {@code MD_Identifier} XML element. Those identifiers are also excluded
from the
+ * tree formatted by {@link org.apache.sis.metadata.AbstractMetadata#asTree()}.
+ * <p>
+ * There is two kinds of non-marshalled identifiers:
+ *
+ * <ul>
+ *   <li><p>The XML attributes declared by ISO 19139 specification in the {@code
gco:PropertyType}
+ *       element: {@code gml:id}, {@code gco:uuid} and {@code xlink:href}. Those attributes
are not
+ *       part of the ISO 19115 specification. Those authorities are declared in the
+ *       {@link IdentifierSpace} interfaces.</p></li>
+ *
+ *   <li><p>ISO 19115 attributes that we choose, for the SIS implementation,
to merge with
+ *       other identifiers: ISBN and ISSN codes. Those attributes are declared in the
+ *       {@link org.apache.sis.metadata.iso.citation.Citations} class.</p></li>
+ * </ul>
+ *
+ * In the current SIS library, there is different places where identifiers are filtered on
the
+ * basis of this class, as below:
+ *
+ * {@preformat java
+ *     if (identifier.getAuthority() instanceof NonMarshalledAuthority<?>) {
+ *         // Omit that identifier.
+ *     }
+ * }
+ *
+ * @param <T> The type of object used as identifier values.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-3.19)
+ * @version 0.3
+ * @module
+ *
+ * @see IdentifierSpace
+ */
+public final class NonMarshalledAuthority<T> extends SimpleCitation implements IdentifierSpace<T>,
Serializable {
+    /**
+     * For cross-version compatibility.
+     */
+    private static final long serialVersionUID = -6309485399210742418L;
+
+    /**
+     * Ordinal values for switch statements. The constant defined here shall
+     * mirror the constants defined in the {@link IdentifierSpace} interface
+     * and {@link org.apache.sis.metadata.iso.citation.DefaultCitation} class.
+     */
+    public static final int ID=0, UUID=1, HREF=2, XLINK=3, ISSN=4, ISBN=5;
+    // If more codes are added, please update readResolve() below.
+
+    /**
+     * Ordinal values for switch statements, as one of the {@link #ID}, {@link #UUID},
+     * <i>etc.</i> constants.
+     * <p>
+     * This value is not serialized because its value may not be consistent between different
+     * versions of the SIS library (the attribute name is more reliable). This instance
+     * should be replaced by one of the exiting constants at deserialization time anyway.
+     */
+    final transient int ordinal;
+
+    /**
+     * Creates a new enum for the given attribute.
+     *
+     * @param attribute The XML attribute name, to be returned by {@link #getName()}.
+     * @param ordinal   Ordinal value for switch statement, as one of the {@link #ID},
+     *                  {@link #UUID}, <i>etc.</i> constants.
+     */
+    public NonMarshalledAuthority(final String attribute, final int ordinal) {
+        super(attribute);
+        this.ordinal = ordinal;
+    }
+
+    /**
+     * Returns the XML attribute name with its prefix. Attribute names can be {@code "gml:id"},
+     * {@code "gco:uuid"} or {@code "xlink:href"}.
+     */
+    @Override
+    public String getName() {
+        return title;
+    }
+
+    /**
+     * Returns a string representation of this identifier space.
+     */
+    @Override
+    public String toString() {
+        return "IdentifierSpace[" + title + ']';
+    }
+
+    /**
+     * Returns the first marshallable identifier from the given collection. This method omits
+     * "special" identifiers (ISO 19139 attributes, ISBN codes...), which are recognized
by
+     * the implementation class of their authority.
+     *
+     * @param  <T> The type of object used as identifier values.
+     * @param  identifiers The collection from which to get identifiers, or {@code null}.
+     * @return The first identifier, or {@code null} if none.
+     */
+    public static <T extends Identifier> T getMarshallable(final Collection<? extends
T> identifiers) {
+        if (identifiers != null) {
+            for (final T id : identifiers) {
+                if (id != null && !(id.getAuthority() instanceof NonMarshalledAuthority<?>))
{
+                    return id;
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Sets the given identifier in the given collection. This method removes all identifiers
+     * that are not ISO 19139 identifiers before to adds the given one in the collection.
This
+     * method is used when the given collection is expected to contains only one ISO 19115
+     * identifier.
+     *
+     * @param <T> The type of object used as identifier values.
+     * @param identifiers The collection in which to add the identifier.
+     * @param id The identifier to add, or {@code null}.
+     */
+    public static <T extends Identifier> void setMarshallable(final Collection<T>
identifiers, final T id) {
+        final Iterator<T> it = identifiers.iterator();
+        while (it.hasNext()) {
+            final T old = it.next();
+            if (old != null) {
+                if (old.getAuthority() instanceof NonMarshalledAuthority<?>) {
+                    continue; // Don't touch this identifier.
+                }
+            }
+            it.remove();
+        }
+        addIfNonNull(identifiers, id);
+    }
+
+    /**
+     * Returns a collection containing only the identifiers having a {@code NonMarshalledAuthority}.
+     *
+     * @param  <T> The type of object used as identifier values.
+     * @param  identifiers The identifiers to getIdentifiers, or {@code null} if none.
+     * @return The filtered identifiers, or {@code null} if none.
+     */
+    public static <T extends Identifier> Collection<T> getIdentifiers(final Collection<?
extends T> identifiers) {
+        Collection<T> filtered = null;
+        if (identifiers != null) {
+            int remaining = identifiers.size();
+            for (final T candidate : identifiers) {
+                if (candidate != null && candidate.getAuthority() instanceof NonMarshalledAuthority<?>)
{
+                    if (filtered == null) {
+                        filtered = new ArrayList<>(remaining);
+                    }
+                    filtered.add(candidate);
+                }
+                remaining--;
+            }
+        }
+        return filtered;
+    }
+
+    /**
+     * Removes from the given collection every identifiers having a {@code NonMarshalledAuthority},
+     * then adds the previously filtered identifiers (if any).
+     *
+     * @param <T> The type of object used as identifier values.
+     * @param identifiers The collection from which to remove identifiers, or {@code null}.
+     * @param filtered The previous filtered identifiers returned by {@link #getIdentifiers}.
+     */
+    public static <T extends Identifier> void setIdentifiers(final Collection<T>
identifiers, final Collection<T> filtered) {
+        if (identifiers != null) {
+            for (final Iterator<T> it=identifiers.iterator(); it.hasNext();) {
+                final T id = it.next();
+                if (id == null || id.getAuthority() instanceof NonMarshalledAuthority<?>)
{
+                    it.remove();
+                }
+            }
+            if (filtered != null) {
+                identifiers.addAll(filtered);
+            }
+        }
+    }
+
+    /**
+     * Returns one of the constants in the {@link DefaultCitation} class.
+     */
+    private static IdentifierSpace<?> getCitation(final String name) throws ObjectStreamException
{
+        try {
+            final Field field = Class.forName("org.apache.sis.metadata.iso.citation.DefaultCitation").getDeclaredField(name);
+            field.setAccessible(true);
+            return (IdentifierSpace<?>) field.get(null);
+        } catch (ReflectiveOperationException e) {
+            Logging.unexpectedException(NonMarshalledAuthority.class, "readResolve", e);
+        }
+        return null;
+    }
+
+    /**
+     * Invoked at deserialization time in order to setIdentifiers the deserialized instance
+     * by the appropriate instance defined in the {@link IdentifierSpace} interface.
+     */
+    private Object readResolve() throws ObjectStreamException {
+        int code = 0;
+        while (true) {
+            final IdentifierSpace<?> candidate;
+            switch (code) {
+                case ID:    candidate = IdentifierSpace.ID;    break;
+                case UUID:  candidate = IdentifierSpace.UUID;  break;
+                case HREF:  candidate = IdentifierSpace.HREF;  break;
+                case XLINK: candidate = IdentifierSpace.XLINK; break;
+                case ISBN:  candidate = getCitation("ISBN");   break;
+                case ISSN:  candidate = getCitation("ISSN");   break;
+                default: return this;
+            }
+            if (candidate instanceof NonMarshalledAuthority<?> &&
+                    ((NonMarshalledAuthority<?>) candidate).title.equals(title))
+            {
+                return candidate;
+            }
+            code++;
+        }
+    }
+}

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/package-info.java?rev=1398646&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/package-info.java
(added)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/package-info.java
Tue Oct 16 05:14:47 2012
@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+
+/**
+ * A set of helper classes for JAXB handling in the SIS implementation.
+ *
+ * <STRONG>Do not use!</STRONG>
+ *
+ * This package is for internal use by SIS only. Classes in this package
+ * may change in incompatible ways in any future version without notice.
+ *
+ * @author  Cédric Briançon (Geomatys)
+ * @since   0.3 (derived from geotk-3.00)
+ * @version 0.3
+ * @module
+ */
+package org.apache.sis.internal.jaxb;

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/package-info.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/IdentifierSpace.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/IdentifierSpace.java?rev=1398646&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/IdentifierSpace.java (added)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/IdentifierSpace.java Tue
Oct 16 05:14:47 2012
@@ -0,0 +1,117 @@
+/*
+ * 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.xml;
+
+import java.net.URI;
+import java.util.UUID;
+import org.opengis.metadata.citation.Citation;
+import org.apache.sis.internal.jaxb.NonMarshalledAuthority;
+
+
+/**
+ * Some identifier name spaces that are handled in a special way. The identifier name spaces
are
+ * usually defined as authorities in the {@link org.apache.sis.metadata.iso.citation.Citations}
+ * class. However a few identifiers defined in the {@code gco:ObjectIdentification} XML attribute
+ * group are handled in a special way. For example identifiers associated to the {@link #HREF}
+ * space are marshalled in the outer property element, as in the example below:
+ *
+ * {@preformat xml
+ *   <gmd:CI_Citation>
+ *     <gmd:series xlink:href="http://myReference">
+ *       <gmd:CI_Series>
+ *         <gmd:name>...</gmd:name>
+ *       </gmd:CI_Series>
+ *     </gmd:series>
+ *   </gmd:CI_Citation>
+ * }
+ *
+ * The values defined in this interface can be used as keys in the map returned by
+ * {@link IdentifiedObject#getIdentifierMap()}.
+ *
+ * @param <T> The type of object used as identifier values.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-3.18)
+ * @version 0.3
+ * @module
+ *
+ * @see org.apache.sis.metadata.iso.citation.Citations
+ * @see IdentifiedObject
+ * @see IdentifierMap
+ */
+public interface IdentifierSpace<T> extends Citation {
+    /**
+     * A standard GML attribute available on every object-with-identity.
+     * Its type is {@code "xs:ID"} - i.e. it is a fragment identifier, unique within document
scope only,
+     * for internal cross-references. It is not useful by itself as a persistent unique identifier.
+     *
+     * <p>The XML {@linkplain #getName() attribute name} is {@code "gml:id"}.</p>
+     *
+     * @see javax.xml.bind.annotation.XmlID
+     */
+    IdentifierSpace<String> ID = new NonMarshalledAuthority<>("gml:id", NonMarshalledAuthority.ID);
+
+    /**
+     * An optional attribute available on every object-with-identity provided in the GMD
schemas
+     * that implement ISO 19115 in XML. May be used as a persistent unique identifier, but
only
+     * available within GMD context.
+     *
+     * <p>The XML {@linkplain #getName() attribute name} is {@code "gco:uuid"}.</p>
+     *
+     * @see UUID
+     */
+    IdentifierSpace<UUID> UUID = new NonMarshalledAuthority<>("gco:uuid", NonMarshalledAuthority.UUID);
+
+    /**
+     * An optional attribute for URN to an external resources, or to an other part of a XML
+     * document, or an identifier. This is one of the many attributes available in the
+     * {@link #XLINK} identifier space, but is provided as a special constant because
+     * {@code href} is the most frequently used {@code xlink} attribute.
+     *
+     * <p>The XML {@linkplain #getName() attribute name} is {@code "xlink:href"}.</p>
+     *
+     * @see XLink#getHRef()
+     */
+    IdentifierSpace<URI> HREF = new NonMarshalledAuthority<>("xlink:href", NonMarshalledAuthority.HREF);
+
+    /**
+     * Any XML attributes defined by OGC in the
+     * <a href="http://schemas.opengis.net/xlink/1.0.0/xlinks.xsd">xlink</a>
schema.
+     * Note that the above {@link #HREF} identifier space is a special case of this
+     * {@code xlink} identifier space.
+     *
+     * @see XLink
+     */
+    IdentifierSpace<XLink> XLINK = new NonMarshalledAuthority<>("xlink", NonMarshalledAuthority.XLINK);
+
+    /**
+     * Returns the name of this identifier space.
+     *
+     * <ul>
+     *   <li><p>For the constants defined in this {@code IdentifierSpace} interface,
this is
+     *       the XML attribute name with its prefix. Attribute names can be {@code "gml:id"},
+     *       {@code "gco:uuid"} or {@code "xlink:href"}.</p></li>
+     *
+     *   <li><p>For the constants defined in the {@link org.apache.sis.metadata.iso.citation.Citations}
+     *       class, this is the identifier name space. They are usually not XML attribute
name because those
+     *       identifiers are marshalled as {@code <MD_Identifier>} XML elements rather
than attributes.</p></li>
+     * </ul>
+     *
+     * @return The name of this identifier space (may be XML attribute name).
+     */
+    String getName();
+}

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/IdentifierSpace.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/IdentifierSpace.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message