Index: lutinutil/src/java/org/codelutin/util/MyProperties.java diff -u lutinutil/src/java/org/codelutin/util/MyProperties.java:1.1 lutinutil/src/java/org/codelutin/util/MyProperties.java:1.2 --- lutinutil/src/java/org/codelutin/util/MyProperties.java:1.1 Sun Dec 9 20:01:42 2007 +++ lutinutil/src/java/org/codelutin/util/MyProperties.java Mon Dec 10 02:35:41 2007 @@ -26,6 +26,7 @@ import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.Reader; import java.util.ArrayList; import java.util.Collections; @@ -227,10 +228,20 @@ if (tmp != null) { tmp.clear(); } + clearUnsafeData(); + keys.clear(); + } + + /** + * clear the instance unsafeData. + *
+ * After the method invocation, the {@link #isSafe()}'s method's return is + * alwaystrue.
+ */
+ public void clearUnsafeData() {
if (unsafeData != null) {
unsafeData.clear();
}
- keys.clear();
}
/**
@@ -311,6 +322,53 @@
return this;
}
+ /**
+ * TODO We should not save unsafe data, so perform a clearUnsafeData before all.
+ * Save current configuration in a simple properties file.
+ *
+ * Each valid property is saved using the his key given by the method
+ * {@link MyPropertyKey#getKey()} for his entry.
+ *
+ * Unsafe data are stored as they are.
+ *
+ * @param out where to store
+ * @param comments comment to add in file
+ * @throws IOException if any problem while saving
+ */
+ public void store(OutputStream out, String comments) throws IOException {
+ getTmp().clear();
+ try {
+ if (!isEmpty()) {
+ for (T key : getKeys()) {
+ MyPropertyKey entry = getManager().getEntry((MyPropertyDef) key, false);
+ Object val = data.get(key);
+ tmp.put(entry.getKey(), val == null ? "" : val.toString());
+ }
+ }
+ if (!isSafe()) {
+ for (Object key : getUnsafeDataKeys()) {
+ Object val = unsafeData.get(key);
+ tmp.put(key, val == null ? "" : val.toString());
+ }
+ }
+ tmp.store(out, comments);
+ } finally {
+ tmp.clear();
+ }
+ }
+
+ public Properties toProperties() {
+ Properties result = new Properties();
+ if (!isEmpty()) {
+ for (T key : getKeys()) {
+ MyPropertyKey entry = getManager().getEntry((MyPropertyDef) key, false);
+ Object val = data.get(key);
+ result.put(entry.getKey(), val == null ? "" : val.toString());
+ }
+ }
+ return result;
+ }
+
@Override
public String toString() {
StringBuilder s = new StringBuilder(super.toString()).append("