Skip to content

Commit

Permalink
GUI: Allow quotes in console dialog
Browse files Browse the repository at this point in the history
Tested with quotes and escaped quotes.

Fixes semuxproject#13
  • Loading branch information
orogvany committed Jul 5, 2019
1 parent 639338e commit a8dc9b3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/main/java/org/semux/gui/dialog/ConsoleDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
Expand All @@ -43,6 +45,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 +159,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 +172,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
41 changes: 41 additions & 0 deletions src/test/java/org/semux/util/CommandParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* 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));

}
}

0 comments on commit a8dc9b3

Please sign in to comment.