📄 property.java
字号:
* @param replaceVariables true: replace occurrences of ${key} with the value of key */ public Property(String fileName, boolean scanSystemProperties, java.util.Properties extraProps, boolean replaceVariables) throws XmlBlasterException { Vector buffer = new Vector(); Enumeration e = extraProps.propertyNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); buffer.add(new String("-" + name)); buffer.add(extraProps.getProperty(name)); } String[] arr = new String[buffer.size()]; buffer.copyInto(arr); init(fileName, scanSystemProperties, arr, replaceVariables, supportArrays); } /** * Construct a property container from supplied property file and enumeration. * <p /> * Usually used by servlets (ServletConfig conf):<br /> * <pre> * Properties extraProps = new Properties(); * Enumeration e = conf.getInitParameterNames(); * while (e.hasMoreElements()) { * String name = (String)e.nextElement(); * extraProps.put(name, conf.getInitParameter(name)); * } * </pre> * * @param fileName The property file name, e.g. "project.properties" * @param scanSystemProperties Scan System.getProperties() as well, you can * add variable to JVM like: java -DmyName=Joe ... * @param name of project which is to be extracted from user.home/projects.properties * @param replaceVariables true: replace occurrences of ${key} with the value of key */ public Property (String fileName, boolean scanSystemProperties, String projectname, boolean replaceVariables) throws XmlBlasterException { init(fileName, scanSystemProperties, (String[])null, replaceVariables, supportArrays); FileInputStream fis = null; String projectFileName = null; try { // Read user supplied properties file if (userHome != null) { projectFileName = userHome + "/projects.properties"; File file = new File (projectFileName); fis = new FileInputStream (file); } Properties myProps = new Properties(); myProps.load (fis); if (!myProps.getProperty (projectname, "").equals("")) { this.projectHomePath = myProps.getProperty (projectname); if (verbose>=1) System.out.println("Property: found " + projectname + " in " + projectFileName); } else { throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME + ".Error", "Unable to find " + projectname + " in " + projectFileName); } if (fis != null) fis.close(); } catch (IOException e) { throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME + ".Error", "Unable to find " + projectFileName + ": " + e); } } /** * Construct a property container from supplied property file and args array. * <p /> * @param fileName The property file name, e.g. "project.properties" * @param scanSystemProperties Scan System.getProperties() as well, you can * add variable to JVM like: java -DmyName=Joe ... * @param args A String array with properties, usually from command line, e.g. * java myApp -trace true -name Joe * @param replaceVariables true: replace occurrences of ${key} with the value of key * @param output false: no info output to System.out is printed. Only successfull operation. */ public Property(String fileName, boolean scanSystemProperties, String[] args, boolean replaceVariables, boolean output) throws XmlBlasterException { if (output) this.verbose = 2; init (fileName, scanSystemProperties, args, replaceVariables, supportArrays); } /** * Construct a property container from supplied property file (URL) and Applet. * <p /> * NOTE: Applet HTML PARAM values may not contain variables ${...}, they are currently not replaced. * * @param propertyFileUrl applet.getCodeBase()+"xy.properties" e.g. "http://myHost.com:80/xy.properties" * * @param scanSystemProperties Scan System.getProperties() as well, you can * add variable to JVM like: java -DmyName=Joe ...<br /> * NOTE: If you set this to true, you need a signed applet! * * @param applet Not yet supported. How to access all applet parameters??? * @param replaceVariables true: replace occurrences of ${key} with the value of key */ public Property(String propertyFileUrl, boolean scanSystemProperties, Applet applet, boolean replaceVariables) throws XmlBlasterException { System.out.println("Property for Applet: propertyFileUrl=" + propertyFileUrl + ", scanSystemProperties=" + scanSystemProperties + ", replaceVariables=" + replaceVariables); this.scanSystemProperties = scanSystemProperties; this.replaceVariables = replaceVariables; this.applet = applet; // Variant downloading it with a jar file using HTML ARCHIVE tag? // java.net.URL url; // url = this.getClass().getResource(propertyFileName); if (propertyFileUrl == null) { loadProps(null, null); if (properties == null) properties = dummyProperties; return; } URL url = null; FileInfo info = new FileInfo(propertyFileUrl); try { url = new URL(propertyFileUrl); this.propertyFileName = url.getFile(); info.fullPath = this.propertyFileName; try { info.in = url.openStream(); } catch (java.io.IOException e) { System.out.println("Property file " + url + " read error, continuing without it: " + e.toString()); //throw new XmlBlasterException(ME + ".Error", "URL access: " + e.toString()); } } catch (java.net.MalformedURLException e) { System.out.println("Property file " + propertyFileUrl + " is malformed, continuing without it: " + e.toString()); } loadProps(info, null); if (properties == null) properties = dummyProperties; } /** * We do a deep copy for all properties and listeners */ public Object clone() { try { Property p = (Property)super.clone(); p.properties = (Properties)this.properties.clone(); //p.dummyProperties = (Properties)this.dummyProperties.clone(); p.propMap = this.propMap != null ? (TreeMap)((TreeMap)this.propMap).clone():null; p.changeListenerMap = (HashMap)changeListenerMap.clone(); return p; } catch (CloneNotSupportedException e) { if (verbose>=1) System.err.println("Property clone failed: " + e.toString()); throw new RuntimeException("org.xmlBlaster.util.property.Property clone failed: " + e.toString()); } } /** * Internal Initialize. * @param argsProps The checked command line arguments */ private void init(String fileName_, boolean scanSystemProperties, String[] args, boolean replaceVariables, boolean supportArrays) throws XmlBlasterException { this.propertyFileName = fileName_; if (System.getProperty("property.verbose") != null) { try { verbose = Integer.parseInt(System.getProperty("property.verbose").trim()); } catch(NumberFormatException e) { System.err.println("-property.verbose expects a number"); } } Properties argsProps = argsToProps(args); if (argsProps != null && argsProps.get("---help") != null) { wantsHelp = true; } if (verbose>=2) { System.out.println( "Property: filenName=" + fileName_ + ", scanSystemProperties=" + scanSystemProperties + ", args=" + (argsProps == null ? "null" : "[" + argsProps.size() + "]") + ", replaceVariables=" + replaceVariables + ", supportArrays=" + supportArrays ); } this.propertyFileName = fileName_; this.scanSystemProperties = scanSystemProperties; this.replaceVariables = replaceVariables; this.supportArrays = supportArrays; separator = System.getProperty("file.separator"); userHome = System.getProperty("user.home"); this.currentPath = System.getProperty("user.dir"); this.projectHomePath = System.getProperty("PROJECT_HOME"); javaHomeExt = System.getProperty("java.ext.dirs"); javaHome = System.getProperty("java.home"); loadProps(findGivenFile(propertyFileName), argsProps); if (properties == null) properties = dummyProperties; } /** * Get the internal handle. * @return The Properties handle. */ public final java.util.Properties getProperties() { return properties; } public void setApplet(Applet applet) { this.applet = applet; if (applet.getParameter("--help")!=null || applet.getParameter("-?")!=null || applet.getParameter("-h")!=null || applet.getParameter("-help")!=null) wantsHelp = true; } private String get_(String key) { if (key == null) return null; if (applet != null) { String a = applet.getParameter(key); if (a != null) return a; } return (String)properties.get(key); } private Map getMap_(String key) { if (applet != null) { } if (propMap == null) return null; return (Map)propMap.get(key); } /** * Set or overwrite a property, * note that currently no variable replacement is implemented for the passed value. * @param key The key for this property * @param value The value for it * @return The value, ${...} variables are replaced */ public final String set(String key, String value) throws XmlBlasterException { String oldValue = get_(key); value = replaceVariable(key, value); properties.setProperty(key, value); fireChangeEvent(key, oldValue, value); if (supportArrays == true) scanArray(key, value); return value; } /** * Try to find the given key. * @param key the key to look for * @param defaultVal the default value to return if key is not found * @return The String value for the given key */ public final String get(String key, String defaultVal) { String str = get_(key); if (str == null) return defaultVal; return str; } /** * Try to find the given key. * <p /> * Example:<br /> * NameList=Josua,David,Ken,Abel<br /> * Will return each name separately in the array. * @param key the key to look for * @param defaultVal The default value to return if key is not found * @param separator The separator, typically "," * @return The String array for the given key, the elements are trimmed (no leading/following white spaces) */ public final String[] get(String key, String[] defaultVal, String separator) { String str = get_(key); if (str == null) return defaultVal; StringTokenizer st = new StringTokenizer(str, separator); int num = st.countTokens(); String[] arr = new String[num]; int ii=0; while (st.hasMoreTokens()) { arr[ii++] = st.nextToken().trim(); } return arr; } /** * Try to find the given key. * @param key the key to look for * @param defaultVal the default value to return if key is not found * @return The int value for the given key */ public final int get(String key, int defaultVal) { String str = get_(key); if (str == null) return defaultVal; try { return Integer.parseInt(str); } catch (Exception e) { return defaultVal; } } /** * Try to find the given key. * @param key the key to look for * @param defaultVal the default value to return if key is not found * @return The long value for the given key */ public final long get(String key, long defaultVal) { String str = get_(key); if (str == null) return defaultVal; try { return Long.parseLong(str); } catch (Exception e) { return defaultVal; } } /** * Try to find the given key. * @param key the key to look for * @param defaultVal the default value to return if key is not found * @return The double value for the given key */ public final double get(String key, double defaultVal) { String str = get_(key); if (str == null) return defaultVal; try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -