Search in sources :

Example 1 with NameSpace

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

the class DefaultRecordTypeTest method testArgumentChecks.

/**
 * Ensures that constructions of {@link DefaultRecordType} with
 * inconsistent arguments throw an exception.
 */
@Test
@DependsOnMethod("testConstructor")
public void testArgumentChecks() {
    init();
    final DefaultTypeName correctRecordName = recordTypeName;
    final NameSpace correctMemberNamespace = memberName.scope();
    final DefaultNameSpace wrongNamespace = new DefaultNameSpace(null, "WrongNameSpace", ":", ":");
    /*
         * RecordType namespace validation.
         * Constructor shall require "MyNameSpace:MyRecordType".
         */
    recordTypeName = new DefaultTypeName(wrongNamespace, "MyRecordType");
    try {
        create();
        fail("Should have detected namespace mismatch.");
    } catch (IllegalArgumentException e) {
        final String message = e.getMessage();
        // Expected name.
        assertTrue(message, message.contains("MyNameSpace"));
        // Actual namespace.
        assertTrue(message, message.contains("WrongNameSpace:MyRecordType"));
    }
    /*
         * MemberName namespace validation.
         * Constructor shall require "MyNameSpace:MyRecordType:aMember".
         */
    recordTypeName = correctRecordName;
    memberName = new DefaultMemberName(wrongNamespace, "aMember", memberTypeName);
    try {
        create();
        fail("Should have detected namespace mismatch.");
    } catch (IllegalArgumentException e) {
        final String message = e.getMessage();
        // Expected name.
        assertTrue(message, message.contains("MyNameSpace:MyRecordType"));
        // Actual namespace.
        assertTrue(message, message.contains("WrongNameSpace:aMember"));
    }
    /*
         * MemberName type validation.
         */
    final DefaultTypeName otherType = new DefaultTypeName(memberTypeName.scope(), "Real");
    memberName = new DefaultMemberName(correctMemberNamespace, "aMember", otherType);
    try {
        create();
        fail("Should have detected type mismatch.");
    } catch (IllegalArgumentException e) {
        final String message = e.getMessage();
        assertTrue(message, message.contains("aMember"));
        assertTrue(message, message.contains("gco:Integer"));
    }
}
Also used : NameSpace(org.opengis.util.NameSpace) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 2 with NameSpace

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

the class EPSGDataAccess method createProperties.

/**
 * Returns the name and aliases for the {@link IdentifiedObject} to construct.
 *
 * @param  table       the table on which a query has been executed.
 * @param  name        the name for the {@link IdentifiedObject} to construct.
 * @param  code        the EPSG code of the object to construct.
 * @param  remarks     remarks as a {@link String} or {@link InternationalString}, or {@code null} if none.
 * @param  deprecated  {@code true} if the object to create is deprecated.
 * @return the name together with a set of properties.
 */
