001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/model/csct/resources/Naming.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2007 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/exse/ 008 lat/lon GmbH 009 http://www.lat-lon.de 010 011 It has been implemented within SEAGIS - An OpenSource implementation of OpenGIS specification 012 (C) 2001, Institut de Recherche pour le D�veloppement (http://sourceforge.net/projects/seagis/) 013 SEAGIS Contacts: Surveillance de l'Environnement Assist�e par Satellite 014 Institut de Recherche pour le D�veloppement / US-Espace 015 mailto:seasnet@teledetection.fr 016 017 018 This library is free software; you can redistribute it and/or 019 modify it under the terms of the GNU Lesser General Public 020 License as published by the Free Software Foundation; either 021 version 2.1 of the License, or (at your option) any later version. 022 023 This library is distributed in the hope that it will be useful, 024 but WITHOUT ANY WARRANTY; without even the implied warranty of 025 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 026 Lesser General Public License for more details. 027 028 You should have received a copy of the GNU Lesser General Public 029 License along with this library; if not, write to the Free Software 030 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 031 032 Contact: 033 034 Andreas Poth 035 lat/lon GmbH 036 Aennchenstr. 19 037 53115 Bonn 038 Germany 039 E-Mail: poth@lat-lon.de 040 041 Klaus Greve 042 Department of Geography 043 University of Bonn 044 Meckenheimer Allee 166 045 53115 Bonn 046 Germany 047 E-Mail: klaus.greve@uni-bonn.de 048 049 050 ---------------------------------------------------------------------------*/ 051 package org.deegree.model.csct.resources; 052 053 // Parameters 054 import java.util.HashMap; 055 import java.util.Iterator; 056 import java.util.Map; 057 058 import javax.media.jai.ParameterList; 059 import javax.media.jai.ParameterListDescriptor; 060 import javax.media.jai.ParameterListImpl; 061 import javax.media.jai.util.CaselessStringKey; 062 063 import org.deegree.model.csct.resources.css.ResourceKeys; 064 import org.deegree.model.csct.resources.css.Resources; 065 066 /** 067 * Methods for binding names to {@link ParameterListDescriptor}s. For example, 068 * {@link org.deegree.model.csct.cs.Projection} using this class for binding classification name to 069 * parameter list descriptors. 070 * 071 * @version 1.0 072 * @author Martin Desruisseaux 073 */ 074 public final class Naming { 075 /** 076 * The naming to use for mapping projection's classification name to parameter descriptor. 077 */ 078 public static final Naming PROJECTIONS = new Naming( "org.deegree.model.csct.ct.MathTransformFactory" ); 079 080 /** 081 * Map classification name to {@link ParameterListDescriptor} objects. Keys are 082 * {@link CaselessStringKey} object, while values are {@link ParameterListDescriptor} objects. 083 */ 084 private Map<CaselessStringKey, ParameterListDescriptor> descriptors; 085 086 /** 087 * The fully qualified name of the class to load for initializing binding, or <code>null</code> 088 * if none. If non-null, then the static initializer of this class should invokes {@link #bind} 089 * for binding a default set of descriptors. 090 */ 091 private final String initializer; 092 093 /** 094 * Construct a <code>Naming</code> object. 095 * 096 * @param initializer 097 * The fully qualified name of the class to load for initializing binding. 098 */ 099 private Naming( final String initializer ) { 100 this.initializer = initializer; 101 } 102 103 /** 104 * Try to bind a set of default projections. Those default projections are binded during the 105 * static initialization of {org.deegree.model.csct.ct.MathTransformFactory} class. If the 106 * operation fail, a warning is logged but the process continue. 107 */ 108 private void bindDefaults() { 109 try { 110 Class.forName( initializer ); 111 } catch ( ClassNotFoundException exception ) { 112 exception.printStackTrace(); 113 } 114 } 115 116 /** 117 * Binds a classification name to a parameter list descriptor. 118 * 119 * @param classification 120 * The classification name. 121 * @param descriptor 122 * the parameter list descriptor. 123 * @throws IllegalArgumentException 124 * if a descriptor is already bounds for the specified classification name. 125 */ 126 public synchronized void bind( final String classification, final ParameterListDescriptor descriptor ) 127 throws IllegalArgumentException { 128 if ( descriptors == null ) { 129 descriptors = new HashMap<CaselessStringKey, ParameterListDescriptor>(); 130 bindDefaults(); 131 } 132 final CaselessStringKey key = new CaselessStringKey( classification ); 133 if ( descriptors.containsKey( key ) ) { 134 throw new IllegalArgumentException( Resources.format( ResourceKeys.PROJECTION_ALREADY_BOUNDS_$1, 135 classification ) ); 136 } 137 descriptors.put( key, descriptor ); 138 } 139 140 /** 141 * Returns a default parameter descriptor for the specified classification name, or 142 * <code>null</code> if none is found. 143 * 144 * @param classification 145 * The classification to look for. 146 * @return The descriptor for the specified classification, or <code>null</code> if none. 147 */ 148 public synchronized ParameterListDescriptor lookup( final String classification ) { 149 if ( descriptors == null ) { 150 descriptors = new HashMap<CaselessStringKey, ParameterListDescriptor>(); 151 bindDefaults(); 152 } 153 return descriptors.get( new CaselessStringKey( classification ) ); 154 } 155 156 /** 157 * Returns a parameter list for the specified classification. If there is no explicit parameter 158 * descriptor for the specified classification, then a default descriptor is used. 159 * 160 * @param classification 161 * The classification to look for. 162 * @param fallback 163 * The default parameter list descriptor to use if no descriptor has been found for 164 * the specified classification. 165 * @return A parameter list to use for the specified classification 166 */ 167 public ParameterList getParameterList( final String classification, final ParameterListDescriptor fallback ) { 168 ParameterListDescriptor descriptor = lookup( classification ); 169 if ( descriptor == null ) { 170 descriptor = fallback; 171 } 172 return new ParameterListImpl( descriptor ); 173 } 174 175 /** 176 * Returns the list of classification names. 177 * @return list of classification names. 178 */ 179 public synchronized String[] list() { 180 if ( descriptors == null ) { 181 descriptors = new HashMap<CaselessStringKey, ParameterListDescriptor>(); 182 bindDefaults(); 183 } 184 int count = 0; 185 final String[] names = new String[descriptors.size()]; 186 for ( final Iterator it = descriptors.keySet().iterator(); it.hasNext(); ) { 187 names[count++] = it.next().toString(); 188 } 189 return names; 190 } 191 }