001/* 002 * Copyright 2006 - 2013 003 * Stefan Balev <stefan.balev@graphstream-project.org> 004 * Julien Baudry <julien.baudry@graphstream-project.org> 005 * Antoine Dutot <antoine.dutot@graphstream-project.org> 006 * Yoann Pigné <yoann.pigne@graphstream-project.org> 007 * Guilhelm Savin <guilhelm.savin@graphstream-project.org> 008 * 009 * This file is part of GraphStream <http://graphstream-project.org>. 010 * 011 * GraphStream is a library whose purpose is to handle static or dynamic 012 * graph, create them from scratch, file or any source and display them. 013 * 014 * This program is free software distributed under the terms of two licenses, the 015 * CeCILL-C license that fits European law, and the GNU Lesser General Public 016 * License. You can use, modify and/ or redistribute the software under the terms 017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following 018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by 019 * the Free Software Foundation, either version 3 of the License, or (at your 020 * option) any later version. 021 * 022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 024 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 025 * 026 * You should have received a copy of the GNU Lesser General Public License 027 * along with this program. If not, see <http://www.gnu.org/licenses/>. 028 * 029 * The fact that you are presently reading this means that you have had 030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms. 031 */ 032package org.graphstream.stream.file; 033 034import java.io.FileReader; 035import java.io.IOException; 036import java.io.InputStream; 037import java.io.InputStreamReader; 038import java.io.Reader; 039import java.net.URL; 040 041import org.graphstream.stream.SourceBase; 042import org.graphstream.util.parser.ParseException; 043import org.graphstream.util.parser.Parser; 044import org.graphstream.util.parser.ParserFactory; 045 046/** 047 * This defines source using a {@link org.graphstream.util.parser.Parser} object 048 * to parse a stream and generate graph events. 049 * 050 */ 051public abstract class FileSourceParser extends SourceBase implements FileSource { 052 /** 053 * Factory used to create parser. 054 */ 055 protected ParserFactory factory; 056 057 /** 058 * Parser opened by a call to {@link #begin(Reader)}. 059 */ 060 protected Parser parser; 061 062 /** 063 * Get a new parser factory. 064 * 065 * @return a parser factory 066 */ 067 public abstract ParserFactory getNewParserFactory(); 068 069 protected FileSourceParser() { 070 factory = getNewParserFactory(); 071 } 072 073 /* 074 * (non-Javadoc) 075 * 076 * @see org.graphstream.stream.file.FileSource#readAll(java.lang.String) 077 */ 078 public void readAll(String fileName) throws IOException { 079 Parser parser = factory.newParser(createReaderForFile(fileName)); 080 081 try { 082 parser.all(); 083 parser.close(); 084 } catch (ParseException e) { 085 throw new IOException(e); 086 } 087 } 088 089 /* 090 * (non-Javadoc) 091 * 092 * @see org.graphstream.stream.file.FileSource#readAll(java.net.URL) 093 */ 094 public void readAll(URL url) throws IOException { 095 Parser parser = factory.newParser(new InputStreamReader(url 096 .openStream())); 097 098 try { 099 parser.all(); 100 parser.close(); 101 } catch (ParseException e) { 102 throw new IOException(e); 103 } 104 } 105 106 /* 107 * (non-Javadoc) 108 * 109 * @see org.graphstream.stream.file.FileSource#readAll(java.io.InputStream) 110 */ 111 public void readAll(InputStream stream) throws IOException { 112 Parser parser = factory.newParser(new InputStreamReader(stream)); 113 114 try { 115 parser.all(); 116 parser.close(); 117 } catch (ParseException e) { 118 throw new IOException(e); 119 } 120 } 121 122 /* 123 * (non-Javadoc) 124 * 125 * @see org.graphstream.stream.file.FileSource#readAll(java.io.Reader) 126 */ 127 public void readAll(Reader reader) throws IOException { 128 Parser parser = factory.newParser(reader); 129 130 try { 131 parser.all(); 132 parser.close(); 133 } catch (ParseException e) { 134 throw new IOException(e); 135 } 136 } 137 138 /* 139 * (non-Javadoc) 140 * 141 * @see org.graphstream.stream.file.FileSource#begin(java.lang.String) 142 */ 143 public void begin(String fileName) throws IOException { 144 if (parser != null) 145 end(); 146 147 parser = factory.newParser(createReaderForFile(fileName)); 148 149 try { 150 parser.open(); 151 } catch (ParseException e) { 152 throw new IOException(e); 153 } 154 } 155 156 /* 157 * (non-Javadoc) 158 * 159 * @see org.graphstream.stream.file.FileSource#begin(java.net.URL) 160 */ 161 public void begin(URL url) throws IOException { 162 parser = factory.newParser(new InputStreamReader(url.openStream())); 163 164 try { 165 parser.open(); 166 } catch (ParseException e) { 167 throw new IOException(e); 168 } 169 } 170 171 /* 172 * (non-Javadoc) 173 * 174 * @see org.graphstream.stream.file.FileSource#begin(java.io.InputStream) 175 */ 176 public void begin(InputStream stream) throws IOException { 177 parser = factory.newParser(new InputStreamReader(stream)); 178 179 try { 180 parser.open(); 181 } catch (ParseException e) { 182 throw new IOException(e); 183 } 184 } 185 186 /* 187 * (non-Javadoc) 188 * 189 * @see org.graphstream.stream.file.FileSource#begin(java.io.Reader) 190 */ 191 public void begin(Reader reader) throws IOException { 192 parser = factory.newParser(reader); 193 194 try { 195 parser.open(); 196 } catch (ParseException e) { 197 throw new IOException(e); 198 } 199 } 200 201 /* 202 * (non-Javadoc) 203 * 204 * @see org.graphstream.stream.file.FileSource#nextEvents() 205 */ 206 public boolean nextEvents() throws IOException { 207 try { 208 return parser.next(); 209 } catch (ParseException e) { 210 throw new IOException(e); 211 } 212 } 213 214 /** 215 * Since there is no step in DOT, this does the same action than 216 * {@link #nextEvents()}. 217 */ 218 public boolean nextStep() throws IOException { 219 return nextEvents(); 220 } 221 222 /* 223 * (non-Javadoc) 224 * 225 * @see org.graphstream.stream.file.FileSource#end() 226 */ 227 public void end() throws IOException { 228 parser.close(); 229 parser = null; 230 } 231 232 protected Reader createReaderForFile(String filename) throws IOException { 233 return new FileReader(filename); 234 } 235}