Search in sources :

Example 6 with Identifier

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

the class IdentifiedObjects method getUnicodeIdentifier.

/**
 * Returns the first name, alias or identifier which is a
 * {@linkplain CharSequences#isUnicodeIdentifier(CharSequence) valid Unicode identifier}.
 * This method performs the search in the following order:
 *
 * <ul>
 *   <li><code>object.{@linkplain AbstractIdentifiedObject#getName() getName()}</code></li>
 *   <li><code>object.{@linkplain AbstractIdentifiedObject#getAlias() getAlias()}</code> in iteration order</li>
 *   <li><code>object.{@linkplain AbstractIdentifiedObject#getIdentifiers() getIdentifiers()}</code> in iteration order</li>
 * </ul>
 *
 * @param  object  the identified object, or {@code null}.
 * @return the first name, alias or identifier which is a valid Unicode identifier, or {@code null} if none.
 *
 * @see org.apache.sis.metadata.iso.ImmutableIdentifier
 * @see org.apache.sis.metadata.iso.citation.Citations#getUnicodeIdentifier(Citation)
 * @see org.apache.sis.util.CharSequences#isUnicodeIdentifier(CharSequence)
 */
public static String getUnicodeIdentifier(final IdentifiedObject object) {
    if (object != null) {
        Identifier identifier = object.getName();
        if (identifier != null) {
            // Paranoiac check.
            final String code = identifier.getCode();
            if (CharSequences.isUnicodeIdentifier(code)) {
                return code;
            }
        }
        final Iterator<GenericName> it = iterator(object.getAlias());
        if (it != null)
            while (it.hasNext()) {
                GenericName alias = it.next();
                if (alias != null && (alias = alias.tip()) != null) {
                    final String code = alias.toString();
                    if (CharSequences.isUnicodeIdentifier(code)) {
                        return code;
                    }
                }
            }
        final Iterator<? extends Identifier> id = iterator(object.getIdentifiers());
        if (id != null)
            while (id.hasNext()) {
                identifier = id.next();
                if (identifier != null) {
                    // Paranoiac check.
                    final String code = identifier.getCode();
                    if (CharSequences.isUnicodeIdentifier(code)) {
                        return code;
                    }
                }
            }
    }
    return null;
}
Also used : GenericName(org.opengis.util.GenericName) NameToIdentifier(org.apache.sis.internal.metadata.NameToIdentifier) Identifier(org.opengis.metadata.Identifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier)

Example 7 with Identifier

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

the class ParameterFormat method formatSummary.

/**
 * Implementation of public {@code format(…)} methods for {@code NAME_SUMMARY} content level.
 *
 * @param  objects  the collection of objects to format.
 * @param  out      the stream or buffer where to write the summary.
 * @throws IOException if an error occurred will writing to the given appendable.
 */
