basicfactory.java

来自「world wind java sdk 源码」· Java 代码 · 共 201 行

JAVA
201
字号
/*Copyright (C) 2001, 2009 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind;import gov.nasa.worldwind.avlist.*;import gov.nasa.worldwind.exception.*;import gov.nasa.worldwind.globes.ElevationModel;import gov.nasa.worldwind.terrain.BasicElevationModelFactory;import gov.nasa.worldwind.util.*;import gov.nasa.worldwind.wms.Capabilities;import java.io.File;/** * A basic implementation of the {@link Factory} interface. * * @author tag * @version $Id: BasicFactory.java 9600 2009-03-22 20:04:40Z tgaskins $ */public class BasicFactory implements Factory{    /**     * Creates an object, determining the method to use from a list of specified keys.     * <p/>     * The method recognizes keys with terminating characters of "ConfigFile" and "ClassName". The key itself is used to     * determine from {@link Configuration} the actual configuration file name or class name, respectively. A key     * without recognizable terminating characters is considered to be a class name key, as though its terminating     * characters are "ClassName".     * <p/>     * Each key is tried in the order specified. The method whose key is first recognized and whose corresponding     * configuration component, e.g., class name or configuration file name, is found is used to create an object. If no     * mechanism is matched then an exception is thrown. An exception is also thrown when a matched method fails during     * its attempt to create the object.     * <p/>     * Subclasses are given the chance to handle each key before the other mechanisms are tried. For each key, the     * method {@link #doCreateFromKey(String)} is called prior to trying the other mechanisms. If that method returns     * non-null then this method returns immediately. If {@link #doCreateFromKey(String)} returns null, the other     * mechanisms are tried before calling it again with the next key.     *     * @param sourceKeys an array of keys identifying which mechanisms to use to create the object.     *     * @return the desired object if a mechanism was found to successfully create it.     *     * @throws IllegalArgumentException if <code>sourceKeys</code> is null or empty.     * @throws WWRuntimeException       if no mechanism is matched or the matched mechanism throws an exception or     *                                  otherwise fails.     */    public Object createFromKeys(String[] sourceKeys)    {        if (sourceKeys == null || sourceKeys.length == 0)        {            String msg = Logging.getMessage("nullValue.KeyListIsNull");            Logging.logger().severe(msg);            throw new IllegalArgumentException(msg);        }        for (String key : sourceKeys)        {            try            {                Object o = this.doCreateFromKey(key);                if (o != null)                    return 0;                if (key.endsWith("ClassName"))                {                    if (Configuration.getStringValue(key) != null)                        return createFromClassName(Configuration.getStringValue(key));                }                else if (key.endsWith("ConfigFile"))                {                    if (Configuration.getStringValue(key) != null)                        return createFromConfigFile(Configuration.getStringValue(key));                }                else if (key.endsWith("Capabilities"))                {                    if (Configuration.getStringValue(key) != null)                        return createFromCapabilities(Configuration.getStringValue(key), null);                }                else                {                    return createFromClassName(Configuration.getStringValue(key));                }            }            catch (WWRuntimeException e)            {                String msg = Logging.getMessage("BasicFactory.ConfigurationFailed", key);                throw new WWRuntimeException(msg, e);            }        }        throw new WWUnrecognizedException(Logging.getMessage("BasicFactory.UnrecognizedConfigurationKeys"));    }    /**     * Called during {@link #createFromKeys(String[])} for each key prior to attempting to create an object by the     * default mechanisms. This give subclasses the first opportunity to create the object. If this method returns null,     * the other mechanisms are tried for the indicated key. If those other mechanisms fail, this method is called again     * with the next key.     *     * @param key the key identifying the mechanism to use to create an object and the resource with which to do it.     *     * @return an instance of the desired object if creation is successful, otherwise null. The default instance of this     *         method always returns null.     */    @SuppressWarnings({"UnusedDeclaration"})    protected Object doCreateFromKey(String key)    {        return null;    }    /**     * Creates an object given the object's class name. Effectively an argument checking and exception logging wrapper     * around the statement <code>Class.forName(className.trim()).newInstance()</code>.     *     * @param className the fully qualified class name of the class to create.     *     * @return the new object     */    public Object createFromClassName(String className)    {        return WorldWind.createComponent(className);    }    /**     * A no-op implemenation of the {@link Factory#createFromConfigFile(String)} method provided so that this class can     * be instantiated. Not all factories need a <code>createfromConfigFile</code> method; this class is therefore     * useful as is in these cases.     *     * @param fileName normally the path to the configuration file. It must be either an absolute path or a relative     *                 path available on the classpath. But this dummy method implementation ignores this argument.     *     * @return this no-op method always returns null.     */    public Object createFromConfigFile(String fileName)    {        return null;    }    /**     * Create an object such as a layer or elevation model given a local OGC capabilities document containing named     * layer descriptions.     *     * @param capsFileName the path to the capabilities file. The file must be either an absolute path or a relative     *                     path available on the classpath. The file contents must be a valid OGC capabilities     *                     document.     * @param params       a list of configuration properties. These properties override any specified in the     *                     capabilities document. The list should contain the {@link AVKey#LAYER_NAMES} property for     *                     services that define layer, indicating which named layers described in the capabilities     *                     document to create. If this argumet is null or contains no layers, the first named layer is     *                     used.     *     * @return the requested object.     *     * @throws IllegalArgumentException if the file name is null.     * @throws IllegalStateException    if the capabilites document contains no named layer definitions.     * @throws WWRuntimeException       if an error occurs while opening, reading or parsing the capabilities document.     */    public ElevationModel createFromCapabilities(String capsFileName, AVList params)    {        if (capsFileName == null)        {            String message = Logging.getMessage("nullValue.FilePathIsNull");            Logging.logger().severe(message);            throw new IllegalArgumentException(message);        }        File capsFile = new File(capsFileName);        if (!capsFile.exists())        {            String message = Logging.getMessage("generic.FileNotFound", capsFile.getPath());            Logging.logger().severe(message);            throw new IllegalArgumentException(message);        }        Capabilities caps = Capabilities.parse(WWXML.openDocumentFile(capsFileName, BasicElevationModelFactory.class));        return this.doCreateFromCapabilities(caps, params);    }    /**     * Implemented by subclasses to perform the actual object creation. This default implementation always returns null.     *     * @param caps   the capabilities document.     * @param params a list of configuration properties. These properties override any specified in the capabilities     *               document. The list should contain the {@link AVKey#LAYER_NAMES} property for services that define     *               layers, indicating which named layers described in the capabilities document to create. If this     *               argumet is null or contains no layers, the first named layer is used.     *     * @return the requested object.     */    protected ElevationModel doCreateFromCapabilities(Capabilities caps, AVList params)    {        return null;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?