use of org.apache.zeppelin.display.ui.Password in project zeppelin by apache.
the class SparkInterpreterTest method testSparkInterpreter.
@Test
public void testSparkInterpreter() throws IOException, InterruptedException, InterpreterException {
Properties properties = new Properties();
properties.setProperty(SparkStringConstants.MASTER_PROP_NAME, "local");
properties.setProperty(SparkStringConstants.APP_NAME_PROP_NAME, "test");
properties.setProperty("zeppelin.spark.maxResult", "100");
properties.setProperty("zeppelin.spark.uiWebUrl", "fake_spark_weburl/{{applicationId}}");
// disable color output for easy testing
properties.setProperty("zeppelin.spark.scala.color", "false");
properties.setProperty("zeppelin.spark.deprecatedMsg.show", "false");
InterpreterContext context = InterpreterContext.builder().setInterpreterOut(new InterpreterOutput()).setIntpEventClient(mockRemoteEventClient).setAngularObjectRegistry(new AngularObjectRegistry("spark", null)).build();
InterpreterContext.set(context);
interpreter = new SparkInterpreter(properties);
interpreter.setInterpreterGroup(mock(InterpreterGroup.class));
interpreter.open();
InterpreterResult result = interpreter.interpret("val a=\"hello world\"", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals("a: String = hello world\n", output);
result = interpreter.interpret("print(a)", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals("hello world", output);
// java stdout
result = interpreter.interpret("System.out.print(a)", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals("hello world", output);
// incomplete
result = interpreter.interpret("println(a", getInterpreterContext());
assertEquals(InterpreterResult.Code.INCOMPLETE, result.code());
// syntax error
result = interpreter.interpret("println(b)", getInterpreterContext());
assertEquals(InterpreterResult.Code.ERROR, result.code());
assertTrue(output.contains("not found: value b"));
// multiple line
result = interpreter.interpret("\"123\".\ntoInt", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
// single line comment
result = interpreter.interpret("print(\"hello world\")/*comment here*/", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals("hello world", output);
result = interpreter.interpret("/*comment here*/\nprint(\"hello world\")", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
// multiple line comment
result = interpreter.interpret("/*line 1 \n line 2*/", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
// test function
result = interpreter.interpret("def add(x:Int, y:Int)\n{ return x+y }", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
result = interpreter.interpret("print(add(1,2))", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
result = interpreter.interpret("/*line 1 \n line 2*/print(\"hello world\")", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
result = interpreter.interpret("$intp", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
// Companion object with case class
result = interpreter.interpret("import scala.math._\n" + "object Circle {\n" + " private def calculateArea(radius: Double): Double = Pi * pow(radius, 2.0)\n" + "}\n" + "case class Circle(radius: Double) {\n" + " import Circle._\n" + " def area: Double = calculateArea(radius)\n" + "}\n" + "\n" + "val circle1 = new Circle(5.0)", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
// class extend
result = interpreter.interpret("import java.util.ArrayList", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
result = interpreter.interpret("class MyArrayList extends ArrayList{}", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
// spark rdd operation
context = getInterpreterContext();
context.setParagraphId("pid_1");
result = interpreter.interpret("sc\n.range(1, 10)\n.sum", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertTrue(output.contains("45"));
ArgumentCaptor<Map> captorEvent = ArgumentCaptor.forClass(Map.class);
verify(mockRemoteEventClient).onParaInfosReceived(captorEvent.capture());
assertEquals("pid_1", captorEvent.getValue().get("paraId"));
reset(mockRemoteEventClient);
context = getInterpreterContext();
context.setParagraphId("pid_2");
result = interpreter.interpret("sc\n.range(1, 10)\n.sum", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertTrue(output.contains("45"));
captorEvent = ArgumentCaptor.forClass(Map.class);
verify(mockRemoteEventClient).onParaInfosReceived(captorEvent.capture());
assertEquals("pid_2", captorEvent.getValue().get("paraId"));
// spark job url is sent
ArgumentCaptor<Map> onParaInfosReceivedArg = ArgumentCaptor.forClass(Map.class);
verify(mockRemoteEventClient).onParaInfosReceived(onParaInfosReceivedArg.capture());
assertTrue(((String) onParaInfosReceivedArg.getValue().get("jobUrl")).startsWith("fake_spark_weburl/" + interpreter.getJavaSparkContext().sc().applicationId()));
// case class
result = interpreter.interpret("val bankText = sc.textFile(\"bank.csv\")", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
result = interpreter.interpret("case class Bank(age:Integer, job:String, marital : String, education : String, balance : Integer)\n" + "val bank = bankText.map(s=>s.split(\";\")).filter(s => s(0)!=\"\\\"age\\\"\").map(\n" + " s => Bank(s(0).toInt, \n" + " s(1).replaceAll(\"\\\"\", \"\"),\n" + " s(2).replaceAll(\"\\\"\", \"\"),\n" + " s(3).replaceAll(\"\\\"\", \"\"),\n" + " s(5).replaceAll(\"\\\"\", \"\").toInt\n" + " )\n" + ").toDF()", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
// spark version
result = interpreter.interpret("sc.version", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
// spark sql test
String version = output.trim();
if (version.contains("String = 1.")) {
result = interpreter.interpret("sqlContext", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
result = interpreter.interpret("val df = sqlContext.createDataFrame(Seq((1,\"a\"),(2, null)))\n" + "df.show()", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertTrue(output.contains("+---+----+\n" + "| _1| _2|\n" + "+---+----+\n" + "| 1| a|\n" + "| 2|null|\n" + "+---+----+"));
} else {
// create dataset from case class
context = getInterpreterContext();
result = interpreter.interpret("case class Person(id:Int, name:String, age:Int, country:String)\n" + "val df2 = spark.createDataFrame(Seq(Person(1, \"andy\", 20, \"USA\"), " + "Person(2, \"jeff\", 23, \"China\"), Person(3, \"james\", 18, \"USA\")))\n" + "df2.printSchema\n" + "df2.show() ", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
result = interpreter.interpret("spark", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
result = interpreter.interpret("val df = spark.createDataFrame(Seq((1,\"a\"),(2, null)))\n" + "df.show()", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertTrue(output.contains("+---+----+\n" + "| _1| _2|\n" + "+---+----+\n" + "| 1| a|\n" + "| 2|null|\n" + "+---+----+"));
}
// ZeppelinContext
context = getInterpreterContext();
result = interpreter.interpret("z.show(df)", context);
assertEquals(context.out.toString(), InterpreterResult.Code.SUCCESS, result.code());
assertEquals(InterpreterResult.Type.TABLE, messageOutput.getType());
messageOutput.flush();
assertEquals("_1\t_2\n1\ta\n2\tnull\n", messageOutput.toInterpreterResultMessage().getData());
context = getInterpreterContext();
result = interpreter.interpret("z.input(\"name\", \"default_name\")", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals(1, context.getGui().getForms().size());
assertTrue(context.getGui().getForms().get("name") instanceof TextBox);
TextBox textBox = (TextBox) context.getGui().getForms().get("name");
assertEquals("name", textBox.getName());
assertEquals("default_name", textBox.getDefaultValue());
context = getInterpreterContext();
result = interpreter.interpret("z.password(\"pwd\")", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals(1, context.getGui().getForms().size());
assertTrue(context.getGui().getForms().get("pwd") instanceof Password);
Password pwd = (Password) context.getGui().getForms().get("pwd");
assertEquals("pwd", pwd.getName());
context = getInterpreterContext();
result = interpreter.interpret("z.checkbox(\"checkbox_1\", Seq((\"value_1\", \"name_1\"), (\"value_2\", \"name_2\")), Seq(\"value_2\"))", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals(1, context.getGui().getForms().size());
assertTrue(context.getGui().getForms().get("checkbox_1") instanceof CheckBox);
CheckBox checkBox = (CheckBox) context.getGui().getForms().get("checkbox_1");
assertEquals("checkbox_1", checkBox.getName());
assertEquals(1, checkBox.getDefaultValue().length);
assertEquals("value_2", checkBox.getDefaultValue()[0]);
assertEquals(2, checkBox.getOptions().length);
assertEquals("value_1", checkBox.getOptions()[0].getValue());
assertEquals("name_1", checkBox.getOptions()[0].getDisplayName());
assertEquals("value_2", checkBox.getOptions()[1].getValue());
assertEquals("name_2", checkBox.getOptions()[1].getDisplayName());
context = getInterpreterContext();
result = interpreter.interpret("z.select(\"select_1\", Seq((\"value_1\", \"name_1\"), (\"value_2\", \"name_2\")), Seq(\"value_2\"))", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals(1, context.getGui().getForms().size());
assertTrue(context.getGui().getForms().get("select_1") instanceof Select);
Select select = (Select) context.getGui().getForms().get("select_1");
assertEquals("select_1", select.getName());
// TODO(zjffdu) it seems a bug of GUI, the default value should be 'value_2', but it is List(value_2)
// assertEquals("value_2", select.getDefaultValue());
assertEquals(2, select.getOptions().length);
assertEquals("value_1", select.getOptions()[0].getValue());
assertEquals("name_1", select.getOptions()[0].getDisplayName());
assertEquals("value_2", select.getOptions()[1].getValue());
assertEquals("name_2", select.getOptions()[1].getDisplayName());
// completions
List<InterpreterCompletion> completions = interpreter.completion("a.", 2, getInterpreterContext());
assertTrue(completions.size() > 0);
completions = interpreter.completion("a.isEm", 6, getInterpreterContext());
assertEquals(1, completions.size());
assertEquals("isEmpty", completions.get(0).name);
completions = interpreter.completion("sc.ra", 5, getInterpreterContext());
assertEquals(1, completions.size());
assertEquals("range", completions.get(0).name);
// cursor in middle of code
completions = interpreter.completion("sc.ra\n1+1", 5, getInterpreterContext());
assertEquals(1, completions.size());
assertEquals("range", completions.get(0).name);
// Zeppelin-Display
result = interpreter.interpret("import org.apache.zeppelin.display.angular.notebookscope._\n" + "import AngularElem._", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
result = interpreter.interpret("<div style=\"color:blue\">\n" + "<h4>Hello Angular Display System</h4>\n" + "</div>.display", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals(InterpreterResult.Type.ANGULAR, messageOutput.getType());
assertTrue(messageOutput.toInterpreterResultMessage().getData().contains("Hello Angular Display System"));
result = interpreter.interpret("<div class=\"btn btn-success\">\n" + " Click me\n" + "</div>.onClick{() =>\n" + " println(\"hello world\")\n" + "}.display", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals(InterpreterResult.Type.ANGULAR, messageOutput.getType());
assertTrue(messageOutput.toInterpreterResultMessage().getData().contains("Click me"));
// getProgress
final InterpreterContext context2 = getInterpreterContext();
Thread interpretThread = new Thread() {
@Override
public void run() {
InterpreterResult result = null;
try {
result = interpreter.interpret("val df = sc.parallelize(1 to 10, 5).foreach(e=>Thread.sleep(1000))", context2);
} catch (InterpreterException e) {
e.printStackTrace();
}
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
}
};
interpretThread.start();
boolean nonZeroProgress = false;
int progress = 0;
while (interpretThread.isAlive()) {
progress = interpreter.getProgress(context2);
assertTrue(progress >= 0);
if (progress != 0 && progress != 100) {
nonZeroProgress = true;
}
Thread.sleep(100);
}
assertTrue(nonZeroProgress);
// cancel
final InterpreterContext context3 = getInterpreterContext();
interpretThread = new Thread() {
@Override
public void run() {
InterpreterResult result = null;
try {
result = interpreter.interpret("val df = sc.parallelize(1 to 10, 2).foreach(e=>Thread.sleep(1000))", context3);
} catch (InterpreterException e) {
e.printStackTrace();
}
assertEquals(InterpreterResult.Code.ERROR, result.code());
assertTrue(output.contains("cancelled"));
}
};
interpretThread.start();
// sleep 1 second to wait for the spark job start
Thread.sleep(1000);
interpreter.cancel(context3);
interpretThread.join();
}
use of org.apache.zeppelin.display.ui.Password in project zeppelin by apache.
the class BasePythonInterpreterTest method testZeppelinContext.
@Test
public void testZeppelinContext() throws InterpreterException, InterruptedException, IOException {
// TextBox
InterpreterContext context = getInterpreterContext();
InterpreterResult result = interpreter.interpret("z.input(name='text_1', defaultValue='value_1')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
List<InterpreterResultMessage> interpreterResultMessages = context.out.toInterpreterResultMessage();
assertTrue(interpreterResultMessages.get(0).getData().contains("'value_1'"));
assertEquals(1, context.getGui().getForms().size());
assertTrue(context.getGui().getForms().get("text_1") instanceof TextBox);
TextBox textbox = (TextBox) context.getGui().getForms().get("text_1");
assertEquals("text_1", textbox.getName());
assertEquals("value_1", textbox.getDefaultValue());
// Password
context = getInterpreterContext();
result = interpreter.interpret("z.password(name='pwd_1')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertTrue(context.getGui().getForms().get("pwd_1") instanceof Password);
Password password = (Password) context.getGui().getForms().get("pwd_1");
assertEquals("pwd_1", password.getName());
// Select
context = getInterpreterContext();
result = interpreter.interpret("z.select(name='select_1'," + " options=[('value_1', 'name_1'), ('value_2', 'name_2')])", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals(1, context.getGui().getForms().size());
assertTrue(context.getGui().getForms().get("select_1") instanceof Select);
Select select = (Select) context.getGui().getForms().get("select_1");
assertEquals("select_1", select.getName());
assertEquals(2, select.getOptions().length);
assertEquals("name_1", select.getOptions()[0].getDisplayName());
assertEquals("value_1", select.getOptions()[0].getValue());
// CheckBox
context = getInterpreterContext();
result = interpreter.interpret("z.checkbox(name='checkbox_1'," + "options=[('value_1', 'name_1'), ('value_2', 'name_2')])", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals(1, context.getGui().getForms().size());
assertTrue(context.getGui().getForms().get("checkbox_1") instanceof CheckBox);
CheckBox checkbox = (CheckBox) context.getGui().getForms().get("checkbox_1");
assertEquals("checkbox_1", checkbox.getName());
assertEquals(2, checkbox.getOptions().length);
assertEquals("name_1", checkbox.getOptions()[0].getDisplayName());
assertEquals("value_1", checkbox.getOptions()[0].getValue());
// Pandas DataFrame
context = getInterpreterContext();
result = interpreter.interpret("import pandas as pd\n" + "df = pd.DataFrame({'id':[1,2,3], 'name':['a\ta','b\\nb','c\\r\\nc']})\nz.show(df)", context);
assertEquals(context.out.toInterpreterResultMessage().toString(), InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
assertEquals("id\tname\n1\ta a\n2\tb b\n3\tc c\n", interpreterResultMessages.get(0).getData());
context = getInterpreterContext();
result = interpreter.interpret("import pandas as pd\n" + "df = pd.DataFrame({'id':[1,2,3,4], 'name':['a','b','c', 'd']})\nz.show(df)", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(2, interpreterResultMessages.size());
assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
assertEquals("id\tname\n1\ta\n2\tb\n3\tc\n", interpreterResultMessages.get(0).getData());
assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(1).getType());
assertEquals("<font color=red>Results are limited by 3.</font>\n", interpreterResultMessages.get(1).getData());
// z.show(df, show_index=True)
context = getInterpreterContext();
result = interpreter.interpret("import pandas as pd\n" + "df = pd.DataFrame({'id':[1,2,3], 'name':['a','b','c']})\n" + "z.show(df, show_index=True)", context);
assertEquals(context.out.toInterpreterResultMessage().toString(), InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
assertEquals("\tid\tname\n" + "%html <strong>0</strong>\t1\ta\n" + "%html <strong>1</strong>\t2\tb\n" + "%html <strong>2</strong>\t3\tc\n", interpreterResultMessages.get(0).getData());
// z.show(matplotlib)
context = getInterpreterContext();
result = interpreter.interpret("import matplotlib.pyplot as plt\n" + "data=[1,1,2,3,4]\nplt.figure()\nplt.plot(data)\nz.show(plt)", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(0).getType());
// clear output
context = getInterpreterContext();
result = interpreter.interpret("import time\nprint(\"Hello\")\n" + "time.sleep(0.5)\nz.getInterpreterContext().out().clear()\nprint(\"world\")\n", context);
assertEquals("%text world\n", context.out.getCurrentOutput().toString());
}
use of org.apache.zeppelin.display.ui.Password in project zeppelin by apache.
the class InputTest method testFormExtraction.
@Test
public void testFormExtraction() {
// textbox form
String script = "${input_form=}";
Map<String, Input> forms = Input.extractSimpleQueryForm(script, false);
assertEquals(1, forms.size());
Input form = forms.get("input_form");
assertEquals("input_form", form.name);
assertEquals("input_form", form.displayName);
assertEquals("", form.defaultValue);
assertTrue(form instanceof TextBox);
// textbox form with display name & default value
script = "${input_form(Input Form)=xxx}";
forms = Input.extractSimpleQueryForm(script, false);
form = forms.get("input_form");
assertEquals("xxx", form.defaultValue);
assertTrue(form instanceof TextBox);
assertEquals("Input Form", form.getDisplayName());
// password form with display name
script = "${password:my_pwd(My Password)}";
forms = Input.extractSimpleQueryForm(script, false);
form = forms.get("my_pwd");
assertTrue(form instanceof Password);
assertEquals("My Password", form.getDisplayName());
// selection form
script = "${select_form(Selection Form)=op1,op1|op2(Option 2)|op3}";
form = Input.extractSimpleQueryForm(script, false).get("select_form");
assertEquals("select_form", form.name);
assertEquals("op1", form.defaultValue);
assertEquals("Selection Form", form.getDisplayName());
assertTrue(form instanceof Select);
assertArrayEquals(new ParamOption[] { new ParamOption("op1", null), new ParamOption("op2", "Option 2"), new ParamOption("op3", null) }, ((Select) form).getOptions());
// checkbox form
script = "${checkbox:checkbox_form=op1,op1|op2|op3}";
form = Input.extractSimpleQueryForm(script, false).get("checkbox_form");
assertEquals("checkbox_form", form.name);
assertEquals("checkbox_form", form.displayName);
assertTrue(form instanceof CheckBox);
assertArrayEquals(new Object[] { "op1" }, (Object[]) form.defaultValue);
assertArrayEquals(new ParamOption[] { new ParamOption("op1", null), new ParamOption("op2", null), new ParamOption("op3", null) }, ((CheckBox) form).getOptions());
// checkbox form with multiple default checks
script = "${checkbox:checkbox_form(Checkbox Form)=op1|op3,op1(Option 1)|op2|op3}";
form = Input.extractSimpleQueryForm(script, false).get("checkbox_form");
assertEquals("checkbox_form", form.name);
assertEquals("Checkbox Form", form.displayName);
assertTrue(form instanceof CheckBox);
assertArrayEquals(new Object[] { "op1", "op3" }, (Object[]) form.defaultValue);
assertArrayEquals(new ParamOption[] { new ParamOption("op1", "Option 1"), new ParamOption("op2", null), new ParamOption("op3", null) }, ((CheckBox) form).getOptions());
// checkbox form with no default check
script = "${checkbox:checkbox_form(Checkbox Form)=,op1(Option 1)|op2(Option 2)|op3(Option 3)}";
form = Input.extractSimpleQueryForm(script, false).get("checkbox_form");
assertEquals("checkbox_form", form.name);
assertEquals("Checkbox Form", form.displayName);
assertTrue(form instanceof CheckBox);
assertArrayEquals(new Object[] {}, (Object[]) form.defaultValue);
assertArrayEquals(new ParamOption[] { new ParamOption("op1", "Option 1"), new ParamOption("op2", "Option 2"), new ParamOption("op3", "Option 3") }, ((CheckBox) form).getOptions());
}
use of org.apache.zeppelin.display.ui.Password in project zeppelin by apache.
the class Input method getInputForm.
private static Input getInputForm(Matcher match) {
String hiddenPart = match.group(1);
boolean hidden = false;
if ("_".equals(hiddenPart)) {
hidden = true;
}
String m = match.group(2);
String namePart;
String valuePart;
int p = m.indexOf('=');
if (p > 0) {
namePart = m.substring(0, p);
valuePart = m.substring(p + 1);
} else {
namePart = m;
valuePart = null;
}
String varName;
String displayName = null;
String type = null;
String arg = null;
Object defaultValue = null;
ParamOption[] paramOptions = null;
// get var name type
String varNamePart;
String[] typeArray = getType(namePart);
if (typeArray != null) {
type = typeArray[0];
arg = typeArray[1];
varNamePart = typeArray[2];
} else {
varNamePart = namePart;
}
// get var name and displayname
String[] varNameArray = getNameAndDisplayName(varNamePart);
if (varNameArray != null) {
varName = varNameArray[0];
displayName = varNameArray[1];
} else {
varName = varNamePart.trim();
}
// get defaultValue
if (valuePart != null) {
// find default value
int optionP = valuePart.indexOf(",");
if (optionP >= 0) {
// option available
defaultValue = valuePart.substring(0, optionP);
if (type != null && type.equals("checkbox")) {
// checkbox may contain multiple default checks
defaultValue = Input.splitPipe((String) defaultValue);
}
String optionPart = valuePart.substring(optionP + 1);
String[] options = Input.splitPipe(optionPart);
paramOptions = new ParamOption[options.length];
for (int i = 0; i < options.length; i++) {
String[] optNameArray = getNameAndDisplayName(options[i]);
if (optNameArray != null) {
paramOptions[i] = new ParamOption(optNameArray[0], optNameArray[1]);
} else {
paramOptions[i] = new ParamOption(options[i], null);
}
}
} else {
// no option
defaultValue = valuePart;
}
}
Input input = null;
if (type == null) {
if (paramOptions == null) {
input = new TextBox(varName, (String) defaultValue);
} else {
input = new Select(varName, defaultValue, paramOptions);
}
} else if (type.equals("checkbox")) {
input = new CheckBox(varName, (Object[]) defaultValue, paramOptions);
} else if (type.equals("password")) {
input = new Password(varName);
} else {
throw new RuntimeException("Could not recognize dynamic form with type: " + type);
}
input.setArgument(arg);
if (!StringUtils.isBlank(displayName)) {
// only set displayName when it is not empty (user explicitly specify it)
// e.g. ${name(display_name)=value)
input.setDisplayName(displayName);
}
input.setHidden(hidden);
return input;
}
Aggregations