001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.referencing;
018:
019: // J2SE direct dependencies
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.util.List;
023: import java.util.Locale;
024: import java.util.Iterator;
025: import java.util.ArrayList;
026: import java.util.Comparator;
027: import java.util.Collections;
028:
029: // OpenGIS dependencies
030: import org.opengis.metadata.citation.Citation;
031: import org.opengis.referencing.Factory;
032: import org.opengis.referencing.AuthorityFactory;
033:
034: // Geotools dependencies
035: import org.geotools.factory.FactoryRegistry;
036: import org.geotools.io.TableWriter;
037: import org.geotools.resources.Utilities;
038: import org.geotools.resources.i18n.Vocabulary;
039: import org.geotools.resources.i18n.VocabularyKeys;
040:
041: /**
042: * Prints a list of factory. This is used for {@link ReferencingFactoryFinder#listProviders}
043: * implementation only.
044: *
045: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/referencing/FactoryPrinter.java $
046: * @version $Id: FactoryPrinter.java 25050 2007-04-06 00:41:49Z jgarnett $
047: * @author Desruisseaux
048: */
049: final class FactoryPrinter implements Comparator {
050: /**
051: * Constructs a default instance of this printer.
052: */
053: public FactoryPrinter() {
054: }
055:
056: /**
057: * Compares two factories for order. This is used for sorting out the factories
058: * before to display them.
059: */
060: public int compare(final Object object1, final Object object2) {
061: final Class/*<Factory>*/factory1 = (Class) object1;
062: final Class/*<Factory>*/factory2 = (Class) object2;
063: if (false) {
064: // Sort authority factory last
065: final boolean isAuthority1 = AuthorityFactory.class
066: .isAssignableFrom(factory1);
067: final boolean isAuthority2 = AuthorityFactory.class
068: .isAssignableFrom(factory2);
069: if (isAuthority1 && !isAuthority2)
070: return +1;
071: if (isAuthority2 && !isAuthority1)
072: return -1;
073: return 0;
074: } else {
075: // Or sort by name
076: return Utilities.getShortName(factory1)
077: .compareToIgnoreCase(
078: Utilities.getShortName(factory2));
079: }
080: }
081:
082: /**
083: * List all available factory implementations in a tabular format. For each factory interface,
084: * the first implementation listed is the default one. This method provides a way to check the
085: * state of a system, usually for debugging purpose.
086: *
087: * @param FactoryRegistry Where the factories are registered.
088: * @param out The output stream where to format the list.
089: * @param locale The locale for the list, or {@code null}.
090: * @throws IOException if an error occurs while writing to {@code out}.
091: */
092: public void list(final FactoryRegistry registry, final Writer out,
093: final Locale locale) throws IOException {
094: /*
095: * Gets the categories in some sorted order.
096: */
097: final List categories = new ArrayList();
098: for (final Iterator it = registry.getCategories(); it.hasNext();) {
099: categories.add(it.next());
100: }
101: Collections.sort(categories, this );
102: /*
103: * Prints the table header.
104: */
105: final Vocabulary resources = Vocabulary.getResources(locale);
106: final TableWriter table = new TableWriter(out, " \u2502 ");
107: table.setMultiLinesCells(true);
108: table.writeHorizontalSeparator();
109: table.write(resources.getString(VocabularyKeys.FACTORY));
110: table.nextColumn();
111: table.write(resources.getString(VocabularyKeys.AUTHORITY));
112: table.nextColumn();
113: table.write(resources.getString(VocabularyKeys.VENDOR));
114: table.nextColumn();
115: table
116: .write(resources
117: .getString(VocabularyKeys.IMPLEMENTATIONS));
118: table.nextLine();
119: table.nextLine('\u2550');
120: final StringBuffer vendors = new StringBuffer();
121: final StringBuffer implementations = new StringBuffer();
122: for (final Iterator it = categories.iterator(); it.hasNext();) {
123: /*
124: * Writes the category name (CRSFactory, DatumFactory, etc.)
125: */
126: final Class category = (Class) it.next();
127: table.write(Utilities.getShortName(category));
128: table.nextColumn();
129: /*
130: * Write the authorities in a single cell. Same for vendors and implementations.
131: */
132: final Iterator providers = registry.getServiceProviders(
133: category, null, null);
134: while (providers.hasNext()) {
135: if (implementations.length() != 0) {
136: table.write('\n');
137: vendors.append('\n');
138: implementations.append('\n');
139: }
140: final Factory provider = (Factory) providers.next();
141: final Citation vendor = provider.getVendor();
142: vendors.append(vendor.getTitle().toString(locale));
143: implementations.append(Utilities
144: .getShortClassName(provider));
145: if (provider instanceof AuthorityFactory) {
146: final Citation authority = ((AuthorityFactory) provider)
147: .getAuthority();
148: final Iterator identifiers = authority
149: .getIdentifiers().iterator();
150: final String identifier = identifiers.hasNext() ? identifiers
151: .next().toString()
152: : authority.getTitle().toString(locale);
153: table.write(identifier);
154: }
155: }
156: /*
157: * Writes the vendors.
158: */
159: table.nextColumn();
160: table.write(vendors.toString());
161: vendors.setLength(0);
162: /*
163: * Writes the implementation class name.
164: */
165: table.nextColumn();
166: table.write(implementations.toString());
167: implementations.setLength(0);
168: table.writeHorizontalSeparator();
169: }
170: table.flush();
171: }
172: }
|