Search in sources :

Example 6 with IdentifierMap

use of org.apache.sis.xml.IdentifierMap in project sis by apache.

the class DefaultCitationTest method testIdentifierMap.

/**
 * Tests the identifier map, which handles ISBN and ISSN codes in a special way.
 */
@Test
public void testIdentifierMap() {
    final DefaultCitation citation = new DefaultCitation();
    final Collection<Identifier> identifiers = citation.getIdentifiers();
    final IdentifierMap identifierMap = citation.getIdentifierMap();
    assertTrue("Expected an initially empty set of identifiers.", identifiers.isEmpty());
    /*
         * Set the ISBN code, and ensure that the the ISBN is reflected in the identifier map.
         */
    citation.setISBN("MyISBN");
    assertEquals("MyISBN", citation.getISBN());
    assertEquals("ISBN code shall be included in the set of identifiers.", 1, identifiers.size());
    assertEquals("{ISBN=“MyISBN”}", identifierMap.toString());
    /*
         * Set the identifiers with a list containing ISBN and ISSN codes.
         * The ISBN code shall be ignored because and ISBN property was already set.
         * The ISSN code shall be retained because it is a new code.
         */
    assertNull("ISSN shall be initially null.", citation.getISSN());
    citation.setIdentifiers(Arrays.asList(new DefaultIdentifier(Citations.NETCDF, "MyNetCDF"), new DefaultIdentifier(Citations.EPSG, "MyEPSG"), new DefaultIdentifier(Citations.ISBN, "NewISBN"), new DefaultIdentifier(Citations.ISSN, "MyISSN")));
    assertEquals("The ISBN value shall have been overwritten.", "NewISBN", citation.getISBN());
    assertEquals("The ISSN value shall have been added, because new.", "MyISSN", citation.getISSN());
    assertEquals("{NetCDF=“MyNetCDF”, EPSG=“MyEPSG”, ISBN=“NewISBN”, ISSN=“MyISSN”}", identifierMap.toString());
}
Also used : DefaultIdentifier(org.apache.sis.metadata.iso.DefaultIdentifier) Identifier(org.opengis.metadata.Identifier) IdentifierMap(org.apache.sis.xml.IdentifierMap) DefaultIdentifier(org.apache.sis.metadata.iso.DefaultIdentifier) Test(org.junit.Test)

Example 7 with IdentifierMap

use of org.apache.sis.xml.IdentifierMap in project sis by apache.

the class ObjectReference method resolve.

/**
 * If the given metadata object is null, tries to get an instance from the identifiers
 * declared in this {@code ObjectReference}. If the given metadata object is non-null,
 * assigns to that object the identifiers declared in this {@code ObjectReference}.
 *
 * <p>This method is invoked at unmarshalling time by {@link PropertyType#resolve(Context)}.</p>
 *
 * @param  <T>       the compile-time type of the {@code type} argument.
 * @param  context   the marshalling context, or {@code null} if none.
 * @param  type      the expected type of the metadata object.
 * @param  metadata  the metadata object, or {@code null}.
 * @return a metadata object for the identifiers, or {@code null}
 */
final <T> T resolve(final Context context, final Class<T> type, T metadata) {
    if (metadata == null) {
        final ReferenceResolver resolver = Context.resolver(context);
        if ((uuid == null || (metadata = resolver.resolve(context, type, uuid)) == null) && (xlink == null || (metadata = resolver.resolve(context, type, xlink)) == null)) {
            /*
                 * Failed to find an existing metadata instance.
                 * Creates an empty instance with the identifiers.
                 */
            int count = 0;
            SpecializedIdentifier<?>[] identifiers = new SpecializedIdentifier<?>[2];
            if (uuid != null)
                identifiers[count++] = new SpecializedIdentifier<>(IdentifierSpace.UUID, uuid);
            if (xlink != null)
                identifiers[count++] = new SpecializedIdentifier<>(IdentifierSpace.XLINK, xlink);
            identifiers = ArraysExt.resize(identifiers, count);
            metadata = resolver.newIdentifiedObject(context, type, identifiers);
        }
    } else {
        /*
             * In principle, the XML should contain a full metadata object OR a uuidref attribute.
             * However if both are present, assign the identifiers to that instance.
             */
        if (metadata instanceof IdentifiedObject) {
            final IdentifierMap map = ((IdentifiedObject) metadata).getIdentifierMap();
            putInto(context, map, IdentifierSpace.UUID, uuid);
            putInto(context, map, IdentifierSpace.XLINK, xlink);
        }
    }
    return metadata;
}
Also used : IdentifierMap(org.apache.sis.xml.IdentifierMap) SpecializedIdentifier(org.apache.sis.internal.jaxb.SpecializedIdentifier) IdentifiedObject(org.apache.sis.xml.IdentifiedObject) ReferenceResolver(org.apache.sis.xml.ReferenceResolver)

Aggregations

IdentifierMap (org.apache.sis.xml.IdentifierMap)7 Test (org.junit.Test)6 Identifier (org.opengis.metadata.Identifier)6 ArrayList (java.util.ArrayList)5 UUID (java.util.UUID)2 URI (java.net.URI)1 UUID.fromString (java.util.UUID.fromString)1 SpecializedIdentifier (org.apache.sis.internal.jaxb.SpecializedIdentifier)1 DefaultIdentifier (org.apache.sis.metadata.iso.DefaultIdentifier)1 IdentifiedObject (org.apache.sis.xml.IdentifiedObject)1 ReferenceResolver (org.apache.sis.xml.ReferenceResolver)1 XLink (org.apache.sis.xml.XLink)1 Citation (org.opengis.metadata.citation.Citation)1