* @param objects The collection of objects to format.
* @param out The stream or buffer where to write the summary.
* @throws IOException if an error occurred will writing to the given appendable.
*/
private void formatSummary(final IdentifiedObject[] objects, final Appendable out) throws IOException {
final Vocabulary resources = Vocabulary.getResources(displayLocale);
/*
* Prepares all rows before we write them to the output stream, because not all
* identified objects may have names with the same scopes in the same order. We
* also need to iterate over all rows in order to know the number of columns.
*
* The first column is reserved for the identifier. We put null as a sentinal key for
* that column name, to be replaced later by "Identifier" in user locale. We can not
* put the localized strings in the map right now because they could conflict with
* the scope of some alias to be processed below.
*/
boolean hasIdentifiers = false;
final List<String[]> rows = new ArrayList<String[]>();
final Map<String,Integer> columnIndices = new LinkedHashMap<String,Integer>();
columnIndices.put(null, 0); // See above comment for the meaning of "null" here.
if (preferredCodespaces != null) {
for (final String codespace : preferredCodespaces) {
columnIndices.put(codespace, columnIndices.size());
}
}
for (final IdentifiedObject object : objects) {
String[] row = new String[columnIndices.size()]; // Will growth later if needed.
/*
* Put the first identifier in the first column. If no identifier has a codespace in the list
* supplied by the user, then we will use the first identifier (any codespace) as a fallback.
*/
final Set<ReferenceIdentifier> identifiers = object.getIdentifiers();
if (identifiers != null) { // Paranoiac check.
ReferenceIdentifier identifier = null;
for (final ReferenceIdentifier candidate : identifiers) {
if (candidate != null) { // Paranoiac check.
if (isPreferredCodespace(candidate.getCodeSpace())) {
identifier = candidate;
break; // Format now.
}
if (identifier == null) {
identifier = candidate; // To be used as a fallback if we find nothing better.
}
}
}
if (identifier != null) {
row[0] = IdentifiedObjects.toString(identifier);
hasIdentifiers = true;
}
}
/*
* If the name's codespace is in the list of codespaces asked by the user, add that name
* in the current row and clear the 'name' locale variable. Otherwise, keep the 'name'
* locale variable in case we found no alias to format.
*/
ReferenceIdentifier name = object.getName();
if (name != null) { // Paranoiac check.
final String codespace = name.getCodeSpace();
if (isPreferredCodespace(codespace)) {
row = putIfAbsent(resources, row, columnIndices, codespace, name.getCode());
name = null;
}
}
/*
* Put all aliases having a codespace in the list asked by the user.
*/
final Collection<GenericName> aliases = object.getAlias();
if (aliases != null) { // Paranoiac check.
for (final GenericName alias : aliases) {
if (alias != null) { // Paranoiac check.
final String codespace = NameToIdentifier.getCodespaceOrAuthority(alias, displayLocale);
if (isPreferredCodespace(codespace)) {
row = putIfAbsent(resources, row, columnIndices, codespace,
alias.tip().toInternationalString().toString(displayLocale));
name = null;
}
}
}
}
/*
* If no name and no alias have a codespace in the list of codespaces asked by the user,
* force the addition of primary name regardless its codespace.
*/
if (name != null) {
row = putIfAbsent(resources, row, columnIndices, name.getCodeSpace(), name.getCode());
}
rows.add(row);
}
/*
* Writes the table. The header will contain one column for each codespace in the order declared
* by the user. If the user did not specified any codespace, or if we had to write codespace not
* on the user list, then those codespaces will be written in the order we found them.
*/
final boolean hasColors = (colors != null);
final TableAppender table = new TableAppender(out, columnSeparator);
table.setMultiLinesCells(true);
table.appendHorizontalSeparator();
for (String codespace : columnIndices.keySet()) {
if (codespace == null) {
if (!hasIdentifiers) continue; // Skip empty column.
codespace = resources.getString(Vocabulary.Keys.Identifier);
}
if (hasColors) {
codespace = X364.BOLD.sequence() + codespace + X364.NORMAL.sequence();
}
table.append(codespace);