📄 uberproperties.java
字号:
/* * A souped up version of the Java Properties format which can * handle multiple properties with the same name. * Copyright (C) 2002-2003 Stephen Ostermiller * http://ostermiller.org/contact.pl?regarding=Java+Utilities * * Copyright (C) 2003 Carlo Magnaghi <software at tecnosoft dot net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * See COPYING.TXT for details. */package com.Ostermiller.util;import java.io.IOException;import java.io.InputStream;import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.FileOutputStream;import java.io.FileNotFoundException;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Set;import java.util.Arrays;import java.io.ByteArrayOutputStream;import java.io.UnsupportedEncodingException;/** * The Properties class represents a persistent set of properties. The * Properties can be saved to a stream or loaded from a stream. Each key and * its corresponding value in the property list is a string. * More information about this class is available from <a target="_top" href= * "http://ostermiller.org/utils/UberProperties.html">ostermiller.org</a>. * <p> * A property list can contain another property list as its "defaults"; this * second property list is searched if the property key is not found in the * original property list. * <p> * When saving properties to a stream or loading them from a stream, the ISO * 8859-1 character encoding is used. For characters that cannot be directly * represented in this encoding, Unicode escapes are used; however, only a * single 'u' character is allowed in an escape sequence. The native2ascii tool * can be used to convert property files to and from other character encodings. * <p> * Unlike the java.util.Properties, UberProperties does not inherit from * java.util.Hashtable, so Objects other than strings cannot be stored in it. * Also, comments from a files are preserved, and there can be several * properties for a given name. * <p> * This class is not synchronized, so it should not be used in a * multi-threaded environment without external synchronization. * <p> * The file format that UberProperties uses is as follows: * <blockquote> * The file is assumed to be using the ISO 8859-1 character encoding. All of the * comment lines (starting with a '#' or '!') at the beginning of the file before the * first line that is not a comment, are the comment associated with the file. * After that, each comment will be associated with the next property. If there * is more than one property with the same name, the first comment will be the * only one that is loaded. * <p> * Every property occupies one line of the input stream. Each line is terminated * by a line terminator (\n or \r or \r\n). * <p> * A line that contains only whitespace or whose first non-whitespace character * is an ASCII # or ! is ignored (thus, # or ! indicate comment lines). * <p> * Every line other than a blank line or a comment line describes one property * to be added to the table (except that if a line ends with \, then the * following line, if it exists, is treated as a continuation line, * as described below). The key consists of all the characters in the line * starting with the first non-whitespace character and up to, but not * including, the first ASCII =, :, or whitespace character. All of the key * termination characters may be included in the key by preceding them with a \. * Any whitespace after the key is skipped; if the first non-whitespace * character after the key is = or :, then it is ignored and any whitespace * characters after it are also skipped. All remaining characters on the line * become part of the associated element string. Within the element string, the * ASCII escape sequences \t, \n, \r, \\, \", \', \ (a backslash and a space), * and \\uxxxx are recognized and converted to single characters. Moreover, if * the last character on the line is \, then the next line is treated as a * continuation of the current line; the \ and line terminator are simply * discarded, and any leading whitespace characters on the continuation line are * also discarded and are not part of the element string. * <p> * As an example, each of the following four lines specifies the key "Truth" * and the associated element value "Beauty":<br> * <pre>Truth = Beauty * Truth:Beauty * Truth :Beauty</pre> * <p> * As another example, the following three lines specify a single property:<br> * <pre>fruits apple, banana, pear, \ * cantaloupe, watermelon, \ * kiwi, mango</pre> * <p> * The key is "fruits" and the associated element is:<br> * "apple, banana, pear, cantaloupe, watermelon, kiwi, mango"<br> * Note that a space appears before each \ so that a space will appear after * each comma in the final result; the \, line terminator, and leading * whitespace on the continuation line are merely discarded and are not replaced * by one or more other characters. * <p> * As a third example, the line:<br> * cheeses<br> * specifies that the key is "cheeses" and the associated element is the empty * string. * </blockquote> * * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities * @since ostermillerutils 1.00.00 */public class UberProperties { /** * A hash map that contains all the properties. * This should never be null, but may be empty. * This should hold objects of type Property. * * @since ostermillerutils 1.00.00 */ private HashMap properties = new HashMap(); /** * Comment for this set of properties. * This may be either null or empty. * * @since ostermillerutils 1.00.00 */ private String comment = null; /** * The object type that goes in the HashMap. * * @since ostermillerutils 1.00.00 */ private class Property { /** * List of values for this property. * This should never be null or empty. * * @since ostermillerutils 1.00.00 */ private ArrayList list; /** * Comment for this set of properties. * This may be either null or empty. * * @since ostermillerutils 1.00.00 */ private String comment = null; /** * Set the comment associated with this property. * * @param comment the comment for this property, or null to clear. * * @since ostermillerutils 1.00.00 */ public void setComment(String comment){ this.comment = comment; } /** * Get the comment associated with this property. * * @return comment for this property, or null if none is set. * * @since ostermillerutils 1.00.00 */ public String getComment(){ return this.comment; } /** * Construct a new property with the given value. * * @param value initial value for this property. * * @since ostermillerutils 1.00.00 */ public Property(String value){ list = new ArrayList(1); add(value); } /** * Construct a new property with the given values. * * @param values initial values for this property. * * @since ostermillerutils 1.00.00 */ public Property(String[] values){ list = new ArrayList(values.length); add(values); } /** * Set this property to have this single value. * * @param value lone value for this property. * * @since ostermillerutils 1.00.00 */ public void set(String value){ list.clear(); add(value); } /** * Set this property to have only these values. * * @param values lone values for this property. * * @since ostermillerutils 1.00.00 */ public void set(String[] values){ list.clear(); add(values); } /** * Add this value to the list of values for this property. * * @param value another value for this property. * * @since ostermillerutils 1.00.00 */ public void add(String value){ list.add(value); } /** * Add these values to the list of values for this property. * * @param values other values for this property. * * @since ostermillerutils 1.00.00 */ public void add(String[] values){ list.ensureCapacity(list.size() + values.length); for (int i=0; i<values.length; i++){ add(values[i]); } } /** * Get the last value for this property. * * @return the last value. * * @since ostermillerutils 1.00.00 */ public String getValue(){ return (String)list.get(list.size() - 1); } /** * Get all the values for this property. * * @return a list of all the values. * * @since ostermillerutils 1.00.00 */ public String[] getValues(){ return (String[])list.toArray(new String[list.size()]); } } /** * Creates an empty property list with no default values. * * @since ostermillerutils 1.00.00 */ public UberProperties(){ } /** * Creates an empty property list with the specified defaults. * * @param defaults the defaults. * @throws NullPointerException if defaults is null. * * @since ostermillerutils 1.00.00 */ public UberProperties(UberProperties defaults){ merge(defaults); } /** * Put all the properties from the defaults in this. * Calling this from a constructor will clone (deep) * the default properties. * * @since ostermillerutils 1.00.00 */ private void merge(UberProperties defaults){ setComment(defaults.getComment()); String[] names = defaults.propertyNames(); for (int i=0; i<names.length; i++){ setProperties(names[i], defaults.getProperties(names[i])); setComment(names[i], defaults.getComment(names[i])); } } /** * Test to see if a property with the given name exists. * * @param name the name of the property. * @return true if the property existed and was removed, false if it did not exist. * @throws NullPointerException if name is null. * * @since ostermillerutils 1.00.00 */ public boolean contains(String name){ if (name == null) throw new NullPointerException(); return properties.containsKey(name); } /** * Remove any property with the given name. * * @param name the name of the property. * @return true if the property existed and was removed, false if it did not exist. * @throws NullPointerException if name is null. * * @since ostermillerutils 1.00.00 */ public boolean remove(String name){ if (!contains(name)) return false; properties.remove(name); return true; } /** * 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. * @throws NullPointerException if name is null. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -