📄 propertyhandler.java
字号:
// config dir properties. merge(systemProperties, "system properties", "system"); // in user's home directory, most precedence. String userHomeDirectory = systemProperties.getProperty("user.home"); if (showDebugMessages) { Debug.output("PropertyHandler: Looking for " + propsFileName + " in user's home directory: " + userHomeDirectory); } // We want foundProperties to reflect if properties have ever // been found. foundProperties |= PropUtils.loadProperties(tmpProperties, userHomeDirectory, propsFileName); if (showDebugMessages) { Debug.output("***** Done with property search ****"); } if (!foundProperties && !Environment.isApplet()) { PropUtils.copyProperties(PropUtils.promptUserForProperties(), properties); } // Before we the user properties into the overall properties, // need to check for the include properties URLs, and load // those first. includeProperties = getIncludeProperties(tmpProperties.getProperty(includeProperty), tmpProperties); merge(includeProperties, "include file properties", userHomeDirectory); // Now, load the user home preferences last, since they take // the highest precedence. merge(tmpProperties, propsFileName, userHomeDirectory); // Well, they used to take the highest precedence. Now, we // look for a localized property file, and write those // properties on top. localizedProperties = getLocalizedProperties(tmpProperties.getProperty(localizedProperty), userHomeDirectory); merge(localizedProperties, "localized properties", null); Environment.init(getProperties()); } /** * Load the localized properties that will take precidence over * all other properties. If the localizedPropertyFile is null, a * localized version of the openmap.properties file will be * searched for in the classpath and in the user home directory * (if that isn't null as well). */ protected Properties getLocalizedProperties(String localizedPropertyFile, String userHomeDirectory) { Properties props = null; if (localizedPropertyFile == null) { java.util.Locale loc = java.util.Locale.getDefault(); localizedPropertyFile = "openmap_" + loc.toString() + ".properties"; } boolean tryHomeDirectory = false; if (DEBUG) { Debug.output("PropertyHandler: Looking for localized file: " + localizedPropertyFile); } try { URL propsURL = PropUtils.getResourceOrFileOrURL(localizedPropertyFile); if (propsURL == null) { tryHomeDirectory = true; } else { if (DEBUG) { Debug.output("Found localized properties in classpath"); } props = fetchProperties(propsURL); } } catch (MalformedURLException murle) { Debug.error("PropertyHandler can't find localized property file: " + localizedPropertyFile); tryHomeDirectory = true; } if (tryHomeDirectory) { props = new Properties(); if (!PropUtils.loadProperties(props, userHomeDirectory, localizedPropertyFile)) { props = null; } else { if (DEBUG) { Debug.output("Found localized properties in home directory"); } } } if (props == null) { props = new Properties(); } return props; } /** * Initialize internal properties from Properties object. Appends * all the properties it finds, overwriting the ones with the same * key. Called by the two constructors where a Properties object * is passed in, or when a URL for a Properties file is provided. * This is not called by the consstructor that has to go looking * for the properties to use. * * @param props the properties to merge into the properties held * by the PropertyHandler. * @param howString a string describing where the properties come * from. Just used for debugging purposes, so passing in a * null value is no big deal. * @return the properties contained in this PropertyHandler. */ protected void init(Properties props, String howString) { // Include properties noted in resources properties. Properties includeProperties = getIncludeProperties(props.getProperty(includeProperty), props); merge(includeProperties, "include file properties", howString); if (!Environment.isApplet()) { Properties systemProperties = System.getProperties(); merge(systemProperties, props); } merge(props, "loaded", howString); if (DEBUG) { Debug.output("PropertyHandler: loaded properties"); } } /** * Take a marker name list (space separated names), and open the * properties files listed in the propertu with keys of * marker.URL. * * @param markerList space separated marker names in a single * string that needs to be parsed. * @param props the properties that the markerList comes from, in * order to get the marker.URL properties. * @return an allocated Properties object containing all the * properties from the inlude files. If no include files * are listed, the Properties object is empty, not null. */ protected Properties getIncludeProperties(String markerList, Properties props) { Properties newProps = new Properties(); Properties tmpProps = new Properties(); Vector includes = PropUtils.parseSpacedMarkers(markerList); int size = includes.size(); if (size > 0) { if (Debug.debugging("propertiesdetail")) { Debug.output("PropertyHandler: handling include files: " + includes); } for (int i = 0; i < size; i++) { String includeName = (String) includes.elementAt(i); String includeProperty = includeName + ".URL"; String include = props.getProperty(includeProperty); if (include == null) { Debug.error("PropertyHandler.getIncludeProperties(): Failed to locate include file \"" + includeName + "\" with URL \"" + includeProperty + "\"\n Skipping include file \"" + include + "\""); continue; } try { tmpProps.clear(); // Open URL to read in properties URL tmpInclude = PropUtils.getResourceOrFileOrURL(null, include); if (tmpInclude == null) { continue; } InputStream is = tmpInclude.openStream(); tmpProps.load(is); if (DEBUG) { Debug.output("PropertyHandler.getIncludeProperties(): located include properties file URL: " + include); } // Include properties noted in resources // properties - a little recursive action, // here. Properties includeProperties = getIncludeProperties(tmpProps.getProperty(includeProperty), tmpProps); merge(includeProperties, newProps, "include file properties", "within " + include); merge(tmpProps, newProps, "include file properties", include); } catch (MalformedURLException e) { Debug.error("PropertyHandler: malformed URL for include file: |" + include + "| for " + includeName); } catch (IOException ioe) { Debug.error("PropertyHandler: IOException processing " + include + "| for " + includeName); } } } else { Debug.message("properties", "PropertyHandler.getIncludeProperties(): no include files found."); } return newProps; } /** * Take the from properties, copy them into the internal * PropertyHandler properties. * * @param from the source properties. */ protected void merge(Properties from) { merge(from, getProperties(), null, null); } /** * Take the from properties, copy them into the to properties. * * @param from the source properties. * @param to the destination properties. */ protected void merge(Properties from, Properties to) { merge(from, to, null, null); } /** * 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; try { plg = new ProgressListenerGauge("OpenMap Setup"); 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.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -