Search in sources :

Example 6 with Extension

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;
}
Also used : IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ImportException(org.jgrapht.nio.ImportException) AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser) Extension(org.matheclipse.core.io.Extension) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) SyntaxError(org.matheclipse.parser.client.SyntaxError) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FileReader(java.io.FileReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) IStringX(org.matheclipse.core.interfaces.IStringX) IExpr(org.matheclipse.core.interfaces.IExpr) File(java.io.File)

Aggregations

Extension (org.matheclipse.core.io.Extension)6 IStringX (org.matheclipse.core.interfaces.IStringX)5 IExpr (org.matheclipse.core.interfaces.IExpr)4 AST2Expr (org.matheclipse.core.convert.AST2Expr)3 Parser (org.matheclipse.parser.client.Parser)3 ASTNode (org.matheclipse.parser.client.ast.ASTNode)3 File (java.io.File)2 IOException (java.io.IOException)2 CSVFormat (org.apache.commons.csv.CSVFormat)2 CSVRecord (org.apache.commons.csv.CSVRecord)2 DefaultEdge (org.jgrapht.graph.DefaultEdge)2 ExportException (org.jgrapht.nio.ExportException)2 GraphExpr (org.matheclipse.core.expression.data.GraphExpr)2 IAST (org.matheclipse.core.interfaces.IAST)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 IASTDataset (org.matheclipse.core.interfaces.IASTDataset)2 SyntaxError (org.matheclipse.parser.client.SyntaxError)2 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 StringReader (java.io.StringReader)1