Search in sources :

Example 1 with ValueConverter

use of org.apache.sis.xml.ValueConverter 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 2 with ValueConverter

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

the class MD_CharacterSetCode method marshal.

/**
 * Substitutes the code list by the adapter to be marshalled into an XML file
 * or stream. JAXB calls automatically this method at marshalling time.
 *
 * @param  value  the code list value.
 * @return the adapter for the given code list.
 */
@Override
public final MD_CharacterSetCode marshal(final Charset value) {
    final Context context = Context.current();
    final ValueConverter converter = Context.converter(context);
    final String code = converter.toCharsetCode(context, value);
    if (code != null) {
        final Locale locale = context.getLocale();
        final MD_CharacterSetCode c = new MD_CharacterSetCode();
        c.identifier = new CodeListUID(context, "MD_CharacterSetCode", code, (locale != null) ? converter.toLanguageCode(context, locale) : null, (locale != null) ? value.displayName(locale) : value.displayName());
        return c;
    }
    return null;
}
Also used : Context(org.apache.sis.internal.jaxb.Context) Locale(java.util.Locale) CodeListUID(org.apache.sis.internal.jaxb.cat.CodeListUID) ValueConverter(org.apache.sis.xml.ValueConverter)

Aggregations

ValueConverter (org.apache.sis.xml.ValueConverter)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Locale (java.util.Locale)1 Context (org.apache.sis.internal.jaxb.Context)1 CodeListUID (org.apache.sis.internal.jaxb.cat.CodeListUID)1 Identifier (org.opengis.metadata.Identifier)1