-
Notifications
You must be signed in to change notification settings - Fork 160
/
Convert.java
156 lines (137 loc) · 4.31 KB
/
Convert.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
* Copyright (c) 2018 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/
package org.eclipse.rdf4j.console.command;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import org.eclipse.rdf4j.console.ConsoleIO;
import org.eclipse.rdf4j.console.ConsoleState;
import org.eclipse.rdf4j.console.Util;
import org.eclipse.rdf4j.console.setting.ConsoleSetting;
import org.eclipse.rdf4j.console.setting.WorkDir;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.RDFWriter;
import org.eclipse.rdf4j.rio.Rio;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Convert RDF file from one format to another
*
* @author Bart Hanssens
*/
public class Convert extends ConsoleCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(Convert.class);
@Override
public String getName() {
return "convert";
}
@Override
public String getHelpShort() {
return "Converts a file from one RDF format to another";
}
@Override
public String getHelpLong() {
return PrintHelp.USAGE + "convert <fileFrom> <fileTo> Converts a file from one RDF format to another\n";
}
@Override
public String[] usesSettings() {
return new String[] { WorkDir.NAME };
}
/**
* Constructor
*
* @param consoleIO
* @param state
* @param settings
*/
public Convert(ConsoleIO consoleIO, ConsoleState state, Map<String, ConsoleSetting> settings) {
super(consoleIO, state, settings);
}
@Override
public void execute(String... tokens) {
if (tokens.length < 3) {
consoleIO.writeln(getHelpLong());
return;
}
convert(tokens[1], tokens[2]);
}
/**
* Get working dir setting.
*
* @return path of working dir
*/
private Path getWorkDir() {
return ((WorkDir) settings.get(WorkDir.NAME)).get();
}
/**
* Convert a file
*
* @param fileFrom file name
* @param fileTo file name
*/
private void convert(String fileFrom, String fileTo) {
// check from
Path pathFrom = Util.getPath(fileFrom);
if (pathFrom == null) {
consoleIO.writeError("Invalid file name (from) " + fileFrom);
return;
}
if (Files.notExists(pathFrom)) {
consoleIO.writeError("File not found (from) " + fileFrom);
return;
}
Optional<RDFFormat> fmtFrom = Rio.getParserFormatForFileName(fileFrom);
if (!fmtFrom.isPresent()) {
consoleIO.writeError("No RDF parser for " + fileFrom);
return;
}
// check to
Path pathTo = Util.getPath(fileTo);
if (pathTo == null) {
consoleIO.writeError("Invalid file name (to) " + pathTo);
return;
}
Optional<RDFFormat> fmtTo = Rio.getWriterFormatForFileName(fileTo);
if (!fmtTo.isPresent()) {
consoleIO.writeError("No RDF writer for " + fileTo);
return;
}
if (Files.exists(pathTo)) {
try {
boolean overwrite = consoleIO.askProceed("File exists, continue ?", false);
if (!overwrite) {
consoleIO.writeln("Conversion aborted");
return;
}
} catch (IOException ioe) {
consoleIO.writeError("I/O error " + ioe.getMessage());
}
}
RDFParser parser = Rio.createParser(fmtFrom.get());
String baseURI = pathFrom.toUri().toString();
try (BufferedInputStream r = new BufferedInputStream(Files.newInputStream(pathFrom));
BufferedWriter w = Files.newBufferedWriter(pathTo)) {
RDFWriter writer = Rio.createWriter(fmtTo.get(), w);
parser.setRDFHandler(writer);
long startTime = System.nanoTime();
consoleIO.writeln("Converting file ...");
parser.parse(r, baseURI);
long diff = (System.nanoTime() - startTime) / 1_000_000;
consoleIO.writeln("Data has been written to file (" + diff + " ms)");
} catch (IOException | RDFParseException | RDFHandlerException e) {
consoleIO.writeError("Failed to convert data: " + e.getMessage());
}
}
}