valueIsAcceptable
* method. Note that in this case, correct behavior depends on a previous
* successful call to valueIsAcceptable so that the value read
* from the file may be stored in the name-to-value hash and used in place
* of the filename here.
*
* @param valueString
* The string representation of the value to add to this
* argument.
*/
@Override
public void addValue(final String valueString) {
final String actualValue = namesToValues.get(valueString);
if (actualValue != null) {
super.addValue(actualValue);
}
}
/**
* Retrieves a map between the filenames specified on the command line and
* the first lines read from those files.
*
* The returned map is the live one: the tools building an equivalent command line add
* entries to it, so that a password read interactively is displayed as a password file.
*
* @return A map between the filenames specified on the command line and the
* first lines read from those files.
*/
public Maptrue if the value is acceptable, or
* false if it is not.
*/
@Override
public boolean valueIsAcceptable(final String valueString,
final LocalizableMessageBuilder invalidReason) {
// First, make sure that the specified file exists.
File valueFile;
try {
valueFile = new File(valueString);
if (!valueFile.exists()) {
invalidReason.append(ERR_FILEARG_NO_SUCH_FILE.get(valueString, longIdentifier));
return false;
}
} catch (final Exception e) {
invalidReason.append(ERR_FILEARG_CANNOT_VERIFY_FILE_EXISTENCE.get(
valueString, longIdentifier, getExceptionMessage(e)));
return false;
}
// Open the file, read the first line and close the file.
String line;
try (BufferedReader reader = new BufferedReader(new FileReader(valueFile))) {
line = reader.readLine();
} catch (final FileNotFoundException e) {
invalidReason.append(ERR_FILEARG_CANNOT_OPEN_FILE.get(valueString, longIdentifier, getExceptionMessage(e)));
return false;
} catch (final IOException e) {
invalidReason.append(ERR_FILEARG_CANNOT_READ_FILE.get(valueString, longIdentifier, getExceptionMessage(e)));
return false;
}
// If the line read is null, then that means the file was empty.
if (line == null) {
invalidReason.append(ERR_FILEARG_EMPTY_FILE.get(valueString, longIdentifier));
return false;
}
// Store the value in the hash so it will be available for addValue.
// We won't do any validation on the value itself, so anything that we
// read will be considered acceptable.
namesToValues.put(valueString, line);
return true;
}
}