Search in sources :

Example 81 with InternationalString

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

the class PT_FreeText method toCharSequence.

/**
 * Returns the content of this {@code <gco:CharacterString>} as an {@code InternationalString}.
 *
 * @return the character sequence for this {@code <gco:CharacterString>}.
 */
@Override
protected CharSequence toCharSequence() {
    // May be null.
    String defaultValue = toString();
    if (defaultValue != null && contains(defaultValue)) {
        /*
             * If the <gco:CharacterString> value is repeated in one of the
             * <lan:LocalisedCharacterString> elements, keep only the localized
             * version  (because it specifies the locale, while the unlocalized
             * string saids nothing on that matter).
             */
        defaultValue = null;
    }
    /*
         * Create the international string with all locales found in the <gml:textGroup>
         * element. If the <gml:textGroup> element is missing or empty, then we will use
         * an instance of SimpleInternationalString instead than the more heavy
         * DefaultInternationalString.
         */
    DefaultInternationalString i18n = null;
    final TextGroup[] textGroup = this.textGroup;
    if (textGroup != null) {
        for (final TextGroup group : textGroup) {
            if (group != null) {
                final LocalisedCharacterString[] localised = group.localized;
                if (localised != null) {
                    for (final LocalisedCharacterString text : localised) {
                        if (text != null) {
                            if (i18n == null) {
                                i18n = new DefaultInternationalString(defaultValue);
                            }
                            i18n.add(text.locale, text.text);
                        }
                    }
                }
            }
        }
    }
    if (i18n == null && defaultValue != null) {
        return new SimpleInternationalString(defaultValue);
    }
    return i18n;
}
Also used : SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) DefaultInternationalString(org.apache.sis.util.iso.DefaultInternationalString) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) DefaultInternationalString(org.apache.sis.util.iso.DefaultInternationalString) InternationalString(org.opengis.util.InternationalString) GO_CharacterString(org.apache.sis.internal.jaxb.gco.GO_CharacterString)

Example 82 with InternationalString

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

the class DefaultNameFactory method parseGenericName.

/**
 * Constructs a generic name from a qualified name.
 * This method splits the given name around a separator inferred from the given scope, or the
 * {@link DefaultNameSpace#DEFAULT_SEPARATOR ':'} separator if the given scope is null.
 *
 * @param  scope  the {@linkplain AbstractName#scope() scope} of the generic name to be created,
 *                or {@code null} for a global namespace.
 * @param  name   the qualified name, as a sequence of names separated by a scope-dependent separator.
 * @return a name parsed from the given string.
 *
 * @see Names#parseGenericName(CharSequence, String, CharSequence)
 */
@Override
public GenericName parseGenericName(final NameSpace scope, final CharSequence name) {
    final String separator;
    if (scope instanceof DefaultNameSpace) {
        separator = ((DefaultNameSpace) scope).separator;
    } else {
        separator = DEFAULT_SEPARATOR_STRING;
    }
    final int s = separator.length();
    final List<String> names = new ArrayList<>();
    int lower = 0;
    final String string = name.toString();
    while (true) {
        final int upper = string.indexOf(separator, lower);
        if (upper >= 0) {
            names.add(string.substring(lower, upper));
            lower = upper + s;
        } else {
            names.add(string.substring(lower));
            break;
        }
    }
    if (names.size() == 1) {
        /*
             * Preserves the InternationalString (current implementation of
             * the parsing code above has lost the internationalization).
             */
        return createLocalName(scope, name);
    }
    return createGenericName(scope, names.toArray(new String[names.size()]));
}
Also used : ArrayList(java.util.ArrayList) InternationalString(org.opengis.util.InternationalString)

Example 83 with InternationalString

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

the class Types method toInternationalString.

/**
 * Returns an international string for the values in the given properties map, or {@code null} if none.
 * This method is used when a property in a {@link java.util.Map} may have many localized variants.
 * For example the given map may contains a {@code "remarks"} property defined by values associated to
 * the {@code "remarks_en"} and {@code "remarks_fr"} keys, for English and French locales respectively.
 *
 * <p>If the given map is {@code null}, then this method returns {@code null}.
 * Otherwise this method iterates over the entries having a key that starts with the specified prefix,
 * followed by the {@code '_'} character. For each such key:</p>
 *
 * <ul>
 *   <li>If the key is exactly equals to {@code prefix}, selects {@link Locale#ROOT}.</li>
 *   <li>Otherwise the characters after {@code '_'} are parsed as an ISO language and country code
 *       by the {@link Locales#parse(String, int)} method. Note that 3-letters codes are replaced
 *       by their 2-letters counterparts on a <cite>best effort</cite> basis.</li>
 *   <li>The value for the decoded locale is added in the international string to be returned.</li>
 * </ul>
 *
 * @param  properties  the map from which to get the string values for an international string, or {@code null}.
 * @param  prefix      the prefix of keys to use for creating the international string.
 * @return the international string, or {@code null} if the given map is null or does not contain values
 *         associated to keys starting with the given prefix.
 * @throws IllegalArgumentException if a key starts by the given prefix and:
 *         <ul>
 *           <li>The key suffix is an illegal {@link Locale} code,</li>
 *           <li>or the value associated to that key is a not a {@link CharSequence}.</li>
 *         </ul>
 *
 * @see Locales#parse(String, int)
 * @see DefaultInternationalString#DefaultInternationalString(Map)
 *
 * @since 0.4
 */
