Search in sources :

Example 1 with Extension

use of org.matheclipse.core.io.Extension in project symja_android_library by axkr.

the class SemanticImport 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 (fileName.startsWith("https://") || fileName.startsWith("http://")) {
            return readURL(fileName, format, engine);
        }
        if (format.equals(Extension.CSV) || format.equals(Extension.TSV)) {
            return readFile(fileName, engine);
        }
    }
    return F.NIL;
}
Also used : Extension(org.matheclipse.core.io.Extension) IStringX(org.matheclipse.core.interfaces.IStringX)

Example 2 with Extension

use of org.matheclipse.core.io.Extension in project symja_android_library by axkr.

the class Import method ofString.

/**
 * Get arbitrary data represented as a Symja expression string
 *
 * @param file
 * @param engine
 * @return
 * @throws IOException
 */
public static IExpr ofString(File file, EvalEngine engine) throws IOException {
    String filename = file.getName();
    Extension extension = Extension.importFilename(filename);
    // Extension extension = filename.extension();
    if (extension.equals(Extension.JPG) || extension.equals(Extension.PNG)) {
        // if (filename.hasExtension("jpg") || filename.hasExtension("png")) {
        return ImageFormat.from(ImageIO.read(file));
    }
    String str = com.google.common.io.Files.asCharSource(file, Charset.defaultCharset()).read();
    AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
    final Parser parser = new Parser(engine.isRelaxedSyntax(), true);
    final ASTNode node = parser.parse(str);
    return ast2Expr.convert(node);
}
Also used : Extension(org.matheclipse.core.io.Extension) ASTNode(org.matheclipse.parser.client.ast.ASTNode) AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser)

Example 3 with Extension

use of org.matheclipse.core.io.Extension in project symja_android_library by axkr.

the class Export 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.exportFilename(arg1.toString());
        if (ast.size() == 4) {
            if (!(ast.arg3() instanceof IStringX)) {
                return F.NIL;
            }
            // format = ((IStringX) ast.arg3()).toString();
            format = Extension.exportExtension(((IStringX) ast.arg3()).toString());
        }
        IExpr arg2 = ast.arg2();
        try (FileWriter writer = new FileWriter(arg1.toString())) {
            if (arg2 instanceof GraphExpr) {
                graphExport(((GraphExpr<DefaultEdge>) arg2).toData(), writer, format);
                return arg1;
            }
            if (format.equals(Extension.CSV) || format.equals(Extension.TSV)) {
                if (arg2.isDataset()) {
                    ((IASTDataset) arg2).csv(writer);
                    return arg1;
                }
            } else if (format.equals(Extension.TABLE)) {
                int[] dims = arg2.isMatrix();
                if (dims != null) {
                    for (int j = 0; j < dims[0]; j++) {
                        IAST rowList = (IAST) arg2.getAt(j + 1);
                        for (int i = 1; i <= dims[1]; i++) {
                            if (rowList.get(i).isReal()) {
                                writer.append(rowList.get(i).toString());
                            } else {
                                writer.append("\"");
                                writer.append(rowList.get(i).toString());
                                writer.append("\"");
                            }
                            if (i < dims[1]) {
                                writer.append(" ");
                            }
                        }
                        writer.append("\n");
                    }
                    return arg1;
                } else {
                    if (arg2.isList()) {
                    }
                }
            } else if (format.equals(Extension.DAT)) {
                File file = new File(arg1.toString());
                com.google.common.io.Files.write(arg2.toString(), file, Charset.defaultCharset());
                return arg1;
            } else if (format.equals(Extension.WXF)) {
                File file = new File(arg1.toString());
                byte[] bArray = WL.serialize(arg2);
                com.google.common.io.Files.write(bArray, file);
                return arg1;
            }
        } catch (IOException ioe) {
            LOGGER.log(engine.getLogLevel(), "Export: file {} not found!", arg1, ioe);
        } catch (Exception ex) {
            LOGGER.log(engine.getLogLevel(), "Export: file {}", arg1, ex);
        }
    }
    return F.NIL;
}
Also used : Extension(org.matheclipse.core.io.Extension) IASTDataset(org.matheclipse.core.interfaces.IASTDataset) GraphExpr(org.matheclipse.core.expression.data.GraphExpr) FileWriter(java.io.FileWriter) DefaultEdge(org.jgrapht.graph.DefaultEdge) IStringX(org.matheclipse.core.interfaces.IStringX) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IOException(java.io.IOException) File(java.io.File) ExportException(org.jgrapht.nio.ExportException) IOException(java.io.IOException)

