Replace file extension properly in FixExportedOntology
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / refactoring / FixExportedOntology.java
1 package org.simantics.graph.refactoring;
2
3 import java.io.BufferedInputStream;
4 import java.io.DataInputStream;
5 import java.io.InputStream;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.nio.file.Paths;
9 import java.nio.file.StandardOpenOption;
10
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.databoard.container.DataContainer;
13 import org.simantics.databoard.container.DataContainers;
14 import org.simantics.graph.representation.PrettyPrintTG;
15 import org.simantics.graph.representation.TransferableGraph1;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * @author Antti Villberg
21  * @since 1.24.0
22  */
23 public class FixExportedOntology {
24
25         private static final Logger LOGGER = LoggerFactory.getLogger(FixExportedOntology.class);
26
27         static TransferableGraph1 convertExportedSharedOntologyIntoBundleOntology(Path input, Path output) throws Exception {
28                 LOGGER.info("Converting exported shared ontology\n\t{}\nto bundle-compatible ontology\n\t{}", input.toString(), output.toString());
29                 try (InputStream is = new BufferedInputStream(Files.newInputStream(input), 128*1024)) {
30                         Binding tgBinding = TransferableGraph1.BINDING;
31                         DataContainer container = DataContainers.readFile(new DataInputStream(is), tgBinding); 
32                         TransferableGraph1 graph = (TransferableGraph1) container.content.getValue(tgBinding);
33
34                         GraphRefactoringUtils.fixOntologyExport(graph);
35                         container = TransferableGraphHasher.addHashToTG(container, graph);
36
37                         DataContainers.writeFile(output.toFile(), container);
38                         return graph;
39                 }
40         }
41
42         private static Path replaceExtension(Path p, String newExtension) {
43                 String newName = p.getFileName().toString();
44                 int lastDot = newName.lastIndexOf('.');
45                 if (lastDot > -1)
46                         newName = newName.substring(0, lastDot);
47                 return p.resolveSibling(newName + newExtension);
48         }
49
50         private static void createTg(Path input, Path output) throws Exception {
51                 convertExportedSharedOntologyIntoBundleOntology(input, output);
52         }
53
54         public static void createTg(Path input) throws Exception {
55                 createTg(input, replaceExtension(input, ".tg"));
56         }
57
58         public static void createTGAndPGraph(Path input) throws Exception {
59                 createTGAndPGraph(input, true);
60         }
61
62         public static void createTGAndPGraph(Path input, boolean writePGraph) throws Exception {
63                 TransferableGraph1 tg = convertExportedSharedOntologyIntoBundleOntology(input, replaceExtension(input, ".tg"));
64                 if (writePGraph) {
65                         String listing = PrettyPrintTG.print(tg, false);
66                         Files.write(replaceExtension(input, ".pgraph"), listing.getBytes(),StandardOpenOption.CREATE);
67                 }
68         }
69
70         public static void main(String[] args) throws Exception {
71                 if (args.length == 0) {
72                         System.out.println("Required arguments: <input .sharedLibrary file> [<output .tg file>]");
73                 } else if (args.length == 1) {
74                         Path input = Paths.get(args[0]);
75                         createTGAndPGraph(input);
76                 } else {
77                         convertExportedSharedOntologyIntoBundleOntology(Paths.get(args[0]), Paths.get(args[1]));
78                 }
79         }
80
81 }