Search in sources :

Example 11 with Identifier

use of org.opengis.metadata.Identifier in project sis by apache.

the class CitationsTest method testIdentifierMatches.

/**
 * Tests {@link Citations#identifierMatches(Citation, Identifier, CharSequence)}.
 */
@Test
public void testIdentifierMatches() {
    final Identifier ogc = identifier("OGC", "06-042");
    final Identifier iso = identifier("ISO", "19128");
    final Citation citation = citation("Web Map Server", ogc, iso, identifier("Foo", "06-042"));
    assertTrue("With full identifier", Citations.identifierMatches(citation, ogc, ogc.getCode()));
    assertTrue("With full identifier", Citations.identifierMatches(citation, iso, iso.getCode()));
    assertFalse("With wrong code", Citations.identifierMatches(citation, identifier("ISO", "19115"), "19115"));
    assertFalse("With wrong code space", Citations.identifierMatches(citation, identifier("Foo", "19128"), "19128"));
    assertFalse("With wrong code", Citations.identifierMatches(citation, null, "Foo"));
    assertTrue("Without identifier", Citations.identifierMatches(citation, null, "19128"));
    assertTrue("With parsing", Citations.identifierMatches(citation, null, "ISO:19128"));
    assertFalse("With wrong code space", Citations.identifierMatches(citation, null, "Foo:19128"));
}
Also used : Identifier(org.opengis.metadata.Identifier) Citation(org.opengis.metadata.citation.Citation) Test(org.junit.Test)

Example 12 with Identifier

use of org.opengis.metadata.Identifier in project sis by apache.

the class ModifiableIdentifierMap method put.

/**
 * Sets the code of the identifier having the given authority to the given value.
 * If no identifier is found for the given authority, a new one is created.
 * If more than one identifier is found for the given authority, then all previous identifiers may be removed
 * in order to ensure that the new entry will be the first entry, so it can be find by the {@code get} method.
 *
 * <p>If the given {@code authority} is {@code HREF} and if the given string is parsable as a {@link URI},
 * then this method will actually store the value as the {@link XLink#getHRef()} property of the {@code XLink}
 * associated to the {@code XLINK} key. Only if the given string can not be parsed, then the value is stored
 * <cite>as-is</cite> under the {@code HREF} key.</p>
 *
 * @param  authority  the authority for which to set the code.
 * @param  code  the new code for the given authority, or {@code null} for removing the entry.
 * @return the previous code for the given authority, or {@code null} if none.
 */
@Override
public String put(final Citation authority, final String code) {
    ArgumentChecks.ensureNonNull("authority", authority);
    String previous = null;
    Object discarded = null;
    switch(specialCase(authority)) {
        case NonMarshalledAuthority.HREF:
            {
                URI uri = null;
                if (code != null) {
                    final Context context = Context.current();
                    final ValueConverter converter = Context.converter(context);
                    try {
                        uri = converter.toURI(context, code);
                    } catch (URISyntaxException e) {
                        SpecializedIdentifier.parseFailure(context, code, URI.class, e);
                        discarded = setHRef(null);
                        // Fallback on generic code below.
                        break;
                    }
                }
                final Identifier identifier = getIdentifier(authority);
                uri = setHRef(uri);
                if (uri != null) {
                    previous = uri.toString();
                } else if (identifier != null) {
                    previous = identifier.getCode();
                }
                return previous;
            }
    }
    /*
         * Generic code to be executed when the given authority is not one of the special case,
         * or when it was a special case but parsing of the given string failed.
         */
    final Iterator<? extends Identifier> it = identifiers.iterator();
    while (it.hasNext()) {
        final Identifier identifier = it.next();
        if (identifier == null) {
            // Opportunist cleaning, but should not happen.
            it.remove();
        } else if (Objects.equals(authority, identifier.getAuthority())) {
            if (code != null && identifier instanceof IdentifierMapEntry) {
                return ((IdentifierMapEntry) identifier).setValue(code);
            /*
                     * No need to suppress other occurrences of the key (if any)
                     * because we made a replacement in the first entry, so the
                     * new value will be visible by the getter methods.
                     */
            }
            if (previous == null) {
                previous = identifier.getCode();
            }
            it.remove();
        /*
                 * Continue the iteration in order to remove all other occurrences,
                 * in order to ensure that the getter methods will see the new value.
                 */
        }
    }
    if (code != null) {
        identifiers.add(SpecializedIdentifier.parse(authority, code));
    }
    if (previous == null && discarded != null) {
        previous = discarded.toString();
    }
    return previous;
}
Also used : Identifier(org.opengis.metadata.Identifier) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ValueConverter(org.apache.sis.xml.ValueConverter)