@SuppressWarnings("ReturnOfCollectionOrArrayField")
private Map<String, Object> createProperties(final String table, String name, final Integer code, CharSequence remarks, final boolean deprecated) throws SQLException, FactoryDataException {
    /*
         * Search for aliases. Note that searching for the object code is not sufficient. We also need to check if the
         * record is really from the table we are looking for since different tables may have objects with the same ID.
         *
         * Some aliases are identical to the name except that some letters are replaced by their accented letters.
         * For example "Reseau Geodesique Francais" → "Réseau Géodésique Français". If we find such alias, replace
         * the name by the alias so we have proper display in user interface. Notes:
         *
         *   - WKT formatting will still be compliant with ISO 19162 because the WKT formatter replaces accented
         *     letters by ASCII ones.
         *   - We do not perform this replacement directly in our EPSG database because ASCII letters are more
         *     convenient for implementing accent-insensitive searches.
         */
    final List<GenericName> aliases = new ArrayList<>();
    try (ResultSet result = executeQuery("Alias", "SELECT OBJECT_TABLE_NAME, NAMING_SYSTEM_NAME, ALIAS" + " FROM [Alias] INNER JOIN [Naming System]" + " ON [Alias].NAMING_SYSTEM_CODE =" + " [Naming System].NAMING_SYSTEM_CODE" + " WHERE OBJECT_CODE = ?", code)) {
        while (result.next()) {
            if (tableMatches(table, result.getString(1))) {
                final String naming = getOptionalString(result, 2);
                final String alias = getString(code, result, 3);
                NameSpace ns = null;
                if (naming != null) {
                    ns = namingSystems.get(naming);
                    if (ns == null) {
                        ns = owner.nameFactory.createNameSpace(owner.nameFactory.createLocalName(null, naming), null);
                        namingSystems.put(naming, ns);
                    }
                }
                if (CharSequences.toASCII(alias).toString().equals(name)) {
                    name = alias;
                } else {
                    aliases.add(owner.nameFactory.createLocalName(ns, alias));
                }
            }
        }
    }
    /*
         * At this point we can fill the properties map.
         */
    properties.clear();
    GenericName gn = null;
    final Locale locale = getLocale();
    final Citation authority = owner.getAuthority();
    final InternationalString edition = authority.getEdition();
    final String version = (edition != null) ? edition.toString() : null;
    if (name != null) {
        gn = owner.nameFactory.createGenericName(namespace, Constants.EPSG, name);
        properties.put("name", gn);
        properties.put(NamedIdentifier.CODE_KEY, name);
        properties.put(NamedIdentifier.VERSION_KEY, version);
        properties.put(NamedIdentifier.AUTHORITY_KEY, authority);
        properties.put(AbstractIdentifiedObject.LOCALE_KEY, locale);
        final NamedIdentifier id = new NamedIdentifier(properties);
        properties.clear();
        properties.put(IdentifiedObject.NAME_KEY, id);
    }
    if (!aliases.isEmpty()) {
        properties.put(IdentifiedObject.ALIAS_KEY, aliases.toArray(new GenericName[aliases.size()]));
    }
    if (code != null) {
        final String codeString = code.toString();
        final ImmutableIdentifier identifier;
        if (deprecated) {
            final String replacedBy = getSupersession(table, code, locale);
            identifier = new DeprecatedCode(authority, Constants.EPSG, codeString, version, Character.isDigit(replacedBy.charAt(0)) ? replacedBy : null, Vocabulary.formatInternational(Vocabulary.Keys.SupersededBy_1, replacedBy));
            properties.put(AbstractIdentifiedObject.DEPRECATED_KEY, Boolean.TRUE);
        } else {
            identifier = new ImmutableIdentifier(authority, Constants.EPSG, codeString, version, (gn != null) ? gn.toInternationalString() : null);
        }
        properties.put(IdentifiedObject.IDENTIFIERS_KEY, identifier);
    }
    properties.put(IdentifiedObject.REMARKS_KEY, remarks);
    properties.put(AbstractIdentifiedObject.LOCALE_KEY, locale);
    properties.put(ReferencingServices.MT_FACTORY, owner.mtFactory);
    return properties;
}
Also used : Locale(java.util.Locale) GenericName(org.opengis.util.GenericName) InternationalString(org.opengis.util.InternationalString) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) NamedIdentifier(org.apache.sis.referencing.NamedIdentifier) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) NameSpace(org.opengis.util.NameSpace) DefaultNameSpace(org.apache.sis.util.iso.DefaultNameSpace) InternationalString(org.opengis.util.InternationalString) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) Citation(org.opengis.metadata.citation.Citation) DefaultCitation(org.apache.sis.metadata.iso.citation.DefaultCitation) DeprecatedCode(org.apache.sis.internal.referencing.DeprecatedCode) ImmutableIdentifier(org.apache.sis.metadata.iso.ImmutableIdentifier)

Example 3 with NameSpace

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

the class DefaultLocalName method castOrCopy.

/**
 * Returns a SIS local name implementation with the values of the given arbitrary implementation.
 * This method performs the first applicable action in the following choices:
 *
 * <ul>
 *   <li>If the given object is {@code null}, then this method returns {@code null}.</li>
 *   <li>Otherwise if the given object is an instance of {@link MemberName} or {@link TypeName},
 *       then this method delegates to {@code castOrCopy(…)} method of the corresponding subclass.</li>
 *   <li>Otherwise if the given object is already an instance of {@code DefaultLocalName},
 *       then it is returned unchanged.</li>
 *   <li>Otherwise a new {@code DefaultLocalName} instance is created
 *       with the same values than the given name.</li>
 * </ul>
 *
 * @param  object  the object to get as a SIS implementation, or {@code null} if none.
 * @return a SIS implementation containing the values of the given object (may be the
 *         given object itself), or {@code null} if the argument was null.
 */
