📄 uberproperties.java
字号:
* @since ostermillerutils 1.00.00 */ public void setProperty(String name, String value){ if (name == null) throw new NullPointerException(); if (value == null){ properties.remove(name); } else { Property property; if (properties.containsKey(name)){ property = (Property)properties.get(name); property.set(value); } else { property = new Property(value); properties.put(name, property); } } } /** * Replaces all properties of the given name with * properties with the given values. * * @param name the name of the property. * @param values for the property. * @throws NullPointerException if name is null. * @throws NullPointerException if values is null. * @throws IllegalArgumentException if values is empty. * * @since ostermillerutils 1.00.00 */ public void setProperties(String name, String[] values){ if (name == null) throw new NullPointerException(); if (values.length == 0) throw new IllegalArgumentException(); Property property; if (properties.containsKey(name)){ property = (Property)properties.get(name); property.set(values); } else { property = new Property(values); properties.put(name, property); } } /** * Replaces all properties of the given name with * a single property with the given value. * * @param name the name of the property. * @param value the value of the property or null to remove it. * @param comment the comment for the property, or null to remove it. * @throws NullPointerException if name is null. * @throws NullPointerException if comment is null. * * @since ostermillerutils 1.00.00 */ public void setProperty(String name, String value, String comment){ if (name == null) throw new NullPointerException(); if (value == null){ properties.remove(name); } else { setProperty(name, value); setComment(name, comment); } } /** * Replaces all properties of the given name with * properties with the given values. * * @param name the name of the property. * @param values value of the property. * @param comment the comment for the property, or null to remove it. * @throws NullPointerException if name is null. * @throws NullPointerException if values is null. * @throws IllegalArgumentException if values is empty. * * @since ostermillerutils 1.00.00 */ public void setProperties(String name, String[] values, String comment){ if (name == null) throw new NullPointerException(); if (values.length == 0) throw new IllegalArgumentException(); setProperties(name, values); setComment(name, comment); } /** * Set the comment on the property of the given name. * The property must exist before this method is called. * * @param name the name of the property. * @param comment the comment for the property. * @param comment the comment for the property, or null to remove it. * @throws NullPointerException if name is null. * @throws IllegalArgumentException if name is not a known key. * * @since ostermillerutils 1.00.00 */ private void setComment(String name, String comment){ if (name == null) throw new NullPointerException(); if (!properties.containsKey(name)) throw new IllegalArgumentException(); ((Property)properties.get(name)).setComment(comment); } /** * Adds a value to the list of properties with the * given name. * * @param name the name of the property. * @param value the values for the property, or null to remove. * @param comment the comment for the property, or null to remove it. * @throws NullPointerException if name is null. * @throws NullPointerException if value is null. * * @since ostermillerutils 1.00.00 */ public void addProperty(String name, String value, String comment){ if (name == null) throw new NullPointerException(); if (value == null) throw new NullPointerException(); addProperty(name, value); setComment(name, comment); } /** * Adds the values to the list of properties with the * given name. * * @param name the name of the property. * @param values the values for the property. * @param comment the comment for the property, or null to remove it. * @throws NullPointerException if name is null. * @throws NullPointerException if values is null. * * @since ostermillerutils 1.00.00 */ public void addProperties(String name, String[] values, String comment){ if (name == null) throw new NullPointerException(); if (values == null) throw new NullPointerException(); addProperties(name, values); setComment(name, comment); } /** * Adds a value to the list of properties with the * given name. * * @param name the name of the property. * @param value the values for the property. * @throws NullPointerException if name is null. * @throws NullPointerException if value is null. * * @since ostermillerutils 1.00.00 */ public void addProperty(String name, String value){ if (name == null) throw new NullPointerException(); if (value == null) throw new NullPointerException(); Property property; if (properties.containsKey(name)){ property = (Property)properties.get(name); property.add(value); } else { property = new Property(value); properties.put(name, property); } } /** * Adds the values to the list of properties with the * given name. * * @param name the name of the property. * @param values the values for the property. * @throws NullPointerException if name is null. * @throws NullPointerException if values is null. * * @since ostermillerutils 1.00.00 */ public void addProperties(String name, String[] values){ if (name == null) throw new NullPointerException(); if (values == null) throw new NullPointerException(); Property property; if (properties.containsKey(name)){ property = (Property)properties.get(name); property.add(values); } else { property = new Property(values); properties.put(name, property); } } private static int hexDigitValue(char c){ switch (c){ case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case 'a': case 'A': return 10; case 'b': case 'B': return 11; case 'c': case 'C': return 12; case 'd': case 'D': return 13; case 'e': case 'E': return 14; case 'f': case 'F': return 15; default: return -1; } } private static String unescape(String s){ StringBuffer sb = new StringBuffer(s.length()); for(int i=0; i<s.length(); i++){ char c = s.charAt(i); if (c == '\\'){ i++; if (i < s.length()){ c = s.charAt(i); switch (c){ case 'n': { sb.append('\n'); } break; case 'r': { sb.append('\r'); } break; case 't': { sb.append('\t'); } break; case 'f': { sb.append('\f'); } break; case 'u': { boolean foundUnicode = false; if (i+4 < s.length()){ int unicodeValue = 0; for (int j = 3; unicodeValue >= 0 && j >= 0; j--){ int val = hexDigitValue(s.charAt(i+(4-j))); if (val == -1){ unicodeValue = -1; } else { unicodeValue |= (val << (j << 2)); } } if (unicodeValue >= 0) { i+=4; foundUnicode = true; sb.append((char)unicodeValue); } } if (!foundUnicode) sb.append(c); } break; default: { sb.append(c); } break; } } } else { sb.append(c); } } return sb.toString(); } /** * Load these properties from a user file with default properties * from a system resource. * <p> * Ex: * <pre>load( * new String(){".java","tld","company","package","component.properties"} * "tld/company/package/component.properties", * )</pre> * This will load the properties file relative to the classpath as the * defaults and the file <%userhome%>/.java/tld/company/package/component.properties * if the file exists. The .java directory is recommended as it is a common, * possibly hidden, directory in the users home directory commonly used by * Java programs. * * This method is meant to be used with the save(String systemResource) method * which will save modified properties back to the user directory. * * @param userFile array of Strings representing a path and file name relative to the user home directory. * @param systemResource name relative to classpath of default properties, or null to ignore. * @throws IOException if an error occurs when reading. * @throws NullPointerException if userFile is null. * @throws IllegalArgumentException if userFile is empty. * * @since ostermillerutils 1.00.00 */ public void load(String[] userFile, String systemResource) throws IOException { int length = userFile.length; if (userFile.length == 0) throw new IllegalArgumentException(); InputStream in = ClassLoader.getSystemResourceAsStream(systemResource); if (in==null) throw new FileNotFoundException(systemResource); if (systemResource != null) load(in); File f = new File(System.getProperty("user.home")); for (int i=0; f.exists() && i<length; i++){ f = new File(f, userFile[i]); } if (f.exists()) load(new FileInputStream(f)); } /** * Add the properties from the input stream to this * UberProperties. * * @param in InputStream containing properties. * @param add whether parameters should add to parameters with the same name or replace them. * @throws IOException if an error occurs when reading. * * @since ostermillerutils 1.00.00 */ public void load(InputStream in, boolean add) throws IOException { PropertiesLexer lex = new PropertiesLexer(new InputStreamReader(in, "ISO-8859-1")); PropertiesToken t; HashSet names = new HashSet(); StringBuffer comment = new StringBuffer(); boolean foundComment = false; StringBuffer name = new StringBuffer(); StringBuffer value = new StringBuffer(); int last = TYPE_COMMENT; boolean atStart = true; String lastSeparator = null; while ((t = lex.getNextToken()) != null){ if (t.getID() == PropertiesToken.COMMENT){ int start = 1; String commentText = t.getContents(); if (commentText.startsWith("# ")) start = 2; comment.append(commentText.substring(start, commentText.length())); comment.append("\n"); lex.getNextToken(); foundComment = true; } else if (t.getID() == PropertiesToken.NAME){ if (atStart){ setComment(comment.toString()); comment.setLength(0); atStart = false; } name.append(t.getContents()); } else if (t.getID() == PropertiesToken.VALUE){ if (atStart){ setComment(comment.toString()); comment.setLength(0); atStart = false; } value.append(t.getContents()); } else if (t.getID() == PropertiesToken.SEPARATOR){ lastSeparator = t.getContents(); } else if (t.getID() == PropertiesToken.END_LINE_WHITE_SPACE){ if (atStart){ setComment(comment.toString()); comment.setLength(0); atStart = false; } String stName = unescape(name.toString());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -