for (Iterator i = values.entrySet().iterator(); i.hasNext();) {
Map.Entry defn = (Map.Entry) i.next();
String localName = (String) defn.getKey();
Object valueObj = defn.getValue();
String dataName = createDataName(dataPrefix, localName);
SaveableData o = instantiateValue(dataName, dataPrefix, valueObj,
!fileEditable);
if (o instanceof MalformedData)
logger.warning("Data value for '" + dataName + "' in file '"
+ datafilePath + "' is malformed.");
DataElement d = (DataElement)data.get(dataName);
if (d == null) {
boolean isDefaultName = defaultData.containsKey(localName);
if (o != null || isDefaultName) {
try {
add(dataName, isDefaultName, o, IS_NOT_DEFAULT_VAL,
dataFile, DO_NOTIFY);
} catch (DataElementAlreadyExistsException e) {
d = e.elem;
}
}
}
if (d != null) {
putValue(dataName, o, IS_NOT_DEFAULT_VAL,
NOT_MODIFYING_DATAFILE);
d = (DataElement)data.get(dataName);
if (d != null) {
d.datafile = dataFile;
d.isDefaultName = defaultData.containsKey(localName);
}
}
if (registerDataNames && (o instanceof DoubleData
|| o instanceof CompiledFunction))
dataElementNameSet.add(localName);
}
// Next, handle the default values that this datafile inherits.
String dataPrefixSlash = dataPrefix + "/";
for (Iterator i = defaultData.entrySet().iterator(); i.hasNext();) {
Map.Entry defn = (Map.Entry) i.next();
String localName = (String) defn.getKey();
Object valueObj = defn.getValue();
// if we already processed an explicit value with this same
// name, do nothing.
if (values.containsKey(localName))
continue;
// Ignore renaming operations; they are not relevant.
if (valueObj instanceof DataRenamingOperation)
continue;
boolean shouldCreateEagerly = shouldCreateEagerly(valueObj);
if ("@now".equals(valueObj))
shouldCreateEagerly = datafileModified = true;
String dataName = dataPrefixSlash + localName;
DataElement d = (DataElement)data.get(dataName);
if (d == null) {
// this data element does not already exist in the repository.
// most such items do not need to be created; we can let them
// be lazily created later if needed.
if (shouldCreateEagerly) {
// the item doesn't exist, but we should create it anyway.
SaveableData o = instantiateValue(dataName,
dataPrefix, valueObj, !fileEditable);
if (o != null) {
try {
add(dataName, IS_DEFAULT_NAME, o, IS_DEFAULT_VAL,
dataFile, DO_NOT_NOTIFY);
} catch (DataElementAlreadyExistsException e) {
d = e.elem;
}
}
}
}
if (d != null) {
// a matching data element exists.
d.datafile = dataFile;
d.isDefaultName = true;
dataName = d.name;
if (instantiatedDataMatches(valueObj, d.getValue())) {
// the data element already has the proper value. (This
// will be common, as clients registering data listeners
// will cause lazy data to spring to life even as this
// for loop is executing.) Nothing needs to be done.
} else if (!d.hasListeners()) {
// This element has the wrong value, but no one is
// listening to it. Revert the element to a lazy
// default value.
janitor.cleanup(d);
} else {
// This element has the wrong value, and clients are
// listening to it. Create and save the correct value.
SaveableData o = instantiateValue(dataName,
dataPrefix, valueObj, !fileEditable);
putValue(dataName, o, IS_DEFAULT_VAL,
NOT_MODIFYING_DATAFILE);
}
}