1 | /* | |
2 | * Copyright 2006 - 2013 | |
3 | * Stefan Balev <stefan.balev@graphstream-project.org> | |
4 | * Julien Baudry <julien.baudry@graphstream-project.org> | |
5 | * Antoine Dutot <antoine.dutot@graphstream-project.org> | |
6 | * Yoann Pign�� <yoann.pigne@graphstream-project.org> | |
7 | * Guilhelm Savin <guilhelm.savin@graphstream-project.org> | |
8 | * | |
9 | * This file is part of GraphStream <http://graphstream-project.org>. | |
10 | * | |
11 | * GraphStream is a library whose purpose is to handle static or dynamic | |
12 | * graph, create them from scratch, file or any source and display them. | |
13 | * | |
14 | * This program is free software distributed under the terms of two licenses, the | |
15 | * CeCILL-C license that fits European law, and the GNU Lesser General Public | |
16 | * License. You can use, modify and/ or redistribute the software under the terms | |
17 | * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following | |
18 | * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by | |
19 | * the Free Software Foundation, either version 3 of the License, or (at your | |
20 | * option) any later version. | |
21 | * | |
22 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY | |
23 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | |
24 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | |
25 | * | |
26 | * You should have received a copy of the GNU Lesser General Public License | |
27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
28 | * | |
29 | * The fact that you are presently reading this means that you have had | |
30 | * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms. | |
31 | */ | |
32 | package org.graphstream.stream.file; | |
33 | ||
34 | import java.io.IOException; | |
35 | import java.io.OutputStream; | |
36 | import java.io.PrintWriter; | |
37 | import java.io.Writer; | |
38 | ||
39 | import org.graphstream.graph.Edge; | |
40 | import org.graphstream.graph.Graph; | |
41 | import org.graphstream.graph.Node; | |
42 | ||
43 | /** | |
44 | * Base implementation for graph output to files. | |
45 | * | |
46 | * <p> | |
47 | * This class provides base services to write graphs into files using a specific | |
48 | * file format. It allows to create an output stream. By default a print stream | |
49 | * for easy text output, but binary files are possible. | |
50 | * </p> | |
51 | * | |
52 | * <p> | |
53 | * It handles completely the {@link #writeAll(Graph, OutputStream)}, | |
54 | * {@link #writeAll(Graph, String)}, {@link #begin(OutputStream)}, | |
55 | * {@link #begin(String)}, {@link #flush()} and {@link #end()} methods. You | |
56 | * should not have to modify or override these. | |
57 | * </p> | |
58 | * | |
59 | * <p> | |
60 | * In order to implement an output you have to: | |
61 | * <ul> | |
62 | * <li>Eventually override {@link #createWriter(OutputStream)} or | |
63 | * {@link #createWriter(String)} to replace the default instance of PrintStream | |
64 | * created for you.</li> | |
65 | * <li>Implement the {@link #outputHeader()} method. This method is called at | |
66 | * start, before any graph event is sent to output. Use it to output the header | |
67 | * of your file.</li> | |
68 | * <li>Implement the {@link #outputEndOfFile()} method. This method is called at | |
69 | * the end of the output, just before closing the output stream. Use it to | |
70 | * output any terminating syntax for the file format you implement.</li> | |
71 | * <li>Implement all the methods of {@link org.graphstream.stream.Sink}. All | |
72 | * these methods will be called for each graph event and must export these | |
73 | * events to the file you are writing. You should use the {@link #output} field | |
74 | * to write to the file. This field has type {@link java.io.OutputStream} but by | |
75 | * default is of type {@link java.io.PrintStream}, as most of the file format | |
76 | * will be textual.</li> | |
77 | * </ul> | |
78 | * </p> | |
79 | */ | |
80 | public abstract class FileSinkBase implements FileSink { | |
81 | // Attribute | |
82 | ||
83 | /** | |
84 | * The output. | |
85 | */ | |
86 | protected Writer output; | |
87 | ||
88 | // Command | |
89 | ||
90 | public void writeAll(Graph graph, String fileName) throws IOException { | |
91 |
1
1. writeAll : removed call to org/graphstream/stream/file/FileSinkBase::begin → NO_COVERAGE |
begin(fileName); |
92 |
1
1. writeAll : removed call to org/graphstream/stream/file/FileSinkBase::exportGraph → NO_COVERAGE |
exportGraph(graph); |
93 |
1
1. writeAll : removed call to org/graphstream/stream/file/FileSinkBase::end → NO_COVERAGE |
end(); |
94 | } | |
95 | ||
96 | public void writeAll(Graph graph, OutputStream stream) throws IOException { | |
97 |
1
1. writeAll : removed call to org/graphstream/stream/file/FileSinkBase::begin → NO_COVERAGE |
begin(stream); |
98 |
1
1. writeAll : removed call to org/graphstream/stream/file/FileSinkBase::exportGraph → NO_COVERAGE |
exportGraph(graph); |
99 |
1
1. writeAll : removed call to org/graphstream/stream/file/FileSinkBase::end → NO_COVERAGE |
end(); |
100 | } | |
101 | ||
102 | public void writeAll(Graph graph, Writer writer) throws IOException { | |
103 |
1
1. writeAll : removed call to org/graphstream/stream/file/FileSinkBase::begin → NO_COVERAGE |
begin(writer); |
104 |
1
1. writeAll : removed call to org/graphstream/stream/file/FileSinkBase::exportGraph → NO_COVERAGE |
exportGraph(graph); |
105 |
1
1. writeAll : removed call to org/graphstream/stream/file/FileSinkBase::end → NO_COVERAGE |
end(); |
106 | } | |
107 | ||
108 | /** | |
109 | * Echo each element and attribute of the graph to the actual output. | |
110 | * | |
111 | * The elements are echoed as add events (add node, add edge, add | |
112 | * attribute). This method guarantees there are no change or delete events. | |
113 | * | |
114 | * @param graph | |
115 | * The graph to export. | |
116 | */ | |
117 | protected void exportGraph(Graph graph) { | |
118 | String graphId = graph.getId(); | |
119 | long timeId = 0; | |
120 | ||
121 |
1
1. exportGraph : negated conditional → NO_COVERAGE |
for (String key : graph.getAttributeKeySet()) |
122 |
2
1. exportGraph : Replaced long addition with subtraction → NO_COVERAGE 2. exportGraph : removed call to org/graphstream/stream/file/FileSinkBase::graphAttributeAdded → NO_COVERAGE |
graphAttributeAdded(graphId, timeId++, key, graph.getAttribute(key)); |
123 | ||
124 |
1
1. exportGraph : negated conditional → NO_COVERAGE |
for (Node node : graph) { |
125 | String nodeId = node.getId(); | |
126 |
2
1. exportGraph : Replaced long addition with subtraction → NO_COVERAGE 2. exportGraph : removed call to org/graphstream/stream/file/FileSinkBase::nodeAdded → NO_COVERAGE |
nodeAdded(graphId, timeId++, nodeId); |
127 | ||
128 |
2
1. exportGraph : changed conditional boundary → NO_COVERAGE 2. exportGraph : negated conditional → NO_COVERAGE |
if (node.getAttributeCount() > 0) |
129 |
1
1. exportGraph : negated conditional → NO_COVERAGE |
for (String key : node.getAttributeKeySet()) |
130 |
2
1. exportGraph : Replaced long addition with subtraction → NO_COVERAGE 2. exportGraph : removed call to org/graphstream/stream/file/FileSinkBase::nodeAttributeAdded → NO_COVERAGE |
nodeAttributeAdded(graphId, timeId++, nodeId, key, |
131 | node.getAttribute(key)); | |
132 | } | |
133 | ||
134 |
1
1. exportGraph : negated conditional → NO_COVERAGE |
for (Edge edge : graph.getEachEdge()) { |
135 | String edgeId = edge.getId(); | |
136 |
2
1. exportGraph : Replaced long addition with subtraction → NO_COVERAGE 2. exportGraph : removed call to org/graphstream/stream/file/FileSinkBase::edgeAdded → NO_COVERAGE |
edgeAdded(graphId, timeId++, edgeId, edge.getNode0().getId(), edge |
137 | .getNode1().getId(), edge.isDirected()); | |
138 | ||
139 |
2
1. exportGraph : changed conditional boundary → NO_COVERAGE 2. exportGraph : negated conditional → NO_COVERAGE |
if (edge.getAttributeCount() > 0) |
140 |
1
1. exportGraph : negated conditional → NO_COVERAGE |
for (String key : edge.getAttributeKeySet()) |
141 |
2
1. exportGraph : Replaced long addition with subtraction → NO_COVERAGE 2. exportGraph : removed call to org/graphstream/stream/file/FileSinkBase::edgeAttributeAdded → NO_COVERAGE |
edgeAttributeAdded(graphId, timeId++, edgeId, key, |
142 | edge.getAttribute(key)); | |
143 | } | |
144 | } | |
145 | ||
146 | /* | |
147 | * (non-Javadoc) | |
148 | * @see org.graphstream.stream.file.FileSink#begin(java.lang.String) | |
149 | */ | |
150 | public void begin(String fileName) throws IOException { | |
151 |
1
1. begin : negated conditional → NO_COVERAGE |
if (output != null) |
152 | throw new IOException( | |
153 | "cannot call begin() twice without calling end() before."); | |
154 | ||
155 | output = createWriter(fileName); | |
156 | ||
157 |
1
1. begin : removed call to org/graphstream/stream/file/FileSinkBase::outputHeader → NO_COVERAGE |
outputHeader(); |
158 | } | |
159 | ||
160 | /* | |
161 | * (non-Javadoc) | |
162 | * @see org.graphstream.stream.file.FileSink#begin(java.io.OutputStream) | |
163 | */ | |
164 | public void begin(OutputStream stream) throws IOException { | |
165 |
1
1. begin : negated conditional → NO_COVERAGE |
if (output != null) |
166 | throw new IOException( | |
167 | "cannot call begin() twice without calling end() before."); | |
168 | ||
169 | output = createWriter(stream); | |
170 | ||
171 |
1
1. begin : removed call to org/graphstream/stream/file/FileSinkBase::outputHeader → NO_COVERAGE |
outputHeader(); |
172 | } | |
173 | ||
174 | /* | |
175 | * (non-Javadoc) | |
176 | * @see org.graphstream.stream.file.FileSink#begin(java.io.Writer) | |
177 | */ | |
178 | public void begin(Writer writer) throws IOException { | |
179 |
1
1. begin : negated conditional → NO_COVERAGE |
if (output != null) |
180 | throw new IOException( | |
181 | "cannot call begin() twice without calling end() before."); | |
182 | ||
183 | output = createWriter(writer); | |
184 | ||
185 |
1
1. begin : removed call to org/graphstream/stream/file/FileSinkBase::outputHeader → NO_COVERAGE |
outputHeader(); |
186 | } | |
187 | ||
188 | /* | |
189 | * (non-Javadoc) | |
190 | * @see org.graphstream.stream.file.FileSink#flush() | |
191 | */ | |
192 | public void flush() throws IOException { | |
193 |
1
1. flush : negated conditional → NO_COVERAGE |
if (output != null) |
194 |
1
1. flush : removed call to java/io/Writer::flush → NO_COVERAGE |
output.flush(); |
195 | } | |
196 | ||
197 | /* | |
198 | * (non-Javadoc) | |
199 | * @see org.graphstream.stream.file.FileSink#end() | |
200 | */ | |
201 | public void end() throws IOException { | |
202 |
1
1. end : removed call to org/graphstream/stream/file/FileSinkBase::outputEndOfFile → NO_COVERAGE |
outputEndOfFile(); |
203 |
1
1. end : removed call to java/io/Writer::flush → NO_COVERAGE |
output.flush(); |
204 |
1
1. end : removed call to java/io/Writer::close → NO_COVERAGE |
output.close(); |
205 | output = null; | |
206 | } | |
207 | ||
208 | /** | |
209 | * Method called at start just after the {@link #output} field is created. | |
210 | * Use it to output the header of the file. | |
211 | * | |
212 | * @throws IOException | |
213 | * If any I/O error occurs. | |
214 | */ | |
215 | protected abstract void outputHeader() throws IOException; | |
216 | ||
217 | /** | |
218 | * Method called at the end just before the {@link #output} field is flushed | |
219 | * and closed. Use it to output any information that closes the file. | |
220 | * | |
221 | * @throws IOException | |
222 | * If any I/O error occurs. | |
223 | */ | |
224 | protected abstract void outputEndOfFile() throws IOException; | |
225 | ||
226 | /** | |
227 | * Create a a writer from a file name. Override this method if the default | |
228 | * PrintWriter does not suits your needs. This method is called by | |
229 | * {@link #begin(String)} and {@link #writeAll(Graph, String)}. | |
230 | * | |
231 | * @param fileName | |
232 | * Name of the file to output to. | |
233 | * @return A new writer. | |
234 | * @throws IOException | |
235 | * If any I/O error occurs. | |
236 | */ | |
237 | protected Writer createWriter(String fileName) throws IOException { | |
238 |
1
1. createWriter : mutated return of Object value for org/graphstream/stream/file/FileSinkBase::createWriter to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return new PrintWriter(fileName); |
239 | } | |
240 | ||
241 | /** | |
242 | * Create a writer from an existing output stream. Override this method if | |
243 | * the default PrintWriter does not suits your needs. This method is called | |
244 | * by {@link #begin(OutputStream)} and | |
245 | * {@link #writeAll(Graph, OutputStream)}. This method does not create an | |
246 | * output stream if the given stream is already instance of PrintStream. | |
247 | * | |
248 | * @param stream | |
249 | * An already existing output stream. | |
250 | * @return A new writer. | |
251 | * @throws IOException | |
252 | * If any I/O error occurs. | |
253 | */ | |
254 | protected Writer createWriter(OutputStream stream) throws IOException { | |
255 |
1
1. createWriter : mutated return of Object value for org/graphstream/stream/file/FileSinkBase::createWriter to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return new PrintWriter(stream); |
256 | } | |
257 | ||
258 | /** | |
259 | * Create a writer from an existing writer. Override this method if the | |
260 | * default PrintWriter does not suits your needs. This method is called by | |
261 | * {@link #begin(Writer)} and {@link #writeAll(Graph, Writer)}. This method | |
262 | * does not create a new writer if the given writer is already instance of | |
263 | * PrintWriter. | |
264 | * | |
265 | * @param writer | |
266 | * An already existing writer. | |
267 | * @return A new writer. | |
268 | * @throws IOException | |
269 | * If any I/O error occurs. | |
270 | */ | |
271 | protected Writer createWriter(Writer writer) throws IOException { | |
272 |
1
1. createWriter : negated conditional → NO_COVERAGE |
if (writer instanceof PrintWriter) |
273 |
1
1. createWriter : mutated return of Object value for org/graphstream/stream/file/FileSinkBase::createWriter to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return writer; |
274 | ||
275 |
1
1. createWriter : mutated return of Object value for org/graphstream/stream/file/FileSinkBase::createWriter to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return new PrintWriter(writer); |
276 | } | |
277 | } | |
Mutations | ||
91 |
1.1 |
|
92 |
1.1 |
|
93 |
1.1 |
|
97 |
1.1 |
|
98 |
1.1 |
|
99 |
1.1 |
|
103 |
1.1 |
|
104 |
1.1 |
|
105 |
1.1 |
|
121 |
1.1 |
|
122 |
1.1 2.2 |
|
124 |
1.1 |
|
126 |
1.1 2.2 |
|
128 |
1.1 2.2 |
|
129 |
1.1 |
|
130 |
1.1 2.2 |
|
134 |
1.1 |
|
136 |
1.1 2.2 |
|
139 |
1.1 2.2 |
|
140 |
1.1 |
|
141 |
1.1 2.2 |
|
151 |
1.1 |
|
157 |
1.1 |
|
165 |
1.1 |
|
171 |
1.1 |
|
179 |
1.1 |
|
185 |
1.1 |
|
193 |
1.1 |
|
194 |
1.1 |
|
202 |
1.1 |
|
203 |
1.1 |
|
204 |
1.1 |
|
238 |
1.1 |
|
255 |
1.1 |
|
272 |
1.1 |
|
273 |
1.1 |
|
275 |
1.1 |