propertyhandler.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 1,648 行 · 第 1/5 页

JAVA
1,648
字号
     * Take the from properties, copy them into the internal PropertyHandler     * properties. The what and where are simple for a more clearly defined     * Debug statement. The what and where are only used for debugging     * statements when there are no properties found, so don't put too much work     * into creating them, like adding strings together before passing them in.     * The what and where fit into a debug output statement like:     * PropertyHandler.merge(): no _what_ found in _where_.     *      * @param from the source properties.     * @param what a description of what the from properties represent.     * @param where a description of where the properties were read from.     */    protected void merge(Properties from, String what, String where) {        merge(from, getProperties(), what, where);    }    /**     * Take the from properties, copy them into the to properties. The what and     * where are simple for a more clearly defined Debug statement. The what and     * where are only used for debugging statements when there are no properties     * found, so don't put too much work into creating them, like adding strings     * together before passing them in. The what and where fit into a debug     * output statement like: PropertyHandler.merge(): no _what_ found in     * _where_.     *      * @param from the source properties.     * @param to the destination properties.     * @param what a description of what the from properties represent.     * @param where a description of where the properties were read from.     */    protected void merge(Properties from, Properties to, String what,                         String where) {        if (from.size() > 0) {            if (to == null) {                to = getProperties();            }            PropUtils.copyProperties(from, to);        } else {            if (what != null && DEBUG) {                Debug.output("PropertyHandler.merge(): no " + what + " found"                        + (where == null ? "." : (" in " + where)));            }        }    }    /**     * Merges the properties to the overall properties held by the     * PropertyHandler.     */    public void setProperties(Properties props) {        init(props, null);    }    /**     * Get the current properties set within this handler.     */    public Properties getProperties() {        if (properties == null) {            properties = new Properties();        }        return properties;    }    /**     * Given a property prefix, or markername, from the properties file, get the     * object that was created for it. This method uses the prefix librarian.     */    public Object get(String markername) {        return prefixLibrarian.get(markername.intern());    }    /**     * Get a properties object containing all the properties with the given     * prefix.     */    public Properties getProperties(String prefix) {        Properties prefixProperties = new Properties();        Properties props = getProperties();        if (prefix != null) {            String scopedPrefix = prefix + ".";            for (Enumeration e = props.keys(); e.hasMoreElements();) {                String key = (String) e.nextElement();                if (key.startsWith(scopedPrefix)) {                    prefixProperties.put(key, props.get(key));                }            }        }        return prefixProperties;    }    /**     * Register an object with the prefix librarian against a specific     * markername.     */    public void put(String markername, Object obj) {        prefixLibrarian.put(markername.intern(), obj);    }    /**     * Remove an object from the prefix librarian register, returning that     * object if it has been found.     */    public Object remove(String markername) {        return prefixLibrarian.remove(markername);    }    /**     * Get the Hashtable being held that matches property prefix strings with     * components.     */    public Hashtable getPrefixLibrarian() {        return prefixLibrarian;    }    /**     * Given a BeanContext (actually a MapHandler, to handle SoloMapComponents),     * look for the openmap.components property in the current properties, and     * parse the list given as that property. From that list of marker names,     * look for the marker.class properties and create those Java objects. Those     * objects will be added to the BeanContext given.     *      * @param mapHandler BeanContext.     */    public void createComponents(MapHandler mapHandler) {        int i; // default counter        if (mapHandler == null) {            Debug.message("properties",                    "PropertyHandler.createComponents(): null handler.");            return;        }        ProgressListenerGauge plg = null;        if (updateProgress) {            try {                String internString = i18n.get(this.getClass(),                        "progressTitle",                        "Progress");                plg = new ProgressListenerGauge(internString);                plg.setWindowSupport(new WindowSupport(plg, new WindowSupport.Frm("", true)));                addProgressListener(plg);            } catch (Exception e) {                // Since ProgressListenerGauge is a Swing component, catch                // any exception that would be tossed if it can't be                // created, like if the PropertyHandler is being used on a                // unix system without a DISPLAY.                // plg = null;            }        }        Vector debugList = PropUtils.parseSpacedMarkers(properties.getProperty(Environment.DebugList));        int size = debugList.size();        for (i = 0; i < size; i++) {            String debugMarker = (String) debugList.elementAt(i);            Debug.put(debugMarker);            if (DEBUG) {                Debug.output("PropertyHandler: adding " + debugMarker                        + " to Debug list.");            }        }        String componentProperty = getPropertyPrefix()                + PropertyHandler.componentProperty;        Vector componentList = PropUtils.parseSpacedMarkers(properties.getProperty(componentProperty));        if (Debug.debugging("propertiesdetail")) {            Debug.output("PropertyHandler: creating components from "                    + componentList);        }        if (updateProgress) {            fireProgressUpdate(ProgressEvent.START, i18n.get(this.getClass(),                    "creatingComponentsProgressMessage",                    "Creating Components"), 0, 100);        }        Vector components = ComponentFactory.create(componentList,                properties,                (updateProgress ? getProgressSupport() : null),                true);        size = components.size();        for (i = 0; i < size; i++) {            Object obj = (Object) components.elementAt(i);            try {                if (obj instanceof String) {                    Debug.error("PropertyHandler finding out that the " + obj                            + " wasn't created");                    continue;                }                // mapHandler.add(obj);                // The call to add(obj) above was replaced by the call to                // addLayer() below. This seems to speed up the startup process,                // but if some other component calls mapHandler.add(obj), then                // this list will be purged.                mapHandler.addLater(obj);                String markerName = ((String) componentList.elementAt(i)).intern();                prefixLibrarian.put(markerName, obj);                addUsedPrefix(markerName);            } catch (MultipleSoloMapComponentException msmce) {                Debug.error("PropertyHandler.createComponents(): "                        + "tried to add multiple components of the same "                        + "type when only one is allowed! - " + msmce);            }        }        // Added as a result of the addLater(obj) call above...        mapHandler.purgeLaterList();        if (updateProgress) {            fireProgressUpdate(ProgressEvent.DONE, i18n.get(this.getClass(),                    "completedProgressMessage",                    "Created all components, ready..."), size, size);            removeProgressListener(plg);        }    }    public String getPropertyPrefix() {        String propertyPrefix = this.propertyPrefix;        if (propertyPrefix == null) {            propertyPrefix = Environment.OpenMapPrefix;        }        propertyPrefix = PropUtils.getScopedPropertyPrefix(propertyPrefix);        return propertyPrefix;    }    public void setPropertyPrefix(String propertyPrefix) {        this.propertyPrefix = propertyPrefix;    }    /**     * Creates a Properties object containing the current settings as defined by     * OpenMap components held by the MapHandler. If the MapHandler contains a     * PropertyHandler, that property handler will be consulted for properties     * for different objects in case those objects don't know how to provide     * their settings correctly.     *      * @param mapHandler MapHandler containing components to use for Properties.     * @param ps PrintStream to write properties to, may be null if you just     *        want the Properties object that is returned.     * @return Properties object containing everything written (or that would     *         have been written, if the PrintStream is null) to PrintStream.     */    public static Properties createOpenMapProperties(MapHandler mapHandler,                                                     PrintStream ps) {        Properties createdProperties = new Properties();        // First, get all the components in the MapHandler. Create        // the openmap.components list, with the .class properties        // listing all the class names. Ignore the layers for now,        // and if the class is a PropertyConsumer, get its properties        // too.        if (mapHandler == null) {            Debug.error("PropertyHandler.createOpenMapProperties: can't create properties with null MapHandler");            return null;        }        Iterator it = mapHandler.iterator();        Object someObj;        Debug.message("properties",                "PropertyHandler: Looking for Objects in mapHandler");        MapBean mapBean = null;        LayerHandler layerHandler = null;        PropertyHandler propertyHandler = null;        InformationDelegator infoDelegator = null;        Vector otherComponents = new Vector();        while (it.hasNext()) {            someObj = it.next();            Debug.message("properties", "PropertyHandler found "                    + someObj.getClass().getName());            if (someObj instanceof MapBean) {                mapBean = (MapBean) someObj;            } else if (someObj instanceof LayerHandler) {                layerHandler = (LayerHandler) someObj;            } else if (someObj instanceof Layer || someObj instanceof PlugIn) {                // do nothing, layerhandler will handle            } else if (someObj instanceof PropertyHandler) {                propertyHandler = (PropertyHandler) someObj;                if (infoDelegator != null) {                    propertyHandler.addProgressListener(infoDelegator);                }            } else if (someObj instanceof InformationDelegator) {                infoDelegator = (InformationDelegator) someObj;                if (propertyHandler != null) {                    propertyHandler.addProgressListener((ProgressListener) someObj);                }            } else {                // Add the rest to a component vector thingy.                otherComponents.add(someObj);            }        }        // if the MapBean and/or the LayerHandler are null, what's the        // point?        if (mapBean == null || layerHandler == null) {            Debug.error("PropertyHandler: no MapBean(" + mapBean                    + ") or LayerHandler(" + layerHandler                    + ") to use to write properties");            return null;        }        // First, print the Map parameters...        ps.println("######  OpenMap properties file ######");        ps.println("## Refer to original openmap.properties file\n## for instructions on how to modify this file.");        ps.println("######################################");        printMapProperties(mapBean, ps, createdProperties);        printComponentProperties(otherComponents,                propertyHandler,                ps,                createdProperties);        printLayerProperties(layerHandler,                propertyHandler,                ps,                createdProperties);

⌨️ 快捷键说明

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