private void formatSummary(final IdentifiedObject[] objects, final Appendable out) throws IOException {
    final Vocabulary resources = Vocabulary.getResources(displayLocale);
    /*
         * Prepares all rows before we write them to the output stream, because not all
         * identified objects may have names with the same scopes in the same order. We
         * also need to iterate over all rows in order to know the number of columns.
         *
         * The first column is reserved for the identifier. We put null as a sentinal key for
         * that column name, to be replaced later by "Identifier" in user locale. We can not
         * put the localized strings in the map right now because they could conflict with
         * the scope of some alias to be processed below.
         */
    boolean hasIdentifiers = false;
    final List<String[]> rows = new ArrayList<>();
    final Map<String, Integer> columnIndices = new LinkedHashMap<>();
    // See above comment for the meaning of "null" here.
    columnIndices.put(null, 0);
    if (preferredCodespaces != null) {
        for (final String codespace : preferredCodespaces) {
            columnIndices.put(codespace, columnIndices.size());
        }
    }
    for (final IdentifiedObject object : objects) {
        // Will growth later if needed.
        String[] row = new String[columnIndices.size()];
        /*
             * Put the first identifier in the first column. If no identifier has a codespace in the list
             * supplied by the user, then we will use the first identifier (any codespace) as a fallback.
             */
        final Set<ReferenceIdentifier> identifiers = object.getIdentifiers();
        if (identifiers != null) {
            // Paranoiac check.
            Identifier identifier = null;
            for (final ReferenceIdentifier candidate : identifiers) {
                if (candidate != null) {
                    // Paranoiac check.
                    if (isPreferredCodespace(candidate.getCodeSpace())) {
                        identifier = candidate;
                        // Format now.
                        break;
                    }
                    if (identifier == null) {
                        // To be used as a fallback if we find nothing better.
                        identifier = candidate;
                    }
                }
            }
            if (identifier != null) {
                row[0] = IdentifiedObjects.toString(identifier);
                hasIdentifiers = true;
            }
        }
        /*
             * If the name's codespace is in the list of codespaces asked by the user, add that name
             * in the current row and clear the 'name' locale variable. Otherwise, keep the 'name'
             * locale variable in case we found no alias to format.
             */
        ReferenceIdentifier name = object.getName();
        if (name != null) {
            // Paranoiac check.
            final String codespace = name.getCodeSpace();
            if (isPreferredCodespace(codespace)) {
                row = putIfAbsent(resources, row, columnIndices, codespace, name.getCode());
                name = null;
            }
        }
        /*
             * Put all aliases having a codespace in the list asked by the user.
             */
        final Collection<GenericName> aliases = object.getAlias();
        if (aliases != null) {
            // Paranoiac check.
            for (final GenericName alias : aliases) {
                if (alias != null) {
                    // Paranoiac check.
                    final String codespace = NameToIdentifier.getCodeSpace(alias, displayLocale);
                    if (isPreferredCodespace(codespace)) {
                        row = putIfAbsent(resources, row, columnIndices, codespace, alias.tip().toInternationalString().toString(displayLocale));
                        name = null;
                    }
                }
            }
        }
        /*
             * If no name and no alias have a codespace in the list of codespaces asked by the user,
             * force the addition of primary name regardless its codespace.
             */
        if (name != null) {
            row = putIfAbsent(resources, row, columnIndices, name.getCodeSpace(), name.getCode());
        }
        rows.add(row);
    }
    /*
         * Writes the table. The header will contain one column for each codespace in the order declared
         * by the user. If the user did not specified any codespace, or if we had to write codespace not
         * on the user list, then those codespaces will be written in the order we found them.
         */
    final boolean hasColors = (colors != null);
    final TableAppender table = new TableAppender(out, columnSeparator);
    table.setMultiLinesCells(true);
    table.appendHorizontalSeparator();
    for (String codespace : columnIndices.keySet()) {
        if (codespace == null) {
            // Skip empty column.
            if (!hasIdentifiers)
                continue;
            codespace = resources.getString(Vocabulary.Keys.Identifier);
        }
        if (hasColors) {
            codespace = X364.BOLD.sequence() + codespace + X364.NORMAL.sequence();
        }
        table.append(codespace);
        nextColumn(table);
    }
    table.appendHorizontalSeparator();
    /*
         * Writes row content.
         */
    final int numColumns = columnIndices.size();
    for (final String[] row : rows) {
        for (int i = hasIdentifiers ? 0 : 1; i < numColumns; i++) {
            if (i < row.length) {
                final String name = row[i];
                if (name != null) {
                    table.append(name);
                }
            }
            nextColumn(table);
        }
        table.nextLine();
    }
    table.appendHorizontalSeparator();
    table.flush();
}
Also used : Vocabulary(org.apache.sis.util.resources.Vocabulary) ArrayList(java.util.ArrayList) TableAppender(org.apache.sis.io.TableAppender) LinkedHashMap(java.util.LinkedHashMap) GenericName(org.opengis.util.GenericName) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) NameToIdentifier(org.apache.sis.internal.metadata.NameToIdentifier) Identifier(org.opengis.metadata.Identifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) IdentifiedObject(org.opengis.referencing.IdentifiedObject)

Example 8 with Identifier

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

the class ParameterFormat method format.

/**
 * Formats the given object to the given stream of buffer.
 * The object may be an instance of any of the following types:
 *
 * <ul>
 *   <li>{@link ParameterValueGroup}</li>
 *   <li>{@link ParameterDescriptorGroup}</li>
 *   <li>{@link OperationMethod}</li>
 *   <li><code>{@linkplain IdentifiedObject}[]</code> — accepted only for {@link ContentLevel#NAME_SUMMARY}.</li>
 * </ul>
 *
 * @throws IOException if an error occurred while writing to the given appendable.
 */
