Search in sources :

Example 41 with InternationalString

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

the class PropertyAccessorTest method testSetNull.

/**
 * Tests the {@link PropertyAccessor#set(int, Object, Object, int)} method with a {@code null} value.
 * Setting a property to {@code null} is equivalent to removing that property value.
 * The metadata object used by this test (before removal) is:
 *
 * {@preformat text
 *   DefaultCitation
 *     └─Title………………………… Some title
 * }
 */
@Test
@DependsOnMethod("testSet")
public void testSetNull() {
    final DefaultCitation instance = new DefaultCitation("Some title");
    final PropertyAccessor accessor = createPropertyAccessor();
    final InternationalString title = instance.getTitle();
    final int index = accessor.indexOf("title", true);
    // Sanity check before to continue.
    assertEquals("Some title", title.toString());
    assertNull("title", accessor.set(index, instance, null, RETURN_NULL));
    assertNull("title", instance.getTitle());
    instance.setTitle(title);
    assertSame("title", title, accessor.set(index, instance, null, RETURN_PREVIOUS));
    assertNull("title", instance.getTitle());
}
Also used : DefaultCitation(org.apache.sis.metadata.iso.citation.DefaultCitation) InternationalString(org.opengis.util.InternationalString) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 42 with InternationalString

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

the class PropertyInformationTest method validatePresentationForm.

/**
 * Validates information for {@link Citation#getPresentationForms()}.
 * This is validation code to be shared with {@link PropertyAccessorTest#testInformation()}.
 */
static void validatePresentationForm(final ExtendedElementInformation information) {
    assertParentIsCitation(information);
    assertEquals("presentationForm", information.getName());
    final InternationalString definition = information.getDefinition();
    assertEquals("Mode in which the resource is represented.", definition.toString(Locale.ENGLISH));
    // Test other locale here, if any.
    assertInstanceOf("Specific to SIS implementation.", CheckedContainer.class, information);
    assertEquals(PresentationForm.class, ((CheckedContainer<?>) information).getElementType());
    assertEquals(Datatype.CODE_LIST, information.getDataType());
    assertEquals(Obligation.OPTIONAL, information.getObligation());
    assertEquals(Integer.valueOf(Integer.MAX_VALUE), information.getMaximumOccurrence());
    assertNull(information.getDomainValue());
}
Also used : InternationalString(org.opengis.util.InternationalString)

Example 43 with InternationalString

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

the class PropertyInformationTest method testGetDomainValue.

/**
 * Tests {@link PropertyInformation#getDomainValue()} with a non-null range.
 *
 * @throws NoSuchMethodException if the {@code getMaxRelativeHumidity()} or other method has not been found.
 */
@Test
@SuppressWarnings("UnnecessaryBoxing")
public void testGetDomainValue() throws NoSuchMethodException {
    final ExtendedElementInformation information = new PropertyInformation<>(HardCodedCitations.ISO_19115, "maxRelativeHumidity", EnvironmentalRecord.class.getMethod("getMaxRelativeHumidity"), Double.class, DefaultEnvironmentalRecord.class.getMethod("getMaxRelativeHumidity").getAnnotation(ValueRange.class));
    final InternationalString domainValue = information.getDomainValue();
    assertNotNull(domainValue);
    assertEquals("[0.0 … 100.0]", domainValue.toString());
    assertEquals("[0 … 100]", domainValue.toString(Locale.ENGLISH));
    assertEquals("[0 … 100]", domainValue.toString(Locale.FRENCH));
    assertInstanceOf("Specific to SIS implementation.", Range.class, domainValue);
    assertEquals("getMinValue()", Double.valueOf(0), ((Range) domainValue).getMinValue());
    assertEquals("getMaxValue()", Double.valueOf(100), ((Range) domainValue).getMaxValue());
}
Also used : ValueRange(org.apache.sis.measure.ValueRange) ExtendedElementInformation(org.opengis.metadata.ExtendedElementInformation) InternationalString(org.opengis.util.InternationalString) EnvironmentalRecord(org.opengis.metadata.acquisition.EnvironmentalRecord) DefaultEnvironmentalRecord(org.apache.sis.metadata.iso.acquisition.DefaultEnvironmentalRecord) Test(org.junit.Test)

Example 44 with InternationalString

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

the class CustomMetadataTest method testProxy.

/**
 * Tests the marshalling of a metadata implemented by {@link Proxy}.
 *
 * @throws JAXBException if an error occurred during (un)marshalling.
 */
@Test
public void testProxy() throws JAXBException {
    /*
         * A trivial metadata implementation which return the method name
         * for every attribute of type InternationalString.
         */
    final InvocationHandler handler = (Object proxy, Method method, Object[] args) -> {
        if (method.getReturnType() == InternationalString.class) {
            return new SimpleInternationalString(method.getName());
        }
        return null;
    };
    Citation data = (Citation) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { Citation.class }, handler);
    /*
         * Wrap the metadata in a DefaultMetadata, and ensure
         * we can marshall it without an exception being throw.
         */
    data = new DefaultCitation(data);
    final String xml = XML.marshal(data);
    /*
         * A few simple checks.
         */
    assertTrue(xml.contains("title"));
    assertTrue(xml.contains("edition"));
}
Also used : InternationalString(org.opengis.util.InternationalString) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) DefaultCitation(org.apache.sis.metadata.iso.citation.DefaultCitation) Method(java.lang.reflect.Method) Citation(org.opengis.metadata.citation.Citation) DefaultCitation(org.apache.sis.metadata.iso.citation.DefaultCitation) InternationalString(org.opengis.util.InternationalString) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 45 with InternationalString

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

the class Citations method getIdentifier.

