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