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.DataFlavor;
029    import java.awt.datatransfer.Transferable;
030    import java.io.File;
031    import java.io.IOException;
032    import java.io.InputStream;
033    import java.io.Reader;
034    import java.lang.reflect.InvocationTargetException;
035    import java.lang.reflect.Method;
036    import java.net.URL;
037    import java.net.URLConnection;
038    
039    import org.ujmp.core.Matrix;
040    import org.ujmp.core.enums.FileFormat;
041    import org.ujmp.core.exceptions.MatrixException;
042    
043    public abstract class ImportMatrix {
044    
045            public static Matrix fromFile(File file, Object... parameters) throws MatrixException,
046                            IOException {
047                    return fromFile(ExportMatrix.guessFormat(file), file, parameters);
048            }
049    
050            public static Matrix fromFile(FileFormat format, File file, Object... parameters)
051                            throws MatrixException, IOException {
052                    try {
053                            Class<?> c = Class.forName("org.ujmp.core.io.ImportMatrix" + format.name());
054                            Method m = c.getMethod("fromFile", new Class<?>[] { File.class, Object[].class });
055                            Matrix matrix = (Matrix) m.invoke(null, file, parameters);
056                            return matrix;
057                    } catch (ClassNotFoundException e) {
058                            throw new MatrixException("format not supported: " + format, e);
059                    } catch (NoSuchMethodException e) {
060                            throw new MatrixException("format not supported: " + format, e);
061                    } catch (IllegalAccessException e) {
062                            throw new MatrixException("format not supported: " + format, e);
063                    } catch (InvocationTargetException e) {
064                            throw new MatrixException("could not import", e);
065                    }
066            }
067    
068            public static Matrix fromString(String string, Object... parameters) {
069                    return ImportMatrixCSV.fromString(string, parameters);
070            }
071    
072            public static Matrix fromString(FileFormat format, String string, Object parameters)
073                            throws MatrixException {
074                    try {
075                            Class<?> c = Class.forName("org.ujmp.core.io.ImportMatrix" + format.name());
076                            Method m = c.getMethod("fromString", new Class<?>[] { String.class, Object[].class });
077                            Matrix matrix = (Matrix) m.invoke(null, string, parameters);
078                            return matrix;
079                    } catch (ClassNotFoundException e) {
080                            throw new MatrixException("format not supported: " + format, e);
081                    } catch (NoSuchMethodException e) {
082                            throw new MatrixException("format not supported: " + format, e);
083                    } catch (IllegalAccessException e) {
084                            throw new MatrixException("format not supported: " + format, e);
085                    } catch (InvocationTargetException e) {
086                            throw new MatrixException("could not import", e);
087                    }
088            }
089    
090            public static Matrix fromURL(FileFormat format, URL url, Object... parameters)
091                            throws MatrixException, IOException {
092                    URLConnection connection = url.openConnection();
093                    connection.setRequestProperty("User-Agent",
094                                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
095                    connection.setUseCaches(false);
096                    connection.setDoInput(true);
097                    connection.setDoOutput(true);
098                    connection.setConnectTimeout(3000);
099                    Matrix m = fromStream(format, connection.getInputStream(), parameters);
100                    return m;
101            }
102    
103            public static Matrix fromURL(FileFormat format, String urlString, Object... parameters)
104                            throws MatrixException, IOException {
105                    if (FileFormat.ImapMessages.equals(format)) {
106                            return ImportMatrixImapMessages.fromURL(urlString, parameters);
107                    } else if (FileFormat.ImapFolders.equals(format)) {
108                            return ImportMatrixImapFolders.fromURL(urlString, parameters);
109                    } else {
110                            return fromURL(format, new URL(urlString), parameters);
111                    }
112            }
113    
114            public static Matrix fromStream(FileFormat format, InputStream stream, Object parameters)
115                            throws MatrixException, IOException {
116                    try {
117                            Class<?> c = Class.forName("org.ujmp.core.io.ImportMatrix" + format.name());
118                            Method m = c.getMethod("fromStream",
119                                            new Class<?>[] { InputStream.class, Object[].class });
120                            Matrix matrix = (Matrix) m.invoke(null, stream, parameters);
121                            return matrix;
122                    } catch (ClassNotFoundException e) {
123                            throw new MatrixException("format not supported: " + format, e);
124                    } catch (NoSuchMethodException e) {
125                            throw new MatrixException("format not supported: " + format, e);
126                    } catch (IllegalAccessException e) {
127                            throw new MatrixException("format not supported: " + format, e);
128                    } catch (InvocationTargetException e) {
129                            throw new MatrixException("could not import", e);
130                    }
131            }
132    
133            public static Matrix fromReader(FileFormat format, Reader reader, Object parameters)
134                            throws MatrixException, IOException {
135                    try {
136                            Class<?> c = Class.forName("org.ujmp.core.io.ImportMatrix" + format.name());
137                            Method m = c.getMethod("fromReader", new Class<?>[] { Reader.class, Object[].class });
138                            Matrix matrix = (Matrix) m.invoke(null, reader, parameters);
139                            return matrix;
140                    } catch (ClassNotFoundException e) {
141                            throw new MatrixException("format not supported: " + format, e);
142                    } catch (NoSuchMethodException e) {
143                            throw new MatrixException("format not supported: " + format, e);
144                    } catch (IllegalAccessException e) {
145                            throw new MatrixException("format not supported: " + format, e);
146                    } catch (InvocationTargetException e) {
147                            throw new MatrixException("could not import", e);
148                    }
149            }
150    
151            public static Matrix fromClipboard(FileFormat format, Object... parameters)
152                            throws MatrixException {
153                    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
154                    Transferable clipData = clipboard.getContents(null);
155                    String s;
156                    try {
157                            s = (String) (clipData.getTransferData(DataFlavor.stringFlavor));
158                    } catch (Exception ex) {
159                            s = ex.toString();
160                    }
161                    return fromString(format, s, parameters);
162            }
163    }