]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConverter.java
XML Schema converter
[simantics/interop.git] / org.simantics.xml.sax / src / org / simantics / xml / sax / SchemaConverter.java
1 package org.simantics.xml.sax;\r
2 \r
3 import java.io.File;\r
4 import java.io.FileInputStream;\r
5 import java.io.IOException;\r
6 import java.io.InputStream;\r
7 import java.util.Date;\r
8 \r
9 import javax.xml.bind.JAXBContext;\r
10 import javax.xml.bind.JAXBElement;\r
11 import javax.xml.bind.JAXBException;\r
12 import javax.xml.bind.Unmarshaller;\r
13 \r
14 import org.simantics.xml.sax.configuration.Configuration;\r
15 import org.w3._2001.xmlschema.Schema;\r
16 \r
17 /**\r
18  * This file is developed for XMpLant / Proteus schema conversion is not able to handle all XML Schema definitions.\r
19  * \r
20  * @author mlmarko\r
21  *\r
22  */\r
23 public class SchemaConverter {\r
24         \r
25         File schemaFile;\r
26         File conversionFile;\r
27         File ontologyFile;\r
28         File parserDir;\r
29         Schema schema;\r
30         Configuration configuration;\r
31         \r
32         String pluginName;\r
33         \r
34         private String[] header;\r
35         \r
36 \r
37         public void convertSchema(File schemaFile, File conversionFile, File outputPlugin) throws JAXBException, IOException {\r
38                 pluginName = outputPlugin.getName();\r
39                 String packageParts[] = pluginName.split("\\.");\r
40                 String outputLoc = outputPlugin.getAbsolutePath();\r
41                 outputLoc += "/src";\r
42                 for (String s : packageParts)\r
43                         outputLoc += "/"+s;\r
44                 String outputGraph = outputPlugin.getAbsolutePath();\r
45                 outputGraph += "/graph";\r
46                 outputGraph += "/" + schemaFile.getName().substring(0, schemaFile.getName().length()-4) +".pgraph";\r
47                 \r
48                 this.schemaFile = schemaFile;\r
49                 this.conversionFile = conversionFile;\r
50                 this.ontologyFile = new File(outputGraph);\r
51                 this.parserDir = new File(outputLoc);\r
52                 \r
53                 if (!ontologyFile.exists()) {\r
54                         ontologyFile.getParentFile().mkdirs();\r
55                         ontologyFile.createNewFile();\r
56                 }\r
57                 if (!parserDir.exists())\r
58                         parserDir.mkdirs();\r
59                 \r
60                 convert();\r
61                 \r
62         }\r
63         \r
64         private void convert() throws JAXBException, IOException {\r
65                 JAXBContext jc = JAXBContext.newInstance("org.w3._2001.xmlschema");\r
66                 Unmarshaller u = jc.createUnmarshaller();\r
67                 //u.setSchema(schema);\r
68         InputStream fileStream = new FileInputStream(schemaFile);\r
69                 schema = (Schema)u.unmarshal(fileStream);\r
70                 \r
71                 if (conversionFile != null) {\r
72                         jc = JAXBContext.newInstance("org.simantics.xml.sax.configuration");\r
73                         u = jc.createUnmarshaller();\r
74                         fileStream = new FileInputStream(conversionFile);\r
75                         configuration = (Configuration)((JAXBElement<?>)u.unmarshal(fileStream)).getValue();\r
76                 } else {\r
77                         configuration = new Configuration();\r
78                 }\r
79                 \r
80                 header = new String[4];\r
81                 header[0] = "Generated with org.simantics.xml.sax XML schema converter";\r
82                 header[1] = "";\r
83                 header[2] = "File " + schemaFile.getAbsolutePath();\r
84                 header[3] = "Date " + new Date().toString();\r
85                 \r
86                 \r
87                 OntologyGenerator ontologyGenerator = new OntologyGenerator(configuration);\r
88                 ontologyGenerator.createOntology(schema, this);\r
89                 ImporterGenerator importerGenerator = new ImporterGenerator(configuration);\r
90                 importerGenerator.createParser(schema, ontologyGenerator.getClassName(), this);\r
91         }\r
92         \r
93         public File getOntologyFile() {\r
94                 return ontologyFile;\r
95         }\r
96         public File getParserDir() {\r
97                 return parserDir;\r
98         }\r
99         \r
100         public Schema getSchema() {\r
101                 return schema;\r
102         }\r
103         \r
104         public File getSchemaFile() {\r
105                 return schemaFile;\r
106         }\r
107         \r
108         public String getPluginName() {\r
109                 return pluginName;\r
110         }\r
111         \r
112         public String[] getHeader() {\r
113                 return header;\r
114         }\r
115         \r
116         public Configuration getConfiguration() {\r
117                 return configuration;\r
118         }\r
119 \r
120 \r
121 }\r