statistical-algorithms-impo.../src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/codeparser/CodeParser.java

101 lines
2.5 KiB
Java

package org.gcube.portlets.user.statisticalalgorithmsimporter.client.codeparser;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.DataType;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.IOType;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.input.SelectedRowsVariables;
import com.google.gwt.regexp.shared.RegExp;
/**
*
* @author Giancarlo Panichi email: <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class CodeParser {
private static final String IMAGE_PATTERN = "([^\\s]+(\\.(jpg|png|gif|bmp)\"))";
private static final String FILE_PATTERN = "([^\\s]+(\\.(txt|csv|pdf|doc)\"))";
private static final String ENUM1_PATTERN = "(c\\([^\\)]*\\))";
private static final String ENUM2_PATTERN = "(\\{[^\\}]*\\})";
public CodeParser() {
}
public SelectedRowsVariables parse(String parameter, IOType ioType) {
SelectedRowsVariables selectedRowsVariables = null;
if (parameter == null) {
return null;
}
if (parameter.contains("<-")) {
String[] varDescription = parameter.split("<-");
selectedRowsVariables = new SelectedRowsVariables(
varDescription[0].trim(), varDescription[0].trim(),
varDescription[1].trim(),
checkDataType(varDescription[1].trim()), ioType, parameter);
} else {
if (parameter.contains("=")) {
String[] varDescription = parameter.split("=");
selectedRowsVariables = new SelectedRowsVariables(
varDescription[0].trim(), varDescription[0].trim(),
varDescription[1].trim(),
checkDataType(varDescription[1].trim()), ioType,
parameter);
} else {
return null;
}
}
return selectedRowsVariables;
}
private DataType checkDataType(String data) {
if (data == null || data.isEmpty()) {
return DataType.STRING;
}
// Compile and use regular expression
RegExp regExpFile = RegExp.compile(FILE_PATTERN);
if (regExpFile.test(data)) {
return DataType.FILE;
}
RegExp regExpImage = RegExp.compile(IMAGE_PATTERN);
if (regExpImage.test(data)) {
return DataType.FILE;
}
RegExp regExpEnum1 = RegExp.compile(ENUM1_PATTERN);
if (regExpEnum1.test(data)) {
return DataType.ENUMERATED;
}
RegExp regExpEnum2 = RegExp.compile(ENUM2_PATTERN);
if (regExpEnum2.test(data)) {
return DataType.ENUMERATED;
}
try {
Integer.parseInt(data);
return DataType.INTEGER;
} catch (NumberFormatException e) {
}
try {
Double.parseDouble(data);
return DataType.DOUBLE;
} catch (NumberFormatException e) {
}
return DataType.STRING;
}
}