Search in sources :

Example 31 with GenericName

use of org.opengis.util.GenericName in project sis by apache.

the class NameMarshallingTest method testScopedName.

/**
 * Tests XML of a {@link org.opengis.util.ScopedName}.
 *
 * @throws JAXBException if (un)marshalling failed.
 */
@Test
public void testScopedName() throws JAXBException {
    final NameFactory factory = DefaultFactories.forBuildin(NameFactory.class);
    final GenericName name = factory.createGenericName(null, "myScope", "myName");
    assertEquals("myScope:myName", name.toString());
    final String expected = "<gml:IO_IdentifiedObject xmlns:gml=\"" + Namespaces.GML + '"' + " xmlns:gco=\"" + LegacyNamespaces.GCO + "\">\n" + "  <gml:alias>\n" + "    <gco:ScopedName>myScope:myName</gco:ScopedName>\n" + "  </gml:alias>\n" + "</gml:IO_IdentifiedObject>\n";
    final String actual = marshal(name);
    assertXmlEquals(expected, actual, "xmlns:*");
    assertEquals(name, unmarshal(expected));
}
Also used : GenericName(org.opengis.util.GenericName) NameFactory(org.opengis.util.NameFactory) Test(org.junit.Test)

Example 32 with GenericName

use of org.opengis.util.GenericName in project sis by apache.

the class Builder method addNamesAndIdentifiers.

/**
 * Adds all non-deprecated names and identifiers from the given object.
 * Other properties like description and remarks are ignored.
 *
 * <p>This is a convenience method for using an existing object as a template, before to modify
 * some names by calls to {@link #rename(Citation, CharSequence[])}.</p>
 *
 * @param  object  the object from which to copy the references to names and identifiers.
 * @return {@code this}, for method call chaining.
 *
 * @since 0.6
 */
public B addNamesAndIdentifiers(final IdentifiedObject object) {
    ensureNonNull("object", object);
    for (final ReferenceIdentifier id : object.getIdentifiers()) {
        if (!isDeprecated(id)) {
            addIdentifier(id);
        }
    }
    ReferenceIdentifier id = object.getName();
    if (!isDeprecated(id)) {
        addName(id);
    }
    for (final GenericName alias : object.getAlias()) {
        if (!isDeprecated(alias)) {
            addName(alias);
        }
    }
    return self();
}
Also used : GenericName(org.opengis.util.GenericName) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier)

Example 33 with GenericName

use of org.opengis.util.GenericName in project sis by apache.

the class NameIterator method next.

/**
 * Returns the next name or alias in the iteration.
 *
 * Note: we do not bother checking for {@code NoSuchElementException} because this iterator
 * will be used only by JAXB, which is presumed checking for {@link #hasNext()} correctly.
 */
@Override
public ReferenceIdentifier next() {
    final ReferenceIdentifier n = next;
    while (alias.hasNext()) {
        final GenericName c = alias.next();
        if (c instanceof ReferenceIdentifier) {
            next = (ReferenceIdentifier) c;
            return n;
        }
    }
    next = null;
    return n;
}
Also used : GenericName(org.opengis.util.GenericName) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier)

Example 34 with GenericName

use of org.opengis.util.GenericName in project sis by apache.

the class NameIterator method getID.

/**
 * Implementation of {@link AbstractIdentifiedObject#getID()}, provided here for reducing the amount of code
 * to load in the common case where XML support is not needed.
 *
 * <p>The current implementation searches for the first identifier, regardless its authority.
 * If no identifier is found, then the name and aliases are used.
 * Then, this method returns the concatenation of the following elements separated by hyphens:</p>
 * <ul>
 *   <li>The code space in lower case, retaining only characters that are valid for Unicode identifiers.</li>
 *   <li>The object type as defined in OGC's URN (see {@link org.apache.sis.internal.util.DefinitionURI})</li>
 *   <li>The object code, retaining only characters that are valid for Unicode identifiers.</li>
 * </ul>
 *
 * Example: {@code "epsg-crs-4326"}.
 *
 * <p>The returned ID needs to be unique only in the XML document being marshalled.
 * Consecutive invocations of this method do not need to return the same value,
 * since it may depends on the marshalling context.</p>
 *
 * @param  context      the (un)marshalling context.
 * @param  object       the object for which to get a {@code gml:id}.
 * @param  name         the identified object name, or {@code null} if none.
 * @param  alias        the identified object aliases, or {@code null} if none.
 * @param  identifiers  the identifiers, or {@code null} if none.
 * @return proposed value for {@code gml:id} attribute, or {@code null} if none.
 */
