|
1 | 1 | package cz.cvut.spipes.util; |
2 | 2 |
|
3 | 3 | import cz.cvut.spipes.riot.CustomLangs; |
| 4 | +import cz.cvut.spipes.riot.SPipesFormatter; |
4 | 5 | import org.apache.commons.codec.digest.DigestUtils; |
5 | 6 | import org.apache.jena.graph.Graph; |
6 | 7 | import org.apache.jena.graph.compose.MultiUnion; |
7 | 8 | import org.apache.jena.rdf.model.*; |
8 | | -import org.apache.jena.riot.*; |
| 9 | +import org.apache.jena.riot.Lang; |
| 10 | +import org.apache.jena.riot.RDFFormat; |
| 11 | +import org.apache.jena.riot.RDFWriter; |
| 12 | +import org.apache.jena.riot.system.PrefixMapFactory; |
9 | 13 | import org.apache.jena.util.FileUtils; |
10 | 14 | import org.apache.jena.util.iterator.ExtendedIterator; |
11 | 15 | import org.apache.jena.vocabulary.OWL; |
|
17 | 21 | import java.io.*; |
18 | 22 | import java.nio.file.Files; |
19 | 23 | import java.nio.file.Path; |
20 | | -import java.util.*; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Comparator; |
| 26 | +import java.util.List; |
21 | 27 | import java.util.stream.Stream; |
22 | 28 |
|
23 | 29 | public class JenaUtils { |
@@ -154,6 +160,29 @@ public static void writeScript(OutputStream outputStream, Model model) { |
154 | 160 | .output(outputStream); |
155 | 161 | } |
156 | 162 |
|
| 163 | + /** |
| 164 | + * Common method to write SPipes scripts to specified file using {@link Path}. |
| 165 | + * Preferred over {@link #writeScript(OutputStream, Model)} for modifying <b>existing</b> |
| 166 | + * SPipes scripts as it avoids file truncation if the model is invalid. |
| 167 | + * To write generic rdf data use {@link #write(OutputStream, Model)} instead. |
| 168 | + * |
| 169 | + * @param scriptPath path to write data to |
| 170 | + * @param model rdf data to write |
| 171 | + * @throws IllegalArgumentException if model is not valid SPipes script |
| 172 | + * @throws IOException if writing fails |
| 173 | + */ |
| 174 | + public static void writeScript(Path scriptPath, Model model) throws IOException { |
| 175 | + SPipesFormatter formatter = getScriptFormatter(model); |
| 176 | + try (OutputStream os = Files.newOutputStream(scriptPath)) { |
| 177 | + formatter.writeTo(os); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + public static SPipesFormatter getScriptFormatter(Model model) { |
| 182 | + Graph graph = model.getGraph(); |
| 183 | + return new SPipesFormatter(graph, PrefixMapFactory.create(graph.getPrefixMapping())); |
| 184 | + } |
| 185 | + |
157 | 186 | /** |
158 | 187 | * Hotfix for default namespace bug in Jena: default namespace of an import is returned when <code>model</code> |
159 | 188 | * does not declare it. |
|
0 commit comments