001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.io;
025    
026    import java.awt.Toolkit;
027    import java.awt.datatransfer.Clipboard;
028    import java.awt.datatransfer.StringSelection;
029    import java.io.File;
030    import java.io.IOException;
031    import java.io.OutputStream;
032    import java.io.StringWriter;
033    import java.io.Writer;
034    import java.lang.reflect.InvocationTargetException;
035    import java.lang.reflect.Method;
036    
037    import org.ujmp.core.Matrix;
038    import org.ujmp.core.enums.FileFormat;
039    import org.ujmp.core.exceptions.MatrixException;
040    
041    public abstract class ExportMatrix {
042    
043            public static final void toFile(File file, Matrix m, Object... parameters)
044                            throws MatrixException, IOException {
045                    toFile(guessFormat(file), file, m, parameters);
046            }
047    
048            public static final void toFile(String file, Matrix m, Object... parameters)
049                            throws MatrixException, IOException {
050                    toFile(guessFormat(new File(file)), file, m, parameters);
051            }
052    
053            public static final void toFile(FileFormat format, String filename, Matrix matrix,
054                            Object... parameters) throws MatrixException, IOException {
055                    toFile(format, new File(filename), matrix, parameters);
056            }
057    
058            public static final void toFile(FileFormat format, File file, Matrix matrix,
059                            Object... parameters) throws MatrixException, IOException {
060                    try {
061                            File dir = file.getParentFile();
062                            if (dir != null && !dir.exists()) {
063                                    dir.mkdirs();
064                            }
065                            Class<?> c = Class.forName("org.ujmp.core.io.ExportMatrix" + format.name());
066                            Method m = c.getMethod("toFile", new Class<?>[] { File.class, Matrix.class,
067                                            Object[].class });
068                            m.invoke(null, file, matrix, parameters);
069                    } catch (ClassNotFoundException e) {
070                            throw new MatrixException("file format not supported: " + format, e);
071                    } catch (NoSuchMethodException e) {
072                            throw new MatrixException("file format not supported: " + format, e);
073                    } catch (IllegalAccessException e) {
074                            throw new MatrixException("file format not supported: " + format, e);
075                    } catch (InvocationTargetException e) {
076                            throw new MatrixException("error exporting matrix in format: " + format, e);
077                    }
078            }
079    
080            public static FileFormat guessFormat(File file) {
081                    String filename = file.getAbsolutePath();
082                    String[] components = filename.split("\\.");
083                    String suffix = components[components.length - 1];
084                    if (suffix.equalsIgnoreCase("gz") || suffix.equalsIgnoreCase("z")
085                                    || suffix.equalsIgnoreCase("gzip") || suffix.equalsIgnoreCase(".zip")
086                                    || suffix.equalsIgnoreCase(".7zip") || suffix.equalsIgnoreCase(".7z")) {
087                            suffix = components[components.length - 2];
088                    }
089    
090                    for (FileFormat f : FileFormat.values()) {
091                            if (suffix.equalsIgnoreCase(f.name())) {
092                                    return f;
093                            }
094                    }
095    
096                    throw new MatrixException(
097                                    "could not guess file format, please use exportToFile(Format,File,Matrix)");
098            }
099    
100            public static final String toString(FileFormat format, Matrix matrix, Object... parameters)
101                            throws MatrixException, IOException {
102                    StringWriter writer = new StringWriter();
103                    toWriter(format, writer, matrix, parameters);
104                    writer.close();
105                    return writer.toString();
106            }
107    
108            public static final void toClipboard(FileFormat format, Matrix matrix, Object... parameters)
109                            throws MatrixException, IOException {
110                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
111                    StringSelection ms = new StringSelection(toString(format, matrix, parameters));
112                    clipboard.setContents(ms, ms);
113            }
114    
115            public static final void toStream(FileFormat format, OutputStream outputStream, Matrix matrix,
116                            Object... parameters) throws IOException {
117                    try {
118                            Class<?> c = Class.forName("org.ujmp.core.io.ExportMatrix" + format.name());
119                            Method m = c.getMethod("toStream", new Class<?>[] { OutputStream.class, Matrix.class,
120                                            Object[].class });
121                            m.invoke(null, outputStream, matrix, parameters);
122                    } catch (Exception e) {
123                            throw new MatrixException("stream format not supported: " + format, e);
124                    }
125            }
126    
127            public static final void toWriter(FileFormat format, Writer writer, Matrix matrix,
128                            Object... parameters) {
129                    try {
130                            Class<?> c = Class.forName("org.ujmp.core.io.ExportMatrix" + format.name());
131                            Method m = c.getMethod("toWriter", new Class<?>[] { Writer.class, Matrix.class,
132                                            Object[].class });
133                            m.invoke(null, writer, matrix, parameters);
134                    } catch (Exception e) {
135                            throw new MatrixException("writer format not supported: " + format, e);
136                    }
137            }
138    }