/**
 * Infers an identifier from the given citation, or returns {@code null} if no identifier has been found.
 * This method removes leading and trailing {@linkplain Character#isWhitespace(int) whitespaces}.
 * See {@link org.apache.sis.metadata.iso.citation.Citations#getIdentifier(Citation)}
 * for the public documentation of this method.
 *
 * <p><b>Which method to use:</b></p>
 * <ul>
 *   <li>For information purpose (e.g. some {@code toString()} methods), use {@code getIdentifier(…, false)}.</li>
 *   <li>For WKT formatting, use {@code getIdentifier(…, true)} in order to preserve formatting characters.</li>
 *   <li>For assigning a value to a {@code codeSpace} field, use
 *       {@link org.apache.sis.metadata.iso.citation.Citations#getUnicodeIdentifier(Citation)}.</li>
 * </ul>
 *
 * Use {@code getUnicodeIdentifier(…)} method when assigning values to be returned by methods like
 * {@link ReferenceIdentifier#getCodeSpace()}, since those values are likely to be compared without special
 * care about ignorable identifier characters. But if the intent is to format a more complex string
 * like WKT or {@code toString()}, then we suggest to use {@code getIdentifier(citation, true)} instead,
 * which will produce the same result but preserving the ignorable characters, which can be useful
 * for formatting purpose.
 *
 * @param  citation  the citation for which to get the identifier, or {@code null}.
 * @param  strict    {@code true} for returning a non-null value only if the identifier is a valid Unicode identifier.
 * @return a non-empty identifier for the given citation without leading or trailing whitespaces,
 *         or {@code null} if the given citation is null or does not declare any identifier or title.
 *
 * @see <a href="https://issues.apache.org/jira/browse/SIS-201">SIS-201</a>
 */
public static String getIdentifier(final Citation citation, final boolean strict) {
    if (citation != null) {
        // Whether 'identifier' is a Unicode identifier.
        boolean isUnicode = false;
        // The best identifier found so far.
        String identifier = null;
        // Code space of the identifier, or null if none.
        String codeSpace = null;
        final Iterator<? extends Identifier> it = iterator(citation.getIdentifiers());
        if (it != null)
            while (it.hasNext()) {
                final Identifier id = it.next();
                if (id != null && !isDeprecated(id)) {
                    final String candidate = CharSequences.trimWhitespaces(id.getCode());
                    if (candidate != null && !candidate.isEmpty()) {
                        /*
                         * For a non-empty identifier, verify if both the code and its codespace are valid
                         * Unicode identifiers. If a codespace exists, then the code does not need to begin
                         * with a "Unicode identifier start" (it may be a "Unicode identifier part").
                         */
                        String cs = (id instanceof ReferenceIdentifier) ? CharSequences.trimWhitespaces(((ReferenceIdentifier) id).getCodeSpace()) : null;
                        if (cs == null || cs.isEmpty()) {
                            cs = null;
                            isUnicode = CharSequences.isUnicodeIdentifier(candidate);
                        } else {
                            isUnicode = CharSequences.isUnicodeIdentifier(cs);
                            if (isUnicode)
                                for (int i = 0; i < candidate.length(); ) {
                                    final int c = candidate.codePointAt(i);
                                    if (!Character.isUnicodeIdentifierPart(c) && (strict || (c != '.' && c != '-'))) {
                                        /*
                                     * Above special case for '.' and '-' characters is documented
                                     * in the public Citations.getIdentifier(Citation) method.
                                     */
                                        isUnicode = false;
                                        break;
                                    }
                                    i += Character.charCount(c);
                                }
                        }
                        /*
                         * If we found a Unicode identifier, we are done and we can exit the loop.
                         * Otherwise retain the first identifier and continue the search for Unicode identifier.
                         */
                        if (identifier == null || isUnicode) {
                            identifier = candidate;
                            codeSpace = cs;
                            if (isUnicode)
                                break;
                        }
                    }
                }
            }
        /*
             * If no identifier has been found, fallback on the first title or alternate title.
             * We search for alternate titles because ISO specification said that those titles
             * are often used for abbreviations. Again we give preference to Unicode identifiers,
             * which are typically alternate titles.
             */
        if (identifier == null) {
            // Whitepaces removed by toString(…).
            identifier = toString(citation.getTitle());
            if (identifier != null) {
                if (identifier.isEmpty()) {
                    identifier = null;
                } else {
                    isUnicode = CharSequences.isUnicodeIdentifier(identifier);
                }
            }
            if (!isUnicode) {
                final Iterator<? extends InternationalString> iterator = iterator(citation.getAlternateTitles());
                if (iterator != null)
                    while (iterator.hasNext()) {
                        final String candidate = toString(iterator.next());
                        if (candidate != null && !candidate.isEmpty()) {
                            isUnicode = CharSequences.isUnicodeIdentifier(candidate);
                            if (identifier == null || isUnicode) {
                                identifier = candidate;
                                if (isUnicode)
                                    break;
                            }
                        }
                    }
            }
        }
        /*
             * Finished searching in the identifiers, title and alternate titles. If the identifier that
             * we found is not a valid Unicode identifier, we will return it only if the caller did not
             * asked for strictly valid Unicode identifier.
             */
        if (isUnicode || !strict) {
            if (codeSpace != null && !isEPSG(codeSpace, identifier)) {
                return codeSpace + (strict ? '_' : DEFAULT_SEPARATOR) + identifier;
            } else {
                return identifier;
            }
        }
    }
    return null;
}
Also used : Identifier(org.opengis.metadata.Identifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) ReferenceIdentifier(org.opengis.referencing.ReferenceIdentifier) InternationalString(org.opengis.util.InternationalString)

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