public Object toInstance(InputStream stream, Class clazz) throws IOException {
return toInstanceSimpleFormat(stream);
}
private CommandModel toInstanceSimpleFormat(InputStream stream) throws IOException {
CachedCommandModel result = null;
InputStreamReader isr = null;
BufferedReader r = null;
boolean inParam = false;
String name = null;
String eTag = null;
boolean unknownAreOperands = false;
String usage = null;
boolean addedUploadOption = false;
String pName = null;
Class pCls = null;
boolean pOptional = false;
String pDefaultValue = null;
String pShortName = null;
boolean pObsolete = false;
String pAlias = null;
boolean pPrimary = false;
boolean pMultiple = false;
boolean pPassword = false;
String pPrompt = null;
String pPromptAgain = null;
try {
isr = new InputStreamReader(stream, charset);
r = new BufferedReader(isr);
String line;
while ((line = r.readLine()) != null) {
int ind = line.indexOf(':');
if (ind <= 0) {
continue;
}
String key = line.substring(0, ind);
String value = line.substring(ind + 1).trim();
// @todo Java SE 7: String switch-case
if (inParam) {
if (NAME_ELEMENT.equals(key)) {
//Add before parameter
CommandModelData.ParamModelData pmd =
new CommandModelData.ParamModelData(pName,
pCls, pOptional, pDefaultValue, pShortName,
pObsolete, pAlias);
pmd.param._primary = pPrimary;
pmd.param._multiple = pMultiple;
pmd.param._password = pPassword;
pmd.prompt = pPrompt;
pmd.promptAgain = pPromptAgain;
result.add(pmd);
//Reset values
pCls = null;
pOptional = false;
pDefaultValue = null;
pShortName = null;
pObsolete = false;
pAlias = null;
pPrimary = false;
pMultiple = false;
pPassword = false;
pPrompt = null;
pPromptAgain = null;
//New param
pName = value;
} else if (CLASS_ELEMENT.equals(key)) {
if (!value.isEmpty()) {
try {
pCls = Class.forName(value);
} catch (Exception ex) {
}
}
} else if (OPTIONAL_ELEMENT.equals(key)) {
pOptional = value.startsWith("t");
} else if (DEFAULT_VALUE_ELEMENT.equals(key)) {
pDefaultValue = value;
} else if (SHORTNAME_ELEMENT.equals(key)) {
pShortName = value;
} else if (OBSOLETE_ELEMENT.equals(key)) {
pObsolete = value.startsWith("t");
} else if (ALIAS_ELEMENT.equals(key)) {
pAlias = value;
} else if (PRIMARY_ELEMENT.equals(key)) {
pPrimary = value.startsWith("t");
} else if (MULTIPLE_ELEMENT.equals(key)) {
pMultiple = value.startsWith("t");
} else if (PASSWORD_ELEMENT.equals(key)) {
pPassword = value.startsWith("t");
} else if (PROMPT_ELEMENT.equals(key)) {
pPrompt = resolveEndLines(value);
} else if (PROMPT_AGAIN_ELEMENT.equals(key)) {
pPromptAgain = resolveEndLines(value);
}
} else {
if (ROOT_ELEMENT.equals(key)) {
name = value;
} else if (ETAG_ELEMENT.equals(key)) {
eTag = value;
} else if (UNKNOWN_ARE_OPERANDS_ELEMENT.equals(key)) {
unknownAreOperands = value.startsWith("t");
} else if (ADDEDUPLOADOPTIONS_ELEMENT.equals(key)) {
addedUploadOption = value.startsWith("t");
} else if (USAGE_ELEMENT.equals(key)) {
usage = resolveEndLines(value);
} else if (NAME_ELEMENT.equals(key)) {
//Create base
result = new CachedCommandModel(name, eTag);
result.dashOk = unknownAreOperands;
result.setUsage(usage);
result.setAddedUploadOption(addedUploadOption);
//Continue in params
inParam = true;
pName = value;
}
}
}
if (inParam) {
//Add parameter
CommandModelData.ParamModelData pmd =
new CommandModelData.ParamModelData(pName,
pCls, pOptional, pDefaultValue, pShortName,
pObsolete, pAlias);
pmd.param._primary = pPrimary;
pmd.param._multiple = pMultiple;
pmd.param._password = pPassword;
pmd.prompt = pPrompt;
pmd.promptAgain = pPromptAgain;
result.add(pmd);
} else if (result == null && name != null && !name.isEmpty()) {
result = new CachedCommandModel(name, eTag);
result.dashOk = unknownAreOperands;
result.setUsage(usage);
result.setAddedUploadOption(addedUploadOption);
}
} finally {
try {r.close();} catch (Exception ex) {}
try {isr.close();} catch (Exception ex) {}
}