public static InternationalString toInternationalString(Map<String, ?> properties, final String prefix) throws IllegalArgumentException {
    ArgumentChecks.ensureNonEmpty("prefix", prefix);
    if (properties == null) {
        return null;
    }
    /*
         * If the given map is an instance of SortedMap using the natural ordering of keys,
         * we can skip all keys that lexicographically precedes the given prefix.
         */
    boolean isSorted = false;
    if (properties instanceof SortedMap<?, ?>) {
        final SortedMap<String, ?> sorted = (SortedMap<String, ?>) properties;
        if (sorted.comparator() == null) {
            // We want natural ordering.
            properties = sorted.tailMap(prefix);
            isSorted = true;
        }
    }
    /*
         * Now iterates over the map entry and lazily create the InternationalString
         * only when first needed. In most cases, we have 0 or 1 matching entry.
         */
    CharSequence i18n = null;
    Locale firstLocale = null;
    DefaultInternationalString dis = null;
    final int offset = prefix.length();
    for (final Map.Entry<String, ?> entry : properties.entrySet()) {
        final String key = entry.getKey();
        if (key == null) {
            // Tolerance for Map that accept null keys.
            continue;
        }
        if (!key.startsWith(prefix)) {
            // If the map is sorted, there is no need to check next entries.
            if (isSorted)
                break;
            continue;
        }
        final Locale locale;
        if (key.length() == offset) {
            locale = Locale.ROOT;
        } else {
            final char c = key.charAt(offset);
            if (c != '_') {
                if (isSorted && c > '_')
                    break;
                continue;
            }
            final int s = offset + 1;
            try {
                locale = Locales.parse(key, s);
            } catch (IllformedLocaleException e) {
                throw new IllegalArgumentException(Errors.getResources(properties).getString(Errors.Keys.IllegalLanguageCode_1, '(' + key.substring(0, s) + ')' + key.substring(s), e));
            }
        }
        final Object value = entry.getValue();
        if (value != null) {
            if (!(value instanceof CharSequence)) {
                throw new IllegalArgumentException(Errors.getResources(properties).getString(Errors.Keys.IllegalPropertyValueClass_2, key, value.getClass()));
            }
            if (i18n == null) {
                i18n = (CharSequence) value;
                firstLocale = locale;
            } else {
                if (dis == null) {
                    dis = new DefaultInternationalString();
                    dis.add(firstLocale, i18n);
                    i18n = dis;
                }
                dis.add(locale, (CharSequence) value);
            }
        }
    }
    return toInternationalString(i18n);
}
Also used : Locale(java.util.Locale) IllformedLocaleException(java.util.IllformedLocaleException) InternationalString(org.opengis.util.InternationalString) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 84 with InternationalString

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

the class TableColumnTest method testConstantHeader.

/**
 * Test the header of some constants.
 */
@Test
public void testConstantHeader() {
    InternationalString i18n = NAME.getHeader();
    assertEquals("Name", i18n.toString(Locale.ROOT));
    assertEquals("Name", i18n.toString(Locale.ENGLISH));
    assertEquals("Nom", i18n.toString(Locale.FRENCH));
    assertSame("Test caching", i18n, NAME.getHeader());
    i18n = TYPE.getHeader();
    assertEquals("Type", i18n.toString(Locale.ROOT));
    assertEquals("Type", i18n.toString(Locale.ENGLISH));
    assertEquals("Type", i18n.toString(Locale.FRENCH));
    assertSame("Test caching", i18n, TYPE.getHeader());
}
Also used : InternationalString(org.opengis.util.InternationalString) Test(org.junit.Test)

Example 85 with InternationalString

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

the class IndexedResourceBundleTest method testFormatInternational.

/**
 * Tests the formatting of an international string.
 */
@Test
@DependsOnMethod("testGetResources")
public void testFormatInternational() {
    InternationalString i18n = Errors.formatInternational(Errors.Keys.NullArgument_1);
    assertEquals("Argument ‘{0}’ shall not be null.", i18n.toString(Locale.ROOT));
    assertEquals("Argument ‘{0}’ shall not be null.", i18n.toString(Locale.ENGLISH));
    assertEquals("L’argument ‘{0}’ ne doit pas être nul.", i18n.toString(Locale.FRENCH));
    assertNotSame(i18n, assertSerializedEquals(i18n));
    i18n = Errors.formatInternational(Errors.Keys.NullArgument_1, "CRS");
    assertEquals("Argument ‘CRS’ shall not be null.", i18n.toString(Locale.ROOT));
    assertEquals("Argument ‘CRS’ shall not be null.", i18n.toString(Locale.ENGLISH));
    assertEquals("L’argument ‘CRS’ ne doit pas être nul.", i18n.toString(Locale.FRENCH));
    assertNotSame(i18n, assertSerializedEquals(i18n));
}
Also used : InternationalString(org.opengis.util.InternationalString) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Aggregations

InternationalString (org.opengis.util.InternationalString)86 SimpleInternationalString (org.apache.sis.util.iso.SimpleInternationalString)20 Test (org.junit.Test)17 DefaultCitation (org.apache.sis.metadata.iso.citation.DefaultCitation)13 IdentifiedObject (org.opengis.referencing.IdentifiedObject)10 ArrayList (java.util.ArrayList)7 Locale (java.util.Locale)6 GenericName (org.opengis.util.GenericName)6 Extent (org.opengis.metadata.extent.Extent)5 DefaultDataIdentification (org.apache.sis.metadata.iso.identification.DefaultDataIdentification)4 DependsOnMethod (org.apache.sis.test.DependsOnMethod)4 Vocabulary (org.apache.sis.util.resources.Vocabulary)4 Citation (org.opengis.metadata.citation.Citation)4 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)3 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TableAppender (org.apache.sis.io.TableAppender)3 AbstractIdentifiedObject (org.apache.sis.referencing.AbstractIdentifiedObject)3 DataStoreException (org.apache.sis.storage.DataStoreException)3