FileSourceFactory.java

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.File;
35
import java.io.FileReader;
36
import java.io.IOException;
37
import java.io.RandomAccessFile;
38
39
import javax.xml.stream.FactoryConfigurationError;
40
import javax.xml.stream.XMLEventReader;
41
import javax.xml.stream.XMLInputFactory;
42
import javax.xml.stream.XMLStreamException;
43
import javax.xml.stream.events.XMLEvent;
44
45
/**
46
 * File source factory.
47
 * 
48
 * <p>
49
 * A graph reader factory allow to create readers according to a given file. It
50
 * both tries to read the start of the file to infer its type (works well for
51
 * file formats with a magic cookie or header), and if it fails it tries to look
52
 * at the file name extension.
53
 * </p>
54
 */
55
public class FileSourceFactory {
56
	/**
57
	 * Create a file input for the given file name.
58
	 * 
59
	 * <p>
60
	 * This method first tests if the file is a regular file and is readable. If
61
	 * so, it opens it and reads the magic cookie to test the known file formats
62
	 * that can be inferred from their header. If it works, it returns a file
63
	 * input for the format. Else it looks at the file name extension, and
64
	 * returns a file input for the extension. Finally if all fail, it throws a
65
	 * NotFoundException.
66
	 * </p>
67
	 * 
68
	 * <p>
69
	 * Notice that this method only creates the file input and does not connect
70
	 * it to a graph.
71
	 * </p>
72
	 * 
73
	 * @param fileName
74
	 *            Name of the graph file.
75
	 * @return A graph reader suitable for the fileName graph format.
76
	 * @throws IOException
77
	 *             If the file is not readable or accessible.
78
	 */
79
	public static FileSource sourceFor(String fileName) throws IOException {
80
		File file = new File(fileName);
81
82 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (!file.isFile())
83
			throw new IOException("not a regular file '" + fileName + "'");
84
85 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (!file.canRead())
86
			throw new IOException("not a readable file '" + fileName + "'");
87
88
		// Try to read the beginning of the file.
89
90
		RandomAccessFile in = new RandomAccessFile(fileName, "r");
91
92
		byte b[] = new byte[10];
93
		int n = in.read(b, 0, 10);
94
95
		// System.err.printf( "[" );
96
		// for( int i=0; i<n; ++i )
97
		// {
98
		// System.err.printf( "%c", (char)b[i] );
99
		// }
100
		// System.err.printf( "]%n" );
101
102 1 1. sourceFor : removed call to java/io/RandomAccessFile::close → NO_COVERAGE
		in.close();
103
104
		// Surely match a DGS file, as DGS files are well done and have a
105
		// signature.
106
107 5 1. sourceFor : changed conditional boundary → NO_COVERAGE
2. sourceFor : negated conditional → NO_COVERAGE
3. sourceFor : negated conditional → NO_COVERAGE
4. sourceFor : negated conditional → NO_COVERAGE
5. sourceFor : negated conditional → NO_COVERAGE
		if (n >= 3 && b[0] == 'D' && b[1] == 'G' && b[2] == 'S') {
108 4 1. sourceFor : changed conditional boundary → NO_COVERAGE
2. sourceFor : negated conditional → NO_COVERAGE
3. sourceFor : negated conditional → NO_COVERAGE
4. sourceFor : negated conditional → NO_COVERAGE
			if (n >= 6 && b[3] == '0' && b[4] == '0') {
109 2 1. sourceFor : negated conditional → NO_COVERAGE
2. sourceFor : negated conditional → NO_COVERAGE
				if (b[5] == '1' || b[5] == '2') {
110 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
					return new FileSourceDGS1And2();
111 2 1. sourceFor : negated conditional → NO_COVERAGE
2. sourceFor : negated conditional → NO_COVERAGE
				} else if (b[5] == '3' || b[5] == '4') {
112 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
					return new FileSourceDGS();
113
				}
114
			}
115
		}
116
117
		// Maybe match a GML file as most GML files begin by the line "graph [",
118
		// but not sure, you may create a GML file that starts by a comment, an
119
		// empty line, with any kind of spaces, etc.
120
121 6 1. sourceFor : changed conditional boundary → NO_COVERAGE
2. sourceFor : negated conditional → NO_COVERAGE
3. sourceFor : negated conditional → NO_COVERAGE
4. sourceFor : negated conditional → NO_COVERAGE
5. sourceFor : negated conditional → NO_COVERAGE
6. sourceFor : negated conditional → NO_COVERAGE
		if (n >= 7 && b[0] == 'g' && b[1] == 'r' && b[2] == 'a' && b[3] == 'p'
122 3 1. sourceFor : negated conditional → NO_COVERAGE
2. sourceFor : negated conditional → NO_COVERAGE
3. sourceFor : negated conditional → NO_COVERAGE
				&& b[4] == 'h' && b[5] == ' ' && b[6] == '[') {
123 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new org.graphstream.stream.file.FileSourceGML();
124
		}
125
126 6 1. sourceFor : changed conditional boundary → NO_COVERAGE
2. sourceFor : negated conditional → NO_COVERAGE
3. sourceFor : negated conditional → NO_COVERAGE
4. sourceFor : negated conditional → NO_COVERAGE
5. sourceFor : negated conditional → NO_COVERAGE
6. sourceFor : negated conditional → NO_COVERAGE
		if (n >= 4 && b[0] == '(' && b[1] == 't' && b[2] == 'l' && b[3] == 'p')
127 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new FileSourceTLP();
128
129
		// The web reader.
130
131
		String flc = fileName.toLowerCase();
132
133
		// If we did not found anything, we try with the filename extension ...
134
135 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".dgs")) {
136 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new FileSourceDGS();
137
		}
138
139 2 1. sourceFor : negated conditional → NO_COVERAGE
2. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".gml") || flc.endsWith(".dgml")) {
140 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new org.graphstream.stream.file.FileSourceGML();
141
		}
142
143 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".net")) {
144 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new FileSourcePajek();
145
		}
146
147 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".chaco") || flc.endsWith(".graph")) {
148
			// return new GraphReaderChaco();
149
		}
150
151 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".dot")) {
152 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new org.graphstream.stream.file.FileSourceDOT();
153
		}
154
155 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".edge")) {
156 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new FileSourceEdge();
157
		}
158
159 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".lgl")) {
160 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new FileSourceLGL();
161
		}
162
163 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".ncol")) {
164 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new FileSourceNCol();
165
		}
166
167 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".tlp")) {
168 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new FileSourceTLP();
169
		}
170
171 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".xml")) {
172
			String root = getXMLRootElement(fileName);
173
174 1 1. sourceFor : negated conditional → NO_COVERAGE
			if (root.equalsIgnoreCase("gexf"))
175 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
				return new FileSourceGEXF();
176
			
177 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new FileSourceGraphML();
178
		}
179
180 1 1. sourceFor : negated conditional → NO_COVERAGE
		if (flc.endsWith(".gexf")) {
181 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return new FileSourceGEXF();
182
		}
183
184 1 1. sourceFor : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return null;
185
	}
186
187
	public static String getXMLRootElement(String fileName) throws IOException {
188
		FileReader stream = new FileReader(fileName);
189
		XMLEventReader reader;
190
		XMLEvent e;
191
		String root;
192
193
		try {
194
			reader = XMLInputFactory.newInstance().createXMLEventReader(stream);
195
196
			do {
197
				e = reader.nextEvent();
198 2 1. getXMLRootElement : negated conditional → NO_COVERAGE
2. getXMLRootElement : negated conditional → NO_COVERAGE
			} while (!e.isStartElement() && !e.isEndDocument());
199
200 1 1. getXMLRootElement : negated conditional → NO_COVERAGE
			if (e.isEndDocument())
201
				throw new IOException(
202
						"document ended before catching root element");
203
204
			root = e.asStartElement().getName().getLocalPart();
205 1 1. getXMLRootElement : removed call to javax/xml/stream/XMLEventReader::close → NO_COVERAGE
			reader.close();
206 1 1. getXMLRootElement : removed call to java/io/FileReader::close → NO_COVERAGE
			stream.close();
207
208 1 1. getXMLRootElement : mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::getXMLRootElement to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
			return root;
209
		} catch (XMLStreamException ex) {
210
			throw new IOException(ex);
211
		} catch (FactoryConfigurationError ex) {
212
			throw new IOException(ex);
213
		}
214
	}
215
}

Mutations

82

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

85

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

102

1.1
Location : sourceFor
Killed by : none
removed call to java/io/RandomAccessFile::close → NO_COVERAGE

107

1.1
Location : sourceFor
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

5.5
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

108

1.1
Location : sourceFor
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

109

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

110

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

111

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

112

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

121

1.1
Location : sourceFor
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

5.5
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

6.6
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

122

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

123

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

126

1.1
Location : sourceFor
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

4.4
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

5.5
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

6.6
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

127

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

135

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

136

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

139

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

140

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

143

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

144

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

147

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

151

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

152

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

155

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

156

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

159

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

160

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

163

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

164

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

167

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

168

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

171

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

174

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

175

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

177

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

180

1.1
Location : sourceFor
Killed by : none
negated conditional → NO_COVERAGE

181

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

184

1.1
Location : sourceFor
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::sourceFor to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

198

1.1
Location : getXMLRootElement
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : getXMLRootElement
Killed by : none
negated conditional → NO_COVERAGE

200

1.1
Location : getXMLRootElement
Killed by : none
negated conditional → NO_COVERAGE

205

1.1
Location : getXMLRootElement
Killed by : none
removed call to javax/xml/stream/XMLEventReader::close → NO_COVERAGE

206

1.1
Location : getXMLRootElement
Killed by : none
removed call to java/io/FileReader::close → NO_COVERAGE

208

1.1
Location : getXMLRootElement
Killed by : none
mutated return of Object value for org/graphstream/stream/file/FileSourceFactory::getXMLRootElement to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 0.33