Example 13 with Identifier

use of org.opengis.metadata.Identifier in project sis by apache.

the class NonMarshalledAuthority method setMarshallables.

/**
 * Returns a collection containing all marshallable values of {@code newValues}, together with unmarshallable
 * values of {@code identifiers}. This method is invoked for preserving the identifiers that are conceptually
 * stored in distinct fields (XML identifier, UUID, ISBN, ISSN) when setting the collection of all identifiers
 * in a metadata object.
 *
 * <p>This method is used for implementation of {@code setIdentifiers(Collection)} methods
 * in public metadata objects.</p>
 *
 * @param  identifiers  the metadata internal identifiers collection, or {@code null} if none.
 * @param  newValues    the identifiers to add, or {@code null}.
 * @return the collection to set (may be {@code newValues}.
 *
 * @see #setMarshallable(Collection, Identifier)
 */
@SuppressWarnings("null")
public static Collection<? extends Identifier> setMarshallables(final Collection<Identifier> identifiers, final Collection<? extends Identifier> newValues) {
    int remaining;
    if (identifiers == null || (remaining = identifiers.size()) == 0) {
        return newValues;
    }
    /*
         * If there is any identifiers that need to be preserved (XML identifier, UUID, ISBN, etc.),
         * remember them. Otherwise there is nothing special to do and we can return the new values directly.
         */
    List<Identifier> toPreserve = null;
    for (final Identifier id : identifiers) {
        if (id != null && id.getAuthority() instanceof NonMarshalledAuthority<?>) {
            if (toPreserve == null) {
                toPreserve = new ArrayList<>(remaining);
            }
            toPreserve.add(id);
        }
        remaining--;
    }
    if (toPreserve == null) {
        return newValues;
    }
    /*
         * We find at least one identifier that may need to be preserved.
         * We need to create a combination of the two collections.
         */
    final Map<Citation, Identifier> authorities = new IdentityHashMap<>(4);
    final List<Identifier> merged = new ArrayList<>(newValues.size());
    for (final Identifier id : newValues) {
        merged.add(id);
        if (id != null) {
            final Citation authority = id.getAuthority();
            if (authority instanceof NonMarshalledAuthority<?>) {
                authorities.put(authority, id);
            }
        }
    }
    for (final Identifier id : toPreserve) {
        if (!authorities.containsKey(id.getAuthority())) {
            merged.add(id);
        }
    }
    /*
         * Wraps in an unmodifiable list in case the caller is creating an unmodifiable metadata.
         */
    switch(merged.size()) {
        case 0:
            return Collections.emptyList();
        case 1:
            return Collections.singletonList(merged.get(0));
        default:
            return Containers.unmodifiableList(CollectionsExt.toArray(merged, Identifier.class));
    }
}
Also used : Identifier(org.opengis.metadata.Identifier) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) UnmodifiableArrayList(org.apache.sis.internal.util.UnmodifiableArrayList) Citation(org.opengis.metadata.citation.Citation)

Example 14 with Identifier

use of org.opengis.metadata.Identifier in project sis by apache.

the class DefaultCitationTest method testFreeze.

/**
 * Tests {@link DefaultCitation#freeze()}, which is needed for the constants defined in {@link Citations}.
 */
