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.io.File;
027    import java.io.FileInputStream;
028    import java.io.IOException;
029    import java.io.InputStream;
030    
031    import javax.xml.parsers.DocumentBuilder;
032    import javax.xml.parsers.DocumentBuilderFactory;
033    import javax.xml.parsers.ParserConfigurationException;
034    
035    import org.ujmp.core.Matrix;
036    import org.ujmp.core.MatrixFactory;
037    import org.ujmp.core.enums.ValueType;
038    import org.ujmp.core.exceptions.MatrixException;
039    import org.w3c.dom.Document;
040    import org.w3c.dom.Node;
041    import org.w3c.dom.NodeList;
042    import org.xml.sax.SAXException;
043    
044    public class ImportMatrixRSS {
045    
046            public static final Matrix fromStream(InputStream stream, Object... parameters)
047                            throws MatrixException, ParserConfigurationException, SAXException, IOException {
048                    Matrix m = null;
049                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
050                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
051                    Document doc = dBuilder.parse(stream);
052    
053                    NodeList items = doc.getElementsByTagName("item");
054                    m = MatrixFactory.dense(ValueType.OBJECT, items.getLength(), 5);
055                    m.setColumnLabel(0, "Id");
056                    m.setColumnLabel(1, "Label");
057                    m.setColumnLabel(2, "Link");
058                    m.setColumnLabel(3, "Description");
059                    m.setColumnLabel(4, "Date");
060                    for (int i = 0; i < items.getLength(); i++) {
061                            Node item = items.item(i);
062                            for (int c = 0; c < item.getChildNodes().getLength(); c++) {
063                                    Node n = item.getChildNodes().item(c);
064                                    if (n.getNodeName().equalsIgnoreCase("guid")) {
065                                            m.setAsObject(n.getTextContent(), i, 0);
066                                    } else if (n.getNodeName().equalsIgnoreCase("title")) {
067                                            m.setAsObject(n.getTextContent(), i, 1);
068                                    } else if (n.getNodeName().equalsIgnoreCase("link")) {
069                                            m.setAsObject(n.getTextContent(), i, 2);
070                                    } else if (n.getNodeName().equalsIgnoreCase("description")) {
071                                            m.setAsObject(n.getTextContent(), i, 3);
072                                    } else if (n.getNodeName().equalsIgnoreCase("pubDate")) {
073                                            m.setAsObject(n.getTextContent(), i, 4);
074                                    }
075                            }
076                    }
077    
078                    NodeList channels = doc.getElementsByTagName("channel");
079                    if (channels.getLength() > 0) {
080                            Node channel = channels.item(0);
081                            for (int c = 0; c < channel.getChildNodes().getLength(); c++) {
082                                    Node n = channel.getChildNodes().item(c);
083                                    if (n.getNodeName().equalsIgnoreCase("title")) {
084                                            m.setLabel(n.getTextContent());
085                                    }
086                            }
087                    }
088    
089                    return m;
090            }
091    
092            public static final Matrix fromFile(File file, Object... parameters) throws MatrixException,
093                            ParserConfigurationException, SAXException, IOException {
094                    FileInputStream lr = new FileInputStream(file);
095                    Matrix m = fromStream(lr, parameters);
096                    lr.close();
097                    return m;
098            }
099    
100    }