100 lines
2.6 KiB
Java
100 lines
2.6 KiB
Java
package org.gcube.portlets.user.statisticalalgorithmsimporter.client.codeparser;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
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;
|
|
|
|
/**
|
|
*
|
|
* @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]+(\\.(?i)(jpg|png|gif|bmp)\")$)";
|
|
private static final String FILE_PATTERN = "([^\\s]+(\\.(?i)(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;
|
|
}
|
|
|
|
|
|
Pattern patternFile = Pattern.compile(FILE_PATTERN);
|
|
if (patternFile.matcher(data).matches()) {
|
|
return DataType.FILE;
|
|
}
|
|
|
|
Pattern patternImage = Pattern.compile(IMAGE_PATTERN);
|
|
if (patternImage.matcher(data).matches()) {
|
|
return DataType.FILE;
|
|
}
|
|
|
|
Pattern patternEnum1 = Pattern.compile(ENUM1_PATTERN);
|
|
if (patternEnum1.matcher(data).matches()) {
|
|
return DataType.ENUMERATED;
|
|
}
|
|
|
|
Pattern patternEnum2 = Pattern.compile(ENUM2_PATTERN);
|
|
if (patternEnum2.matcher(data).matches()) {
|
|
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;
|
|
|
|
}
|
|
|
|
}
|