@Test
public void testFreeze() {
    final DefaultCitation original = create();
    // This will invoke 'freeze()'.
    final DefaultCitation clone = (DefaultCitation) original.unmodifiable();
    assertNotSame(original, clone);
    assertTrue("original.isModifiable", original.isModifiable());
    assertFalse("clone.isModifiable", clone.isModifiable());
    assertSame("original.unmodifiable", clone, original.unmodifiable());
    assertSame("clone.unmodifiable", clone, clone.unmodifiable());
    assertSame("ISBN", original.getISBN(), clone.getISBN());
    assertSame("title", original.getTitle(), clone.getTitle());
    assertSame("alternateTitle", getSingleton(original.getAlternateTitles()), getSingleton(clone.getAlternateTitles()));
    assertCopy(original.getIdentifiers(), clone.getIdentifiers());
    assertCopy(original.getCitedResponsibleParties(), clone.getCitedResponsibleParties());
    assertCopy(original.getPresentationForms(), clone.getPresentationForms());
    /*
         * Verify the unique identifier, which is the ISBN code. ISBN and ISSN codes are handled
         * in a special way by DefaultCitation (they are instances of SpecializedIdentifier), but
         * the should nevertheless be cloned.
         */
    final Identifier ide = getSingleton(original.getIdentifiers());
    final Identifier ida = getSingleton(clone.getIdentifiers());
    assertNotSame("identifier", ide, ida);
    assertSame("code", ide.getCode(), ida.getCode());
    assertSame("authority", ide.getAuthority(), ida.getAuthority());
    /*
         * Verify the author metadata.
         */
    final ResponsibleParty re = CollectionsExt.first(original.getCitedResponsibleParties());
    final ResponsibleParty ra = CollectionsExt.first(clone.getCitedResponsibleParties());
    assertNotSame("citedResponsibleParty", re, ra);
    assertSame("role", re.getRole(), ra.getRole());
    assertSame("name", re.getIndividualName(), ra.getIndividualName());
}
Also used : DefaultIdentifier(org.apache.sis.metadata.iso.DefaultIdentifier) Identifier(org.opengis.metadata.Identifier) ResponsibleParty(org.opengis.metadata.citation.ResponsibleParty) Test(org.junit.Test)

Example 15 with Identifier

use of org.opengis.metadata.Identifier in project sis by apache.

the class ModifiableIdentifierMapTest method testGetSpecialized.

/**
 * Tests read operations on an {@link IdentifierMap} using specific API.
 */
@Test
public void testGetSpecialized() {
    final List<Identifier> identifiers = new ArrayList<>();
    final IdentifierMap map = new ModifiableIdentifierMap(identifiers);
    assertNull(map.put(ID, "myID"));
    assertNull(map.put(UUID, "a1eb6e53-93db-4942-84a6-d9e7fb9db2c7"));
    assertNull(map.put(HREF, "http://mylink"));
    assertMapEquals("{gml:id=“myID”," + " gco:uuid=“a1eb6e53-93db-4942-84a6-d9e7fb9db2c7”," + " xlink:href=“http://mylink”}", map);
    assertEquals("myID", map.get(ID));
    assertEquals("a1eb6e53-93db-4942-84a6-d9e7fb9db2c7", map.get(UUID));
    assertEquals("http://mylink", map.get(HREF));
    assertEquals("myID", map.getSpecialized(ID));
    assertEquals(URI.create("http://mylink"), map.getSpecialized(HREF));
    assertEquals(fromString("a1eb6e53-93db-4942-84a6-d9e7fb9db2c7"), map.getSpecialized(UUID));
}
Also used : Identifier(org.opengis.metadata.Identifier) IdentifierMap(org.apache.sis.xml.IdentifierMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Identifier (org.opengis.metadata.Identifier)60 ReferenceIdentifier (org.opengis.referencing.ReferenceIdentifier)21 Test (org.junit.Test)14 ImmutableIdentifier (org.apache.sis.metadata.iso.ImmutableIdentifier)11 Citation (org.opengis.metadata.citation.Citation)10 IdentifiedObject (org.opengis.referencing.IdentifiedObject)10 InternationalString (org.opengis.util.InternationalString)10 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)6 DefaultIdentifier (org.apache.sis.metadata.iso.DefaultIdentifier)6 IdentifierMap (org.apache.sis.xml.IdentifierMap)6 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)5 OperationMethod (org.opengis.referencing.operation.OperationMethod)5 NameToIdentifier (org.apache.sis.internal.metadata.NameToIdentifier)4 NamedIdentifier (org.apache.sis.referencing.NamedIdentifier)4 GenericName (org.opengis.util.GenericName)4 URI (java.net.URI)3 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)3 IdentityHashMap (java.util.IdentityHashMap)2 LinkedHashMap (java.util.LinkedHashMap)2