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 ImportMatrixATOM { 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("entry"); 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("id")) { 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 Node h = n.getAttributes().getNamedItem("href"); 070 if (h != null) { 071 m.setAsObject(h.getTextContent(), i, 2); 072 } else { 073 m.setAsObject(n.getTextContent(), i, 2); 074 } 075 } else if (n.getNodeName().equalsIgnoreCase("summary")) { 076 m.setAsObject(n.getTextContent(), i, 3); 077 } else if (n.getNodeName().equalsIgnoreCase("updated")) { 078 m.setAsObject(n.getTextContent(), i, 4); 079 } 080 } 081 } 082 083 NodeList channels = doc.getElementsByTagName("feed"); 084 if (channels.getLength() > 0) { 085 Node channel = channels.item(0); 086 for (int c = 0; c < channel.getChildNodes().getLength(); c++) { 087 Node n = channel.getChildNodes().item(c); 088 if (n.getNodeName().equalsIgnoreCase("title")) { 089 m.setLabel(n.getTextContent()); 090 } 091 } 092 } 093 094 return m; 095 } 096 097 public static final Matrix fromFile(File file, Object... parameters) throws MatrixException, 098 ParserConfigurationException, SAXException, IOException { 099 FileInputStream lr = new FileInputStream(file); 100 Matrix m = fromStream(lr, parameters); 101 lr.close(); 102 return m; 103 } 104 105 }