📄 proputils.java
字号:
* @param propName name of the property associated with the wanted value. * @param defaultValue what to return if the property name doesn't exist, or * if the value isn't a numerical value. * @return boolean value associated with the property. */ public static boolean booleanFromProperties(Properties p, String propName, boolean defaultValue) { boolean ret = defaultValue; String booleanString = p.getProperty(propName); if (booleanString != null) { ret = booleanString.trim().toLowerCase().equals("true"); } return ret; } /** * Creates an object out of a property name. If anything fails, return null. * * @param p properties * @param propName name of class to instantiate. * @return null on failure, otherwise, a default constructed instance of the * class named in the property. */ public static Object objectFromProperties(Properties p, String propName) { Object ret = null; String objectName = p.getProperty(propName); if (objectName != null) { ret = ComponentFactory.create(objectName); } return ret; } /** * Takes a string of representing token separated properties and returns an * array of parsed strings. NOTE: this method currently doesn't support * appropriate quoting of the token, although it probably should... * * @param p properties * @param propName the name of the property * @param tok the characters separating the strings. * @return Array of strings between the tokens. */ public static String[] stringArrayFromProperties(Properties p, String propName, String tok) { String[] ret = null; String raw = p.getProperty(propName); if (raw != null && raw.length() != 0) { try { StringTokenizer token = new StringTokenizer(raw, tok); int numPaths = token.countTokens(); ret = new String[numPaths]; for (int i = 0; i < numPaths; i++) { ret[i] = token.nextToken(); } return ret; } catch (java.util.NoSuchElementException e) { e.printStackTrace(); } } return ret; } /** * Gets a double out of a properties object. Returns the default value if * something goes wrong. * * @param p properties * @param propName name of the property associated with the wanted value. * @param defaultValue what to return if the property name doesn't exist, or * if the value isn't a numerical value. * @return double value associated with the property. */ public static double doubleFromProperties(Properties p, String propName, double defaultValue) { double ret = defaultValue; String doubleString = p.getProperty(propName); if (doubleString != null) { try { ret = Double.parseDouble(doubleString.trim()); } catch (NumberFormatException e) { ret = defaultValue; } } return ret; } /** * Gets a long out of a properties object. Returns the default value if * something goes wrong. * * @param p properties * @param propName name of the property associated with the wanted value. * @param defaultValue what to return if the property name doesn't exist, or * if the value isn't a numerical value. * @return long value associated with the property. */ public static long longFromProperties(Properties p, String propName, long defaultValue) { long ret = defaultValue; String longString = p.getProperty(propName); if (longString != null) { try { ret = Long.parseLong(longString.trim()); } catch (NumberFormatException e) { ret = defaultValue; } } return ret; } /** * Take a string from a properties file, representing the 24bit RGB or 32bit * ARGB hex values for a color, and convert it to a java.awt.Color. * * @param p properties * @param propName the name of the property * @param dfault color to use if the property value doesn't work * @return java.awt.Color * @exception NumberFormatException if the specified string cannot be * interpreted as a hexidecimal integer * @see ColorFactory#parseColorFromProperties(Properties, String, String, * boolean) */ public static Color parseColorFromProperties(Properties p, String propName, String dfault) throws NumberFormatException { return ColorFactory.parseColorFromProperties(p, propName, dfault, false); } /** * Take a string from a properties file, representing the 24bit RGB or 32bit * ARGB hex values for a color, and convert it to a java.awt.Color. * * @param p properties * @param propName the name of the property * @param dfault color to use if the property value doesn't work * @return java.awt.Color * @see ColorFactory#parseColorFromProperties(Properties, String, String, * boolean) */ public static Paint parseColorFromProperties(Properties p, String propName, Paint dfault) { return ColorFactory.parseColorFromProperties(p, propName, dfault); } /** * Convert a string representing a 24/32bit hex color value into a Color * value. NOTE: * <ul> * <li>Only 24bit (RGB) java.awt.Color is supported on the JDK 1.1 * platform. * <li>Both 24/32bit (ARGB) java.awt.Color is supported on the Java 2 * platform. * </ul> * * @param colorString the 24/32bit hex string value (ARGB) * @return java.awt.Color (24bit RGB on JDK 1.1, 24/32bit ARGB on JDK1.2) * @exception NumberFormatException if the specified string cannot be * interpreted as a hexidecimal integer * @see ColorFactory#parseColor(String, boolean) */ public static Color parseColor(String colorString) throws NumberFormatException { return ColorFactory.parseColor(colorString, false); } /** * Returns a string representing a color, properly buffered for zeros for * different alpha values. * * @param color * @return string for color with alpha values. */ public static String getProperty(Color color) { StringBuffer hexstring = new StringBuffer(Integer.toHexString(color.getRGB())); while (hexstring.length() < 8) { hexstring.insert(0, '0'); } return hexstring.toString(); } /** * Converts a properties object to an array of Strings. The resulting array * will consist of alternating key-value strings. * * @param props the properties object to convert. * @return an array of Strings representing key-value pairs. */ public static String[] getPropertiesAsStringArray(Properties props) { int size = props.size(); String[] ret = new String[size * 2]; // key and value int count = 0; Enumeration things = props.propertyNames(); while (things.hasMoreElements()) { ret[count] = (String) things.nextElement(); ret[count + 1] = (String) props.getProperty(ret[count]); count += 2; } return ret; } /** * Returns a URL that names either a resource, a local file, or an internet * URL. Resources are checked for in the general classpath. * * @param name name of the resource, file or URL. * @throws java.net.MalformedURLException * @return URL */ public static URL getResourceOrFileOrURL(String name) throws java.net.MalformedURLException { return getResourceOrFileOrURL(null, name); } /** * Returns a URL that names either a resource, a local file, or an internet * URL. * * @param askingClass the object asking for the URL. * @param name name of the resource, file or URL. * @throws java.net.MalformedURLException * @return URL */ public static URL getResourceOrFileOrURL(Object askingClass, String name) throws java.net.MalformedURLException { return getResourceOrFileOrURL(askingClass.getClass(), name); } /** * Returns a URL that names either a resource, a local file, or an internet * URL. * * @param askingClass the class asking for the URL. Can be null. * @param name name of the resource, file or URL. * @throws java.net.MalformedURLException * @return URL */ public static URL getResourceOrFileOrURL(Class askingClass, String name) throws java.net.MalformedURLException { boolean DEBUG = Debug.debugging("proputils"); if (name == null) { if (DEBUG) Debug.output("PropUtils.getROFOU(): null file name"); return null; } URL retval = null; if (DEBUG) Debug.output("PropUtils.getROFOU(): looking for " + name); if (askingClass != null) { // First see if we have a resource by that name if (DEBUG) Debug.output("PropUtils.getROFOU(): checking as resource"); retval = askingClass.getResource(name); } if (retval == null) { // Check the general classpath... if (DEBUG) Debug.output("PropUtils.getROFOU(): checking in general classpath"); retval = Thread.currentThread() .getContextClassLoader() .getResource(name); } if (retval == null && !Environment.isApplet()) { // Check the classpath plus the share directory, which may // be in the openmap.jar file or in the development // environment. if (DEBUG) Debug.output("PropUtils.getROFOU(): checking with ClassLoader"); retval = ClassLoader.getSystemResource("share/" + name); } if (retval == null && Environment.isApplet()) { if (DEBUG) Debug.output("PropUtils.getROFOU(): checking with URLClassLoader"); URL[] cba = new URL[1]; cba[0] = Environment.getApplet().getCodeBase(); URLClassLoader ucl = URLClassLoader.newInstance(cba); retval = ucl.getResource(name); } // If there was no resource by that name available if (retval == null) { if (DEBUG) Debug.output("PropUtils.getROFOU(): not found as resource"); try { java.io.File file = new java.io.File(name); if (file.exists()) { retval = file.toURL(); if (DEBUG) Debug.output("PropUtils.getROFOU(): found as file :)"); } else { // Otherwise treat it as a raw URL. if (DEBUG) Debug.output("PropUtils.getROFOU(): Not a file, checking as URL"); retval = new URL(name); java.io.InputStream is = retval.openStream(); is.close(); if (DEBUG) Debug.output("PropUtils.getROFOU(): OK as URL :)"); } } catch (java.io.IOException ioe) { retval = null; } catch (java.security.AccessControlException ace) { Debug.error("PropUtils: AccessControlException trying to access " + name); retval = null; } catch (Exception e) { Debug.error("PropUtils: caught exception " + e.getMessage()); retval = null; } } if (DEBUG) { if (retval != null) { Debug.output("Resource " + name + "=" + retval.toString()); } else { Debug.output("Resource " + name + " can't be found..."); } } return retval; } /** * Simple space saving implementation of common I18n Property Info setting. * * @param i18n i18n object to use to search for internationalized strings. * @param info the properties class being used to set information into. * @param classToSetFor class to use for i18n search. * @param propertyName property to set for. * @param label label to use for GUI (can be null if N/A). * @param tooltip tooltip to use for GUI (can be null if N/A). * @param editor editor class string to use for GUI (can be null if N/A). * @return Properties object passed in, or new one if null Properties passed * in. */ public static Properties setI18NPropertyInfo(I18n i18n, Properties info, Class classToSetFor, String propertyName, String label, String tooltip, String editor) { if (info == null) { info = new Properties(); } if (i18n != null) { if (tooltip != null) { String internString = i18n.get(classToSetFor, propertyName, I18n.TOOLTIP, tooltip); info.put(propertyName, internString); } if (label != null) { String internString = i18n.get(classToSetFor, propertyName, label); info.put(propertyName + PropertyConsumer.LabelEditorProperty, internString); } if (editor != null) { info.put(propertyName + PropertyConsumer.ScopedEditorProperty, editor); } } return info; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -