use of org.opengis.referencing.ReferenceIdentifier in project sis by apache.
the class NameIterator method next.
/**
* Returns the next name or alias in the iteration.
*
* Note: we do not bother checking for {@code NoSuchElementException} because this iterator
* will be used only by JAXB, which is presumed checking for {@link #hasNext()} correctly.
*/
@Override
public ReferenceIdentifier next() {
final ReferenceIdentifier n = next;
while (alias.hasNext()) {
final GenericName c = alias.next();
if (c instanceof ReferenceIdentifier) {
next = (ReferenceIdentifier) c;
return n;
}
}
next = null;
return n;
}
use of org.opengis.referencing.ReferenceIdentifier in project sis by apache.
the class NameIterator method getID.
/**
* Implementation of {@link AbstractIdentifiedObject#getID()}, provided here for reducing the amount of code
* to load in the common case where XML support is not needed.
*
* <p>The current implementation searches for the first identifier, regardless its authority.
* If no identifier is found, then the name and aliases are used.
* Then, this method returns the concatenation of the following elements separated by hyphens:</p>
* <ul>
* <li>The code space in lower case, retaining only characters that are valid for Unicode identifiers.</li>
* <li>The object type as defined in OGC's URN (see {@link org.apache.sis.internal.util.DefinitionURI})</li>
* <li>The object code, retaining only characters that are valid for Unicode identifiers.</li>
* </ul>
*
* Example: {@code "epsg-crs-4326"}.
*
* <p>The returned ID needs to be unique only in the XML document being marshalled.
* Consecutive invocations of this method do not need to return the same value,
* since it may depends on the marshalling context.</p>
*
* @param context the (un)marshalling context.
* @param object the object for which to get a {@code gml:id}.
* @param name the identified object name, or {@code null} if none.
* @param alias the identified object aliases, or {@code null} if none.
* @param identifiers the identifiers, or {@code null} if none.
* @return proposed value for {@code gml:id} attribute, or {@code null} if none.
*/
static String getID(final Context context, final IdentifiedObject object, final ReferenceIdentifier name, final Collection<? extends GenericName> alias, final Collection<? extends ReferenceIdentifier> identifiers) {
String candidate = Context.getObjectID(context, object);
if (candidate == null) {
final StringBuilder id = new StringBuilder();
/*
* We will iterate over the identifiers first. Only after the iteration is over,
* if we found no suitable ID, then we will use the primary name as a last resort.
*/
if (identifiers != null) {
for (final ReferenceIdentifier identifier : identifiers) {
if (// Really |, not ||
appendUnicodeIdentifier(id, '-', identifier.getCodeSpace(), "", true) | appendUnicodeIdentifier(id, '-', NameMeaning.toObjectType(object.getClass()), "", false) | appendUnicodeIdentifier(id, '-', identifier.getCode(), "", true)) {
/*
* Check for ID uniqueness. If the ID is rejected, then we just need to clear
* the buffer and let the iteration continue the search for another ID.
*/
candidate = id.toString();
if (Context.setObjectForID(context, object, candidate)) {
return candidate;
}
}
// Clear the buffer for another try.
id.setLength(0);
}
}
/*
* In last ressort, use the name or an alias. The name will be used without codespace since
* names are often verbose. If that name is also used, append a number until we find a free ID.
*/
if (isUnnamed(name) || !appendUnicodeIdentifier(id, '-', name.getCode(), "", false)) {
if (alias != null) {
for (final GenericName a : alias) {
if (appendUnicodeIdentifier(id, '-', a.toString(), "", false)) {
break;
}
}
}
}
if (id.length() != 0) {
candidate = id.toString();
if (!Context.setObjectForID(context, object, candidate)) {
final int s = id.append('-').length();
int n = 0;
do {
// Arbitrary limit.
if (++n == 100)
return null;
candidate = id.append(n).toString();
id.setLength(s);
} while (!Context.setObjectForID(context, object, candidate));
}
}
}
return candidate;
}
use of org.opengis.referencing.ReferenceIdentifier in project sis by apache.
the class IdentifiedObjectFormat method format.
/**
* Formats the given object.
*/
@Override
public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) {
final ReferenceIdentifier identifier = ((IdentifiedObject) obj).getName();
if (identifier == null) {
return toAppendTo.append(Vocabulary.getResources(locale).getString(Vocabulary.Keys.Unnamed));
}
if (identifier instanceof GenericName) {
// The toString() behavior is specified by the GenericName javadoc.
return toAppendTo.append(((GenericName) identifier).toInternationalString().toString(locale));
}
final String code = identifier.getCode();
String cs = identifier.getCodeSpace();
if (cs == null || cs.isEmpty()) {
cs = Citations.getIdentifier(identifier.getAuthority(), true);
}
if (cs != null) {
toAppendTo.append(cs).append(Citations.DEFAULT_SEPARATOR);
}
return toAppendTo.append(code);
}
use of org.opengis.referencing.ReferenceIdentifier in project sis by apache.
the class DefaultVerticalDatum method type.
/**
* Returns the type of this datum, or infers the type from the datum name if no type were specified.
* The later case occurs after unmarshalling, since GML 3.2 does not contain any attribute for the datum type.
* It may also happen if the datum were created using reflection.
*
* <p>This method uses heuristic rules and may be changed in any future SIS version. If the type can not be
* determined, default on the ellipsoidal type since it will usually implies no additional calculation.</p>
*
* <p>No synchronization needed; this is not a problem if this value is computed twice.
* This method returns only existing immutable instances.</p>
*
* @see #getVerticalDatumType()
* @see #getTypeElement()
*/
private VerticalDatumType type() {
VerticalDatumType t = type;
if (t == null) {
final ReferenceIdentifier name = super.getName();
type = t = VerticalDatumTypes.guess(name != null ? name.getCode() : null, super.getAlias(), null);
}
return t;
}
use of org.opengis.referencing.ReferenceIdentifier in project sis by apache.
the class NameToIdentifier method isHeuristicMatchForIdentifier.
/**
* Returns {@code true} if the given identifier to search matches one of the object identifiers.
*
* @param identifiers the identifiers to compare against {@code toSearch}.
* @param toSearch the identifier to check for equality.
* @return {@code true} if the identifier to search is found in the given set of identifiers.
*
* @since 0.8
*/
public static boolean isHeuristicMatchForIdentifier(final Iterable<? extends ReferenceIdentifier> identifiers, final String toSearch) {
if (toSearch != null && identifiers != null) {
int s = toSearch.indexOf(DefaultNameSpace.DEFAULT_SEPARATOR);
if (s < 0) {
// no codespace in searched name
for (final Identifier id : identifiers) {
if (toSearch.equalsIgnoreCase(id.getCode())) {
return true;
}
}
return false;
}
do {
final String codespace = toSearch.substring(0, s).trim();
final String code = toSearch.substring(++s).trim();
for (final ReferenceIdentifier id : identifiers) {
if (codespace.equalsIgnoreCase(id.getCodeSpace()) && code.equalsIgnoreCase(id.getCode())) {
return true;
}
}
s = toSearch.indexOf(DefaultNameSpace.DEFAULT_SEPARATOR, s);
} while (s >= 0);
}
return false;
}
Aggregations