Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUI: Allow quotes in console dialog #203

Merged
merged 2 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/main/java/org/semux/gui/dialog/ConsoleDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.fasterxml.jackson.databind.SerializationFeature;

import io.swagger.annotations.ApiOperation;
import org.semux.util.CommandParser;

public class ConsoleDialog extends JDialog implements ActionListener {

Expand Down Expand Up @@ -156,8 +157,9 @@ public void actionPerformed(ActionEvent e) {
* @param input
*/
protected String callApi(String input) {
String[] commandArguments = input.split(" ");
String command = commandArguments[0];
List<String> commandArguments = CommandParser.parseInput(input);

String command = commandArguments.get(0);

MethodDescriptor md = methods.get(command);
if (md == null) {
Expand All @@ -168,19 +170,19 @@ protected String callApi(String input) {
Method method = api.getClass().getMethod(command, md.argumentTypes);
Object[] arguments = new Object[md.argumentTypes.length];

if (arguments.length < commandArguments.length - 1) {
if (arguments.length < commandArguments.size() - 1) {
return GuiMessages.get("MethodError", command);
}

for (int i = 0; i < commandArguments.length - 1; i++) {
String argument = commandArguments[i + 1];
for (int i = 0; i < commandArguments.size() - 1; i++) {
String argument = commandArguments.get(i + 1);
if (md.argumentTypes[i] == Boolean.class) {
arguments[i] = Boolean.parseBoolean(argument);
} else {
if (NULL.equals(argument)) {
arguments[i] = null;
} else {
arguments[i] = commandArguments[i + 1];
arguments[i] = commandArguments.get(i + 1);
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/semux/util/CommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2017-2018 The Semux Developers
*
* Distributed under the MIT software license, see the accompanying file
* LICENSE or https://opensource.org/licenses/mit-license.php
*/
package org.semux.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommandParser {

public static List<String> parseInput(String input) {
List<String> commandArguments = new ArrayList<>();
Pattern ptrn = Pattern.compile("\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"|\\S+");
Matcher matcher = ptrn.matcher(input);
while (matcher.find()) {
commandArguments.add(sanitize(matcher.group(0)));
}
return commandArguments;
}

private static String sanitize(String value) {
if (value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
return value.replace("\\\"", "\"");
}
}
4 changes: 3 additions & 1 deletion src/test/java/org/semux/gui/dialog/ConsoleDialogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.DialogFixture;
Expand Down Expand Up @@ -72,7 +74,7 @@ public void testBasicUse() throws InterruptedException {
when(blockChain.getBlock(anyLong())).thenReturn(block);
kernelRule1.getKernel().setBlockchain(blockChain);

console.textBox("txtInput").enterText("getBlockByNumber 1\n");
console.textBox("txtInput").enterText("getBlockByNumber \"1\"\n");

await().until(() -> consoleText.text().contains("transactionsRoot"));
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/org/semux/util/CommandParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2017-2018 The Semux Developers
*
* Distributed under the MIT software license, see the accompanying file
* LICENSE or https://opensource.org/licenses/mit-license.php
*/
package org.semux.util;

import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class CommandParserTest {

@Test
public void testDelimiting() {
String input = "foo and \"bar stuff\" \"stuff \\\" with quotes \\\" \" more \"\"";
List<String> parsed = CommandParser.parseInput(input);

Assert.assertEquals(6, parsed.size());
Assert.assertEquals("foo", parsed.get(0));
Assert.assertEquals("and", parsed.get(1));
Assert.assertEquals("bar stuff", parsed.get(2));
Assert.assertEquals("stuff \" with quotes \" ", parsed.get(3));
Assert.assertEquals("more", parsed.get(4));
Assert.assertEquals("", parsed.get(5));
}

@Test
public void testBasic() {
String input = "getBlockByNumber 1";
List<String> parsed = CommandParser.parseInput(input);
Assert.assertEquals(2, parsed.size());
Assert.assertEquals("getBlockByNumber", parsed.get(0));
Assert.assertEquals("1", parsed.get(1));
}
}