Example 4 with Extension

use of org.matheclipse.core.io.Extension in project symja_android_library by axkr.

the class ImportString method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    if (!(ast.arg1() instanceof IStringX)) {
        return F.NIL;
    }
    String str1 = ((IStringX) ast.arg1()).toString();
    Extension format = Extension.TXT;
    if (ast.size() > 2) {
        if (!(ast.arg2() instanceof IStringX)) {
            return F.NIL;
        }
        format = Extension.importExtension(((IStringX) ast.arg2()).toString());
    }
    try {
        switch(format) {
            case JSON:
                return JSONConvert.importJSON(str1);
            case EXPRESSIONJSON:
                return ExpressionJSONConvert.importExpressionJSON(str1);
            case TABLE:
                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(new StringReader(str1));
                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(str1, engine);
            case TXT:
                return ofText(str1, engine);
            default:
        }
    } catch (SyntaxError se) {
        LOGGER.log(engine.getLogLevel(), "ImportString: syntax error!", se);
    } catch (Exception ex) {
        LOGGER.log(engine.getLogLevel(), "ImportString", ex);
    }
    return F.NIL;
}
Also used : 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) StringReader(java.io.StringReader) ASTNode(org.matheclipse.parser.client.ast.ASTNode) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) IStringX(org.matheclipse.core.interfaces.IStringX) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 5 with Extension

use of org.matheclipse.core.io.Extension in project symja_android_library by axkr.

the class ExportString method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    IExpr arg1 = ast.arg1();
    if (!(ast.arg2() instanceof IStringX)) {
        return F.NIL;
    }
    Extension format = Extension.exportExtension(((IStringX) ast.arg2()).toString());
    try (StringWriter writer = new StringWriter()) {
        if (format.equals(Extension.EXPRESSIONJSON)) {
            if (arg1.isNumber() || arg1.isSymbol()) {
                return F.stringx(arg1.toString());
            } else if (arg1.isString()) {
                return F.stringx("'" + arg1.toString() + "'");
            }
            return ExpressionJSONConvert.exportExpressionJSONIStringX(arg1);
        }
        if (arg1 instanceof GraphExpr) {
            graphExport(((GraphExpr<DefaultEdge>) arg1).toData(), writer, format);
            return F.stringx(writer.toString());
        }
        if (format.equals(Extension.CSV) || format.equals(Extension.TSV)) {
            if (arg1.isDataset()) {
                ((IASTDataset) arg1).csv(writer);
                return F.stringx(writer.toString());
            }
        } else if (format.equals(Extension.TABLE)) {
            int[] dims = arg1.isMatrix();
            if (dims != null) {
                for (int j = 0; j < dims[0]; j++) {
                    IAST rowList = (IAST) arg1.getAt(j + 1);
                    for (int i = 1; i <= dims[1]; i++) {
                        if (rowList.get(i).isReal()) {
                            writer.append(rowList.get(i).toString());
                        } else {
                            writer.append("\"");
                            writer.append(rowList.get(i).toString());
                            writer.append("\"");
                        }
                        if (i < dims[1]) {
                            writer.append(" ");
                        }
                    }
                    writer.append("\n");
                }
                return F.stringx(writer.toString());
            } else {
                if (arg1.isList()) {
                }
            }
        // } else if (format.equals(Extension.DAT)) {
        // File file = new File(arg1.toString());
        // com.google.common.io.Files.write(arg2.toString(), file, Charset.defaultCharset());
        // return arg1;
        // } else if (format.equals(Extension.WXF)) {
        // File file = new File(arg1.toString());
        // byte[] bArray = WL.serialize(arg2);
        // com.google.common.io.Files.write(bArray, file);
        // return arg1;
        }
    // } catch (IOException ioe) {
    // return engine.printMessage("ExportString: " + arg1.toString() + " not found!");
    } catch (Exception ex) {
        LOGGER.log(engine.getLogLevel(), "format: {}", arg1, ex);
        return F.NIL;
    }
    return F.NIL;
}
Also used : Extension(org.matheclipse.core.io.Extension) IASTDataset(org.matheclipse.core.interfaces.IASTDataset) StringWriter(java.io.StringWriter) GraphExpr(org.matheclipse.core.expression.data.GraphExpr) DefaultEdge(org.jgrapht.graph.DefaultEdge) IExpr(org.matheclipse.core.interfaces.IExpr) IStringX(org.matheclipse.core.interfaces.IStringX) IAST(org.matheclipse.core.interfaces.IAST) ExportException(org.jgrapht.nio.ExportException)

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