public static DefaultLocalName castOrCopy(final LocalName object) {
    if (object instanceof MemberName) {
        return DefaultMemberName.castOrCopy((MemberName) object);
    }
    if (object instanceof TypeName) {
        return DefaultTypeName.castOrCopy((TypeName) object);
    }
    if (object == null || object instanceof DefaultLocalName) {
        return (DefaultLocalName) object;
    }
    final NameSpace scope = object.scope();
    final InternationalString name = object.toInternationalString();
    if (scope instanceof DefaultNameSpace) {
        // May return a cached instance.
        return ((DefaultNameSpace) scope).local(name, null);
    } else {
        return new DefaultLocalName(scope, name);
    }
}
Also used : TypeName(org.opengis.util.TypeName) InternationalString(org.opengis.util.InternationalString) NameSpace(org.opengis.util.NameSpace) MemberName(org.opengis.util.MemberName)

Example 4 with NameSpace

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

the class Names method toExpandedString.

/**
 * Formats the given name in <cite>expanded form</cite> close to the Java Content Repository (JCR) definition.
 * The expanded form is defined as below:
 *
 * <blockquote><pre> ExpandedName ::= '{' NameSpace '}' LocalPart
 * NameSpace    ::= name.{@linkplain AbstractName#scope() scope()}.{@linkplain DefaultNameSpace#name() name()}.toString()
 * LocalPart    ::= name.{@linkplain AbstractName#toString() toString()}</pre></blockquote>
 *
 * @param  name  the generic name to format in expanded form, or {@code null}.
 * @return expanded form of the given generic name, or {@code null} if the given name was null.
 *
 * @see DefaultNameSpace#toString()
 */
public static String toExpandedString(final GenericName name) {
    if (name == null) {
        return null;
    }
    final String localPart = name.toString();
    final NameSpace scope = name.scope();
    if (scope == null || scope.isGlobal()) {
        return localPart;
    }
    final String ns = scope.name().toString();
    return new StringBuilder(ns.length() + localPart.length() + 2).append('{').append(ns).append('}').append(localPart).toString();
}
Also used : NameSpace(org.opengis.util.NameSpace) InternationalString(org.opengis.util.InternationalString)

Example 5 with NameSpace

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

the class NameMarshallingTest method testLocalNameWithScope.

/**
 * Tests XML of a {@link LocalName} with a scope.
 *
 * @throws JAXBException if (un)marshalling failed.
 */
@Test
@DependsOnMethod("testLocalName")
public void testLocalNameWithScope() throws JAXBException {
    final NameFactory factory = DefaultFactories.forBuildin(NameFactory.class);
    final NameSpace scope = factory.createNameSpace(factory.createLocalName(null, "A code space"), null);
    final LocalName name = factory.createLocalName(scope, "A name in a scope");
    assertEquals("A name in a scope", name.toString());
    final String expected = "<gml:IO_IdentifiedObject xmlns:gml=\"" + Namespaces.GML + '"' + " xmlns:gco=\"" + LegacyNamespaces.GCO + "\">\n" + "  <gml:alias>\n" + "    <gco:LocalName codeSpace=\"A code space\">A name in a scope</gco:LocalName>\n" + "  </gml:alias>\n" + "</gml:IO_IdentifiedObject>\n";
    final String actual = marshal(name);
    assertXmlEquals(expected, actual, "xmlns:*");
    assertEquals(name, unmarshal(expected));
}
Also used : NameSpace(org.opengis.util.NameSpace) NameFactory(org.opengis.util.NameFactory) LocalName(org.opengis.util.LocalName) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Aggregations

NameSpace (org.opengis.util.NameSpace)10 InternationalString (org.opengis.util.InternationalString)4 Test (org.junit.Test)3 DependsOnMethod (org.apache.sis.test.DependsOnMethod)2 GenericName (org.opengis.util.GenericName)2 LocalName (org.opengis.util.LocalName)2 NameFactory (org.opengis.util.NameFactory)2 TypeName (org.opengis.util.TypeName)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1 DeprecatedCode (org.apache.sis.internal.referencing.DeprecatedCode)1 ImmutableIdentifier (org.apache.sis.metadata.iso.ImmutableIdentifier)1 DefaultCitation (org.apache.sis.metadata.iso.citation.DefaultCitation)1 NamedIdentifier (org.apache.sis.referencing.NamedIdentifier)1 DefaultNameSpace (org.apache.sis.util.iso.DefaultNameSpace)1 SimpleInternationalString (org.apache.sis.util.iso.SimpleInternationalString)1