@Override
public void format(final Object object, final Appendable toAppendTo) throws IOException {
    ArgumentChecks.ensureNonNull("object", object);
    ArgumentChecks.ensureNonNull("toAppendTo", toAppendTo);
    final boolean isSummary = contentLevel == ContentLevel.NAME_SUMMARY;
    final ParameterDescriptorGroup descriptor;
    final ParameterValueGroup values;
    final Identifier name;
    if (object instanceof ParameterValueGroup) {
        values = (ParameterValueGroup) object;
        descriptor = values.getDescriptor();
        name = descriptor.getName();
    } else if (object instanceof ParameterDescriptorGroup) {
        descriptor = (ParameterDescriptorGroup) object;
        values = null;
        name = descriptor.getName();
    } else if (object instanceof OperationMethod) {
        final OperationMethod operation = (OperationMethod) object;
        descriptor = operation.getParameters();
        values = null;
        name = operation.getName();
    } else if (isSummary && object instanceof IdentifiedObject[]) {
        formatSummary((IdentifiedObject[]) object, toAppendTo);
        return;
    } else {
        throw new IllegalArgumentException(Errors.getResources(displayLocale).getString(Errors.Keys.UnsupportedType_1, object.getClass()));
    }
    if (isSummary) {
        final List<GeneralParameterDescriptor> parameters = descriptor.descriptors();
        formatSummary(parameters.toArray(new IdentifiedObject[parameters.size()]), toAppendTo);
    } else {
        format(name.getCode(), descriptor, values, toAppendTo);
    }
}
Also used : NameToIdentifier(org.apache.sis.internal.metadata.NameToIdentifier) Identifier(org.opengis.metadata.Identifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) IdentifiedObject(org.opengis.referencing.IdentifiedObject) OperationMethod(org.opengis.referencing.operation.OperationMethod)

Example 9 with Identifier

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

the class ParameterValueList method add.

/**
 * Adds a {@link ParameterValue} or an other {@link ParameterValueGroup} to this list.
 * If an existing parameter is already included for the same name and adding the new
 * parameter would increase the number past what is allowable by {@code maximumOccurs},
 * then an {@link InvalidParameterCardinalityException} will be thrown.
 *
 * @param  parameter  new parameter to be added to this group.
 * @return always {@code true} since this object changes as a result of this call.
 * @throws IllegalArgumentException if the specified parameter is not allowable by the groups descriptor.
 * @throws InvalidParameterCardinalityException if adding this parameter would result in more parameters
 *         than allowed by {@code maximumOccurs}.
 */
@Override
public boolean add(final GeneralParameterValue parameter) {
    ArgumentChecks.ensureNonNull("parameter", parameter);
    final GeneralParameterDescriptor desc = parameter.getDescriptor();
    ensureDescriptorExists(desc);
    /*
         * If we had an uninitialized parameter (a parameter created by the DefaultParameterValueGroup constructor
         * and never been queried or set by the user), then the given parameter will replace the uninitialized.
         * The intent is to allow users to set its own parameters by a call to group.values().addAll(myParam).
         * Otherwise the given parameter will be added, in which case we need to check the cardinality.
         */
    final Identifier name = desc.getName();
    int count = 0;
    for (int i = 0; i < size; i++) {
        final GeneralParameterValue value = values[i];
        if (name.equals(value.getDescriptor().getName())) {
            if (value instanceof UninitializedParameter) {
                values[i] = parameter;
                return true;
            }
            count++;
        }
    }
    final int max = desc.getMaximumOccurs();
    if (count >= max) {
        throw new InvalidParameterCardinalityException(Errors.format(Errors.Keys.TooManyOccurrences_2, max, name), name.getCode());
    }
    addUnchecked(parameter);
    modCount++;
    return true;
}
Also used : GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) Identifier(org.opengis.metadata.Identifier) InvalidParameterCardinalityException(org.opengis.parameter.InvalidParameterCardinalityException) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor)

Example 10 with Identifier

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

the class ParameterValueList method ensureCanAdd.

/**
 * Verifies if adding a parameter with the given descriptor is allowed by the cardinality constraints. If adding
 * the parameter would result in more occurrences than {@link DefaultParameterDescriptor#getMaximumOccurs()},
 * then this method throws an {@link InvalidParameterCardinalityException}.
 */
private void ensureCanAdd(final GeneralParameterDescriptor desc) {
    final Identifier name = desc.getName();
    int count = 0;
    for (int i = 0; i < size; i++) {
        if (name.equals(values[i].getDescriptor().getName())) {
            count++;
        }
    }
    final int max = desc.getMaximumOccurs();
    if (count >= max) {
        throw new InvalidParameterCardinalityException(Errors.format(Errors.Keys.TooManyOccurrences_2, max, name), name.getCode());
    }
}
Also used : Identifier(org.opengis.metadata.Identifier) InvalidParameterCardinalityException(org.opengis.parameter.InvalidParameterCardinalityException)

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