📄 uberproperties.java
字号:
String stValue = unescape(value.toString()); if (lastSeparator != null || stName.length() > 0 || stValue.length() > 0 ){ if (add || names.contains(stName)){ addProperty(stName, stValue); } else { setProperty(stName, stValue); names.add(stName); } if (foundComment) setComment(stName, unescape(comment.toString())); } comment.setLength(0); name.setLength(0); value.setLength(0); foundComment = false; lastSeparator = null; } } } /** * Add the properties from the input stream to this * UberProperties. * <p> * Properties that are found replace any properties that * were there before. * * @param in InputStream containing properties. * @throws IOException if an error occurs when reading. * * @since ostermillerutils 1.00.00 */ public void load(InputStream in) throws IOException { load(in, false); } /** * Save these properties from a user file. * <p> * Ex: * <pre>save( * new String(){"tld","company","package","component.properties"} * )</pre> * This will save the properties file relative to the user directory: * <%userhome%>/tld/company/package/component.properties * Directories will be created as needed. * * @param userFile array of Strings representing a path and file name relative to the user home directory. * @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 save(String[] userFile) throws IOException { int length = userFile.length; if (length == 0) throw new IllegalArgumentException(); File f = new File(System.getProperty("user.home")); for (int i=0; i<length; i++){ f = new File(f, userFile[i]); if (i == length - 2 && !f.exists()){ f.mkdirs(); } } OutputStream out = new FileOutputStream(f); save(out); out.close(); } /** * Save these properties to the given stream. * * @param out OutputStream to which these properties should be written. * @throws IOException if an error occurs when writing. * * @since ostermillerutils 1.00.00 */ public void save(OutputStream out) throws IOException { writeComment(out, comment); out.write('\n'); String[] names = propertyNames(); Arrays.sort(names); for (int i=0; i<names.length; i++){ writeComment(out, getComment(names[i])); String[] values = getProperties(names[i]); for (int j=0; j<values.length; j++){ writeProperty(out, names[i], values[j]); } } out.flush(); } private static void writeProperty(OutputStream out, String name, String value) throws IOException { writeEscapedISO88591(out, name, TYPE_NAME); out.write('='); writeEscapedISO88591(out, value, TYPE_VALUE); out.write('\n'); } private static void writeComment(OutputStream out, String comment) throws IOException { if (comment != null){ java.util.StringTokenizer tok = new java.util.StringTokenizer(comment, "\r\n"); while (tok.hasMoreTokens()){ out.write('#'); out.write(' '); writeEscapedISO88591(out, tok.nextToken(), TYPE_COMMENT); out.write('\n'); } } } private static final int TYPE_COMMENT = 0; private static final int TYPE_NAME = 1; private static final int TYPE_VALUE = 2; private static void writeEscapedISO88591(OutputStream out, String s, int type) throws IOException { for (int i=0; i<s.length(); i++){ int c = (int)s.charAt(i); if (c < 0x100){ boolean escape = false; if (c == '\r' || c == '\n' || c == '\\'){ escape = true; } else if (c == ' ' || c == '\t' || c == '\f'){ if(type == TYPE_NAME){ escape = true; } else if (type == TYPE_VALUE && (i==0 || i == s.length() - 1)){ escape = true; } } else if (type == TYPE_NAME && (c == '=' || c == ':')){ escape = true; } if (escape){ switch (c){ case '\n': { switch (type){ case TYPE_COMMENT: { out.write('\n'); out.write('#'); out.write(' '); } break; case TYPE_NAME: { out.write('\\'); out.write('n'); out.write('\\'); out.write('\n'); out.write('\t'); } break; case TYPE_VALUE: { out.write('\\'); out.write('n'); out.write('\\'); out.write('\n'); out.write('\t'); out.write('\t'); } break; } } break; case '\\': { out.write('\\'); out.write('\\'); } break; case '\r': { out.write('\\'); out.write('r'); } break; case '\t': { out.write('\\'); out.write('t'); } break; case '\f': { out.write('\\'); out.write('f'); } break; default : { out.write('\\'); out.write((byte)c); } break; } } else { out.write((byte)c); } } else { out.write('\\'); out.write('u'); out.write(StringHelper.prepad(Integer.toHexString(c), 4, '0').getBytes("ISO-8859-1")); } } } /** * Get the first property with the given name. * If the property is not specified in this UberProperties * but it is in the default UberProperties, the default is * used. If no default is found, null is returned. * * @return the first value of this property, or null if the property does not exist. * * @since ostermillerutils 1.00.00 */ public String getProperty(String name){ String value = null; if (properties.containsKey(name)){ value = ((Property)properties.get(name)).getValue(); } return value; } /** * Get the first property with the given name. * If the property is not specified in this UberProperties * but it is in the default UberProperties, the default * UberProperties is consulted, otherwise, the supplied * default is used. * * @return the first value of this property. * * @since ostermillerutils 1.00.00 */ public String getProperty(String name, String defaultValue){ String value = getProperty(name); if (value == null) value = defaultValue; return value; } /** * Get the values for a property. * Properties returned in the same order in which * they were added. * <p> * If the property is not specified in this UberProperties * but it is in the default UberProperties, the default is * used. If no default is found, null is returned. * * @return all the values associated with the given key, or null if the property does not exist. * * @since ostermillerutils 1.00.00 */ public String[] getProperties(String name){ String[] values = null; if (properties.containsKey(name)){ values = ((Property)properties.get(name)).getValues(); } return values; } /** * Get the values for a property. * Properties returned in the same order in which * they were added. * <p> * If the property is not specified in this UberProperties * but it is in the default UberProperties, the default * UberProperties is consulted, otherwise, the supplied * defaults are used. * * @return all the values associated with the given key, or null if the property does not exist. * * @since ostermillerutils 1.00.00 */ public String[] getProperties(String name, String[] defaultValues){ String[] values = getProperties(name); if (values == null) values = defaultValues; return values; } /** * Get the comment associated with this property. * <p> * If the property is not specified in this UberProperties * but it is in the default UberProperties, the default is * used. If no default is found, null is returned. * * @return the comment for this property, or null if there is no comment or the property does not exist. * * @since ostermillerutils 1.00.00 */ public String getComment(String name){ String comment = null; if (properties.containsKey(name)){ comment = ((Property)properties.get(name)).getComment(); } return comment; } /** * Returns an enumeration of all the keys in this property list, including * distinct keys in the default property list if a key of the same name has * not already been found from the main properties list. * * @return an enumeration of all the keys in this property list, including the keys in the default property list. * * @since ostermillerutils 1.00.00 */ public String[] propertyNames(){ Set names = properties.keySet(); return (String[])names.toArray(new String[names.size()]); } /** * Set the comment associated with this set of properties. * * @param comment the comment for entire set of properties, or null to clear. * * @since ostermillerutils 1.00.00 */ public void setComment(String comment){ this.comment = comment; } /** * Get the comment associated with this set of properties. * * @return comment for entire set of properties, or null if there is no comment. * * @since ostermillerutils 1.00.00 */ public String getComment(){ return this.comment; } /** * Get the number of unique names for properties stored * in this UberProperties. * * @return number of names. * * @since ostermillerutils 1.00.00 */ public int getPropertyNameCount(){ return properties.keySet().size(); } /** * Save these properties to a string. * * @return Serialized String version of these properties. * * @since ostermillerutils 1.02.23 */ public String toString(){ ByteArrayOutputStream out = new ByteArrayOutputStream(); try { this.save(out); } catch (IOException iox){ throw new Error("IO constructed on memory, this shouldn't happen.", iox); } String s = null; try { s = new String(out.toByteArray(), "ISO-8859-1"); } catch (UnsupportedEncodingException uee){ throw new Error("ISO-8859-1 should be recognized.", uee); } return s; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -