use of org.matheclipse.core.io.Extension in project symja_android_library by axkr.
the class Import method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (Config.isFileSystemEnabled(engine)) {
if (!(ast.arg1() instanceof IStringX)) {
return F.NIL;
}
IStringX arg1 = (IStringX) ast.arg1();
Extension format = Extension.importFilename(arg1.toString());
String fileName = arg1.toString();
if (ast.size() > 2) {
if (!(ast.arg2() instanceof IStringX)) {
return F.NIL;
}
format = Extension.importExtension(((IStringX) ast.arg2()).toString());
}
FileReader reader = null;
try {
File file = new File(fileName);
switch(format) {
case DOT:
case GRAPHML:
// graph Format
reader = new FileReader(fileName);
return graphImport(reader, format, engine);
case EXPRESSIONJSON:
return expressionJSONImport(fileName);
case JSON:
return jsonImport(fileName);
case M:
if (ast.isAST1()) {
return S.Get.of(engine, ast.arg1());
}
break;
case TABLE:
reader = new FileReader(fileName);
AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
final Parser parser = new Parser(engine.isRelaxedSyntax(), true);
CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(' ');
Iterable<CSVRecord> records = csvFormat.parse(reader);
IASTAppendable rowList = F.ListAlloc(256);
for (CSVRecord record : records) {
IASTAppendable columnList = F.ListAlloc(record.size());
for (String string : record) {
final ASTNode node = parser.parse(string);
IExpr temp = ast2Expr.convert(node);
columnList.append(temp);
}
rowList.append(columnList);
}
return rowList;
case STRING:
return ofString(file, engine);
case TXT:
return ofText(file, engine);
case WXF:
byte[] byteArray = com.google.common.io.Files.toByteArray(file);
return WL.deserialize(byteArray);
default:
}
} catch (IOException ioe) {
LOGGER.log(engine.getLogLevel(), "Import: file {} not found!", fileName, ioe);
} catch (SyntaxError se) {
LOGGER.log(engine.getLogLevel(), "Import: file {} syntax error!", fileName, se);
} catch (Exception ex) {
LOGGER.log(engine.getLogLevel(), "Import: file {} ", fileName, ex);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
return F.NIL;
}
Aggregations