static String getID(final Context context, final IdentifiedObject object, final ReferenceIdentifier name, final Collection<? extends GenericName> alias, final Collection<? extends ReferenceIdentifier> identifiers) {
    String candidate = Context.getObjectID(context, object);
    if (candidate == null) {
        final StringBuilder id = new StringBuilder();
        /*
             * We will iterate over the identifiers first. Only after the iteration is over,
             * if we found no suitable ID, then we will use the primary name as a last resort.
             */
        if (identifiers != null) {
            for (final ReferenceIdentifier identifier : identifiers) {
                if (// Really |, not ||
                appendUnicodeIdentifier(id, '-', identifier.getCodeSpace(), "", true) | appendUnicodeIdentifier(id, '-', NameMeaning.toObjectType(object.getClass()), "", false) | appendUnicodeIdentifier(id, '-', identifier.getCode(), "", true)) {
                    /*
                         * Check for ID uniqueness. If the ID is rejected, then we just need to clear
                         * the buffer and let the iteration continue the search for another ID.
                         */
                    candidate = id.toString();
                    if (Context.setObjectForID(context, object, candidate)) {
                        return candidate;
                    }
                }
                // Clear the buffer for another try.
                id.setLength(0);
            }
        }
        /*
             * In last ressort, use the name or an alias. The name will be used without codespace since
             * names are often verbose. If that name is also used, append a number until we find a free ID.
             */
        if (isUnnamed(name) || !appendUnicodeIdentifier(id, '-', name.getCode(), "", false)) {
            if (alias != null) {
                for (final GenericName a : alias) {
                    if (appendUnicodeIdentifier(id, '-', a.toString(), "", false)) {
                        break;
                    }
                }
            }
        }
        if (id.length() != 0) {
            candidate = id.toString();
            if (!Context.setObjectForID(context, object, candidate)) {
                final int s = id.append('-').length();
                int n = 0;
                do {
                    // Arbitrary limit.
                    if (++n == 100)
                        return null;
                    candidate = id.append(n).toString();
                    id.setLength(s);
                } while (!Context.setObjectForID(context, object, candidate));
            }
        }
    }
    return candidate;
}
Also used : GenericName(org.opengis.util.GenericName) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier)

Example 35 with GenericName

use of org.opengis.util.GenericName in project sis by apache.

the class IdentifiedObjectFormat method format.

/**
 * Formats the given object.
 */
@Override
public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) {
    final ReferenceIdentifier identifier = ((IdentifiedObject) obj).getName();
    if (identifier == null) {
        return toAppendTo.append(Vocabulary.getResources(locale).getString(Vocabulary.Keys.Unnamed));
    }
    if (identifier instanceof GenericName) {
        // The toString() behavior is specified by the GenericName javadoc.
        return toAppendTo.append(((GenericName) identifier).toInternationalString().toString(locale));
    }
    final String code = identifier.getCode();
    String cs = identifier.getCodeSpace();
    if (cs == null || cs.isEmpty()) {
        cs = Citations.getIdentifier(identifier.getAuthority(), true);
    }
    if (cs != null) {
        toAppendTo.append(cs).append(Citations.DEFAULT_SEPARATOR);
    }
    return toAppendTo.append(code);
}
Also used : GenericName(org.opengis.util.GenericName) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) IdentifiedObject(org.opengis.referencing.IdentifiedObject)

Aggregations

GenericName (org.opengis.util.GenericName)46 Test (org.junit.Test)11 ReferenceIdentifier (org.opengis.referencing.ReferenceIdentifier)9 InternationalString (org.opengis.util.InternationalString)9 ArrayList (java.util.ArrayList)5 IdentifiedObject (org.opengis.referencing.IdentifiedObject)5 NameFactory (org.opengis.util.NameFactory)5 Identifier (org.opengis.metadata.Identifier)4 ScopedName (org.opengis.util.ScopedName)4 HashMap (java.util.HashMap)3 DependsOnMethod (org.apache.sis.test.DependsOnMethod)3 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Collection (java.util.Collection)2 IdentityHashMap (java.util.IdentityHashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 AbstractIdentifiedType (org.apache.sis.feature.AbstractIdentifiedType)2 NameToIdentifier (org.apache.sis.internal.metadata.NameToIdentifier)2 TableAppender (org.apache.sis.io.TableAppender)2