bf0224103228a0838952cf553b9fea9b54d14ed6
[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.DataInput;
5 import java.io.DataInputStream;
6 import java.io.InputStream;
7 import java.nio.file.Files;
8 import java.nio.file.Path;
9 import java.nio.file.Paths;
10
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.databoard.binding.mutable.Variant;
13 import org.simantics.databoard.container.DataContainer;
14 import org.simantics.databoard.container.DataContainers;
15 import org.simantics.graph.representation.TransferableGraph1;
16
17 /**
18  * @author Antti Villberg
19  * @since 1.24.0
20  */
21 public class FixExportedOntology {
22         
23         static void convertExportedSharedOntologyIntoBundleOntology(Path input, Path output) throws Exception {
24                 System.out.format("Converting exported shared ontology%n\t" + input.toString() + "%nto bundle-compatible ontology%n\t" + output.toString());
25                 try (InputStream is = new BufferedInputStream(Files.newInputStream(input), 128*1024)) {
26                         DataInput dis = new DataInputStream(is);
27                         org.simantics.databoard.container.DataContainer container = 
28                                         DataContainers.readFile(dis); 
29                         Binding binding = TransferableGraph1.BINDING;
30                         TransferableGraph1 graph = (TransferableGraph1)container.content.getValue(binding);
31                         GraphRefactoringUtils.fixOntologyExport(graph);
32
33                         DataContainers.writeFile(output.toFile(), new DataContainer(
34                                         container.format, container.version,
35                                         container.metadata, new Variant(TransferableGraph1.BINDING, graph)));
36                 }
37         }
38
39         public static void main(String[] args) throws Exception {
40                 if (args.length < 1) {
41                         System.out.println("Required arguments: <input .sharedOntology file> [<output .tg file>]");
42                 } else if (args.length < 2) {
43                         Path input = Paths.get(args[0]);
44                         Path output = input.getParent().resolve(input.getName(input.getNameCount()-1) + ".fixed");
45                         convertExportedSharedOntologyIntoBundleOntology(input, output);
46                 } else {
47                         convertExportedSharedOntologyIntoBundleOntology(Paths.get(args[0]), Paths.get(args[1]));
48                 }
49         }
50
51 }